Example usage for java.lang Class getAnnotations

List of usage examples for java.lang Class getAnnotations

Introduction

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

Prototype

public Annotation[] getAnnotations() 

Source Link

Usage

From source file:org.springframework.integration.config.annotation.MessagingAnnotationPostProcessor.java

private boolean isStereotype(Class<?> beanClass) {
    List<Annotation> annotations = new ArrayList<Annotation>(Arrays.asList(beanClass.getAnnotations()));
    Class<?>[] interfaces = beanClass.getInterfaces();
    for (Class<?> iface : interfaces) {
        annotations.addAll(Arrays.asList(iface.getAnnotations()));
    }/*from  ww  w  . j a  v a2  s .c  o  m*/
    for (Annotation annotation : annotations) {
        Class<? extends Annotation> annotationType = annotation.annotationType();
        if (annotationType.equals(Component.class) || annotationType.isAnnotationPresent(Component.class)) {
            return true;
        }
    }
    return false;
}

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

/**
 * Handles all the class level annotation of the given class and manipulates the given validation configuration
 * accordingly./*from  www .  ja v  a  2 s .  c  o m*/
 *
 * @param clazz The annotated class.
 * @param configuration The bean validation configuration to manipulate.
 */
protected void handleClassAnnotations(Class clazz, MutableBeanValidationConfiguration configuration) {
    Annotation[] annotations = clazz.getAnnotations();
    for (Annotation annotation : annotations) {
        ClassValidationAnnotationHandler handler = handlerRegistry.findClassHanlder(annotation, clazz);
        if (handler == null) {
            logger.warn("No hanlder is defined for annotation '" + annotation.annotationType().getName()
                    + "'... Annotation will be ignored...");
        } else {
            handler.handleAnnotation(annotation, clazz, configuration);
        }
    }
}

From source file:org.jsonschema2pojo.integration.config.CustomAnnotatorIT.java

@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void defaultCustomAnnotatorIsNoop()
        throws ClassNotFoundException, SecurityException, NoSuchMethodException {

    ClassLoader resultsClassLoader = schemaRule.generateAndCompile(
            "/schema/properties/primitiveProperties.json", "com.example", config("annotationStyle", "none")); // turn off core annotations

    Class generatedType = resultsClassLoader.loadClass("com.example.PrimitiveProperties");

    Method getter = generatedType.getMethod("getA");

    assertThat(generatedType.getAnnotations().length, is(0));
    assertThat(getter.getAnnotations().length, is(0));
}

From source file:org.springframework.yarn.config.annotation.SpringYarnAnnotationPostProcessor.java

/**
 * Checks if class is a stereotype meaning if there is
 * a Component annotation present.//w  w w .  j av a 2s .c o m
 *
 * @param beanClass the bean class
 * @return true, if is stereotype
 */
private boolean isStereotype(Class<?> beanClass) {
    List<Annotation> annotations = new ArrayList<Annotation>(Arrays.asList(beanClass.getAnnotations()));
    Class<?>[] interfaces = beanClass.getInterfaces();
    for (Class<?> iface : interfaces) {
        annotations.addAll(Arrays.asList(iface.getAnnotations()));
    }
    for (Annotation annotation : annotations) {
        Class<? extends Annotation> annotationType = annotation.annotationType();
        if (annotationType.equals(Component.class) || annotationType.isAnnotationPresent(Component.class)) {
            return true;
        }
    }
    return false;
}

From source file:org.castor.jaxb.reflection.ClassAnnotationProcessingServiceTest.java

/**
 * Test method for/*from   ww w  . j a v a 2 s  .  com*/
 * {@link org.castor.jaxb.annoproc.BaseAnnotationProcessingService#processAnnotations(org.castor.jaxb.reflection.info.Info, java.lang.annotation.Annotation[])}
 * .
 */
@Test
public final void testProcessAnnotations() {
    Class<WithAnnotations> clazz = WithAnnotations.class;
    Annotation[] annotations = clazz.getAnnotations();
    JaxbClassNature classInfo = new JaxbClassNature(new ClassInfo("Franz"));
    classAnnotationProcessingService.processAnnotations(classInfo, annotations);
    Assert.assertEquals("Hugo", classInfo.getRootElementName());
    Assert.assertNull(classInfo.getRootElementNamespace());
    Assert.assertEquals("Franz", classInfo.getTypeName());
    Assert.assertNull(classInfo.getTypeNamespace());
}

From source file:org.castor.jaxb.reflection.ClassAnnotationProcessingServiceTest.java

/**
 * Test method for// w  w  w . ja va  2  s.c o  m
 * {@link org.castor.jaxb.annoproc.BaseAnnotationProcessingService#processAnnotations(org.castor.jaxb.reflection.info.Info, java.lang.annotation.Annotation[])}
 * .
 */
@Test
public void testProcessAnnotationsWithXmlTypeAnnotation() {
    Class<WithXmlTypeAnnotation> clazz = WithXmlTypeAnnotation.class;
    Annotation[] annotations = clazz.getAnnotations();
    JaxbClassNature classInfo = new JaxbClassNature(new ClassInfo(WithXmlTypeAnnotation.class.getName()));
    classAnnotationProcessingService.processAnnotations(classInfo, annotations);

    // XmlRootElement annotations
    Assert.assertNull(classInfo.getRootElementName());
    Assert.assertNull(classInfo.getRootElementNamespace());
    // XmlType annotation
    Assert.assertNull(classInfo.getTypeFactoryClass());
    Assert.assertNull(classInfo.getTypeFactoryMethod());
    Assert.assertEquals("Franz", classInfo.getTypeName());
    Assert.assertNull(classInfo.getTypeNamespace());
    Assert.assertEquals(1, classInfo.getTypeProperties().size());
    Assert.assertEquals("", classInfo.getTypeProperties().get(0));
    // XmlTransient annotation
    Assert.assertFalse(classInfo.getXmlTransient());
    // XmlSeeAlso annotation
    Assert.assertNull(classInfo.getSeeAlsoClasses());
    // XmlAccessorOrder annotations
    Assert.assertNull(classInfo.getXmlAccessOrder());
    // XmlAccessorType annotations
    Assert.assertNull(classInfo.getXmlAccessType());
}

From source file:org.castor.jaxb.reflection.ClassAnnotationProcessingServiceTest.java

@Test
public void testProcessAnnotationsWithXmlSeeAlsoAnnotation() {
    Class<WithXmlSeeAlsoAnnotation> clazz = WithXmlSeeAlsoAnnotation.class;
    Annotation[] annotations = clazz.getAnnotations();
    JaxbClassNature classInfo = new JaxbClassNature(
            new ClassInfo(WithXmlRootElementAnnotation.class.getName()));
    classAnnotationProcessingService.processAnnotations(classInfo, annotations);

    // XmlRootElement annotations
    Assert.assertNull(classInfo.getRootElementName());
    Assert.assertNull(classInfo.getRootElementNamespace());
    // XmlType annotation
    Assert.assertNull(classInfo.getTypeFactoryClass());
    Assert.assertNull(classInfo.getTypeFactoryMethod());
    Assert.assertNull(classInfo.getTypeName());
    Assert.assertNull(classInfo.getTypeNamespace());
    Assert.assertNull(classInfo.getTypeProperties());
    // XmlTransient annotation
    Assert.assertFalse(classInfo.getXmlTransient());
    // XmlSeeAlso annotation
    Assert.assertEquals(1, classInfo.getSeeAlsoClasses().size());
    Assert.assertEquals(WithXmlTransientAnnotation.class, classInfo.getSeeAlsoClasses().get(0));
    // XmlAccessorOrder annotations
    Assert.assertNull(classInfo.getXmlAccessOrder());
    // XmlAccessorType annotations
    Assert.assertNull(classInfo.getXmlAccessType());
}

From source file:net.mojodna.sprout.SproutAutoLoaderPlugIn.java

private void loadForm(Class bean) {
    final Annotation[] annotations = bean.getAnnotations();

    for (int j = 0; j < annotations.length; j++) {
        final Annotation a = annotations[j];
        final Class type = a.annotationType();

        if (type.equals(SproutForm.class)) {
            final SproutForm form = (SproutForm) a;
            String actionFormName = form.name();
            String actionFormType = bean.getName();

            if (log.isDebugEnabled()) {
                log.debug("ActionForm " + actionFormName + " -> " + actionFormType);
            }/*  ww w.  j  a v a  2s .  c o m*/
            getModuleConfig().addFormBeanConfig(new ActionFormBean(actionFormName, actionFormType));
        }
    }
}

From source file:org.castor.jaxb.reflection.ClassAnnotationProcessingServiceTest.java

@Test
public final void testProcessAnnotationsWithXmlTransientAnnotation() {
    Class<WithXmlTransientAnnotation> clazz = WithXmlTransientAnnotation.class;
    Annotation[] annotations = clazz.getAnnotations();
    JaxbClassNature classInfo = new JaxbClassNature(
            new ClassInfo(WithXmlRootElementAnnotation.class.getName()));
    classAnnotationProcessingService.processAnnotations(classInfo, annotations);

    // XmlRootElement annotations
    Assert.assertNull(classInfo.getRootElementName());
    Assert.assertNull(classInfo.getRootElementNamespace());
    // XmlType annotation
    Assert.assertNull(classInfo.getTypeFactoryClass());
    Assert.assertNull(classInfo.getTypeFactoryMethod());
    Assert.assertNull(classInfo.getTypeName());
    Assert.assertNull(classInfo.getTypeNamespace());
    Assert.assertNull(classInfo.getTypeProperties());
    // XmlTransient annotation
    Assert.assertTrue(classInfo.getXmlTransient());
    // XmlSeeAlso annotation
    Assert.assertNull(classInfo.getSeeAlsoClasses());
    // XmlAccessorOrder annotations
    Assert.assertNull(classInfo.getXmlAccessOrder());
    // XmlAccessorType annotations
    Assert.assertNull(classInfo.getXmlAccessType());
}

From source file:org.mule.config.processors.DecoratingAnnotatedServiceProcessor.java

protected void processInboundRouters(Class componentFactoryClass, org.mule.api.service.Service service)
        throws MuleException {
    for (int i = 0; i < componentFactoryClass.getAnnotations().length; i++) {
        Annotation annotation = componentFactoryClass.getAnnotations()[i];
        Router routerAnnotation = annotation.annotationType().getAnnotation(Router.class);
        if (routerAnnotation != null && routerAnnotation.type() == RouterType.Inbound) {
            MessageProcessorAnnotationParser parser = parserFactory.getRouterParser(annotation,
                    componentFactoryClass, null);
            if (parser != null) {
                ((ServiceCompositeMessageSource) service.getMessageSource())
                        .addMessageProcessor(parser.parseMessageProcessor(annotation));
            } else {
                //TODO i18n
                throw new IllegalStateException(
                        "Cannot find parser for router annotation: " + annotation.toString());
            }/*from  w w  w.ja va 2  s  .  c  o  m*/
        }
    }
}