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();
}
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();
}
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");
}
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());
}
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());
}
Aggregations