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

This commit is contained in:
LinRuChang
2022-09-30 11:36:30 +08:00
parent e59548ebbf
commit 80f973093c
3 changed files with 32 additions and 1 deletions

View File

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

View File

@@ -1,5 +1,6 @@
package cn.hutool.core.annotation;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ReflectUtil;
import org.junit.Assert;
import org.junit.Test;
@@ -34,6 +35,12 @@ public class AnnotationUtilTest {
}
@Test
public void getAnnotationValueTest2() {
String[] names = AnnotationUtil.getAnnotationValue(ClassWithAnnotation.class, AnnotationForTest.class, AnnotationForTest::names);
Assert.assertTrue(ArrayUtil.equals(names, new String[]{"测试1", "测试2"}));
}
@Test
public void getAnnotationSyncAlias() {
// 直接获取
@@ -45,7 +52,7 @@ public class AnnotationUtilTest {
Assert.assertTrue(AnnotationUtil.isSynthesizedAnnotation(annotation));
}
@AnnotationForTest("测试")
@AnnotationForTest(value = "测试", names = {"测试1", "测试2"})
@RepeatAnnotationForTest
static class ClassWithAnnotation{
public void test(){