Skip to content

Commit 60ee625

Browse files
committed
Add an attribute 'allMethods' to cache all methods,reduce repetitive operations
1 parent 5e89813 commit 60ee625

File tree

1 file changed

+11
-28
lines changed

1 file changed

+11
-28
lines changed

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

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,15 @@
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.text.MessageFormat;
28-
import java.util.ArrayList;
29-
import java.util.Arrays;
30-
import java.util.Collection;
31-
import java.util.HashMap;
32-
import java.util.List;
33-
import java.util.Locale;
34-
import java.util.Map;
35-
import java.util.Map.Entry;
36-
37-
import org.apache.ibatis.reflection.invoker.AmbiguousMethodInvoker;
38-
import org.apache.ibatis.reflection.invoker.GetFieldInvoker;
39-
import org.apache.ibatis.reflection.invoker.Invoker;
40-
import org.apache.ibatis.reflection.invoker.MethodInvoker;
41-
import org.apache.ibatis.reflection.invoker.SetFieldInvoker;
18+
import org.apache.ibatis.reflection.invoker.*;
4219
import org.apache.ibatis.reflection.property.PropertyNamer;
4320
import org.apache.ibatis.util.MapUtil;
4421

22+
import java.lang.reflect.*;
23+
import java.text.MessageFormat;
24+
import java.util.*;
25+
import java.util.Map.Entry;
26+
4527
/**
4628
* This class represents a cached set of class definition information that
4729
* allows for easy mapping between property names and getter/setter methods.
@@ -60,6 +42,7 @@ public class Reflector {
6042
private Constructor<?> defaultConstructor;
6143

6244
private Map<String, String> caseInsensitivePropertyMap = new HashMap<>();
45+
private Method[] allMethods;
6346

6447
public Reflector(Class<?> clazz) {
6548
type = clazz;
@@ -85,7 +68,7 @@ private void addDefaultConstructor(Class<?> clazz) {
8568

8669
private void addGetMethods(Class<?> clazz) {
8770
Map<String, List<Method>> conflictingGetters = new HashMap<>();
88-
Method[] methods = getClassMethods(clazz);
71+
Method[] methods = allMethods==null?getClassMethods(clazz):allMethods;
8972
Arrays.stream(methods).filter(m -> m.getParameterTypes().length == 0 && PropertyNamer.isGetter(m.getName()))
9073
.forEach(m -> addMethodConflict(conflictingGetters, PropertyNamer.methodToProperty(m.getName()), m));
9174
resolveGetterConflicts(conflictingGetters);
@@ -136,7 +119,7 @@ private void addGetMethod(String name, Method method, boolean isAmbiguous) {
136119

137120
private void addSetMethods(Class<?> clazz) {
138121
Map<String, List<Method>> conflictingSetters = new HashMap<>();
139-
Method[] methods = getClassMethods(clazz);
122+
Method[] methods = allMethods==null?getClassMethods(clazz):allMethods;
140123
Arrays.stream(methods).filter(m -> m.getParameterTypes().length == 1 && PropertyNamer.isSetter(m.getName()))
141124
.forEach(m -> addMethodConflict(conflictingSetters, PropertyNamer.methodToProperty(m.getName()), m));
142125
resolveSetterConflicts(conflictingSetters);
@@ -290,8 +273,8 @@ private Method[] getClassMethods(Class<?> clazz) {
290273
}
291274

292275
Collection<Method> methods = uniqueMethods.values();
293-
294-
return methods.toArray(new Method[0]);
276+
allMethods = methods.toArray(new Method[0]);
277+
return allMethods;
295278
}
296279

297280
private void addUniqueMethods(Map<String, Method> uniqueMethods, Method[] methods) {

0 commit comments

Comments
 (0)