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 9fb29b1536a..8e199b7aa00 100644 --- a/rewrite-groovy/src/main/java/org/openrewrite/groovy/GroovyParserVisitor.java +++ b/rewrite-groovy/src/main/java/org/openrewrite/groovy/GroovyParserVisitor.java @@ -2083,9 +2083,15 @@ public TypeTree visitVariableExpressionType(VariableExpression expression) { JavaType type = typeMapping.type(staticType(((org.codehaus.groovy.ast.expr.Expression) expression))); Space prefix = whitespace(); StringBuilder keyword = new StringBuilder(); - while (!Character.isWhitespace(source.charAt(cursor))) { - keyword.append(source.charAt(cursor)); - cursor++; + + if (expression.isDynamicTyped()) { + while (!Character.isWhitespace(source.charAt(cursor))) { + keyword.append(source.charAt(cursor)); + cursor++; + } + } else { + keyword.append(expression.getOriginType().getUnresolvedName()); + cursor += keyword.length(); } J.Identifier ident = new J.Identifier(randomId(), EMPTY,