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

This commit is contained in:
Looly 2022-09-30 19:07:36 +08:00
parent 82824793ba
commit 44684aa036
3 changed files with 8 additions and 3 deletions

View File

@ -7,6 +7,7 @@
### 🐣新特性
* 【core 】 DateUtil增加isLastDayOfMonth、getLastDayOfMonth方法pr#824@Gitee
* 【core 】 AnnotationUtil类支持Lambda获取某注解属性值pr#827@Gitee
### 🐞Bug修复
* 【poi 】 修复ExcelReader读取只有标题行报错问题issue#I5U1JA@Gitee

View File

@ -240,6 +240,7 @@ public class AnnotationUtil {
* @param propertyName 属性名例如注解中定义了name()方法 此处传入name
* @return 注解对象
* @throws UtilException 调用注解中的方法时执行异常
* @since 5.8.9
*/
public static <A extends Annotation, R> R getAnnotationValue(AnnotatedElement annotationEle, Func1<A, R> propertyName) {
if(propertyName == null) {

View File

@ -1,12 +1,15 @@
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;
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.Collection;
import java.util.Collections;
@ -38,7 +41,7 @@ public class AnnotationUtilTest {
@Test
public void getAnnotationValueTest2() {
String[] names = AnnotationUtil.getAnnotationValue(ClassWithAnnotation.class, AnnotationForTest::names);
final String[] names = AnnotationUtil.getAnnotationValue(ClassWithAnnotation.class, AnnotationForTest::names);
Assert.assertTrue(ArrayUtil.equals(names, new String[]{"测试1", "测试2"}));
}