public ApplicationListenerMethodTransactionalAdapter(String beanName, Class<?> targetClass, Method method) { super(beanName, targetClass, method); TransactionalEventListener ann = AnnotatedElementUtils.findMergedAnnotation(method, TransactionalEventListener.class); if (ann == null) { throw new IllegalStateException("No TransactionalEventListener annotation found on method: " + method); } this.annotation = ann; }
@Nullable private SendTo getSendTo(Method specificMethod) { SendTo ann = AnnotatedElementUtils.findMergedAnnotation(specificMethod, SendTo.class); if (ann == null) { ann = AnnotatedElementUtils.findMergedAnnotation(specificMethod.getDeclaringClass(), SendTo.class); } return ann; }
/** * Return a single annotation on the underlying method traversing its super methods * if no annotation can be found on the given method itself. * <p>Also supports <em>merged</em> composed annotations with attribute * overrides as of Spring Framework 4.3. * @param annotationType the type of annotation to introspect the method for * @return the annotation, or {@code null} if none found * @see AnnotatedElementUtils#findMergedAnnotation */ @Nullable public <A extends Annotation> A getMethodAnnotation(Class<A> annotationType) { return AnnotatedElementUtils.findMergedAnnotation(this.method, annotationType); }
private void detectAnnotationExceptionMappings(Method method, List<Class<? extends Throwable>> result) { ExceptionHandler ann = AnnotatedElementUtils.findMergedAnnotation(method, ExceptionHandler.class); Assert.state(ann != null, "No ExceptionHandler annotation"); result.addAll(Arrays.asList(ann.value())); }
@Override @Nullable protected HttpStatus determineStatus(Throwable ex) { HttpStatus status = super.determineStatus(ex); if (status == null) { ResponseStatus ann = AnnotatedElementUtils.findMergedAnnotation(ex.getClass(), ResponseStatus.class); if (ann != null) { status = ann.code(); } } return status; }
private String getAttributeName(MethodParameter param) { return Optional .ofNullable(AnnotatedElementUtils.findMergedAnnotation(param.getAnnotatedElement(), ModelAttribute.class)) .filter(ann -> StringUtils.hasText(ann.value())) .map(ModelAttribute::value) .orElseGet(() -> Conventions.getVariableNameForParameter(param)); }
@Override public boolean test(Method method) { ModelAttribute annot = AnnotatedElementUtils.findMergedAnnotation(method, ModelAttribute.class); return annot != null && (this.name == null || annot.name().equals(this.name)); } }
/** * Filter on methods annotated with the given annotation type. * @see #annot(Predicate[]) */ @SafeVarargs public final Builder<T> annotPresent(Class<? extends Annotation>... annotationTypes) { String message = "annotationPresent=" + Arrays.toString(annotationTypes); addFilter(message, method -> Arrays.stream(annotationTypes).allMatch(annotType -> AnnotatedElementUtils.findMergedAnnotation(method, annotType) != null)); return this; }
@Test public void javaLangAnnotationTypeViaFindMergedAnnotation() throws Exception { Constructor<?> deprecatedCtor = Date.class.getConstructor(String.class); assertEquals(deprecatedCtor.getAnnotation(Deprecated.class), findMergedAnnotation(deprecatedCtor, Deprecated.class)); assertEquals(Date.class.getAnnotation(Deprecated.class), findMergedAnnotation(Date.class, Deprecated.class)); }
@Test public void javaxAnnotationTypeViaFindMergedAnnotation() throws Exception { assertEquals(ResourceHolder.class.getAnnotation(Resource.class), findMergedAnnotation(ResourceHolder.class, Resource.class)); assertEquals(SpringAppConfigClass.class.getAnnotation(Resource.class), findMergedAnnotation(SpringAppConfigClass.class, Resource.class)); }
private void evaluateResponseStatus() { ResponseStatus annotation = getMethodAnnotation(ResponseStatus.class); if (annotation == null) { annotation = AnnotatedElementUtils.findMergedAnnotation(getBeanType(), ResponseStatus.class); } if (annotation != null) { this.responseStatus = annotation.code(); this.responseStatusReason = annotation.reason(); } }
@Test // SPR-17146 public void findMethodAnnotationFromGenericSuperclass() throws Exception { Method method = ExtendsBaseClassWithGenericAnnotatedMethod.class.getMethod("foo", String.class); Order order = findMergedAnnotation(method, Order.class); assertNotNull(order); }
public ApplicationListenerMethodAdapter(String beanName, Class<?> targetClass, Method method) { this.beanName = beanName; this.method = BridgeMethodResolver.findBridgedMethod(method); this.targetMethod = (!Proxy.isProxyClass(targetClass) ? AopUtils.getMostSpecificMethod(method, targetClass) : this.method); this.methodKey = new AnnotatedElementKey(this.targetMethod, targetClass); EventListener ann = AnnotatedElementUtils.findMergedAnnotation(this.targetMethod, EventListener.class); this.declaredEventTypes = resolveDeclaredEventTypes(method, ann); this.condition = (ann != null ? ann.condition() : null); this.order = resolveOrder(this.targetMethod); }
@Test // SPR-16060 public void findMethodAnnotationFromGenericInterface() throws Exception { Method method = ImplementsInterfaceWithGenericAnnotatedMethod.class.getMethod("foo", String.class); Order order = findMergedAnnotation(method, Order.class); assertNotNull(order); }
@Override public boolean test(Method method) { RequestMapping annot = AnnotatedElementUtils.findMergedAnnotation(method, RequestMapping.class); return annot != null && Arrays.equals(this.path, annot.path()) && Arrays.equals(this.method, annot.method()) && (this.params == null || Arrays.equals(this.params, annot.params())); } }
@Override public boolean test(Method method) { ResponseStatus annot = AnnotatedElementUtils.findMergedAnnotation(method, ResponseStatus.class); return annot != null && annot.code().equals(this.code); } }
private void assertPhase(Method method, TransactionPhase expected) { assertNotNull("Method must not be null", method); TransactionalEventListener annotation = AnnotatedElementUtils.findMergedAnnotation(method, TransactionalEventListener.class); assertEquals("Wrong phase for '" + method + "'", expected, annotation.phase()); }
@Test public void findMergedAnnotationWithAttributeAliasesInTargetAnnotation() { Class<?> element = AliasedTransactionalComponentClass.class; AliasedTransactional annotation = findMergedAnnotation(element, AliasedTransactional.class); assertNotNull("@AliasedTransactional on " + element, annotation); assertEquals("TX value via synthesized annotation.", "aliasForQualifier", annotation.value()); assertEquals("TX qualifier via synthesized annotation.", "aliasForQualifier", annotation.qualifier()); }
private void assertWebMapping(AnnotatedElement element) throws ArrayComparisonFailure { WebMapping webMapping = findMergedAnnotation(element, WebMapping.class); assertNotNull(webMapping); assertArrayEquals("value attribute: ", asArray("/test"), webMapping.value()); assertArrayEquals("path attribute: ", asArray("/test"), webMapping.path()); }
@Test public void findMergedAnnotationWithLocalAliasesThatConflictWithAttributesInMetaAnnotationByConvention() { final String[] EMPTY = new String[0]; Class<?> element = SpringAppConfigClass.class; ContextConfig contextConfig = findMergedAnnotation(element, ContextConfig.class); assertNotNull("Should find @ContextConfig on " + element, contextConfig); assertArrayEquals("locations for " + element, EMPTY, contextConfig.locations()); // 'value' in @SpringAppConfig should not override 'value' in @ContextConfig assertArrayEquals("value for " + element, EMPTY, contextConfig.value()); assertArrayEquals("classes for " + element, new Class<?>[] {Number.class}, contextConfig.classes()); }