Skip to content
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
29 changes: 19 additions & 10 deletions src/main/java/org/openrewrite/staticanalysis/EmptyBlockVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.format.ShiftFormat;
import org.openrewrite.java.style.EmptyBlockStyle;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.Space;
import org.openrewrite.java.tree.Statement;
import org.openrewrite.java.tree.*;
import org.openrewrite.marker.Markers;

import java.util.ArrayList;
Expand All @@ -48,9 +45,15 @@ public class EmptyBlockVisitor<P> extends JavaIsoVisitor<P> {
public J.WhileLoop visitWhileLoop(J.WhileLoop whileLoop, P p) {
J.WhileLoop w = super.visitWhileLoop(whileLoop, p);

if (Boolean.TRUE.equals(emptyBlockStyle.getLiteralWhile()) && isEmptyBlock(w.getBody())) {
J.Block body = (J.Block) w.getBody();
w = continueStatement.apply(updateCursor(w), body.getCoordinates().lastStatement());
if (isEmptyBlock(w.getBody())) {
if (EmptyBlockStyle.BlockPolicy.STATEMENT == emptyBlockStyle.getBlockPolicy()) {
w = continueStatement.apply(updateCursor(w), ((J.Block) w.getBody()).getCoordinates().lastStatement());
}
if (EmptyBlockStyle.BlockPolicy.TEXT == emptyBlockStyle.getBlockPolicy()) {
J.Block body = (J.Block) w.getBody();
TextComment textComment = new TextComment(false, " do nothing", "\n" + body.getEnd().getIndent(), Markers.EMPTY);
w = autoFormat(w.withBody(body.withEnd(body.getEnd().withComments(ListUtils.concat(body.getEnd().getComments(), textComment)))), p);
}
}

return w;
Expand All @@ -60,9 +63,15 @@ public J.WhileLoop visitWhileLoop(J.WhileLoop whileLoop, P p) {
public J.DoWhileLoop visitDoWhileLoop(J.DoWhileLoop doWhileLoop, P p) {
J.DoWhileLoop w = super.visitDoWhileLoop(doWhileLoop, p);

if (Boolean.TRUE.equals(emptyBlockStyle.getLiteralWhile()) && isEmptyBlock(w.getBody())) {
J.Block body = (J.Block) w.getBody();
w = continueStatement.apply(updateCursor(w), body.getCoordinates().lastStatement());
if (isEmptyBlock(w.getBody())) {
if (EmptyBlockStyle.BlockPolicy.STATEMENT == emptyBlockStyle.getBlockPolicy()) {
w = continueStatement.apply(updateCursor(w), ((J.Block) w.getBody()).getCoordinates().lastStatement());
}
if (EmptyBlockStyle.BlockPolicy.TEXT == emptyBlockStyle.getBlockPolicy()) {
J.Block body = (J.Block) w.getBody();
TextComment textComment = new TextComment(false, " do nothing", "\n" + body.getEnd().getIndent(), Markers.EMPTY);
w = autoFormat(w.withBody(body.withEnd(body.getEnd().withComments(ListUtils.concat(body.getEnd().getComments(), textComment)))), p);
}
}

return w;
Expand Down
94 changes: 89 additions & 5 deletions src/test/java/org/openrewrite/staticanalysis/EmptyBlockTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.java.JavaParser;
import org.openrewrite.java.style.Checkstyle;
import org.openrewrite.java.style.EmptyBlockStyle;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static java.util.Collections.singleton;
import static org.openrewrite.java.Assertions.java;

@SuppressWarnings({
Expand Down Expand Up @@ -215,29 +219,109 @@ public void foo() {
}

@Test
void emptyLoops() {
void emptyWhileDefault() { // Checkstyle.emptyBlock() uses `BlockPolicy` `TEXT`
rewriteRun(
//language=java
java(
"""
public class A {
public void foo() {
while(true) {
while (true) {
}
do {
} while(true);
} while (true);
}
}
""",
"""
public class A {
public void foo() {
while(true) {
while (true) {
// do nothing
}
do {
// do nothing
} while (true);
}
}
"""
)
);
}

@Test
void whileWithContinueDefault() {
rewriteRun(
//language=java
java( // No change expected
"""
public class A {
public void foo() {
while (true) {
continue;
}
do {
continue;
} while (true);
}
}
"""
)
);
}

@Test
void whileWithCommentDefault() {
rewriteRun(
//language=java
java( // No change expected
"""
public class A {
public void foo() {
while (true) {
// do nothing
}
do {
// do nothing
} while (true);
}
}
"""
)
);
}

@Test
void emptyWhileStyleStatement() {
rewriteRun(
spec -> {
spec.parser(JavaParser.fromJavaVersion().styles(
singleton(Checkstyle.defaults()
.withStyles(singleton(Checkstyle.emptyBlock()
.withBlockPolicy(EmptyBlockStyle.BlockPolicy.STATEMENT))))
));
},
//language=java
java(
"""
public class A {
public void foo() {
while (true) {
}
do {
} while (true);
}
}
""",
"""
public class A {
public void foo() {
while (true) {
continue;
}
do {
continue;
} while(true);
} while (true);
}
}
"""
Expand Down