Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use TypedTree for J.ForEachLoop.Control#variable #3996

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
Expand Up @@ -280,31 +280,6 @@ public void a() {

@Test
void deleteForEachBody() {
rewriteRun(
spec -> spec.recipe(toRecipe(() -> new JavaIsoVisitor<>() {
@Override
public J.ForEachLoop visitForEachLoop(J.ForEachLoop forLoop, ExecutionContext ctx) {
J.ForEachLoop f = super.visitForEachLoop(forLoop, ctx);
doAfterVisit(new DeleteStatement<>(f.getControl().getVariable()));
return f;
}
})),
java(
"""
public class A {
public void a() {
for (int i : new int[]{ 1 }) {
int j = 0;
}
}
}
"""
)
);
}

@Test
void dontDeleteForEachControl() {
rewriteRun(
spec -> spec.recipe(toRecipe(() -> new JavaIsoVisitor<>() {
@Override
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