Skip to content

Commit

Permalink
[ggj][engx][ast] feat: prevent null args in ReferenceConstructorExpr (#…
Browse files Browse the repository at this point in the history
…302)

* fix: refactor and rename Bazel rules to use googleapis' toolchain

* fix: simplify Bazel rules post-refactoring

* fix: rename protoc plugin binary

* fix!: use key=value pairs for the plugin arguments (for Bazel)

* feat: prevent null args in MethodInvocationExpr

* feat: prevent null args in ReferenceConstructorExpr
  • Loading branch information
miraleung authored Sep 12, 2020
1 parent ff5606a commit a0b267a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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 @@ -64,13 +65,24 @@ public Builder setArguments(Expr... arguments) {
// private.
abstract Builder setKeywordKind(KeywordKind keywordKind);

abstract TypeNode type();

abstract ImmutableList<Expr> arguments();

abstract ReferenceConstructorExpr autoBuild();

public ReferenceConstructorExpr build() {
ReferenceConstructorExpr referenceConstructorExpr = autoBuild();
Preconditions.checkState(
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()));

return referenceConstructorExpr;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ public void validReferenceConstructorExpr_thisConstructorBasic() {
// No exception thrown, so we succeeded.
}

@Test
public void validReferenceConstructorExpr_hasArguments() {
VaporReference ref =
VaporReference.builder()
.setName("Student")
.setPakkage("com.google.example.examples.v1")
.build();
TypeNode typeNode = TypeNode.withReference(ref);
ReferenceConstructorExpr.thisBuilder()
.setType(typeNode)
.setArguments(
ValueExpr.withValue(StringObjectValue.withValue("Stu Dent")),
ValueExpr.withValue(
PrimitiveValue.builder().setType(TypeNode.INT).setValue("12345678").build()))
.build();
// No exception thrown, so we succeeded.
}

@Test
public void validReferenceConstructorExpr_superConstructorBasic() {
VaporReference ref =
Expand Down Expand Up @@ -61,4 +79,21 @@ public void invalidReferenceConstructorExpr_nullType() {
ReferenceConstructorExpr.superBuilder().setType(TypeNode.NULL).build();
});
}

@Test
public void invalidReferenceConstructorExpr_nullArgument() {
VaporReference ref =
VaporReference.builder()
.setName("Student")
.setPakkage("com.google.example.examples.v1")
.build();
TypeNode typeNode = TypeNode.withReference(ref);
assertThrows(
NullPointerException.class,
() ->
ReferenceConstructorExpr.thisBuilder()
.setType(typeNode)
.setArguments(ValueExpr.withValue(StringObjectValue.withValue("Stu Dent")), null)
.build());
}
}

0 comments on commit a0b267a

Please sign in to comment.