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

feat(ast): Add support for Throwable causes [ggj] #801

Merged
merged 6 commits into from
Jul 30, 2021
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
17 changes: 17 additions & 0 deletions src/main/java/com/google/api/generator/engine/ast/ThrowExpr.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public abstract class ThrowExpr implements Expr {
@Nullable
public abstract Expr messageExpr();

@Nullable
public abstract Expr causeExpr();

@Override
public void accept(AstNodeVisitor visitor) {
visitor.visit(this);
Expand All @@ -47,22 +50,36 @@ public Builder setMessageExpr(String message) {

public abstract Builder setMessageExpr(Expr expr);

public abstract Builder setCauseExpr(Expr expr);

// Private.
abstract TypeNode type();

abstract Expr messageExpr();

abstract Expr causeExpr();

abstract ThrowExpr autoBuild();

public ThrowExpr build() {
Preconditions.checkState(
TypeNode.isExceptionType(type()),
String.format("Type %s must be an exception type", type()));

if (messageExpr() != null) {
Preconditions.checkState(
messageExpr().type().equals(TypeNode.STRING),
String.format("Message expression type must be a string for exception %s", type()));
}

if (causeExpr() != null) {
Preconditions.checkState(
TypeNode.THROWABLE.reference().isSupertypeOrEquals(causeExpr().type().reference()),
String.format(
"Cause expression type must be a subclass of Throwable, but found %s",
causeExpr().type()));
}

return autoBuild();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ public enum TypeKind {
public static final TypeNode STRING = withReference(ConcreteReference.withClazz(String.class));
public static final TypeNode VOID_OBJECT = withReference(ConcreteReference.withClazz(Void.class));

public static final TypeNode THROWABLE =
withReference(ConcreteReference.withClazz(Throwable.class));

public static final TypeNode DEPRECATED =
withReference(ConcreteReference.withClazz(Deprecated.class));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,9 @@ public void visit(ThrowExpr throwExpr) {
if (throwExpr.messageExpr() != null) {
throwExpr.messageExpr().accept(this);
}
if (throwExpr.causeExpr() != null) {
throwExpr.causeExpr().accept(this);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,13 @@ public void visit(ThrowExpr throwExpr) {
if (throwExpr.messageExpr() != null) {
throwExpr.messageExpr().accept(this);
}
if (throwExpr.causeExpr() != null) {
if (throwExpr.messageExpr() != null) {
buffer.append(COMMA);
space();
}
throwExpr.causeExpr().accept(this);
}
rightParen();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,65 @@ public void createThrowExpr_badMessageExpr() {
IllegalStateException.class,
() -> ThrowExpr.builder().setType(npeType).setMessageExpr(messageExpr).build());
}

@Test
public void createThrowExpr_causeExpr() {
TypeNode npeType =
TypeNode.withReference(ConcreteReference.withClazz(NullPointerException.class));
ThrowExpr.builder()
.setType(npeType)
.setCauseExpr(
NewObjectExpr.builder()
.setType(TypeNode.withReference(ConcreteReference.withClazz(Throwable.class)))
.build())
.build();
// Successfully created a ThrowExpr.
}

@Test
public void createThrowExpr_causeExpr_throwableSubtype() {
TypeNode npeType =
TypeNode.withReference(ConcreteReference.withClazz(NullPointerException.class));
ThrowExpr.builder()
.setType(npeType)
.setCauseExpr(
NewObjectExpr.builder()
.setType(TypeNode.withExceptionClazz(IllegalStateException.class))
.build())
.build();
// Successfully created a ThrowExpr.
}

@Test
public void createThrowExpr_causeExpr_onThrowableSubtype() {
TypeNode npeType =
TypeNode.withReference(ConcreteReference.withClazz(NullPointerException.class));
assertThrows(
IllegalStateException.class,
() ->
ThrowExpr.builder()
.setType(npeType)
.setCauseExpr(NewObjectExpr.builder().setType(TypeNode.STRING).build())
.build());
}

@Test
public void createThrowExpr_messageAndCauseExpr() {
TypeNode npeType =
TypeNode.withReference(ConcreteReference.withClazz(NullPointerException.class));
Expr messageExpr =
MethodInvocationExpr.builder()
.setMethodName("foobar")
.setReturnType(TypeNode.STRING)
.build();
ThrowExpr.builder()
.setType(npeType)
.setMessageExpr(messageExpr)
.setCauseExpr(
NewObjectExpr.builder()
.setType(TypeNode.withReference(ConcreteReference.withClazz(Throwable.class)))
.build())
.build();
// Successfully created a ThrowExpr.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import com.google.common.base.Function;
import com.google.common.base.Strings;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -475,6 +476,42 @@ public void writeThrowExprImports_messageExpr() {
writerVisitor.write());
}

@Test
public void writeThrowExprImports_messageAndCauseExpr() {
TypeNode npeType = TypeNode.withExceptionClazz(NullPointerException.class);
Expr messageExpr =
MethodInvocationExpr.builder()
.setStaticReferenceType(
TypeNode.withReference(ConcreteReference.withClazz(IfStatement.class)))
.setMethodName("conditionExpr")
.setReturnType(TypeNode.withReference(ConcreteReference.withClazz(Expr.class)))
.build();

messageExpr =
MethodInvocationExpr.builder()
.setExprReferenceExpr(messageExpr)
.setMethodName("foobar")
.setReturnType(TypeNode.STRING)
.build();
ThrowExpr throwExpr =
ThrowExpr.builder()
.setType(npeType)
.setMessageExpr(messageExpr)
.setCauseExpr(
NewObjectExpr.builder()
.setType(TypeNode.withExceptionClazz(FileNotFoundException.class))
.build())
.build();

throwExpr.accept(writerVisitor);
assertEquals(
LineFormatter.lines(
"import com.google.api.generator.engine.ast.Expr;\n",
"import com.google.api.generator.engine.ast.IfStatement;\n",
"import java.io.FileNotFoundException;\n\n"),
writerVisitor.write());
}

@Test
public void writeInstanceofExprImports_basic() {
TypeNode exprType = TypeNode.withReference(ConcreteReference.withClazz(Expr.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,22 @@ public void writeThrowExpr_basicWithMessage() {
assertEquals("throw new NullPointerException(\"Some message asdf\")", writerVisitor.write());
}

@Test
public void writeThrowExpr_basicWithCause() {
TypeNode npeType =
TypeNode.withReference(ConcreteReference.withClazz(NullPointerException.class));
ThrowExpr throwExpr =
ThrowExpr.builder()
.setType(npeType)
.setCauseExpr(
NewObjectExpr.builder()
.setType(TypeNode.withReference(ConcreteReference.withClazz(Throwable.class)))
.build())
.build();
throwExpr.accept(writerVisitor);
assertEquals("throw new NullPointerException(new Throwable())", writerVisitor.write());
}

@Test
public void writeThrowExpr_messageExpr() {
TypeNode npeType = TypeNode.withExceptionClazz(NullPointerException.class);
Expand All @@ -1055,6 +1071,29 @@ public void writeThrowExpr_messageExpr() {
assertEquals("throw new NullPointerException(foobar())", writerVisitor.write());
}

@Test
public void writeThrowExpr_messageAndCauseExpr() {
TypeNode npeType = TypeNode.withExceptionClazz(NullPointerException.class);
Expr messageExpr =
MethodInvocationExpr.builder()
.setMethodName("foobar")
.setReturnType(TypeNode.STRING)
.build();
ThrowExpr throwExpr =
ThrowExpr.builder()
.setType(npeType)
.setMessageExpr(messageExpr)
.setCauseExpr(
NewObjectExpr.builder()
.setType(TypeNode.withReference(ConcreteReference.withClazz(Throwable.class)))
.build())
.build();

throwExpr.accept(writerVisitor);
assertEquals(
"throw new NullPointerException(foobar(), new Throwable())", writerVisitor.write());
}

@Test
public void writeInstanceofExpr() {
Variable variable = Variable.builder().setName("x").setType(TypeNode.STRING).build();
Expand Down