From 0de5ecb687b420a5e1cd503e766aeea47f02f839 Mon Sep 17 00:00:00 2001
From: Peter Streef
Date: Sat, 24 Feb 2024 22:26:11 +0100
Subject: [PATCH 1/8] Add `AddNullMethodArgument` recipe
When changing a method we often add a nullable argument. Sometimes overriding it as to not break existing consumers. With this recipe we can migrate more quickly by adding a null value as the argument.
---
.../java/AddNullMethodArgumentTest.java | 74 +++++++++++
.../java/AddNullMethodArgument.java | 125 ++++++++++++++++++
2 files changed, 199 insertions(+)
create mode 100644 rewrite-java-test/src/test/java/org/openrewrite/java/AddNullMethodArgumentTest.java
create mode 100644 rewrite-java/src/main/java/org/openrewrite/java/AddNullMethodArgument.java
diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/AddNullMethodArgumentTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/AddNullMethodArgumentTest.java
new file mode 100644
index 00000000000..e56988b6b42
--- /dev/null
+++ b/rewrite-java-test/src/test/java/org/openrewrite/java/AddNullMethodArgumentTest.java
@@ -0,0 +1,74 @@
+/*
+ * Copyright 2020 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.openrewrite.java;
+
+import org.intellij.lang.annotations.Language;
+import org.junit.jupiter.api.Test;
+import org.openrewrite.test.RewriteTest;
+
+import static org.openrewrite.java.Assertions.java;
+
+class AddNullMethodArgumentTest implements RewriteTest {
+ @Language("java")
+ String b = """
+ class B {
+ public static void foo() {}
+ public static void foo(Integer n) {}
+ public static void foo(Integer n1, Integer n2) {}
+ public static void foo(Integer n1, Integer n2, Integer n3) {}
+ public B() {}
+ public B(Integer n) {}
+ }
+ """;
+
+ @Test
+ void addToMiddleArgument() {
+ rewriteRun(
+ spec -> spec.recipe(new AddNullMethodArgument("B foo(Integer, Integer)", 1)),
+ java(b),
+ java(
+ "public class A {{ B.foo(0, 1); }}",
+ "public class A {{ B.foo(0, null, 1); }}"
+ )
+ );
+ }
+
+ @Test
+ void addArgumentsConsecutively() {
+ rewriteRun(
+ spec -> spec.recipes(
+ new AddNullMethodArgument("B foo(Integer)", 1),
+ new AddNullMethodArgument("B foo(Integer, Integer)", 1)
+ ),
+ java(b),
+ java("public class A {{ B.foo(0); }}",
+ "public class A {{ B.foo(0, null, null); }}"
+ )
+ );
+ }
+
+ @Test
+ void addToConstructorArgument() {
+ rewriteRun(
+ spec -> spec.recipe(new AddNullMethodArgument("B ()", 0)),
+ java(b),
+ java(
+ "public class A { B b = new B(); }",
+ "public class A { B b = new B(null); }"
+ )
+ );
+ }
+}
diff --git a/rewrite-java/src/main/java/org/openrewrite/java/AddNullMethodArgument.java b/rewrite-java/src/main/java/org/openrewrite/java/AddNullMethodArgument.java
new file mode 100644
index 00000000000..3dbd87ee91c
--- /dev/null
+++ b/rewrite-java/src/main/java/org/openrewrite/java/AddNullMethodArgument.java
@@ -0,0 +1,125 @@
+/*
+ * Copyright 2020 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.openrewrite.java;
+
+import lombok.EqualsAndHashCode;
+import lombok.Value;
+import org.openrewrite.*;
+import org.openrewrite.java.search.UsesMethod;
+import org.openrewrite.java.tree.*;
+import org.openrewrite.marker.Markers;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.openrewrite.Tree.randomId;
+
+/**
+ * This recipe finds method invocations matching a method pattern and uses a zero-based argument index to determine
+ * which argument is added with a null value.
+ */
+@Value
+@EqualsAndHashCode(callSuper = false)
+public class AddNullMethodArgument extends Recipe {
+
+ /**
+ * A method pattern that is used to find matching method invocations.
+ * See {@link MethodMatcher} for details on the expression's syntax.
+ */
+ @Option(displayName = "Method pattern",
+ description = "A method pattern that is used to find matching method invocations.",
+ example = "com.yourorg.A foo(int, int)")
+ String methodPattern;
+
+ /**
+ * A zero-based index that indicates which argument will be added as null to the method invocation.
+ */
+ @Option(displayName = "Argument index",
+ description = "A zero-based index that indicates which argument will be added as null to the method invocation.",
+ example = "0")
+ int argumentIndex;
+
+ @Override
+ public String getInstanceNameSuffix() {
+ return String.format("%d in methods `%s`", argumentIndex, methodPattern);
+ }
+
+ @Override
+ public String getDisplayName() {
+ return "Delete method argument";
+ }
+
+ @Override
+ public String getDescription() {
+ return "Delete an argument from method invocations.";
+ }
+
+ @Override
+ public TreeVisitor, ExecutionContext> getVisitor() {
+ return Preconditions.check(new UsesMethod<>(methodPattern), new AddNullMethodArgumentVisitor(new MethodMatcher(methodPattern)));
+ }
+
+ private class AddNullMethodArgumentVisitor extends JavaIsoVisitor {
+ private final MethodMatcher methodMatcher;
+
+ public AddNullMethodArgumentVisitor(MethodMatcher methodMatcher) {
+ this.methodMatcher = methodMatcher;
+ }
+
+ @Override
+ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
+ J.MethodInvocation m = super.visitMethodInvocation(method, ctx);
+ return (J.MethodInvocation) visitMethodCall(m);
+ }
+
+ @Override
+ public J.NewClass visitNewClass(J.NewClass newClass, ExecutionContext ctx) {
+ J.NewClass n = super.visitNewClass(newClass, ctx);
+ return (J.NewClass) visitMethodCall(n);
+ }
+
+ private MethodCall visitMethodCall(MethodCall methodCall) {
+ MethodCall m = methodCall;
+ List originalArgs = m.getArguments();
+ if (methodMatcher.matches(m) && (long) originalArgs.size() >= argumentIndex) {
+ List args = new ArrayList<>(originalArgs);
+
+ if(args.size() == 1 && args.get(0) instanceof J.Empty) {
+ args.remove(0);
+ }
+
+ args.add(argumentIndex, new J.Literal(randomId(), args.isEmpty() ? Space.EMPTY : Space.SINGLE_SPACE, Markers.EMPTY, "null", "null", null, JavaType.Primitive.Null));
+ m = m.withArguments(args);
+
+ JavaType.Method methodType = m.getMethodType();
+ if (methodType != null) {
+ List parameterNames = new ArrayList<>(methodType.getParameterNames());
+ parameterNames.add(argumentIndex, "null");
+ List parameterTypes = new ArrayList<>(methodType.getParameterTypes());
+ parameterTypes.add(argumentIndex, JavaType.Primitive.Null);
+
+ m = m.withMethodType(methodType
+ .withParameterNames(parameterNames)
+ .withParameterTypes(parameterTypes));
+ if (m instanceof J.MethodInvocation && ((J.MethodInvocation) m).getName().getType() != null) {
+ m = ((J.MethodInvocation) m).withName(((J.MethodInvocation) m).getName().withType(m.getMethodType()));
+ }
+ }
+ }
+ return m;
+ }
+ }
+}
From af9f7196b69f0f630f86bc4264b47309ac85e0a2 Mon Sep 17 00:00:00 2001
From: Peter Streef
Date: Sat, 24 Feb 2024 22:37:05 +0100
Subject: [PATCH 2/8] fix test: Method matcher does not match null as Integer
---
.../java/org/openrewrite/java/AddNullMethodArgumentTest.java | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/AddNullMethodArgumentTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/AddNullMethodArgumentTest.java
index e56988b6b42..44abef210f4 100644
--- a/rewrite-java-test/src/test/java/org/openrewrite/java/AddNullMethodArgumentTest.java
+++ b/rewrite-java-test/src/test/java/org/openrewrite/java/AddNullMethodArgumentTest.java
@@ -51,7 +51,7 @@ void addArgumentsConsecutively() {
rewriteRun(
spec -> spec.recipes(
new AddNullMethodArgument("B foo(Integer)", 1),
- new AddNullMethodArgument("B foo(Integer, Integer)", 1)
+ new AddNullMethodArgument("B foo(Integer, null)", 1)
),
java(b),
java("public class A {{ B.foo(0); }}",
From 768bd771045f739c6494e955a794b966fa737ccd Mon Sep 17 00:00:00 2001
From: Peter Streef
Date: Sun, 25 Feb 2024 07:24:53 +0100
Subject: [PATCH 3/8] Apply suggestions from code review
Co-authored-by: Tim te Beek
Co-authored-by: Shannon Pamperl
---
.../java/AddNullMethodArgumentTest.java | 32 ++++++++++---------
.../java/AddNullMethodArgument.java | 6 ++--
2 files changed, 20 insertions(+), 18 deletions(-)
diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/AddNullMethodArgumentTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/AddNullMethodArgumentTest.java
index 44abef210f4..ae5360844e9 100644
--- a/rewrite-java-test/src/test/java/org/openrewrite/java/AddNullMethodArgumentTest.java
+++ b/rewrite-java-test/src/test/java/org/openrewrite/java/AddNullMethodArgumentTest.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2020 the original author or authors.
+ * Copyright 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,23 +22,24 @@
import static org.openrewrite.java.Assertions.java;
class AddNullMethodArgumentTest implements RewriteTest {
- @Language("java")
- String b = """
- class B {
- public static void foo() {}
- public static void foo(Integer n) {}
- public static void foo(Integer n1, Integer n2) {}
- public static void foo(Integer n1, Integer n2, Integer n3) {}
- public B() {}
- public B(Integer n) {}
- }
- """;
+ @Override
+ public void defaults(RecipeSpec spec) {
+ spec.parser(JavaParser.fromJavaVersion().dependsOn("""
+ class B {
+ public static void foo() {}
+ public static void foo(Integer n) {}
+ public static void foo(Integer n1, Integer n2) {}
+ public static void foo(Integer n1, Integer n2, Integer n3) {}
+ public B() {}
+ public B(Integer n) {}
+ }
+ """));
+ }
@Test
void addToMiddleArgument() {
rewriteRun(
spec -> spec.recipe(new AddNullMethodArgument("B foo(Integer, Integer)", 1)),
- java(b),
java(
"public class A {{ B.foo(0, 1); }}",
"public class A {{ B.foo(0, null, 1); }}"
@@ -54,8 +55,9 @@ void addArgumentsConsecutively() {
new AddNullMethodArgument("B foo(Integer, null)", 1)
),
java(b),
- java("public class A {{ B.foo(0); }}",
- "public class A {{ B.foo(0, null, null); }}"
+ java(
+ "class A {{ B.foo(0); }}",
+ "class A {{ B.foo(0, null, null); }}"
)
);
}
diff --git a/rewrite-java/src/main/java/org/openrewrite/java/AddNullMethodArgument.java b/rewrite-java/src/main/java/org/openrewrite/java/AddNullMethodArgument.java
index 3dbd87ee91c..b40b54c4b49 100644
--- a/rewrite-java/src/main/java/org/openrewrite/java/AddNullMethodArgument.java
+++ b/rewrite-java/src/main/java/org/openrewrite/java/AddNullMethodArgument.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2020 the original author or authors.
+ * Copyright 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -59,12 +59,12 @@ public String getInstanceNameSuffix() {
@Override
public String getDisplayName() {
- return "Delete method argument";
+ return "Add a `null` method argument"
}
@Override
public String getDescription() {
- return "Delete an argument from method invocations.";
+ return "Add a `null` argument to method invocations."
}
@Override
From 7d0296c29afc160d072def77900dfe6bc0d2fca2 Mon Sep 17 00:00:00 2001
From: Peter Streef
Date: Sun, 25 Feb 2024 07:28:05 +0100
Subject: [PATCH 4/8] fix compilation
---
.../java/AddNullMethodArgumentTest.java | 25 +++++++++----------
.../java/AddNullMethodArgument.java | 4 +--
2 files changed, 14 insertions(+), 15 deletions(-)
diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/AddNullMethodArgumentTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/AddNullMethodArgumentTest.java
index ae5360844e9..97077102fd7 100644
--- a/rewrite-java-test/src/test/java/org/openrewrite/java/AddNullMethodArgumentTest.java
+++ b/rewrite-java-test/src/test/java/org/openrewrite/java/AddNullMethodArgumentTest.java
@@ -15,23 +15,24 @@
*/
package org.openrewrite.java;
-import org.intellij.lang.annotations.Language;
import org.junit.jupiter.api.Test;
+import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
import static org.openrewrite.java.Assertions.java;
class AddNullMethodArgumentTest implements RewriteTest {
+
@Override
public void defaults(RecipeSpec spec) {
spec.parser(JavaParser.fromJavaVersion().dependsOn("""
class B {
- public static void foo() {}
- public static void foo(Integer n) {}
- public static void foo(Integer n1, Integer n2) {}
- public static void foo(Integer n1, Integer n2, Integer n3) {}
- public B() {}
- public B(Integer n) {}
+ static void foo() {}
+ static void foo(Integer n) {}
+ static void foo(Integer n1, Integer n2) {}
+ static void foo(Integer n1, Integer n2, Integer n3) {}
+ B() {}
+ B(Integer n) {}
}
"""));
}
@@ -41,8 +42,8 @@ void addToMiddleArgument() {
rewriteRun(
spec -> spec.recipe(new AddNullMethodArgument("B foo(Integer, Integer)", 1)),
java(
- "public class A {{ B.foo(0, 1); }}",
- "public class A {{ B.foo(0, null, 1); }}"
+ "class A {{ B.foo(0, 1); }}",
+ "class A {{ B.foo(0, null, 1); }}"
)
);
}
@@ -54,7 +55,6 @@ void addArgumentsConsecutively() {
new AddNullMethodArgument("B foo(Integer)", 1),
new AddNullMethodArgument("B foo(Integer, null)", 1)
),
- java(b),
java(
"class A {{ B.foo(0); }}",
"class A {{ B.foo(0, null, null); }}"
@@ -66,10 +66,9 @@ void addArgumentsConsecutively() {
void addToConstructorArgument() {
rewriteRun(
spec -> spec.recipe(new AddNullMethodArgument("B ()", 0)),
- java(b),
java(
- "public class A { B b = new B(); }",
- "public class A { B b = new B(null); }"
+ "class A { B b = new B(); }",
+ "class A { B b = new B(null); }"
)
);
}
diff --git a/rewrite-java/src/main/java/org/openrewrite/java/AddNullMethodArgument.java b/rewrite-java/src/main/java/org/openrewrite/java/AddNullMethodArgument.java
index b40b54c4b49..f4c155bf9cc 100644
--- a/rewrite-java/src/main/java/org/openrewrite/java/AddNullMethodArgument.java
+++ b/rewrite-java/src/main/java/org/openrewrite/java/AddNullMethodArgument.java
@@ -59,12 +59,12 @@ public String getInstanceNameSuffix() {
@Override
public String getDisplayName() {
- return "Add a `null` method argument"
+ return "Add a `null` method argument";
}
@Override
public String getDescription() {
- return "Add a `null` argument to method invocations."
+ return "Add a `null` argument to method invocations.";
}
@Override
From f92811aa06e40920c305a839945647588cbaccea Mon Sep 17 00:00:00 2001
From: Peter Streef
Date: Sun, 25 Feb 2024 08:10:37 +0100
Subject: [PATCH 5/8] add explicit cast
---
.../java/AddNullMethodArgumentTest.java | 22 +++++++++--
.../java/AddNullMethodArgument.java | 38 ++++++++++++++++---
2 files changed, 51 insertions(+), 9 deletions(-)
diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/AddNullMethodArgumentTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/AddNullMethodArgumentTest.java
index 97077102fd7..d7c8f302d3f 100644
--- a/rewrite-java-test/src/test/java/org/openrewrite/java/AddNullMethodArgumentTest.java
+++ b/rewrite-java-test/src/test/java/org/openrewrite/java/AddNullMethodArgumentTest.java
@@ -31,6 +31,8 @@ static void foo() {}
static void foo(Integer n) {}
static void foo(Integer n1, Integer n2) {}
static void foo(Integer n1, Integer n2, Integer n3) {}
+ static void foo(Integer n1, Integer n2, Integer n3, Integer n4) {}
+ static void foo(Integer n1, Integer n2, Integer n3, String n4) {}
B() {}
B(Integer n) {}
}
@@ -40,7 +42,7 @@ static void foo(Integer n1, Integer n2, Integer n3) {}
@Test
void addToMiddleArgument() {
rewriteRun(
- spec -> spec.recipe(new AddNullMethodArgument("B foo(Integer, Integer)", 1)),
+ spec -> spec.recipe(new AddNullMethodArgument("B foo(Integer, Integer)", 1, Integer.class.getName(), "n2", false)),
java(
"class A {{ B.foo(0, 1); }}",
"class A {{ B.foo(0, null, 1); }}"
@@ -52,8 +54,8 @@ void addToMiddleArgument() {
void addArgumentsConsecutively() {
rewriteRun(
spec -> spec.recipes(
- new AddNullMethodArgument("B foo(Integer)", 1),
- new AddNullMethodArgument("B foo(Integer, null)", 1)
+ new AddNullMethodArgument("B foo(Integer)", 1, Integer.class.getName(), "n2", false),
+ new AddNullMethodArgument("B foo(Integer, Integer)", 1, Integer.class.getName(), "n2", false)
),
java(
"class A {{ B.foo(0); }}",
@@ -65,11 +67,23 @@ void addArgumentsConsecutively() {
@Test
void addToConstructorArgument() {
rewriteRun(
- spec -> spec.recipe(new AddNullMethodArgument("B ()", 0)),
+ spec -> spec.recipe(new AddNullMethodArgument("B ()", 0, Integer.class.getName(), "arg", false)),
java(
"class A { B b = new B(); }",
"class A { B b = new B(null); }"
)
);
}
+
+ @Test
+ void addCastToConflictingArgumentType() {
+ rewriteRun(
+ spec -> spec.recipe(new AddNullMethodArgument("B foo(Integer,Integer,Integer)", 3, "java.lang.String", "n2", true)),
+ java(
+ "class A {{ B.foo(0, 1, 2); }}",
+ "class A {{ B.foo(0, 1, 2, (String) null); }}"
+ )
+ );
+ }
+
}
diff --git a/rewrite-java/src/main/java/org/openrewrite/java/AddNullMethodArgument.java b/rewrite-java/src/main/java/org/openrewrite/java/AddNullMethodArgument.java
index f4c155bf9cc..e3d4022fbaf 100644
--- a/rewrite-java/src/main/java/org/openrewrite/java/AddNullMethodArgument.java
+++ b/rewrite-java/src/main/java/org/openrewrite/java/AddNullMethodArgument.java
@@ -18,6 +18,8 @@
import lombok.EqualsAndHashCode;
import lombok.Value;
import org.openrewrite.*;
+import org.openrewrite.internal.ListUtils;
+import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.search.UsesMethod;
import org.openrewrite.java.tree.*;
import org.openrewrite.marker.Markers;
@@ -26,6 +28,7 @@
import java.util.List;
import static org.openrewrite.Tree.randomId;
+import static org.openrewrite.java.tree.Space.EMPTY;
/**
* This recipe finds method invocations matching a method pattern and uses a zero-based argument index to determine
@@ -52,6 +55,23 @@ public class AddNullMethodArgument extends Recipe {
example = "0")
int argumentIndex;
+ @Option(displayName = "Parameter type",
+ description = "The type of the parameter that we add the argument for.",
+ example = "java.lang.String")
+ String parameterType;
+
+ @Option(displayName = "Parameter name",
+ description = "The name of the parameter that we add the argument for.",
+ required = false,
+ example = "name")
+ @Nullable String parameterName;
+
+ @Option(displayName = "Explicit cast",
+ description = "Explicitly cast the argument to the parameter type. Useful if the method is overridden with another type.",
+ required = false,
+ example = "true")
+ @Nullable Boolean explicitCast;
+
@Override
public String getInstanceNameSuffix() {
return String.format("%d in methods `%s`", argumentIndex, methodPattern);
@@ -97,19 +117,26 @@ private MethodCall visitMethodCall(MethodCall methodCall) {
if (methodMatcher.matches(m) && (long) originalArgs.size() >= argumentIndex) {
List args = new ArrayList<>(originalArgs);
- if(args.size() == 1 && args.get(0) instanceof J.Empty) {
+ if (args.size() == 1 && args.get(0) instanceof J.Empty) {
args.remove(0);
}
- args.add(argumentIndex, new J.Literal(randomId(), args.isEmpty() ? Space.EMPTY : Space.SINGLE_SPACE, Markers.EMPTY, "null", "null", null, JavaType.Primitive.Null));
- m = m.withArguments(args);
+ Expression nullLiteral = new J.Literal(randomId(), args.isEmpty() ? Space.EMPTY : Space.SINGLE_SPACE, Markers.EMPTY, "null", "null", null, JavaType.Primitive.Null);
+ if (explicitCast == Boolean.TRUE) {
+ nullLiteral = new J.TypeCast(randomId(), Space.SINGLE_SPACE, Markers.EMPTY,
+ new J.ControlParentheses<>(randomId(), EMPTY, Markers.EMPTY,
+ new JRightPadded<>(TypeTree.build(parameterType), EMPTY, Markers.EMPTY)),
+ nullLiteral);
+ maybeAddImport(parameterType);
+ }
+ m = m.withArguments(ListUtils.insert(args, nullLiteral, argumentIndex));
JavaType.Method methodType = m.getMethodType();
if (methodType != null) {
List parameterNames = new ArrayList<>(methodType.getParameterNames());
- parameterNames.add(argumentIndex, "null");
+ parameterNames.add(argumentIndex, parameterName == null ? "arg" + argumentIndex : parameterName);
List parameterTypes = new ArrayList<>(methodType.getParameterTypes());
- parameterTypes.add(argumentIndex, JavaType.Primitive.Null);
+ parameterTypes.add(argumentIndex, JavaType.buildType(parameterType));
m = m.withMethodType(methodType
.withParameterNames(parameterNames)
@@ -121,5 +148,6 @@ private MethodCall visitMethodCall(MethodCall methodCall) {
}
return m;
}
+
}
}
From a99e581e471496fd0b8d0b4b052d5eadd03f5608 Mon Sep 17 00:00:00 2001
From: Tim te Beek
Date: Sun, 25 Feb 2024 10:40:10 +0100
Subject: [PATCH 6/8] Consistently use ListUtils.insert
---
.../openrewrite/java/AddNullMethodArgumentTest.java | 2 +-
.../org/openrewrite/java/AddNullMethodArgument.java | 11 ++++-------
2 files changed, 5 insertions(+), 8 deletions(-)
diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/AddNullMethodArgumentTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/AddNullMethodArgumentTest.java
index d7c8f302d3f..c1dcbea9795 100644
--- a/rewrite-java-test/src/test/java/org/openrewrite/java/AddNullMethodArgumentTest.java
+++ b/rewrite-java-test/src/test/java/org/openrewrite/java/AddNullMethodArgumentTest.java
@@ -81,7 +81,7 @@ void addCastToConflictingArgumentType() {
spec -> spec.recipe(new AddNullMethodArgument("B foo(Integer,Integer,Integer)", 3, "java.lang.String", "n2", true)),
java(
"class A {{ B.foo(0, 1, 2); }}",
- "class A {{ B.foo(0, 1, 2, (String) null); }}"
+ "class A {{ B.foo(0, 1, 2, (java.lang.String) null); }}"
)
);
}
diff --git a/rewrite-java/src/main/java/org/openrewrite/java/AddNullMethodArgument.java b/rewrite-java/src/main/java/org/openrewrite/java/AddNullMethodArgument.java
index e3d4022fbaf..63fba5462ab 100644
--- a/rewrite-java/src/main/java/org/openrewrite/java/AddNullMethodArgument.java
+++ b/rewrite-java/src/main/java/org/openrewrite/java/AddNullMethodArgument.java
@@ -133,14 +133,11 @@ private MethodCall visitMethodCall(MethodCall methodCall) {
JavaType.Method methodType = m.getMethodType();
if (methodType != null) {
- List parameterNames = new ArrayList<>(methodType.getParameterNames());
- parameterNames.add(argumentIndex, parameterName == null ? "arg" + argumentIndex : parameterName);
- List parameterTypes = new ArrayList<>(methodType.getParameterTypes());
- parameterTypes.add(argumentIndex, JavaType.buildType(parameterType));
-
m = m.withMethodType(methodType
- .withParameterNames(parameterNames)
- .withParameterTypes(parameterTypes));
+ .withParameterNames(ListUtils.insert(methodType.getParameterNames(),
+ parameterName == null ? "arg" + argumentIndex : parameterName, argumentIndex))
+ .withParameterTypes(ListUtils.insert(methodType.getParameterTypes(),
+ JavaType.buildType(parameterType), argumentIndex)));
if (m instanceof J.MethodInvocation && ((J.MethodInvocation) m).getName().getType() != null) {
m = ((J.MethodInvocation) m).withName(((J.MethodInvocation) m).getName().withType(m.getMethodType()));
}
From 3cf3a28ed0d9b8802a9a3b0de6ca9f13e403fcce Mon Sep 17 00:00:00 2001
From: Tim te Beek
Date: Sun, 25 Feb 2024 10:44:33 +0100
Subject: [PATCH 7/8] Use `"java.lang.Integer"` instead of
`Integer.class.getName()`
---
.../org/openrewrite/java/AddNullMethodArgumentTest.java | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/AddNullMethodArgumentTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/AddNullMethodArgumentTest.java
index c1dcbea9795..686a9a25a2d 100644
--- a/rewrite-java-test/src/test/java/org/openrewrite/java/AddNullMethodArgumentTest.java
+++ b/rewrite-java-test/src/test/java/org/openrewrite/java/AddNullMethodArgumentTest.java
@@ -42,7 +42,7 @@ static void foo(Integer n1, Integer n2, Integer n3, String n4) {}
@Test
void addToMiddleArgument() {
rewriteRun(
- spec -> spec.recipe(new AddNullMethodArgument("B foo(Integer, Integer)", 1, Integer.class.getName(), "n2", false)),
+ spec -> spec.recipe(new AddNullMethodArgument("B foo(Integer, Integer)", 1, "java.lang.Integer", "n2", false)),
java(
"class A {{ B.foo(0, 1); }}",
"class A {{ B.foo(0, null, 1); }}"
@@ -54,8 +54,8 @@ void addToMiddleArgument() {
void addArgumentsConsecutively() {
rewriteRun(
spec -> spec.recipes(
- new AddNullMethodArgument("B foo(Integer)", 1, Integer.class.getName(), "n2", false),
- new AddNullMethodArgument("B foo(Integer, Integer)", 1, Integer.class.getName(), "n2", false)
+ new AddNullMethodArgument("B foo(Integer)", 1, "java.lang.Integer", "n2", false),
+ new AddNullMethodArgument("B foo(Integer, Integer)", 1, "java.lang.Integer", "n2", false)
),
java(
"class A {{ B.foo(0); }}",
@@ -67,7 +67,7 @@ void addArgumentsConsecutively() {
@Test
void addToConstructorArgument() {
rewriteRun(
- spec -> spec.recipe(new AddNullMethodArgument("B ()", 0, Integer.class.getName(), "arg", false)),
+ spec -> spec.recipe(new AddNullMethodArgument("B ()", 0, "java.lang.Integer", "arg", false)),
java(
"class A { B b = new B(); }",
"class A { B b = new B(null); }"
From 6928bf28fede6a70a428481f096e02d58b2c5fac Mon Sep 17 00:00:00 2001
From: Peter Streef
Date: Sun, 25 Feb 2024 11:13:28 +0100
Subject: [PATCH 8/8] Dont try to shorten fully qualified
---
.../main/java/org/openrewrite/java/AddNullMethodArgument.java | 1 -
1 file changed, 1 deletion(-)
diff --git a/rewrite-java/src/main/java/org/openrewrite/java/AddNullMethodArgument.java b/rewrite-java/src/main/java/org/openrewrite/java/AddNullMethodArgument.java
index 63fba5462ab..745373d7ba6 100644
--- a/rewrite-java/src/main/java/org/openrewrite/java/AddNullMethodArgument.java
+++ b/rewrite-java/src/main/java/org/openrewrite/java/AddNullMethodArgument.java
@@ -127,7 +127,6 @@ private MethodCall visitMethodCall(MethodCall methodCall) {
new J.ControlParentheses<>(randomId(), EMPTY, Markers.EMPTY,
new JRightPadded<>(TypeTree.build(parameterType), EMPTY, Markers.EMPTY)),
nullLiteral);
- maybeAddImport(parameterType);
}
m = m.withArguments(ListUtils.insert(args, nullLiteral, argumentIndex));