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:com.fxc.lib.pmsion.EasyPermissions.java

private static void runAnnotatedMethods(@NonNull Object object, int requestCode) {
    Class clazz = object.getClass();
    if (isUsingAndroidAnnotations(object)) {
        clazz = clazz.getSuperclass();/*from w w  w.j  a va 2 s .  c  om*/
    }
    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 method " + method.getName()
                            + " because it is non-void method and/or has input parameters.");
                }

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

From source file:com.mm.yamingapp.core.MethodDescriptor.java

/** Collects all methods with {@code RPC} annotation from given class. */
public static Collection<MethodDescriptor> collectFrom(Class<? extends WrapperBase> clazz) {
    List<MethodDescriptor> descriptors = new ArrayList<MethodDescriptor>();

    for (Method method : clazz.getMethods()) {
        if (method.isAnnotationPresent(Rpc.class)) {
            descriptors.add(new MethodDescriptor(clazz, method));
        }/*from  w w w  . j  a va2  s . c  o m*/
    }
    return descriptors;
}

From source file:org.jspare.forvertx.web.collector.HandlerCollector.java

@SuppressWarnings("unchecked")
private static List<Annotation> getHandlersPresents(Method method) {

    List<Class<?>> handlerClass = Arrays.asList(HandlerType.values()).stream().map(ht -> ht.getHandlerClass())
            .collect(Collectors.toList());
    return handlerClass.stream()
            .filter(handlerType -> method.isAnnotationPresent((Class<? extends Annotation>) handlerType))
            .map(handlerType -> method.getAnnotation((Class<? extends Annotation>) handlerType))
            .collect(Collectors.toList());
}

From source file:com.github.gekoh.yagen.util.FieldInfo.java

public static AccessibleObject getIdFieldOrMethod(Class entityClass) {
    for (Field field : entityClass.getDeclaredFields()) {
        if (field.isAnnotationPresent(Id.class)) {
            return field;
        }/*from  w  w  w. ja v  a  2 s .  co  m*/
    }
    for (Method method : entityClass.getDeclaredMethods()) {
        if (method.isAnnotationPresent(Id.class)) {
            return method;
        }
    }
    return entityClass.getSuperclass() != null ? getIdFieldOrMethod(entityClass.getSuperclass()) : null;
}

From source file:Main.java

public static List<Method> findAnnotatedMethods(final Class<?> type,
        final Class<? extends Annotation> annotation) {
    final List<Method> methods = new ArrayList<>();
    Method[] ms = type.getDeclaredMethods();
    for (Method method : ms) {
        // Must not static
        if (Modifier.isStatic(method.getModifiers())) {
            continue;
        }//from   w w  w .  j a va2 s .c o  m
        // Must be public
        if (Modifier.isPublic(method.getModifiers())) {
            continue;
        }
        // Must has only one parameter
        if (method.getParameterTypes().length != 1) {
            continue;
        }
        // Must has annotation
        if (!method.isAnnotationPresent(annotation)) {
            continue;
        }
        methods.add(method);
    }
    return methods;
}

From source file:com.LFPermission.lib.LFPermissions.java

private static void runAnnotatedMethods(@NonNull Object object, int requestCode) {
    Class clazz = object.getClass();
    if (isUsingAndroidAnnotations(object)) {
        clazz = clazz.getSuperclass();//from  ww  w . j ava 2s . c  om
    }

    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 method " + method.getName()
                            + " because it is non-void method and/or " + "has input parameters.");
                }

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

From source file:com.hurence.logisland.util.kura.Metrics.java

public static <T> T readFrom(final T object, final Map<String, Object> metrics) {
    Objects.requireNonNull(object);

    for (final Field field : FieldUtils.getFieldsListWithAnnotation(object.getClass(), Metric.class)) {
        final Metric m = field.getAnnotation(Metric.class);
        final boolean optional = field.isAnnotationPresent(Optional.class);

        final Object value = metrics.get(m.value());
        if (value == null && !optional) {
            throw new IllegalArgumentException(
                    String.format("Field '%s' is missing metric '%s'", field.getName(), m.value()));
        }/* ww w.  ja v a 2  s  .  com*/

        if (value == null) {
            // not set but optional
            continue;
        }

        try {
            FieldUtils.writeField(field, object, value, true);
        } catch (final IllegalArgumentException e) {
            // provide a better message
            throw new IllegalArgumentException(String.format("Failed to assign '%s' (%s) to field '%s'", value,
                    value.getClass().getName(), field.getName()), e);
        } catch (final IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    for (final Method method : MethodUtils.getMethodsListWithAnnotation(object.getClass(), Metric.class)) {
        final Metric m = method.getAnnotation(Metric.class);
        final boolean optional = method.isAnnotationPresent(Optional.class);

        final Object value = metrics.get(m.value());
        if (value == null && !optional) {
            throw new IllegalArgumentException(
                    String.format("Method '%s' is missing metric '%s'", method.getName(), m.value()));
        }

        if (value == null) {
            // not set but optional
            continue;
        }

        try {
            method.invoke(object, value);
        } catch (final IllegalArgumentException e) {
            // provide a better message
            throw new IllegalArgumentException(String.format("Failed to call '%s' (%s) with method '%s'", value,
                    value.getClass().getName(), method.getName()), e);
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }

    return object;
}

From source file:com.googlecode.android_scripting.rpc.MethodDescriptor.java

/** Collects all methods with {@code RPC} annotation from given class. */
public static Collection<MethodDescriptor> collectFrom(Class<? extends RpcReceiver> clazz) {
    List<MethodDescriptor> descriptors = new ArrayList<MethodDescriptor>();
    for (Method method : clazz.getMethods()) {
        if (method.isAnnotationPresent(Rpc.class)) {
            descriptors.add(new MethodDescriptor(clazz, method));
        }//from ww  w.  jav  a  2  s . c o m
    }
    return descriptors;
}

From source file:com.zhangwx.dynamicpermissionsrequest.permission.EasyPermissions.java

private static void runAnnotatedMethods(@NonNull Object object, int requestCode) {
    Class clazz = object.getClass();
    if (isUsingAndroidAnnotations(object)) {
        clazz = clazz.getSuperclass();/*from  w  w w .  j  a  v  a 2s  .  com*/
    }

    while (clazz != null) {
        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 method " + method.getName()
                                + " because it is non-void method and/or has input parameters.");
                    }

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

        clazz = clazz.getSuperclass();
    }
}

From source file:org.agiso.core.i18n.util.I18nUtils.java

public static String getCode(Method m) {
    if (m.isAnnotationPresent(I18n.class)) {
        if (m.getAnnotation(I18n.class).value().length() > 0) {
            return m.getAnnotation(I18n.class).value();
        } else {//from   w  w w .j  ava 2  s  .com
            return m.getDeclaringClass().getCanonicalName() + CODE_SEPARATOR + findGetterFieldName(m);
        }
    }
    // else if(m.isAnnotationPresent(I18nRef.class)) {
    //    Object ref = m.getAnnotation(I18nRef.class).value();
    //    if(((Class<?>)ref).isEnum()) {
    //       return getCode((Enum<?>)ref);
    //    } else {
    //       return getCode((Class<?>)ref);
    //    }
    // }
    return m.getDeclaringClass().getCanonicalName() + CODE_SEPARATOR + findGetterFieldName(m);
}