Refine search
@Nullable public static <A extends Annotation> A findAnnotation(Method method, @Nullable Class<A> annotationType) { Assert.notNull(method, "Method must not be null"); if (annotationType == null) { return null; result = findAnnotation((AnnotatedElement) resolvedMethod, annotationType); if (result == null) { result = searchOnInterfaces(method, annotationType, method.getDeclaringClass().getInterfaces()); break; Set<Method> annotatedMethods = getAnnotatedMethodsInBaseType(clazz); if (!annotatedMethods.isEmpty()) { for (Method annotatedMethod : annotatedMethods) { if (isOverride(method, annotatedMethod)) { Method resolvedSuperMethod = BridgeMethodResolver.findBridgedMethod(annotatedMethod); result = findAnnotation((AnnotatedElement) resolvedSuperMethod, annotationType); if (result != null) { break; result = searchOnInterfaces(method, annotationType, clazz.getInterfaces()); result = synthesizeAnnotation(result, method); findAnnotationCache.put(cacheKey, result);
/** * Determine any validation triggered by the given annotation. * @param ann the annotation (potentially a validation annotation) * @return the validation hints to apply (possibly an empty array), * or {@code null} if this annotation does not trigger any validation * @since 5.1 */ @Nullable private Object[] determineValidationHints(Annotation ann) { Validated validatedAnn = AnnotationUtils.getAnnotation(ann, Validated.class); if (validatedAnn != null || ann.annotationType().getSimpleName().startsWith("Valid")) { Object hints = (validatedAnn != null ? validatedAnn.value() : AnnotationUtils.getValue(ann)); if (hints == null) { return new Object[0]; } return (hints instanceof Object[] ? (Object[]) hints : new Object[] {hints}); } return null; }
private AnnotationAttributes enableMethodSecurity() { if (enableMethodSecurity == null) { // if it is null look at this instance (i.e. a subclass was used) EnableGlobalMethodSecurity methodSecurityAnnotation = AnnotationUtils .findAnnotation(getClass(), EnableGlobalMethodSecurity.class); Assert.notNull(methodSecurityAnnotation, () -> EnableGlobalMethodSecurity.class.getName() + " is required"); Map<String, Object> methodSecurityAttrs = AnnotationUtils .getAnnotationAttributes(methodSecurityAnnotation); this.enableMethodSecurity = AnnotationAttributes.fromMap(methodSecurityAttrs); } return this.enableMethodSecurity; } }
@Nullable private Object getAdaptedValue( @Nullable AnnotatedElement element, Annotation annotation, String sourceAttributeName) { Object value = AnnotationUtils.getValue(annotation, sourceAttributeName); return AnnotationUtils.adaptValue(element, value, this.classValuesAsString, this.nestedAnnotationsAsMap); } }
/** * Get the array of raw (unsynthesized) annotations from the {@code value} * attribute of the supplied repeatable annotation {@code container}. * @since 4.3 */ @SuppressWarnings("unchecked") private static <A extends Annotation> A[] getRawAnnotationsFromContainer( @Nullable AnnotatedElement element, Annotation container) { try { A[] value = (A[]) AnnotationUtils.getValue(container); if (value != null) { return value; } } catch (Throwable ex) { AnnotationUtils.handleIntrospectionFailure(element, ex); } // Unable to read value from repeating annotation container -> ignore it. return (A[]) EMPTY_ANNOTATION_ARRAY; }
@SuppressWarnings("unchecked") private List<A> getValue(AnnotatedElement element, Annotation annotation) { try { List<A> synthesizedAnnotations = new ArrayList<>(); A[] value = (A[]) AnnotationUtils.getValue(annotation); if (value != null) { for (A anno : value) { synthesizedAnnotations.add(synthesizeAnnotation(anno, element)); } } return synthesizedAnnotations; } catch (Throwable ex) { handleIntrospectionFailure(element, ex); } // Unable to read value from repeating annotation container -> ignore it. return Collections.emptyList(); } }
private String getName(Class<?> type) { ConfigurationProperties annotation = AnnotationUtils.findAnnotation(type, ConfigurationProperties.class); String prefix = (annotation != null) ? annotation.prefix() : ""; return (StringUtils.hasText(prefix) ? prefix + "-" + type.getName() : type.getName()); }
/** * Perform the actual work for {@link #findAnnotation(AnnotatedElement, Class)}, * honoring the {@code synthesize} flag. * @param clazz the class to look for annotations on * @param annotationType the type of annotation to look for * @param synthesize {@code true} if the result should be * {@linkplain #synthesizeAnnotation(Annotation) synthesized} * @return the first matching annotation, or {@code null} if not found * @since 4.2.1 */ @SuppressWarnings("unchecked") @Nullable private static <A extends Annotation> A findAnnotation( Class<?> clazz, @Nullable Class<A> annotationType, boolean synthesize) { Assert.notNull(clazz, "Class must not be null"); if (annotationType == null) { return null; } AnnotationCacheKey cacheKey = new AnnotationCacheKey(clazz, annotationType); A result = (A) findAnnotationCache.get(cacheKey); if (result == null) { result = findAnnotation(clazz, annotationType, new HashSet<>()); if (result != null && synthesize) { result = synthesizeAnnotation(result, clazz); findAnnotationCache.put(cacheKey, result); } } return result; }
/** * Determine if an annotation of type {@code metaAnnotationType} is * <em>meta-present</em> on the supplied {@code annotationType}. * @param annotationType the annotation type to search on * @param metaAnnotationType the type of meta-annotation to search for * @return {@code true} if such an annotation is meta-present * @since 4.2.1 */ public static boolean isAnnotationMetaPresent(Class<? extends Annotation> annotationType, @Nullable Class<? extends Annotation> metaAnnotationType) { Assert.notNull(annotationType, "Annotation type must not be null"); if (metaAnnotationType == null) { return false; } AnnotationCacheKey cacheKey = new AnnotationCacheKey(annotationType, metaAnnotationType); Boolean metaPresent = metaPresentCache.get(cacheKey); if (metaPresent != null) { return metaPresent; } metaPresent = Boolean.FALSE; if (findAnnotation(annotationType, metaAnnotationType, false) != null) { metaPresent = Boolean.TRUE; } metaPresentCache.put(cacheKey, metaPresent); return metaPresent; }
static String determineHeaderName(Annotation headerAnnotation, MethodParameter methodParameter) { String valueAttribute = (String) AnnotationUtils.getValue(headerAnnotation); String headerName = StringUtils.hasText(valueAttribute) ? valueAttribute : methodParameter.getParameterName(); Assert.notNull(headerName, "Cannot determine header name. Possible reasons: -debug is " + "disabled or header name is not explicitly provided via @Header annotation."); return headerName; }
private void assertHasAnnotation(Class<?> type) { Assert.notNull( AnnotationUtils.findAnnotation(type, ConfigurationProperties.class), () -> "No " + ConfigurationProperties.class.getSimpleName() + " annotation found on '" + type.getName() + "'."); }
protected String[] getTargetDestinations(@Nullable Annotation annotation, Message<?> message, String defaultPrefix) { if (annotation != null) { String[] value = (String[]) AnnotationUtils.getValue(annotation); if (!ObjectUtils.isEmpty(value)) { return value; } } String name = DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER; String destination = (String) message.getHeaders().get(name); if (!StringUtils.hasText(destination)) { throw new IllegalStateException("No lookup destination header in " + message); } return (destination.startsWith("/") ? new String[] {defaultPrefix + destination} : new String[] {defaultPrefix + '/' + destination}); }
Assert.notNull(testClass, "Class must not be null"); Assert.notNull(desc, () -> String.format( "Could not find an 'annotation declaring class' for annotation type [%s] or [%s] and test class [%s]", contextConfigType.getName(), contextHierarchyType.getName(), testClass.getName())); Class<?> declaringClass = desc.getDeclaringClass(); boolean contextConfigDeclaredLocally = isAnnotationDeclaredLocally(contextConfigType, declaringClass); boolean contextHierarchyDeclaredLocally = isAnnotationDeclaredLocally(contextHierarchyType, declaringClass); ContextConfiguration contextConfiguration = AnnotationUtils.synthesizeAnnotation( desc.getAnnotationAttributes(), ContextConfiguration.class, desc.getRootDeclaringClass()); convertContextConfigToConfigAttributesAndAddToList( ContextHierarchy contextHierarchy = getAnnotation(declaringClass, contextHierarchyType); if (contextHierarchy != null) { for (ContextConfiguration contextConfiguration : contextHierarchy.value()) {
Assert.notNull(localSqlConfig, "Local @SqlConfig must not be null"); Assert.notNull(testClass, "testClass must not be null"); AnnotationAttributes attributes = AnnotatedElementUtils.findMergedAnnotationAttributes( testClass, SqlConfig.class.getName(), false, false); Object value = AnnotationUtils.getValue(localSqlConfig, key); if (value != null) { attributes = AnnotationUtils.getAnnotationAttributes(localSqlConfig, false, false);
@Override @Nullable public org.springframework.jmx.export.metadata.ManagedMetric getManagedMetric(Method method) throws InvalidMetadataException { ManagedMetric ann = AnnotationUtils.findAnnotation(method, ManagedMetric.class); return copyPropertiesToBean(ann, org.springframework.jmx.export.metadata.ManagedMetric.class); }
public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { Annotation annotation = AnnotationUtils.findAnnotation(method, annotationType); if (annotation != null) { Class<?>[] paramTypes = method.getParameterTypes(); if (paramTypes.length > 0) { String errorMsg = "The method [" + method.getName() + "] on target class [" + targetClass.getSimpleName() + "] is incompatable with the signature [" + getParamTypesString(expectedParamTypes) + "] expected for the annotation [" + annotationType.getSimpleName() + "]."; Assert.isTrue( paramTypes.length == expectedParamTypes.length, errorMsg); for (int i = 0; i < paramTypes.length; i++) { Assert.isTrue(expectedParamTypes[i] .isAssignableFrom(paramTypes[i]), errorMsg); } } } } });
public static String findRel(Class<?> type) { RestResource anno = findAnnotation(type, RestResource.class); if (anno != null) { if (hasText(anno.rel())) { return anno.rel(); } } return uncapitalize(type.getSimpleName().replaceAll("Repository", "")); }
@Test public void findMethodAnnotationWithAnnotationOnMethodInInterface() throws Exception { Method m = Leaf.class.getMethod("fromInterfaceImplementedByRoot"); // @Order is not @Inherited assertNull(m.getAnnotation(Order.class)); // getAnnotation() does not search on interfaces assertNull(getAnnotation(m, Order.class)); // findAnnotation() does search on interfaces assertNotNull(findAnnotation(m, Order.class)); }
private RetryPolicy getRetryPolicy(Annotation retryable) { Map<String, Object> attrs = AnnotationUtils.getAnnotationAttributes(retryable); @SuppressWarnings("unchecked") Class<? extends Throwable>[] includes = (Class<? extends Throwable>[]) attrs.get("value"); String exceptionExpression = (String) attrs.get("exceptionExpression"); boolean hasExpression = StringUtils.hasText(exceptionExpression); if (includes.length == 0) { @SuppressWarnings("unchecked") Integer maxAttempts = (Integer) attrs.get("maxAttempts"); String maxAttemptsExpression = (String) attrs.get("maxAttemptsExpression"); if (StringUtils.hasText(maxAttemptsExpression)) { maxAttempts = PARSER.parseExpression(resolve(maxAttemptsExpression), PARSER_CONTEXT) .getValue(this.evaluationContext, Integer.class);