2020import org .openrewrite .TreeVisitor ;
2121import org .openrewrite .java .JavaTemplate ;
2222import org .openrewrite .java .JavaVisitor ;
23+ import org .openrewrite .java .cleanup .SimplifyBooleanExpressionVisitor ;
2324import org .openrewrite .java .tree .J ;
2425import org .openrewrite .java .tree .Statement ;
2526
@@ -35,7 +36,7 @@ public String getDisplayName() {
3536 @ Override
3637 public String getDescription () {
3738 return "Simplifies `while (true)` loops where the first statement is an `if` statement that only contains a `break`. " +
38- "The condition is inverted and moved to the loop condition for better readability." ;
39+ "The condition is inverted and moved to the loop condition for better readability." ;
3940 }
4041
4142 @ Override
@@ -45,35 +46,25 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
4546 public J visitWhileLoop (J .WhileLoop whileLoop , ExecutionContext ctx ) {
4647 J .WhileLoop wl = (J .WhileLoop ) super .visitWhileLoop (whileLoop , ctx );
4748
48- if (!(wl .getCondition ().getTree () instanceof J .Literal )) {
49+ if (!(wl .getCondition ().getTree () instanceof J .Literal ) ||
50+ !J .Literal .isLiteralValue (wl .getCondition ().getTree (), true ) ||
51+ !(wl .getBody () instanceof J .Block )) {
4952 return wl ;
5053 }
5154
52- J .Literal condition = (J .Literal ) wl .getCondition ().getTree ();
53- if (!Boolean .TRUE .equals (condition .getValue ())) {
54- return wl ;
55- }
56-
57- if (!(wl .getBody () instanceof J .Block )) {
58- return wl ;
59- }
60-
61- J .Block body = (J .Block ) wl .getBody ();
62- List <Statement > statements = body .getStatements ();
63-
55+ List <Statement > statements = ((J .Block ) wl .getBody ()).getStatements ();
6456 if (statements .isEmpty () || !(statements .get (0 ) instanceof J .If )) {
6557 return wl ;
6658 }
6759
6860 J .If ifStatement = (J .If ) statements .get (0 );
69-
7061 if (ifStatement .getElsePart () != null ) {
7162 // Actually in some cases it would be safe to proceed, but let's skip for now. Can be amended later.
7263 return wl ;
7364 }
7465
7566 Statement thenBody = ifStatement .getThenPart ();
76- J .Break breakStatement = null ;
67+ final J .Break breakStatement ;
7768 if (thenBody instanceof J .Block ) {
7869 J .Block thenBlock = (J .Block ) thenBody ;
7970 if (thenBlock .getStatements ().size () != 1 || !(thenBlock .getStatements ().get (0 ) instanceof J .Break )) {
@@ -82,21 +73,22 @@ public J visitWhileLoop(J.WhileLoop whileLoop, ExecutionContext ctx) {
8273 breakStatement = (J .Break ) thenBlock .getStatements ().get (0 );
8374 } else if (thenBody instanceof J .Break ) {
8475 breakStatement = (J .Break ) thenBody ;
76+ } else {
77+ return wl ;
8578 }
8679
8780 // Check that the break has no label
88- if (breakStatement == null || breakStatement .getLabel () != null ) {
81+ if (breakStatement .getLabel () != null ) {
8982 return wl ;
9083 }
9184
92- JavaTemplate whileTemplate = JavaTemplate .builder ("while (!(#{any()})) #{}" )
93- .build ();
94-
95- List <Statement > remainingStatements = statements .subList (1 , statements .size ());
96- J .Block newBody = body .withStatements (remainingStatements );
97-
98- return whileTemplate .apply (getCursor (), wl .getCoordinates ().replace (),
99- ifStatement .getIfCondition ().getTree (), newBody );
85+ doAfterVisit (new SimplifyBooleanExpressionVisitor ());
86+ return JavaTemplate .apply (
87+ "while (!(#{any()})) #{}" ,
88+ getCursor (),
89+ wl .getCoordinates ().replace (),
90+ ifStatement .getIfCondition ().getTree (),
91+ ((J .Block ) wl .getBody ()).withStatements (statements .subList (1 , statements .size ())));
10092 }
10193 };
10294 }
0 commit comments