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

    Class generatedType = schemaRule.generateAndCompile("/schema/properties/primitiveProperties.json",
            "com.example", config("annotationStyle", "jackson2")).loadClass("com.example.PrimitiveProperties");

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

    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.reactivetechnologies.platform.rest.depre.RequestDispatchers.java

private void addAnnotatedClass(Class<?> restletClass) throws InstantiationException, IllegalAccessException {
    final JAXRSInstanceMetadata proxy = new JAXRSInstanceMetadata(restletClass.newInstance());
    if (restletClass.isAnnotationPresent(Path.class)) {
        String rootUri = restletClass.getAnnotation(Path.class).value();
        proxy.setRootUri(rootUri);/*from   ww  w.ja  v  a 2  s . c om*/
    }
    ReflectionUtils.doWithMethods(restletClass, new MethodCallback() {

        @Override
        public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
            String subUri = "";
            if (method.isAnnotationPresent(Path.class)) {
                subUri = method.getAnnotation(Path.class).value();
            }
            if (method.isAnnotationPresent(GET.class)) {
                MethodDetail m = new MethodDetail(method);
                Annotation[][] mAnnots = method.getParameterAnnotations();
                int i = 0;
                for (Annotation[] annots : mAnnots) {
                    for (Annotation annot : annots) {
                        if (annot.annotationType() == QueryParam.class) {
                            m.setQueryParam(i, ((QueryParam) annot).value());
                        }
                    }
                    i++;
                }
                proxy.addGetMethod(proxy.getRootUri() + subUri, m);
            }
            if (method.isAnnotationPresent(POST.class)) {
                MethodDetail m = new MethodDetail(method);
                Annotation[][] mAnnots = method.getParameterAnnotations();
                int i = 0;
                for (Annotation[] annots : mAnnots) {
                    for (Annotation annot : annots) {
                        if (annot.annotationType() == QueryParam.class) {
                            m.setQueryParam(i, ((QueryParam) annot).value());
                        }
                    }
                    i++;
                }
                proxy.addPostMethod(proxy.getRootUri() + subUri, m);
            }

        }
    }, new MethodFilter() {

        @Override
        public boolean matches(Method method) {
            return (method.isAnnotationPresent(GET.class) || method.isAnnotationPresent(POST.class));
        }
    });

    allClasses.add(proxy);
}

From source file:com.lp.server.util.logger.HvDtoLogger.java

private Object getEntityCBez(Method theMethod, Object primarykey) {
    if (primarykey == null)
        return primarykey;

    HvDtoLogIdCBez annotationId = theMethod.getAnnotation(HvDtoLogIdCBez.class);
    Object o = em.find(annotationId.entityClass(), primarykey);
    if (o != null) {
        o = ((ICBez) o).getCBez();/*from w  w  w  .ja  va2  s . co m*/
    }

    return o;
}

From source file:org.liquibase.test.junit.LiquibaseTestExecutionListener.java

/**
 * Called from spring before a test method will be invoked.
 * /*from   w  w w .ja v a  2 s .  c  o  m*/
 * @param testContext
 *            default test context filled from spring
 * 
 * @throws Exception
 *             if any error occurred
 */
@Override
public void beforeTestMethod(final TestContext testContext) throws Exception {
    final Method testMethod = testContext.getTestMethod();

    final Annotation annotation = testMethod.getAnnotation(LiquibaseTest.class);

    executeChanglog(testContext, (LiquibaseTest) annotation);
}

From source file:com.evolveum.midpoint.repo.sql.query2.definition.ClassDefinitionParser.java

private Class getJaxbClass(Method method, Class returnedClass) {
    JaxbType annotation = (JaxbType) method.getAnnotation(JaxbType.class);
    if (annotation != null) {
        return annotation.type();
    }/*from   w ww  .j  ava 2  s. c  o m*/
    Class classFromEntity = getJaxbClassForEntity(returnedClass);
    if (classFromEntity != null) {
        return classFromEntity;
    }
    Package returnedClassPkg = returnedClass.getPackage();
    Package dataObjectsPkg = Marker.class.getPackage();
    if (returnedClassPkg != null && returnedClassPkg.getName().startsWith(dataObjectsPkg.getName())) {
        return null;
    } else {
        return returnedClass; // probably the JAXB value
    }
}

From source file:com.lp.server.util.logger.HvDtoLogger.java

private Object getEntityCnr(Method theMethod, Object primarykey) {
    if (primarykey == null)
        return primarykey;

    HvDtoLogIdCnr annotationIdCnr = theMethod.getAnnotation(HvDtoLogIdCnr.class);
    Object o = em.find(annotationIdCnr.entityClass(), primarykey);
    if (o != null) {
        o = ((ICNr) o).getCNr();//from  w w  w .ja  v a  2s . c  o m
    }

    return o;
}

From source file:net.minder.config.impl.DefaultConfigurationInjector.java

private void injectMethodValue(Method method, Object target, ConfigurationAdapter adapter,
        ConfigurationBinding binding) throws ConfigurationException {
    Configure methodTag = method.getAnnotation(Configure.class);
    if (methodTag != null) {
        Alias aliasTag = method.getAnnotation(Alias.class);
        String methodName = getConfigName(method, aliasTag);
        Class[] argTypes = method.getParameterTypes();
        Object[] args = new Object[argTypes.length];
        Annotation[][] argTags = method.getParameterAnnotations();
        for (int i = 0; i < argTypes.length; i++) {
            String argName = getConfigName(methodName, argTags[i]);
            String bndName = getBindName(target, argName, binding);
            Object argValue = retrieveValue(target, bndName, argName, argTypes[i], adapter, binding);
            if (argValue == null) {
                Default defTag = findAnnotation(argTags[i], Default.class);
                if (defTag != null) {
                    String strValue = defTag.value();
                    argValue = convertValue(target, argName, strValue, argTypes[i]);
                } else {
                    throw new ConfigurationException(
                            String.format("Failed to find configuration for %s of %s via %s", bndName, argName,
                                    target.getClass().getName(), adapter.getClass().getName()));
                }/*from  www.  j  a  v a 2 s .  c o m*/
            }
            args[i] = argValue;
        }
        if (!method.isAccessible()) {
            method.setAccessible(true);
        }
        try {
            method.invoke(target, args);
        } catch (Exception e) {
            throw new ConfigurationException(String.format("Failed to inject method configuration via %s of %s",
                    methodName, target.getClass().getName()), e);
        }
    }
}

From source file:com.dbs.sdwt.jpa.JpaUtil.java

public <T extends Identifiable<?>> String compositePkPropertyName(T entity) {
    Class<?> entityClass = entity.getClass();
    if (compositePkCache.containsKey(entityClass)) {
        return compositePkCache.get(entityClass);
    }//from   w w  w . j a v a  2s .  co  m

    for (Method m : entity.getClass().getMethods()) {
        if (m.getAnnotation(EmbeddedId.class) != null) {
            String propertyName = methodToProperty(m);
            compositePkCache.put(entityClass, propertyName);
            return propertyName;
        }
    }
    for (Field f : entity.getClass().getFields()) {
        if (f.getAnnotation(EmbeddedId.class) != null) {
            String propertyName = f.getName();
            compositePkCache.put(entityClass, propertyName);
            return propertyName;
        }
    }
    compositePkCache.put(entityClass, null);
    return null;
}

From source file:io.milton.http.annotated.AbstractAnnotationHandler.java

@Override
public void parseController(Object controller) {
    for (java.lang.reflect.Method m : controller.getClass().getMethods()) {
        Annotation a = m.getAnnotation(annoClass);
        if (a != null) {
            Class<?>[] params = m.getParameterTypes();
            if (params == null || params.length == 0) {
                throw new RuntimeException(
                        "Invalid controller method: " + m.getName() + " does not have a source argument");
            }//from   w ww  .j  av a  2 s. c om
            Class sourceType = params[0];
            ControllerMethod cm = new ControllerMethod(controller, m, sourceType, a);
            controllerMethods.add(cm);
        }
    }
}