Skip to content

Commit d607f89

Browse files
committed
Minor changes.
1 parent 5000415 commit d607f89

File tree

1 file changed

+21
-59
lines changed

1 file changed

+21
-59
lines changed

src/main/java/org/apache/ibatis/reflection/Reflector.java

Lines changed: 21 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,8 @@
1515
*/
1616
package org.apache.ibatis.reflection;
1717

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.*;
3320
import java.util.Map.Entry;
3421

3522
import org.apache.ibatis.reflection.invoker.GetFieldInvoker;
@@ -63,8 +50,8 @@ public Reflector(Class<?> clazz) {
6350
addGetMethods(clazz);
6451
addSetMethods(clazz);
6552
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]);
6855
for (String propName : readablePropertyNames) {
6956
caseInsensitivePropertyMap.put(propName.toUpperCase(Locale.ENGLISH), propName);
7057
}
@@ -74,28 +61,16 @@ public Reflector(Class<?> clazz) {
7461
}
7562

7663
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);
8367
}
8468

85-
private void addGetMethods(Class<?> cls) {
69+
private void addGetMethods(Class<?> clazz) {
8670
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));
9974
resolveGetterConflicts(conflictingGetters);
10075
}
10176

@@ -142,18 +117,11 @@ private void addGetMethod(String name, Method method) {
142117
}
143118
}
144119

145-
private void addSetMethods(Class<?> cls) {
120+
private void addSetMethods(Class<?> clazz) {
146121
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));
157125
resolveSetterConflicts(conflictingSetters);
158126
}
159127

@@ -169,8 +137,7 @@ private void resolveSetterConflicts(Map<String, List<Method>> conflictingSetters
169137
Method match = null;
170138
ReflectionException exception = null;
171139
for (Method setter : setters) {
172-
Class<?> paramType = setter.getParameterTypes()[0];
173-
if (paramType.equals(getterType)) {
140+
if (setter.getParameterTypes()[0].equals(getterType)) {
174141
// should be the best match
175142
match = setter;
176143
break;
@@ -285,12 +252,12 @@ private boolean isValidPropertyName(String name) {
285252
* We use this method, instead of the simpler <code>Class.getMethods()</code>,
286253
* because we want to look for private methods as well.
287254
*
288-
* @param cls The class
255+
* @param clazz The class
289256
* @return An array containing all methods in this class
290257
*/
291-
private Method[] getClassMethods(Class<?> cls) {
258+
private Method[] getClassMethods(Class<?> clazz) {
292259
Map<String, Method> uniqueMethods = new HashMap<>();
293-
Class<?> currentClass = cls;
260+
Class<?> currentClass = clazz;
294261
while (currentClass != null && currentClass != Object.class) {
295262
addUniqueMethods(uniqueMethods, currentClass.getDeclaredMethods());
296263

@@ -306,7 +273,7 @@ private Method[] getClassMethods(Class<?> cls) {
306273

307274
Collection<Method> methods = uniqueMethods.values();
308275

309-
return methods.toArray(new Method[methods.size()]);
276+
return methods.toArray(new Method[0]);
310277
}
311278

312279
private void addUniqueMethods(Map<String, Method> uniqueMethods, Method[] methods) {
@@ -332,12 +299,7 @@ private String getSignature(Method method) {
332299
sb.append(method.getName());
333300
Class<?>[] parameters = method.getParameterTypes();
334301
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());
341303
}
342304
return sb.toString();
343305
}

0 commit comments

Comments
 (0)