diff --git a/src/main/java/com/google/api/generator/engine/ast/AnonymousClassExpr.java b/src/main/java/com/google/api/generator/engine/ast/AnonymousClassExpr.java index e4ddc7e11d..ac0723c5e7 100644 --- a/src/main/java/com/google/api/generator/engine/ast/AnonymousClassExpr.java +++ b/src/main/java/com/google/api/generator/engine/ast/AnonymousClassExpr.java @@ -58,18 +58,26 @@ public Builder setMethods(MethodDefinition... methods) { public AnonymousClassExpr build() { AnonymousClassExpr anonymousClassExpr = autoBuild(); + // 1. The anonymous class expression should be reference types. Preconditions.checkState( TypeNode.isReferenceType(anonymousClassExpr.type()), "Anonymous class expression must be reference types."); + + // 2. Check that there are no null methods or statements. + String contextInfo = + String.format("anonymous class of type %s", anonymousClassExpr.type().reference().name()); + NodeValidator.checkNoNullElements(anonymousClassExpr.methods(), "methods", contextInfo); + NodeValidator.checkNoNullElements(anonymousClassExpr.statements(), "statements", contextInfo); + for (MethodDefinition method : anonymousClassExpr.methods()) { - // 2. Static methods are not allowed in anonymous class. + // 3. Static methods are not allowed in anonymous class. Preconditions.checkState(!method.isStatic(), "Anonymous class cannot have static methods."); - // 3. Anonymous class cannot have explicit constructors. + // 4. Anonymous class cannot have explicit constructors. Preconditions.checkState( !method.isConstructor(), "Anonymous class cannot have explicit constructors."); } - // 4. Static variable expression is not allowed unless it is final. + // 5. Static variable expression is not allowed unless it is final. for (Statement statement : anonymousClassExpr.statements()) { if (statement instanceof ExprStatement) { Expr expr = ((ExprStatement) statement).expression();