Search in sources :

Example 6 with ThrowExpr

use of com.google.api.generator.engine.ast.ThrowExpr in project gapic-generator-java by googleapis.

the class JavaWriterVisitorTest method writeThrowExpr_basic.

@Test
public void writeThrowExpr_basic() {
    TypeNode npeType = TypeNode.withReference(ConcreteReference.withClazz(NullPointerException.class));
    ThrowExpr throwExpr = ThrowExpr.builder().setType(npeType).build();
    throwExpr.accept(writerVisitor);
    assertEquals("throw new NullPointerException()", writerVisitor.write());
}
Also used : ThrowExpr(com.google.api.generator.engine.ast.ThrowExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) Test(org.junit.Test)

Example 7 with ThrowExpr

use of com.google.api.generator.engine.ast.ThrowExpr in project gapic-generator-java by googleapis.

the class JavaWriterVisitorTest method writeThrowExpr_messageAndCauseExpr.

@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());
}
Also used : ReferenceConstructorExpr(com.google.api.generator.engine.ast.ReferenceConstructorExpr) LogicalOperationExpr(com.google.api.generator.engine.ast.LogicalOperationExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) EnumRefExpr(com.google.api.generator.engine.ast.EnumRefExpr) ReturnExpr(com.google.api.generator.engine.ast.ReturnExpr) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) Expr(com.google.api.generator.engine.ast.Expr) TernaryExpr(com.google.api.generator.engine.ast.TernaryExpr) RelationalOperationExpr(com.google.api.generator.engine.ast.RelationalOperationExpr) UnaryOperationExpr(com.google.api.generator.engine.ast.UnaryOperationExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) InstanceofExpr(com.google.api.generator.engine.ast.InstanceofExpr) CastExpr(com.google.api.generator.engine.ast.CastExpr) ArithmeticOperationExpr(com.google.api.generator.engine.ast.ArithmeticOperationExpr) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr) LambdaExpr(com.google.api.generator.engine.ast.LambdaExpr) AssignmentOperationExpr(com.google.api.generator.engine.ast.AssignmentOperationExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ThrowExpr(com.google.api.generator.engine.ast.ThrowExpr) ThrowExpr(com.google.api.generator.engine.ast.ThrowExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) Test(org.junit.Test)

Example 8 with ThrowExpr

use of com.google.api.generator.engine.ast.ThrowExpr in project gapic-generator-java by googleapis.

the class JavaWriterVisitorTest method writeThrowExpr_basicWithMessage.

@Test
public void writeThrowExpr_basicWithMessage() {
    TypeNode npeType = TypeNode.withReference(ConcreteReference.withClazz(NullPointerException.class));
    String message = "Some message asdf";
    ThrowExpr throwExpr = ThrowExpr.builder().setType(npeType).setMessageExpr(message).build();
    throwExpr.accept(writerVisitor);
    assertEquals("throw new NullPointerException(\"Some message asdf\")", writerVisitor.write());
}
Also used : ThrowExpr(com.google.api.generator.engine.ast.ThrowExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) Test(org.junit.Test)

Example 9 with ThrowExpr

use of com.google.api.generator.engine.ast.ThrowExpr in project gapic-generator-java by googleapis.

the class ImportWriterVisitorTest method writeThrowExprImports_messageExpr.

@Test
public void writeThrowExprImports_messageExpr() {
    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).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\n"), writerVisitor.write());
}
Also used : IfStatement(com.google.api.generator.engine.ast.IfStatement) ReferenceConstructorExpr(com.google.api.generator.engine.ast.ReferenceConstructorExpr) LogicalOperationExpr(com.google.api.generator.engine.ast.LogicalOperationExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) EnumRefExpr(com.google.api.generator.engine.ast.EnumRefExpr) ReturnExpr(com.google.api.generator.engine.ast.ReturnExpr) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) Expr(com.google.api.generator.engine.ast.Expr) TernaryExpr(com.google.api.generator.engine.ast.TernaryExpr) RelationalOperationExpr(com.google.api.generator.engine.ast.RelationalOperationExpr) UnaryOperationExpr(com.google.api.generator.engine.ast.UnaryOperationExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) InstanceofExpr(com.google.api.generator.engine.ast.InstanceofExpr) ArithmeticOperationExpr(com.google.api.generator.engine.ast.ArithmeticOperationExpr) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr) LambdaExpr(com.google.api.generator.engine.ast.LambdaExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ThrowExpr(com.google.api.generator.engine.ast.ThrowExpr) ThrowExpr(com.google.api.generator.engine.ast.ThrowExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) Test(org.junit.Test)

Example 10 with ThrowExpr

use of com.google.api.generator.engine.ast.ThrowExpr in project gapic-generator-java by googleapis.

the class ImportWriterVisitorTest method writeThrowExprImports_throwExpr.

@Test
public void writeThrowExprImports_throwExpr() {
    Expr exprToThrow = MethodInvocationExpr.builder().setStaticReferenceType(TypeNode.withReference(ConcreteReference.withClazz(Statement.class))).setMethodName("createException").setReturnType(TypeNode.withExceptionClazz(Exception.class)).build();
    TypeNode ignoredExceptionType = TypeNode.withReference(ConcreteReference.withClazz(IOException.class));
    ThrowExpr throwExpr = ThrowExpr.builder().setType(ignoredExceptionType).setThrowExpr(exprToThrow).build();
    throwExpr.accept(writerVisitor);
    assertEquals(LineFormatter.lines("import com.google.api.generator.engine.ast.Statement;\n\n"), writerVisitor.write());
}
Also used : ReferenceConstructorExpr(com.google.api.generator.engine.ast.ReferenceConstructorExpr) LogicalOperationExpr(com.google.api.generator.engine.ast.LogicalOperationExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) EnumRefExpr(com.google.api.generator.engine.ast.EnumRefExpr) ReturnExpr(com.google.api.generator.engine.ast.ReturnExpr) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) Expr(com.google.api.generator.engine.ast.Expr) TernaryExpr(com.google.api.generator.engine.ast.TernaryExpr) RelationalOperationExpr(com.google.api.generator.engine.ast.RelationalOperationExpr) UnaryOperationExpr(com.google.api.generator.engine.ast.UnaryOperationExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) InstanceofExpr(com.google.api.generator.engine.ast.InstanceofExpr) ArithmeticOperationExpr(com.google.api.generator.engine.ast.ArithmeticOperationExpr) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr) LambdaExpr(com.google.api.generator.engine.ast.LambdaExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ThrowExpr(com.google.api.generator.engine.ast.ThrowExpr) EmptyLineStatement(com.google.api.generator.engine.ast.EmptyLineStatement) IfStatement(com.google.api.generator.engine.ast.IfStatement) SynchronizedStatement(com.google.api.generator.engine.ast.SynchronizedStatement) CommentStatement(com.google.api.generator.engine.ast.CommentStatement) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) Statement(com.google.api.generator.engine.ast.Statement) ThrowExpr(com.google.api.generator.engine.ast.ThrowExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

ThrowExpr (com.google.api.generator.engine.ast.ThrowExpr)11 Test (org.junit.Test)10 TypeNode (com.google.api.generator.engine.ast.TypeNode)9 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)7 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)7 AnonymousClassExpr (com.google.api.generator.engine.ast.AnonymousClassExpr)6 ArithmeticOperationExpr (com.google.api.generator.engine.ast.ArithmeticOperationExpr)6 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)6 EnumRefExpr (com.google.api.generator.engine.ast.EnumRefExpr)6 Expr (com.google.api.generator.engine.ast.Expr)6 InstanceofExpr (com.google.api.generator.engine.ast.InstanceofExpr)6 LambdaExpr (com.google.api.generator.engine.ast.LambdaExpr)6 LogicalOperationExpr (com.google.api.generator.engine.ast.LogicalOperationExpr)6 NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)6 ReferenceConstructorExpr (com.google.api.generator.engine.ast.ReferenceConstructorExpr)6 RelationalOperationExpr (com.google.api.generator.engine.ast.RelationalOperationExpr)6 ReturnExpr (com.google.api.generator.engine.ast.ReturnExpr)6 TernaryExpr (com.google.api.generator.engine.ast.TernaryExpr)6 UnaryOperationExpr (com.google.api.generator.engine.ast.UnaryOperationExpr)6 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)6