Skip to content

Commit

Permalink
TypeUtils#isOfType() should not compare annotations on methods
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Jan 24, 2025
1 parent d71f13c commit 3f3f5ce
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.EnumSet;
import java.util.function.Consumer;

import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;
Expand Down Expand Up @@ -233,6 +234,29 @@ public J.VariableDeclarations.NamedVariable visitVariable(J.VariableDeclarations
);
}

@Test
void methodWithAnnotationsIsOfType() {
rewriteRun(
java(
"""
class Test {
@Deprecated
void foo() {}
}
""",
spec -> spec.afterRecipe(cu -> new JavaIsoVisitor<>() {
@Override
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Object o) {
assertThat(TypeUtils.isOfType(method.getMethodType(), method.getMethodType())).isTrue();
assertThat(TypeUtils.isOfType(method.getMethodType().withAnnotations(emptyList()), method.getMethodType())).isTrue();
assertThat(TypeUtils.isOfType(method.getMethodType(), method.getMethodType().withAnnotations(emptyList()))).isTrue();
return method;
}
}.visit(cu, new InMemoryExecutionContext()))
)
);
}

@Test
void isParameterizedTypeOfType() {
rewriteRun(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,9 @@ public static boolean isOfType(@Nullable JavaType type1, @Nullable JavaType type
JavaType.Method method1 = (JavaType.Method) type1;
JavaType.Method method2 = (JavaType.Method) type2;
if (!method1.getName().equals(method2.getName()) ||
method1.getFlags().size() != method2.getFlags().size() ||
!method1.getFlags().containsAll(method2.getFlags()) ||
method1.getFlagsBitMap() != method2.getFlagsBitMap() ||
!TypeUtils.isOfType(method1.getDeclaringType(), method2.getDeclaringType()) ||
!TypeUtils.isOfType(method1.getReturnType(), method2.getReturnType()) ||
method1.getAnnotations().size() != method2.getAnnotations().size() ||
method1.getThrownExceptions().size() != method2.getThrownExceptions().size() ||
method1.getParameterTypes().size() != method2.getParameterTypes().size()) {
return false;
Expand All @@ -159,11 +157,6 @@ public static boolean isOfType(@Nullable JavaType type1, @Nullable JavaType type
return false;
}
}
for (int index = 0; index < method1.getAnnotations().size(); index++) {
if (!TypeUtils.isOfType(method1.getAnnotations().get(index), method2.getAnnotations().get(index))) {
return false;
}
}
return true;
}
return type1.equals(type2);
Expand Down

0 comments on commit 3f3f5ce

Please sign in to comment.