mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
commit
82824793ba
@ -7,11 +7,12 @@ import cn.hutool.core.annotation.scanner.TypeAnnotationScanner;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.exceptions.UtilException;
|
||||
import cn.hutool.core.lang.Opt;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import cn.hutool.core.lang.func.Func1;
|
||||
import cn.hutool.core.lang.func.LambdaUtil;
|
||||
import cn.hutool.core.util.*;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
import java.lang.invoke.SerializedLambda;
|
||||
import java.lang.reflect.AnnotatedElement;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Proxy;
|
||||
@ -229,6 +230,28 @@ public class AnnotationUtil {
|
||||
return ReflectUtil.invoke(annotation, method);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定注解属性的值<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(AnnotatedElement annotationEle, Func1<A, R> propertyName) {
|
||||
if(propertyName == null) {
|
||||
return null;
|
||||
}else {
|
||||
final SerializedLambda lambda = LambdaUtil.resolve(propertyName);
|
||||
final String instantiatedMethodType = lambda.getInstantiatedMethodType();
|
||||
Class<A> annotationClass = ClassUtil.loadClass(StrUtil.sub(instantiatedMethodType, 2, StrUtil.indexOf(instantiatedMethodType, ';')));
|
||||
return getAnnotationValue(annotationEle,annotationClass, lambda.getImplMethodName());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定注解中所有属性值<br>
|
||||
* 如果无指定的属性方法返回null
|
||||
|
@ -26,4 +26,6 @@ public @interface AnnotationForTest {
|
||||
|
||||
@Alias("value")
|
||||
String retry() default "";
|
||||
|
||||
String[] names() default "";
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
package cn.hutool.core.annotation;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
@ -34,6 +36,12 @@ public class AnnotationUtilTest {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAnnotationValueTest2() {
|
||||
String[] names = AnnotationUtil.getAnnotationValue(ClassWithAnnotation.class, AnnotationForTest::names);
|
||||
Assert.assertTrue(ArrayUtil.equals(names, new String[]{"测试1", "测试2"}));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAnnotationSyncAlias() {
|
||||
// 直接获取
|
||||
@ -45,7 +53,7 @@ public class AnnotationUtilTest {
|
||||
Assert.assertTrue(AnnotationUtil.isSynthesizedAnnotation(annotation));
|
||||
}
|
||||
|
||||
@AnnotationForTest("测试")
|
||||
@AnnotationForTest(value = "测试", names = {"测试1", "测试2"})
|
||||
@RepeatAnnotationForTest
|
||||
static class ClassWithAnnotation{
|
||||
public void test(){
|
||||
|
Loading…
x
Reference in New Issue
Block a user