Example usage for java.lang Class getAnnotation

List of usage examples for java.lang Class getAnnotation

Introduction

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

Prototype

@SuppressWarnings("unchecked")
public <A extends Annotation> A getAnnotation(Class<A> annotationClass) 

Source Link

Usage

From source file:com.sixt.service.framework.injection.MetricsReporterModule.java

private Object findPlugin(Injector injector) {
    Object retval = null;// ww w.j  a v a  2  s  .c o m
    ServiceProperties serviceProperties = injector.getInstance(ServiceProperties.class);
    String pluginName = serviceProperties.getProperty("metricsReporter");
    if (StringUtils.isBlank(pluginName)) {
        return null;
    }
    if (plugins == null) { // only scan if not already set
        plugins = new FastClasspathScanner().scan()
                .getNamesOfClassesWithAnnotation(MetricsReporterPlugin.class);
    }
    boolean found = false;
    for (String plugin : plugins) {
        try {
            @SuppressWarnings("unchecked")
            Class<? extends MetricsReporterPlugin> pluginClass = (Class<? extends MetricsReporterPlugin>) Class
                    .forName(plugin);
            MetricsReporterPlugin anno = pluginClass.getAnnotation(MetricsReporterPlugin.class);
            if (anno != null && pluginName.equals(anno.name())) {
                retval = injector.getInstance(pluginClass);
                found = true;
                break;
            }
        } catch (ClassNotFoundException e) {
            logger.error("MetricsReporterPlugin not found", e);
        }
    }
    if (!found) {
        logger.warn("Metrics reporting plugin '{}' was not found in the class path", pluginName);
    }
    return retval;
}

From source file:org.ambraproject.struts2.TransactionInterceptor.java

private <A extends Annotation> A getAnnotation(Class<? extends Action> actionClass, String methodName,
        Class<A> annotationType) throws Exception {
    A annotation = actionClass.getAnnotation(annotationType);
    if (annotation == null) {
        annotation = getMethodAnnotation(actionClass, methodName, annotationType);
    }/* w  ww  .ja  va 2s  .c o m*/

    return annotation;
}

From source file:jofc2.OFC.java

/**
 * ?/*www  .  java  2 s  . c o m*/
 * @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.bytesoft.bytetcc.supports.spring.CompensableAnnotationValidator.java

private void validateTransactionalRollbackFor(Method method, Class<?> clazz, String beanName)
        throws IllegalStateException {
    Transactional transactional = method.getAnnotation(Transactional.class);
    if (transactional == null) {
        Class<?> declaringClass = method.getDeclaringClass();
        transactional = declaringClass.getAnnotation(Transactional.class);
    }//from   w w  w  .j av  a 2 s  . com

    if (transactional == null) {
        throw new IllegalStateException(
                String.format("Method(%s) must be specificed a Transactional annotation!", method));
    }

    String[] rollbackForClassNameArray = transactional.rollbackForClassName();
    if (rollbackForClassNameArray != null && rollbackForClassNameArray.length > 0) {
        throw new IllegalStateException(String.format(
                "The transactional annotation on the confirm/cancel class does not support the property rollbackForClassName yet(beanId= %s)!",
                beanName));
    }

    Class<?>[] rollErrorArray = transactional.rollbackFor();

    Class<?>[] errorTypeArray = method.getExceptionTypes();
    for (int j = 0; errorTypeArray != null && j < errorTypeArray.length; j++) {
        Class<?> errorType = errorTypeArray[j];
        if (RuntimeException.class.isAssignableFrom(errorType)) {
            continue;
        }

        boolean matched = false;
        for (int k = 0; rollErrorArray != null && k < rollErrorArray.length; k++) {
            Class<?> rollbackError = rollErrorArray[k];
            if (rollbackError.isAssignableFrom(errorType)) {
                matched = true;
                break;
            }
        }

        if (matched == false) {
            throw new IllegalStateException(String.format(
                    "The value of Transactional.rollbackFor annotated on method(%s) must includes %s!", method,
                    errorType.getName()));
        }
    }
}

From source file:info.archinnov.achilles.helper.EntityIntrospector.java

public String inferColumnFamilyName(Class<?> entity, String canonicalName) {
    String columnFamilyName = null;
    Entity annotation = entity.getAnnotation(Entity.class);
    if (annotation != null) {
        if (StringUtils.isNotBlank(annotation.table())) {
            columnFamilyName = annotation.table();
        }//  www  . ja  v  a 2s. c o m
    }

    if (!StringUtils.isBlank(columnFamilyName)) {
        columnFamilyName = TableNameNormalizer.normalizerAndValidateColumnFamilyName(columnFamilyName);
    } else {
        columnFamilyName = TableNameNormalizer.normalizerAndValidateColumnFamilyName(canonicalName);
    }

    log.debug("Inferred columnFamilyName for entity {} : {}", canonicalName, columnFamilyName);
    return columnFamilyName;
}

From source file:io.mandrel.OtherTest.java

@Test
public void test() throws JsonProcessingException {

    ObjectMapper objectMapper = new ObjectMapper();

    Class<?> clazz = LinkFilter.class;
    for (JsonSubTypes.Type type : clazz.getAnnotation(JsonSubTypes.class).value()) {
        System.err.println(objectMapper.writerWithDefaultPrettyPrinter()
                .writeValueAsString(new SchemaGenerator(objectMapper).getSchema(type.value())));
    }/*  w  w w .  jav  a 2  s  .c  o  m*/

}

From source file:net.paoding.rose.jade.context.spring.SpringDataSourceFactory.java

@Override
public DataSource getDataSource(StatementMetaData metaData, Map<String, Object> runtimeProperties) {
    Class<?> daoClass = metaData.getDAOMetaData().getDAOClass();
    DataSource dataSource = cached.get(daoClass);
    if (dataSource != null) {
        return dataSource;
    }/* w w w .  j  a va 2  s.c  om*/

    String catalog = daoClass.getAnnotation(DAO.class).catalog();
    if (catalog.length() > 0) {
        dataSource = getDataSourceByDirectory(daoClass, catalog);
    }
    if (dataSource != null) {
        cached.put(daoClass, dataSource);
        return dataSource;
    }
    dataSource = getDataSourceByDirectory(daoClass, daoClass.getName());
    if (dataSource != null) {
        cached.put(daoClass, dataSource);
        return dataSource;
    }
    dataSource = getDataSourceByKey(daoClass, "jade.dataSource");
    if (dataSource != null) {
        cached.put(daoClass, dataSource);
        return dataSource;
    }
    dataSource = getDataSourceByKey(daoClass, "dataSource");
    if (dataSource != null) {
        cached.put(daoClass, dataSource);
    }
    return dataSource;
}

From source file:org.everrest.spring.SpringComponentsLoader.java

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    beanFactory.registerSingleton("org.everrest.lifecycle.SpringEverrestProcessorLifecycle",
            new SpringEverrestProcessorLifecycle(processor));
    beanFactory.registerSingleton("org.everrest.lifecycle.SpringFileCollectorDestroyer",
            new SpringFileCollectorDestroyer(makeFileCollectorDestroyer()));
    EverrestApplication everrest = makeEverrestApplication();

    ResourceDescriptorValidator rdv = ResourceDescriptorValidator.getInstance();
    addAutowiredDependencies(beanFactory);
    for (String beanName : beanFactory.getBeanDefinitionNames()) {
        Class<?> beanClass = beanFactory.getType(beanName);
        if (beanClass.getAnnotation(Provider.class) != null) {
            ProviderDescriptor pDescriptor = new ProviderDescriptorImpl(beanClass);
            pDescriptor.accept(rdv);/*from   w ww . ja v  a 2s . c o  m*/
            everrest.addFactory(new SpringObjectFactory<>(pDescriptor, beanName, beanFactory));
        } else if (beanClass.getAnnotation(Filter.class) != null) {
            FilterDescriptor fDescriptor = new FilterDescriptorImpl(beanClass);
            fDescriptor.accept(rdv);
            everrest.addFactory(new SpringObjectFactory<>(fDescriptor, beanName, beanFactory));
        } else if (beanClass.getAnnotation(Path.class) != null) {
            AbstractResourceDescriptor rDescriptor = new AbstractResourceDescriptorImpl(beanClass);
            rDescriptor.accept(rdv);
            everrest.addFactory(new SpringObjectFactory<>(rDescriptor, beanName, beanFactory));
        }
    }

    processor.addApplication(everrest);
}

From source file:com.izforge.izpack.compiler.container.TestCompilationContainer.java

/**
 * Constructs a <tt>TestCompilationContainer</tt> for a specific test.
 *
 * @param testClass the test class//from w w  w .j  a v a2s . c o m
 * @param method    the test method
 */
public TestCompilationContainer(Class<?> testClass, FrameworkMethod method) {
    super(null);
    InstallFile installFile = method.getAnnotation(InstallFile.class);
    if (installFile == null) {
        installFile = testClass.getAnnotation(InstallFile.class);
    }
    this.installFile = installFile.value();
    initialise();
}

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);/*from   www  .  j a  va2s  .  c o  m*/
    } else {
        logger.debug("Class {} is not annotated with @Path", clazz);
    }
}