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 testIsRequired() throws NoSuchMethodException {
    Method setter = TestCase.class.getDeclaredMethod("setProperty", String.class, String.class);
    OptionProperties optionProperties = setter.getAnnotation(OptionProperties.class);
    OptionPropertiesSetter<TestCase> test = new OptionPropertiesSetter<TestCase>(optionProperties, setter);
    assertTrue("expected to be required", test.isRequired());

    setter = TestCase.class.getDeclaredMethod("setPropertyNoDescription", String.class, String.class);
    optionProperties = setter.getAnnotation(OptionProperties.class);
    test = new OptionPropertiesSetter<TestCase>(optionProperties, setter);
    assertFalse("did not expect to be required", test.isRequired());
}

From source file:candr.yoclip.option.OptionPropertiesSetterTest.java

@Test
public void testGetDescription() throws NoSuchMethodException {
    Method setter = TestCase.class.getDeclaredMethod("setProperty", String.class, String.class);
    OptionProperties optionProperties = setter.getAnnotation(OptionProperties.class);
    OptionPropertiesSetter<TestCase> test = new OptionPropertiesSetter<TestCase>(optionProperties, setter);
    assertThat("description", test.getDescription(), is("test  description"));

    setter = TestCase.class.getDeclaredMethod("setPropertyNoDescription", String.class, String.class);
    optionProperties = setter.getAnnotation(OptionProperties.class);
    test = new OptionPropertiesSetter<TestCase>(optionProperties, setter);
    assertTrue("expected empty description not " + test.getDescription(),
            StringUtils.isEmpty(test.getDescription()));
}

From source file:com.infinities.skyport.timeout.ServiceProviderTimeLimiter.java

private <T> void checkMethodOwnFunctionConfiguration(Class<T> interfaceType, final Object configuration)
        throws InitializationException {
    Method[] methods = interfaceType.getMethods();
    for (Method method : methods) {
        if (IGNORED_SET.contains(method.getName())) {
            continue;
        }/*w  w w .  j ava 2 s .co  m*/
        if (method.getAnnotation(Deprecated.class) != null) {
            continue;
        }
        try {
            Class<?> c = PropertyUtils.getPropertyType(configuration, method.getName());
            Preconditions.checkArgument(c == FunctionConfiguration.class);
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException
                | IllegalArgumentException e) {
            throw new InitializationException(
                    "FunctionConfiguration for '" + method.getName() + "' cannot be found");
        }
    }

}

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

@Override
protected void withMethod(final Object bean, String beanName, Class<?> targetClass, final Method method) {
    if (method.getParameterTypes().length > 0) {
        throw new IllegalStateException(
                "Method " + method.getName() + " is annotated with @Gauge but requires parameters.");
    }/*www  . j a  v  a2  s .c o m*/

    final Gauge annotation = method.getAnnotation(Gauge.class);
    final String metricName = Util.forGauge(targetClass, method, annotation);

    metrics.register(metricName, new com.codahale.metrics.Gauge<Object>() {
        @Override
        public Object getValue() {
            return ReflectionUtils.invokeMethod(method, bean);
        }
    });

    LOG.debug("Created gauge {} for method {}.{}", metricName, targetClass.getCanonicalName(),
            method.getName());
}

From source file:candr.yoclip.option.OptionPropertiesSetterTest.java

@Test(expected = UnsupportedOperationException.class)
public void initWithWrongArgumentCount() throws NoSuchMethodException {
    final Method setter = TestCase.class.getDeclaredMethod("setWithWrongNumberArgs", String.class);
    final OptionProperties optionProperties = setter.getAnnotation(OptionProperties.class);
    new OptionPropertiesSetter<TestCase>(optionProperties, setter);
}

From source file:ar.com.jrules.core.service.LoadJRules.java

private boolean haveAnyMethodWithJRuleImplementation(Class<?> clazz) {

    for (Method method : clazz.getMethods()) {
        if (method.getAnnotation(ExecuteRule.class) != null) {
            return true;
        }//  w  w w.  ja va  2  s.  c om
    }

    return false;
}

From source file:ch.digitalfondue.npjt.QueryFactory.java

@SuppressWarnings("unchecked")
public <T> T from(final Class<T> clazz) {

    return (T) Proxy.newProxyInstance(clazz.getClassLoader(), new Class[] { clazz }, new InvocationHandler() {
        @Override/* ww w .  ja  v a 2 s  .  c  o m*/
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            boolean hasAnnotation = method.getAnnotation(Query.class) != null;
            if (hasAnnotation) {
                QueryTypeAndQuery qs = extractQueryAnnotation(clazz, method);
                return qs.type.apply(qs.query, qs.rowMapperClass, jdbc, method, args, columnMapperFactories.set,
                        parameterConverters.set);
            } else if (method.getReturnType().equals(NamedParameterJdbcTemplate.class) && args == null) {
                return jdbc;
            } else if (IS_DEFAULT_METHOD != null && (boolean) IS_DEFAULT_METHOD.invoke(method)) {
                final Class<?> declaringClass = method.getDeclaringClass();
                return LOOKUP_CONSTRUCTOR.newInstance(declaringClass, MethodHandles.Lookup.PRIVATE)
                        .unreflectSpecial(method, declaringClass).bindTo(proxy).invokeWithArguments(args);
            } else {
                throw new IllegalArgumentException(
                        String.format("missing @Query annotation for method %s in interface %s",
                                method.getName(), clazz.getSimpleName()));
            }

        }
    }

    );
}

From source file:org.jellycastle.build.JellyBuild.java

private void loadPlugins(Project project) {
    Build build = project.getBuild();//  w  w  w.j a  v  a2s  . c  o m
    if (build == null) {
        build = new ObjectFactory().createBuild();
        project.setBuild(build);
    }

    PluginManagement pluginManagement = build.getPluginManagement();
    if (pluginManagement == null) {
        pluginManagement = new ObjectFactory().createPluginManagement();
        pluginManagement.setPlugins(new ObjectFactory().createPluginManagementPlugins());
        build.setPluginManagement(pluginManagement);
    }

    Build.Plugins plugins = build.getPlugins();
    if (plugins == null) {
        plugins = new ObjectFactory().createBuildPlugins();
        build.setPlugins(plugins);
    }

    addJellyBuildPlugin(pluginManagement, plugins);

    for (Method method : configuration.getDeclaredMethods()) {
        if (method.getAnnotation(org.jellycastle.annotation.plugin.Plugin.class) != null) {
            org.jellycastle.annotation.plugin.Plugin annotation = method
                    .getAnnotation(org.jellycastle.annotation.plugin.Plugin.class);

            if (StringUtils.hasText(annotation.version())) {
                Plugin plugin = new ObjectFactory().createPlugin();
                plugin.setGroupId(annotation.groupId());
                plugin.setArtifactId(annotation.artifactId());
                plugin.setVersion(annotation.version());

                pluginManagement.getPlugins().getPlugins().add(plugin);
            }

            Plugin plugin = new ObjectFactory().createPlugin();
            plugin.setGroupId(annotation.groupId());
            plugin.setArtifactId(annotation.artifactId());

            plugins.getPlugins().add(plugin);
        }
    }
}

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);
    }

    if (transactional == null) {
        throw new IllegalStateException(
                String.format("Method(%s) must be specificed a Transactional annotation!", method));
    }//from   w w w . ja va2  s  .c om
    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:ar.com.allium.rules.core.service.LoadAlliumRules.java

private boolean haveAnyMethodWithAlliumRuleImplementation(Class<?> clazz) {

    for (Method method : clazz.getMethods()) {
        if (method.getAnnotation(ExecuteRule.class) != null) {
            return true;
        }/*from  w  ww.j  a v  a  2 s. co m*/
    }

    return false;
}