Skip to content

Commit

Permalink
Use TypedTree for J.ForEachLoop.Control#variable
Browse files Browse the repository at this point in the history
This allows Kotlin to use destructuring declarations as the loop variable.
  • Loading branch information
knutwannheden committed Feb 13, 2024
1 parent 6df0289 commit ce3f678
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ public void visitMethod(MethodNode method) {
// Method name might be in quotes
Space namePrefix = whitespace();
String methodName;
if(source.startsWith(method.getName(), cursor)) {
if (source.startsWith(method.getName(), cursor)) {
methodName = method.getName();
} else {
char openingQuote = source.charAt(cursor);
Expand Down Expand Up @@ -601,7 +601,7 @@ private <T> List<JRightPadded<T>> visitRightPadded(ASTNode[] nodes, @Nullable St
private Expression insideParentheses(ASTNode node, Function<Space, Expression> parenthesizedTree) {
Integer insideParenthesesLevel;
Object rawIpl = node.getNodeMetaData("_INSIDE_PARENTHESES_LEVEL");
if(rawIpl instanceof AtomicInteger) {
if (rawIpl instanceof AtomicInteger) {
// On Java 11 and newer _INSIDE_PARENTHESES_LEVEL is an AtomicInteger
insideParenthesesLevel = ((AtomicInteger) rawIpl).get();
} else {
Expand Down Expand Up @@ -697,8 +697,8 @@ public void visitArgumentlistExpression(ArgumentListExpression expression) {
}
}

if(unparsedArgs.isEmpty()) {
args.add(JRightPadded.build((Expression)new J.Empty(randomId(), whitespace(), Markers.EMPTY))
if (unparsedArgs.isEmpty()) {
args.add(JRightPadded.build((Expression) new J.Empty(randomId(), whitespace(), Markers.EMPTY))
.withAfter(omitParentheses == null ? sourceBefore(")") : EMPTY));
} else {
for (int i = 0; i < unparsedArgs.size(); i++) {
Expand Down Expand Up @@ -1304,7 +1304,7 @@ public void visitExpressionStatement(ExpressionStatement statement) {
queue.add(labeled(statement, () -> {
super.visitExpressionStatement(statement);
Object e = queue.poll();
if(e instanceof Statement) {
if (e instanceof Statement) {
return (Statement) e;
}
return new G.ExpressionStatement(randomId(), (Expression) e);
Expand Down Expand Up @@ -1362,7 +1362,7 @@ public void visitForLoop(ForStatement forLoop) {
forEachMarkers = forEachMarkers.add(new InStyleForEachLoop(randomId()));
}

JRightPadded<J.VariableDeclarations> variable = JRightPadded.build(new J.VariableDeclarations(randomId(), paramFmt,
JRightPadded<TypedTree> variable = JRightPadded.<TypedTree>build(new J.VariableDeclarations(randomId(), paramFmt,
Markers.EMPTY, emptyList(), emptyList(), paramType, null, emptyList(),
singletonList(paramName))
).withAfter(rightPad);
Expand Down
10 changes: 5 additions & 5 deletions rewrite-java/src/main/java/org/openrewrite/java/tree/J.java
Original file line number Diff line number Diff line change
Expand Up @@ -2138,13 +2138,13 @@ public static final class Control implements J {
@Getter
Markers markers;

JRightPadded<VariableDeclarations> variable;
JRightPadded<TypedTree> variable; // not `J.VariableDeclarations` to allow Kotlin destructuring declarations

public VariableDeclarations getVariable() {
public TypedTree getVariable() {
return variable.getElement();
}

public Control withVariable(VariableDeclarations variable) {
public Control withVariable(TypedTree variable) {
return getPadding().withVariable(this.variable.withElement(variable));
}

Expand Down Expand Up @@ -2187,11 +2187,11 @@ public String toString() {
public static class Padding {
private final Control t;

public JRightPadded<VariableDeclarations> getVariable() {
public JRightPadded<TypedTree> getVariable() {
return t.variable;
}

public Control withVariable(JRightPadded<VariableDeclarations> variable) {
public Control withVariable(JRightPadded<TypedTree> variable) {
return t.variable == variable ? t : new Control(t.id, t.prefix, t.markers, variable, t.iterable);
}

Expand Down

0 comments on commit ce3f678

Please sign in to comment.