Example usage for java.lang.reflect Method isSynthetic

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

Introduction

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

Prototype

@Override
public boolean isSynthetic() 

Source Link

Usage

From source file:org.apache.bval.jsr.JsrMetaBeanFactory.java

/**
 * Process class annotations, field and method annotations.
 * /*from   w w  w.  j  av  a 2 s. c o m*/
 * @param beanClass
 * @param metabean
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 */
private void processClass(Class<?> beanClass, MetaBean metabean)
        throws IllegalAccessException, InvocationTargetException {

    // if NOT ignore class level annotations
    if (!factory.getAnnotationIgnores().isIgnoreAnnotations(beanClass)) {
        annotationProcessor.processAnnotations(null, beanClass, beanClass, null,
                new AppendValidationToMeta(metabean));
    }

    final Collection<String> missingValid = new ArrayList<String>();

    final Field[] fields = Reflection.getDeclaredFields(beanClass);
    for (final Field field : fields) {
        MetaProperty metaProperty = metabean.getProperty(field.getName());
        // create a property for those fields for which there is not yet a
        // MetaProperty
        if (!factory.getAnnotationIgnores().isIgnoreAnnotations(field)) {
            AccessStrategy access = new FieldAccess(field);
            boolean create = metaProperty == null;
            if (create) {
                metaProperty = addMetaProperty(metabean, access);
            }
            if (!annotationProcessor.processAnnotations(metaProperty, beanClass, field, access,
                    new AppendValidationToMeta(metaProperty)) && create) {
                metabean.putProperty(metaProperty.getName(), null);
            }

            if (field.getAnnotation(ConvertGroup.class) != null) {
                missingValid.add(field.getName());
            }
        }
    }
    final Method[] methods = Reflection.getDeclaredMethods(beanClass);
    for (final Method method : methods) {
        if (method.isSynthetic() || method.isBridge()) {
            continue;
        }
        String propName = null;
        if (method.getParameterTypes().length == 0) {
            propName = MethodAccess.getPropertyName(method);
        }
        if (propName != null) {
            if (!factory.getAnnotationIgnores().isIgnoreAnnotations(method)) {
                AccessStrategy access = new MethodAccess(propName, method);
                MetaProperty metaProperty = metabean.getProperty(propName);
                boolean create = metaProperty == null;
                // create a property for those methods for which there is
                // not yet a MetaProperty
                if (create) {
                    metaProperty = addMetaProperty(metabean, access);
                }
                if (!annotationProcessor.processAnnotations(metaProperty, beanClass, method, access,
                        new AppendValidationToMeta(metaProperty)) && create) {
                    metabean.putProperty(propName, null);
                }
            }
        }
    }

    addXmlConstraints(beanClass, metabean);

    for (final String name : missingValid) {
        final MetaProperty metaProperty = metabean.getProperty(name);
        if (metaProperty != null && metaProperty.getFeature(JsrFeatures.Property.REF_CASCADE) == null) {
            throw new ConstraintDeclarationException("@ConvertGroup needs @Valid");
        }
    }
    missingValid.clear();
}

From source file:org.apache.niolex.commons.reflect.MethodFilter.java

/**
 * This is the override of super method.
 * @see org.apache.niolex.commons.reflect.MethodUtil.Filter#isValid(java.lang.reflect.Method)
 *//* w  ww . j  ava2 s  .  c o m*/
@Override
public boolean isValid(Method m) {
    if (methodName != null && !methodName.equals(m.getName())) {
        return false;
    }
    if (returnType != null && !ClassUtils.isAssignable(m.getReturnType(), returnType, true)) {
        return false;
    }
    if (parameterTypes != null && !ClassUtils.isAssignable(parameterTypes, m.getParameterTypes(), true)) {
        return false;
    }
    if (!includeStatic && Modifier.isStatic(m.getModifiers())) {
        return false;
    }
    if (!includeAbstract && Modifier.isAbstract(m.getModifiers())) {
        return false;
    }
    if (!includeSynthetic && m.isSynthetic()) {
        return false;
    }
    return true;
}

From source file:org.batoo.common.reflect.ReflectHelper.java

/**
 * Returns the property descriptors for the class.
 * //from  ww w .  j a  va2s.  co  m
 * @param clazz
 *            the class
 * @return the property descriptors
 * 
 * @since 2.0.1
 */
public static PropertyDescriptor[] getProperties(Class<?> clazz) {
    final List<PropertyDescriptor> properties = Lists.newArrayList();

    final Method[] methodList = clazz.getDeclaredMethods();

    // check each method.
    for (final Method method : methodList) {
        if (method == null) {
            continue;
        }

        // skip static and private methods.
        final int mods = method.getModifiers();
        if (Modifier.isStatic(mods) || !Modifier.isPublic(mods) || method.isBridge() || method.isSynthetic()) {
            continue;
        }

        final String name = method.getName();

        if (method.getParameterTypes().length == 0) {
            if (name.startsWith(ReflectHelper.GET_PREFIX)) {
                properties.add(new PropertyDescriptor(clazz, name.substring(3), method));
            } else if ((method.getReturnType() == boolean.class) && name.startsWith(ReflectHelper.IS_PREFIX)) {
                properties.add(new PropertyDescriptor(clazz, name.substring(2), method));
            }
        }
    }

    return properties.toArray(new PropertyDescriptor[properties.size()]);
}

From source file:org.batoo.jpa.parser.impl.metadata.type.ManagedTypeMetadatImpl.java

/**
 * Infers and returns the access type based on all persistence annotations being on fields or methods and parent parent access type.
 * /*from w ww  .jav a  2 s . c o m*/
 * @param parentAccessType
 *            the parent access type
 * @return the inferred access type
 * 
 * @since 2.0.0
 */
private AccessType inferAccessType(AccessType parentAccessType) {
    boolean methodsHasAnnotations = false;
    boolean fieldsHasAnnotations = false;

    final List<String> alternated = Lists.newArrayList();

    final Field[] fields = this.clazz.getDeclaredFields();
    final Method[] methods = this.clazz.getDeclaredMethods();

    // find the alternated ones with @Access
    for (final Method m : methods) {
        // skip static and private methods.
        final int mods = m.getModifiers();
        if (Modifier.isStatic(mods) || !Modifier.isPublic(mods) || m.isBridge() || m.isSynthetic()) {
            continue;
        }

        if ((m.getParameterTypes().length != 0) || (m.getReturnType() == null)) {
            continue;
        }

        final Access access = m.getAnnotation(Access.class);
        if (access != null) {
            final String name = m.getName();
            if ((m.getReturnType() == boolean.class) && name.startsWith("is")) {
                alternated.add(StringUtils.capitalize(name.substring(2)));
            } else if (name.startsWith("get")) {
                alternated.add(StringUtils.capitalize(name.substring(3)));
            }
        }
    }

    for (final Field f : fields) {
        final Access access = f.getAnnotation(Access.class);

        if (access != null) {
            alternated.add(StringUtils.capitalize(f.getName()));
        }
    }

    // check methods
    for (final Method m : methods) {
        for (final Annotation a : m.getAnnotations()) {
            // ignore @Access(PROPERTY)
            if (a instanceof Access) {
                if (((Access) a).value() != AccessType.PROPERTY) {
                    continue;
                }
            }

            // ignore @Transient
            if (a instanceof Transient) {
                continue;
            }

            if ((m.getReturnType() == null) || (m.getParameterTypes().length > 0)) {
                continue;
            }

            String name = a.annotationType().getName();
            // ignore the listener annotations
            if (name.startsWith("javax.persistence.Post") || name.startsWith("javax.persistence.Pre")) {
                continue;
            }

            if (name.startsWith("javax.persistence") || name.startsWith("org.batoo.jpa.annotation")) {
                name = m.getName();

                if ((boolean.class == m.getReturnType()) || name.startsWith("is")) {
                    name = name.substring(2);
                } else if (name.startsWith("get")) {
                    name = name.substring(3);
                }

                if (alternated.contains(StringUtils.capitalize(name))) {
                    continue;
                }

                methodsHasAnnotations = true;
                break;
            }
        }
    }

    // check fields
    for (final Field f : fields) {
        for (final Annotation a : f.getAnnotations()) {
            // ignore @Access(FIELD)
            if (a instanceof Access) {
                if (((Access) a).value() != AccessType.FIELD) {
                    continue;
                }
            }

            // ignore @Transient
            if (a instanceof Transient) {
                continue;
            }

            final String name = a.annotationType().getName();
            if (name.startsWith("javax.persistence") || name.startsWith("org.batoo.jpa.annotation")) {
                if (alternated.contains(StringUtils.capitalize(f.getName()))) {
                    continue;
                }

                fieldsHasAnnotations = true;
                break;
            }
        }
    }

    if (fieldsHasAnnotations && methodsHasAnnotations) {
        throw new PersistenceException(
                "At least one field and one method has persistence annotations: " + this.clazz.getName());
    }

    if (methodsHasAnnotations) {
        return AccessType.PROPERTY;
    }

    if (fieldsHasAnnotations) {
        return AccessType.FIELD;
    }

    if (parentAccessType != null) {
        return parentAccessType;
    }

    return AccessType.FIELD;
}

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;
            }/*from w w  w  .  ja  va2s. 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:org.codehaus.groovy.grails.commons.metaclass.BaseApiProvider.java

private boolean isNotExcluded(Method method, final int modifiers) {
    final String name = method.getName();

    if (EXCLUDED_METHODS.contains(name))
        return false;

    boolean isStatic = Modifier.isStatic(modifiers);

    // skip plain setters/getters by default for instance methods (non-static)
    if (!isStatic && (GrailsClassUtils.isSetter(name, method.getParameterTypes())
            || GrailsClassUtils.isGetter(name, method.getParameterTypes()))) {
        return false;
    }//from   www . j a  v a 2s .  c  o  m

    int minParameters = isStatic ? 0 : 1;

    return Modifier.isPublic(modifiers) && !(method.isSynthetic() || method.isBridge())
            && !Modifier.isAbstract(modifiers) && !name.contains("$")
            && (method.getParameterTypes().length >= minParameters);
}

From source file:org.compass.annotations.config.binding.AnnotationsMappingBinding.java

/**
 * Recursivly process the class to find all it's annotations. Lower level
 * class/interfaces with annotations will be added first.
 *///from w w w .j  av  a2  s  . c o  m
private void processAnnotatedClass(Class<?> clazz) {
    if (clazz.equals(Class.class)) {
        return;
    }
    Class<?> superClazz = clazz.getSuperclass();
    if (superClazz != null && !superClazz.equals(Object.class)) {
        processAnnotatedClass(superClazz);
    }
    Class<?>[] interfaces = clazz.getInterfaces();
    for (Class<?> anInterface : interfaces) {
        processAnnotatedClass(anInterface);
    }

    SearchableConstant searchableConstant = clazz.getAnnotation(SearchableConstant.class);
    if (searchableConstant != null) {
        bindConstantMetaData(searchableConstant);
    }

    SearchableConstants searchableConstants = clazz.getAnnotation(SearchableConstants.class);
    if (searchableConstants != null) {
        for (SearchableConstant metaData : searchableConstants.value()) {
            bindConstantMetaData(metaData);
        }
    }

    SearchableDynamicMetaData searchableDynamicMetaData = clazz.getAnnotation(SearchableDynamicMetaData.class);
    if (searchableDynamicMetaData != null) {
        bindDynamicMetaData(searchableDynamicMetaData);
    }
    SearchableDynamicMetaDatas searchableDynamicMetaDatas = clazz
            .getAnnotation(SearchableDynamicMetaDatas.class);
    if (searchableDynamicMetaDatas != null) {
        for (SearchableDynamicMetaData metaData : searchableDynamicMetaDatas.value()) {
            bindDynamicMetaData(metaData);
        }
    }

    // handles recursive extends and the original extend
    if (clazz.isAnnotationPresent(Searchable.class)) {
        Searchable searchable = clazz.getAnnotation(Searchable.class);
        String[] extend = searchable.extend();
        if (extend.length != 0) {
            ArrayList<String> extendedMappings = new ArrayList<String>();
            if (classMapping.getExtendedAliases() != null) {
                extendedMappings.addAll(Arrays.asList(classMapping.getExtendedAliases()));
            }
            for (String extendedAlias : extend) {
                Alias extendedAliasLookup = valueLookup.lookupAlias(extendedAlias);
                if (extendedAliasLookup == null) {
                    extendedMappings.add(extendedAlias);
                } else {
                    extendedMappings.add(extendedAliasLookup.getName());
                }
            }
            classMapping.setExtendedAliases(extendedMappings.toArray(new String[extendedMappings.size()]));
        }
    }

    // if the super class has Searchable annotation as well, add it to the list of extends
    ArrayList<Class> extendedClasses = new ArrayList<Class>();
    if (clazz.getSuperclass() != null) {
        extendedClasses.add(clazz.getSuperclass());
    }
    extendedClasses.addAll(Arrays.asList(clazz.getInterfaces()));
    for (Class<?> superClass : extendedClasses) {
        if (!superClass.isAnnotationPresent(Searchable.class)) {
            continue;
        }
        Searchable superSearchable = superClass.getAnnotation(Searchable.class);
        String alias = getAliasFromSearchableClass(superClass, superSearchable);
        HashSet<String> extendedMappings = new HashSet<String>();
        if (classMapping.getExtendedAliases() != null) {
            extendedMappings.addAll(Arrays.asList(classMapping.getExtendedAliases()));
        }
        extendedMappings.add(alias);
        classMapping.setExtendedAliases(extendedMappings.toArray(new String[extendedMappings.size()]));
    }

    for (Field field : clazz.getDeclaredFields()) {
        for (Annotation annotation : field.getAnnotations()) {
            processsAnnotatedElement(clazz, field.getName(), "field", field.getType(), field.getGenericType(),
                    annotation, field);
        }
    }
    for (Method method : clazz.getDeclaredMethods()) {
        if (!method.isSynthetic() && !method.isBridge() && !Modifier.isStatic(method.getModifiers())
                && method.getParameterTypes().length == 0 && method.getReturnType() != void.class
                && (method.getName().startsWith("get") || method.getName().startsWith("is"))) {

            for (Annotation annotation : method.getAnnotations()) {
                processsAnnotatedElement(clazz, ClassUtils.getShortNameForMethod(method), "property",
                        method.getReturnType(), method.getGenericReturnType(), annotation, method);
            }
        }
    }
}

From source file:org.cruxframework.crux.core.server.rest.core.registry.ResourceRegistry.java

/**
 * /*  w ww. ja  va2s  .  co m*/
 * @param clazz
 * @param base
 */
protected void addResource(Class<?> clazz, String base) {
    Set<String> restMethodNames = new HashSet<String>();
    Map<String, List<RestMethodRegistrationInfo>> validRestMethods = new HashMap<String, List<RestMethodRegistrationInfo>>();
    for (Method method : clazz.getMethods()) {
        if (!method.isSynthetic()) {
            RestMethodRegistrationInfo methodRegistrationInfo = null;
            try {
                methodRegistrationInfo = processMethod(base, clazz, method, restMethodNames);
            } catch (Exception e) {
                throw new InternalServerErrorException("Error to processMethod: " + method.toString(),
                        "Can not execute requested service", e);
            }
            if (methodRegistrationInfo != null) {
                List<RestMethodRegistrationInfo> methodsForPath = validRestMethods
                        .get(methodRegistrationInfo.pathExpression);
                if (methodsForPath == null) {
                    methodsForPath = new ArrayList<RestMethodRegistrationInfo>();
                    validRestMethods.put(methodRegistrationInfo.pathExpression, methodsForPath);

                }
                methodsForPath.add(methodRegistrationInfo);
            }
        }
    }
    checkConditionalWriteMethods(validRestMethods);
    createCorsAllowedMethodsList(validRestMethods);
}

From source file:org.dozer.util.ReflectionUtils.java

/**
 * There are some nasty bugs for introspection with generics. This method addresses those nasty bugs and tries to find proper methods if available
 *  http://bugs.sun.com/view_bug.do?bug_id=6788525
 *  http://bugs.sun.com/view_bug.do?bug_id=6528714
 * @param descriptor/*from   w  w  w . j  a  v a2s  .  c  o m*/
 * @return
 */
private static PropertyDescriptor fixGenericDescriptor(Class<?> clazz, PropertyDescriptor descriptor) {
    Method readMethod = descriptor.getReadMethod();
    Method writeMethod = descriptor.getWriteMethod();

    if (readMethod != null && (readMethod.isBridge() || readMethod.isSynthetic())) {
        String propertyName = descriptor.getName();
        //capitalize the first letter of the string;
        String baseName = Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1);
        String setMethodName = "set" + baseName;
        String getMethodName = "get" + baseName;
        Method[] methods = clazz.getMethods();
        for (Method method : methods) {
            if (method.getName().equals(getMethodName) && !method.isBridge() && !method.isSynthetic()) {
                try {
                    descriptor.setReadMethod(method);
                } catch (IntrospectionException e) {
                    //move on
                }
            }
            if (method.getName().equals(setMethodName) && !method.isBridge() && !method.isSynthetic()) {
                try {
                    descriptor.setWriteMethod(method);
                } catch (IntrospectionException e) {
                    //move on
                }
            }
        }
    }
    return descriptor;
}

From source file:org.evosuite.assertion.InspectorManager.java

private boolean isInspectorMethod(Method method) {
    if (!Modifier.isPublic(method.getModifiers()))
        return false;

    if (!method.getReturnType().isPrimitive() && !method.getReturnType().equals(String.class)
            && !method.getReturnType().isEnum() && !ClassUtils.isPrimitiveWrapper(method.getReturnType())) {
        return false;
    }/* ww  w .j av a 2 s. co  m*/

    if (method.getReturnType().equals(void.class))
        return false;

    if (method.getParameterTypes().length != 0)
        return false;

    if (method.getName().equals("hashCode"))
        return false;

    if (method.getDeclaringClass().equals(Object.class))
        return false;

    if (method.getDeclaringClass().equals(Enum.class))
        return false;

    if (method.isSynthetic())
        return false;

    if (method.isBridge())
        return false;

    if (method.getName().equals("pop"))
        return false;

    if (isBlackListed(method))
        return false;

    if (isImpureJDKMethod(method))
        return false;

    if (isAWTToString(method))
        return false;

    if (Properties.PURE_INSPECTORS) {
        if (!CheapPurityAnalyzer.getInstance().isPure(method)) {
            return false;
        }
    }

    return true;

}