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

Merge pull request !975 from Sparks/v5-dev
This commit is contained in:
Looly 2023-04-11 15:16:43 +00:00 committed by Gitee
commit 6f68e2fac7
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
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{