Search in sources :

Example 6 with IfStatement

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

the class JavaCodeGeneratorTest method createAddBooksContainsNovel.

private MethodDefinition createAddBooksContainsNovel() {
    ConcreteReference bookKindStackRef = ConcreteReference.builder().setClazz(Stack.class).setGenerics(Arrays.asList(bookKindRef)).build();
    Variable bookKindStackVar = createVarFromConcreteRef(bookKindStackRef, "stack");
    Variable containsNovelVar = createVarFromType(TypeNode.BOOLEAN, "containsNovel");
    Variable bookVar = createVarFromVaporRef(bookClassRef, "addedBook");
    TernaryExpr ternaryExpr = createTernaryExpr(containsNovelVar);
    AssignmentExpr setContainsNovelToFalse = createAssignmentExpr(createVarDeclExpr(containsNovelVar), ValueExpr.withValue(createBooleanValue("false")));
    MethodInvocationExpr stackIsEmpty = MethodInvocationExpr.builder().setMethodName("isEmpty").setExprReferenceExpr(VariableExpr.withVariable(bookKindStackVar)).setReturnType(TypeNode.BOOLEAN).build();
    MethodInvocationExpr stackPop = methodExprWithRef(bookKindStackVar, "pop");
    MethodInvocationExpr addBookToShelfMethod = MethodInvocationExpr.builder().setMethodName("addBookToShelf").setArguments(stackPop, VariableExpr.withVariable(shelfVar)).setReturnType(TypeNode.withReference(bookClassRef)).build();
    AssignmentExpr createNewAddedBook = createAssignmentExpr(createVarDeclExpr(bookVar), addBookToShelfMethod);
    InstanceofExpr addedBookIsNovelInstance = InstanceofExpr.builder().setExpr(VariableExpr.withVariable(bookVar)).setCheckType(TypeNode.withReference(novelClassRef)).build();
    AssignmentExpr setContainsNovelToTrue = createAssignmentExpr(VariableExpr.withVariable(containsNovelVar), ValueExpr.withValue(createBooleanValue("true")));
    IfStatement ifStatement = IfStatement.builder().setConditionExpr(addedBookIsNovelInstance).setBody(Arrays.asList(ExprStatement.withExpr(setContainsNovelToTrue))).build();
    // TODO: update the conditionExpr from `stack.isEmpty()` to `!stack.isEmpty()`
    WhileStatement whileStatement = WhileStatement.builder().setConditionExpr(stackIsEmpty).setBody(Arrays.asList(ExprStatement.withExpr(createNewAddedBook), ifStatement)).build();
    return MethodDefinition.builder().setHeaderCommentStatements(createPreMethodJavaDocComment()).setArguments(Arrays.asList(createVarDeclExpr(shelfVar), createVarDeclExpr(bookKindStackVar))).setName("addBooksContainsNovel").setReturnType(TypeNode.STRING).setScope(ScopeNode.PUBLIC).setBody(Arrays.asList(ExprStatement.withExpr(setContainsNovelToFalse), whileStatement)).setReturnExpr(ternaryExpr).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) InstanceofExpr(com.google.api.generator.engine.ast.InstanceofExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) WhileStatement(com.google.api.generator.engine.ast.WhileStatement) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) TernaryExpr(com.google.api.generator.engine.ast.TernaryExpr)

Example 7 with IfStatement

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

the class JavaWriterVisitor method visit.

@Override
public void visit(IfStatement ifStatement) {
    buffer.append(IF);
    space();
    leftParen();
    ifStatement.conditionExpr().accept(this);
    rightParen();
    space();
    leftBrace();
    newline();
    statements(ifStatement.body());
    buffer.append(RIGHT_BRACE);
    if (!ifStatement.elseIfs().isEmpty()) {
        for (Map.Entry<Expr, List<Statement>> elseIfEntry : ifStatement.elseIfs().entrySet()) {
            Expr elseIfConditionExpr = elseIfEntry.getKey();
            List<Statement> elseIfBody = elseIfEntry.getValue();
            space();
            buffer.append(ELSE);
            space();
            buffer.append(IF);
            space();
            leftParen();
            elseIfConditionExpr.accept(this);
            rightParen();
            space();
            leftBrace();
            newline();
            statements(elseIfBody);
            rightBrace();
        }
    }
    if (!ifStatement.elseBody().isEmpty()) {
        space();
        buffer.append(ELSE);
        space();
        leftBrace();
        newline();
        statements(ifStatement.elseBody());
        rightBrace();
    }
    newline();
}
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) List(java.util.List) Map(java.util.Map)

Example 8 with IfStatement

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

the class GrpcServiceStubClassComposer method createRequestParamsExtractorBodyForRoutingHeaders.

private void createRequestParamsExtractorBodyForRoutingHeaders(Method method, VariableExpr requestVarExpr, List<Statement> classStatements, List<Statement> bodyStatements, MethodInvocationExpr.Builder returnExprBuilder) {
    TypeNode routingHeadersBuilderType = TypeNode.withReference(ConcreteReference.builder().setClazz(RequestParamsBuilder.class).build());
    VariableExpr routingHeadersBuilderVarExpr = VariableExpr.builder().setVariable(Variable.builder().setName("builder").setType(routingHeadersBuilderType).build()).setIsDecl(true).build();
    MethodInvocationExpr routingHeaderBuilderInvokeExpr = MethodInvocationExpr.builder().setStaticReferenceType(routingHeadersBuilderType).setMethodName("create").setReturnType(routingHeadersBuilderType).build();
    Expr routingHeadersBuilderInitExpr = AssignmentExpr.builder().setVariableExpr(routingHeadersBuilderVarExpr).setValueExpr(routingHeaderBuilderInvokeExpr).build();
    bodyStatements.add(ExprStatement.withExpr(routingHeadersBuilderInitExpr));
    List<RoutingHeaderParam> routingHeaderParams = method.routingHeaderRule().routingHeaderParams();
    VariableExpr routingHeadersBuilderVarNonDeclExpr = VariableExpr.builder().setVariable(Variable.builder().setName("builder").setType(routingHeadersBuilderType).build()).build();
    for (int i = 0; i < routingHeaderParams.size(); i++) {
        RoutingHeaderParam routingHeaderParam = routingHeaderParams.get(i);
        MethodInvocationExpr requestFieldGetterExpr = createRequestFieldGetterExpr(requestVarExpr, routingHeaderParam.fieldName());
        Expr routingHeaderKeyExpr = ValueExpr.withValue(StringObjectValue.withValue(routingHeaderParam.key()));
        String pathTemplateName = String.format("%s_%s_PATH_TEMPLATE", JavaStyle.toUpperSnakeCase(method.name()), i);
        TypeNode pathTemplateType = TypeNode.withReference(ConcreteReference.withClazz(PathTemplate.class));
        Variable pathTemplateVar = Variable.builder().setType(pathTemplateType).setName(pathTemplateName).build();
        Expr routingHeaderPatternExpr = VariableExpr.withVariable(pathTemplateVar);
        Statement pathTemplateClassVar = createPathTemplateClassStatement(routingHeaderParam, pathTemplateType, pathTemplateVar);
        classStatements.add(pathTemplateClassVar);
        MethodInvocationExpr addParamMethodExpr = MethodInvocationExpr.builder().setExprReferenceExpr(routingHeadersBuilderVarNonDeclExpr).setMethodName("add").setArguments(requestFieldGetterExpr, routingHeaderKeyExpr, routingHeaderPatternExpr).build();
        ExprStatement addParamStatement = ExprStatement.withExpr(addParamMethodExpr);
        // No need to add null check if there is no nested fields
        if (routingHeaderParam.getDescendantFieldNames().size() == 1) {
            bodyStatements.add(addParamStatement);
        } else {
            IfStatement ifStatement = IfStatement.builder().setConditionExpr(fieldValuesNotNullConditionExpr(requestVarExpr, routingHeaderParam.getDescendantFieldNames())).setBody(ImmutableList.of(addParamStatement)).build();
            bodyStatements.add(ifStatement);
        }
    }
    returnExprBuilder.setExprReferenceExpr(routingHeadersBuilderVarNonDeclExpr).setMethodName("build");
}
Also used : RoutingHeaderParam(com.google.api.generator.gapic.model.RoutingHeaderRule.RoutingHeaderParam) Variable(com.google.api.generator.engine.ast.Variable) IfStatement(com.google.api.generator.engine.ast.IfStatement) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) Statement(com.google.api.generator.engine.ast.Statement) PathTemplate(com.google.api.pathtemplate.PathTemplate) IfStatement(com.google.api.generator.engine.ast.IfStatement) EnumRefExpr(com.google.api.generator.engine.ast.EnumRefExpr) LogicalOperationExpr(com.google.api.generator.engine.ast.LogicalOperationExpr) Expr(com.google.api.generator.engine.ast.Expr) RelationalOperationExpr(com.google.api.generator.engine.ast.RelationalOperationExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) LambdaExpr(com.google.api.generator.engine.ast.LambdaExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode)

Example 9 with IfStatement

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

the class JavaWriterVisitorTest method writeIfStatement_withElse.

@Test
public void writeIfStatement_withElse() {
    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).setElseBody(ifBody).build();
    ifStatement.accept(writerVisitor);
    assertEquals(String.format("%s%s%s" + "%s%s%s%s", "if (condition) {\n", "int x = 3;\n", "int x = 3;\n", "} else {\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)

Example 10 with IfStatement

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

the class JavaWriterVisitorTest method writeIfStatement_nested.

@Test
public void writeIfStatement_nested() {
    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 nestedTwoIfStatement = IfStatement.builder().setConditionExpr(condExprThree).setBody(ifBody).setElseBody(ifBody).build();
    IfStatement nestedOneIfStatement = IfStatement.builder().setConditionExpr(condExprTwo).setBody(Arrays.asList(ExprStatement.withExpr(createAssignmentExpr("anInt", "10", TypeNode.INT)), nestedTwoIfStatement)).build();
    IfStatement nestedZeroIfStatement = IfStatement.builder().setConditionExpr(condExprOne).setBody(Arrays.asList(nestedOneIfStatement)).addElseIf(condExprFour, ifBody).build();
    IfStatement ifStatement = IfStatement.builder().setConditionExpr(condExprOne).setBody(Arrays.asList(nestedZeroIfStatement)).build();
    ifStatement.accept(writerVisitor);
    String expected = LineFormatter.lines("if (condition) {\n", "if (condition) {\n", "if (fooBarCheck) {\n", "int anInt = 10;\n", "if (anotherCondition) {\n", "int x = 3;\n", "boolean fooBar = true;\n", "} else {\n", "int x = 3;\n", "boolean fooBar = true;\n", "}\n", "}\n", "} else if (lookAtMe) {\n", "int x = 3;\n", "boolean fooBar = true;\n", "}\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)

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