7
7
import java .lang .reflect .Field ;
8
8
import java .util .ArrayList ;
9
9
import java .util .Arrays ;
10
- import java .util .Collections ;
11
10
import java .util .HashSet ;
12
11
import java .util .List ;
13
12
import java .util .Map ;
17
16
import java .util .OptionalInt ;
18
17
import java .util .OptionalLong ;
19
18
import java .util .Set ;
19
+ import java .util .function .Predicate ;
20
20
import java .util .stream .Stream ;
21
21
22
22
import io .swagger .v3 .oas .annotations .Parameter ;
@@ -37,19 +37,16 @@ private static Stream<MethodParameter> extractFrom(Class<?> clazz, String fieldN
37
37
}
38
38
39
39
private static Stream <MethodParameter > fromGetterOfField (Class <?> paramClass , Field field , String fieldNamePrefix ) {
40
- if (isSimpleType (field .getType ())) {
40
+ if (isSimpleType (field .getType ()))
41
41
return fromSimpleClass (paramClass , field , fieldNamePrefix );
42
- }
43
- else {
42
+ else
44
43
return extractFrom (field .getType (), fieldNamePrefix + field .getName () + "." );
45
- }
46
44
}
47
45
48
46
private static Stream <MethodParameter > fromSimpleClass (Class <?> paramClass , Field field , String fieldNamePrefix ) {
49
47
Annotation [] fieldAnnotations = field .getDeclaredAnnotations ();
50
- if (isOptional (field )) {
48
+ if (isOptional (field ))
51
49
fieldAnnotations = ArrayUtils .add (fieldAnnotations , NULLABLE_ANNOTATION );
52
- }
53
50
try {
54
51
Annotation [] finalFieldAnnotations = fieldAnnotations ;
55
52
return Stream .of (Introspector .getBeanInfo (paramClass ).getPropertyDescriptors ())
@@ -79,10 +76,8 @@ private static List<Field> allFieldsOf(Class<?> clazz) {
79
76
}
80
77
81
78
private static boolean isSimpleType (Class <?> clazz ) {
82
- if (clazz .isPrimitive ()) return true ;
83
- if (clazz .isArray ()) return true ;
84
- if (clazz .isEnum ()) return true ;
85
- return SIMPLE_TYPES .stream ().anyMatch (c -> c .isAssignableFrom (clazz ));
79
+ return SIMPLE_TYPE_PREDICATES .stream ().anyMatch (p -> p .test (clazz )) ||
80
+ SIMPLE_TYPES .stream ().anyMatch (c -> c .isAssignableFrom (clazz ));
86
81
}
87
82
88
83
private static final Nullable NULLABLE_ANNOTATION = new Nullable () {
@@ -92,22 +87,37 @@ public Class<? extends Annotation> annotationType() {
92
87
}
93
88
};
94
89
95
- private static final Set <Class <?>> SIMPLE_TYPES ;
90
+ private static final List <Predicate <Class <?>>> SIMPLE_TYPE_PREDICATES = new ArrayList <>();
91
+
92
+ private static final Set <Class <?>> SIMPLE_TYPES = new HashSet <>();
93
+
94
+ static void addSimpleTypePredicate (Predicate <Class <?>> predicate ) {
95
+ SIMPLE_TYPE_PREDICATES .add (predicate );
96
+ }
97
+
98
+ static void addSimpleTypes (Class <?>... classes ) {
99
+ SIMPLE_TYPES .addAll (Arrays .asList (classes ));
100
+ }
101
+
102
+ static void removeSimpleTypes (Class <?>... classes ) {
103
+ SIMPLE_TYPES .removeAll (Arrays .asList (classes ));
104
+ }
96
105
97
106
static {
98
- Set <Class <?>> simpleTypes = new HashSet <>();
99
- simpleTypes .add (Boolean .class );
100
- simpleTypes .add (Character .class );
101
- simpleTypes .add (Number .class );
102
- simpleTypes .add (CharSequence .class );
103
- simpleTypes .add (Optional .class );
104
- simpleTypes .add (OptionalInt .class );
105
- simpleTypes .add (OptionalLong .class );
106
- simpleTypes .add (OptionalDouble .class );
107
-
108
- simpleTypes .add (Map .class );
109
- simpleTypes .add (Iterable .class );
110
-
111
- SIMPLE_TYPES = Collections .unmodifiableSet (simpleTypes );
107
+ SIMPLE_TYPES .add (Boolean .class );
108
+ SIMPLE_TYPES .add (Character .class );
109
+ SIMPLE_TYPES .add (Number .class );
110
+ SIMPLE_TYPES .add (CharSequence .class );
111
+ SIMPLE_TYPES .add (Optional .class );
112
+ SIMPLE_TYPES .add (OptionalInt .class );
113
+ SIMPLE_TYPES .add (OptionalLong .class );
114
+ SIMPLE_TYPES .add (OptionalDouble .class );
115
+
116
+ SIMPLE_TYPES .add (Map .class );
117
+ SIMPLE_TYPES .add (Iterable .class );
118
+
119
+ SIMPLE_TYPE_PREDICATES .add (Class ::isPrimitive );
120
+ SIMPLE_TYPE_PREDICATES .add (Class ::isEnum );
121
+ SIMPLE_TYPE_PREDICATES .add (Class ::isArray );
112
122
}
113
123
}
0 commit comments