diff --git a/rewrite-groovy/src/main/java/org/openrewrite/groovy/GroovyParserVisitor.java b/rewrite-groovy/src/main/java/org/openrewrite/groovy/GroovyParserVisitor.java index f58cf2de98f..39d3b18f619 100644 --- a/rewrite-groovy/src/main/java/org/openrewrite/groovy/GroovyParserVisitor.java +++ b/rewrite-groovy/src/main/java/org/openrewrite/groovy/GroovyParserVisitor.java @@ -1575,8 +1575,8 @@ public void visitForLoop(ForStatement forLoop) { } else { Parameter param = forLoop.getVariable(); Space paramFmt = whitespace(); - TypeTree paramType = param.getOriginType().getColumnNumber() >= 0 ? - visitTypeTree(param.getOriginType()) : null; + List modifiers = getModifiers(); + TypeTree paramType = param.getOriginType().getColumnNumber() >= 0 ? visitTypeTree(param.getOriginType()) : null; JRightPadded paramName = JRightPadded.build( new J.VariableDeclarations.NamedVariable(randomId(), whitespace(), Markers.EMPTY, new J.Identifier(randomId(), EMPTY, Markers.EMPTY, emptyList(), param.getName(), null, null), @@ -1593,7 +1593,7 @@ public void visitForLoop(ForStatement forLoop) { } JRightPadded variable = JRightPadded.build(new J.VariableDeclarations(randomId(), paramFmt, - Markers.EMPTY, emptyList(), emptyList(), paramType, null, emptyList(), + Markers.EMPTY, emptyList(), modifiers, paramType, null, emptyList(), singletonList(paramName)) ).withAfter(rightPad); @@ -2641,6 +2641,7 @@ private TypeTree visitTypeTree(ClassNode classNode, boolean inferredType) { int saveCursor = cursor; Space fmt = whitespace(); + // TODO: remove this? At least use `getModifiers` function if (cursor < source.length() && source.startsWith("def", cursor)) { cursor += 3; return new J.Identifier(randomId(), fmt, Markers.EMPTY, emptyList(), "def", diff --git a/rewrite-groovy/src/test/java/org/openrewrite/groovy/tree/ForLoopTest.java b/rewrite-groovy/src/test/java/org/openrewrite/groovy/tree/ForLoopTest.java index a1432f73256..afb2445f900 100755 --- a/rewrite-groovy/src/test/java/org/openrewrite/groovy/tree/ForLoopTest.java +++ b/rewrite-groovy/src/test/java/org/openrewrite/groovy/tree/ForLoopTest.java @@ -162,6 +162,17 @@ void multiVariableInitialization() { @Test void forEachWithColon() { + rewriteRun( + groovy( + """ + for(def i : [1, 2, 3]) {} + """ + ) + ); + } + + @Test + void forEachTypedWithColon() { rewriteRun( groovy( """ @@ -176,9 +187,7 @@ void forIn() { rewriteRun( groovy( """ - def dependenciesType = ['implementation', 'testImplementation'] - for (type in dependenciesType) { - } + for (type in ['implementation', 'testImplementation']) {} """ ) );