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..4b2f094f0 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 @@ -57,8 +57,7 @@ public static Stream> findAnnotate methodAnnotationClass.getName()); Stream methods = Arrays.stream(ReflectionUtils.getAllDeclaredMethods(clazz)) - .filter(AnnotationScannerUtil::isMethodInSourceCode) - .filter(AnnotationScannerUtil::isNotTypicalJavaMethod) + .filter(ReflectionUtils.USER_DECLARED_METHODS::matches) .filter(AnnotationScannerUtil::isNotHidden); if (methodAnnotationClass == AllMethods.class) { @@ -72,21 +71,7 @@ public static Stream> findAnnotate .map(annotation -> new MethodAndAnnotation<>(method, annotation))); } - /** - * Check that a method was written by a developer and not generated by the compiler. - */ - private static boolean isMethodInSourceCode(Method method) { - return !method.isSynthetic(); - } - 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()); - } } 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..4c481799c 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 equalsWithObjectSignatureOverride = + ClassWithTypicalJavaMethodNames.class.getDeclaredMethod("equals", Object.class); + Method waitWithCustomSignature = + ClassWithTypicalJavaMethodNames.class.getDeclaredMethod("wait", String.class); + + assertThat(methods) + .hasSize(3) + .contains(new MethodAndAnnotation<>( + equalsWithCustomSignature, equalsWithCustomSignature.getAnnotation(MethodAnnotation.class))) + .contains(new MethodAndAnnotation<>( + waitWithCustomSignature, waitWithCustomSignature.getAnnotation(MethodAnnotation.class))) + .contains(new MethodAndAnnotation<>( + equalsWithObjectSignatureOverride, + equalsWithObjectSignatureOverride.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)