Example usage for java.lang.reflect Method isAnnotationPresent

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

Introduction

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

Prototype

@Override
public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) 

Source Link

Usage

From source file:RssAtomGenerationTest.java

public static <T> T convertObject(final T destination, final Object source, FeedType type, Object... context) {
    Map<Class, Object> contentMap = new HashMap<Class, Object>();
    for (Object o : context) {
        contentMap.put(o.getClass(), o);
    }// ww  w .ja v a2 s .  c o m
    final Method[] methods = source.getClass().getMethods();
    for (Method method : methods) {
        final boolean refPresent = method.isAnnotationPresent(SyndicationRefs.class);
        if (refPresent || (method.isAnnotationPresent(SyndicationElement.class)
                && method.getAnnotation(SyndicationElement.class).type().equals(type))) {
            SyndicationElement annotation = null;
            if (refPresent) {
                final SyndicationElement[] value = method.getAnnotation(SyndicationRefs.class).value();
                for (SyndicationElement element : value) {
                    if (element.type().equals(type)) {
                        annotation = element;
                        break;
                    }
                }
                if (annotation == null) {
                    continue;
                }
            } else {
                annotation = method.getAnnotation(SyndicationElement.class);
            }
            //final SyndicationElement annotation =
            final String name = annotation.name();
            try {
                final Object initValue = method.invoke(source);
                Object value = null;
                if (!(annotation.converter().isAssignableFrom(NoopConverter.class))) {
                    final Converter converter = annotation.converter().newInstance();
                    value = converter.convert(initValue);
                }
                if (!(annotation.transformer().isAssignableFrom(NoopTransformer.class))) {
                    contentMap.put(initValue.getClass(), initValue);
                    final Class<?> transformer = annotation.transformer();
                    final Method[] transformerMethods = transformer.getMethods();
                    for (Method transformerMethod : transformerMethods) {
                        if (transformerMethod.isAnnotationPresent(ContextTransformable.class)) {
                            final Class<?>[] parameterTypes = transformerMethod.getParameterTypes();
                            List<Object> parameters = new ArrayList<Object>();
                            for (Class clazz : parameterTypes) {
                                if (contentMap.containsKey(clazz)) {
                                    parameters.add(contentMap.get(clazz));
                                } else {
                                    boolean found = false;
                                    for (Object obj : contentMap.values()) {
                                        if (clazz.isInstance(obj)) {
                                            parameters.add(obj);
                                            found = true;
                                            break;
                                        }
                                    }
                                    if (!found) {
                                        parameters.add(null);
                                    }
                                }
                            }
                            if (Modifier.isStatic(transformerMethod.getModifiers())) {
                                value = transformerMethod.invoke(null, parameters.toArray());
                            } else {
                                value = transformerMethod.invoke(transformer.newInstance(),
                                        parameters.toArray());
                            }
                            break;
                        }
                    }

                }
                BeanUtils.setProperty(destination, name, value);
            } catch (Exception e) {
                log.error("test", e);
                e.printStackTrace();
            }

        }
    }
    return destination;
}

From source file:com.github.shareme.blackandroids.greenandroid.easpermissions.EasyPermissions.java

private static void runAnnotatedMethods(Object object, int requestCode) {
    Class clazz = object.getClass();
    if (isUsingAndroidAnnotations(object)) {
        clazz = clazz.getSuperclass();/*from  www  . j a v  a2 s  . c o m*/
    }
    for (Method method : clazz.getDeclaredMethods()) {
        if (method.isAnnotationPresent(AfterPermissionGranted.class)) {
            // Check for annotated methods with matching request code.
            AfterPermissionGranted ann = method.getAnnotation(AfterPermissionGranted.class);
            if (ann.value() == requestCode) {
                // Method must be void so that we can invoke it
                if (method.getParameterTypes().length > 0) {
                    throw new RuntimeException("Cannot execute non-void method " + method.getName());
                }

                try {
                    // Make method accessible if private
                    if (!method.isAccessible()) {
                        method.setAccessible(true);
                    }
                    method.invoke(object);
                } catch (IllegalAccessException e) {
                    Timber.e(TAG, "runDefaultMethod:IllegalAccessException", e);
                } catch (InvocationTargetException e) {
                    Timber.e(TAG, "runDefaultMethod:InvocationTargetException", e);
                }
            }
        }
    }
}

From source file:com.nridge.core.base.field.data.DataBeanBag.java

/**
 * Accepts a POJO containing one or more public annotated get
 * methods and creates a DataBag from them.  The DataBeanObject2
 * test class provides a reference example.
 *
 * @param anObject POJO instance.//from w ww  . j a va 2  s.c o  m
 *
 * @return Data bag instance populated with field information.
 *
 * @throws NSException Thrown if object is null.
 * @throws IllegalAccessException Thrown if access is illegal.
 * @throws InvocationTargetException Thrown if target execution fails.
 */
public static DataBag fromMethodsToBag(Object anObject)
        throws NSException, IllegalAccessException, InvocationTargetException {
    DataField dataField;
    BeanField beanField;
    boolean isPublicAccess, isAnnotationPresent;

    if (anObject == null)
        throw new NSException("Object is null");

    DataBag dataBag = new DataBag(anObject.toString());
    Class<?> objClass = anObject.getClass();
    Method[] methodArray = objClass.getDeclaredMethods();
    for (Method objMethod : methodArray) {
        isPublicAccess = Modifier.isPublic(objMethod.getModifiers());
        isAnnotationPresent = objMethod.isAnnotationPresent(BeanField.class);
        if ((isAnnotationPresent) && (isPublicAccess)) {
            beanField = objMethod.getAnnotation(BeanField.class);
            dataField = reflectMethod(anObject, beanField, objMethod);
            dataBag.add(dataField);
        }
    }

    return dataBag;
}

From source file:pl.bristleback.server.bristle.utils.PropertyUtils.java

public static List<Method> getMethodsAnnotatedWith(Class owner, Class<? extends Annotation> annotationClass,
        boolean includeSuper) {
    List<Method> selectedMethods = new ArrayList<Method>();
    Method[] allMethods = owner.getMethods();
    for (Method method : allMethods) {
        if (!includeSuper && isMethodInherited(owner, method)) {
            continue;
        }//from  w w w . j  a  v  a2 s  .co m
        if (method.isAnnotationPresent(annotationClass)) {
            selectedMethods.add(method);
        }
    }

    return selectedMethods;
}

From source file:ch.ralscha.extdirectspring.util.MethodInfo.java

/**
 * Find a method that is annotated with a specific annotation. Starts with the method
 * and goes up to the superclasses of the class.
 *
 * @param method the starting method/*  w  ww.ja  v a  2  s . com*/
 * @param annotation the annotation to look for
 * @return the method if there is a annotated method, else null
 */
public static Method findMethodWithAnnotation(Method method, Class<? extends Annotation> annotation) {
    if (method.isAnnotationPresent(annotation)) {
        return method;
    }

    Class<?> cl = method.getDeclaringClass();
    while (cl != null && cl != Object.class) {
        try {
            Method equivalentMethod = cl.getDeclaredMethod(method.getName(), method.getParameterTypes());
            if (equivalentMethod.isAnnotationPresent(annotation)) {
                return equivalentMethod;
            }
        } catch (NoSuchMethodException e) {
            // do nothing here
        }
        cl = cl.getSuperclass();
    }

    return null;
}

From source file:com.rockagen.commons.util.ClassUtil.java

/**
 * obtain methods list of specified class and which are annotated by
 * incoming annotation class If recursively is true, obtain methods from all
 * class hierarchy//www .ja  v  a2s .  c o  m
 * 
 * @param clazz class
 *            - where methods are searching
 * @param annotationClass
 *            - specified annotation class
 * @param recursively
 *            param
 * @return list of annotated methods
 */
public static Method[] getAnnotatedDeclaredMethods(Class<?> clazz, Class<? extends Annotation> annotationClass,
        boolean recursively) {
    Method[] allMethods = getDeclaredMethods(clazz, recursively);
    List<Method> annotatedMethods = new LinkedList<Method>();

    for (Method method : allMethods) {
        if (method.isAnnotationPresent(annotationClass))
            annotatedMethods.add(method);
    }

    return annotatedMethods.toArray(new Method[annotatedMethods.size()]);
}

From source file:com.manydesigns.portofino.logic.SecurityLogic.java

public static boolean satisfiesRequiresAdministrator(HttpServletRequest request, ActionBean actionBean,
        Method handler) {
    logger.debug("Checking if action or method required administrator");
    boolean requiresAdministrator = false;
    if (handler.isAnnotationPresent(RequiresAdministrator.class)) {
        logger.debug("Action method requires administrator: {}", handler);
        requiresAdministrator = true;/*from  w ww . ja va2 s.  c  om*/
    } else {
        Class actionClass = actionBean.getClass();
        while (actionClass != null) {
            if (actionClass.isAnnotationPresent(RequiresAdministrator.class)) {
                logger.debug("Action class requires administrator: {}", actionClass);
                requiresAdministrator = true;
                break;
            }
            actionClass = actionClass.getSuperclass();
        }
    }

    boolean isNotAdmin = !isAdministrator(request);
    boolean doesNotSatisfy = requiresAdministrator && isNotAdmin;
    if (doesNotSatisfy) {
        logger.info("User is not an administrator");
        return false;
    }
    return true;
}

From source file:com.miandui.utils.runtimePermission.AndPermission.java

private static <T extends Annotation> Method[] findMethodForRequestCode(@NonNull Class<?> source,
        @NonNull Class<T> annotation, int requestCode) {
    List<Method> methods = new ArrayList<>(1);
    for (Method method : source.getDeclaredMethods())
        if (method.isAnnotationPresent(annotation))
            if (isSameRequestCode(method, annotation, requestCode))
                methods.add(method);//ww  w  .j a  va  2 s  .co m
    return methods.toArray(new Method[methods.size()]);
}

From source file:org.faster.opm.PropertyHelper.java

public static final boolean isResourceIDMethod(Method method) {
    return method.isAnnotationPresent(ResourceID.class);
}

From source file:org.faster.opm.PropertyHelper.java

public static final boolean isPropertyMethod(Resource resource, Method method) {
    if (method.isAnnotationPresent(Property.class)) {
        return true;
    }//from w ww. j  a v  a  2  s .  c o  m
    if (resource.defaultPropertyStrategy() == PropertyStrategy.EXCLUDE) {
        return false;
    }
    if (Modifier.isStatic(method.getModifiers())) {
        return false;
    }
    return !method.isAnnotationPresent(ExcludeProperty.class);
}