From 4fe7d3fa660e6a2080689429bab312532c6f4af1 Mon Sep 17 00:00:00 2001 From: Mira Leung Date: Fri, 6 Nov 2020 19:44:07 -0800 Subject: [PATCH] [ggj][ast][engx] fix: validate non-null elements for IfStatement (#456) * 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 --- .../com/google/api/generator/engine/ast/IfStatement.java | 4 ++++ .../google/api/generator/engine/ast/NodeValidator.java | 8 ++++++++ 2 files changed, 12 insertions(+) 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); + } }