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:candr.yoclip.option.OptionPropertiesSetterTest.java

@Test
public void getNamesWithDefault() throws NoSuchMethodException {
    final Method setter = TestCase.class.getDeclaredMethod("setPropertyNoDescription", String.class,
            String.class);
    final OptionProperties optionProperties = setter.getAnnotation(OptionProperties.class);
    final OptionPropertiesSetter<TestCase> test = new OptionPropertiesSetter<TestCase>(optionProperties,
            setter);/*from w  w  w . j  a v a  2 s  .c o  m*/
    assertThat("property name", test.getNames()[0], is("D"));
}

From source file:org.fishwife.jrugged.spring.CircuitBreakerBeanFactory.java

/**
 * If packageScanBase is defined will search packages for {@link org.fishwife.jrugged.aspects.CircuitBreaker}
 * annotations and create circuitbreakers for them.
 *//*from www  . j  ava 2s  .c  o m*/
public void buildAnnotatedCircuitBreakers() {
    if (packageScanBase != null) {
        AnnotatedMethodScanner methodScanner = new AnnotatedMethodScanner();
        for (Method m : methodScanner.findAnnotatedMethods(packageScanBase,
                org.fishwife.jrugged.aspects.CircuitBreaker.class)) {
            org.fishwife.jrugged.aspects.CircuitBreaker circuitBreakerAnnotation = m
                    .getAnnotation(org.fishwife.jrugged.aspects.CircuitBreaker.class);
            DefaultFailureInterpreter dfi = new DefaultFailureInterpreter(circuitBreakerAnnotation.ignore(),
                    circuitBreakerAnnotation.target(), circuitBreakerAnnotation.limit(),
                    circuitBreakerAnnotation.windowMillis());
            CircuitBreakerConfig config = new CircuitBreakerConfig(circuitBreakerAnnotation.resetMillis(), dfi);
            createCircuitBreaker(circuitBreakerAnnotation.name(), config);
        }
    }

}

From source file:com.ryantenney.metrics.spring.MetricParamAnnotationTest.java

@Test
public void timedParameterMethod() {
    Timer timedMethod = forTimedMethod(metricRegistry, MetricParamClass.class, "timedParameterMethod", 1);
    assertNull(timedMethod);/*  ww  w .  ja  va  2s  .  c  o  m*/

    metricParamClass.timedParameterMethod(1);
    timedMethod = forTimedMethod(metricRegistry, MetricParamClass.class, "timedParameterMethod", 1);
    assertNotNull(timedMethod);
    assertEquals(1, timedMethod.getCount());

    Method method = findMethod(MetricParamClass.class, "timedParameterMethod");
    String metricName = forTimedMethod(MetricParamClass.class, method, method.getAnnotation(Timed.class), 1);
    assertEquals("timed-param.1", metricName);
}

From source file:gobblin.runtime.cli.PublicMethodsGobblinCliFactory.java

private Options inferOptionsFromMethods() {
    Options options = new Options();

    for (Method method : klazz.getMethods()) {
        if (canUseMethod(method)) {
            EmbeddedGobblinCliOption annotation = method.isAnnotationPresent(EmbeddedGobblinCliOption.class)
                    ? method.getAnnotation(EmbeddedGobblinCliOption.class)
                    : null;// w w  w  .  j a  v  a2  s. c  om
            String optionName = annotation == null || Strings.isNullOrEmpty(annotation.name())
                    ? method.getName()
                    : annotation.name();
            String description = annotation == null ? "" : annotation.description();
            Option.Builder builder = Option.builder(optionName).desc(description);
            boolean hasArg = method.getParameterTypes().length > 0;
            if (hasArg) {
                builder.hasArg();
            }
            Option option = builder.build();
            options.addOption(option);
            this.methodsMap.put(option.getOpt(), method);
        }
    }

    return options;
}

From source file:com.agimatec.validation.jsr303.AnnotationConstraintBuilder.java

/** read overridesAttributes from constraintValidation.annotation */
private void buildOverridesAttributes() {
    overrides = new LinkedList();
    for (Method method : constraintValidation.getAnnotation().annotationType().getDeclaredMethods()) {
        OverridesAttribute.List annoOAL = method.getAnnotation(OverridesAttribute.List.class);
        if (annoOAL != null) {
            for (OverridesAttribute annoOA : annoOAL.value()) {
                parseConstraintOverride(method.getName(), annoOA);
            }/*w ww  . j a v a 2s. c o  m*/
        }
        OverridesAttribute annoOA = method.getAnnotation(OverridesAttribute.class);
        if (annoOA != null) {
            parseConstraintOverride(method.getName(), annoOA);
        }
    }
}

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

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

    Class generatedType = schemaRule
            .generateAndCompile("/json/examples/torrent.json", "com.example",
                    config("annotationStyle", "moshi1", "propertyWordDelimiters", "_", "sourceType", "json"))
            .loadClass("com.example.Torrent");

    assertThat(schemaRule.getGenerateDir(), not(containsText("org.codehaus.jackson")));
    assertThat(schemaRule.getGenerateDir(), not(containsText("com.fasterxml.jackson")));
    assertThat(schemaRule.getGenerateDir(), not(containsText("com.google.gson")));
    assertThat(schemaRule.getGenerateDir(), not(containsText("@SerializedName")));
    assertThat(schemaRule.getGenerateDir(), containsText("com.squareup.moshi"));
    assertThat(schemaRule.getGenerateDir(), containsText("@Json"));

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

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

From source file:com.github.kongchen.swagger.docgen.mavenplugin.SpringMavenDocumentSource.java

private Map<String, SpringResource> analyzeController(Class<?> clazz, Map<String, SpringResource> resourceMap,
        String description) throws ClassNotFoundException {

    for (int i = 0; i < clazz.getAnnotation(RequestMapping.class).value().length; i++) {
        String controllerMapping = clazz.getAnnotation(RequestMapping.class).value()[i];
        String resourceName = Utils.parseResourceName(clazz);
        for (Method m : clazz.getMethods()) {
            if (m.isAnnotationPresent(RequestMapping.class)) {
                RequestMapping methodReq = m.getAnnotation(RequestMapping.class);
                if (methodReq.value() == null || methodReq.value().length == 0
                        || Utils.parseResourceName(methodReq.value()[0]).equals("")) {
                    if (resourceName.length() != 0) {
                        String resourceKey = Utils.createResourceKey(resourceName,
                                Utils.parseVersion(controllerMapping));
                        if ((!(resourceMap.containsKey(resourceKey)))) {
                            resourceMap.put(resourceKey,
                                    new SpringResource(clazz, resourceName, resourceKey, description));
                        }//  w  ww . j  av a  2 s .  com
                        resourceMap.get(resourceKey).addMethod(m);
                    }
                }
            }
        }
    }
    clazz.getFields();
    clazz.getDeclaredFields(); //<--In case developer declares a field without an associated getter/setter.
    //this will allow NoClassDefFoundError to be caught before it triggers bamboo failure.

    return resourceMap;
}

From source file:com.ryantenney.metrics.spring.MetricParamAnnotationTest.java

@Test
public void timedNullParameterMethod() {
    Timer timedMethod = forTimedMethod(metricRegistry, MetricParamClass.class, "timedParameterMethod",
            (Object[]) null);/*from  w  w w.j ava 2  s . c o m*/
    assertNull(timedMethod);

    metricParamClass.timedParameterMethod(null);
    timedMethod = forTimedMethod(metricRegistry, MetricParamClass.class, "timedParameterMethod",
            (Object[]) null);
    assertNotNull(timedMethod);
    assertEquals(1, timedMethod.getCount());

    Method method = findMethod(MetricParamClass.class, "timedParameterMethod");
    String metricName = forTimedMethod(MetricParamClass.class, method, method.getAnnotation(Timed.class),
            (Object[]) null);
    assertEquals("timed-param.null", metricName);
}

From source file:name.ikysil.beanpathdsl.codegen.Context.java

@SuppressWarnings("unchecked")
private <A extends Annotation> void scanMethod(Map<Class<?>, A> result, Method method,
        Class<A> annotationClass) {
    final A methodAnnotation = method.getAnnotation(annotationClass);
    if (methodAnnotation != null) {
        result.put(resolveClass(method.getReturnType()), methodAnnotation);
    }//w  w  w .  j  a  va  2s .c  o m
    Class<?>[] paramTypes = method.getParameterTypes();
    Annotation[][] paramsAnnotations = method.getParameterAnnotations();
    for (int i = 0; i < paramTypes.length; i++) {
        Annotation[] paramAnnotations = paramsAnnotations[i];
        A paramAnnotation = methodAnnotation;
        for (Annotation annotation : paramAnnotations) {
            if (annotation.annotationType().equals(annotationClass)) {
                paramAnnotation = (A) annotation;
                break;
            }
        }
        if (paramAnnotation != null) {
            result.put(resolveClass(paramTypes[i]), paramAnnotation);
        }
    }
}

From source file:com.ryantenney.metrics.spring.MetricParamAnnotationTest.java

@Test
public void countedParameterMethod() {
    Counter countedMethod = forCountedMethod(metricRegistry, MetricParamClass.class, "countedParameterMethod",
            "foo");
    assertNull(countedMethod);/*from   w  ww  .  ja va 2  s. c  om*/

    metricParamClass.countedParameterMethod("foo");
    countedMethod = forCountedMethod(metricRegistry, MetricParamClass.class, "countedParameterMethod", "foo");
    assertNotNull(countedMethod);
    assertEquals(1, countedMethod.getCount());

    Method method = findMethod(MetricParamClass.class, "countedParameterMethod");
    String metricName = forCountedMethod(MetricParamClass.class, method, method.getAnnotation(Counted.class),
            "foo");
    assertEquals("counted-param.foo", metricName);
}