Skip to content

Commit 65cbef0

Browse files
risingcultsam0r040timonback
authored
fix(core): handle overloaded typical Java methods in annotation scan (#1524)
* fix(core): handle overloaded typical Java methods in annotation scan * refactor(core): use ReflectionsUtils from Spring to find user declared methods when scanning a class for methods Co-authored-by: Timon Back <timonback@users.noreply.github.com> --------- Co-authored-by: David Müller <david.mueller@codecentric.de> Co-authored-by: Timon Back <timonback@users.noreply.github.com>
1 parent 56adb40 commit 65cbef0

File tree

2 files changed

+40
-16
lines changed

2 files changed

+40
-16
lines changed

springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/scanners/common/annotation/AnnotationScannerUtil.java

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ public static <A extends Annotation> Stream<MethodAndAnnotation<A>> findAnnotate
5757
methodAnnotationClass.getName());
5858

5959
Stream<Method> methods = Arrays.stream(ReflectionUtils.getAllDeclaredMethods(clazz))
60-
.filter(AnnotationScannerUtil::isMethodInSourceCode)
61-
.filter(AnnotationScannerUtil::isNotTypicalJavaMethod)
60+
.filter(ReflectionUtils.USER_DECLARED_METHODS::matches)
6261
.filter(AnnotationScannerUtil::isNotHidden);
6362

6463
if (methodAnnotationClass == AllMethods.class) {
@@ -72,21 +71,7 @@ public static <A extends Annotation> Stream<MethodAndAnnotation<A>> findAnnotate
7271
.map(annotation -> new MethodAndAnnotation<>(method, annotation)));
7372
}
7473

75-
/**
76-
* Check that a method was written by a developer and not generated by the compiler.
77-
*/
78-
private static boolean isMethodInSourceCode(Method method) {
79-
return !method.isSynthetic();
80-
}
81-
8274
private static boolean isNotHidden(AnnotatedElement element) {
8375
return Objects.isNull(AnnotationUtil.findFirstAnnotation(Hidden.class, element));
8476
}
85-
86-
private static final Set<String> typicalJavaMethods =
87-
Set.of("clone", "equals", "finalize", "getClass", "hashCode", "notify", "notifyAll", "toString", "wait");
88-
89-
private static boolean isNotTypicalJavaMethod(Method method) {
90-
return !typicalJavaMethods.contains(method.getName());
91-
}
9277
}

springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/scanners/common/annotation/AnnotationScannerUtilTest.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,30 @@ void getAllMethods() throws Exception {
155155
ClassWithMethodAnnotation.class.getDeclaredMethod("hiddenAnnotatedMethod"), null));
156156
}
157157

158+
@Test
159+
void methodsWithTypicalNamesButDifferentSignaturesAreNotIgnored() throws Exception {
160+
List<MethodAndAnnotation<MethodAnnotation>> methods = AnnotationScannerUtil.findAnnotatedMethods(
161+
ClassWithTypicalJavaMethodNames.class, MethodAnnotation.class)
162+
.toList();
163+
164+
Method equalsWithCustomSignature =
165+
ClassWithTypicalJavaMethodNames.class.getDeclaredMethod("equals", String.class);
166+
Method equalsWithObjectSignatureOverride =
167+
ClassWithTypicalJavaMethodNames.class.getDeclaredMethod("equals", Object.class);
168+
Method waitWithCustomSignature =
169+
ClassWithTypicalJavaMethodNames.class.getDeclaredMethod("wait", String.class);
170+
171+
assertThat(methods)
172+
.hasSize(3)
173+
.contains(new MethodAndAnnotation<>(
174+
equalsWithCustomSignature, equalsWithCustomSignature.getAnnotation(MethodAnnotation.class)))
175+
.contains(new MethodAndAnnotation<>(
176+
waitWithCustomSignature, waitWithCustomSignature.getAnnotation(MethodAnnotation.class)))
177+
.contains(new MethodAndAnnotation<>(
178+
equalsWithObjectSignatureOverride,
179+
equalsWithObjectSignatureOverride.getAnnotation(MethodAnnotation.class)));
180+
}
181+
158182
class ClassWithMethodAnnotation {
159183
@MethodAnnotation
160184
void annotatedMethod() {}
@@ -165,6 +189,21 @@ void nonAnnotatedMethod() {}
165189
@Hidden
166190
void hiddenAnnotatedMethod() {}
167191
}
192+
193+
class ClassWithTypicalJavaMethodNames {
194+
195+
@MethodAnnotation
196+
public void wait(String str) {}
197+
198+
@MethodAnnotation
199+
public void equals(String str) {}
200+
201+
@Override
202+
@MethodAnnotation
203+
public boolean equals(Object obj) {
204+
return super.equals(obj);
205+
}
206+
}
168207
}
169208

170209
@Retention(RetentionPolicy.RUNTIME)

0 commit comments

Comments
 (0)