修复使用AnnotationUtil.getAnnotationAlias获取注解时可能会出现空指针的问题

This commit is contained in:
zhangjiayu
2023-04-11 16:52:58 +08:00
parent a97901f0e3
commit 9f1fd664eb
2 changed files with 11 additions and 0 deletions

View File

@@ -457,6 +457,9 @@ public class AnnotationUtil {
*/
public static <T extends Annotation> T getAnnotationAlias(AnnotatedElement annotationEle, Class<T> annotationType) {
final T annotation = getAnnotation(annotationEle, annotationType);
if (null == annotation) {
return null;
}
return aggregatingFromAnnotation(annotation).synthesize(annotationType);
}

View File

@@ -56,6 +56,14 @@ public class AnnotationUtilTest {
Assert.assertTrue(AnnotationUtil.isSynthesizedAnnotation(annotation));
}
@Test
public void getAnnotationSyncAliasWhenNotAnnotation() {
getAnnotationSyncAlias();
// 使用AnnotationUtil.getAnnotationAlias获取对象上并不存在的注解
final Alias alias = AnnotationUtil.getAnnotationAlias(ClassWithAnnotation.class, Alias.class);
Assert.assertNull(alias);
}
@AnnotationForTest(value = "测试", names = {"测试1", "测试2"})
@RepeatAnnotationForTest
static class ClassWithAnnotation{