Search in sources :

Example 11 with ThrowExpr

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

the class JavaCodeGeneratorTest method createUpdateShelfMap.

private MethodDefinition createUpdateShelfMap() {
    ConcreteReference nonexistentShelfExceptionRef = ConcreteReference.builder().setClazz(Exception.class).build();
    Variable shelfVar = createVarFromVaporRef(shelfClassRef, "newShelf");
    VariableExpr shelfNameFromNewShelfObject = fieldFromShelfObjectExpr(shelfVar, createVarFromType(TypeNode.STRING, "shelfName"));
    MethodInvocationExpr mapContainsKeyExpr = methodExprWithRefArgAndReturn(shelfMapVar, Arrays.asList(shelfNameFromNewShelfObject));
    MethodInvocationExpr putShelfToMapExpr = methodExprWithRefAndArg(shelfMapVar, "put", Arrays.asList(shelfNameFromNewShelfObject, VariableExpr.withVariable(shelfVar)));
    ThrowExpr throwExpr = ThrowExpr.builder().setMessageExpr("Updating shelf is not existing in the map").setType(TypeNode.withReference(nonexistentShelfExceptionRef)).build();
    IfStatement updateShelfMapIfElseBlock = IfStatement.builder().setConditionExpr(mapContainsKeyExpr).setBody(Arrays.asList(ExprStatement.withExpr(putShelfToMapExpr))).setElseBody(Arrays.asList(ExprStatement.withExpr(throwExpr))).build();
    return MethodDefinition.builder().setName("updateShelfMap").setThrowsExceptions(Arrays.asList(TypeNode.withReference(nonexistentShelfExceptionRef))).setReturnType(TypeNode.VOID).setScope(ScopeNode.PUBLIC).setBody(Arrays.asList(updateShelfMapIfElseBlock)).setArguments(Arrays.asList(VariableExpr.builder().setVariable(shelfVar).setIsDecl(true).build())).build();
}
Also used : IfStatement(com.google.api.generator.engine.ast.IfStatement) Variable(com.google.api.generator.engine.ast.Variable) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ThrowExpr(com.google.api.generator.engine.ast.ThrowExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) IOException(java.io.IOException)

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