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
@@ -547,12 +545,16 @@ public int hashCode() {
547
545
public String toString () {
548
546
StringBuilder builder = new StringBuilder ();
549
547
for (Annotation ann : getAnnotations ()) {
550
- builder .append ('@' ).append (ann .annotationType (). getName ( )).append (' ' );
548
+ builder .append ('@' ).append (getName ( ann .annotationType ())).append (' ' );
551
549
}
552
550
builder .append (getResolvableType ());
553
551
return builder .toString ();
554
552
}
555
553
554
+ private static String getName (Class <?> clazz ) {
555
+ String canonicalName = clazz .getCanonicalName ();
556
+ return (canonicalName != null ? canonicalName : clazz .getName ());
557
+ }
556
558
557
559
/**
558
560
* Create a new type descriptor for an object.
@@ -742,15 +744,23 @@ public static TypeDescriptor nested(Property property, int nestingLevel) {
742
744
* @see AnnotatedElementUtils#isAnnotated(AnnotatedElement, Class)
743
745
* @see AnnotatedElementUtils#getMergedAnnotation(AnnotatedElement, Class)
744
746
*/
745
- private class AnnotatedElementAdapter implements AnnotatedElement , Serializable {
747
+ private static final class AnnotatedElementAdapter implements AnnotatedElement , Serializable {
748
+
749
+ private static final AnnotatedElementAdapter EMPTY = new AnnotatedElementAdapter (new Annotation [0 ]);
746
750
747
- @ Nullable
748
751
private final Annotation [] annotations ;
749
752
750
- public AnnotatedElementAdapter (@ Nullable Annotation [] annotations ) {
753
+ private AnnotatedElementAdapter (Annotation [] annotations ) {
751
754
this .annotations = annotations ;
752
755
}
753
756
757
+ private static AnnotatedElementAdapter from (@ Nullable Annotation [] annotations ) {
758
+ if (annotations == null || annotations .length == 0 ) {
759
+ return EMPTY ;
760
+ }
761
+ return new AnnotatedElementAdapter (annotations );
762
+ }
763
+
754
764
@ Override
755
765
public boolean isAnnotationPresent (Class <? extends Annotation > annotationClass ) {
756
766
for (Annotation annotation : getAnnotations ()) {
@@ -775,7 +785,7 @@ public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
775
785
776
786
@ Override
777
787
public Annotation [] getAnnotations () {
778
- return (this .annotations != null ? this .annotations .clone () : EMPTY_ANNOTATION_ARRAY );
788
+ return (isEmpty () ? this .annotations : this .annotations .clone ());
779
789
}
780
790
781
791
@ Override
@@ -784,7 +794,7 @@ public Annotation[] getDeclaredAnnotations() {
784
794
}
785
795
786
796
public boolean isEmpty () {
787
- return ObjectUtils . isEmpty (this .annotations );
797
+ return (this .annotations . length == 0 );
788
798
}
789
799
790
800
@ Override
@@ -800,7 +810,7 @@ public int hashCode() {
800
810
801
811
@ Override
802
812
public String toString () {
803
- return TypeDescriptor . this . toString ();
813
+ return "AnnotatedElementAdapter annotations=" + Arrays . toString (this . annotations );
804
814
}
805
815
}
806
816
0 commit comments