1
1
/*
2
- * Copyright 2002-2023 the original author or authors.
2
+ * Copyright 2002-2024 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
52
52
@ SuppressWarnings ("serial" )
53
53
public class TypeDescriptor implements Serializable {
54
54
55
- private static final Annotation [] EMPTY_ANNOTATION_ARRAY = new Annotation [0 ];
56
-
57
55
private static final Map <Class <?>, TypeDescriptor > commonTypesCache = new HashMap <>(32 );
58
56
59
57
private static final Class <?>[] CACHED_COMMON_TYPES = {
@@ -84,7 +82,7 @@ public class TypeDescriptor implements Serializable {
84
82
public TypeDescriptor (MethodParameter methodParameter ) {
85
83
this .resolvableType = ResolvableType .forMethodParameter (methodParameter );
86
84
this .type = this .resolvableType .resolve (methodParameter .getNestedParameterType ());
87
- this .annotatedElement = new AnnotatedElementAdapter (methodParameter .getParameterIndex () == -1 ?
85
+ this .annotatedElement = AnnotatedElementAdapter . from (methodParameter .getParameterIndex () == -1 ?
88
86
methodParameter .getMethodAnnotations () : methodParameter .getParameterAnnotations ());
89
87
}
90
88
@@ -96,7 +94,7 @@ public TypeDescriptor(MethodParameter methodParameter) {
96
94
public TypeDescriptor (Field field ) {
97
95
this .resolvableType = ResolvableType .forField (field );
98
96
this .type = this .resolvableType .resolve (field .getType ());
99
- this .annotatedElement = new AnnotatedElementAdapter (field .getAnnotations ());
97
+ this .annotatedElement = AnnotatedElementAdapter . from (field .getAnnotations ());
100
98
}
101
99
102
100
/**
@@ -109,7 +107,7 @@ public TypeDescriptor(Property property) {
109
107
Assert .notNull (property , "Property must not be null" );
110
108
this .resolvableType = ResolvableType .forMethodParameter (property .getMethodParameter ());
111
109
this .type = this .resolvableType .resolve (property .getType ());
112
- this .annotatedElement = new AnnotatedElementAdapter (property .getAnnotations ());
110
+ this .annotatedElement = AnnotatedElementAdapter . from (property .getAnnotations ());
113
111
}
114
112
115
113
/**
@@ -125,7 +123,7 @@ public TypeDescriptor(Property property) {
125
123
public TypeDescriptor (ResolvableType resolvableType , @ Nullable Class <?> type , @ Nullable Annotation [] annotations ) {
126
124
this .resolvableType = resolvableType ;
127
125
this .type = (type != null ? type : resolvableType .toClass ());
128
- this .annotatedElement = new AnnotatedElementAdapter (annotations );
126
+ this .annotatedElement = AnnotatedElementAdapter . from (annotations );
129
127
}
130
128
131
129
@@ -512,12 +510,16 @@ public int hashCode() {
512
510
public String toString () {
513
511
StringBuilder builder = new StringBuilder ();
514
512
for (Annotation ann : getAnnotations ()) {
515
- builder .append ('@' ).append (ann .annotationType (). getName ( )).append (' ' );
513
+ builder .append ('@' ).append (getName ( ann .annotationType ())).append (' ' );
516
514
}
517
515
builder .append (getResolvableType ());
518
516
return builder .toString ();
519
517
}
520
518
519
+ private static String getName (Class <?> clazz ) {
520
+ String canonicalName = clazz .getCanonicalName ();
521
+ return (canonicalName != null ? canonicalName : clazz .getName ());
522
+ }
521
523
522
524
/**
523
525
* Create a new type descriptor for an object.
@@ -733,15 +735,23 @@ private static TypeDescriptor getRelatedIfResolvable(TypeDescriptor source, Reso
733
735
* @see AnnotatedElementUtils#isAnnotated(AnnotatedElement, Class)
734
736
* @see AnnotatedElementUtils#getMergedAnnotation(AnnotatedElement, Class)
735
737
*/
736
- private class AnnotatedElementAdapter implements AnnotatedElement , Serializable {
738
+ private static final class AnnotatedElementAdapter implements AnnotatedElement , Serializable {
739
+
740
+ private static final AnnotatedElementAdapter EMPTY = new AnnotatedElementAdapter (new Annotation [0 ]);
737
741
738
- @ Nullable
739
742
private final Annotation [] annotations ;
740
743
741
- public AnnotatedElementAdapter (@ Nullable Annotation [] annotations ) {
744
+ private AnnotatedElementAdapter (Annotation [] annotations ) {
742
745
this .annotations = annotations ;
743
746
}
744
747
748
+ private static AnnotatedElementAdapter from (@ Nullable Annotation [] annotations ) {
749
+ if (annotations == null || annotations .length == 0 ) {
750
+ return EMPTY ;
751
+ }
752
+ return new AnnotatedElementAdapter (annotations );
753
+ }
754
+
745
755
@ Override
746
756
public boolean isAnnotationPresent (Class <? extends Annotation > annotationClass ) {
747
757
for (Annotation annotation : getAnnotations ()) {
@@ -766,7 +776,7 @@ public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
766
776
767
777
@ Override
768
778
public Annotation [] getAnnotations () {
769
- return (this .annotations != null ? this .annotations .clone () : EMPTY_ANNOTATION_ARRAY );
779
+ return (isEmpty () ? this .annotations : this .annotations .clone ());
770
780
}
771
781
772
782
@ Override
@@ -775,7 +785,7 @@ public Annotation[] getDeclaredAnnotations() {
775
785
}
776
786
777
787
public boolean isEmpty () {
778
- return ObjectUtils . isEmpty (this .annotations );
788
+ return (this .annotations . length == 0 );
779
789
}
780
790
781
791
@ Override
@@ -791,7 +801,7 @@ public int hashCode() {
791
801
792
802
@ Override
793
803
public String toString () {
794
- return TypeDescriptor . this . toString ();
804
+ return "AnnotatedElementAdapter annotations=" + Arrays . toString (this . annotations );
795
805
}
796
806
}
797
807
0 commit comments