Example usage for org.springframework.core.annotation AnnotationUtils getAnnotation

List of usage examples for org.springframework.core.annotation AnnotationUtils getAnnotation

Introduction

In this page you can find the example usage for org.springframework.core.annotation AnnotationUtils getAnnotation.

Prototype

@Nullable
public static <A extends Annotation> A getAnnotation(Method method, Class<A> annotationType) 

Source Link

Document

Get a single Annotation of annotationType from the supplied Method , where the annotation is either present or meta-present on the method.

Usage

From source file:com.geodevv.testing.irmina.MockedOrSpiedInjectionPointScanner.java

private List<Annotation> getQualifierOrNamed(Field field) {
    List<Annotation> annotations = new ArrayList<>();

    Qualifier qualifier = AnnotationUtils.getAnnotation(field, Qualifier.class);
    if (qualifier != null) {
        annotations.add(new NamedImpl(qualifier.value()));
    } else {/*from   w  ww. j  av a  2 s  . com*/

        Named named = AnnotationUtils.getAnnotation(field, Named.class);
        if (named != null) {
            annotations.add(new NamedImpl(named.value()));
        }
    }
    return annotations;
}

From source file:demo.lifecycle.SmartLifecycleRegistry.java

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    Class<?> targetClass = AopUtils.getTargetClass(bean);
    ReflectionUtils.doWithMethods(targetClass, new MethodCallback() {
        @Override//from   w w w.j a  va2  s. c om
        public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
            Start start = AnnotationUtils.getAnnotation(method, Start.class);
            if (start != null) {
                processStart(beanName, start, method, bean);
            }
        }

    });
    return bean;
}

From source file:com.delphix.test.UnitTestMethodInterceptor.java

@Override
public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {
    List<IMethodInstance> out = new ArrayList<IMethodInstance>();
    for (IMethodInstance method : methods) {
        UnitTest classAnnote = findAnnotation(method.getMethod().getRealClass(), UnitTest.class);
        if (classAnnote != null && classAnnote.disabled().isEmpty()) {
            Method javaMethod = method.getMethod().getConstructorOrMethod().getMethod();
            if (javaMethod != null) {
                UnitTest methodAnnote = AnnotationUtils.getAnnotation(javaMethod, UnitTest.class);
                if (methodAnnote == null || methodAnnote.disabled().isEmpty()) {
                    out.add(method);//from  w w  w .j  a va2  s.c om
                }
            }
        }
    }
    return out;
}

From source file:com.aw.swing.mvp.grid.GridInfoProvider.java

/**
 * Get Object that will be used to get the data to be shown
 *
 * @return/*from   w ww.j ava 2 s .com*/
 */
public ListProvider getListProvider(final Presenter presenter) {
    final GridInfoProvider gridInfoProvider = this;
    return new ListProvider() {
        public List getList(Object param) {
            List list = getValues((E) param);
            if (list == null) {
                list = new ArrayList();
            }
            if (!(presenter instanceof FindPresenter)) {
                Method method = null;
                try {
                    method = gridInfoProvider.getClass().getMethod("getValues", param.getClass());
                } catch (NoSuchMethodException e) {
                }
                if (method == null) {
                    method = ReflectionUtils.findMethodByName(gridInfoProvider.getClass(), "getValues");
                }
                DeleteableListField deleteableListField = AnnotationUtils.getAnnotation(method,
                        DeleteableListField.class);
                if (deleteableListField != null) {
                    if (!(list instanceof DeleteableList)) {
                        DeleteableList deleteableList = new DeleteableList();
                        deleteableList.init(list);
                        BeanWrapper bw = new BeanWrapperImpl(param);
                        bw.setPropertyValue(deleteableListField.value(), deleteableList);
                        return deleteableList;
                    }
                }
            }
            return list;
        }
    };
}

From source file:com.stehno.sjdbcx.reflection.extractor.AnnotatedCollaborationExtractor.java

@Override
@SuppressWarnings("unchecked")
public C extract(final Method method) {
    final Annotation annotation = AnnotationUtils.getAnnotation(method, annoType);
    if (annotation != null) {
        final String extractionKey = (String) AnnotationUtils.getValue(annotation);
        if (!StringUtils.isEmpty(extractionKey)) {
            return resolve(extractionKey);
        } else {/*  ww w .j a  v  a  2s  .  co  m*/
            return resolve((Class<C>) AnnotationUtils.getValue(annotation, TYPE_ATTRIBUTE));
        }

    } else {
        return defaultExtractor;
    }
}

From source file:com.opensymphony.able.introspect.PropertyInfo.java

protected <T extends Annotation> T findAnnotation(Class<T> clazz) {
    String name = descriptor.getName();
    try {/*w w  w .  jav a  2  s .c o  m*/
        Field field = entityClass.getDeclaredField(name);
        T a = field.getAnnotation(clazz);
        if (a != null) {
            return a;
        }
    } catch (NoSuchFieldException e) {
        // this should generally never happen...
    }

    Method readMethod = descriptor.getReadMethod();
    if (readMethod != null) {
        return AnnotationUtils.getAnnotation(readMethod, clazz);
    }

    return null;
}

From source file:org.activiti.spring.components.aop.ProcessStartingMethodInterceptor.java

public Object invoke(MethodInvocation invocation) throws Throwable {

    Method method = invocation.getMethod();

    StartProcess startProcess = AnnotationUtils.getAnnotation(method, StartProcess.class);

    String processKey = startProcess.processKey();

    Assert.hasText(processKey, "you must provide the name of process to start");

    Object result;/*from   w  w w. j  ava  2s. c  o  m*/
    try {
        result = invocation.proceed();
        Map<String, Object> vars = this.processVariablesFromAnnotations(invocation);

        String businessKey = this.processBusinessKey(invocation);

        log.info("variables for the started process: {}", vars.toString());

        RuntimeService runtimeService = this.processEngine.getRuntimeService();
        ProcessInstance pi;
        if (null != businessKey && StringUtils.hasText(businessKey)) {
            pi = runtimeService.startProcessInstanceByKey(processKey, businessKey, vars);
            log.info("the business key for the started process is '{}'", businessKey);
        } else {
            pi = runtimeService.startProcessInstanceByKey(processKey, vars);
        }

        String pId = pi.getId();

        if (invocation.getMethod().getReturnType().equals(void.class))
            return null;

        if (shouldReturnProcessInstance(startProcess, invocation, result))
            return pi;

        if (shouldReturnProcessInstanceId(startProcess, invocation, result))
            return pId;

        if (shouldReturnAsyncResultWithProcessInstance(startProcess, invocation, result)) {
            return new AsyncResult<ProcessInstance>(pi);
        }

    } catch (Throwable th) {
        throw new RuntimeException(th);
    }
    return result;
}

From source file:org.raistlic.spring.test.flyway.FlywayTestExecutionListener.java

/**
 * The method gets and returns the {@link DataReset} annotation from the {@code testContext}, or
 * {@code null} if none found.//  w w  w .  j a va 2s. co m
 *
 * @param testContext the test context to get {@link DataReset} annotation from, cannot be
 *                    {@code null} and must be valid.
 * @return the {@link DataReset} found, or {@code null} if none found.
 *
 * @throws PreconditionException when {@code testContext} is {@code null} or invalid.
 */
static DataReset getDataResetAnnotation(TestContext testContext) {

    Precondition.param(testContext, "testContext").notNull();
    Precondition.param(testContext.getTestMethod(), "testContext.testMethod").notNull();
    Precondition.param(testContext.getTestClass(), "testContext.testClass").notNull();

    DataReset annotation = AnnotationUtils.getAnnotation(testContext.getTestMethod(), DataReset.class);
    if (annotation == null) {
        annotation = AnnotationUtils.getAnnotation(testContext.getTestClass(), DataReset.class);
    }
    return annotation;
}

From source file:com.joshlong.activiti.coordinator.aop.ActivitiStateAnnotationBeanPostProcessor.java

public Object postProcessAfterInitialization(final Object bean, final String beanName) throws BeansException {
    // first sift through and get all the methods
    // then get all the annotations
    // then build the metadata and register the metadata
    final Class<?> targetClass = AopUtils.getTargetClass(bean);
    final ActivitiComponent component = targetClass.getAnnotation(ActivitiComponent.class);

    ReflectionUtils.doWithMethods(targetClass, new ReflectionUtils.MethodCallback() {
        @SuppressWarnings("unchecked")
        public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {

            ActivitiState activitiState = AnnotationUtils.getAnnotation(method, ActivitiState.class);

            String processName = component.processKey();

            if (StringUtils.hasText(activitiState.processName())) {
                processName = activitiState.processName();
            }/*w  w w . j av  a2  s .co  m*/

            String stateName = activitiState.stateName();

            if (!StringUtils.hasText(stateName)) {
                stateName = activitiState.value();
            }

            Assert.notNull(stateName, "You must provide a stateName!");

            Map<Integer, String> vars = new HashMap<Integer, String>();
            Annotation[][] paramAnnotationsArray = method.getParameterAnnotations();

            int ctr = 0;
            int pvMapIndex = -1;
            int procIdIndex = -1;

            for (Annotation[] paramAnnotations : paramAnnotationsArray) {
                ctr += 1;

                for (Annotation pa : paramAnnotations) {
                    if (pa instanceof ProcessVariable) {
                        ProcessVariable pv = (ProcessVariable) pa;
                        String pvName = pv.value();
                        vars.put(ctr, pvName);
                    } else if (pa instanceof ProcessVariables) {
                        pvMapIndex = ctr;
                    } else if (pa instanceof ProcessId) {
                        procIdIndex = ctr;
                    }
                }
            }

            ActivitiStateHandlerRegistration registration = new ActivitiStateHandlerRegistration(vars, method,
                    bean, stateName, beanName, pvMapIndex, procIdIndex, processName);
            registry.registerActivitiStateHandler(registration);
        }
    }, new ReflectionUtils.MethodFilter() {
        public boolean matches(Method method) {
            return null != AnnotationUtils.getAnnotation(method, ActivitiState.class);
        }
    });

    return bean;
}

From source file:org.activiti.spring.components.aop.ActivitiStateAnnotationBeanPostProcessor.java

public Object postProcessAfterInitialization(final Object bean, final String beanName) throws BeansException {
    // first sift through and get all the methods
    // then get all the annotations
    // then build the metadata and register the metadata
    final Class<?> targetClass = AopUtils.getTargetClass(bean);
    final org.activiti.spring.annotations.ActivitiComponent component = targetClass
            .getAnnotation(org.activiti.spring.annotations.ActivitiComponent.class);

    ReflectionUtils.doWithMethods(targetClass, new ReflectionUtils.MethodCallback() {
        @SuppressWarnings("unchecked")
        public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {

            State state = AnnotationUtils.getAnnotation(method, State.class);

            String processName = component.processKey();

            if (StringUtils.hasText(state.process())) {
                processName = state.process();
            }/*  w w  w  . ja  v a 2  s .  c o  m*/

            String stateName = state.state();

            if (!StringUtils.hasText(stateName)) {
                stateName = state.value();
            }

            Assert.notNull(stateName, "You must provide a stateName!");

            Map<Integer, String> vars = new HashMap<Integer, String>();
            Annotation[][] paramAnnotationsArray = method.getParameterAnnotations();

            int ctr = 0;
            int pvMapIndex = -1;
            int procIdIndex = -1;

            for (Annotation[] paramAnnotations : paramAnnotationsArray) {
                ctr += 1;

                for (Annotation pa : paramAnnotations) {
                    if (pa instanceof ProcessVariable) {
                        ProcessVariable pv = (ProcessVariable) pa;
                        String pvName = pv.value();
                        vars.put(ctr, pvName);
                    } else if (pa instanceof ProcessVariables) {
                        pvMapIndex = ctr;
                    } else if (pa instanceof ProcessId) {
                        procIdIndex = ctr;
                    }
                }
            }

            ActivitiStateHandlerRegistration registration = new ActivitiStateHandlerRegistration(vars, method,
                    bean, stateName, beanName, pvMapIndex, procIdIndex, processName);
            registry.registerActivitiStateHandler(registration);
        }
    }, new ReflectionUtils.MethodFilter() {
        public boolean matches(Method method) {
            return null != AnnotationUtils.getAnnotation(method, State.class);
        }
    });

    return bean;
}