Reduce meta-data code duplicate with default methods

Extract and pull-up some common method implementations and make them
default methods of the interface.

See gh-22884
This commit is contained in:
Phillip Webb 2018-07-09 07:00:37 +01:00 committed by Juergen Hoeller
parent f592c1f211
commit 30ba80a3c3
9 changed files with 18 additions and 86 deletions

View File

@ -60,7 +60,9 @@ public interface AnnotatedTypeMetadata {
* {@code null} if no matching annotation is defined.
*/
@Nullable
Map<String, Object> getAnnotationAttributes(String annotationName);
default Map<String, Object> getAnnotationAttributes(String annotationName) {
return getAnnotationAttributes(annotationName, false);
}
/**
* Retrieve the attributes of the annotation of the given type, if any (i.e. if
@ -90,7 +92,9 @@ public interface AnnotatedTypeMetadata {
* @see #getAllAnnotationAttributes(String, boolean)
*/
@Nullable
MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationName);
default MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationName) {
return getAllAnnotationAttributes(annotationName, false);
}
/**
* Retrieve all attributes of all annotations of the given type, if any (i.e. if

View File

@ -56,7 +56,9 @@ public interface AnnotationMetadata extends ClassMetadata, AnnotatedTypeMetadata
* type to look for
* @return {@code true} if a matching annotation is present
*/
boolean hasAnnotation(String annotationName);
default boolean hasAnnotation(String annotationName) {
return getAnnotationTypes().contains(annotationName);
}
/**
* Determine whether the underlying class has an annotation that is itself

View File

@ -55,7 +55,9 @@ public interface ClassMetadata {
* Return whether the underlying class represents a concrete class,
* i.e. neither an interface nor an abstract class.
*/
boolean isConcrete();
default boolean isConcrete() {
return !(isInterface() || isAbstract());
}
/**
* Return whether the underlying class is marked as 'final'.
@ -76,7 +78,9 @@ public interface ClassMetadata {
* <p>If this method returns {@code false}, then the underlying
* class is a top-level class.
*/
boolean hasEnclosingClass();
default boolean hasEnclosingClass() {
return (getEnclosingClassName() != null);
}
/**
* Return the name of the enclosing class of the underlying class,
@ -88,7 +92,9 @@ public interface ClassMetadata {
/**
* Return whether the underlying class has a super class.
*/
boolean hasSuperClass();
default boolean hasSuperClass() {
return (getSuperClassName() != null);
}
/**
* Return the name of the super class of the underlying class,

View File

@ -123,11 +123,6 @@ public class StandardAnnotationMetadata extends StandardClassMetadata implements
AnnotatedElementUtils.isAnnotated(getIntrospectedClass(), annotationName));
}
@Override
public Map<String, Object> getAnnotationAttributes(String annotationName) {
return getAnnotationAttributes(annotationName, false);
}
@Override
@Nullable
public Map<String, Object> getAnnotationAttributes(String annotationName, boolean classValuesAsString) {
@ -135,12 +130,6 @@ public class StandardAnnotationMetadata extends StandardClassMetadata implements
getIntrospectedClass(), annotationName, classValuesAsString, this.nestedAnnotationsAsMap) : null);
}
@Override
@Nullable
public MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationName) {
return getAllAnnotationAttributes(annotationName, false);
}
@Override
@Nullable
public MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationName, boolean classValuesAsString) {

View File

@ -72,11 +72,6 @@ public class StandardClassMetadata implements ClassMetadata {
return Modifier.isAbstract(this.introspectedClass.getModifiers());
}
@Override
public boolean isConcrete() {
return !(isInterface() || isAbstract());
}
@Override
public boolean isFinal() {
return Modifier.isFinal(this.introspectedClass.getModifiers());
@ -89,11 +84,6 @@ public class StandardClassMetadata implements ClassMetadata {
Modifier.isStatic(this.introspectedClass.getModifiers())));
}
@Override
public boolean hasEnclosingClass() {
return (this.introspectedClass.getEnclosingClass() != null);
}
@Override
@Nullable
public String getEnclosingClassName() {
@ -101,11 +91,6 @@ public class StandardClassMetadata implements ClassMetadata {
return (enclosingClass != null ? enclosingClass.getName() : null);
}
@Override
public boolean hasSuperClass() {
return (this.introspectedClass.getSuperclass() != null);
}
@Override
@Nullable
public String getSuperClassName() {

View File

@ -115,12 +115,6 @@ public class StandardMethodMetadata implements MethodMetadata {
return AnnotatedElementUtils.isAnnotated(this.introspectedMethod, annotationName);
}
@Override
@Nullable
public Map<String, Object> getAnnotationAttributes(String annotationName) {
return getAnnotationAttributes(annotationName, false);
}
@Override
@Nullable
public Map<String, Object> getAnnotationAttributes(String annotationName, boolean classValuesAsString) {
@ -128,12 +122,6 @@ public class StandardMethodMetadata implements MethodMetadata {
annotationName, classValuesAsString, this.nestedAnnotationsAsMap);
}
@Override
@Nullable
public MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationName) {
return getAllAnnotationAttributes(annotationName, false);
}
@Override
@Nullable
public MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationName, boolean classValuesAsString) {

View File

@ -109,14 +109,6 @@ public class AnnotationMetadataReadingVisitor extends ClassMetadataReadingVisito
return (metaAnnotationTypes != null ? metaAnnotationTypes : Collections.emptySet());
}
@Override
public boolean hasAnnotation(String annotationName) {
if (AnnotationUtils.isInJavaLangAnnotationPackage(annotationName)) {
return false;
}
return this.annotationSet.contains(annotationName);
}
@Override
public boolean hasMetaAnnotation(String metaAnnotationType) {
if (AnnotationUtils.isInJavaLangAnnotationPackage(metaAnnotationType)) {
@ -137,12 +129,6 @@ public class AnnotationMetadataReadingVisitor extends ClassMetadataReadingVisito
this.attributesMap.containsKey(annotationName));
}
@Override
@Nullable
public AnnotationAttributes getAnnotationAttributes(String annotationName) {
return getAnnotationAttributes(annotationName, false);
}
@Override
@Nullable
public AnnotationAttributes getAnnotationAttributes(String annotationName, boolean classValuesAsString) {
@ -155,12 +141,6 @@ public class AnnotationMetadataReadingVisitor extends ClassMetadataReadingVisito
"class '" + getClassName() + "'", this.classLoader, raw, classValuesAsString);
}
@Override
@Nullable
public MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationName) {
return getAllAnnotationAttributes(annotationName, false);
}
@Override
@Nullable
public MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationName, boolean classValuesAsString) {

View File

@ -165,11 +165,6 @@ class ClassMetadataReadingVisitor extends ClassVisitor implements ClassMetadata
return this.isAbstract;
}
@Override
public boolean isConcrete() {
return !(this.isInterface || this.isAbstract);
}
@Override
public boolean isFinal() {
return this.isFinal;
@ -191,11 +186,6 @@ class ClassMetadataReadingVisitor extends ClassVisitor implements ClassMetadata
return this.enclosingClassName;
}
@Override
public boolean hasSuperClass() {
return (this.superClassName != null);
}
@Override
@Nullable
public String getSuperClassName() {

View File

@ -119,12 +119,6 @@ public class MethodMetadataReadingVisitor extends MethodVisitor implements Metho
return this.attributesMap.containsKey(annotationName);
}
@Override
@Nullable
public AnnotationAttributes getAnnotationAttributes(String annotationName) {
return getAnnotationAttributes(annotationName, false);
}
@Override
@Nullable
public AnnotationAttributes getAnnotationAttributes(String annotationName, boolean classValuesAsString) {
@ -137,12 +131,6 @@ public class MethodMetadataReadingVisitor extends MethodVisitor implements Metho
"method '" + getMethodName() + "'", this.classLoader, raw, classValuesAsString);
}
@Override
@Nullable
public MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationName) {
return getAllAnnotationAttributes(annotationName, false);
}
@Override
@Nullable
public MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationName, boolean classValuesAsString) {