From 60ee6252125ca6a217c55fea3e01161e3c34afaf Mon Sep 17 00:00:00 2001 From: mbond <1733794124@qq.com> Date: Thu, 2 Sep 2021 16:40:15 +0800 Subject: [PATCH 1/3] Add an attribute 'allMethods' to cache all methods,reduce repetitive operations --- .../apache/ibatis/reflection/Reflector.java | 39 ++++++------------- 1 file changed, 11 insertions(+), 28 deletions(-) diff --git a/src/main/java/org/apache/ibatis/reflection/Reflector.java b/src/main/java/org/apache/ibatis/reflection/Reflector.java index ee7dd1a6323..c364991ebc5 100644 --- a/src/main/java/org/apache/ibatis/reflection/Reflector.java +++ b/src/main/java/org/apache/ibatis/reflection/Reflector.java @@ -15,33 +15,15 @@ */ package org.apache.ibatis.reflection; -import java.lang.reflect.Array; -import java.lang.reflect.Constructor; -import java.lang.reflect.Field; -import java.lang.reflect.GenericArrayType; -import java.lang.reflect.Method; -import java.lang.reflect.Modifier; -import java.lang.reflect.ParameterizedType; -import java.lang.reflect.ReflectPermission; -import java.lang.reflect.Type; -import java.text.MessageFormat; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashMap; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.Map.Entry; - -import org.apache.ibatis.reflection.invoker.AmbiguousMethodInvoker; -import org.apache.ibatis.reflection.invoker.GetFieldInvoker; -import org.apache.ibatis.reflection.invoker.Invoker; -import org.apache.ibatis.reflection.invoker.MethodInvoker; -import org.apache.ibatis.reflection.invoker.SetFieldInvoker; +import org.apache.ibatis.reflection.invoker.*; import org.apache.ibatis.reflection.property.PropertyNamer; import org.apache.ibatis.util.MapUtil; +import java.lang.reflect.*; +import java.text.MessageFormat; +import java.util.*; +import java.util.Map.Entry; + /** * This class represents a cached set of class definition information that * allows for easy mapping between property names and getter/setter methods. @@ -60,6 +42,7 @@ public class Reflector { private Constructor defaultConstructor; private Map caseInsensitivePropertyMap = new HashMap<>(); + private Method[] allMethods; public Reflector(Class clazz) { type = clazz; @@ -85,7 +68,7 @@ private void addDefaultConstructor(Class clazz) { private void addGetMethods(Class clazz) { Map> conflictingGetters = new HashMap<>(); - Method[] methods = getClassMethods(clazz); + Method[] methods = allMethods==null?getClassMethods(clazz):allMethods; Arrays.stream(methods).filter(m -> m.getParameterTypes().length == 0 && PropertyNamer.isGetter(m.getName())) .forEach(m -> addMethodConflict(conflictingGetters, PropertyNamer.methodToProperty(m.getName()), m)); resolveGetterConflicts(conflictingGetters); @@ -136,7 +119,7 @@ private void addGetMethod(String name, Method method, boolean isAmbiguous) { private void addSetMethods(Class clazz) { Map> conflictingSetters = new HashMap<>(); - Method[] methods = getClassMethods(clazz); + Method[] methods = allMethods==null?getClassMethods(clazz):allMethods; Arrays.stream(methods).filter(m -> m.getParameterTypes().length == 1 && PropertyNamer.isSetter(m.getName())) .forEach(m -> addMethodConflict(conflictingSetters, PropertyNamer.methodToProperty(m.getName()), m)); resolveSetterConflicts(conflictingSetters); @@ -290,8 +273,8 @@ private Method[] getClassMethods(Class clazz) { } Collection methods = uniqueMethods.values(); - - return methods.toArray(new Method[0]); + allMethods = methods.toArray(new Method[0]); + return allMethods; } private void addUniqueMethods(Map uniqueMethods, Method[] methods) { From e2d32a9bcf9da1f275f8ec88d423f75dbfdaaa79 Mon Sep 17 00:00:00 2001 From: mbond <1733794124@qq.com> Date: Mon, 6 Sep 2021 09:49:39 +0800 Subject: [PATCH 2/3] Modify constructor ,add local variable 'allMethods' (Method[]),change method 'addGetMethods' and 'addSetMethods' input parameter,from Class to Method[] --- .../apache/ibatis/reflection/Reflector.java | 39 +++++++++++++------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/apache/ibatis/reflection/Reflector.java b/src/main/java/org/apache/ibatis/reflection/Reflector.java index c364991ebc5..72db540f3a9 100644 --- a/src/main/java/org/apache/ibatis/reflection/Reflector.java +++ b/src/main/java/org/apache/ibatis/reflection/Reflector.java @@ -15,13 +15,31 @@ */ package org.apache.ibatis.reflection; -import org.apache.ibatis.reflection.invoker.*; +import org.apache.ibatis.reflection.invoker.AmbiguousMethodInvoker; +import org.apache.ibatis.reflection.invoker.GetFieldInvoker; +import org.apache.ibatis.reflection.invoker.Invoker; +import org.apache.ibatis.reflection.invoker.MethodInvoker; +import org.apache.ibatis.reflection.invoker.SetFieldInvoker; import org.apache.ibatis.reflection.property.PropertyNamer; import org.apache.ibatis.util.MapUtil; -import java.lang.reflect.*; +import java.lang.reflect.Array; +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.GenericArrayType; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.ReflectPermission; +import java.lang.reflect.Type; import java.text.MessageFormat; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; import java.util.Map.Entry; /** @@ -42,13 +60,13 @@ public class Reflector { private Constructor defaultConstructor; private Map caseInsensitivePropertyMap = new HashMap<>(); - private Method[] allMethods; public Reflector(Class clazz) { type = clazz; addDefaultConstructor(clazz); - addGetMethods(clazz); - addSetMethods(clazz); + Method [] allMethods = getClassMethods(clazz); + addGetMethods(allMethods); + addSetMethods(allMethods); addFields(clazz); readablePropertyNames = getMethods.keySet().toArray(new String[0]); writablePropertyNames = setMethods.keySet().toArray(new String[0]); @@ -66,9 +84,8 @@ private void addDefaultConstructor(Class clazz) { .findAny().ifPresent(constructor -> this.defaultConstructor = constructor); } - private void addGetMethods(Class clazz) { + private void addGetMethods(Method[] methods) { Map> conflictingGetters = new HashMap<>(); - Method[] methods = allMethods==null?getClassMethods(clazz):allMethods; Arrays.stream(methods).filter(m -> m.getParameterTypes().length == 0 && PropertyNamer.isGetter(m.getName())) .forEach(m -> addMethodConflict(conflictingGetters, PropertyNamer.methodToProperty(m.getName()), m)); resolveGetterConflicts(conflictingGetters); @@ -117,9 +134,8 @@ private void addGetMethod(String name, Method method, boolean isAmbiguous) { getTypes.put(name, typeToClass(returnType)); } - private void addSetMethods(Class clazz) { + private void addSetMethods(Method[] methods) { Map> conflictingSetters = new HashMap<>(); - Method[] methods = allMethods==null?getClassMethods(clazz):allMethods; Arrays.stream(methods).filter(m -> m.getParameterTypes().length == 1 && PropertyNamer.isSetter(m.getName())) .forEach(m -> addMethodConflict(conflictingSetters, PropertyNamer.methodToProperty(m.getName()), m)); resolveSetterConflicts(conflictingSetters); @@ -273,8 +289,7 @@ private Method[] getClassMethods(Class clazz) { } Collection methods = uniqueMethods.values(); - allMethods = methods.toArray(new Method[0]); - return allMethods; + return methods.toArray(new Method[0]); } private void addUniqueMethods(Map uniqueMethods, Method[] methods) { From bd26999622c43b68a2252f349b71986c0a6e7d6d Mon Sep 17 00:00:00 2001 From: Iwao AVE! Date: Wed, 8 Sep 2021 03:51:31 +0900 Subject: [PATCH 3/3] Cosmetic changes --- .../apache/ibatis/reflection/Reflector.java | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/apache/ibatis/reflection/Reflector.java b/src/main/java/org/apache/ibatis/reflection/Reflector.java index 72db540f3a9..c8d75ed1112 100644 --- a/src/main/java/org/apache/ibatis/reflection/Reflector.java +++ b/src/main/java/org/apache/ibatis/reflection/Reflector.java @@ -15,14 +15,6 @@ */ package org.apache.ibatis.reflection; -import org.apache.ibatis.reflection.invoker.AmbiguousMethodInvoker; -import org.apache.ibatis.reflection.invoker.GetFieldInvoker; -import org.apache.ibatis.reflection.invoker.Invoker; -import org.apache.ibatis.reflection.invoker.MethodInvoker; -import org.apache.ibatis.reflection.invoker.SetFieldInvoker; -import org.apache.ibatis.reflection.property.PropertyNamer; -import org.apache.ibatis.util.MapUtil; - import java.lang.reflect.Array; import java.lang.reflect.Constructor; import java.lang.reflect.Field; @@ -42,6 +34,14 @@ import java.util.Map; import java.util.Map.Entry; +import org.apache.ibatis.reflection.invoker.AmbiguousMethodInvoker; +import org.apache.ibatis.reflection.invoker.GetFieldInvoker; +import org.apache.ibatis.reflection.invoker.Invoker; +import org.apache.ibatis.reflection.invoker.MethodInvoker; +import org.apache.ibatis.reflection.invoker.SetFieldInvoker; +import org.apache.ibatis.reflection.property.PropertyNamer; +import org.apache.ibatis.util.MapUtil; + /** * This class represents a cached set of class definition information that * allows for easy mapping between property names and getter/setter methods. @@ -64,9 +64,9 @@ public class Reflector { public Reflector(Class clazz) { type = clazz; addDefaultConstructor(clazz); - Method [] allMethods = getClassMethods(clazz); - addGetMethods(allMethods); - addSetMethods(allMethods); + Method[] classMethods = getClassMethods(clazz); + addGetMethods(classMethods); + addSetMethods(classMethods); addFields(clazz); readablePropertyNames = getMethods.keySet().toArray(new String[0]); writablePropertyNames = setMethods.keySet().toArray(new String[0]); @@ -289,6 +289,7 @@ private Method[] getClassMethods(Class clazz) { } Collection methods = uniqueMethods.values(); + return methods.toArray(new Method[0]); }