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

Fixed the issue where the annotation parameter scan skipped first-level conflicts #16312

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -146,25 +146,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 @@ -295,6 +295,13 @@ void scanParameterAnnotationWhenInterfaceNoAnnotationsThenException() throws Exc
.isThrownBy(() -> this.parameterScanner.scan(parameter));
}

@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 @@ -321,6 +328,8 @@ interface ThirdPartyUserService {

interface RemoteUserService extends ThirdPartyUserService {

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

}

static class UserServiceImpl implements UserService, OtherUserService, RemoteUserService {
Expand All @@ -345,6 +354,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
Loading