Skip to content

Commit

Permalink
Support NewClass in MethodMatcher and ChangeMethodName
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed May 6, 2024
1 parent ba548dc commit 415bc28
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,38 @@ public void test() {
);
}

@Test
void changeMethodNameForOverriddenMethodAnonymousClass() {
rewriteRun(
spec -> spec.recipe(new ChangeMethodName("com.abc.B singleArg(String)", "bar", true, null)),
java(b, SourceSpec::skip),
java(
"""
package com.abc;
class A {
public void test() {
new B() {
@Override
public void singleArg(String s) {}
};
}
}
""",
"""
package com.abc;
class A {
public void test() {
new B() {
@Override
public void bar(String s) {}
};
}
}
"""
)
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite/issues/1215")
@SuppressWarnings("MethodMayBeStatic")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,12 @@ public J visit(@Nullable Tree tree, ExecutionContext ctx) {
@Override
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
J.MethodDeclaration m = super.visitMethodDeclaration(method, ctx);

J.NewClass newClass = getCursor().firstEnclosing(J.NewClass.class);
J.ClassDeclaration classDecl = getCursor().firstEnclosing(J.ClassDeclaration.class);
if (classDecl == null) {
return m;
}
if (methodMatcher.matches(method, classDecl)) {
boolean methodMatches = newClass != null && methodMatcher.matches(method, newClass) ||
classDecl != null && methodMatcher.matches(method, classDecl);
if (methodMatches) {
JavaType.Method type = m.getMethodType();
if (type != null) {
type = type.withName(newMethodName);
Expand Down
27 changes: 27 additions & 0 deletions rewrite-java/src/main/java/org/openrewrite/java/MethodMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,33 @@ public boolean matches(J.MethodDeclaration method, J.ClassDeclaration enclosing)
return matchesParameterTypes(parameterTypes);
}

public boolean matches(J.MethodDeclaration method, J.NewClass enclosing) {
if (enclosing.getType() == null) {
return false;
}

// aspectJUtils does not support matching classes separated by packages.
// [^.]* is the product of a fully wild card match for a method. `* foo()`
boolean matchesTargetType = (targetTypePattern != null && "[^.]*".equals(targetTypePattern.pattern()))
|| TypeUtils.isAssignableTo(targetType, enclosing.getType());
if (!matchesTargetType) {
return false;
}

if (method.getMethodType() != null && !matchesMethodName(method.getMethodType().getName())) {
return false;
}

List<JavaType> parameterTypes =
method
.getParameters()
.stream()
.map(MethodMatcher::variableDeclarationsType)
.filter(Objects::nonNull)
.collect(Collectors.toList());
return matchesParameterTypes(parameterTypes);
}

@Nullable
private static JavaType variableDeclarationsType(Statement v) {
if (v instanceof J.VariableDeclarations) {
Expand Down

0 comments on commit 415bc28

Please sign in to comment.