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

[ggj][ast][engx] fix: add node validator to refactor/centralize null element checks #455

Merged
merged 3 commits into from
Nov 7, 2020
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 @@ -19,7 +19,6 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import javax.annotation.Nullable;

@AutoValue
Expand Down Expand Up @@ -117,17 +116,15 @@ public MethodInvocationExpr build() {
|| methodInvocationExpr.staticReferenceType() == null,
"Only the expression reference or the static reference can be set, not both");

Preconditions.checkState(
methodInvocationExpr.arguments().stream().allMatch(e -> !Objects.isNull(e)),
String.format(
"Found null expression in arguments %s for %s",
methodInvocationExpr.arguments(), methodInvocationExpr.methodIdentifier().name()));
NodeValidator.checkNoNullElements(
methodInvocationExpr.arguments(),
"arguments",
String.format("method invocation of %s", methodInvocationExpr.methodIdentifier().name()));

Preconditions.checkState(
methodInvocationExpr.generics().stream().allMatch(e -> !Objects.isNull(e)),
String.format(
"Found null expression in generics %s for %s",
methodInvocationExpr.generics(), methodInvocationExpr.methodIdentifier().name()));
NodeValidator.checkNoNullElements(
methodInvocationExpr.generics(),
"generics",
String.format("method invocation of %s", methodInvocationExpr.methodIdentifier().name()));

return methodInvocationExpr;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@AutoValue
public abstract class NewObjectExpr implements Expr {
Expand Down Expand Up @@ -70,10 +69,8 @@ public NewObjectExpr build() {
Preconditions.checkState(
TypeNode.isReferenceType(type()), "New object expression should be reference types.");

Preconditions.checkState(
arguments().stream().allMatch(e -> !Objects.isNull(e)),
String.format(
"Found null argument for the \"new\" constructor of %s", type().reference().name()));
NodeValidator.checkNoNullElements(
arguments(), "the \"new\" constructor", type().reference().name());

// Only the case where generics() is empty and isGeneric() is false, we set isGeneric() to
// false. Otherwise, isGeneric() should be true.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.api.generator.engine.ast;

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

public class NodeValidator {
public static void checkNoNullElements(
Collection<?> collection, String fieldTypeName, String nodeContextInfo) {
Preconditions.checkState(
collection.stream().allMatch(e -> !Objects.isNull(e)),
String.format(
"Found null expression in %s %s for %s", fieldTypeName, collection, nodeContextInfo));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@AutoValue
public abstract class ReferenceConstructorExpr implements Expr {
Expand Down Expand Up @@ -77,11 +76,8 @@ public ReferenceConstructorExpr build() {
referenceConstructorExpr.type().isReferenceType(referenceConstructorExpr.type()),
"A this/super constructor must have a reference type.");

Preconditions.checkState(
arguments().stream().allMatch(e -> !Objects.isNull(e)),
String.format(
"Found null argumentfor in the \"this/super\" initialization of %s",
type().reference().name()));
NodeValidator.checkNoNullElements(
arguments(), "the \"this\" or \"super\" initialization", type().reference().name());

return referenceConstructorExpr;
}
Expand Down
Loading