Example usage for java.lang.reflect Method getAnnotation

List of usage examples for java.lang.reflect Method getAnnotation

Introduction

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

Prototype

public <T extends Annotation> T getAnnotation(Class<T> annotationClass) 

Source Link

Usage

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

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

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

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

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

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

    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: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.dbs.sdwt.jpa.JpaUtil.java

public <T extends Identifiable<?>> boolean hasSimplePk(T entity) {
    for (Method m : entity.getClass().getMethods()) {
        if (m.getAnnotation(Id.class) != null) {
            return true;
        }//from  w  w w .ja v a 2  s. c  o  m
    }
    for (Field f : entity.getClass().getFields()) {
        if (f.getAnnotation(Id.class) != null) {
            return true;
        }
    }
    return false;
}

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:com.massfords.maven.spel.SpelPlugin.java

/**
 * Scans the classpath looking for instances of the annotation on methods
 * to validate the expressions on the annotation if present.
 *
 * @param reflections//from w ww .  java 2 s.  co  m
 * @param sa
 * @return
 * @throws Exception
 */
private int validateAllAnnotationExpressions(Reflections reflections, SpelAnnotation sa) throws Exception {
    int processedCount = 0;
    Class<? extends Annotation> annoType = sa.getClazz();
    Set<Method> set = reflections.getMethodsAnnotatedWith(annoType);
    for (Method m : set) {
        Annotation anno = m.getAnnotation(annoType);

        Method attrGetter = annoType.getDeclaredMethod(sa.getAttribute());

        Object expressionObj = attrGetter.invoke(anno);
        if (expressionObj instanceof String) {
            getLog().debug(String.format("Validating expression: %s", expressionObj));
            String expression = (String) attrGetter.invoke(anno);
            try {
                processedCount++;
                validator.validate(expression, sa.getExpressionRootClass());
                report.success();
            } catch (ParseException e) {
                String pattern = "Spel annotation %s.%s with expression %s failed to parse. Error message: %s";
                String formatted = String.format(pattern, sa.getName(), sa.getAttribute(), expression,
                        e.getMessage());
                reportError(formatted);
            } catch (ExpressionValidationException e) {
                String message = "Spel annotation %s.%s with expression %s has validation errors. Validation error message: %s";
                String formatted = String.format(message, sa.getName(), sa.getAttribute(), expression,
                        e.getMessage());
                reportError(formatted);
            }
        } else if (expressionObj != null) {
            String message = "Spel annotation %s.%s is not configured with a string.";
            reportError(String.format(message, sa.getName(), sa.getAttribute()));
        }
    }
    return processedCount;
}

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

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

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

    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 annotationStyleJacksonProducesJackson2Annotations()
        throws ClassNotFoundException, SecurityException, NoSuchMethodException {

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

    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:ch.systemsx.cisd.openbis.generic.server.authorization.DefaultAccessController.java

private Set<Role> getMethodRoles(final Method method) {
    synchronized (methodRolesCache) {
        Set<Role> roles = methodRolesCache.get(method);
        if (roles == null) {
            roles = new LinkedHashSet<Role>();
            final RolesAllowed rolesAllowed = method.getAnnotation(RolesAllowed.class);
            if (rolesAllowed != null) {
                roles = rolesAllowed.value().getRoles();
            }/*from   w ww. j  a v  a 2s  .  c o  m*/
            methodRolesCache.put(method, roles);
        }
        return roles;
    }
}

From source file:es.logongas.ix3.rule.impl.RuleEngineImpl.java

@Override
public void fireActionRules(Object rulesObject, RuleContext<T> ruleContext, Class<?>... groups)
        throws BusinessException {
    List<Method> methods = getRuleMethods(rulesObject, ActionRule.class);

    Collections.sort(methods, new Comparator<Method>() {

        @Override/*from  w w w. j ava 2 s.c o  m*/
        public int compare(Method method1, Method method2) {
            ActionRule actionRule1 = method1.getAnnotation(ActionRule.class);
            ActionRule actionRule2 = method2.getAnnotation(ActionRule.class);

            if (actionRule1.priority() == actionRule2.priority()) {
                return 0;
            } else if (actionRule1.priority() > actionRule2.priority()) {
                return 1;
            } else if (actionRule1.priority() < actionRule2.priority()) {
                return -1;
            } else {
                throw new RuntimeException("Error de lgica");
            }

        }

    });

    for (Method method : methods) {
        ActionRule actionRule = method.getAnnotation(ActionRule.class);

        if (isExecuteActionRule(actionRule, groups)) {
            try {
                int numArgs = method.getParameterTypes().length;
                if (numArgs == 0) {
                    method.invoke(rulesObject);
                } else if (numArgs == 1) {
                    Class argumenType = method.getParameterTypes()[0];

                    if (RuleContext.class.isAssignableFrom(argumenType) == false) {
                        throw new RuntimeException("El mtodo " + method.getName()
                                + " debe tener el nico argumento del tipo:" + RuleContext.class.getName());
                    }

                    method.invoke(rulesObject, ruleContext);

                } else {
                    throw new RuntimeException("El mtodo " + method.getName()
                            + " solo puede tener 0 o 1 argumentos pero tiene:" + numArgs);
                }

            } catch (IllegalAccessException | IllegalArgumentException ex) {
                throw new RuntimeException(ex);
            } catch (InvocationTargetException ex) {
                BusinessException businessException = ExceptionUtil.getBusinessExceptionFromThrowable(ex);
                if (businessException != null) {
                    throw businessException;
                } else {
                    throw new RuntimeException(ex);
                }
            }
        }
    }

}