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:ru.jts_dev.gameserver.handlers.HandlerManager.java

protected List<Method> getAnnotatedMethods(Class<? extends ICommandHandler<TCommandType>> handlerClass,
        Class<? extends Annotation> annotationClass) {
    ArrayList<Method> methods = new ArrayList<>();
    for (Method method : handlerClass.getDeclaredMethods()) {
        if (method.isAnnotationPresent(annotationClass)) {
            methods.add(method);/*from w ww .j  a va 2s. co  m*/
        }
    }
    methods.trimToSize();
    return methods;
}

From source file:org.diorite.impl.bean.BeanScanner.java

private void processClass(final Class<?> clazz) throws BeanException {
    // Bean registration by class
    if (clazz.isAnnotationPresent(DioriteBean.class)) {
        final DioriteBean beanInfo = clazz.getAnnotation(DioriteBean.class);
        final BeanContainer beanContainer = new BeanContainer(clazz, new ClassBeanProvider(clazz));

        this.beanManager.registerBean(beanContainer);
    }//from ww w  .  j  a  v  a2s. c  o m

    // Bean registration by constructor
    for (final Constructor<?> constructor : clazz.getConstructors()) {
        if (constructor.isAnnotationPresent(DioriteBean.class)) {
            final DioriteBean beanInfo = constructor.getAnnotation(DioriteBean.class);
            final BeanContainer beanContainer = new BeanContainer(clazz,
                    new ConstructorBeanProvider(constructor));

            this.beanManager.registerBean(beanContainer);
        }
    }

    // Bean registration by method
    for (final Method method : clazz.getMethods()) {
        if (method.isAnnotationPresent(DioriteBean.class)) {
            final Class<?> returnType = method.getReturnType();
            if (returnType.equals(Void.TYPE) || returnType.isPrimitive()) {
                throw new BeanRegisteringException(MessageFormat.format(
                        "Can't register method '{0}' in class '{1}' as Diorite Bean. Method must return object.",
                        method.getName(), clazz.getName()));
            } else if (returnType.getPackage().equals(JAVA_PACKAGE)) {
                throw new BeanRegisteringException(MessageFormat.format(
                        "Can't register method '{0}' in class '{1}' as Diorite Bean. Method can't return object from java package.",
                        method.getName(), clazz.getName()));
            }
            final DioriteBean beanInfo = method.getAnnotation(DioriteBean.class);

            final BeanContainer beanContainer = new BeanContainer(returnType, new MethodBeanProvider(method));
            this.beanManager.registerBean(beanContainer);
        }
    }

    for (final Field field : clazz.getDeclaredFields()) {
        if (field.isAnnotationPresent(InjectedValue.class)) {
            this.beanManager.addInjectorCode(clazz);
            break;
        }
    }
}

From source file:com.graphaware.test.integration.GraphAwareIntegrationTest.java

/**
 * Find all classes on classpath (only .class files) that have a method annotated with {@link Procedure}.
 *
 * @return classes with procedures./* ww  w . j  a va 2  s.c  o m*/
 */
private Iterable<Class> proceduresOnClassPath() {
    Enumeration<URL> urls;

    try {
        urls = this.getClass().getClassLoader().getResources("");
    } catch (IOException e) {
        throw new RuntimeException();
    }

    List<Class> classes = new ArrayList<>();

    while (urls.hasMoreElements()) {
        Iterator<File> fileIterator;
        File directory;

        try {
            directory = new File(urls.nextElement().toURI());
            fileIterator = FileUtils.iterateFiles(directory, new String[] { "class" }, true);
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }

        while (fileIterator.hasNext()) {
            File file = fileIterator.next();
            try {
                String path = file.getAbsolutePath();
                Class<?> candidate = Class
                        .forName(path.substring(directory.getAbsolutePath().length() + 1, path.length() - 6)
                                .replaceAll("\\/", "."));

                for (Method m : candidate.getDeclaredMethods()) {
                    if (m.isAnnotationPresent(Procedure.class)) {
                        classes.add(candidate);
                    }
                }

            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        }
    }

    return classes;
}

From source file:org.rhq.cassandra.CCMTestNGListener.java

@Override
public void beforeInvocation(IInvokedMethod invokedMethod, ITestResult testResult) {
    Method method = invokedMethod.getTestMethod().getConstructorOrMethod().getMethod();
    if (method.isAnnotationPresent(DeployCluster.class)) {
        try {/*  w  w w  .  j  av a 2  s  .c om*/
            deployCluster(method.getAnnotation(DeployCluster.class));
        } catch (Exception e) {
            log.warn("Failed to deploy cluster", e);
        }
    }
}

From source file:org.os890.ds.addon.spring.impl.CdiAwareBeanFactoryPostProcessor.java

private BeanDefinition createSpringBeanDefinition(Bean<?> cdiBean) throws Exception {
    AbstractBeanDefinition beanDefinition = new GenericBeanDefinition();
    Set<Type> beanTypes = new HashSet<Type>(cdiBean.getTypes());
    beanTypes.remove(Object.class);
    beanTypes.remove(Serializable.class);

    Type beanType = beanTypes.size() == 1 ? beanTypes.iterator().next() : null;

    if (beanType instanceof Class) { //to support producers
        beanDefinition.setBeanClass((Class) beanType);
    } else { //fallback since spring doesn't support multiple types
        beanDefinition.setBeanClass(cdiBean.getBeanClass());
    }/*from  w w w.  j  a  v  a 2  s .com*/

    beanDefinition.setScope(CdiSpringScope.class.getName());

    for (Annotation qualifier : cdiBean.getQualifiers()) {
        if (Any.class.equals(qualifier.annotationType()) || Default.class.equals(qualifier.annotationType())) {
            continue;
        }
        //currently only simple qualifiers are supported
        AutowireCandidateQualifier springQualifier = new AutowireCandidateQualifier(qualifier.annotationType());

        for (Method annotationMethod : qualifier.annotationType().getDeclaredMethods()) {
            if (!annotationMethod.isAnnotationPresent(Nonbinding.class)) {
                springQualifier.setAttribute(annotationMethod.getName(), annotationMethod.invoke(qualifier));
            }
        }
        beanDefinition.addQualifier(springQualifier);
    }
    beanDefinition.setLazyInit(true);
    return beanDefinition;
}

From source file:com.clican.pluto.cms.core.service.impl.DataModelServiceImpl.java

public void convertToDataModel(Map<String, Object> map, IDataModel obj) {
    Method[] methods = obj.getClass().getMethods();
    for (Method method : methods) {
        if (method.isAnnotationPresent(DynamicProperty.class)) {
            DynamicProperty dp = method.getAnnotation(DynamicProperty.class);
            String name = dp.name();
            String firstCharLowerName = name.substring(0, 1).toLowerCase() + name.substring(1);
            Object value = map.get(firstCharLowerName);
            try {
                com.clican.pluto.common.util.BeanUtils.setProperty(obj, firstCharLowerName, value);
            } catch (Exception e) {
                log.error("", e);
            }//from w  w w  . j a v  a 2 s  .co m
        }
    }
}

From source file:com.neelo.glue.ApplicationModule.java

public void register(Class<?> resourceClass) {
    log.info("Registering: " + resourceClass.getName());

    Resource resource = null;//from   ww w . ja v  a  2s  . c om

    if (resourceClass.isAnnotationPresent(Resource.class)) {
        resource = resourceClass.getAnnotation(Resource.class);
    } else {
        log.warn("Class not registerd. Missing " + Resource.class.getSimpleName() + " annotation");
        return;
    }

    ResourceBuilder builder = new ResourceBuilder(resourceClass, resource.path(), resource.layout());

    Method[] methods = resourceClass.getDeclaredMethods();
    for (Method method : methods) {
        if (method.isAnnotationPresent(Get.class)) {
            Get get = method.getAnnotation(Get.class);

            RouteBuilder route = builder.get(method, get.path(), get.content());

            route.setLayout(StringUtils.isNotBlank(get.layout()) ? get.layout() : resource.layout());
            if (StringUtils.isNotBlank(get.name()))
                route.setName(get.name());

            routes.add(route);
        } else if (method.isAnnotationPresent(Post.class)) {
            Post post = method.getAnnotation(Post.class);

            RouteBuilder route = builder.post(method, post.path(), post.content());

            route.setLayout(StringUtils.isNotBlank(post.layout()) ? post.layout() : resource.layout());
            if (StringUtils.isNotBlank(post.name()))
                route.setName(post.name());

            routes.add(route);
        }
    }
}

From source file:com.clican.pluto.cms.core.service.impl.DataModelServiceImpl.java

public Map<String, Object> convertToMap(IDataModel obj) {
    Map<String, Object> map = new HashMap<String, Object>();
    Method[] methods = obj.getClass().getMethods();
    for (Method method : methods) {
        if (method.isAnnotationPresent(DynamicProperty.class)) {
            try {
                Object value = method.invoke(obj, new Object[] {});
                DynamicProperty dp = method.getAnnotation(DynamicProperty.class);
                String name = dp.name();
                String firstCharLowerName = name.substring(0, 1).toLowerCase() + name.substring(1);
                map.put(firstCharLowerName, value);
            } catch (Exception e) {
                log.error("", e);
            }/*w  ww. ja va 2 s.  c om*/

        }
    }
    return map;
}

From source file:org.rhq.cassandra.CCMTestNGListener.java

@Override
public void afterInvocation(IInvokedMethod invokedMethod, ITestResult testResult) {
    Method method = invokedMethod.getTestMethod().getConstructorOrMethod().getMethod();
    if (method.isAnnotationPresent(ShutdownCluster.class)) {
        try {/* w  w  w  . ja v  a  2  s  .  co  m*/
            Boolean skipShutdown = Boolean
                    .valueOf(System.getProperty("rhq.storage.cluster.skip-shutdown", "false"));
            if (!skipShutdown) {
                shutdownCluster();
            }
        } catch (Exception e) {
            log.warn("An error occurred while shutting down the cluster", e);
        }
    }
}

From source file:net.tbnr.gearz.command.NetCommandDispatch.java

/**
 * This will register an object to get calls when any annotated methods ask for them.
 *
 * @param o The object to register./* www .j a  v  a 2  s.c  om*/
 */
public void registerNetCommands(Object o) {
    for (Method m : o.getClass().getDeclaredMethods()) {
        if (!m.isAnnotationPresent(NetCommandHandler.class))
            continue;
        if (m.getParameterTypes().length != 1)
            continue;
        if (!m.getParameterTypes()[0].equals(HashMap.class))
            continue;
        NetCommandHandler annotation = m.getAnnotation(NetCommandHandler.class);
        RegisteredNetCommand command;
        if (!this.netCommands.containsKey(annotation)) {
            command = new RegisteredNetCommand(annotation.name(), Arrays.asList(annotation.args()),
                    new HashMap<Object, Method>());
            this.netCommands.put(annotation, command);
            this.nameToAnnotationMap.put(annotation.name(), annotation); //Other logic
        } else {
            command = this.netCommands.get(annotation);
        }
        command.registerHandler(o, m);
    }
}