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

DRY: Unify empty J.Block creation to helper method #2638

Merged
merged 1 commit into from
Jan 12, 2023
Merged
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 @@ -112,12 +112,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