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:org.springframework.test.context.ContextLoaderUtils.java

/**
 * Attempt to build a {@link org.springframework.test.context.web.WebMergedContextConfiguration}
 * from the supplied arguments, using reflection in order to avoid package cycles.
 *
 * @return the {@code WebMergedContextConfiguration} or {@code null} if it could not be built
 * @since 3.2//w  w w .  j a v a  2  s.com
 */
@SuppressWarnings("unchecked")
private static MergedContextConfiguration buildWebMergedContextConfiguration(Class<?> testClass,
        String[] locations, Class<?>[] classes,
        Set<Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>> initializerClasses,
        String[] activeProfiles, ContextLoader contextLoader,
        CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate,
        MergedContextConfiguration parentConfig) {

    Class<? extends Annotation> webAppConfigClass = loadWebAppConfigurationClass();

    if (webAppConfigClass != null && testClass.isAnnotationPresent(webAppConfigClass)) {
        Annotation annotation = testClass.getAnnotation(webAppConfigClass);
        String resourceBasePath = (String) AnnotationUtils.getValue(annotation);

        try {
            Class<? extends MergedContextConfiguration> webMergedConfigClass = (Class<? extends MergedContextConfiguration>) ClassUtils
                    .forName(WEB_MERGED_CONTEXT_CONFIGURATION_CLASS_NAME,
                            ContextLoaderUtils.class.getClassLoader());

            Constructor<? extends MergedContextConfiguration> constructor = ClassUtils
                    .getConstructorIfAvailable(webMergedConfigClass, Class.class, String[].class, Class[].class,
                            Set.class, String[].class, String.class, ContextLoader.class,
                            CacheAwareContextLoaderDelegate.class, MergedContextConfiguration.class);

            if (constructor != null) {
                return instantiateClass(constructor, testClass, locations, classes, initializerClasses,
                        activeProfiles, resourceBasePath, contextLoader, cacheAwareContextLoaderDelegate,
                        parentConfig);
            }
        } catch (Throwable t) {
            if (logger.isDebugEnabled()) {
                logger.debug("Could not instantiate [" + WEB_MERGED_CONTEXT_CONFIGURATION_CLASS_NAME + "].", t);
            }
        }
    }

    return null;
}

From source file:org.springframework.test.context.ContextLoaderUtils.java

/**
 * Resolve the {@link ContextLoader} {@linkplain Class class} to use for the supplied
 * list of {@link ContextConfigurationAttributes} and then instantiate and return that
 * {@code ContextLoader}.//  ww w .j a va  2s . c om
 *
 * <p>If the supplied {@code defaultContextLoaderClassName} is {@code null} or
 * <em>empty</em>, depending on the absence or presence of
 * {@link org.springframework.test.context.web.WebAppConfiguration @WebAppConfiguration} either
 * {@code "org.springframework.test.context.support.DelegatingSmartContextLoader"} or
 * {@code "org.springframework.test.context.web.WebDelegatingSmartContextLoader"} will
 * be used as the default context loader class name. For details on the class
 * resolution process, see {@link #resolveContextLoaderClass}.
 *
 * @param testClass the test class for which the {@code ContextLoader} should be
 * resolved; must not be {@code null}
 * @param configAttributesList the list of configuration attributes to process; must
 * not be {@code null} or <em>empty</em>; must be ordered <em>bottom-up</em>
 * (i.e., as if we were traversing up the class hierarchy)
 * @param defaultContextLoaderClassName the name of the default {@code ContextLoader}
 * class to use; may be {@code null} or <em>empty</em>
 * @return the resolved {@code ContextLoader} for the supplied {@code testClass}
 * (never {@code null})
 * @see #resolveContextLoaderClass
 */
static ContextLoader resolveContextLoader(Class<?> testClass,
        List<ContextConfigurationAttributes> configAttributesList, String defaultContextLoaderClassName) {
    Assert.notNull(testClass, "Class must not be null");
    Assert.notEmpty(configAttributesList, "ContextConfigurationAttributes list must not be empty");

    if (!StringUtils.hasText(defaultContextLoaderClassName)) {
        Class<? extends Annotation> webAppConfigClass = loadWebAppConfigurationClass();
        defaultContextLoaderClassName = webAppConfigClass != null
                && testClass.isAnnotationPresent(webAppConfigClass) ? DEFAULT_WEB_CONTEXT_LOADER_CLASS_NAME
                        : DEFAULT_CONTEXT_LOADER_CLASS_NAME;
    }

    Class<? extends ContextLoader> contextLoaderClass = resolveContextLoaderClass(testClass,
            configAttributesList, defaultContextLoaderClassName);

    return instantiateClass(contextLoaderClass, ContextLoader.class);
}

From source file:org.devproof.portal.core.config.factory.DevproofClassPathBeanDefinitionScanner.java

@Override
protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
    try {/*from www  .j  a v a  2s .co m*/
        Class<?> clazz = Class.forName(beanDefinition.getBeanClassName());
        return super.isCandidateComponent(beanDefinition) || clazz.isAnnotationPresent(GenericRepository.class)
                || clazz.isAnnotationPresent(RegisterGenericDataProvider.class);
    } catch (ClassNotFoundException e) {
        logger.fatal(e);
    }
    return super.isCandidateComponent(beanDefinition);
}

From source file:org.kuali.rice.krad.test.KRADTestCase.java

@Override
protected void setUpInternal() throws Exception {
    super.setUpInternal();

    List<Class> classes = TestUtilities.getHierarchyClassesToHandle(getClass(),
            new Class[] { TestDictionaryConfig.class }, new HashSet<String>());

    // if annotation is present then initialize test data dictionary (setup once per suite)
    if (!classes.isEmpty()) {
        ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("TestDataDictionary.xml");
        dd = (DataDictionary) context.getBean("testDataDictionary");

        // add any additional dictionary files required by the test
        for (Class c : classes) {
            if (c.isAnnotationPresent(TestDictionaryConfig.class)) {
                TestDictionaryConfig testDictionaryConfig = (TestDictionaryConfig) c
                        .getAnnotation(TestDictionaryConfig.class);

                String namespaceCode = testDictionaryConfig.namespaceCode();
                String dictionaryFileString = testDictionaryConfig.dataDictionaryFiles();

                String[] dictionaryFiles = StringUtils.split(dictionaryFileString, ",");
                for (String dictionaryFile : dictionaryFiles) {
                    LOG.info("Adding test data dictionary file: " + dictionaryFile);

                    dd.addConfigFileLocation(namespaceCode, dictionaryFile);
                }//from  w w  w  .  jav a 2  s .c  o m
            }
        }

        dd.parseDataDictionaryConfigurationFiles(false);
        dd.validateDD(false); // Validation performs some necessary post-processing of the beans - we need to run this each time we add new files
        dd.performBeanOverrides();
    }
}

From source file:org.fornax.cartridges.sculptor.framework.event.annotation.SubscribeBeanPostProcessor.java

Subscribe getAnnotation(Class<?> clazz) {
    if (clazz == null || clazz == Object.class) {
        return null;
    }//from  w w w  .  ja  v  a  2 s.com
    boolean foundIt = clazz.isAnnotationPresent(Subscribe.class);
    if (foundIt) {
        return clazz.getAnnotation(Subscribe.class);
    }
    // recursive call with super class
    return getAnnotation(clazz.getSuperclass());

}

From source file:com.github.lightdocs.ModelBuilder.java

/**
 * Traverses each method annotations checking if it is annotated with the
 * JAXRS HttpMethod annotation.//from   w  w  w  . jav a 2 s .c  o m
 * 
 * @param method
 *            required.
 * @return null if not found
 */
private javax.ws.rs.HttpMethod findJAXRSHttpMethodAnnotation(Method method) {
    // all GET/POST/etc JAXRS annotations have the HttpMethod JAXRS
    // annotation on them
    for (Annotation a : method.getAnnotations()) {
        Class<? extends Annotation> aType = a.annotationType();
        if (aType.isAnnotationPresent(javax.ws.rs.HttpMethod.class)) {
            return aType.getAnnotation(javax.ws.rs.HttpMethod.class);
        }
    }
    return null;
}

From source file:jails.http.converter.xml.Jaxb2RootElementHttpMessageConverter.java

@Override
protected Object readFromSource(Class<?> clazz, HttpHeaders headers, Source source) throws IOException {
    try {/*ww  w . j a v  a 2 s .c o  m*/
        Unmarshaller unmarshaller = createUnmarshaller(clazz);
        if (clazz.isAnnotationPresent(XmlRootElement.class)) {
            return unmarshaller.unmarshal(source);
        } else {
            JAXBElement jaxbElement = unmarshaller.unmarshal(source, clazz);
            return jaxbElement.getValue();
        }
    } catch (UnmarshalException ex) {
        throw new HttpMessageNotReadableException("Could not unmarshal to [" + clazz + "]: " + ex.getMessage(),
                ex);

    } catch (JAXBException ex) {
        throw new HttpMessageConversionException("Could not instantiate JAXBContext: " + ex.getMessage(), ex);
    }
}

From source file:org.springmodules.validation.bean.conf.loader.annotation.AnnotationBeanValidationConfigurationLoader.java

/**
 * If {@link #isCheckValidatableAnnotation()} return <code>true</code>, then this loader supports the given
 * class only if it has the {@link @Validatable} annotation. Otherwise, all classes are supported.
 *
 * @see BeanValidationConfigurationLoader#supports(Class)
 *///from   ww w .  j a  v a  2  s .com
public boolean supports(Class clazz) {
    return (checkValidatableAnnotation) ? clazz.isAnnotationPresent(Validatable.class) : true;
}

From source file:com.jayway.jaxrs.hateoas.DefaultHateoasContext.java

@Override
public void mapClass(Class<?> clazz) {
    if (clazz.isAnnotationPresent(Path.class)) {
        String rootPath = clazz.getAnnotation(Path.class).value();
        mapClass(clazz, rootPath);//  w w w .ja va 2 s  .co m
    } else {
        logger.debug("Class {} is not annotated with @Path", clazz);
    }
}

From source file:com.alliander.osgp.acceptancetests.SpringInstantiationStrategy.java

@Override
public InstantiationState instantiate(final Class<?> markedClass, final Object parameter)
        throws InvocationTargetException, InstantiationException, IllegalAccessException {

    final InstantiationStateCreator creator = new InstantiationStateCreator();

    if (markedClass.isAnnotationPresent(Configurable.class)) {
        final AutowireCapableBeanFactory factory = this.applicationContext.getAutowireCapableBeanFactory();
        final Object object = factory.createBean(markedClass, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME,
                true);/*  w w  w . j av  a 2  s  . com*/
        return creator.didInstantiate(object);
    }

    return creator.didNotInstantiate();
}