Remove unnecessary assertions from annotation resolution code paths
Issue: SPR-16514
This commit is contained in:
parent
3ba858213f
commit
f316f6a46a
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -30,7 +30,6 @@ import java.util.Set;
|
|||
|
||||
import org.springframework.core.BridgeMethodResolver;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
|
||||
|
|
@ -154,9 +153,6 @@ public class AnnotatedElementUtils {
|
|||
* @see #hasMetaAnnotationTypes
|
||||
*/
|
||||
public static Set<String> getMetaAnnotationTypes(AnnotatedElement element, Class<? extends Annotation> annotationType) {
|
||||
Assert.notNull(element, "AnnotatedElement must not be null");
|
||||
Assert.notNull(annotationType, "'annotationType' must not be null");
|
||||
|
||||
return getMetaAnnotationTypes(element, element.getAnnotation(annotationType));
|
||||
}
|
||||
|
||||
|
|
@ -175,9 +171,6 @@ public class AnnotatedElementUtils {
|
|||
* @see #hasMetaAnnotationTypes
|
||||
*/
|
||||
public static Set<String> getMetaAnnotationTypes(AnnotatedElement element, String annotationName) {
|
||||
Assert.notNull(element, "AnnotatedElement must not be null");
|
||||
Assert.hasLength(annotationName, "'annotationName' must not be null or empty");
|
||||
|
||||
return getMetaAnnotationTypes(element, AnnotationUtils.getAnnotation(element, annotationName));
|
||||
}
|
||||
|
||||
|
|
@ -217,9 +210,6 @@ public class AnnotatedElementUtils {
|
|||
* @see #getMetaAnnotationTypes
|
||||
*/
|
||||
public static boolean hasMetaAnnotationTypes(AnnotatedElement element, Class<? extends Annotation> annotationType) {
|
||||
Assert.notNull(element, "AnnotatedElement must not be null");
|
||||
Assert.notNull(annotationType, "'annotationType' must not be null");
|
||||
|
||||
return hasMetaAnnotationTypes(element, annotationType, null);
|
||||
}
|
||||
|
||||
|
|
@ -236,9 +226,6 @@ public class AnnotatedElementUtils {
|
|||
* @see #getMetaAnnotationTypes
|
||||
*/
|
||||
public static boolean hasMetaAnnotationTypes(AnnotatedElement element, String annotationName) {
|
||||
Assert.notNull(element, "AnnotatedElement must not be null");
|
||||
Assert.hasLength(annotationName, "'annotationName' must not be null or empty");
|
||||
|
||||
return hasMetaAnnotationTypes(element, null, annotationName);
|
||||
}
|
||||
|
||||
|
|
@ -271,14 +258,10 @@ public class AnnotatedElementUtils {
|
|||
* @see #hasAnnotation(AnnotatedElement, Class)
|
||||
*/
|
||||
public static boolean isAnnotated(AnnotatedElement element, Class<? extends Annotation> annotationType) {
|
||||
Assert.notNull(element, "AnnotatedElement must not be null");
|
||||
Assert.notNull(annotationType, "'annotationType' must not be null");
|
||||
|
||||
// Shortcut: directly present on the element, with no processing needed?
|
||||
if (element.isAnnotationPresent(annotationType)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return Boolean.TRUE.equals(searchWithGetSemantics(element, annotationType, null, alwaysTrueAnnotationProcessor));
|
||||
}
|
||||
|
||||
|
|
@ -295,9 +278,6 @@ public class AnnotatedElementUtils {
|
|||
* @return {@code true} if a matching annotation is present
|
||||
*/
|
||||
public static boolean isAnnotated(AnnotatedElement element, String annotationName) {
|
||||
Assert.notNull(element, "AnnotatedElement must not be null");
|
||||
Assert.hasLength(annotationName, "'annotationName' must not be null or empty");
|
||||
|
||||
return Boolean.TRUE.equals(searchWithGetSemantics(element, null, annotationName, alwaysTrueAnnotationProcessor));
|
||||
}
|
||||
|
||||
|
|
@ -322,7 +302,6 @@ public class AnnotatedElementUtils {
|
|||
public static AnnotationAttributes getMergedAnnotationAttributes(
|
||||
AnnotatedElement element, Class<? extends Annotation> annotationType) {
|
||||
|
||||
Assert.notNull(annotationType, "'annotationType' must not be null");
|
||||
AnnotationAttributes attributes = searchWithGetSemantics(element, annotationType, null,
|
||||
new MergedAnnotationAttributesProcessor());
|
||||
AnnotationUtils.postProcessAnnotationAttributes(element, attributes, false, false);
|
||||
|
|
@ -382,7 +361,6 @@ public class AnnotatedElementUtils {
|
|||
public static AnnotationAttributes getMergedAnnotationAttributes(AnnotatedElement element,
|
||||
String annotationName, boolean classValuesAsString, boolean nestedAnnotationsAsMap) {
|
||||
|
||||
Assert.hasLength(annotationName, "'annotationName' must not be null or empty");
|
||||
AnnotationAttributes attributes = searchWithGetSemantics(element, null, annotationName,
|
||||
new MergedAnnotationAttributesProcessor(classValuesAsString, nestedAnnotationsAsMap));
|
||||
AnnotationUtils.postProcessAnnotationAttributes(element, attributes, classValuesAsString, nestedAnnotationsAsMap);
|
||||
|
|
@ -409,8 +387,6 @@ public class AnnotatedElementUtils {
|
|||
*/
|
||||
@Nullable
|
||||
public static <A extends Annotation> A getMergedAnnotation(AnnotatedElement element, Class<A> annotationType) {
|
||||
Assert.notNull(annotationType, "'annotationType' must not be null");
|
||||
|
||||
// Shortcut: directly present on the element, with no merging needed?
|
||||
if (!(element instanceof Class)) {
|
||||
// Do not use this shortcut against a Class: Inherited annotations
|
||||
|
|
@ -446,12 +422,7 @@ public class AnnotatedElementUtils {
|
|||
* @see #getAllAnnotationAttributes(AnnotatedElement, String)
|
||||
* @see #findAllMergedAnnotations(AnnotatedElement, Class)
|
||||
*/
|
||||
public static <A extends Annotation> Set<A> getAllMergedAnnotations(AnnotatedElement element,
|
||||
Class<A> annotationType) {
|
||||
|
||||
Assert.notNull(element, "AnnotatedElement must not be null");
|
||||
Assert.notNull(annotationType, "'annotationType' must not be null");
|
||||
|
||||
public static <A extends Annotation> Set<A> getAllMergedAnnotations(AnnotatedElement element, Class<A> annotationType) {
|
||||
MergedAnnotationAttributesProcessor processor = new MergedAnnotationAttributesProcessor(false, false, true);
|
||||
searchWithGetSemantics(element, annotationType, null, processor);
|
||||
return postProcessAndSynthesizeAggregatedResults(element, annotationType, processor.getAggregatedResults());
|
||||
|
|
@ -516,9 +487,6 @@ public class AnnotatedElementUtils {
|
|||
public static <A extends Annotation> Set<A> getMergedRepeatableAnnotations(AnnotatedElement element,
|
||||
Class<A> annotationType, @Nullable Class<? extends Annotation> containerType) {
|
||||
|
||||
Assert.notNull(element, "AnnotatedElement must not be null");
|
||||
Assert.notNull(annotationType, "'annotationType' must not be null");
|
||||
|
||||
if (containerType == null) {
|
||||
containerType = resolveContainerType(annotationType);
|
||||
}
|
||||
|
|
@ -603,14 +571,10 @@ public class AnnotatedElementUtils {
|
|||
* @see #isAnnotated(AnnotatedElement, Class)
|
||||
*/
|
||||
public static boolean hasAnnotation(AnnotatedElement element, Class<? extends Annotation> annotationType) {
|
||||
Assert.notNull(element, "AnnotatedElement must not be null");
|
||||
Assert.notNull(annotationType, "'annotationType' must not be null");
|
||||
|
||||
// Shortcut: directly present on the element, with no processing needed?
|
||||
if (element.isAnnotationPresent(annotationType)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return Boolean.TRUE.equals(searchWithFindSemantics(element, annotationType, null, alwaysTrueAnnotationProcessor));
|
||||
}
|
||||
|
||||
|
|
@ -710,8 +674,6 @@ public class AnnotatedElementUtils {
|
|||
*/
|
||||
@Nullable
|
||||
public static <A extends Annotation> A findMergedAnnotation(AnnotatedElement element, Class<A> annotationType) {
|
||||
Assert.notNull(annotationType, "'annotationType' must not be null");
|
||||
|
||||
// Shortcut: directly present on the element, with no merging needed?
|
||||
if (!(element instanceof Class)) {
|
||||
// Do not use this shortcut against a Class: Inherited annotations
|
||||
|
|
@ -746,12 +708,7 @@ public class AnnotatedElementUtils {
|
|||
* @see #findMergedAnnotation(AnnotatedElement, Class)
|
||||
* @see #getAllMergedAnnotations(AnnotatedElement, Class)
|
||||
*/
|
||||
public static <A extends Annotation> Set<A> findAllMergedAnnotations(AnnotatedElement element,
|
||||
Class<A> annotationType) {
|
||||
|
||||
Assert.notNull(element, "AnnotatedElement must not be null");
|
||||
Assert.notNull(annotationType, "'annotationType' must not be null");
|
||||
|
||||
public static <A extends Annotation> Set<A> findAllMergedAnnotations(AnnotatedElement element, Class<A> annotationType) {
|
||||
MergedAnnotationAttributesProcessor processor = new MergedAnnotationAttributesProcessor(false, false, true);
|
||||
searchWithFindSemantics(element, annotationType, null, processor);
|
||||
return postProcessAndSynthesizeAggregatedResults(element, annotationType, processor.getAggregatedResults());
|
||||
|
|
@ -816,9 +773,6 @@ public class AnnotatedElementUtils {
|
|||
public static <A extends Annotation> Set<A> findMergedRepeatableAnnotations(AnnotatedElement element,
|
||||
Class<A> annotationType, @Nullable Class<? extends Annotation> containerType) {
|
||||
|
||||
Assert.notNull(element, "AnnotatedElement must not be null");
|
||||
Assert.notNull(annotationType, "'annotationType' must not be null");
|
||||
|
||||
if (containerType == null) {
|
||||
containerType = resolveContainerType(annotationType);
|
||||
}
|
||||
|
|
@ -902,8 +856,6 @@ public class AnnotatedElementUtils {
|
|||
@Nullable Class<? extends Annotation> containerType, Processor<T> processor,
|
||||
Set<AnnotatedElement> visited, int metaDepth) {
|
||||
|
||||
Assert.notNull(element, "AnnotatedElement must not be null");
|
||||
|
||||
if (visited.add(element)) {
|
||||
try {
|
||||
// Start searching within locally declared annotations
|
||||
|
|
@ -1096,8 +1048,6 @@ public class AnnotatedElementUtils {
|
|||
@Nullable Class<? extends Annotation> containerType, Processor<T> processor,
|
||||
Set<AnnotatedElement> visited, int metaDepth) {
|
||||
|
||||
Assert.notNull(element, "AnnotatedElement must not be null");
|
||||
|
||||
if (visited.add(element)) {
|
||||
try {
|
||||
// Locally declared annotations (ignoring @Inherited)
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -439,9 +439,6 @@ public abstract class AnnotationUtils {
|
|||
private static <A extends Annotation> Set<A> getRepeatableAnnotations(AnnotatedElement annotatedElement,
|
||||
Class<A> annotationType, @Nullable Class<? extends Annotation> containerAnnotationType, boolean declaredMode) {
|
||||
|
||||
Assert.notNull(annotatedElement, "AnnotatedElement must not be null");
|
||||
Assert.notNull(annotationType, "Annotation type must not be null");
|
||||
|
||||
try {
|
||||
if (annotatedElement instanceof Method) {
|
||||
annotatedElement = BridgeMethodResolver.findBridgedMethod((Method) annotatedElement);
|
||||
|
|
@ -472,8 +469,6 @@ public abstract class AnnotationUtils {
|
|||
*/
|
||||
@Nullable
|
||||
public static <A extends Annotation> A findAnnotation(AnnotatedElement annotatedElement, Class<A> annotationType) {
|
||||
Assert.notNull(annotatedElement, "AnnotatedElement must not be null");
|
||||
|
||||
// Do NOT store result in the findAnnotationCache since doing so could break
|
||||
// findAnnotation(Class, Class) and findAnnotation(Method, Class).
|
||||
A ann = findAnnotation(annotatedElement, annotationType, new HashSet<>());
|
||||
|
|
@ -745,7 +740,6 @@ public abstract class AnnotationUtils {
|
|||
*/
|
||||
@Nullable
|
||||
public static Class<?> findAnnotationDeclaringClass(Class<? extends Annotation> annotationType, @Nullable Class<?> clazz) {
|
||||
Assert.notNull(annotationType, "Annotation type must not be null");
|
||||
if (clazz == null || Object.class == clazz) {
|
||||
return null;
|
||||
}
|
||||
|
|
@ -781,7 +775,6 @@ public abstract class AnnotationUtils {
|
|||
*/
|
||||
@Nullable
|
||||
public static Class<?> findAnnotationDeclaringClassForTypes(List<Class<? extends Annotation>> annotationTypes, @Nullable Class<?> clazz) {
|
||||
Assert.notEmpty(annotationTypes, "List of annotation types must not be empty");
|
||||
if (clazz == null || Object.class == clazz) {
|
||||
return null;
|
||||
}
|
||||
|
|
@ -812,8 +805,6 @@ public abstract class AnnotationUtils {
|
|||
* @see #isAnnotationInherited(Class, Class)
|
||||
*/
|
||||
public static boolean isAnnotationDeclaredLocally(Class<? extends Annotation> annotationType, Class<?> clazz) {
|
||||
Assert.notNull(annotationType, "Annotation type must not be null");
|
||||
Assert.notNull(clazz, "Class must not be null");
|
||||
try {
|
||||
return (clazz.getDeclaredAnnotation(annotationType) != null);
|
||||
}
|
||||
|
|
@ -843,8 +834,6 @@ public abstract class AnnotationUtils {
|
|||
* @see #isAnnotationDeclaredLocally(Class, Class)
|
||||
*/
|
||||
public static boolean isAnnotationInherited(Class<? extends Annotation> annotationType, Class<?> clazz) {
|
||||
Assert.notNull(annotationType, "Annotation type must not be null");
|
||||
Assert.notNull(clazz, "Class must not be null");
|
||||
return (clazz.isAnnotationPresent(annotationType) && !isAnnotationDeclaredLocally(annotationType, clazz));
|
||||
}
|
||||
|
||||
|
|
@ -1444,7 +1433,6 @@ public abstract class AnnotationUtils {
|
|||
|
||||
@SuppressWarnings("unchecked")
|
||||
static <A extends Annotation> A synthesizeAnnotation(A annotation, @Nullable Object annotatedElement) {
|
||||
Assert.notNull(annotation, "Annotation must not be null");
|
||||
if (annotation instanceof SynthesizedAnnotation) {
|
||||
return annotation;
|
||||
}
|
||||
|
|
@ -1497,9 +1485,6 @@ public abstract class AnnotationUtils {
|
|||
public static <A extends Annotation> A synthesizeAnnotation(Map<String, Object> attributes,
|
||||
Class<A> annotationType, @Nullable AnnotatedElement annotatedElement) {
|
||||
|
||||
Assert.notNull(attributes, "'attributes' must not be null");
|
||||
Assert.notNull(annotationType, "'annotationType' must not be null");
|
||||
|
||||
MapAnnotationAttributeExtractor attributeExtractor =
|
||||
new MapAnnotationAttributeExtractor(attributes, annotationType, annotatedElement);
|
||||
InvocationHandler handler = new SynthesizedAnnotationInvocationHandler(attributeExtractor);
|
||||
|
|
@ -1574,7 +1559,6 @@ public abstract class AnnotationUtils {
|
|||
@SuppressWarnings("unchecked")
|
||||
@Nullable
|
||||
static <A extends Annotation> A[] synthesizeAnnotationArray(@Nullable Map<String, Object>[] maps, Class<A> annotationType) {
|
||||
Assert.notNull(annotationType, "'annotationType' must not be null");
|
||||
if (maps == null) {
|
||||
return null;
|
||||
}
|
||||
|
|
@ -1703,7 +1687,6 @@ public abstract class AnnotationUtils {
|
|||
* @see #getAttributeOverrideName(Method, Class)
|
||||
*/
|
||||
static List<String> getAttributeAliasNames(Method attribute) {
|
||||
Assert.notNull(attribute, "attribute must not be null");
|
||||
AliasDescriptor descriptor = AliasDescriptor.from(attribute);
|
||||
return (descriptor != null ? descriptor.getAttributeAliasNames() : Collections.<String> emptyList());
|
||||
}
|
||||
|
|
@ -1726,13 +1709,9 @@ public abstract class AnnotationUtils {
|
|||
*/
|
||||
@Nullable
|
||||
static String getAttributeOverrideName(Method attribute, @Nullable Class<? extends Annotation> metaAnnotationType) {
|
||||
Assert.notNull(attribute, "attribute must not be null");
|
||||
Assert.notNull(metaAnnotationType, "metaAnnotationType must not be null");
|
||||
Assert.isTrue(Annotation.class != metaAnnotationType,
|
||||
"metaAnnotationType must not be [java.lang.annotation.Annotation]");
|
||||
|
||||
AliasDescriptor descriptor = AliasDescriptor.from(attribute);
|
||||
return (descriptor != null ? descriptor.getAttributeOverrideName(metaAnnotationType) : null);
|
||||
return (descriptor != null && metaAnnotationType != null ?
|
||||
descriptor.getAttributeOverrideName(metaAnnotationType) : null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -2043,7 +2022,6 @@ public abstract class AnnotationUtils {
|
|||
@SuppressWarnings("unchecked")
|
||||
private AliasDescriptor(Method sourceAttribute, AliasFor aliasFor) {
|
||||
Class<?> declaringClass = sourceAttribute.getDeclaringClass();
|
||||
Assert.isTrue(declaringClass.isAnnotation(), "sourceAttribute must be from an annotation");
|
||||
|
||||
this.sourceAttribute = sourceAttribute;
|
||||
this.sourceAnnotationType = (Class<? extends Annotation>) declaringClass;
|
||||
|
|
@ -2117,7 +2095,6 @@ public abstract class AnnotationUtils {
|
|||
}
|
||||
|
||||
private void validateDefaultValueConfiguration(Method aliasedAttribute) {
|
||||
Assert.notNull(aliasedAttribute, "aliasedAttribute must not be null");
|
||||
Object defaultValue = this.sourceAttribute.getDefaultValue();
|
||||
Object aliasedDefaultValue = aliasedAttribute.getDefaultValue();
|
||||
|
||||
|
|
@ -2211,10 +2188,6 @@ public abstract class AnnotationUtils {
|
|||
|
||||
@Nullable
|
||||
public String getAttributeOverrideName(Class<? extends Annotation> metaAnnotationType) {
|
||||
Assert.notNull(metaAnnotationType, "metaAnnotationType must not be null");
|
||||
Assert.isTrue(Annotation.class != metaAnnotationType,
|
||||
"metaAnnotationType must not be [java.lang.annotation.Annotation]");
|
||||
|
||||
// Search the attribute override hierarchy, starting with the current attribute
|
||||
for (AliasDescriptor desc = this; desc != null; desc = desc.getAttributeOverrideDescriptor()) {
|
||||
if (desc.isOverrideFor(metaAnnotationType)) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue