Skip to content

Commit

Permalink
DRY: Unify empty J.Block creation to helper method (#2638)
Browse files Browse the repository at this point in the history
Thanks @JLLeitschuh !
  • Loading branch information
JLLeitschuh authored and sambsnyd committed Jan 14, 2023
1 parent b794edd commit 38c85f2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,6 @@ public J preVisit(J tree, P p) {
}

private Statement emptyBlock() {
return new J.Block(randomId(),
Space.EMPTY,
Markers.EMPTY,
JRightPadded.build(false),
emptyList(),
Space.EMPTY
);
return J.Block.createEmptyBlock();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public J visitIf(J.If if_, ExecutionContext context) {
* ```
* The above is not valid java and will cause later processing errors.
*/
return emptyJBlock();
return J.Block.createEmptyBlock();
}
}

Expand Down Expand Up @@ -183,20 +183,6 @@ public J.Throw visitThrow(J.Throw thrown, AtomicBoolean atomicBoolean) {
return visitedCFKeyword.get();
}

/**
* An empty {@link J.Block} with no contents.
*/
private static J.Block emptyJBlock() {
return new J.Block(
Tree.randomId(),
Space.EMPTY,
Markers.EMPTY,
JRightPadded.build(false),
Collections.emptyList(),
Space.EMPTY
);
}

private static boolean isLiteralTrue(@Nullable Expression expression) {
return J.Literal.isLiteralValue(expression, Boolean.TRUE);
}
Expand Down
16 changes: 16 additions & 0 deletions rewrite-java/src/main/java/org/openrewrite/java/tree/J.java
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,11 @@ public Binary withOperator(JLeftPadded<Type> operator) {
}
}

/**
* A block of statements, enclosed in curly braces.
* <p>
* To create an empty block, use {@link #createEmptyBlock()}.
*/
@ToString
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
@EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true)
Expand Down Expand Up @@ -797,6 +802,17 @@ public Block withStatements(List<JRightPadded<Statement>> statements) {
}
}

public static J.Block createEmptyBlock() {
return new J.Block(
Tree.randomId(),
Space.EMPTY,
Markers.EMPTY,
JRightPadded.build(false),
Collections.emptyList(),
Space.EMPTY
);
}

@SelfLoathing(name = "Jonathan Leitschuh")
@LoathingOfOthers("Who didn't encode this in the model?!")
@Incubating(since = "7.25.0")
Expand Down

0 comments on commit 38c85f2

Please sign in to comment.