Skip to content

Commit 30ba80a

Browse files
philwebbjhoeller
authored andcommitted
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
1 parent f592c1f commit 30ba80a

9 files changed

+18
-86
lines changed

spring-core/src/main/java/org/springframework/core/type/AnnotatedTypeMetadata.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ public interface AnnotatedTypeMetadata {
6060
* {@code null} if no matching annotation is defined.
6161
*/
6262
@Nullable
63-
Map<String, Object> getAnnotationAttributes(String annotationName);
63+
default Map<String, Object> getAnnotationAttributes(String annotationName) {
64+
return getAnnotationAttributes(annotationName, false);
65+
}
6466

6567
/**
6668
* Retrieve the attributes of the annotation of the given type, if any (i.e. if
@@ -90,7 +92,9 @@ public interface AnnotatedTypeMetadata {
9092
* @see #getAllAnnotationAttributes(String, boolean)
9193
*/
9294
@Nullable
93-
MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationName);
95+
default MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationName) {
96+
return getAllAnnotationAttributes(annotationName, false);
97+
}
9498

9599
/**
96100
* Retrieve all attributes of all annotations of the given type, if any (i.e. if

spring-core/src/main/java/org/springframework/core/type/AnnotationMetadata.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ public interface AnnotationMetadata extends ClassMetadata, AnnotatedTypeMetadata
5656
* type to look for
5757
* @return {@code true} if a matching annotation is present
5858
*/
59-
boolean hasAnnotation(String annotationName);
59+
default boolean hasAnnotation(String annotationName) {
60+
return getAnnotationTypes().contains(annotationName);
61+
}
6062

6163
/**
6264
* Determine whether the underlying class has an annotation that is itself

spring-core/src/main/java/org/springframework/core/type/ClassMetadata.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ public interface ClassMetadata {
5555
* Return whether the underlying class represents a concrete class,
5656
* i.e. neither an interface nor an abstract class.
5757
*/
58-
boolean isConcrete();
58+
default boolean isConcrete() {
59+
return !(isInterface() || isAbstract());
60+
}
5961

6062
/**
6163
* Return whether the underlying class is marked as 'final'.
@@ -76,7 +78,9 @@ public interface ClassMetadata {
7678
* <p>If this method returns {@code false}, then the underlying
7779
* class is a top-level class.
7880
*/
79-
boolean hasEnclosingClass();
81+
default boolean hasEnclosingClass() {
82+
return (getEnclosingClassName() != null);
83+
}
8084

8185
/**
8286
* Return the name of the enclosing class of the underlying class,
@@ -88,7 +92,9 @@ public interface ClassMetadata {
8892
/**
8993
* Return whether the underlying class has a super class.
9094
*/
91-
boolean hasSuperClass();
95+
default boolean hasSuperClass() {
96+
return (getSuperClassName() != null);
97+
}
9298

9399
/**
94100
* Return the name of the super class of the underlying class,

spring-core/src/main/java/org/springframework/core/type/StandardAnnotationMetadata.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -123,24 +123,13 @@ public boolean isAnnotated(String annotationName) {
123123
AnnotatedElementUtils.isAnnotated(getIntrospectedClass(), annotationName));
124124
}
125125

126-
@Override
127-
public Map<String, Object> getAnnotationAttributes(String annotationName) {
128-
return getAnnotationAttributes(annotationName, false);
129-
}
130-
131126
@Override
132127
@Nullable
133128
public Map<String, Object> getAnnotationAttributes(String annotationName, boolean classValuesAsString) {
134129
return (this.annotations.length > 0 ? AnnotatedElementUtils.getMergedAnnotationAttributes(
135130
getIntrospectedClass(), annotationName, classValuesAsString, this.nestedAnnotationsAsMap) : null);
136131
}
137132

138-
@Override
139-
@Nullable
140-
public MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationName) {
141-
return getAllAnnotationAttributes(annotationName, false);
142-
}
143-
144133
@Override
145134
@Nullable
146135
public MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationName, boolean classValuesAsString) {

spring-core/src/main/java/org/springframework/core/type/StandardClassMetadata.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,6 @@ public boolean isAbstract() {
7272
return Modifier.isAbstract(this.introspectedClass.getModifiers());
7373
}
7474

75-
@Override
76-
public boolean isConcrete() {
77-
return !(isInterface() || isAbstract());
78-
}
79-
8075
@Override
8176
public boolean isFinal() {
8277
return Modifier.isFinal(this.introspectedClass.getModifiers());
@@ -89,23 +84,13 @@ public boolean isIndependent() {
8984
Modifier.isStatic(this.introspectedClass.getModifiers())));
9085
}
9186

92-
@Override
93-
public boolean hasEnclosingClass() {
94-
return (this.introspectedClass.getEnclosingClass() != null);
95-
}
96-
9787
@Override
9888
@Nullable
9989
public String getEnclosingClassName() {
10090
Class<?> enclosingClass = this.introspectedClass.getEnclosingClass();
10191
return (enclosingClass != null ? enclosingClass.getName() : null);
10292
}
10393

104-
@Override
105-
public boolean hasSuperClass() {
106-
return (this.introspectedClass.getSuperclass() != null);
107-
}
108-
10994
@Override
11095
@Nullable
11196
public String getSuperClassName() {

spring-core/src/main/java/org/springframework/core/type/StandardMethodMetadata.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -115,25 +115,13 @@ public boolean isAnnotated(String annotationName) {
115115
return AnnotatedElementUtils.isAnnotated(this.introspectedMethod, annotationName);
116116
}
117117

118-
@Override
119-
@Nullable
120-
public Map<String, Object> getAnnotationAttributes(String annotationName) {
121-
return getAnnotationAttributes(annotationName, false);
122-
}
123-
124118
@Override
125119
@Nullable
126120
public Map<String, Object> getAnnotationAttributes(String annotationName, boolean classValuesAsString) {
127121
return AnnotatedElementUtils.getMergedAnnotationAttributes(this.introspectedMethod,
128122
annotationName, classValuesAsString, this.nestedAnnotationsAsMap);
129123
}
130124

131-
@Override
132-
@Nullable
133-
public MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationName) {
134-
return getAllAnnotationAttributes(annotationName, false);
135-
}
136-
137125
@Override
138126
@Nullable
139127
public MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationName, boolean classValuesAsString) {

spring-core/src/main/java/org/springframework/core/type/classreading/AnnotationMetadataReadingVisitor.java

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,6 @@ public Set<String> getMetaAnnotationTypes(String annotationName) {
109109
return (metaAnnotationTypes != null ? metaAnnotationTypes : Collections.emptySet());
110110
}
111111

112-
@Override
113-
public boolean hasAnnotation(String annotationName) {
114-
if (AnnotationUtils.isInJavaLangAnnotationPackage(annotationName)) {
115-
return false;
116-
}
117-
return this.annotationSet.contains(annotationName);
118-
}
119-
120112
@Override
121113
public boolean hasMetaAnnotation(String metaAnnotationType) {
122114
if (AnnotationUtils.isInJavaLangAnnotationPackage(metaAnnotationType)) {
@@ -137,12 +129,6 @@ public boolean isAnnotated(String annotationName) {
137129
this.attributesMap.containsKey(annotationName));
138130
}
139131

140-
@Override
141-
@Nullable
142-
public AnnotationAttributes getAnnotationAttributes(String annotationName) {
143-
return getAnnotationAttributes(annotationName, false);
144-
}
145-
146132
@Override
147133
@Nullable
148134
public AnnotationAttributes getAnnotationAttributes(String annotationName, boolean classValuesAsString) {
@@ -155,12 +141,6 @@ public AnnotationAttributes getAnnotationAttributes(String annotationName, boole
155141
"class '" + getClassName() + "'", this.classLoader, raw, classValuesAsString);
156142
}
157143

158-
@Override
159-
@Nullable
160-
public MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationName) {
161-
return getAllAnnotationAttributes(annotationName, false);
162-
}
163-
164144
@Override
165145
@Nullable
166146
public MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationName, boolean classValuesAsString) {

spring-core/src/main/java/org/springframework/core/type/classreading/ClassMetadataReadingVisitor.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,6 @@ public boolean isAbstract() {
165165
return this.isAbstract;
166166
}
167167

168-
@Override
169-
public boolean isConcrete() {
170-
return !(this.isInterface || this.isAbstract);
171-
}
172-
173168
@Override
174169
public boolean isFinal() {
175170
return this.isFinal;
@@ -191,11 +186,6 @@ public String getEnclosingClassName() {
191186
return this.enclosingClassName;
192187
}
193188

194-
@Override
195-
public boolean hasSuperClass() {
196-
return (this.superClassName != null);
197-
}
198-
199189
@Override
200190
@Nullable
201191
public String getSuperClassName() {

spring-core/src/main/java/org/springframework/core/type/classreading/MethodMetadataReadingVisitor.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,6 @@ public boolean isAnnotated(String annotationName) {
119119
return this.attributesMap.containsKey(annotationName);
120120
}
121121

122-
@Override
123-
@Nullable
124-
public AnnotationAttributes getAnnotationAttributes(String annotationName) {
125-
return getAnnotationAttributes(annotationName, false);
126-
}
127-
128122
@Override
129123
@Nullable
130124
public AnnotationAttributes getAnnotationAttributes(String annotationName, boolean classValuesAsString) {
@@ -137,12 +131,6 @@ public AnnotationAttributes getAnnotationAttributes(String annotationName, boole
137131
"method '" + getMethodName() + "'", this.classLoader, raw, classValuesAsString);
138132
}
139133

140-
@Override
141-
@Nullable
142-
public MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationName) {
143-
return getAllAnnotationAttributes(annotationName, false);
144-
}
145-
146134
@Override
147135
@Nullable
148136
public MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationName, boolean classValuesAsString) {

0 commit comments

Comments
 (0)