openrewrite / rewrite

Automated mass refactoring of source code.
https://docs.openrewrite.org
Apache License 2.0
2.09k stars 312 forks source link

Issue discovered with `rocketmq-streams-commons/src/main/java/org/apache/rocketmq/streams/common/utils/JarUtil.java` #2192

Closed JLLeitschuh closed 2 years ago

JLLeitschuh commented 2 years ago

Problem

Describe the issue you are experiencing.

Expected behavior

Describe what you expected to see.

Example diff


     private static void writeFile(List<JarEntry> lists,
                                   JarFile jarFile, Map<String, String> jarFilePath2Content) throws IOException {
-        FileOutputStream fos = new FileOutputStream(jarFile.getName(), true);
+        FileOutputStream fos = new FileOutputStream(/*~~(java.lang.IllegalArgumentException: Expected a template that would generate exactly one statement to replace one statement, but generated 2. Template:
+new File(__P__.<java.lang.String>/*__p0__*/p())
+  org.openrewrite.java.JavaTemplate$2.maybeReplaceStatement(JavaTemplate.java:486)
+  org.openrewrite.java.JavaTemplate$2.visitMethodInvocation(JavaTemplate.java:462)
+  org.openrewrite.java.JavaTemplate$2.visitMethodInvocation(JavaTemplate.java:102)
+  org.openrewrite.java.tree.J$MethodInvocation.acceptJava(J.java:3474)
+  org.openrewrite.java.tree.J.accept(J.java:60)
+  org.openrewrite.TreeVisitor.visit(TreeVisitor.java:206)
+  org.openrewrite.TreeVisitor.visit(TreeVisitor.java:145)
+  org.openrewrite.java.JavaTemplate.withTemplate(JavaTemplate.java:520)
+  ...)~~>*/jarFile.getName(), true);
         JarOutputStream jos = new JarOutputStream(fos);
         try {
             for (JarEntry je : lists) {

Recipes in example diff:

pway99 commented 2 years ago

closing this issue as it is accounted for in https://github.com/openrewrite/rewrite/issues/2185