Skip to content

Commit

Permalink
[TypeUtils.findOverridenMethod] Skip found methods with default acces…
Browse files Browse the repository at this point in the history
…s and not same package (#3603)

* fix[TypeUtils.findOverridenMethod]: skip found methods with default access and different package

* Apply suggestions from code review

* Add test and simplify logic for override check

---------

Co-authored-by: Tim te Beek <[email protected]>
Co-authored-by: Tim te Beek <[email protected]>
  • Loading branch information
3 people authored Oct 10, 2023
1 parent acb7c2b commit 8c8c9ad
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void isOverrideBasicInheritance() {
java(
"""
class Superclass {
void foo();
void foo() { }
}
"""
),
Expand All @@ -80,6 +80,33 @@ class Clazz extends Superclass {
);
}

@Test
void isOverrideOnlyVisible() {
rewriteRun(
java(
"""
package foo;
public class Superclass {
void foo() { }
}
"""
),
java(
"""
package bar;
import foo.Superclass;
class Clazz extends Superclass {
public void foo() { }
}
""",
s -> s.afterRecipe(cu -> {
var fooMethodType = ((J.MethodDeclaration) cu.getClasses().get(0).getBody().getStatements().get(0)).getMethodType();
assertThat(TypeUtils.findOverriddenMethod(fooMethodType)).isEmpty();
})
)
);
}

@Issue("https://github.com/openrewrite/rewrite/issues/1759")
@Test
void isOverrideParameterizedInterface() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,12 @@ public static Optional<JavaType.Method> findOverriddenMethod(@Nullable JavaType.
}
}
}
return methodResult.filter(m -> !m.getFlags().contains(Flag.Private) && !m.getFlags().contains(Flag.Static));

return methodResult
.filter(m -> !m.getFlags().contains(Flag.Private))
.filter(m -> !m.getFlags().contains(Flag.Static))
// If access level is default then check if subclass package is the same from parent class
.filter(m -> m.getFlags().contains(Flag.Public) || m.getDeclaringType().getPackageName().equals(dt.getPackageName()));
}

public static Optional<JavaType.Method> findDeclaredMethod(@Nullable JavaType.FullyQualified clazz, String name, List<JavaType> argumentTypes) {
Expand Down

0 comments on commit 8c8c9ad

Please sign in to comment.