Java Code Examples for java.lang.reflect.Field.isSynthetic()

The following are Jave code examples for showing how to use isSynthetic() of the java.lang.reflect.Field class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: drift   File: ReflectionHelper.java   View Source Code Vote up 6 votes
public static Collection<Field> findAnnotatedFields(Class<?> type, Class<? extends Annotation> annotation)
{
    List<Field> result = new ArrayList<>();

    // gather all publicly available fields
    // this returns everything, even if it's declared in a parent
    for (Field field : type.getFields()) {
        if (field.isSynthetic() || isStatic(field.getModifiers())) {
            continue;
        }

        if (field.isAnnotationPresent(annotation)) {
            result.add(field);
        }
    }

    return result;
}
 
Example 2
Project: OperatieBRP   File: ReflectionUtils.java   View Source Code Vote up 6 votes
/**
 * Geef alle velden van een klasse (inclusief velden uit super klassen, exclusief
 * java.lang.Object).
 * @param clazz klasse
 * @return lijst van alle velden
 */
public static Field[] getAllFields(final Class<?> clazz) {
    final java.util.List<Field> result = new ArrayList<>();

    Class<?> theClazz = clazz;
    while (theClazz != null && !Object.class.equals(theClazz)) {
        for (final Field field : theClazz.getDeclaredFields()) {
            if (!field.isSynthetic()) {
                result.add(field);
            }
        }
        theClazz = theClazz.getSuperclass();
    }

    return result.toArray(new Field[]{});
}
 
Example 3
Project: crnk-framework   File: ClassUtils.java   View Source Code Vote up 6 votes
/**
 * Tries to find a class fields. Supports inheritance and doesn't return synthetic fields.
 *
 * @param beanClass class to be searched for
 * @param fieldName field name
 * @return a list of found fields
 */
public static Field findClassField(Class<?> beanClass, String fieldName) {
	Class<?> currentClass = beanClass;
	while (currentClass != null && currentClass != Object.class) {
		for (Field field : currentClass.getDeclaredFields()) {
			if (field.isSynthetic()) {
				continue;
			}

			if (field.getName().equals(fieldName)) {
				return field;
			}
		}
		currentClass = currentClass.getSuperclass();
	}

	return null;
}
 
Example 4
Project: Reer   File: ModelRuleExtractor.java   View Source Code Vote up 5 votes
private void validateClass(Class<?> source, RuleSourceValidationProblemCollector problems) {
    int modifiers = source.getModifiers();

    if (Modifier.isInterface(modifiers)) {
        problems.add("Must be a class, not an interface");
    }

    if (source.getEnclosingClass() != null) {
        if (Modifier.isStatic(modifiers)) {
            if (Modifier.isPrivate(modifiers)) {
                problems.add("Class cannot be private");
            }
        } else {
            problems.add("Enclosed classes must be static and non private");
        }
    }

    Constructor<?>[] constructors = source.getDeclaredConstructors();
    for (Constructor<?> constructor : constructors) {
        if (constructor.getParameterTypes().length > 0) {
            problems.add("Cannot declare a constructor that takes arguments");
            break;
        }
    }

    Field[] fields = source.getDeclaredFields();
    for (Field field : fields) {
        int fieldModifiers = field.getModifiers();
        if (!field.isSynthetic() && !(Modifier.isStatic(fieldModifiers) && Modifier.isFinal(fieldModifiers))) {
            problems.add(field, "Fields must be static final.");
        }
    }
}
 
Example 5
Project: Reer   File: DefaultStructBindingsStore.java   View Source Code Vote up 5 votes
private static void ensureNoInstanceScopedFields(StructBindingValidationProblemCollector problems, Class<?> typeClass) {
    List<Field> declaredFields = Arrays.asList(typeClass.getDeclaredFields());
    for (Field field : declaredFields) {
        int fieldModifiers = field.getModifiers();
        if (!field.isSynthetic() && !(Modifier.isStatic(fieldModifiers) && Modifier.isFinal(fieldModifiers))) {
            problems.add(field, "Fields must be static final.");
        }
    }
}
 
Example 6
Project: nitrite-database   File: ReflectionUtils.java   View Source Code Vote up 5 votes
private static void filterSynthetics(List<Field> fields) {
    if (fields == null || fields.isEmpty()) return;
    Iterator<Field> iterator = fields.iterator();
    while (iterator.hasNext()) {
        Field f = iterator.next();
        if (f.isSynthetic()) iterator.remove();
    }
}
 
Example 7
Project: guava-mock   File: HttpHeadersTest.java   View Source Code Vote up 5 votes
static ImmutableSet<Field> relevantFields(Class<?> cls) {
  ImmutableSet.Builder<Field> builder = ImmutableSet.builder();
  for (Field field : cls.getDeclaredFields()) {
    /*
     * Coverage mode generates synthetic fields.  If we ever add private
     * fields, they will cause similar problems, and we may want to switch
     * this check to isAccessible().
     */
    if (!field.isSynthetic() && field.getType() == String.class) {
      builder.add(field);
    }
  }
  return builder.build();
}
 
Example 8
Project: gremlin-ogm   File: Fields.java   View Source Code Vote up 5 votes
/**
 * Is this field to be hidden from the graph?
 */
private static boolean isHiddenField(Field field) {
  if (field.isSynthetic() || has(field, Hidden.class) || isStatic(field.getModifiers())) {
    return true;
  }
  return SYSTEM_PROPERTIES.contains(propertyKey(field)) ||
      SYSTEM_CLASSES.contains(field.getDeclaringClass()) ||
      isFunctional(field.getType());
}
 
Example 9
Project: ProjectAres   File: DocumentRegistry.java   View Source Code Vote up 5 votes
private void registerField(Map<String, Getter> getters, Map<String, Setter> setters, Field field) {
    if(Modifier.isTransient(field.getModifiers()) ||
       Modifier.isStatic(field.getModifiers()) ||
       field.isSynthetic() ||
       field.isEnumConstant()) return;

    final String name = serializedName(field);
    final boolean gettable = !getters.containsKey(name);
    final boolean settable = !setters.containsKey(name);

    if(gettable || settable) {
        if(logger.isLoggable(Level.FINE)) {
            String access;
            if(gettable && settable) {
                access = "get/set";
            } else if(gettable) {
                access = "get";
            } else {
                access = "set";
            }
            logger.fine("  " + name + " -- " + access + " --> " + field);
        }

        if(gettable) {
            getters.put(name, new FieldGetter(this, field));
        }

        if(settable) {
            setters.put(name, new FieldSetter(this, field));
        }
    }
}
 
Example 10
Project: boohee_v5.6   File: Excluder.java   View Source Code Vote up 5 votes
public boolean excludeField(Field field, boolean serialize) {
    if ((this.modifiers & field.getModifiers()) != 0) {
        return true;
    }
    if (this.version != IGNORE_VERSIONS && !isValidVersion((Since) field.getAnnotation(Since
            .class), (Until) field.getAnnotation(Until.class))) {
        return true;
    }
    if (field.isSynthetic()) {
        return true;
    }
    if (this.requireExpose) {
        Expose annotation = (Expose) field.getAnnotation(Expose.class);
        if (annotation == null || (serialize ? !annotation.serialize() : !annotation
                .deserialize())) {
            return true;
        }
    }
    if (!this.serializeInnerClasses && isInnerClass(field.getType())) {
        return true;
    }
    if (isAnonymousOrLocal(field.getType())) {
        return true;
    }
    List<ExclusionStrategy> list = serialize ? this.serializationStrategies : this
            .deserializationStrategies;
    if (!list.isEmpty()) {
        FieldAttributes fieldAttributes = new FieldAttributes(field);
        for (ExclusionStrategy exclusionStrategy : list) {
            if (exclusionStrategy.shouldSkipField(fieldAttributes)) {
                return true;
            }
        }
    }
    return false;
}
 
Example 11
Project: ditb   File: SplitLogCounters.java   View Source Code Vote up 5 votes
public static void resetCounters() throws Exception {
  Class<?> cl = SplitLogCounters.class;
  for (Field fld : cl.getDeclaredFields()) {
    /* Guard against source instrumentation. */
    if ((!fld.isSynthetic()) && (AtomicLong.class.isAssignableFrom(fld.getType()))) {
      ((AtomicLong)fld.get(null)).set(0);
    }
  }
}
 
Example 12
Project: jsf-sdk   File: ReflectUtils.java   View Source Code Vote up 4 votes
protected static boolean isPublicInstanceField(Field field) {
    return Modifier.isPublic(field.getModifiers())
            && !Modifier.isStatic(field.getModifiers())
            && !Modifier.isFinal(field.getModifiers())
            && !field.isSynthetic();
}
 
Example 13
Project: dubbox-hystrix   File: ReflectUtils.java   View Source Code Vote up 4 votes
public static boolean isPublicInstanceField(Field field) {
    return Modifier.isPublic(field.getModifiers())
        && !Modifier.isStatic(field.getModifiers())
        && !Modifier.isFinal(field.getModifiers())
        && !field.isSynthetic();
}
 
Example 14
Project: manifold   File: IJsonIO.java   View Source Code Vote up 4 votes
static void saveFields( IJsonIO obj, Class cls, Bindings bindings )
{
  Class superclass = cls.getSuperclass();
  if( superclass != null )
  {
    saveFields( obj, superclass, bindings );
  }

  Field[] fields = cls.getDeclaredFields();
  for( Field f : fields )
  {
    if( f.isSynthetic() || Modifier.isStatic( f.getModifiers() ) || Modifier.isTransient( f.getModifiers() ) )
    {
      continue;
    }
    f.setAccessible( true );
    try
    {
      Object value = f.get( obj );
      if( isSimpleType( value ) )
      {
        bindings.put( f.getName(), value );
      }
      else if( value instanceof Enum )
      {
        bindings.put( f.getName(), ((Enum)value).name() );
      }
      else if( value instanceof Boolean )
      {
        bindings.put( f.getName(), (Boolean)value ? 1 : 0 );
      }
      else if( value instanceof List )
      {
        if( !((List)value).isEmpty() )
        {
          if( isSimpleType( ((List)value).get( 0 ) ) )
          {
            bindings.put( f.getName(), value );
          }
          else
          {
            writeList( f.getName(), (List)value, bindings );
          }
        }
        else
        {
          bindings.put( f.getName(), value );
        }
      }
      else if( value instanceof IJsonIO )
      {
        write( f.getName(), (IJsonIO)value, bindings );
      }
      else
      {
        throw new UnsupportedOperationException( "Type: " + value.getClass() + " does not implement " + IJsonIO.class.getName() );
      }
    }
    catch( IllegalAccessException e )
    {
      throw new RuntimeException( e );
    }
  }
}
 
Example 15
Project: odoo-work   File: Excluder.java   View Source Code Vote up 4 votes
public boolean excludeField(Field field, boolean serialize) {
  if ((modifiers & field.getModifiers()) != 0) {
    return true;
  }

  if (version != Excluder.IGNORE_VERSIONS
      && !isValidVersion(field.getAnnotation(Since.class), field.getAnnotation(Until.class))) {
    return true;
  }

  if (field.isSynthetic()) {
    return true;
  }

  if (requireExpose) {
    Expose annotation = field.getAnnotation(Expose.class);
    if (annotation == null || (serialize ? !annotation.serialize() : !annotation.deserialize())) {
      return true;
    }
  }

  if (!serializeInnerClasses && isInnerClass(field.getType())) {
    return true;
  }

  if (isAnonymousOrLocal(field.getType())) {
    return true;
  }

  List<ExclusionStrategy> list = serialize ? serializationStrategies : deserializationStrategies;
  if (!list.isEmpty()) {
    FieldAttributes fieldAttributes = new FieldAttributes(field);
    for (ExclusionStrategy exclusionStrategy : list) {
      if (exclusionStrategy.shouldSkipField(fieldAttributes)) {
        return true;
      }
    }
  }

  return false;
}
 
Example 16
Project: lams   File: Excluder.java   View Source Code Vote up 4 votes
public boolean excludeField(Field field, boolean serialize) {
  if ((modifiers & field.getModifiers()) != 0) {
    return true;
  }

  if (version != Excluder.IGNORE_VERSIONS
      && !isValidVersion(field.getAnnotation(Since.class), field.getAnnotation(Until.class))) {
    return true;
  }

  if (field.isSynthetic()) {
    return true;
  }

  if (requireExpose) {
    Expose annotation = field.getAnnotation(Expose.class);
    if (annotation == null || (serialize ? !annotation.serialize() : !annotation.deserialize())) {
      return true;
    }
  }

  if (!serializeInnerClasses && isInnerClass(field.getType())) {
    return true;
  }

  if (isAnonymousOrLocal(field.getType())) {
    return true;
  }

  List<ExclusionStrategy> list = serialize ? serializationStrategies : deserializationStrategies;
  if (!list.isEmpty()) {
    FieldAttributes fieldAttributes = new FieldAttributes(field);
    for (ExclusionStrategy exclusionStrategy : list) {
      if (exclusionStrategy.shouldSkipField(fieldAttributes)) {
        return true;
      }
    }
  }

  return false;
}
 
Example 17
Project: azeroth   File: ReflectUtils.java   View Source Code Vote up 4 votes
public static boolean isPublicInstanceField(Field field) {
    return Modifier.isPublic(field.getModifiers())
            && !Modifier.isStatic(field.getModifiers())
            && !Modifier.isFinal(field.getModifiers())
            && !field.isSynthetic();
}
 
Example 18
Project: aws-sdk-java-v2   File: ConfigurationBuilderTest.java   View Source Code Vote up 4 votes
private void assertConfigurationClassIsValid(Class<?> configurationClass) throws Exception {
    // Builders should be instantiable from the configuration class
    Method createBuilderMethod = configurationClass.getMethod("builder");
    Object builder = createBuilderMethod.invoke(null);

    // Builders should implement the configuration class's builder interface
    Class<?> builderInterface = Class.forName(configurationClass.getName() + "$Builder");
    assertThat(builder).isInstanceOf(builderInterface);

    Class<?> builderClass = builder.getClass();
    Method[] builderMethods = builderClass.getDeclaredMethods();

    // Builder's build methods should return the configuration object
    Optional<Method> buildMethod = Arrays.stream(builderMethods).filter(m -> m.getName().equals("build")).findFirst();
    assertThat(buildMethod).isPresent();
    Object builtObject = buildMethod.get().invoke(builder);
    assertThat(builtObject).isInstanceOf(configurationClass);

    // Analyze the builder for compliance with the bean specification
    BeanInfo builderBeanInfo = Introspector.getBeanInfo(builderClass);

    Map<String, PropertyDescriptor> builderProperties = Arrays.stream(builderBeanInfo.getPropertyDescriptors())
            .collect(toMap(PropertyDescriptor::getName, p -> p));

    // Validate method names
    for (Field field : configurationClass.getFields()) {
        // Ignore generated fields (eg. by Jacoco)
        if (field.isSynthetic()) {
            continue;
        }

        String builderPropertyName = builderClass.getSimpleName() + "'s " + field.getName() + " property";
        PropertyDescriptor builderProperty = builderProperties.get(field.getName());

        // Builders should have a bean-style write method for each field
        assertThat(builderProperty).as(builderPropertyName).isNotNull();
        assertThat(builderProperty.getReadMethod()).as(builderPropertyName + "'s read method").isNull();
        assertThat(builderProperty.getWriteMethod()).as(builderPropertyName + "'s write method").isNotNull();

        // Builders should have a fluent write method for each field
        Arrays.stream(builderMethods)
              .filter(builderMethod -> matchesSignature(field.getName(), builderProperty, builderMethod))
              .findAny()
              .orElseThrow(() -> new AssertionError(builderClass + " can't write " + field.getName()));
    }
}
 
Example 19
Project: AndroidBasicLibs   File: FieldUtil.java   View Source Code Vote up 2 votes
/**
 * 是静态常量或者内部结构属性
 *
 * @param f
 * @return
 */
public static boolean isInvalid(Field f) {
    return (Modifier.isStatic(f.getModifiers()) && Modifier.isFinal(f.getModifiers())) || f.isSynthetic();
}
 
Example 20
Project: treasure   File: FieldUtil.java   View Source Code Vote up 2 votes
/**
 * 是静态常量或者内部结构属性
 *
 * @param f
 * @return
 */
public static boolean isInvalid(Field f) {
    return (Modifier.isStatic(f.getModifiers()) && Modifier.isFinal(f.getModifiers())) || f.isSynthetic();
}