Skip to content
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 @@ -83,10 +83,13 @@ private static boolean isNotHidden(AnnotatedElement element) {
return Objects.isNull(AnnotationUtil.findFirstAnnotation(Hidden.class, element));
}

private static final Set<String> 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()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,30 @@ void getAllMethods() throws Exception {
ClassWithMethodAnnotation.class.getDeclaredMethod("hiddenAnnotatedMethod"), null));
}

@Test
void methodsWithTypicalNamesButDifferentSignaturesAreNotIgnored() throws Exception {
List<MethodAndAnnotation<MethodAnnotation>> 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() {}
Expand All @@ -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)
Expand Down