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.googlecode.jsonschema2pojo.integration.config.AnnotationStyleIT.java

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

    File generatedOutputDirectory = generate("/schema/properties/primitiveProperties.json", "com.example",
            config("annotationStyle", "jackson2"));

    assertThat(generatedOutputDirectory, not(containsText("org.codehaus.jackson")));
    assertThat(generatedOutputDirectory, containsText("com.fasterxml.jackson"));

    ClassLoader resultsClassLoader = compile(generatedOutputDirectory);

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

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

    assertThat(generatedType.getAnnotation(JsonPropertyOrder.class), is(notNullValue()));
    assertThat(generatedType.getAnnotation(JsonInclude.class), is(notNullValue()));
    assertThat(getter.getAnnotation(JsonProperty.class), is(notNullValue()));
}

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

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

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

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

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

    assertThat(generatedType.getAnnotation(JsonPropertyOrder.class), is(nullValue()));
    assertThat(generatedType.getAnnotation(JsonSerialize.class), is(nullValue()));
    assertThat(getter.getAnnotation(JsonProperty.class), is(nullValue()));

}

From source file:org.bytesoft.bytetcc.supports.spring.CompensableAnnotationValidator.java

private void validateTransactionalPropagation(Method method, Class<?> clazz) throws IllegalStateException {
    Transactional transactional = method.getAnnotation(Transactional.class);
    if (transactional == null) {
        Class<?> declaringClass = method.getDeclaringClass();
        transactional = declaringClass.getAnnotation(Transactional.class);
    }/*ww w. j  a va2  s . c o m*/

    if (transactional == null) {
        throw new IllegalStateException(
                String.format("Method(%s) must be specificed a Transactional annotation!", method));
    }
    Propagation propagation = transactional.propagation();
    if (Propagation.REQUIRED.equals(propagation) == false //
            && Propagation.MANDATORY.equals(propagation) == false //
            && Propagation.REQUIRES_NEW.equals(propagation) == false) {
        throw new IllegalStateException(
                String.format("Method(%s) not support propagation level: %s!", method, propagation.name()));
    }
}

From source file:com.github.djabry.platform.vaadin.navigation.NavigationSecurity.java

private boolean isSecuredClass(Class oClass) {
    Set<Class<? extends Annotation>> annotationsToCheck = Sets.newHashSet(Secured.class, PreAuthorize.class);
    for (Class<? extends Annotation> annotation : annotationsToCheck) {
        if (oClass.getAnnotation(annotation) != null) {
            return true;
        }/*from  ww w .  ja  v a2  s  .c  om*/
    }

    return false;
}

From source file:com.googlecode.jsonschema2pojo.integration.config.AnnotationStyleIT.java

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

    File generatedOutputDirectory = generate("/schema/properties/primitiveProperties.json", "com.example",
            config("annotationStyle", "jackson1"));

    assertThat(generatedOutputDirectory, not(containsText("com.fasterxml.jackson")));
    assertThat(generatedOutputDirectory, containsText("org.codehaus.jackson"));

    ClassLoader resultsClassLoader = compile(generatedOutputDirectory);

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

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

    assertThat(generatedType.getAnnotation(org.codehaus.jackson.annotate.JsonPropertyOrder.class),
            is(notNullValue()));//from   w ww.jav a 2 s .  c  om
    assertThat(generatedType.getAnnotation(org.codehaus.jackson.map.annotate.JsonSerialize.class),
            is(notNullValue()));
    assertThat(getter.getAnnotation(org.codehaus.jackson.annotate.JsonProperty.class), is(notNullValue()));
}

From source file:net.paslavsky.springrest.SpringAnnotationPreprocessor.java

@Override
public RestMethodMetadata parse(Class<?> clientClass, Method method) {
    RestMethodMetadata metadata = new RestMethodMetadata();

    RequestMapping classMapping = clientClass.getAnnotation(RequestMapping.class);
    RequestMapping methodMapping = method.getAnnotation(RequestMapping.class);

    metadata.setCommonPath(getPath(classMapping));
    metadata.setAdditionalPath(getPath(methodMapping));
    metadata.setHttpMethod(getHttpMethod(classMapping, methodMapping));
    metadata.setResponseClass(getResponseType(method));
    metadata.setMethodReturnType(method.getReturnType());
    metadata.setRequestHeaderParameters(getParametersWithAnnotation(method, RequestHeader.class));
    metadata.setUriVarParameters(getParametersWithAnnotation(method, PathVariable.class));
    metadata.setQueryParameters(getParametersWithAnnotation(method, RequestParam.class));
    metadata.setRequestParameter(getRequestParameter(method));

    return metadata;
}

From source file:com.googlecode.jsonschema2pojo.integration.config.CustomAnnotatorIT.java

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

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

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

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

    assertThat(generatedType.getAnnotation(Deprecated.class), is(notNullValue()));
    assertThat(generatedType.getAnnotation(Deprecated.class), is(notNullValue()));
    assertThat(getter.getAnnotation(Deprecated.class), is(notNullValue()));
}

From source file:com.smbtec.xo.tinkerpop.blueprints.impl.TinkerPopMetadataFactory.java

@Override
public PropertyMetadata createPropertyMetadata(final PropertyMethod propertyMethod) {
    final Property property = propertyMethod.getAnnotationOfProperty(Property.class);
    final String name = property != null ? property.value() : propertyMethod.getName();
    final Indexed indexedAnnotation = propertyMethod.getAnnotation(Indexed.class);
    if (indexedAnnotation != null) {
        final Class<?> declaringClass = propertyMethod.getAnnotatedElement().getDeclaringClass();
        Class<? extends Element> type = null;
        if (declaringClass.getAnnotation(Vertex.class) != null) {
            type = com.tinkerpop.blueprints.Vertex.class;
        } else if (declaringClass.getAnnotation(Edge.class) != null) {
            type = com.tinkerpop.blueprints.Edge.class;
        } else {//from   w  ww.  jav  a2  s . co m
            throw new XOException("Property '" + name
                    + "' was found with index annotation, but the declaring type is neither a vertex nor an edge.");
        }
        return new PropertyMetadata(name, type, getIndexParameter(indexedAnnotation.parameters()));
    } else {
        return new PropertyMetadata(name);
    }
}

From source file:com.googlecode.jsonschema2pojo.integration.config.CustomAnnotatorIT.java

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

    ClassLoader resultsClassLoader = generateAndCompile("/schema/properties/primitiveProperties.json",
            "com.example", config("customAnnotator", DeprecatingAnnotator.class.getName()));

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

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

    assertThat(generatedType.getAnnotation(JsonPropertyOrder.class), is(notNullValue()));
    assertThat(generatedType.getAnnotation(JsonInclude.class), is(notNullValue()));
    assertThat(getter.getAnnotation(JsonProperty.class), is(notNullValue()));

    assertThat(generatedType.getAnnotation(Deprecated.class), is(notNullValue()));
    assertThat(generatedType.getAnnotation(Deprecated.class), is(notNullValue()));
    assertThat(getter.getAnnotation(Deprecated.class), is(notNullValue()));
}

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  ww  w  .  jav  a  2  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;
}