Search in sources :

Example 1 with ThrowExpr

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

the class JavaWriterVisitorTest method writeThrowExpr_messageExpr.

@Test
public void writeThrowExpr_messageExpr() {
    TypeNode npeType = TypeNode.withExceptionClazz(NullPointerException.class);
    Expr messageExpr = MethodInvocationExpr.builder().setMethodName("foobar").setReturnType(TypeNode.STRING).build();
    ThrowExpr throwExpr = ThrowExpr.builder().setType(npeType).setMessageExpr(messageExpr).build();
    throwExpr.accept(writerVisitor);
    assertEquals("throw new NullPointerException(foobar())", 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 2 with ThrowExpr

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

the class JavaWriterVisitorTest method writeThrowExpr_basicThrowExpr.

@Test
public void writeThrowExpr_basicThrowExpr() {
    Expr exprToThrow = MethodInvocationExpr.builder().setStaticReferenceType(TypeNode.withReference(ConcreteReference.withClazz(Statement.class))).setMethodName("createException").setReturnType(TypeNode.withExceptionClazz(Exception.class)).build();
    ThrowExpr throwExpr = ThrowExpr.builder().setThrowExpr(exprToThrow).build();
    throwExpr.accept(writerVisitor);
    assertEquals("throw Statement.createException()", 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) BlockStatement(com.google.api.generator.engine.ast.BlockStatement) BreakStatement(com.google.api.generator.engine.ast.BreakStatement) WhileStatement(com.google.api.generator.engine.ast.WhileStatement) ForStatement(com.google.api.generator.engine.ast.ForStatement) EmptyLineStatement(com.google.api.generator.engine.ast.EmptyLineStatement) IfStatement(com.google.api.generator.engine.ast.IfStatement) GeneralForStatement(com.google.api.generator.engine.ast.GeneralForStatement) TryCatchStatement(com.google.api.generator.engine.ast.TryCatchStatement) 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) Test(org.junit.Test)

Example 3 with ThrowExpr

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

the class ImportWriterVisitorTest method writeThrowExprImports_basic.

@Test
public void writeThrowExprImports_basic() {
    TypeNode exceptionTypes = TypeNode.withReference(ConcreteReference.withClazz(IOException.class));
    String message = "Some message asdf";
    ThrowExpr throwExpr = ThrowExpr.builder().setType(exceptionTypes).setMessageExpr(message).build();
    throwExpr.accept(writerVisitor);
    assertEquals("import java.io.IOException;\n\n", writerVisitor.write());
}
Also used : ThrowExpr(com.google.api.generator.engine.ast.ThrowExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) IOException(java.io.IOException) Test(org.junit.Test)

Example 4 with ThrowExpr

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

the class ImportWriterVisitorTest method writeThrowExprImports_messageAndCauseExpr.

@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());
}
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 5 with ThrowExpr

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

the class JavaWriterVisitorTest method writeThrowExpr_basicWithCause.

@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());
}
Also used : ThrowExpr(com.google.api.generator.engine.ast.ThrowExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) 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