15
15
*/
16
16
package org .apache .ibatis .reflection ;
17
17
18
- import java .lang .reflect .Array ;
19
- import java .lang .reflect .Constructor ;
20
- import java .lang .reflect .Field ;
21
- import java .lang .reflect .GenericArrayType ;
22
- import java .lang .reflect .Method ;
23
- import java .lang .reflect .Modifier ;
24
- import java .lang .reflect .ParameterizedType ;
25
- import java .lang .reflect .ReflectPermission ;
26
- import java .lang .reflect .Type ;
27
- import java .util .ArrayList ;
28
- import java .util .Collection ;
29
- import java .util .HashMap ;
30
- import java .util .List ;
31
- import java .util .Locale ;
32
- import java .util .Map ;
18
+ import java .lang .reflect .*;
19
+ import java .util .*;
33
20
import java .util .Map .Entry ;
34
21
35
22
import org .apache .ibatis .reflection .invoker .GetFieldInvoker ;
@@ -63,8 +50,8 @@ public Reflector(Class<?> clazz) {
63
50
addGetMethods (clazz );
64
51
addSetMethods (clazz );
65
52
addFields (clazz );
66
- readablePropertyNames = getMethods .keySet ().toArray (new String [getMethods . keySet (). size () ]);
67
- writablePropertyNames = setMethods .keySet ().toArray (new String [setMethods . keySet (). size () ]);
53
+ readablePropertyNames = getMethods .keySet ().toArray (new String [0 ]);
54
+ writablePropertyNames = setMethods .keySet ().toArray (new String [0 ]);
68
55
for (String propName : readablePropertyNames ) {
69
56
caseInsensitivePropertyMap .put (propName .toUpperCase (Locale .ENGLISH ), propName );
70
57
}
@@ -74,28 +61,16 @@ public Reflector(Class<?> clazz) {
74
61
}
75
62
76
63
private void addDefaultConstructor (Class <?> clazz ) {
77
- Constructor <?>[] consts = clazz .getDeclaredConstructors ();
78
- for (Constructor <?> constructor : consts ) {
79
- if (constructor .getParameterTypes ().length == 0 ) {
80
- this .defaultConstructor = constructor ;
81
- }
82
- }
64
+ Constructor <?>[] constructors = clazz .getDeclaredConstructors ();
65
+ Arrays .stream (constructors ).filter (constructor -> constructor .getParameterTypes ().length == 0 )
66
+ .findFirst ().ifPresent (constructor -> this .defaultConstructor = constructor );
83
67
}
84
68
85
- private void addGetMethods (Class <?> cls ) {
69
+ private void addGetMethods (Class <?> clazz ) {
86
70
Map <String , List <Method >> conflictingGetters = new HashMap <>();
87
- Method [] methods = getClassMethods (cls );
88
- for (Method method : methods ) {
89
- if (method .getParameterTypes ().length > 0 ) {
90
- continue ;
91
- }
92
- String name = method .getName ();
93
- if ((name .startsWith ("get" ) && name .length () > 3 )
94
- || (name .startsWith ("is" ) && name .length () > 2 )) {
95
- name = PropertyNamer .methodToProperty (name );
96
- addMethodConflict (conflictingGetters , name , method );
97
- }
98
- }
71
+ Method [] methods = getClassMethods (clazz );
72
+ Arrays .stream (methods ).filter (m -> m .getParameterTypes ().length == 0 && PropertyNamer .isGetter (m .getName ()))
73
+ .forEach (m -> addMethodConflict (conflictingGetters , PropertyNamer .methodToProperty (m .getName ()), m ));
99
74
resolveGetterConflicts (conflictingGetters );
100
75
}
101
76
@@ -142,18 +117,11 @@ private void addGetMethod(String name, Method method) {
142
117
}
143
118
}
144
119
145
- private void addSetMethods (Class <?> cls ) {
120
+ private void addSetMethods (Class <?> clazz ) {
146
121
Map <String , List <Method >> conflictingSetters = new HashMap <>();
147
- Method [] methods = getClassMethods (cls );
148
- for (Method method : methods ) {
149
- String name = method .getName ();
150
- if (name .startsWith ("set" ) && name .length () > 3 ) {
151
- if (method .getParameterTypes ().length == 1 ) {
152
- name = PropertyNamer .methodToProperty (name );
153
- addMethodConflict (conflictingSetters , name , method );
154
- }
155
- }
156
- }
122
+ Method [] methods = getClassMethods (clazz );
123
+ Arrays .stream (methods ).filter (m -> m .getParameterTypes ().length == 1 && PropertyNamer .isSetter (m .getName ()))
124
+ .forEach (m -> addMethodConflict (conflictingSetters , PropertyNamer .methodToProperty (m .getName ()), m ));
157
125
resolveSetterConflicts (conflictingSetters );
158
126
}
159
127
@@ -169,8 +137,7 @@ private void resolveSetterConflicts(Map<String, List<Method>> conflictingSetters
169
137
Method match = null ;
170
138
ReflectionException exception = null ;
171
139
for (Method setter : setters ) {
172
- Class <?> paramType = setter .getParameterTypes ()[0 ];
173
- if (paramType .equals (getterType )) {
140
+ if (setter .getParameterTypes ()[0 ].equals (getterType )) {
174
141
// should be the best match
175
142
match = setter ;
176
143
break ;
@@ -285,12 +252,12 @@ private boolean isValidPropertyName(String name) {
285
252
* We use this method, instead of the simpler <code>Class.getMethods()</code>,
286
253
* because we want to look for private methods as well.
287
254
*
288
- * @param cls The class
255
+ * @param clazz The class
289
256
* @return An array containing all methods in this class
290
257
*/
291
- private Method [] getClassMethods (Class <?> cls ) {
258
+ private Method [] getClassMethods (Class <?> clazz ) {
292
259
Map <String , Method > uniqueMethods = new HashMap <>();
293
- Class <?> currentClass = cls ;
260
+ Class <?> currentClass = clazz ;
294
261
while (currentClass != null && currentClass != Object .class ) {
295
262
addUniqueMethods (uniqueMethods , currentClass .getDeclaredMethods ());
296
263
@@ -306,7 +273,7 @@ private Method[] getClassMethods(Class<?> cls) {
306
273
307
274
Collection <Method > methods = uniqueMethods .values ();
308
275
309
- return methods .toArray (new Method [methods . size () ]);
276
+ return methods .toArray (new Method [0 ]);
310
277
}
311
278
312
279
private void addUniqueMethods (Map <String , Method > uniqueMethods , Method [] methods ) {
@@ -332,12 +299,7 @@ private String getSignature(Method method) {
332
299
sb .append (method .getName ());
333
300
Class <?>[] parameters = method .getParameterTypes ();
334
301
for (int i = 0 ; i < parameters .length ; i ++) {
335
- if (i == 0 ) {
336
- sb .append (':' );
337
- } else {
338
- sb .append (',' );
339
- }
340
- sb .append (parameters [i ].getName ());
302
+ sb .append (i == 0 ? ":" : "," ).append (parameters [i ].getName ());
341
303
}
342
304
return sb .toString ();
343
305
}
0 commit comments