Example usage for java.lang.reflect Field isSynthetic

List of usage examples for java.lang.reflect Field isSynthetic

Introduction

In this page you can find the example usage for java.lang.reflect Field isSynthetic.

Prototype

public boolean isSynthetic() 

Source Link

Document

Returns true if this field is a synthetic field; returns false otherwise.

Usage

From source file:AccessibleFieldIterator.java

protected void addMoreFields() {
    boolean needMoreFields = true;
    while (needMoreFields) {

        for (Field field : this.currentClass.getDeclaredFields()) {

            if (!(field.isSynthetic() || Modifier.isPrivate(field.getModifiers())
                    || this.visibleFieldNames.contains(field.getName()))) {
                try {
                    field.setAccessible(true);
                    this.fieldQueueIterator.add(field);
                    this.visibleFieldNames.add(field.getName());
                    needMoreFields = false;
                } catch (SecurityException e) {
                    // ignore fields that cannot be set accessible
                }//from w  ww.j  ava 2 s  .c o m
            }
        }

        if (needMoreFields) {
            // found no accessible fields in the current class
            this.currentClass = this.currentClass.getSuperclass();
            if (this.currentClass == null) {
                // no more fields
                return;
            }
        }
    }
}

From source file:org.unitils.core.reflect.ClassWrapper.java

protected void addFields(Class<?> clazz, boolean staticFields, List<FieldWrapper> fields) {
    if (Object.class.equals(clazz)) {
        return;//from   w w  w.  jav a  2  s.  c om
    }
    Field[] classFields = clazz.getDeclaredFields();
    for (Field field : classFields) {
        // exclude static and special fields
        if (!field.isSynthetic() && staticFields == isStatic(field.getModifiers())) {
            FieldWrapper fieldWrapper = new FieldWrapper(field);
            fields.add(fieldWrapper);
        }
    }
    addFields(clazz.getSuperclass(), staticFields, fields);
}

From source file:com.threewks.thundr.bigmetrics.service.BigMetricsServiceImpl.java

protected Map<Field, FieldProcessor<?>> findFieldProcessors(Class<?> eventClass) {
    List<Field> fields = Arrays.asList(ReflectUtil.getSupportedFields(eventClass, Object.class));
    Map<Field, FieldProcessor<?>> processors = new LinkedHashMap<>();
    for (Field field : fields) {
        if (!field.isSynthetic() && !Modifier.isTransient(field.getModifiers())
                && !field.isAnnotationPresent(Ignore.class)) {
            field.setAccessible(true);// w w w. j  a v  a 2  s. c o m
            FieldProcessor<?> processor = determineProcessor(field);
            processors.put(field, processor);
        }
    }
    return processors;
}

From source file:org.codehaus.griffon.commons.ClassPropertyFetcher.java

private void init() {
    FieldCallback fieldCallback = new ReflectionUtils.FieldCallback() {
        public void doWith(Field field) {
            if (field.isSynthetic())
                return;
            final int modifiers = field.getModifiers();
            if (!Modifier.isPublic(modifiers))
                return;

            final String name = field.getName();
            if (name.indexOf('$') == -1) {
                boolean staticField = Modifier.isStatic(modifiers);
                if (staticField) {
                    staticFetchers.put(name, new FieldReaderFetcher(field, staticField));
                } else {
                    instanceFetchers.put(name, new FieldReaderFetcher(field, staticField));
                }//from w ww  .  j  av a 2 s  .c  o m
            }
        }
    };

    MethodCallback methodCallback = new ReflectionUtils.MethodCallback() {
        public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
            if (method.isSynthetic())
                return;
            if (!Modifier.isPublic(method.getModifiers()))
                return;
            if (Modifier.isStatic(method.getModifiers()) && method.getReturnType() != Void.class) {
                if (method.getParameterTypes().length == 0) {
                    String name = method.getName();
                    if (name.indexOf('$') == -1) {
                        if (name.length() > 3 && name.startsWith("get")
                                && Character.isUpperCase(name.charAt(3))) {
                            name = name.substring(3);
                        } else if (name.length() > 2 && name.startsWith("is")
                                && Character.isUpperCase(name.charAt(2))
                                && (method.getReturnType() == Boolean.class
                                        || method.getReturnType() == boolean.class)) {
                            name = name.substring(2);
                        }
                        PropertyFetcher fetcher = new GetterPropertyFetcher(method, true);
                        staticFetchers.put(name, fetcher);
                        staticFetchers.put(StringUtils.uncapitalize(name), fetcher);
                    }
                }
            }
        }
    };

    List<Class<?>> allClasses = resolveAllClasses(clazz);
    for (Class<?> c : allClasses) {
        Field[] fields = c.getDeclaredFields();
        for (Field field : fields) {
            try {
                fieldCallback.doWith(field);
            } catch (IllegalAccessException ex) {
                throw new IllegalStateException(
                        "Shouldn't be illegal to access field '" + field.getName() + "': " + ex);
            }
        }
        Method[] methods = c.getDeclaredMethods();
        for (Method method : methods) {
            try {
                methodCallback.doWith(method);
            } catch (IllegalAccessException ex) {
                throw new IllegalStateException(
                        "Shouldn't be illegal to access method '" + method.getName() + "': " + ex);
            }
        }
    }

    propertyDescriptors = BeanUtils.getPropertyDescriptors(clazz);
    for (PropertyDescriptor desc : propertyDescriptors) {
        Method readMethod = desc.getReadMethod();
        if (readMethod != null) {
            boolean staticReadMethod = Modifier.isStatic(readMethod.getModifiers());
            if (staticReadMethod) {
                staticFetchers.put(desc.getName(), new GetterPropertyFetcher(readMethod, staticReadMethod));
            } else {
                instanceFetchers.put(desc.getName(), new GetterPropertyFetcher(readMethod, staticReadMethod));
            }
        }
    }
}

From source file:org.springframework.data.keyvalue.riak.RiakMappedClass.java

private void initFields() {
    ReflectionUtils.doWithFields(this.clazz, new FieldCallback() {

        @Override/*from   w ww.j ava  2 s .  c o m*/
        public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
            if (!field.isAccessible())
                ReflectionUtils.makeAccessible(field);

            if (field.isAnnotationPresent(Transient.class) || field.isSynthetic()
                    || field.getModifiers() == Modifier.FINAL || field.getModifiers() == Modifier.TRANSIENT) {
                return;
            }

            // Field can only have one of these, if more than one throw an
            // error
            List<Annotation> annots = org.springframework.data.keyvalue.riak.util.AnnotationUtils
                    .getFoundAnnotation(Id.class, Column.class, Embedded.class, Version.class, ManyToOne.class,
                            OneToMany.class, Basic.class);

            // Probably allow auto generation at some point, but for now
            // have to add one of the annotations
            if (annots.size() > 1)
                throw new IllegalArgumentException(String.format(
                        "The field %s must have only one of the following annotations: "
                                + "@Id, @Basic, @Column, @Embedded, @Version, @ManyToOne, @OneToMany",
                        field.getName()));

            Annotation annot = annots.get(0);

            if (annot.annotationType().equals(Id.class))
                RiakMappedClass.this.id = field;

            // Create a new mapped field here and then add to a list of
            // property MappedFields
            propertyFields.add(new RiakMappedField(field, annot));
        }
    });
    Map<Class<? extends Annotation>, Annotation> fieldAnnotMap = new HashMap<Class<? extends Annotation>, Annotation>();
    for (Class<? extends Annotation> a : entityAnnotations) {
        Target target = a.getAnnotation(Target.class);
        if (target != null && (ArrayUtils.contains(target.value(), ElementType.FIELD)
                || ArrayUtils.contains(target.value(), ElementType.METHOD))) {
            Annotation fieldAnnot;
            if ((fieldAnnot = this.clazz.getAnnotation(a)) != null) {
                fieldAnnotMap.put(a, fieldAnnot);
            }
        }
    }
}

From source file:org.grails.datastore.mapping.reflect.ClassPropertyFetcher.java

private void processField(Field field) {
    if (field.isSynthetic()) {
        return;//w  ww.j a  va2  s . c o m
    }
    final int modifiers = field.getModifiers();
    final String name = field.getName();
    if (!Modifier.isPublic(modifiers)) {
        if (name.indexOf('$') == -1) {
            fieldsByName.put(name, field);
        }
    } else {
        if (name.indexOf('$') == -1) {
            boolean staticField = Modifier.isStatic(modifiers);
            if (staticField) {
                List<PropertyFetcher> propertyFetchers = staticFetchers.get(name);
                if (propertyFetchers == null) {
                    staticFetchers.put(name, propertyFetchers = new ArrayList<PropertyFetcher>());
                }
                propertyFetchers.add(new FieldReaderFetcher(field, staticField));
            } else {
                instanceFetchers.put(name, new FieldReaderFetcher(field, staticField));
            }
        }
    }
}

From source file:org.codehaus.groovy.grails.commons.ClassPropertyFetcher.java

private void init() {
    FieldCallback fieldCallback = new ReflectionUtils.FieldCallback() {
        public void doWith(Field field) {
            if (field.isSynthetic()) {
                return;
            }//  w  w w  .j  av  a2 s.  c  om
            final int modifiers = field.getModifiers();
            if (!Modifier.isPublic(modifiers)) {
                return;
            }

            final String name = field.getName();
            if (name.indexOf('$') == -1) {
                boolean staticField = Modifier.isStatic(modifiers);
                if (staticField) {
                    staticFetchers.put(name, new FieldReaderFetcher(field, staticField));
                } else {
                    instanceFetchers.put(name, new FieldReaderFetcher(field, staticField));
                }
            }
        }
    };

    MethodCallback methodCallback = new ReflectionUtils.MethodCallback() {
        public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
            if (method.isSynthetic()) {
                return;
            }
            if (!Modifier.isPublic(method.getModifiers())) {
                return;
            }
            if (Modifier.isStatic(method.getModifiers()) && method.getReturnType() != Void.class) {
                if (method.getParameterTypes().length == 0) {
                    String name = method.getName();
                    if (name.indexOf('$') == -1) {
                        if (name.length() > 3 && name.startsWith("get")
                                && Character.isUpperCase(name.charAt(3))) {
                            name = name.substring(3);
                        } else if (name.length() > 2 && name.startsWith("is")
                                && Character.isUpperCase(name.charAt(2))
                                && (method.getReturnType() == Boolean.class
                                        || method.getReturnType() == boolean.class)) {
                            name = name.substring(2);
                        }
                        PropertyFetcher fetcher = new GetterPropertyFetcher(method, true);
                        staticFetchers.put(name, fetcher);
                        staticFetchers.put(StringUtils.uncapitalize(name), fetcher);
                    }
                }
            }
        }
    };

    List<Class<?>> allClasses = resolveAllClasses(clazz);
    for (Class<?> c : allClasses) {
        Field[] fields = c.getDeclaredFields();
        for (Field field : fields) {
            try {
                fieldCallback.doWith(field);
            } catch (IllegalAccessException ex) {
                throw new IllegalStateException(
                        "Shouldn't be illegal to access field '" + field.getName() + "': " + ex);
            }
        }
        Method[] methods = c.getDeclaredMethods();
        for (Method method : methods) {
            try {
                methodCallback.doWith(method);
            } catch (IllegalAccessException ex) {
                throw new IllegalStateException(
                        "Shouldn't be illegal to access method '" + method.getName() + "': " + ex);
            }
        }
    }

    propertyDescriptors = BeanUtils.getPropertyDescriptors(clazz);
    for (PropertyDescriptor desc : propertyDescriptors) {
        Method readMethod = desc.getReadMethod();
        if (readMethod != null) {
            boolean staticReadMethod = Modifier.isStatic(readMethod.getModifiers());
            if (staticReadMethod) {
                staticFetchers.put(desc.getName(), new GetterPropertyFetcher(readMethod, staticReadMethod));
            } else {
                instanceFetchers.put(desc.getName(), new GetterPropertyFetcher(readMethod, staticReadMethod));
            }
        }
    }
}

From source file:hu.bme.mit.sette.common.model.snippet.SnippetContainer.java

/**
 * Validates the fields of the class./*from   w w  w  . j av a 2  s  .c  o m*/
 *
 * @param validator
 *            a validator
 */
private void validateFields(final AbstractValidator<?> validator) {
    // check: only constant ("public static final") or synthetic fields
    for (Field field : javaClass.getDeclaredFields()) {
        if (field.isSynthetic()) {
            // skip for synthetic fields (e.g. switch for enum generates
            // synthetic methods and fields)
            continue;
        }

        FieldValidator v = new FieldValidator(field);
        v.withModifiers(Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL);
        v.withoutModifiers(Modifier.SYNCHRONIZED | Modifier.TRANSIENT | Modifier.VOLATILE);

        validator.addChildIfInvalid(v);
    }
}

From source file:org.jtester.module.utils.ObjectFormatter.java

/**
 * Formats the field values of the given object.
 *
 * @param object       The object, not null
 * @param clazz        The class for which to format the fields, not null
 * @param currentDepth The current recursion depth
 * @param result       The builder to append the result to, not null
 *//*from w ww  .ja  va 2s .co m*/
protected void formatFields(Object object, Class<?> clazz, int currentDepth, StringBuilder result) {
    Field[] fields = clazz.getDeclaredFields();
    AccessibleObject.setAccessible(fields, true);

    for (int i = 0; i < fields.length; i++) {
        // skip transient and static fields
        Field field = fields[i];
        if (isTransient(field.getModifiers()) || isStatic(field.getModifiers()) || field.isSynthetic()) {
            continue;
        }
        try {
            if (i > 0) {
                result.append(", ");
            }
            result.append(field.getName());
            result.append("=");
            formatImpl(field.get(object), currentDepth + 1, result);

        } catch (IllegalAccessException e) {
            // this can't happen. Would get a Security exception instead
            // throw a runtime exception in case the impossible happens.
            throw new InternalError("Unexpected IllegalAccessException");
        }
    }

    // format fields declared in superclass
    Class<?> superclazz = clazz.getSuperclass();
    while (superclazz != null && !superclazz.getName().startsWith("java.lang")) {
        formatFields(object, superclazz, currentDepth, result);
        superclazz = superclazz.getSuperclass();
    }
}

From source file:org.jtester.hamcrest.matcher.property.report.ObjectFormatter.java

/**
 * Formats the field values of the given object.
 * //from www  .jav a 2s.  com
 * @param object
 *            The object, not null
 * @param clazz
 *            The class for which to format the fields, not null
 * @param currentDepth
 *            The current recursion depth
 * @param result
 *            The builder to append the result to, not null
 */
protected void formatFields(Object object, Class clazz, int currentDepth, StringBuilder result) {
    Field[] fields = clazz.getDeclaredFields();
    AccessibleObject.setAccessible(fields, true);

    for (int i = 0; i < fields.length; i++) {
        // skip transient and static fields
        Field field = fields[i];
        if (isTransient(field.getModifiers()) || isStatic(field.getModifiers()) || field.isSynthetic()) {
            continue;
        }
        try {
            if (i > 0) {
                result.append(", ");
            }
            result.append(field.getName());
            result.append("=");
            formatImpl(field.get(object), currentDepth + 1, result);

        } catch (IllegalAccessException e) {
            // this can't happen. Would get a Security exception instead
            // throw a runtime exception in case the impossible happens.
            throw new InternalError("Unexpected IllegalAccessException");
        }
    }

    // format fields declared in superclass
    Class superclazz = clazz.getSuperclass();
    while (superclazz != null && !superclazz.getName().startsWith("java.lang")) {
        formatFields(object, superclazz, currentDepth, result);
        superclazz = superclazz.getSuperclass();
    }
}