From 7b016ea7c6e03723d37149a4c2d012b6a62740ca Mon Sep 17 00:00:00 2001 From: aoshiguchen <1052045476@qq.com> Date: Mon, 20 Jun 2022 15:58:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=8D=E5=B0=84=E5=B7=A5=E5=85=B7=E7=B1=BB?= =?UTF-8?q?=E9=83=A8=E5=88=86=E4=BB=A3=E7=A0=81=E9=A3=8E=E6=A0=BC=E7=BB=9F?= =?UTF-8?q?=E4=B8=80.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fun/asgc/neutrino/core/context/Bean.java | 14 ++--- .../asgc/neutrino/core/util/ReflectUtil.java | 61 ++++++++++++++----- 2 files changed, 52 insertions(+), 23 deletions(-) diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/context/Bean.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/context/Bean.java index 92342a37..c3c24324 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/context/Bean.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/context/Bean.java @@ -67,8 +67,8 @@ public class Bean implements LifeCycle { } isInit = true; - Method[] methods = ReflectUtil.getMethods(clazz); - if (null != methods && methods.length > 0) { + Set methods = ReflectUtil.getMethods(clazz); + if (CollectionUtil.notEmpty(methods)) { for (Method method : methods) { if (method.isAnnotationPresent(Init.class) && method.getParameters().length == 0) { try { @@ -86,8 +86,8 @@ public class Bean implements LifeCycle { if (!hasInstance()) { return; } - Method[] methods = ReflectUtil.getMethods(clazz); - if (null != methods && methods.length > 0) { + Set methods = ReflectUtil.getMethods(clazz); + if (CollectionUtil.notEmpty(methods)) { for (Method method : methods) { if (method.isAnnotationPresent(Destroy.class) && method.getParameters().length == 0) { try { @@ -131,9 +131,9 @@ public class Bean implements LifeCycle { } private void inject(ApplicationContext context) { - Method[] methods = ReflectUtil.getMethods(clazz); - if (null != methods && methods.length > 0) { - Stream.of(methods).forEach(method -> { + Set methods = ReflectUtil.getMethods(clazz); + if (CollectionUtil.notEmpty(methods)) { + methods.stream().forEach(method -> { fun.asgc.neutrino.core.annotation.Bean bean = method.getAnnotation(fun.asgc.neutrino.core.annotation.Bean.class); if (null == bean) { return; diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/ReflectUtil.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/ReflectUtil.java index 85c2a7ca..e0a56724 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/ReflectUtil.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/ReflectUtil.java @@ -31,6 +31,7 @@ import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.*; import java.util.function.Function; +import java.util.stream.Collectors; import java.util.stream.Stream; /** @@ -40,11 +41,11 @@ import java.util.stream.Stream; */ public class ReflectUtil { - private static Cache, Field[]> fieldsCache = new MemoryCache<>(); - private static Cache, Field[]> declaredFieldsCache = new MemoryCache<>(); + private static Cache, Set> fieldsCache = new MemoryCache<>(); + private static Cache, Set> declaredFieldsCache = new MemoryCache<>(); private static Cache, Set> inheritChainDeclaredFieldSetCache = new MemoryCache<>(); - private static Cache, Method[]> methodsCache = new MemoryCache<>(); - private static Cache, Method[]> declaredMethodsCache = new MemoryCache<>(); + private static Cache, Set> methodsCache = new MemoryCache<>(); + private static Cache, Set> declaredMethodsCache = new MemoryCache<>(); private static Cache getMethodCache = new MemoryCache<>(); private static Cache setMethodCache = new MemoryCache<>(); @@ -53,8 +54,15 @@ public class ReflectUtil { * @param clazz * @return */ - public static Field[] getFields(Class clazz) { - return kvProcess(fieldsCache, clazz, c -> c.getFields()); + public static Set getFields(Class clazz) { + return kvProcess(fieldsCache, clazz, c -> { + Field[] fields = c.getFields(); + Set fieldSet = new HashSet<>(); + if (ArrayUtil.notEmpty(fields)) { + fieldSet = Stream.of(fields).collect(Collectors.toSet()); + } + return fieldSet; + }); } /** @@ -62,8 +70,15 @@ public class ReflectUtil { * @param clazz * @return */ - public static Field[] getDeclaredFields(Class clazz) { - return kvProcess(declaredFieldsCache, clazz, c -> c.getDeclaredFields()); + public static Set getDeclaredFields(Class clazz) { + return kvProcess(declaredFieldsCache, clazz, c -> { + Field[] fields = c.getDeclaredFields(); + Set fieldSet = new HashSet<>(); + if (ArrayUtil.notEmpty(fields)) { + fieldSet = Stream.of(fields).collect(Collectors.toSet()); + } + return fieldSet; + }); } /** @@ -88,8 +103,8 @@ public class ReflectUtil { Set set = new HashSet<>(); Set> ignores = null == ignoreClasses ? new HashSet<>() : ignoreClasses; while (null != c && !ignores.contains(c)) { - Field[] fields = getDeclaredFields(c); - if (null != fields && fields.length > 0) { + Set fields = getDeclaredFields(c); + if (CollectionUtil.notEmpty(fields)) { for (Field field : fields) { if (field.getName().equals("this$0") || nameSet.contains(field.getName())) { continue; @@ -111,8 +126,15 @@ public class ReflectUtil { * @param clazz * @return */ - public static Method[] getMethods(Class clazz) { - return kvProcess(methodsCache, clazz, c -> c.getMethods()); + public static Set getMethods(Class clazz) { + return kvProcess(methodsCache, clazz, c -> { + Method[] methods = c.getMethods(); + Set methodSet = new HashSet<>(); + if (ArrayUtil.notEmpty(methods)) { + methodSet = Stream.of(methods).collect(Collectors.toSet()); + } + return methodSet; + }); } /** @@ -120,8 +142,15 @@ public class ReflectUtil { * @param clazz * @return */ - public static Method[] getDeclaredMethods(Class clazz) { - return kvProcess(declaredMethodsCache, clazz, c -> c.getDeclaredMethods()); + public static Set getDeclaredMethods(Class clazz) { + return kvProcess(declaredMethodsCache, clazz, c -> { + Method[] methods = c.getDeclaredMethods(); + Set methodSet = new HashSet<>(); + if (ArrayUtil.notEmpty(methods)) { + methodSet = Stream.of(methods).collect(Collectors.toSet()); + } + return methodSet; + }); } /** @@ -171,7 +200,7 @@ public class ReflectUtil { public static Method getGetMethod(Field field) { return kvProcess(getMethodCache, field, f -> { String getMethodName = getGetMethodName(field); - return Stream.of(getMethods(field.getDeclaringClass())) + return getMethods(field.getDeclaringClass()).stream() .filter(method -> method.getName().equals(getMethodName) && method.getParameters().length == 0) .findFirst().get(); }); @@ -201,7 +230,7 @@ public class ReflectUtil { public static Method getSetMethod(Field field) { return kvProcess(setMethodCache, field, f -> { String setMethodName = getSetMethodName(field); - Optional methodOptional = Stream.of(getMethods(field.getDeclaringClass())) + Optional methodOptional = getMethods(field.getDeclaringClass()).stream() .filter(method -> method.getName().equals(setMethodName) && method.getParameters().length == 1 && method.getParameterTypes()[0] == field.getType()) .findFirst(); if (methodOptional.isPresent()) {