!827 注解工具类支持Lambda获取某注解属性值

Merge pull request !827 from 嗯嗯/v5-dev
This commit is contained in:
Looly 2022-09-30 11:04:46 +00:00 committed by Gitee
commit 82824793ba
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 37 additions and 4 deletions

View File

@ -7,11 +7,12 @@ import cn.hutool.core.annotation.scanner.TypeAnnotationScanner;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.exceptions.UtilException; import cn.hutool.core.exceptions.UtilException;
import cn.hutool.core.lang.Opt; import cn.hutool.core.lang.Opt;
import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.lang.func.Func1;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.lang.func.LambdaUtil;
import cn.hutool.core.util.ReflectUtil; import cn.hutool.core.util.*;
import java.lang.annotation.*; import java.lang.annotation.*;
import java.lang.invoke.SerializedLambda;
import java.lang.reflect.AnnotatedElement; import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Proxy; import java.lang.reflect.Proxy;
@ -229,6 +230,28 @@ public class AnnotationUtil {
return ReflectUtil.invoke(annotation, method); return ReflectUtil.invoke(annotation, method);
} }
/**
* 获取指定注解属性的值<br>
* 如果无指定的属性方法返回null
*
* @param <A> 注解类型
* @param <R> 注解类型值
* @param annotationEle {@link AnnotatedElement}可以是ClassMethodFieldConstructorReflectPermission
* @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> * 获取指定注解中所有属性值<br>
* 如果无指定的属性方法返回null * 如果无指定的属性方法返回null

View File

@ -26,4 +26,6 @@ public @interface AnnotationForTest {
@Alias("value") @Alias("value")
String retry() default ""; String retry() default "";
String[] names() default "";
} }

View File

@ -1,5 +1,7 @@
package cn.hutool.core.annotation; package cn.hutool.core.annotation;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ReflectUtil; import cn.hutool.core.util.ReflectUtil;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; 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 @Test
public void getAnnotationSyncAlias() { public void getAnnotationSyncAlias() {
// 直接获取 // 直接获取
@ -45,7 +53,7 @@ public class AnnotationUtilTest {
Assert.assertTrue(AnnotationUtil.isSynthesizedAnnotation(annotation)); Assert.assertTrue(AnnotationUtil.isSynthesizedAnnotation(annotation));
} }
@AnnotationForTest("测试") @AnnotationForTest(value = "测试", names = {"测试1", "测试2"})
@RepeatAnnotationForTest @RepeatAnnotationForTest
static class ClassWithAnnotation{ static class ClassWithAnnotation{
public void test(){ public void test(){