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
@@ -513,12 +511,16 @@ public int hashCode() {
513
511
public String toString () {
514
512
StringBuilder builder = new StringBuilder ();
515
513
for (Annotation ann : getAnnotations ()) {
516
- builder .append ('@' ).append (ann .annotationType (). getName ( )).append (' ' );
514
+ builder .append ('@' ).append (getName ( ann .annotationType ())).append (' ' );
517
515
}
518
516
builder .append (getResolvableType ());
519
517
return builder .toString ();
520
518
}
521
519
520
+ private static String getName (Class <?> clazz ) {
521
+ String canonicalName = clazz .getCanonicalName ();
522
+ return (canonicalName != null ? canonicalName : clazz .getName ());
523
+ }
522
524
523
525
/**
524
526
* Create a new type descriptor for an object.
@@ -734,15 +736,23 @@ private static TypeDescriptor getRelatedIfResolvable(TypeDescriptor source, Reso
734
736
* @see AnnotatedElementUtils#isAnnotated(AnnotatedElement, Class)
735
737
* @see AnnotatedElementUtils#getMergedAnnotation(AnnotatedElement, Class)
736
738
*/
737
- private class AnnotatedElementAdapter implements AnnotatedElement , Serializable {
739
+ private static final class AnnotatedElementAdapter implements AnnotatedElement , Serializable {
740
+
741
+ private static final AnnotatedElementAdapter EMPTY = new AnnotatedElementAdapter (new Annotation [0 ]);
738
742
739
- @ Nullable
740
743
private final Annotation [] annotations ;
741
744
742
- public AnnotatedElementAdapter (@ Nullable Annotation [] annotations ) {
745
+ private AnnotatedElementAdapter (Annotation [] annotations ) {
743
746
this .annotations = annotations ;
744
747
}
745
748
749
+ private static AnnotatedElementAdapter from (@ Nullable Annotation [] annotations ) {
750
+ if (annotations == null || annotations .length == 0 ) {
751
+ return EMPTY ;
752
+ }
753
+ return new AnnotatedElementAdapter (annotations );
754
+ }
755
+
746
756
@ Override
747
757
public boolean isAnnotationPresent (Class <? extends Annotation > annotationClass ) {
748
758
for (Annotation annotation : getAnnotations ()) {
@@ -767,7 +777,7 @@ public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
767
777
768
778
@ Override
769
779
public Annotation [] getAnnotations () {
770
- return (this .annotations != null ? this .annotations .clone () : EMPTY_ANNOTATION_ARRAY );
780
+ return (isEmpty () ? this .annotations : this .annotations .clone ());
771
781
}
772
782
773
783
@ Override
@@ -776,7 +786,7 @@ public Annotation[] getDeclaredAnnotations() {
776
786
}
777
787
778
788
public boolean isEmpty () {
779
- return ObjectUtils . isEmpty (this .annotations );
789
+ return (this .annotations . length == 0 );
780
790
}
781
791
782
792
@ Override
@@ -792,7 +802,7 @@ public int hashCode() {
792
802
793
803
@ Override
794
804
public String toString () {
795
- return TypeDescriptor . this . toString ();
805
+ return "AnnotatedElementAdapter annotations=" + Arrays . toString (this . annotations );
796
806
}
797
807
}
798
808
0 commit comments