Skip to content

Commit f6d8578

Browse files
authored
Merge pull request #1589 from peterchenhdu/master
Minor optimization.
2 parents 3becd0a + c9d829f commit f6d8578

File tree

3 files changed

+24
-49
lines changed

3 files changed

+24
-49
lines changed

src/main/java/org/apache/ibatis/parsing/GenericTokenParser.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ public String parse(String text) {
6666
end = text.indexOf(closeToken, offset);
6767
} else {
6868
expression.append(src, offset, end - offset);
69-
offset = end + closeToken.length();
7069
break;
7170
}
7271
}

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

Lines changed: 20 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.lang.reflect.ReflectPermission;
2626
import java.lang.reflect.Type;
2727
import java.util.ArrayList;
28+
import java.util.Arrays;
2829
import java.util.Collection;
2930
import java.util.HashMap;
3031
import java.util.List;
@@ -63,8 +64,8 @@ public Reflector(Class<?> clazz) {
6364
addGetMethods(clazz);
6465
addSetMethods(clazz);
6566
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]);
6869
for (String propName : readablePropertyNames) {
6970
caseInsensitivePropertyMap.put(propName.toUpperCase(Locale.ENGLISH), propName);
7071
}
@@ -74,28 +75,16 @@ public Reflector(Class<?> clazz) {
7475
}
7576

7677
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);
8381
}
8482

85-
private void addGetMethods(Class<?> cls) {
83+
private void addGetMethods(Class<?> clazz) {
8684
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));
9988
resolveGetterConflicts(conflictingGetters);
10089
}
10190

@@ -142,18 +131,11 @@ private void addGetMethod(String name, Method method) {
142131
}
143132
}
144133

145-
private void addSetMethods(Class<?> cls) {
134+
private void addSetMethods(Class<?> clazz) {
146135
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));
157139
resolveSetterConflicts(conflictingSetters);
158140
}
159141

@@ -169,8 +151,7 @@ private void resolveSetterConflicts(Map<String, List<Method>> conflictingSetters
169151
Method match = null;
170152
ReflectionException exception = null;
171153
for (Method setter : setters) {
172-
Class<?> paramType = setter.getParameterTypes()[0];
173-
if (paramType.equals(getterType)) {
154+
if (setter.getParameterTypes()[0].equals(getterType)) {
174155
// should be the best match
175156
match = setter;
176157
break;
@@ -285,12 +266,12 @@ private boolean isValidPropertyName(String name) {
285266
* We use this method, instead of the simpler <code>Class.getMethods()</code>,
286267
* because we want to look for private methods as well.
287268
*
288-
* @param cls The class
269+
* @param clazz The class
289270
* @return An array containing all methods in this class
290271
*/
291-
private Method[] getClassMethods(Class<?> cls) {
272+
private Method[] getClassMethods(Class<?> clazz) {
292273
Map<String, Method> uniqueMethods = new HashMap<>();
293-
Class<?> currentClass = cls;
274+
Class<?> currentClass = clazz;
294275
while (currentClass != null && currentClass != Object.class) {
295276
addUniqueMethods(uniqueMethods, currentClass.getDeclaredMethods());
296277

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

307288
Collection<Method> methods = uniqueMethods.values();
308289

309-
return methods.toArray(new Method[methods.size()]);
290+
return methods.toArray(new Method[0]);
310291
}
311292

312293
private void addUniqueMethods(Map<String, Method> uniqueMethods, Method[] methods) {
@@ -332,12 +313,7 @@ private String getSignature(Method method) {
332313
sb.append(method.getName());
333314
Class<?>[] parameters = method.getParameterTypes();
334315
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());
341317
}
342318
return sb.toString();
343319
}

src/main/java/org/apache/ibatis/reflection/property/PropertyNamer.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2015 the original author or authors.
2+
* Copyright 2009-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -45,15 +45,15 @@ public static String methodToProperty(String name) {
4545
}
4646

4747
public static boolean isProperty(String name) {
48-
return name.startsWith("get") || name.startsWith("set") || name.startsWith("is");
48+
return isGetter(name) || isSetter(name);
4949
}
5050

5151
public static boolean isGetter(String name) {
52-
return name.startsWith("get") || name.startsWith("is");
52+
return (name.startsWith("get") && name.length() > 3) || (name.startsWith("is") && name.length() > 2);
5353
}
5454

5555
public static boolean isSetter(String name) {
56-
return name.startsWith("set");
56+
return name.startsWith("set") && name.length() > 3;
5757
}
5858

5959
}

0 commit comments

Comments
 (0)