diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/JavaTemplateTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/JavaTemplateTest.java index 1d0824ab3e4..cc2041f7236 100755 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/JavaTemplateTest.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/JavaTemplateTest.java @@ -1390,7 +1390,7 @@ void replaceVariableDeclarationWithFinalVar() { @Override public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) { J.VariableDeclarations vd = super.visitVariableDeclarations(multiVariable, ctx); - if (TypeUtils.isString(vd.getType())) { + if (TypeUtils.isString(vd.getType()) && "String".equals(((J.Identifier) vd.getTypeExpression()).getSimpleName())) { JavaCoordinates coordinates = vd.getCoordinates().replace(); return JavaTemplate.builder("final var #{}") .contextSensitive() diff --git a/rewrite-java/src/main/java/org/openrewrite/java/internal/template/BlockStatementTemplateGenerator.java b/rewrite-java/src/main/java/org/openrewrite/java/internal/template/BlockStatementTemplateGenerator.java index f586b011fcb..771ef4466d1 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/internal/template/BlockStatementTemplateGenerator.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/internal/template/BlockStatementTemplateGenerator.java @@ -25,6 +25,7 @@ import org.openrewrite.Cursor; import org.openrewrite.SourceFile; import org.openrewrite.Tree; +import org.openrewrite.internal.ListUtils; import org.openrewrite.java.JavaIsoVisitor; import org.openrewrite.java.JavaVisitor; import org.openrewrite.java.tree.*; @@ -409,7 +410,7 @@ private void contextTemplate(Cursor cursor, J prior, StringBuilder before, Strin } else if (j instanceof J.ForEachLoop.Control) { J.ForEachLoop.Control c = (J.ForEachLoop.Control) j; if (referToSameElement(prior, c.getVariable())) { - after.append(" = /*" + STOP_COMMENT + "/*").append(c.getIterable().printTrimmed(cursor)); + after.append(" = /*" + STOP_COMMENT + "*/").append(c.getIterable().printTrimmed(cursor)); } else if (referToSameElement(prior, c.getIterable())) { before.insert(0, "Object __b" + cursor.getPathAsStream().count() + "__ ="); after.append(";"); @@ -818,6 +819,20 @@ public J visitMethodInvocation(J.MethodInvocation method, Integer integer) { } return mi; } + + @Override + public J visitVariableDeclarations(J.VariableDeclarations multiVariable, Integer integer) { + List variables = multiVariable.getVariables(); + for (J.VariableDeclarations.NamedVariable variable : variables) { + J.VariableDeclarations.NamedVariable.Padding padding = variable.getPadding(); + if (padding.getInitializer() != null && stopCommentExists(padding.getInitializer().getBefore().getComments())) { + // Split the variable declarations at the variable with the `STOP_COMMENT` & trim off initializer + List vars = variables.subList(0, variables.indexOf(variable) + 1); + return multiVariable.withVariables(ListUtils.mapLast(vars, v -> v.withInitializer(null))); + } + } + return super.visitVariableDeclarations(multiVariable, integer); + } } } }