mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
add methodsa
This commit is contained in:
parent
bfdc1ecbda
commit
83a4f14b43
@ -1,14 +1,25 @@
|
||||
package cn.hutool.core.annotation;
|
||||
|
||||
import cn.hutool.core.classloader.ClassLoaderUtil;
|
||||
import cn.hutool.core.exceptions.UtilException;
|
||||
import cn.hutool.core.lang.func.LambdaInfo;
|
||||
import cn.hutool.core.lang.func.LambdaUtil;
|
||||
import cn.hutool.core.lang.func.SerFunction;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.map.WeakConcurrentMap;
|
||||
import cn.hutool.core.reflect.FieldUtil;
|
||||
import cn.hutool.core.reflect.MethodUtil;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Inherited;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import java.lang.reflect.AnnotatedElement;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
@ -39,7 +50,7 @@ public class AnnotationUtil {
|
||||
* @return 注解
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public static Annotation[] getDeclaredAnnotations(AnnotatedElement element) {
|
||||
public static Annotation[] getDeclaredAnnotations(final AnnotatedElement element) {
|
||||
return MapUtil.computeIfAbsent(DECLARED_ANNOTATIONS_CACHE, element, AnnotatedElement::getDeclaredAnnotations);
|
||||
}
|
||||
|
||||
@ -168,6 +179,28 @@ public class AnnotationUtil {
|
||||
return getAnnotationValue(annotationEle, annotationType, "value");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定注解属性的值<br>
|
||||
* 如果无指定的属性方法返回null
|
||||
*
|
||||
* @param <A> 注解类型
|
||||
* @param <R> 注解类型值
|
||||
* @param annotationEle {@link AnnotatedElement},可以是Class、Method、Field、Constructor、ReflectPermission
|
||||
* @param propertyName 属性名,例如注解中定义了name()方法,则 此处传入name
|
||||
* @return 注解对象
|
||||
* @throws UtilException 调用注解中的方法时执行异常
|
||||
*/
|
||||
public static <A extends Annotation, R> R getAnnotationValue(final AnnotatedElement annotationEle, final SerFunction<A, R> propertyName) {
|
||||
if(propertyName == null) {
|
||||
return null;
|
||||
}else {
|
||||
final LambdaInfo lambda = LambdaUtil.resolve(propertyName);
|
||||
final String instantiatedMethodType = lambda.getLambda().getInstantiatedMethodType();
|
||||
final Class<A> annotationClass = ClassLoaderUtil.loadClass(StrUtil.sub(instantiatedMethodType, 2, StrUtil.indexOf(instantiatedMethodType, ';')));
|
||||
return getAnnotationValue(annotationEle,annotationClass, lambda.getLambda().getImplMethodName());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定注解属性的值<br>
|
||||
* 如果无指定的属性方法返回null
|
||||
|
@ -1,11 +1,16 @@
|
||||
package cn.hutool.core.annotation;
|
||||
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
import lombok.SneakyThrows;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Map;
|
||||
|
||||
@ -16,7 +21,7 @@ public class AnnotationUtilTest {
|
||||
|
||||
@Test
|
||||
public void testGetDeclaredAnnotations() {
|
||||
Annotation[] annotations = AnnotationUtil.getDeclaredAnnotations(ClassForTest.class);
|
||||
final Annotation[] annotations = AnnotationUtil.getDeclaredAnnotations(ClassForTest.class);
|
||||
Assert.assertArrayEquals(annotations, ClassForTest.class.getDeclaredAnnotations());
|
||||
Assert.assertSame(annotations, AnnotationUtil.getDeclaredAnnotations(ClassForTest.class));
|
||||
|
||||
@ -85,7 +90,7 @@ public class AnnotationUtilTest {
|
||||
final AnnotationForTest annotation = ClassForTest.class.getAnnotation(AnnotationForTest.class);
|
||||
final Map<String, Object> valueMap = AnnotationUtil.getAnnotationValueMap(ClassForTest.class, AnnotationForTest.class);
|
||||
Assert.assertNotNull(valueMap);
|
||||
Assert.assertEquals(1, valueMap.size());
|
||||
Assert.assertEquals(2, valueMap.size());
|
||||
Assert.assertEquals(annotation.value(), valueMap.get("value"));
|
||||
}
|
||||
|
||||
@ -129,9 +134,9 @@ public class AnnotationUtilTest {
|
||||
|
||||
@Test
|
||||
public void testGetAnnotationAttributes() {
|
||||
Method[] methods = AnnotationUtil.getAnnotationAttributes(AnnotationForTest.class);
|
||||
final Method[] methods = AnnotationUtil.getAnnotationAttributes(AnnotationForTest.class);
|
||||
Assert.assertArrayEquals(methods, AnnotationUtil.getAnnotationAttributes(AnnotationForTest.class));
|
||||
Assert.assertEquals(1, methods.length);
|
||||
Assert.assertEquals(2, methods.length);
|
||||
Assert.assertArrayEquals(AnnotationForTest.class.getDeclaredMethods(), methods);
|
||||
}
|
||||
|
||||
@ -142,6 +147,12 @@ public class AnnotationUtilTest {
|
||||
Assert.assertTrue(AnnotationUtil.isAnnotationAttribute(AnnotationForTest.class.getMethod("value")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAnnotationValueTest2() {
|
||||
final String[] names = AnnotationUtil.getAnnotationValue(ClassForTest.class, AnnotationForTest::names);
|
||||
Assert.assertTrue(ArrayUtil.equals(names, new String[]{"测试1", "测试2"}));
|
||||
}
|
||||
|
||||
@Target(ElementType.TYPE_USE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
private @interface MetaAnnotationForTest{
|
||||
@ -155,9 +166,10 @@ public class AnnotationUtilTest {
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
private @interface AnnotationForTest{
|
||||
String value() default "";
|
||||
String[] names() default "";
|
||||
}
|
||||
|
||||
@AnnotationForTest("foo")
|
||||
@AnnotationForTest(value = "foo", names = {"测试1", "测试2"})
|
||||
private static class ClassForTest{}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user