Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check all parents for further parameter annotations #4003

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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){}
}

}