Example usage for java.lang Class isAnnotationPresent

List of usage examples for java.lang Class isAnnotationPresent

Introduction

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

Prototype

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

Source Link

Usage

From source file:io.coala.json.JsonUtil.java

/**
 * @param om the {@link ObjectMapper} used to parse/deserialize/unmarshal
 * @param type the {@link Class} to register
 * @param imports the {@link Properties} instances for default values, etc.
 * @return//from w w  w.j a va2 s .co  m
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T> Class<T> checkRegistered(final ObjectMapper om, final Class<T> type,
        final Properties... imports) {
    synchronized (JSON_REGISTRATION_CACHE) {
        if (type.isPrimitive())
            return type;
        Set<Class<?>> cache = JSON_REGISTRATION_CACHE.computeIfAbsent(om, key -> new HashSet<>());
        if (type.getPackage() == Object.class.getPackage() || type.getPackage() == Collection.class.getPackage()
                || type.isPrimitive()
                // assume java.lang.* and java.util.* are already mapped
                || TreeNode.class.isAssignableFrom(type) || cache.contains(type))
            return type;

        // use Class.forName(String) ?
        // see http://stackoverflow.com/a/9130560

        //         LOG.trace( "Register JSON conversion of type: {}", type    );
        if (type.isAnnotationPresent(BeanProxy.class)) {
            //            if( !type.isInterface() )
            //               return Thrower.throwNew( IllegalArgumentException.class,
            //                     "@{} must target an interface, but annotates: {}",
            //                     BeanProxy.class.getSimpleName(), type );

            DynaBean.registerType(om, type, imports);
            checkRegisteredMembers(om, type, imports);
            // LOG.trace("Registered Dynabean de/serializer for: " + type);
        } else if (Wrapper.class.isAssignableFrom(type)) {
            Wrapper.Util.registerType(om, (Class<? extends Wrapper>) type);
            checkRegisteredMembers(om, type, imports);
            // LOG.trace("Registered Wrapper de/serializer for: " + type);
        }
        // else
        // LOG.trace("Assume default de/serializer for: " + type);

        cache.add(type);

        return type;
    }
}

From source file:net.java.javabuild.ExecuteMojo.java

private void processClass(String className) throws MalformedURLException, ClassNotFoundException,
        InstantiationException, IllegalAccessException, InvocationTargetException, IOException {
    Class<?> theClass = classLoader.loadClass(className);
    if (theClass.isAnnotationPresent(Builder.class)) {
        Object instance = theClass.newInstance();
        Method[] methods = theClass.getDeclaredMethods();
        for (int j = 0; j < methods.length; j++) {
            Method method = methods[j];
            Execute execute = method.getAnnotation(Execute.class);
            if (execute != null) {
                buildPlan.addMethodExecution(execute.phase(), instance, method);
            }// w  w  w  .j ava  2s .  c  o  m
        }
    }
}

From source file:grails.plugin.springsecurity.acl.AclAutoProxyCreator.java

protected boolean beanIsAnnotated(final Class<?> c) {
    for (Class<? extends Annotation> annotation : ANNOTATIONS) {
        if (c.isAnnotationPresent(annotation)) {
            return true;
        }//from w  ww . ja va 2s  .com

        for (Method method : c.getMethods()) {
            if (method.isAnnotationPresent(annotation)) {
                return true;
            }
        }
    }
    return false;
}

From source file:jofc2.OFC.java

/**
 * ?//w  w w.  j  ava  2s. com
 * @param c
 */
private void doRegisterConverter(Class<?> c) {
    if (c.isAnnotationPresent(Converter.class)) {
        Class<? extends ConverterMatcher> clazz = c.getAnnotation(Converter.class).value();
        try {
            if (SingleValueConverter.class.isAssignableFrom(clazz)) {
                converter.registerConverter((SingleValueConverter) clazz.newInstance());
            } else {
                converter
                        .registerConverter((com.thoughtworks.xstream.converters.Converter) clazz.newInstance());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

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 w  w w.j a v a2s.com*/

    // 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.payu.ratel.register.ServiceRegisterPostProcessor.java

private boolean isService(Object o, String beanName) {
    Class realBeanClazz = getRealBeanClass(o, beanName);

    return !realBeanClazz.isInterface() && realBeanClazz.isAnnotationPresent(Publish.class);
}

From source file:org.jboss.arquillian.spring.integration.javaconfig.utils.DefaultConfigurationClassesProcessor.java

/**
 * Tries to find all default configuration classes for given testClass
 *
 * @param testClass - test class to check
 *
 * @return - list of all inner static classes marked with @Configuration annotation
 *///from   ww  w . j  a v a2 s .co m
private Class<?>[] defaultConfigurationClasses(Class testClass) {
    Class<?>[] configurationCandidates = testClass.getClasses();
    Set<Class<?>> configurationClasses = new HashSet<Class<?>>();

    for (Class<?> configurationCandidate : configurationCandidates) {
        if (configurationCandidate.isAnnotationPresent(Configuration.class)) {
            validateConfigurationCandidate(configurationCandidate);
            configurationClasses.add(configurationCandidate);
        }
    }

    return configurationClasses.toArray(new Class<?>[0]);
}

From source file:com.feedzai.fos.server.remote.impl.RemoteInterfacesTest.java

@Test
public void testRemoteInterfaces() throws Exception {
    for (Class klass : getClasses("com.feedzai.fos.server.remote.impl")) {
        if (klass.isAnnotationPresent(RemoteInterface.class)) {
            RemoteInterface implementation = (RemoteInterface) klass.getAnnotation(RemoteInterface.class);
            Class<?> matchingClass = implementation.of();
            testMatch(matchingClass, klass, 0);
        }//from w w w  .j a  v a  2s .  c  o m
    }
}

From source file:org.nuxeo.ecm.webengine.jaxrs.ApplicationHost.java

@Override
public synchronized Set<Class<?>> getClasses() {
    HashSet<Class<?>> result = new HashSet<Class<?>>();
    for (ApplicationFragment app : getApplications()) {
        try {/*w ww  . ja  v a2s .  com*/
            for (Class<?> clazz : app.getClasses()) {
                if (clazz.isAnnotationPresent(Path.class)) {
                    class2Bundles.put(clazz, app.getBundle());
                }
                result.add(clazz);
            }
        } catch (java.lang.LinkageError e) {
            log.error(e);
        }
    }
    return result;
}

From source file:org.callimachusproject.rewrite.RewriteAdvice.java

private String getSystemId(Method m) {
    if (m.isAnnotationPresent(Iri.class))
        return m.getAnnotation(Iri.class).value();
    Class<?> dclass = m.getDeclaringClass();
    String mame = m.getName();//from  w  w w . j a v  a2  s. co m
    if (dclass.isAnnotationPresent(Iri.class)) {
        String url = dclass.getAnnotation(Iri.class).value();
        if (url.indexOf('#') >= 0)
            return url.substring(0, url.indexOf('#') + 1) + mame;
        return url + "#" + mame;
    }
    String name = dclass.getSimpleName() + ".class";
    URL url = dclass.getResource(name);
    if (url != null)
        return url.toExternalForm() + "#" + mame;
    return "java:" + dclass.getName() + "#" + mame;
}