Search in sources :

Example 11 with IfStatement

use of com.google.api.generator.engine.ast.IfStatement 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)

Example 12 with IfStatement

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

the class JavaCodeGeneratorTest method createAddShelfMethod.

private MethodDefinition createAddShelfMethod() {
    Variable nameVar = createVarFromType(TypeNode.STRING, "name");
    Variable seriesDoubleNumVar = createVarFromType(TypeNode.DOUBLE, "seriesDoubleNum");
    CastExpr seriesNumDoubleToIntExpr = CastExpr.builder().setExpr(VariableExpr.withVariable(seriesDoubleNumVar)).setType(TypeNode.INT).build();
    AssignmentExpr castSeriesNumExpr = createAssignmentExpr(createVarDeclExpr(seriesNumVar), seriesNumDoubleToIntExpr);
    ReturnExpr maxValueReturnExpr = createReturnExpr("Series number equals to max int value.");
    ReturnExpr duplicateKeyReturnExpr = createReturnExpr("Shelf is already existing in the map.");
    // TODO: update the condition from `condition` to `seriesNum == MAX_VALUE`
    IfStatement maxValueCheck = IfStatement.builder().setConditionExpr(VariableExpr.withVariable(createVarFromType(TypeNode.BOOLEAN, "condition"))).setBody(Arrays.asList(ExprStatement.withExpr(maxValueReturnExpr))).build();
    NewObjectExpr newShelfExpr = createNewObjectExprWithArg(nameVar);
    MethodInvocationExpr addShelfToListExpr = methodExprWithRefAndArg(shelfListVar, "add", Arrays.asList(newShelfExpr));
    MethodInvocationExpr putShelfToMapExpr = methodExprWithRefAndArg(shelfMapVar, "put", Arrays.asList(VariableExpr.withVariable(nameVar), newShelfExpr));
    MethodInvocationExpr mapContainsKey = methodExprWithRefArgAndReturn(shelfMapVar, Arrays.asList(VariableExpr.withVariable(nameVar)));
    IfStatement mapContainsKeyCheck = IfStatement.builder().setConditionExpr(mapContainsKey).setBody(Arrays.asList(ExprStatement.withExpr(duplicateKeyReturnExpr))).build();
    return MethodDefinition.builder().setAnnotations(Arrays.asList(AnnotationNode.OVERRIDE)).setName("addShelf").setReturnType(TypeNode.STRING).setReturnExpr(ValueExpr.withValue(StringObjectValue.withValue("Shelf added."))).setScope(ScopeNode.PUBLIC).setBody(Arrays.asList(ExprStatement.withExpr(castSeriesNumExpr), maxValueCheck, ExprStatement.withExpr(addShelfToListExpr), mapContainsKeyCheck, ExprStatement.withExpr(putShelfToMapExpr))).setArguments(Arrays.asList(createVarDeclExpr(nameVar), createVarDeclExpr(seriesDoubleNumVar))).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) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) CastExpr(com.google.api.generator.engine.ast.CastExpr) ReturnExpr(com.google.api.generator.engine.ast.ReturnExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr)

Example 13 with IfStatement

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

the class JavaWriterVisitorTest method writeIfStatement_elseIfs.

@Test
public void writeIfStatement_elseIfs() {
    List<Statement> ifBody = Arrays.asList(ExprStatement.withExpr(createAssignmentExpr("x", "3", TypeNode.INT)), ExprStatement.withExpr(createAssignmentExpr("fooBar", "true", TypeNode.BOOLEAN)));
    VariableExpr condExprOne = createVariableExpr("condition", TypeNode.BOOLEAN);
    VariableExpr condExprTwo = createVariableExpr("fooBarCheck", TypeNode.BOOLEAN);
    VariableExpr condExprThree = createVariableExpr("anotherCondition", TypeNode.BOOLEAN);
    VariableExpr condExprFour = createVariableExpr("lookAtMe", TypeNode.BOOLEAN);
    IfStatement ifStatement = IfStatement.builder().setConditionExpr(condExprOne).setBody(ifBody).addElseIf(condExprTwo, ifBody).addElseIf(condExprThree, ifBody).addElseIf(condExprFour, ifBody).build();
    ifStatement.accept(writerVisitor);
    String expected = LineFormatter.lines("if (condition) {\n", "int x = 3;\n", "boolean fooBar = true;\n", "} else if (fooBarCheck) {\n", "int x = 3;\n", "boolean fooBar = true;\n", "} else if (anotherCondition) {\n", "int x = 3;\n", "boolean fooBar = true;\n", "} else if (lookAtMe) {\n", "int x = 3;\n", "boolean fooBar = true;\n", "}\n");
    assertEquals(expected, writerVisitor.write());
}
Also used : IfStatement(com.google.api.generator.engine.ast.IfStatement) 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) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) Test(org.junit.Test)

Example 14 with IfStatement

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

the class JavaWriterVisitorTest method writeIfStatement_simple.

@Test
public void writeIfStatement_simple() {
    AssignmentExpr assignExpr = createAssignmentExpr("x", "3", TypeNode.INT);
    Statement assignExprStatement = ExprStatement.withExpr(assignExpr);
    List<Statement> ifBody = Arrays.asList(assignExprStatement, assignExprStatement);
    VariableExpr condExpr = createVariableExpr("condition", TypeNode.BOOLEAN);
    IfStatement ifStatement = IfStatement.builder().setConditionExpr(condExpr).setBody(ifBody).build();
    ifStatement.accept(writerVisitor);
    assertEquals(LineFormatter.lines("if (condition) {\n", "int x = 3;\n", "int x = 3;\n", "}\n"), writerVisitor.write());
}
Also used : IfStatement(com.google.api.generator.engine.ast.IfStatement) 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) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) Test(org.junit.Test)

Aggregations

IfStatement (com.google.api.generator.engine.ast.IfStatement)14 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)12 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)11 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)11 Statement (com.google.api.generator.engine.ast.Statement)11 CommentStatement (com.google.api.generator.engine.ast.CommentStatement)10 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)10 ForStatement (com.google.api.generator.engine.ast.ForStatement)9 SynchronizedStatement (com.google.api.generator.engine.ast.SynchronizedStatement)8 Expr (com.google.api.generator.engine.ast.Expr)7 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)7 WhileStatement (com.google.api.generator.engine.ast.WhileStatement)7 CastExpr (com.google.api.generator.engine.ast.CastExpr)6 NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)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 ThrowExpr (com.google.api.generator.engine.ast.ThrowExpr)6 TypeNode (com.google.api.generator.engine.ast.TypeNode)6 Variable (com.google.api.generator.engine.ast.Variable)6