diff --git a/springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/scanners/common/annotation/AnnotationScannerUtil.java b/springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/scanners/common/annotation/AnnotationScannerUtil.java index 906e69d98..9ab4ff924 100644 --- a/springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/scanners/common/annotation/AnnotationScannerUtil.java +++ b/springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/scanners/common/annotation/AnnotationScannerUtil.java @@ -83,10 +83,13 @@ private static boolean isNotHidden(AnnotatedElement element) { return Objects.isNull(AnnotationUtil.findFirstAnnotation(Hidden.class, element)); } - private static final Set typicalJavaMethods = - Set.of("clone", "equals", "finalize", "getClass", "hashCode", "notify", "notifyAll", "toString", "wait"); - private static boolean isNotTypicalJavaMethod(Method method) { - return !typicalJavaMethods.contains(method.getName()); + return !isDeclaredOnObject(method); + } + + private static boolean isDeclaredOnObject(Method method) { + return Arrays.stream(Object.class.getDeclaredMethods()) + .anyMatch(objectMethod -> objectMethod.getName().equals(method.getName()) + && Arrays.equals(objectMethod.getParameterTypes(), method.getParameterTypes())); } } diff --git a/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/scanners/common/annotation/AnnotationScannerUtilTest.java b/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/scanners/common/annotation/AnnotationScannerUtilTest.java index a3a9350a9..4f4aa7b08 100644 --- a/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/scanners/common/annotation/AnnotationScannerUtilTest.java +++ b/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/scanners/common/annotation/AnnotationScannerUtilTest.java @@ -155,6 +155,30 @@ void getAllMethods() throws Exception { ClassWithMethodAnnotation.class.getDeclaredMethod("hiddenAnnotatedMethod"), null)); } + @Test + void methodsWithTypicalNamesButDifferentSignaturesAreNotIgnored() throws Exception { + List> methods = AnnotationScannerUtil.findAnnotatedMethods( + ClassWithTypicalJavaMethodNames.class, MethodAnnotation.class) + .toList(); + + Method equalsWithCustomSignature = + ClassWithTypicalJavaMethodNames.class.getDeclaredMethod("equals", String.class); + Method equalsWithObjectSignature = + ClassWithTypicalJavaMethodNames.class.getDeclaredMethod("equals", Object.class); + Method waitWithCustomSignature = + ClassWithTypicalJavaMethodNames.class.getDeclaredMethod("wait", String.class); + + assertThat(methods) + .hasSize(2) + .contains(new MethodAndAnnotation<>( + equalsWithCustomSignature, equalsWithCustomSignature.getAnnotation(MethodAnnotation.class))) + .contains(new MethodAndAnnotation<>( + waitWithCustomSignature, waitWithCustomSignature.getAnnotation(MethodAnnotation.class))) + .doesNotContain(new MethodAndAnnotation<>( + equalsWithObjectSignature, + equalsWithObjectSignature.getAnnotation(MethodAnnotation.class))); + } + class ClassWithMethodAnnotation { @MethodAnnotation void annotatedMethod() {} @@ -165,6 +189,21 @@ void nonAnnotatedMethod() {} @Hidden void hiddenAnnotatedMethod() {} } + + class ClassWithTypicalJavaMethodNames { + + @MethodAnnotation + public void wait(String str) {} + + @MethodAnnotation + public void equals(String str) {} + + @Override + @MethodAnnotation + public boolean equals(Object obj) { + return super.equals(obj); + } + } } @Retention(RetentionPolicy.RUNTIME)