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.castor.jaxb.reflection.ClassAnnotationProcessingServiceTest.java

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

    // XmlRootElement annotations
    Assert.assertEquals("Hugo", 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.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 testProcessAnnotationsWithXmlAccessorTypeAnnotation() {
    Class<WithXmlAccessorTypeAnnotation> clazz = WithXmlAccessorTypeAnnotation.class;
    Annotation[] annotations = clazz.getAnnotations();
    JaxbClassNature classInfo = new JaxbClassNature(
            new ClassInfo(WithXmlAccessorTypeAnnotation.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.assertNull(classInfo.getSeeAlsoClasses());
    // XmlAccessorOrder annotations
    Assert.assertNull(classInfo.getXmlAccessOrder());
    // XmlAccessorType annotations
    Assert.assertEquals(XmlAccessType.PUBLIC_MEMBER, classInfo.getXmlAccessType());
}

From source file:ar.com.zauber.commons.repository.utils.SpringInjectionInterceptor.java

/**
 * @param persistibleClasses persistible classes that may requiere dependency
 *                           injection/*from  w w w .  j  a  v  a  2 s .c  om*/
 */
public SpringInjectionInterceptor(final List<Class<?>> persistibleClasses) {
    Validate.noNullElements(persistibleClasses);

    for (final Class<?> clazz : persistibleClasses) {
        final List<Entry<Field, String>> fields = new LinkedList<Entry<Field, String>>();
        for (final Annotation annotation : clazz.getAnnotations()) {
            if (annotation instanceof Configurable) {
                final List<Field> clazzFields = new LinkedList<Field>();
                Class<?> c = clazz;
                while (c != null) {
                    clazzFields.addAll(Arrays.asList(c.getDeclaredFields()));
                    c = c.getSuperclass();
                }

                for (final Field field : clazzFields) {
                    for (final Annotation a : field.getAnnotations()) {
                        if (a instanceof Qualifier) {
                            String n = ((Qualifier) a).value();
                            if (StringUtils.isBlank(n)) {
                                n = field.getName();
                            }
                            final String name = n;
                            fields.add(new Entry<Field, String>() {
                                public Field getKey() {
                                    return field;
                                }

                                public String getValue() {
                                    return name;
                                }

                                public String setValue(final String value) {
                                    return null;
                                }
                            });
                        }
                    }
                }
            }
        }

        if (fields.size() > 0) {
            dependencyCache.put(clazz,
                    new DependencyInjection(fields, InitializingBean.class.isAssignableFrom(clazz)));
        }
    }
}

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

@Test
public void testProcessAnnotationsWithXmlAccessorOrderAnnotation() {
    Class<WithXmlAccessorOrderAnnotation> clazz = WithXmlAccessorOrderAnnotation.class;
    Annotation[] annotations = clazz.getAnnotations();
    JaxbClassNature classInfo = new JaxbClassNature(
            new ClassInfo(WithXmlAccessorOrderAnnotation.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.assertNull(classInfo.getSeeAlsoClasses());
    // XmlAccessorOrder annotations
    Assert.assertEquals(XmlAccessOrder.UNDEFINED, classInfo.getXmlAccessOrder());
    // XmlAccessorType annotations
    Assert.assertNull(classInfo.getXmlAccessType());
}

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

@Test
public void testProcessAnnotationsWithXmlAccessorTypeNONEAnnotation() {
    Class<WithXmlAccessorTypeAnnotationNONE> clazz = WithXmlAccessorTypeAnnotationNONE.class;
    Annotation[] annotations = clazz.getAnnotations();
    JaxbClassNature classInfo = new JaxbClassNature(
            new ClassInfo(WithXmlAccessorTypeAnnotationNONE.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.assertNull(classInfo.getSeeAlsoClasses());
    // XmlAccessorOrder annotations
    Assert.assertNull(classInfo.getXmlAccessOrder());
    // XmlAccessorType annotations
    Assert.assertEquals(XmlAccessType.NONE, classInfo.getXmlAccessType());
}

From source file:com.conversantmedia.mapreduce.tool.annotation.handler.MaraAnnotationUtil.java

/**
 * Use any relevant annotations on the supplied class to configure
 * the given job./*  www  . jav a 2s. c  om*/
 * 
 * @param clazz            the class to reflect for mara-related annotations
 * @param job            the job being configured
 * @throws ToolException   if reflection fails for any reason
 */
public void configureJobFromClass(Class<?> clazz, Job job) throws ToolException {

    configureJobFromAnnotations(job, clazz.getAnnotations(), clazz);

    for (Field field : clazz.getDeclaredFields()) {
        configureJobFromAnnotations(job, field.getAnnotations(), field);
    }
    for (Method method : clazz.getDeclaredMethods()) {
        configureJobFromAnnotations(job, method.getAnnotations(), method);
    }
}

From source file:com.ottogroup.bi.spqr.repository.CachedComponentClassLoader.java

/**
 * Steps through {@link Annotation} list, searches for {@link SPQRComponent} 
 * annotation and returns true if any is found
 * @param clazz//from  w  w  w.ja va  2 s .  c  o  m
 * @return
 */
protected Annotation getSPQRComponentAnnotation(final Class<?> clazz) {
    Annotation[] annotations = clazz.getAnnotations();
    if (annotations != null && annotations.length > 0) {
        for (Annotation annotation : annotations) {
            @SuppressWarnings("unchecked")
            Class<Annotation> type = (Class<Annotation>) annotation.annotationType();
            if (StringUtils.equals(type.getName(), SPQRComponent.class.getName())) {
                return annotation;
            }
        }
    }
    return null;
}

From source file:com.github.reinert.jjschema.HyperSchemaGeneratorV4.java

private <T> ObjectNode generateHyperSchemaFromResource(Class<T> type) throws TypeException {
    ObjectNode schema = null;/*from   w ww.j  av a2  s  .  c om*/

    Annotation[] ans = type.getAnnotations();
    boolean hasPath = false;
    for (Annotation a : ans) {
        if (a instanceof Path) {
            hasPath = true;
            Path p = (Path) a;
            if (schema == null) {
                schema = jsonSchemaGenerator.createInstance();
            }
            schema.put("pathStart", p.value());
        }
        if (a instanceof Produces) {
            Produces p = (Produces) a;
            if (schema == null) {
                schema = jsonSchemaGenerator.createInstance();
            }
            schema.put(MEDIA_TYPE, p.value()[0]);
        }
    }
    if (!hasPath) {
        throw new RuntimeException("Invalid Resource class. Must use Path annotation.");
    }

    ArrayNode links = schema.putArray("links");

    for (Method method : type.getDeclaredMethods()) {
        try {
            ObjectNode link = generateLink(method);
            if ("GET".equals(link.get("method").asText()) && "#".equals(link.get("href").asText())) {
                jsonSchemaGenerator.mergeSchema(schema, (ObjectNode) link.get("targetSchema"), true);
            } else {
                links.add(link);
            }
        } catch (InvalidLinkMethod e) {
            e.printStackTrace();
        }
    }

    return schema;
}

From source file:org.springframework.core.type.classreading.AbstractRecursiveAnnotationVisitor.java

private void registerMetaAnnotations(Class<?> annotationClass) {
    // Register annotations that the annotation type is annotated with.
    Set<String> metaAnnotationTypeNames = new LinkedHashSet<String>();
    for (Annotation metaAnnotation : annotationClass.getAnnotations()) {
        metaAnnotationTypeNames.add(metaAnnotation.annotationType().getName());
        if (!this.attributesMap.containsKey(metaAnnotation.annotationType().getName())) {
            this.attributesMap.put(metaAnnotation.annotationType().getName(),
                    AnnotationUtils.getAnnotationAttributes(metaAnnotation, true, true));
        }/*from   w w w  .j a  v  a  2  s  .co  m*/
        for (Annotation metaMetaAnnotation : metaAnnotation.annotationType().getAnnotations()) {
            metaAnnotationTypeNames.add(metaMetaAnnotation.annotationType().getName());
        }
    }
    if (this.metaAnnotationMap != null) {
        this.metaAnnotationMap.put(annotationClass.getName(), metaAnnotationTypeNames);
    }
}

From source file:me.bayes.vertx.vest.VestApplication.java

private void addScannedClasses() {

    PackageHelper helper = new PackageHelper(classLoader);

    for (String packageName : packagesToScan) {

        final String folderLocationName = packageName.replaceAll("[.]", "/");

        try {/*from  w  ww . j a va 2 s . c om*/

            for (JavaFileObject javaObject : helper.find(packageName)) {

                try {

                    final String clazzUriString = javaObject.toUri().toString();
                    final String clazzName = clazzUriString
                            .substring(clazzUriString.lastIndexOf(folderLocationName),
                                    clazzUriString.lastIndexOf('.'))
                            .replaceAll("/", ".");
                    final Class<?> clazz = classLoader.loadClass(clazzName);

                    final Annotation[] annotations = clazz.getAnnotations();
                    Integer priority = DEFAULT_PROVIDER_PRIORITY;
                    Class<?> provider = null;
                    for (Annotation annotation : annotations) {
                        if (annotation.annotationType().equals(Path.class)) {
                            this.endpointClasses.add(clazz);
                            break;
                        } else if (annotation.annotationType().equals(Provider.class)) {
                            provider = clazz;
                        } else if (annotation.annotationType().equals(Priority.class)) {
                            priority = ((Priority) annotation).value();
                        }
                    }
                    if (provider != null) {
                        addProvider(priority, provider);
                    }
                } catch (ClassNotFoundException e) {
                    LOG.error(String.format("Error occurred scanning package %s.", packageName), e);
                }
            }

        } catch (IOException e) {
            LOG.error(String.format("Error occurred scanning package %s.", packageName), e);
        }
    }

}