25
25
import java .lang .reflect .ReflectPermission ;
26
26
import java .lang .reflect .Type ;
27
27
import java .util .ArrayList ;
28
+ import java .util .Arrays ;
28
29
import java .util .Collection ;
29
30
import java .util .HashMap ;
30
31
import java .util .List ;
@@ -63,8 +64,8 @@ public Reflector(Class<?> clazz) {
63
64
addGetMethods (clazz );
64
65
addSetMethods (clazz );
65
66
addFields (clazz );
66
- readablePropertyNames = getMethods .keySet ().toArray (new String [getMethods . keySet (). size () ]);
67
- writablePropertyNames = setMethods .keySet ().toArray (new String [setMethods . keySet (). size () ]);
67
+ readablePropertyNames = getMethods .keySet ().toArray (new String [0 ]);
68
+ writablePropertyNames = setMethods .keySet ().toArray (new String [0 ]);
68
69
for (String propName : readablePropertyNames ) {
69
70
caseInsensitivePropertyMap .put (propName .toUpperCase (Locale .ENGLISH ), propName );
70
71
}
@@ -74,28 +75,16 @@ public Reflector(Class<?> clazz) {
74
75
}
75
76
76
77
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
- }
78
+ Constructor <?>[] constructors = clazz .getDeclaredConstructors ();
79
+ Arrays .stream (constructors ).filter (constructor -> constructor .getParameterTypes ().length == 0 )
80
+ .findAny ().ifPresent (constructor -> this .defaultConstructor = constructor );
83
81
}
84
82
85
- private void addGetMethods (Class <?> cls ) {
83
+ private void addGetMethods (Class <?> clazz ) {
86
84
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
- }
85
+ Method [] methods = getClassMethods (clazz );
86
+ Arrays .stream (methods ).filter (m -> m .getParameterTypes ().length == 0 && PropertyNamer .isGetter (m .getName ()))
87
+ .forEach (m -> addMethodConflict (conflictingGetters , PropertyNamer .methodToProperty (m .getName ()), m ));
99
88
resolveGetterConflicts (conflictingGetters );
100
89
}
101
90
@@ -142,18 +131,11 @@ private void addGetMethod(String name, Method method) {
142
131
}
143
132
}
144
133
145
- private void addSetMethods (Class <?> cls ) {
134
+ private void addSetMethods (Class <?> clazz ) {
146
135
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
- }
136
+ Method [] methods = getClassMethods (clazz );
137
+ Arrays .stream (methods ).filter (m -> m .getParameterTypes ().length == 1 && PropertyNamer .isSetter (m .getName ()))
138
+ .forEach (m -> addMethodConflict (conflictingSetters , PropertyNamer .methodToProperty (m .getName ()), m ));
157
139
resolveSetterConflicts (conflictingSetters );
158
140
}
159
141
@@ -169,8 +151,7 @@ private void resolveSetterConflicts(Map<String, List<Method>> conflictingSetters
169
151
Method match = null ;
170
152
ReflectionException exception = null ;
171
153
for (Method setter : setters ) {
172
- Class <?> paramType = setter .getParameterTypes ()[0 ];
173
- if (paramType .equals (getterType )) {
154
+ if (setter .getParameterTypes ()[0 ].equals (getterType )) {
174
155
// should be the best match
175
156
match = setter ;
176
157
break ;
@@ -285,12 +266,12 @@ private boolean isValidPropertyName(String name) {
285
266
* We use this method, instead of the simpler <code>Class.getMethods()</code>,
286
267
* because we want to look for private methods as well.
287
268
*
288
- * @param cls The class
269
+ * @param clazz The class
289
270
* @return An array containing all methods in this class
290
271
*/
291
- private Method [] getClassMethods (Class <?> cls ) {
272
+ private Method [] getClassMethods (Class <?> clazz ) {
292
273
Map <String , Method > uniqueMethods = new HashMap <>();
293
- Class <?> currentClass = cls ;
274
+ Class <?> currentClass = clazz ;
294
275
while (currentClass != null && currentClass != Object .class ) {
295
276
addUniqueMethods (uniqueMethods , currentClass .getDeclaredMethods ());
296
277
@@ -306,7 +287,7 @@ private Method[] getClassMethods(Class<?> cls) {
306
287
307
288
Collection <Method > methods = uniqueMethods .values ();
308
289
309
- return methods .toArray (new Method [methods . size () ]);
290
+ return methods .toArray (new Method [0 ]);
310
291
}
311
292
312
293
private void addUniqueMethods (Map <String , Method > uniqueMethods , Method [] methods ) {
@@ -332,12 +313,7 @@ private String getSignature(Method method) {
332
313
sb .append (method .getName ());
333
314
Class <?>[] parameters = method .getParameterTypes ();
334
315
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 ());
316
+ sb .append (i == 0 ? ':' : ',' ).append (parameters [i ].getName ());
341
317
}
342
318
return sb .toString ();
343
319
}
0 commit comments