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:org.openspotlight.persist.util.SimpleNodeTypeVisitorSupport.java

@SuppressWarnings("unchecked")
public static Set<SimpleNodeType> fillItems(final SimpleNodeType rootNode,
        final Class<? extends Annotation>... annotationsToIgnore) throws Exception {

    if (rootNode == null) {
        return Collections.emptySet();
    }//from  w ww. ja v  a  2 s .  c  o m
    final Set<SimpleNodeType> itemsToVisit = new LinkedHashSet<SimpleNodeType>();

    final PropertyDescriptor[] allDescriptors = PropertyUtils.getPropertyDescriptors(rootNode);
    looping: for (final PropertyDescriptor desc : allDescriptors) {
        final Method readMethod = desc.getReadMethod();
        if (readMethod.isAnnotationPresent(ParentProperty.class)) {
            continue looping;
        }
        for (final Class<? extends Annotation> annotation : annotationsToIgnore) {
            if (readMethod.isAnnotationPresent(annotation)) {
                continue looping;
            }
        }
        final Class<?> currentType = desc.getPropertyType();
        if (SimpleNodeType.class.isAssignableFrom(currentType)) {
            final SimpleNodeType bean = (SimpleNodeType) readMethod.invoke(rootNode);
            itemsToVisit.add(bean);
        } else if (Iterable.class.isAssignableFrom(currentType)) {
            final UnwrappedCollectionTypeFromMethodReturn<Object> metadata = Reflection
                    .unwrapCollectionFromMethodReturn(readMethod);
            if (SimpleNodeType.class.isAssignableFrom(metadata.getItemType())) {
                final Iterable<SimpleNodeType> collection = (Iterable<SimpleNodeType>) readMethod
                        .invoke(rootNode);
                if (collection != null) {
                    for (final SimpleNodeType t : collection) {
                        itemsToVisit.add(t);
                    }
                }
            }
        } else if (Map.class.isAssignableFrom(currentType)) {
            final UnwrappedMapTypeFromMethodReturn<Object, Object> metadata = Reflection
                    .unwrapMapFromMethodReturn(readMethod);
            if (SimpleNodeType.class.isAssignableFrom(metadata.getItemType().getK2())) {
                final Map<Object, SimpleNodeType> map = (Map<Object, SimpleNodeType>) readMethod
                        .invoke(rootNode);
                if (map != null) {
                    for (final Entry<Object, SimpleNodeType> entry : map.entrySet()) {
                        itemsToVisit.add(entry.getValue());
                    }
                }
            }
        }
    }
    return itemsToVisit;
}

From source file:org.wso2.carbon.registry.core.pagination.PaginationUtils.java

/**
 * Check the resource get method is paginated or not.
 * @param annotation method annotation  name
 * @return true if the method id paginated.
 *//*from  w  ww .  jav  a2 s.c o m*/
public static boolean isPaginationAnnotationFound(String annotation) {
    StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
    for (StackTraceElement element : stackTrace) {
        try {
            Method[] methods = Class.forName(element.getClassName()).getMethods();
            for (Method method : methods) {
                if (method.getName().equals(element.getMethodName())
                        && method.isAnnotationPresent(Paginate.class)
                        && method.getAnnotation(Paginate.class).value().equals(annotation)) {
                    return true;
                }
            }
        } catch (ClassNotFoundException ignore) {
        }
    }
    return false;
}

From source file:RssAtomGenerationTest.java

public static List<Method> getSyndicationElementsOfType(Class clazz, FeedType type) {
    List<Method> methodList = new ArrayList<Method>();
    final Method[] methods = clazz.getMethods();
    for (Method method : methods) {
        if (method.isAnnotationPresent(SyndicationElement.class)
                && method.getAnnotation(SyndicationElement.class).type().equals(type)) {
            methodList.add(method);//from  ww  w.  j a v  a 2s. c  om
        }
    }
    return methodList;
}

From source file:com.ez.gallery.permission.EasyPermissions.java

private static void runAnnotatedMethods(Object object, int requestCode) {
    Class clazz = object.getClass();
    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());
                }//from  w w  w  .j a  v  a  2 s .com

                try {
                    // Make method accessible if private
                    if (!method.isAccessible()) {
                        method.setAccessible(true);
                    }
                    method.invoke(object);
                } catch (IllegalAccessException e) {
                } catch (InvocationTargetException e) {
                }
            }
        }
    }
}

From source file:com.ag.common.permission.EasyPermissions.java

private static void runAnnotatedMethods(Object object, int requestCode) {
    Class clazz = object.getClass();
    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());
                }//from w ww  .  j  a  va 2 s  . co  m

                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:io.cess.core.gallery.permission.EasyPermissions.java

private static void runAnnotatedMethods(Object object, int requestCode) {
    Class clazz = object.getClass();
    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());
                }//  ww  w  .  ja v  a  2  s.c o m

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

From source file:cn.finalteam.galleryfinal.permission.EasyPermissions.java

private static void runAnnotatedMethods(Object object, int requestCode) {
    Class clazz = object.getClass();
    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());
                }//from  w w w.  ja  v  a2  s.  co  m

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

From source file:com.ybg.rp.assistant.permission.EasyPermissions.java

private static void runAnnotatedMethods(Object object, int requestCode) {
    Class clazz = object.getClass();
    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());
                }//from   ww w  .jav a  2s  . co m

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

From source file:plugin.PluginHarness.java

public static void loadPlugins() throws PluginLoadingException {

    unloadAllPlugins();/*from  ww  w.j  a  v a 2s.c  o m*/

    CommandList.dumpCommands();

    File allPlugins = new File("plugins");

    if (!allPlugins.exists())
        allPlugins.mkdirs();

    for (File cPlugin : allPlugins.listFiles()) {

        String ezName = FilenameUtils.removeExtension(cPlugin.getName());

        String pluginLogTag = "[" + ezName + "] ";

        log.info(pluginLogTag + "Attempting to load plugin");

        GroovyClassLoader gcl = new GroovyClassLoader();
        try {

            Class c = gcl.parseClass(cPlugin);

            Object listenerInst = c.newInstance();

            if (!listenerInst.getClass().isAnnotationPresent(PluginInfo.class)) {
                log.error(pluginLogTag + "Could not load plugin, PluginInfo annotation not present");
                continue;
            }

            PluginInfo loadPlug = listenerInst.getClass().getAnnotation(PluginInfo.class);

            boolean plugValid = true;

            for (Method method : c.getMethods()) {

                if (method.isAnnotationPresent(CommandTag.class)) {

                    if (method.getParameters().length != 2) {
                        log.error(pluginLogTag + "Method " + method.toGenericString()
                                + " could not be loaded, expected 2 parameters.");
                        plugValid = false;
                        break;
                    }
                    Class<?> eventClazz = method.getParameters()[0].getType();

                    if (!eventClazz.equals(IMessage.class)) {
                        log.error(pluginLogTag + "Method " + method.toGenericString()
                                + " could not be loaded, parameter 1 should be of type IMessage");
                        plugValid = false;
                        break;
                    }
                    eventClazz = method.getParameters()[1].getType();

                    if (!eventClazz.equals(List.class)) {
                        log.error(pluginLogTag + "Method " + method.toGenericString()
                                + " could not be loaded, parameter 2 should be of type List<String>");
                        plugValid = false;
                        break;
                    }

                    CommandTag cmd = method.getAnnotation(CommandTag.class);
                    // pretty name, scope, command string, description, method
                    CommandList.putC(cmd.prettyName(), cmd.channelScope(), cmd.commandPattern(),
                            cmd.description(), (chatSource, vargs) -> {

                                try {
                                    method.invoke(listenerInst, chatSource, vargs);
                                } catch (InvocationTargetException | IllegalAccessException e) {
                                    throw e.getCause();
                                }

                            });

                    log.info(pluginLogTag + "Registered chat command \"" + cmd.prettyName());

                } else if (method.isAnnotationPresent(EventSubscriber.class)) { // validate method signature as being instance of Event

                    if (method.getParameters().length != 1) {
                        log.error(pluginLogTag + "Method " + method.toGenericString()
                                + " could not be loaded, expected only 1 parameter.");
                        plugValid = false;
                        break;
                    }

                    Class<?> eventClazz = method.getParameters()[0].getType();

                    if (eventClazz.equals(Event.class) || !Event.class.isAssignableFrom(eventClazz)) {
                        log.error(pluginLogTag + "Method " + method.toGenericString()
                                + " could not be loaded, parameter should extend Event");
                        plugValid = false;
                        break;
                    }
                }
            }

            if (!plugValid) {
                log.error(pluginLogTag + "One or more errors occurred, loading attempted");
                continue;
            }

            cli.getDispatcher().registerListener(listenerInst);
            registeredListeners.add(listenerInst);

            log.info(pluginLogTag + "Registered listener: " + listenerInst.toString());

            log.info(pluginLogTag + "Successfully loaded" + " plugin: \"" + loadPlug.name() + "\" version: \""
                    + loadPlug.version() + "\" by: \"" + loadPlug.author() + "\"");

        } catch (Exception e) {
            log.error(pluginLogTag + "Unknown error occurred", e);
        }
    }

}

From source file:com.crosstreelabs.junited.elasticsearch.ElasticsearchRule.java

protected static List<ElasticSetup> getAnnotations(final Description description) {
    Class<?> testClass = description.getTestClass();
    String methodName = description.getMethodName();

    List<ElasticSetup> result = new ArrayList<>();
    if (testClass.isAnnotationPresent(ElasticSetup.class)) {
        result.add(testClass.getAnnotation(ElasticSetup.class));
    }/*from   www. j a  va2  s.  c o  m*/
    if (testClass.isAnnotationPresent(ElasticSetups.class)) {
        result.addAll(Arrays.asList(testClass.getAnnotation(ElasticSetups.class).value()));
    }
    if (methodName == null) {
        return result;
    }

    try {
        Method method = testClass.getDeclaredMethod(methodName);
        if (method.isAnnotationPresent(ElasticSetup.class)) {
            result.add(method.getAnnotation(ElasticSetup.class));
        }
        if (method.isAnnotationPresent(ElasticSetups.class)) {
            result.addAll(Arrays.asList(method.getAnnotation(ElasticSetups.class).value()));
        }
    } catch (NoSuchMethodException | SecurityException ex) {
    }
    return result;
}