diff --git a/src/main/java/com/google/api/generator/engine/ast/IfStatement.java b/src/main/java/com/google/api/generator/engine/ast/IfStatement.java index 0252d61185..6451ab38ee 100644 --- a/src/main/java/com/google/api/generator/engine/ast/IfStatement.java +++ b/src/main/java/com/google/api/generator/engine/ast/IfStatement.java @@ -63,6 +63,10 @@ public Builder addElseIf(Expr conditionExpr, List 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"); diff --git a/src/main/java/com/google/api/generator/engine/ast/NodeValidator.java b/src/main/java/com/google/api/generator/engine/ast/NodeValidator.java index 5eba84e936..b6eb9c6915 100644 --- a/src/main/java/com/google/api/generator/engine/ast/NodeValidator.java +++ b/src/main/java/com/google/api/generator/engine/ast/NodeValidator.java @@ -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 { @@ -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); + } }