Skip to content

Commit

Permalink
fix: check all parents for further parameter annotations
Browse files Browse the repository at this point in the history
see the issue for details

Refs #4002
  • Loading branch information
j-wrensch authored and frantuma committed Aug 19, 2021
1 parent 6ccd2f8 commit 063a5df
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ public static Annotation[][] getParameterAnnotations(Method method) {
Annotation[][] methodAnnotations = method.getParameterAnnotations();
Method overriddenmethod = getOverriddenMethod(method);

if (overriddenmethod != null) {
while (overriddenmethod != null) {
Annotation[][] overriddenAnnotations = overriddenmethod
.getParameterAnnotations();

Expand All @@ -404,6 +404,8 @@ public static Annotation[][] getParameterAnnotations(Method method) {
}

}

overriddenmethod = getOverriddenMethod(overriddenmethod);
}
return methodAnnotations;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
import org.testng.annotations.Test;

import javax.ws.rs.Path;

import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
Expand All @@ -22,6 +27,8 @@

import static org.testng.Assert.assertNull;

import static java.lang.annotation.ElementType.PARAMETER;

public class ReflectionUtilsTest {

@Test
Expand Down Expand Up @@ -161,8 +168,54 @@ public void getRepeatableAnnotationsArrayTest() {
Assert.assertEquals("inherited tag", annotations[0].name());
}

@Test
public void getParameterAnnotationsTest() throws NoSuchMethodException {
Method method = SecondLevelSubClass.class.getMethod("method", String.class);
Annotation[][] parameterAnnotations = ReflectionUtils.getParameterAnnotations(method);
Assert.assertEquals(1, parameterAnnotations.length);
Assert.assertEquals(1, parameterAnnotations[0].length);
Assert.assertTrue(parameterAnnotations[0][0] instanceof AnnotationInterface);
Assert.assertEquals("level1", ((AnnotationInterface)parameterAnnotations[0][0]).value());
}

@Test
public void getParameterAnnotationsForOverriddenAnnotationTest() throws NoSuchMethodException {
Method method = ThirdLevelSubClass.class.getMethod("method", String.class);
Annotation[][] parameterAnnotations = ReflectionUtils.getParameterAnnotations(method);
Assert.assertEquals(1, parameterAnnotations.length);
Assert.assertEquals(1, parameterAnnotations[0].length);
Assert.assertTrue(parameterAnnotations[0][0] instanceof AnnotationInterface);
Assert.assertEquals("level4", ((AnnotationInterface)parameterAnnotations[0][0]).value());
}

@Tag(name = "inherited tag")
private interface AnnotatedInterface {}

private class InheritingClass implements AnnotatedInterface {}

@Target({PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
private @interface AnnotationInterface {
String value();
}

private static class BaseClass {
public void method(@AnnotationInterface("level1") String example) {}
}

private static class FirstLevelSubClass extends BaseClass {
@Override
public void method(String example){}
}

private static class SecondLevelSubClass extends FirstLevelSubClass {
@Override
public void method(String example){}
}

private static class ThirdLevelSubClass extends SecondLevelSubClass {
@Override
public void method(@AnnotationInterface("level4") String example){}
}

}

0 comments on commit 063a5df

Please sign in to comment.