|
12 | 12 | import org.testng.annotations.Test; |
13 | 13 |
|
14 | 14 | import javax.ws.rs.Path; |
| 15 | + |
| 16 | +import java.lang.annotation.Annotation; |
| 17 | +import java.lang.annotation.Retention; |
| 18 | +import java.lang.annotation.RetentionPolicy; |
| 19 | +import java.lang.annotation.Target; |
15 | 20 | import java.lang.reflect.Field; |
16 | 21 | import java.lang.reflect.Method; |
17 | 22 | import java.lang.reflect.Type; |
|
22 | 27 |
|
23 | 28 | import static org.testng.Assert.assertNull; |
24 | 29 |
|
| 30 | +import static java.lang.annotation.ElementType.PARAMETER; |
| 31 | + |
25 | 32 | public class ReflectionUtilsTest { |
26 | 33 |
|
27 | 34 | @Test |
@@ -161,8 +168,54 @@ public void getRepeatableAnnotationsArrayTest() { |
161 | 168 | Assert.assertEquals("inherited tag", annotations[0].name()); |
162 | 169 | } |
163 | 170 |
|
| 171 | + @Test |
| 172 | + public void getParameterAnnotationsTest() throws NoSuchMethodException { |
| 173 | + Method method = SecondLevelSubClass.class.getMethod("method", String.class); |
| 174 | + Annotation[][] parameterAnnotations = ReflectionUtils.getParameterAnnotations(method); |
| 175 | + Assert.assertEquals(1, parameterAnnotations.length); |
| 176 | + Assert.assertEquals(1, parameterAnnotations[0].length); |
| 177 | + Assert.assertTrue(parameterAnnotations[0][0] instanceof AnnotationInterface); |
| 178 | + Assert.assertEquals("level1", ((AnnotationInterface)parameterAnnotations[0][0]).value()); |
| 179 | + } |
| 180 | + |
| 181 | + @Test |
| 182 | + public void getParameterAnnotationsForOverriddenAnnotationTest() throws NoSuchMethodException { |
| 183 | + Method method = ThirdLevelSubClass.class.getMethod("method", String.class); |
| 184 | + Annotation[][] parameterAnnotations = ReflectionUtils.getParameterAnnotations(method); |
| 185 | + Assert.assertEquals(1, parameterAnnotations.length); |
| 186 | + Assert.assertEquals(1, parameterAnnotations[0].length); |
| 187 | + Assert.assertTrue(parameterAnnotations[0][0] instanceof AnnotationInterface); |
| 188 | + Assert.assertEquals("level4", ((AnnotationInterface)parameterAnnotations[0][0]).value()); |
| 189 | + } |
| 190 | + |
164 | 191 | @Tag(name = "inherited tag") |
165 | 192 | private interface AnnotatedInterface {} |
166 | 193 |
|
167 | 194 | private class InheritingClass implements AnnotatedInterface {} |
| 195 | + |
| 196 | + @Target({PARAMETER}) |
| 197 | + @Retention(RetentionPolicy.RUNTIME) |
| 198 | + private @interface AnnotationInterface { |
| 199 | + String value(); |
| 200 | + } |
| 201 | + |
| 202 | + private static class BaseClass { |
| 203 | + public void method(@AnnotationInterface("level1") String example) {} |
| 204 | + } |
| 205 | + |
| 206 | + private static class FirstLevelSubClass extends BaseClass { |
| 207 | + @Override |
| 208 | + public void method(String example){} |
| 209 | + } |
| 210 | + |
| 211 | + private static class SecondLevelSubClass extends FirstLevelSubClass { |
| 212 | + @Override |
| 213 | + public void method(String example){} |
| 214 | + } |
| 215 | + |
| 216 | + private static class ThirdLevelSubClass extends SecondLevelSubClass { |
| 217 | + @Override |
| 218 | + public void method(@AnnotationInterface("level4") String example){} |
| 219 | + } |
| 220 | + |
168 | 221 | } |
0 commit comments