Search in sources :

Example 11 with TryCatchStatement

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

the class JavaWriterVisitorTest method writeTryCatchStatement_withResources.

@Test
public void writeTryCatchStatement_withResources() {
    Reference exceptionReference = ConcreteReference.withClazz(IllegalArgumentException.class);
    TypeNode type = TypeNode.withReference(exceptionReference);
    VariableExpr variableExpr = VariableExpr.builder().setVariable(createVariable("e", type)).setIsDecl(true).build();
    TryCatchStatement tryCatch = TryCatchStatement.builder().setTryResourceExpr(createAssignmentExpr("aBool", "false", TypeNode.BOOLEAN)).setTryBody(Arrays.asList(ExprStatement.withExpr(createAssignmentExpr("y", "4", TypeNode.INT)))).addCatch(variableExpr, Arrays.asList(ExprStatement.withExpr(createAssignmentExpr("foobar", "123", TypeNode.INT)))).build();
    tryCatch.accept(writerVisitor);
    assertEquals(String.format("%s%s%s%s%s", "try (boolean aBool = false) {\n", "int y = 4;\n", "} catch (IllegalArgumentException e) {\n", "int foobar = 123;\n", "}\n"), writerVisitor.write());
}
Also used : Reference(com.google.api.generator.engine.ast.Reference) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) VaporReference(com.google.api.generator.engine.ast.VaporReference) TryCatchStatement(com.google.api.generator.engine.ast.TryCatchStatement) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) Test(org.junit.Test)

Aggregations

TryCatchStatement (com.google.api.generator.engine.ast.TryCatchStatement)11 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)9 Test (org.junit.Test)7 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)5 TypeNode (com.google.api.generator.engine.ast.TypeNode)5 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)4 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)4 Reference (com.google.api.generator.engine.ast.Reference)4 VaporReference (com.google.api.generator.engine.ast.VaporReference)4 Variable (com.google.api.generator.engine.ast.Variable)3 Expr (com.google.api.generator.engine.ast.Expr)2 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)2 Statement (com.google.api.generator.engine.ast.Statement)2 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)2 Message (com.google.api.generator.gapic.model.Message)2 AbstractMessage (com.google.protobuf.AbstractMessage)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ExecutionException (java.util.concurrent.ExecutionException)2