Skip to content
Merged
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 @@ -304,6 +304,129 @@ public void test() {
);
}

@Issue("https://github.com/openrewrite/rewrite/issues/1804")
@Test
void constructorToStaticMethod() {
rewriteRun(
spec -> spec.recipes(
new ChangeMethodTargetToStatic("a.A <constructor>(String)", "b.B", "b.B", null, false),
new ChangeMethodName("b.B A(String)", "foo", null, null)
),
java(
"""
package a;
public class A {
public A(String s) {}
}
"""
),
java(
"""
package b;
public class B {
public static B foo(String s) { return null; }
}
"""
),
java(
"""
import a.A;
class C {
public void test() {
new A("hello");
}
}
""",
"""
import b.B;

class C {
public void test() {
B.foo("hello");
}
}
"""
)
);
}

@Issue("https://github.com/openrewrite/rewrite/issues/1804")
@Test
void constructorToStaticMethodWithReturnType() {
rewriteRun(
spec -> spec.recipe(
new ChangeMethodTargetToStatic("a.A <constructor>(String)", "b.B", "b.B", null, false)
),
java(
"""
package a;
public class A {
public A(String s) {}
}
"""
),
java(
"""
package b;
public class B {
public static B A(String s) { return null; }
}
"""
),
java(
"""
import a.A;
class C {
public void test() {
new A("hello");
}
}
""",
"""
import b.B;

class C {
public void test() {
B.A("hello");
}
}
"""
)
);
}

@Issue("https://github.com/openrewrite/rewrite/issues/1804")
@Test
void constructorWithAnonymousBodyNotChanged() {
rewriteRun(
spec -> spec.recipe(
new ChangeMethodTargetToStatic("a.A <constructor>(String)", "b.B", "b.B", null, false)
),
java(
"""
package a;
public class A {
public A(String s) {}
public void doSomething() {}
}
"""
),
java(
"""
import a.A;
class C {
public void test() {
new A("hello") {
@Override
public void doSomething() {}
};
}
}
"""
)
);
}

@Disabled
@Issue("https://github.com/openrewrite/rewrite/issues/3085")
@SuppressWarnings("ResultOfMethodCallIgnored")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
return matchUnknown ? visitor : Preconditions.check(new UsesMethod<>(methodPattern, matchOverrides), visitor);
}

private class ChangeMethodTargetToStaticVisitor extends JavaIsoVisitor<ExecutionContext> {
private class ChangeMethodTargetToStaticVisitor extends JavaVisitor<ExecutionContext> {
private final MethodMatcher methodMatcher;
private final boolean matchUnknownTypes;
private final JavaType.FullyQualified classType = JavaType.ShallowClass.build(fullyQualifiedTargetTypeName);
Expand Down Expand Up @@ -136,8 +136,8 @@ private JavaType.Method transformMethodType(JavaType.Method methodType) {
}

@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = super.visitMethodInvocation(method, ctx);
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);
Expression select = method.getSelect();
if (!isAlreadyStaticCallOnTargetType(select, method) &&
methodMatcher.matches(method, matchUnknownTypes)) {
Expand Down Expand Up @@ -170,8 +170,8 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
}

@Override
public J.MemberReference visitMemberReference(J.MemberReference memberRef, ExecutionContext ctx) {
J.MemberReference m = super.visitMemberReference(memberRef, ctx);
public J visitMemberReference(J.MemberReference memberRef, ExecutionContext ctx) {
J.MemberReference m = (J.MemberReference) super.visitMemberReference(memberRef, ctx);
Expression containing = memberRef.getContaining();
if (!isAlreadyStaticCallOnTargetType(containing, memberRef) &&
methodMatcher.matches(memberRef)) {
Expand All @@ -195,5 +195,62 @@ public J.MemberReference visitMemberReference(J.MemberReference memberRef, Execu
}
return m;
}

@Override
public J visitNewClass(J.NewClass newClass, ExecutionContext ctx) {
J.NewClass n = (J.NewClass) super.visitNewClass(newClass, ctx);
if (n.getBody() != null || n.getEnclosing() != null) {
return n;
}
if (methodMatcher.matches(n)) {
String methodName;
JavaType.Method transformedType = null;
if (n.getConstructorType() != null) {
methodName = n.getConstructorType().getConstructorName();
if (methodName == null) {
return n;
}
maybeRemoveImport(n.getConstructorType().getDeclaringType());
transformedType = transformMethodType(n.getConstructorType())
.withName(methodName);
} else {
return n;
}

maybeAddImport(fullyQualifiedTargetTypeName, !matchUnknownTypes);

J.Identifier selectId = new J.Identifier(
randomId(),
n.getPrefix(),
Markers.EMPTY,
emptyList(),
classType.getClassName(),
classType,
null
);

J.Identifier nameId = new J.Identifier(
randomId(),
Space.EMPTY,
Markers.EMPTY,
emptyList(),
methodName,
transformedType,
null
);

return new J.MethodInvocation(
randomId(),
Space.EMPTY,
Markers.EMPTY,
JRightPadded.build(selectId),
null,
nameId,
n.getPadding().getArguments(),
transformedType
);
}
return n;
}
}
}