mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
添加获得类直接声明方法的静态方法
This commit is contained in:
parent
84db7222f0
commit
7932e73395
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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));
|
||||
}
|
||||
|
@ -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>
|
||||
* 接口获取方法和默认方法,获取的方法包括:
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user