Skip to content

Annotation templates should pick up deep non-aliased attributes #16312

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

Merged
merged 1 commit into from
Mar 18, 2025
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 @@ -149,25 +149,26 @@ private List<MergedAnnotation<A>> findParameterAnnotations(Parameter current) {
}
Executable executable = current.getDeclaringExecutable();
if (executable instanceof Method method) {
Class<?> clazz = method.getDeclaringClass();
Set<Class<?>> visited = new HashSet<>();
while (clazz != null && clazz != Object.class) {
directAnnotations = findClosestParameterAnnotations(method, clazz, current, visited);
if (!directAnnotations.isEmpty()) {
return directAnnotations;
}
clazz = clazz.getSuperclass();
directAnnotations = findClosestParameterAnnotations(method, method.getDeclaringClass(), current,
new HashSet<>());
if (!directAnnotations.isEmpty()) {
return directAnnotations;
}
}
return Collections.emptyList();
}

private List<MergedAnnotation<A>> findClosestParameterAnnotations(Method method, Class<?> clazz, Parameter current,
Set<Class<?>> visited) {
if (!visited.add(clazz)) {
if (clazz == null || clazz == Object.class || !visited.add(clazz)) {
return Collections.emptyList();
}
List<MergedAnnotation<A>> annotations = new ArrayList<>(findDirectParameterAnnotations(method, clazz, current));
List<MergedAnnotation<A>> directAnnotations = findDirectParameterAnnotations(method, clazz, current);
if (!directAnnotations.isEmpty()) {
return directAnnotations;
}
List<MergedAnnotation<A>> annotations = new ArrayList<>(
findClosestParameterAnnotations(method, clazz.getSuperclass(), current, visited));
for (Class<?> ifc : clazz.getInterfaces()) {
annotations.addAll(findClosestParameterAnnotations(method, ifc, current, visited));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,13 @@ void scanWhenAnnotationOnParameterizedMethodThenLocates() throws Exception {
assertThat(pre).isNotNull();
}

@Test
void scanParameterAnnotationWhenPresentInParentAndInterfaceThenException() throws Exception {
Parameter parameter = DefaultUserService.class.getDeclaredMethod("batch", String[].class).getParameters()[0];
assertThatExceptionOfType(AnnotationConfigurationException.class)
.isThrownBy(() -> this.parameterScanner.scan(parameter));
}

interface UserService {

void add(@CustomParameterAnnotation("one") String user);
Expand All @@ -345,6 +352,8 @@ interface ThirdPartyUserService {

interface RemoteUserService extends ThirdPartyUserService {

void batch(@CustomParameterAnnotation("six") String... user);

}

static class UserServiceImpl implements UserService, OtherUserService, RemoteUserService {
Expand All @@ -369,6 +378,20 @@ public void delete(String user) {

}

@Override
public void batch(@CustomParameterAnnotation("seven") String... user) {

}

}

static class DefaultUserService extends UserServiceImpl implements RemoteUserService {

@Override
public void batch(String... user) {

}

}

@Target({ ElementType.PARAMETER })
Expand Down