Example usage for java.lang.reflect Method getAnnotations

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

Introduction

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

Prototype

public Annotation[] getAnnotations() 

Source Link

Usage

From source file:koper.MessageListenerBeanPostProcessor.java

/**
 * ?/*from   w w w.  j  a  v a  2 s  .  co  m*/
 *
 * @param clazz
 * @param methodName
 * @return
 */
protected Method getMethod(Class<?> clazz, String methodName) {

    Method[] methods = ReflectionUtils.getAllDeclaredMethods(clazz); //clazz.getDeclaredMethods();
    Method findMethod = null;
    for (Method method : methods) {
        if (method.getName().equals(methodName) && method.getAnnotations().length != 0) {
            findMethod = method;
        }
    }
    return findMethod;
}

From source file:com.google.gwt.sample.dynatablemvp.server.loc.ProxyObjectLocator.java

private Object getFieldValueWithAnnotation(E domainObject, String annotation) {
    Object result = null;// w ww.jav  a  2s .co m
    Method[] meths = domainObject.getClass().getMethods();
    Method idMethod = null;
    for (Method m : meths) {
        for (Annotation a : m.getAnnotations()) {
            String annotName = a.annotationType().getName();
            if (annotName.equalsIgnoreCase(annotation)) {
                idMethod = m;
                break;
            }
        }
        if (idMethod != null) {
            try {
                result = idMethod.invoke(domainObject);
            } catch (IllegalAccessException e) {
                log.error(e.getLocalizedMessage(), e);
                result = null;
            } catch (IllegalArgumentException e) {
                log.error(e.getLocalizedMessage(), e);
                result = null;
            } catch (InvocationTargetException e) {
                log.error(e.getLocalizedMessage(), e);
                result = null;
            }
            break;
        }
    }
    return result;
}

From source file:com.beinformed.framework.osgi.osgitest.annotation.processor.TestAnnotationProcessor.java

/**
 * Creates a {@code TestSuiteImpl} instance for the given service parameter.
 * @param   service//from w  w  w  .ja va2 s  .  co  m
 *          the service to create a {@code TestSuite} for.
 * @return   an initialized {@code TestSuiteImpl} instance.
 */
private TestSuiteImpl createTestSuite(Object service) {
    TestSuiteImpl testSuite = new TestSuiteImpl();
    Class<?> c = service.getClass();
    String label = c.getAnnotation(TestSuite.class).label();
    testSuite.setLabel(label);
    Method[] declaredMethods = c.getDeclaredMethods();
    for (Method m : declaredMethods) {
        Annotation[] methodAnnotations = m.getAnnotations();
        for (Annotation a : methodAnnotations) {
            if (a instanceof TestCase) {
                TestCase testAnnotation = (TestCase) a;
                TestCaseImpl testCase = new TestCaseImpl();
                boolean valid = true;
                testCase.setIdentifier(testAnnotation.identifier());
                testCase.setLabel(testAnnotation.label());
                testCase.setInstance(service);
                testCase.setMethod(m);
                if (valid) {
                    testSuite.addTestCase(testCase);
                }
            }
        }
    }
    return testSuite;
}

From source file:com.netsteadfast.greenstep.base.interceptor.QueryParamInspectInterceptor.java

@Override
public String intercept(ActionInvocation actionInvocation) throws Exception {
    Annotation[] actionMethodAnnotations = null;
    Method[] methods = actionInvocation.getAction().getClass().getMethods();
    for (Method method : methods) {
        if (actionInvocation.getProxy().getMethod().equals(method.getName())) {
            actionMethodAnnotations = method.getAnnotations();
        }/*from w  ww.j  a  v a2 s  . c  o  m*/
    }
    if (actionMethodAnnotations != null && actionMethodAnnotations.length > 0) {
        try {
            this.log(actionInvocation, actionMethodAnnotations);
        } catch (ServiceException e) {
            e.printStackTrace();
        }
    }
    return actionInvocation.invoke();
}

From source file:org.seasar.struts.lessconfig.factory.TigerValidatorAnnotationHandler.java

private boolean hasAnnotation(Method method) {
    return method.getAnnotations().length != 0;
}

From source file:com.abiquo.vsm.resource.AbstractResource.java

/**
 * Get the allowed http methods for the current resource.
 * //from ww w .  jav a 2s  .  co  m
 * @return The allowed http methods for the current resource.
 */
protected String getMethodsAllowed() {
    Collection<String> allowed = new LinkedHashSet<String>();
    for (Method method : this.getClass().getMethods()) {
        for (Annotation annotation : method.getAnnotations()) {
            if (REST.contains(annotation.annotationType())) {
                allowed.add(annotation.annotationType().getSimpleName());
                break;
            }
        }
    }

    return allowed.toString();
}

From source file:com.msopentech.odatajclient.proxy.api.impl.EntityCollectionInvocationHandler.java

@Override
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
    final Annotation[] methodAnnots = method.getAnnotations();

    if (isSelfMethod(method, args)) {
        return invokeSelfMethod(method, args);
    } else if (!ArrayUtils.isEmpty(methodAnnots) && methodAnnots[0] instanceof Operation) {
        if (this.uri == null) {
            throw new IllegalStateException("This entity collection has not yet been flushed");
        }/*from w  ww. j  av  a  2s  .co m*/

        final com.msopentech.odatajclient.engine.metadata.edm.v3.EntityContainer container = containerHandler
                .getFactory().getMetadata().getSchema(ClassUtils.getNamespace(itemRef))
                .getEntityContainer(entityContainerName);
        final com.msopentech.odatajclient.engine.metadata.edm.v3.FunctionImport funcImp = container
                .getFunctionImport(((Operation) methodAnnots[0]).name());

        return functionImport((Operation) methodAnnots[0], method, args,
                client.getURIBuilder(this.uri.toASCIIString())
                        .appendFunctionImportSegment(((Operation) methodAnnots[0]).name()).build(),
                funcImp);
    } else {
        throw new UnsupportedOperationException("Method not found: " + method);
    }
}

From source file:org.jdal.vaadin.ui.form.AnnotationFieldFactory.java

/**
 * {@inheritDoc}/*from  w w w.j  a  v  a  2 s . c o m*/
 */
public Field createField(Item item, Object propertyId, Component uiContext) {
    if (item instanceof BeanItem<?>) {
        BeanItem<?> bi = (BeanItem<?>) item;
        String name = (String) propertyId;
        Class<?> clazz = bi.getBean().getClass();
        java.lang.reflect.Field field = ReflectionUtils.findField(clazz, name);
        Annotation[] fa = new Annotation[] {};
        if (field != null) {
            fa = field.getAnnotations();
        }
        java.lang.reflect.Method method = BeanUtils.getPropertyDescriptor(clazz, name).getReadMethod();
        Annotation[] ma = method.getAnnotations();
        Annotation[] annotations = (Annotation[]) ArrayUtils.addAll(fa, ma);
        Field f = null;
        for (Annotation a : annotations) {
            f = findField(a, clazz, name);
            if (f != null) {
                f.setCaption(createCaptionByPropertyId(propertyId));
                applyFieldProcessors(f, propertyId);
                return f;
            }
        }
    }
    // fall back to default
    return super.createField(item, propertyId, uiContext);
}

From source file:org.pmp.budgeto.app.SwaggerDispatcherConfigTest.java

@Test
public void springConf() throws Exception {

    Class<?> clazz = swaggerDispatcherConfig.getClass();
    Assertions.assertThat(clazz.getAnnotations()).hasSize(4);
    Assertions.assertThat(clazz.isAnnotationPresent(Configuration.class)).isTrue();
    Assertions.assertThat(clazz.isAnnotationPresent(EnableWebMvc.class)).isTrue();
    Assertions.assertThat(clazz.isAnnotationPresent(EnableSwagger.class)).isTrue();
    Assertions.assertThat(clazz.isAnnotationPresent(ComponentScan.class)).isTrue();
    Assertions.assertThat(clazz.getAnnotation(ComponentScan.class).basePackages())
            .containsExactly("com.ak.swaggerspringmvc.shared.app", "com.ak.spring3.music");

    Field fSpringSwaggerConfig = clazz.getDeclaredField("springSwaggerConfig");
    Assertions.assertThat(fSpringSwaggerConfig.getAnnotations()).hasSize(1);
    Assertions.assertThat(fSpringSwaggerConfig.isAnnotationPresent(Autowired.class)).isTrue();

    Method mCustomImplementation = clazz.getDeclaredMethod("customImplementation", new Class[] {});
    Assertions.assertThat(mCustomImplementation.getAnnotations()).hasSize(1);
    Assertions.assertThat(mCustomImplementation.getAnnotation(Bean.class)).isNotNull();
}

From source file:org.kantega.dogmaticmvc.web.DogmaticMVCHandler.java

private Method findRequestHandlerMethod(Class clazz) {
    for (Method method : clazz.getMethods()) {
        for (Annotation annotation : method.getAnnotations()) {
            if (annotation instanceof RequestHandler) {
                return method;
            }//from www.  j  av  a2  s .  c  om
        }
    }
    return null;
}