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:com.github.venkateshamurthy.designpatterns.builders.FluentBuilders.java

/**
 * Gets class type of field parameters in a class for which setters are to
 * be found// ww  w  . ja va2s  .co m
 * 
 * @param thisPojoClass
 * @param ctClass
 * @param ctMethodSet
 *            can be a subset of setters for fields in this class
 * @return set of classes representing field types
 * @throws NotFoundException
 */
@SuppressWarnings("serial")
private Set<Class<?>> getPropertyClassTypes(final Class<?> thisPojoClass, final CtClass ctClass,
        final Set<CtMethod> ctMethodSet) throws NotFoundException {
    Set<Class<?>> set = new LinkedHashSet<>();
    try {
        final Object bean = thisPojoClass.newInstance(); // create an
                                                         // instance
        for (Field field : thisPojoClass.getDeclaredFields()) {
            if (field.isSynthetic()) {
                LOGGER.warning(field.getName() + " is syntheticlly added, so ignoring");
                continue;
            }
            PropertyDescriptor pd = PropertyUtils.getPropertyDescriptor(bean, field.getName());

            assert pd != null;
            if (pd != null) {
                if (pd.getPropertyType() != null) {
                    set.add(pd.getPropertyType()); // irrespective of
                                                   // CtMethod just add
                    final Method mutator = pd.getWriteMethod();
                    if (mutator != null && ctMethodSet.add(ctClass.getDeclaredMethod(mutator.getName()))) {
                        LOGGER.info(mutator.getName() + " is ADDED");
                    }
                }
            } else {
                LOGGER.warning("Unable to get Proprty (Field's class) Type for:" + field.getName());
            }

        }
    } catch (InstantiationException | IllegalAccessException | InvocationTargetException
            | NoSuchMethodException e) {
        throw new NotFoundException("Exception Not Found:", e);
    }
    return set;
}

From source file:com.haulmont.chile.core.loader.ChileAnnotationsLoader.java

protected void initProperties(Class<?> clazz, MetaClassImpl metaClass,
        Collection<MetadataObjectInitTask> tasks) {
    if (!metaClass.getOwnProperties().isEmpty())
        return;//from   www. jav  a 2 s  .co m

    // load collection properties after non-collection in order to have all inverse properties loaded up
    ArrayList<Field> collectionProps = new ArrayList<>();
    for (Field field : clazz.getDeclaredFields()) {
        if (field.isSynthetic())
            continue;

        final String fieldName = field.getName();

        if (isMetaPropertyField(field)) {
            MetaPropertyImpl property = (MetaPropertyImpl) metaClass.getProperty(fieldName);
            if (property == null) {
                MetadataObjectInfo<MetaProperty> info;
                if (isCollection(field) || isMap(field)) {
                    collectionProps.add(field);
                } else {
                    info = loadProperty(metaClass, field);
                    tasks.addAll(info.getTasks());
                    MetaProperty metaProperty = info.getObject();
                    onPropertyLoaded(metaProperty, field);
                }
            } else {
                log.warn("Field " + clazz.getSimpleName() + "." + field.getName()
                        + " is not included in metadata because property " + property + " already exists");
            }
        }
    }

    for (Field f : collectionProps) {
        MetadataObjectInfo<MetaProperty> info = loadCollectionProperty(metaClass, f);
        tasks.addAll(info.getTasks());
        MetaProperty metaProperty = info.getObject();
        onPropertyLoaded(metaProperty, f);
    }

    for (Method method : clazz.getDeclaredMethods()) {
        if (method.isSynthetic())
            continue;

        String methodName = method.getName();
        if (!methodName.startsWith("get") || method.getReturnType() == void.class)
            continue;

        if (isMetaPropertyMethod(method)) {
            String name = StringUtils.uncapitalize(methodName.substring(3));

            MetaPropertyImpl property = (MetaPropertyImpl) metaClass.getProperty(name);
            if (property == null) {
                MetadataObjectInfo<MetaProperty> info;
                if (isCollection(method) || isMap(method)) {
                    throw new UnsupportedOperationException(
                            String.format("Method-based property %s.%s doesn't support collections and maps",
                                    clazz.getSimpleName(), method.getName()));
                } else if (method.getParameterCount() != 0) {
                    throw new UnsupportedOperationException(
                            String.format("Method-based property %s.%s doesn't support arguments",
                                    clazz.getSimpleName(), method.getName()));
                } else {
                    info = loadProperty(metaClass, method, name);
                    tasks.addAll(info.getTasks());
                }
                MetaProperty metaProperty = info.getObject();
                onPropertyLoaded(metaProperty, method);
            } else {
                log.warn("Method " + clazz.getSimpleName() + "." + method.getName()
                        + " is not included in metadata because property " + property + " already exists");
            }
        }
    }
}

From source file:org.castor.jaxb.reflection.ClassInfoBuilder.java

/**
 * Checks if a field of a class is describeable or not e.g. static or
 * transient fields are not described.//from  w  w  w . j  av  a  2  s.  c  om
 * 
 * @param type
 *            the Class holding the field
 * @param field
 *            the field to check
 * @return true if the field should be described
 */
private boolean isDescribeable(final Class<?> type, final Field field) {
    boolean isDescribeable = true;
    Class<?> declaringClass = field.getDeclaringClass();
    if ((declaringClass != null) && !type.equals(declaringClass) && (!declaringClass.isInterface())) {
        isDescribeable = false;
    }
    if (Modifier.isStatic(field.getModifiers())) {
        isDescribeable &= false;
    }
    if (Modifier.isTransient(field.getModifiers())) {
        isDescribeable &= false;
    }
    if (field.isSynthetic()) {
        isDescribeable &= false;
    }
    return isDescribeable;
}

From source file:com.haulmont.cuba.core.sys.MetaModelLoader.java

protected void initProperties(Class<?> clazz, MetaClassImpl metaClass, Collection<RangeInitTask> tasks) {
    if (!metaClass.getOwnProperties().isEmpty())
        return;/*  ww  w .j  a v  a 2  s .c  o  m*/

    // load collection properties after non-collection in order to have all inverse properties loaded up
    ArrayList<Field> collectionProps = new ArrayList<>();
    for (Field field : clazz.getDeclaredFields()) {
        if (field.isSynthetic())
            continue;

        final String fieldName = field.getName();

        if (isMetaPropertyField(field)) {
            MetaPropertyImpl property = (MetaPropertyImpl) metaClass.getProperty(fieldName);
            if (property == null) {
                MetadataObjectInfo<MetaProperty> info;
                if (isCollection(field) || isMap(field)) {
                    collectionProps.add(field);
                } else {
                    info = loadProperty(metaClass, field);
                    tasks.addAll(info.getTasks());
                    MetaProperty metaProperty = info.getObject();
                    onPropertyLoaded(metaProperty, field);
                }
            } else {
                log.warn("Field " + clazz.getSimpleName() + "." + field.getName()
                        + " is not included in metadata because property " + property + " already exists");
            }
        }
    }

    for (Field f : collectionProps) {
        MetadataObjectInfo<MetaProperty> info = loadCollectionProperty(metaClass, f);
        tasks.addAll(info.getTasks());
        MetaProperty metaProperty = info.getObject();
        onPropertyLoaded(metaProperty, f);
    }

    for (Method method : clazz.getDeclaredMethods()) {
        if (method.isSynthetic())
            continue;

        String methodName = method.getName();
        if (!methodName.startsWith("get") || method.getReturnType() == void.class)
            continue;

        if (isMetaPropertyMethod(method)) {
            String name = StringUtils.uncapitalize(methodName.substring(3));

            MetaPropertyImpl property = (MetaPropertyImpl) metaClass.getProperty(name);
            if (property == null) {
                MetadataObjectInfo<MetaProperty> info;
                if (isCollection(method) || isMap(method)) {
                    throw new UnsupportedOperationException(
                            String.format("Method-based property %s.%s doesn't support collections and maps",
                                    clazz.getSimpleName(), method.getName()));
                } else if (method.getParameterCount() != 0) {
                    throw new UnsupportedOperationException(
                            String.format("Method-based property %s.%s doesn't support arguments",
                                    clazz.getSimpleName(), method.getName()));
                } else {
                    info = loadProperty(metaClass, method, name);
                    tasks.addAll(info.getTasks());
                }
                MetaProperty metaProperty = info.getObject();
                onPropertyLoaded(metaProperty, method);
            } else {
                log.warn("Method " + clazz.getSimpleName() + "." + method.getName()
                        + " is not included in metadata because property " + property + " already exists");
            }
        }
    }
}

From source file:org.evosuite.setup.TestClusterGenerator.java

public static boolean canUse(Field f, Class<?> ownerClass) {

    // TODO we could enable some methods from Object, like getClass
    if (f.getDeclaringClass().equals(java.lang.Object.class))
        return false;// handled here to avoid printing reasons

    if (f.getDeclaringClass().equals(java.lang.Thread.class))
        return false;// handled here to avoid printing reasons

    if (!Properties.USE_DEPRECATED && f.isAnnotationPresent(Deprecated.class)) {
        logger.debug("Skipping deprecated field {}", f.getName());
        return false;
    }//from   w  ww .ja  va 2s  .c om

    if (f.isSynthetic()) {
        logger.debug("Skipping synthetic field {}", f.getName());
        return false;
    }

    if (f.getName().startsWith("ajc$")) {
        logger.debug("Skipping AspectJ field {}", f.getName());
        return false;
    }

    if (!f.getType().equals(String.class) && !canUse(f.getType())) {
        return false;
    }

    if (Modifier.isPublic(f.getModifiers())) {
        // It may still be the case that the field is defined in a non-visible superclass of the class
        // we already know we can use. In that case, the compiler would be fine with accessing the 
        // field, but reflection would start complaining about IllegalAccess!
        // Therefore, we set the field accessible to be on the safe side
        makeAccessible(f);
        return true;
    }

    // If default access rights, then check if this class is in the same package as the target class
    if (!Modifier.isPrivate(f.getModifiers())) {
        //              && !Modifier.isProtected(f.getModifiers())) {
        String packageName = ClassUtils.getPackageName(ownerClass);

        String declaredPackageName = ClassUtils.getPackageName(f.getDeclaringClass());

        if (packageName.equals(Properties.CLASS_PREFIX) && packageName.equals(declaredPackageName)) {
            makeAccessible(f);
            return true;
        }
    }

    return false;
}