Delete dead code in AnnotationAttributes
This commit is contained in:
parent
3f317b7d3f
commit
53dd88437e
|
@ -17,15 +17,12 @@
|
|||
package org.springframework.core.annotation;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.AnnotatedElement;
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
|
@ -349,104 +346,11 @@ public class AnnotationAttributes extends LinkedHashMap<String, Object> {
|
|||
return (T) value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value stored under the specified {@code attributeName} as an
|
||||
* object of the {@code expectedType}, taking into account alias semantics
|
||||
* defined via {@link AliasFor @AliasFor}.
|
||||
* <p>If there is no value stored under the specified {@code attributeName}
|
||||
* but the attribute has an alias declared via {@code @AliasFor}, the
|
||||
* value of the alias will be returned.
|
||||
* @param attributeName the name of the attribute to get; never
|
||||
* {@code null} or empty
|
||||
* @param annotationType the type of annotation represented by this
|
||||
* {@code AnnotationAttributes} instance; never {@code null}
|
||||
* @param annotationSource the source of the annotation represented by
|
||||
* this {@code AnnotationAttributes} (e.g., the {@link AnnotatedElement});
|
||||
* or {@code null} if unknown
|
||||
* @param expectedType the expected type; never {@code null}
|
||||
* @return the value
|
||||
* @throws IllegalArgumentException if the attribute and its alias do
|
||||
* not exist or are not of the {@code expectedType}
|
||||
* @throws AnnotationConfigurationException if the attribute and its
|
||||
* alias are both present with different non-empty values
|
||||
* @since 4.2
|
||||
* @see ObjectUtils#isEmpty(Object)
|
||||
*/
|
||||
private <T> T getRequiredAttributeWithAlias(String attributeName, Class<? extends Annotation> annotationType,
|
||||
Object annotationSource, Class<T> expectedType) {
|
||||
|
||||
Assert.hasText(attributeName, "'attributeName' must not be null or empty");
|
||||
Assert.notNull(annotationType, "'annotationType' must not be null");
|
||||
Assert.notNull(expectedType, "'expectedType' must not be null");
|
||||
|
||||
T attributeValue = getAttribute(attributeName, expectedType);
|
||||
|
||||
List<String> aliasNames = AnnotationUtils.getAttributeAliasMap(annotationType).get(attributeName);
|
||||
if (aliasNames != null) {
|
||||
for (String aliasName : aliasNames) {
|
||||
T aliasValue = getAttribute(aliasName, expectedType);
|
||||
boolean attributeEmpty = ObjectUtils.isEmpty(attributeValue);
|
||||
boolean aliasEmpty = ObjectUtils.isEmpty(aliasValue);
|
||||
|
||||
if (!attributeEmpty && !aliasEmpty && !ObjectUtils.nullSafeEquals(attributeValue, aliasValue)) {
|
||||
String elementName = (annotationSource == null ? "unknown element" : annotationSource.toString());
|
||||
String msg = String.format("In annotation [%s] declared on [%s], attribute [%s] and its " +
|
||||
"alias [%s] are present with values of [%s] and [%s], but only one is permitted.",
|
||||
annotationType.getName(), elementName, attributeName, aliasName,
|
||||
ObjectUtils.nullSafeToString(attributeValue), ObjectUtils.nullSafeToString(aliasValue));
|
||||
throw new AnnotationConfigurationException(msg);
|
||||
}
|
||||
|
||||
// If we expect an array and the current tracked value is null but the
|
||||
// current alias value is non-null, then replace the current null value
|
||||
// with the non-null value (which may be an empty array).
|
||||
if (expectedType.isArray() && attributeValue == null && aliasValue != null) {
|
||||
attributeValue = aliasValue;
|
||||
}
|
||||
// Else: if we're not expecting an array, we can rely on the behavior of
|
||||
// ObjectUtils.isEmpty().
|
||||
else if (attributeEmpty && !aliasEmpty) {
|
||||
attributeValue = aliasValue;
|
||||
}
|
||||
}
|
||||
assertAttributePresence(attributeName, aliasNames, attributeValue);
|
||||
}
|
||||
|
||||
return attributeValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value stored under the specified {@code attributeName},
|
||||
* ensuring that the value is of the {@code expectedType}.
|
||||
* @param attributeName the name of the attribute to get; never
|
||||
* {@code null} or empty
|
||||
* @param expectedType the expected type; never {@code null}
|
||||
* @return the value
|
||||
* @throws IllegalArgumentException if the attribute is not of the
|
||||
* expected type
|
||||
* @see #getRequiredAttribute(String, Class)
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private <T> T getAttribute(String attributeName, Class<T> expectedType) {
|
||||
Object value = get(attributeName);
|
||||
if (value != null) {
|
||||
assertNotException(attributeName, value);
|
||||
assertAttributeType(attributeName, value, expectedType);
|
||||
}
|
||||
return (T) value;
|
||||
}
|
||||
|
||||
private void assertAttributePresence(String attributeName, Object attributeValue) {
|
||||
Assert.notNull(attributeValue, () -> String.format("Attribute '%s' not found in attributes for annotation [%s]",
|
||||
attributeName, this.displayName));
|
||||
}
|
||||
|
||||
private void assertAttributePresence(String attributeName, List<String> aliases, Object attributeValue) {
|
||||
Assert.notNull(attributeValue, () -> String.format(
|
||||
"Neither attribute '%s' nor one of its aliases %s was found in attributes for annotation [%s]",
|
||||
attributeName, aliases, this.displayName));
|
||||
}
|
||||
|
||||
private void assertNotException(String attributeName, Object attributeValue) {
|
||||
if (attributeValue instanceof Exception) {
|
||||
throw new IllegalArgumentException(String.format(
|
||||
|
|
Loading…
Reference in New Issue