|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2018 the original author or authors. |
| 2 | + * Copyright 2002-2019 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
17 | 17 | package org.springframework.core.annotation;
|
18 | 18 |
|
19 | 19 | import java.lang.annotation.Annotation;
|
| 20 | +import java.lang.annotation.Documented; |
20 | 21 | import java.lang.annotation.ElementType;
|
21 | 22 | import java.lang.annotation.Inherited;
|
22 | 23 | import java.lang.annotation.Retention;
|
|
28 | 29 | import java.util.Date;
|
29 | 30 | import java.util.List;
|
30 | 31 | import java.util.Set;
|
| 32 | +import javax.annotation.Nonnull; |
| 33 | +import javax.annotation.ParametersAreNonnullByDefault; |
31 | 34 | import javax.annotation.Resource;
|
| 35 | +import javax.annotation.meta.When; |
32 | 36 |
|
33 | 37 | import org.junit.Ignore;
|
34 | 38 | import org.junit.Rule;
|
35 | 39 | import org.junit.Test;
|
36 | 40 | import org.junit.internal.ArrayComparisonFailure;
|
37 | 41 | import org.junit.rules.ExpectedException;
|
38 | 42 |
|
| 43 | +import org.springframework.lang.NonNullApi; |
39 | 44 | import org.springframework.stereotype.Component;
|
40 | 45 | import org.springframework.stereotype.Indexed;
|
41 | 46 | import org.springframework.util.Assert;
|
@@ -118,6 +123,14 @@ public void hasMetaAnnotationTypesOnClassWithMetaDepth2() {
|
118 | 123 | assertFalse(hasMetaAnnotationTypes(ComposedTransactionalComponentClass.class, ComposedTransactionalComponent.class.getName()));
|
119 | 124 | }
|
120 | 125 |
|
| 126 | + @Test |
| 127 | + public void isAnnotatedForPlainTypes() { |
| 128 | + assertTrue(isAnnotated(Order.class, Documented.class)); |
| 129 | + assertTrue(isAnnotated(NonNullApi.class, Documented.class)); |
| 130 | + assertTrue(isAnnotated(NonNullApi.class, Nonnull.class)); |
| 131 | + assertTrue(isAnnotated(ParametersAreNonnullByDefault.class, Nonnull.class)); |
| 132 | + } |
| 133 | + |
121 | 134 | @Test
|
122 | 135 | public void isAnnotatedOnNonAnnotatedClass() {
|
123 | 136 | assertFalse(isAnnotated(NonAnnotatedClass.class, TX_NAME));
|
@@ -147,6 +160,14 @@ public void isAnnotatedOnClassWithMetaDepth2() {
|
147 | 160 | assertTrue(isAnnotated(ComposedTransactionalComponentClass.class, ComposedTransactionalComponent.class.getName()));
|
148 | 161 | }
|
149 | 162 |
|
| 163 | + @Test |
| 164 | + public void hasAnnotationForPlainTypes() { |
| 165 | + assertTrue(hasAnnotation(Order.class, Documented.class)); |
| 166 | + assertTrue(hasAnnotation(NonNullApi.class, Documented.class)); |
| 167 | + assertTrue(hasAnnotation(NonNullApi.class, Nonnull.class)); |
| 168 | + assertTrue(hasAnnotation(ParametersAreNonnullByDefault.class, Nonnull.class)); |
| 169 | + } |
| 170 | + |
150 | 171 | @Test
|
151 | 172 | public void getAllAnnotationAttributesOnNonAnnotatedClass() {
|
152 | 173 | assertNull(getAllAnnotationAttributes(NonAnnotatedClass.class, TX_NAME));
|
@@ -207,6 +228,22 @@ public void getAllAnnotationAttributesOnClassWithMultipleComposedAnnotations() {
|
207 | 228 | attributes.get("value"));
|
208 | 229 | }
|
209 | 230 |
|
| 231 | + @Test |
| 232 | + public void getAllAnnotationAttributesOnLangType() { |
| 233 | + MultiValueMap<String, Object> attributes = getAllAnnotationAttributes( |
| 234 | + NonNullApi.class, Nonnull.class.getName()); |
| 235 | + assertNotNull(attributes); |
| 236 | + assertEquals(asList(When.ALWAYS), attributes.get("when")); |
| 237 | + } |
| 238 | + |
| 239 | + @Test |
| 240 | + public void getAllAnnotationAttributesOnJavaxType() { |
| 241 | + MultiValueMap<String, Object> attributes = getAllAnnotationAttributes( |
| 242 | + ParametersAreNonnullByDefault.class, Nonnull.class.getName()); |
| 243 | + assertNotNull(attributes); |
| 244 | + assertEquals(asList(When.ALWAYS), attributes.get("when")); |
| 245 | + } |
| 246 | + |
210 | 247 | @Test
|
211 | 248 | public void getMergedAnnotationAttributesOnClassWithLocalAnnotation() {
|
212 | 249 | Class<?> element = TxConfig.class;
|
@@ -701,14 +738,33 @@ private void assertWebMapping(AnnotatedElement element) throws ArrayComparisonFa
|
701 | 738 | @Test
|
702 | 739 | public void javaLangAnnotationTypeViaFindMergedAnnotation() throws Exception {
|
703 | 740 | Constructor<?> deprecatedCtor = Date.class.getConstructor(String.class);
|
704 |
| - assertEquals(deprecatedCtor.getAnnotation(Deprecated.class), findMergedAnnotation(deprecatedCtor, Deprecated.class)); |
705 |
| - assertEquals(Date.class.getAnnotation(Deprecated.class), findMergedAnnotation(Date.class, Deprecated.class)); |
| 741 | + assertEquals(deprecatedCtor.getAnnotation(Deprecated.class), |
| 742 | + findMergedAnnotation(deprecatedCtor, Deprecated.class)); |
| 743 | + assertEquals(Date.class.getAnnotation(Deprecated.class), |
| 744 | + findMergedAnnotation(Date.class, Deprecated.class)); |
706 | 745 | }
|
707 | 746 |
|
708 | 747 | @Test
|
709 | 748 | public void javaxAnnotationTypeViaFindMergedAnnotation() throws Exception {
|
710 |
| - assertEquals(ResourceHolder.class.getAnnotation(Resource.class), findMergedAnnotation(ResourceHolder.class, Resource.class)); |
711 |
| - assertEquals(SpringAppConfigClass.class.getAnnotation(Resource.class), findMergedAnnotation(SpringAppConfigClass.class, Resource.class)); |
| 749 | + assertEquals(ResourceHolder.class.getAnnotation(Resource.class), |
| 750 | + findMergedAnnotation(ResourceHolder.class, Resource.class)); |
| 751 | + assertEquals(SpringAppConfigClass.class.getAnnotation(Resource.class), |
| 752 | + findMergedAnnotation(SpringAppConfigClass.class, Resource.class)); |
| 753 | + } |
| 754 | + |
| 755 | + @Test |
| 756 | + public void javaxMetaAnnotationTypeViaFindMergedAnnotation() throws Exception { |
| 757 | + assertEquals(ParametersAreNonnullByDefault.class.getAnnotation(Nonnull.class), |
| 758 | + findMergedAnnotation(ParametersAreNonnullByDefault.class, Nonnull.class)); |
| 759 | + assertEquals(ParametersAreNonnullByDefault.class.getAnnotation(Nonnull.class), |
| 760 | + findMergedAnnotation(ResourceHolder.class, Nonnull.class)); |
| 761 | + } |
| 762 | + |
| 763 | + @Test |
| 764 | + public void nullableAnnotationTypeViaFindMergedAnnotation() throws Exception { |
| 765 | + Method method = TransactionalServiceImpl.class.getMethod("doIt"); |
| 766 | + assertEquals(method.getAnnotation(Resource.class), findMergedAnnotation(method, Resource.class)); |
| 767 | + assertEquals(method.getAnnotation(Resource.class), findMergedAnnotation(method, Resource.class)); |
712 | 768 | }
|
713 | 769 |
|
714 | 770 | @Test
|
@@ -1288,6 +1344,7 @@ static class SpringAppConfigClass {
|
1288 | 1344 | }
|
1289 | 1345 |
|
1290 | 1346 | @Resource(name = "x")
|
| 1347 | + @ParametersAreNonnullByDefault |
1291 | 1348 | static class ResourceHolder {
|
1292 | 1349 | }
|
1293 | 1350 |
|
|
0 commit comments