Skip to content

Commit

Permalink
[ggj][ast][engx] fix: validate non-null elements for IfStatement (#456)
Browse files Browse the repository at this point in the history
* fix: swap assertEquals args in JavaWriterVisitorTest to match (expected, actusl) order

* fix: swap assertEquals args in ImportWriterVisitorTest to match (expected, actusl) order

* fix: add node validator to refactor/centralize null element checks

* fix: validate non-null elements for IfStatement
  • Loading branch information
miraleung authored Nov 7, 2020
1 parent 7bfe9d5 commit 4fe7d3f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ public Builder addElseIf(Expr conditionExpr, List<Statement> body) {

public IfStatement build() {
IfStatement ifStatement = autoBuild();
NodeValidator.checkNoNullElements(ifStatement.body(), "body", "if-statement");
NodeValidator.checkNoNullElements(ifStatement.elseIfs(), "else-ifs", "if-statement");
NodeValidator.checkNoNullElements(ifStatement.elseBody(), "else-body", "if-statement");

Preconditions.checkState(
ifStatement.conditionExpr().type().equals(TypeNode.BOOLEAN),
"If-condition must be a boolean-typed expression");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import com.google.common.base.Preconditions;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;

public class NodeValidator {
Expand All @@ -26,4 +27,11 @@ public static void checkNoNullElements(
String.format(
"Found null expression in %s %s for %s", fieldTypeName, collection, nodeContextInfo));
}

public static void checkNoNullElements(
Map<?, ?> map, String fieldTypeName, String nodeContextInfo) {
checkNoNullElements(map.keySet(), String.format("key of %s", fieldTypeName), nodeContextInfo);
checkNoNullElements(
map.values(), String.format("values of %s", fieldTypeName), nodeContextInfo);
}
}

0 comments on commit 4fe7d3f

Please sign in to comment.