添加获得类直接声明方法的静态方法

This commit is contained in:
huangchengxing 2022-09-21 19:33:12 +08:00
parent 84db7222f0
commit 7932e73395
5 changed files with 35 additions and 17 deletions

View File

@ -27,11 +27,6 @@ import java.util.stream.Stream;
*/
public class AnnotationUtil {
/**
* 注解属性缓存
*/
private static final Map<Class<? extends Annotation>, Method[]> ANNOTATION_ATTRIBUTES_CACHE = new WeakConcurrentMap<>();
/**
* 直接声明的注解缓存
*/
@ -324,11 +319,9 @@ public class AnnotationUtil {
* @since 6.0.0
*/
public static Method[] getAnnotationAttributes(final Class<? extends Annotation> annotationType) {
return MapUtil.computeIfAbsent(
ANNOTATION_ATTRIBUTES_CACHE, annotationType, type -> Stream.of(annotationType.getDeclaredMethods())
return Stream.of(MethodUtil.getDeclaredMethods(annotationType))
.filter(AnnotationUtil::isAnnotationAttribute)
.toArray(Method[]::new)
);
.toArray(Method[]::new);
}
/**

View File

@ -2,6 +2,7 @@ package cn.hutool.core.annotation;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.reflect.ClassUtil;
import cn.hutool.core.reflect.MethodUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ArrayUtil;
@ -357,8 +358,7 @@ public class HierarchicalAnnotatedElements implements AnnotatedElement, Iterable
if (!isMethod) {
collectElement(mappings, type);
} else {
// TODO 改为通过带缓存的反射工具类完成
Stream.of(type.getDeclaredMethods())
Stream.of(MethodUtil.getDeclaredMethods(type))
.filter(method -> isMatchMethod(methodSource, method))
.forEach(method -> collectElement(mappings, method));
}

View File

@ -15,11 +15,7 @@ import cn.hutool.core.util.ArrayUtil;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.*;
import java.util.function.Predicate;
/**
@ -33,6 +29,10 @@ public class MethodUtil {
* 方法缓存
*/
private static final WeakConcurrentMap<Class<?>, Method[]> METHODS_CACHE = new WeakConcurrentMap<>();
/**
* 直接声明的方法缓存
*/
private static final WeakConcurrentMap<Class<?>, Method[]> DECLARED_METHODS_CACHE = new WeakConcurrentMap<>();
// --------------------------------------------------------------------------------------------------------- method
@ -323,6 +323,19 @@ public class MethodUtil {
(key) -> getMethodsDirectly(beanClass, true, true));
}
/**
* 获得类中所有直接声明方法不包括其父类中的方法
*
* @param beanClass {@code null}
* @return 方法列表
* @throws SecurityException 安全检查异常
*/
public static Method[] getDeclaredMethods(final Class<?> beanClass) throws SecurityException {
Assert.notNull(beanClass);
return DECLARED_METHODS_CACHE.computeIfAbsent(beanClass,
key -> getMethodsDirectly(beanClass, false, Objects.equals(Object.class, beanClass)));
}
/**
* 获得一个类中所有方法列表直接反射获取无缓存<br>
* 接口获取方法和默认方法获取的方法包括

View File

@ -127,7 +127,7 @@ public class AnnotationUtilTest {
@Test
public void testGetAnnotationAttributes() {
Method[] methods = AnnotationUtil.getAnnotationAttributes(AnnotationForTest.class);
Assert.assertSame(methods, AnnotationUtil.getAnnotationAttributes(AnnotationForTest.class));
Assert.assertArrayEquals(methods, AnnotationUtil.getAnnotationAttributes(AnnotationForTest.class));
Assert.assertEquals(1, methods.length);
Assert.assertArrayEquals(AnnotationForTest.class.getDeclaredMethods(), methods);
}

View File

@ -81,6 +81,18 @@ public class MethodUtilTest {
Assert.assertEquals(10, testClass.getA());
}
@Test
public void getDeclaredMethodsTest() {
Class<?> type = ReflectUtilTest.TestBenchClass.class;
Method[] methods = type.getDeclaredMethods();
Assert.assertArrayEquals(methods, MethodUtil.getDeclaredMethods(type));
Assert.assertSame(MethodUtil.getDeclaredMethods(type), MethodUtil.getDeclaredMethods(type));
type = Object.class;
methods = type.getDeclaredMethods();
Assert.assertArrayEquals(methods, MethodUtil.getDeclaredMethods(type));
}
@Test
@Ignore
public void getMethodBenchTest() {