30
30
import org .springframework .core .MethodParameter ;
31
31
import org .springframework .core .ResolvableType ;
32
32
import org .springframework .core .annotation .AnnotatedElementUtils ;
33
+ import org .springframework .lang .NonNull ;
33
34
import org .springframework .lang .Nullable ;
34
35
import org .springframework .util .Assert ;
35
36
import org .springframework .util .ClassUtils ;
52
53
@ SuppressWarnings ("serial" )
53
54
public class TypeDescriptor implements Serializable {
54
55
55
- private static final Annotation [] EMPTY_ANNOTATION_ARRAY = new Annotation [0 ];
56
-
57
56
private static final Map <Class <?>, TypeDescriptor > commonTypesCache = new HashMap <>(32 );
58
57
59
58
private static final Class <?>[] CACHED_COMMON_TYPES = {
@@ -84,7 +83,7 @@ public class TypeDescriptor implements Serializable {
84
83
public TypeDescriptor (MethodParameter methodParameter ) {
85
84
this .resolvableType = ResolvableType .forMethodParameter (methodParameter );
86
85
this .type = this .resolvableType .resolve (methodParameter .getNestedParameterType ());
87
- this .annotatedElement = new AnnotatedElementAdapter (methodParameter .getParameterIndex () == -1 ?
86
+ this .annotatedElement = AnnotatedElementAdapter . from (methodParameter .getParameterIndex () == -1 ?
88
87
methodParameter .getMethodAnnotations () : methodParameter .getParameterAnnotations ());
89
88
}
90
89
@@ -96,7 +95,7 @@ public TypeDescriptor(MethodParameter methodParameter) {
96
95
public TypeDescriptor (Field field ) {
97
96
this .resolvableType = ResolvableType .forField (field );
98
97
this .type = this .resolvableType .resolve (field .getType ());
99
- this .annotatedElement = new AnnotatedElementAdapter (field .getAnnotations ());
98
+ this .annotatedElement = AnnotatedElementAdapter . from (field .getAnnotations ());
100
99
}
101
100
102
101
/**
@@ -109,7 +108,7 @@ public TypeDescriptor(Property property) {
109
108
Assert .notNull (property , "Property must not be null" );
110
109
this .resolvableType = ResolvableType .forMethodParameter (property .getMethodParameter ());
111
110
this .type = this .resolvableType .resolve (property .getType ());
112
- this .annotatedElement = new AnnotatedElementAdapter (property .getAnnotations ());
111
+ this .annotatedElement = AnnotatedElementAdapter . from (property .getAnnotations ());
113
112
}
114
113
115
114
/**
@@ -125,7 +124,7 @@ public TypeDescriptor(Property property) {
125
124
public TypeDescriptor (ResolvableType resolvableType , @ Nullable Class <?> type , @ Nullable Annotation [] annotations ) {
126
125
this .resolvableType = resolvableType ;
127
126
this .type = (type != null ? type : resolvableType .toClass ());
128
- this .annotatedElement = new AnnotatedElementAdapter (annotations );
127
+ this .annotatedElement = AnnotatedElementAdapter . from (annotations );
129
128
}
130
129
131
130
@@ -742,15 +741,23 @@ public static TypeDescriptor nested(Property property, int nestingLevel) {
742
741
* @see AnnotatedElementUtils#isAnnotated(AnnotatedElement, Class)
743
742
* @see AnnotatedElementUtils#getMergedAnnotation(AnnotatedElement, Class)
744
743
*/
745
- private class AnnotatedElementAdapter implements AnnotatedElement , Serializable {
744
+ private static final class AnnotatedElementAdapter implements AnnotatedElement , Serializable {
745
+ private static final AnnotatedElementAdapter EMPTY = new AnnotatedElementAdapter (new Annotation [0 ]);
746
746
747
- @ Nullable
747
+ @ NonNull
748
748
private final Annotation [] annotations ;
749
749
750
- public AnnotatedElementAdapter (@ Nullable Annotation [] annotations ) {
750
+ private AnnotatedElementAdapter (@ NonNull Annotation [] annotations ) {
751
751
this .annotations = annotations ;
752
752
}
753
753
754
+ private static AnnotatedElementAdapter from (@ Nullable Annotation [] annotations ) {
755
+ if (annotations == null || annotations .length == 0 ) {
756
+ return EMPTY ;
757
+ }
758
+ return new AnnotatedElementAdapter (annotations );
759
+ }
760
+
754
761
@ Override
755
762
public boolean isAnnotationPresent (Class <? extends Annotation > annotationClass ) {
756
763
for (Annotation annotation : getAnnotations ()) {
@@ -775,7 +782,7 @@ public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
775
782
776
783
@ Override
777
784
public Annotation [] getAnnotations () {
778
- return ( this .annotations != null ? this .annotations .clone () : EMPTY_ANNOTATION_ARRAY );
785
+ return isEmpty () ? this .annotations : this .annotations .clone ();
779
786
}
780
787
781
788
@ Override
@@ -784,7 +791,7 @@ public Annotation[] getDeclaredAnnotations() {
784
791
}
785
792
786
793
public boolean isEmpty () {
787
- return ObjectUtils . isEmpty ( this .annotations ) ;
794
+ return this .annotations . length == 0 ;
788
795
}
789
796
790
797
@ Override
@@ -800,7 +807,7 @@ public int hashCode() {
800
807
801
808
@ Override
802
809
public String toString () {
803
- return TypeDescriptor . this . toString () ;
810
+ return "{AnnotatedElementAdapter annotations=" + Arrays . toString (this . annotations ) + "}" ;
804
811
}
805
812
}
806
813
0 commit comments