|
23 | 23 | import java.lang.annotation.RetentionPolicy;
|
24 | 24 | import java.lang.annotation.Target;
|
25 | 25 | import java.lang.reflect.AnnotatedElement;
|
26 |
| -import java.lang.reflect.Constructor; |
27 | 26 | import java.lang.reflect.Method;
|
28 |
| -import java.util.Date; |
29 | 27 | import java.util.List;
|
30 | 28 | import java.util.Set;
|
31 | 29 | import javax.annotation.Resource;
|
|
43 | 41 |
|
44 | 42 | import static java.util.Arrays.*;
|
45 | 43 | import static java.util.stream.Collectors.*;
|
| 44 | +import static org.assertj.core.api.Assertions.assertThat; |
46 | 45 | import static org.hamcrest.Matchers.*;
|
47 | 46 | import static org.junit.Assert.*;
|
48 | 47 | import static org.springframework.core.annotation.AnnotatedElementUtils.*;
|
@@ -736,6 +735,15 @@ public void findMethodAnnotationFromGenericSuperclass() throws Exception {
|
736 | 735 | assertNotNull(order);
|
737 | 736 | }
|
738 | 737 |
|
| 738 | + @Test // gh-22655 |
| 739 | + public void forAnnotationsCreatesCopyOfArrayOnEachCall() { |
| 740 | + AnnotatedElement element = AnnotatedElementUtils.forAnnotations(ForAnnotationsClass.class.getDeclaredAnnotations()); |
| 741 | + // Trigger the NPE as originally reported in the bug |
| 742 | + AnnotationsScanner.getDeclaredAnnotations(element, false); |
| 743 | + AnnotationsScanner.getDeclaredAnnotations(element, false); |
| 744 | + // Also specifically test we get different instances |
| 745 | + assertThat(element.getDeclaredAnnotations()).isNotSameAs(element.getDeclaredAnnotations()); |
| 746 | + } |
739 | 747 |
|
740 | 748 | // -------------------------------------------------------------------------
|
741 | 749 |
|
@@ -1301,4 +1309,10 @@ public void doIt() {
|
1301 | 1309 | }
|
1302 | 1310 | }
|
1303 | 1311 |
|
| 1312 | + @Deprecated |
| 1313 | + @ComponentScan |
| 1314 | + class ForAnnotationsClass { |
| 1315 | + |
| 1316 | + } |
| 1317 | + |
1304 | 1318 | }
|
0 commit comments