Search in sources :

Example 6 with ExprStatement

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

the class HttpJsonServiceStubClassComposer method createGetMethodDescriptorsMethod.

@Override
protected List<MethodDefinition> createGetMethodDescriptorsMethod(Service service, TypeStore typeStore, Map<String, VariableExpr> protoMethodNameToDescriptorVarExprs) {
    List<Expr> bodyExprs = new ArrayList<>();
    VariableExpr methodDescriptorsVarExpr = VariableExpr.withVariable(Variable.builder().setType(TypeNode.withReference(ConcreteReference.builder().setClazz(List.class).setGenerics(Arrays.asList(FIXED_REST_TYPESTORE.get("ApiMethodDescriptor").reference())).build())).setName("methodDescriptors").build());
    bodyExprs.add(AssignmentExpr.builder().setVariableExpr(methodDescriptorsVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(NewObjectExpr.builder().setType(FIXED_REST_TYPESTORE.get("ArrayList")).setIsGeneric(true).build()).build());
    for (VariableExpr methodDescriptorVarExpr : protoMethodNameToDescriptorVarExprs.values()) {
        bodyExprs.add(MethodInvocationExpr.builder().setExprReferenceExpr(methodDescriptorsVarExpr).setMethodName("add").setArguments(methodDescriptorVarExpr).build());
    }
    return Arrays.asList(MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setIsStatic(true).setReturnType(methodDescriptorsVarExpr.type()).setReturnExpr(methodDescriptorsVarExpr).setAnnotations(Arrays.asList(AnnotationNode.withType(FIXED_REST_TYPESTORE.get("InternalApi")))).setName("getMethodDescriptors").setBody(bodyExprs.stream().map(ExprStatement::withExpr).collect(Collectors.toList())).build());
}
Also used : ValueExpr(com.google.api.generator.engine.ast.ValueExpr) EnumRefExpr(com.google.api.generator.engine.ast.EnumRefExpr) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) Expr(com.google.api.generator.engine.ast.Expr) 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) ArrayList(java.util.ArrayList) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList)

Example 7 with ExprStatement

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

the class ServiceClientTestClassComposer method constructRpcTestCheckerLogic.

@Override
protected List<Statement> constructRpcTestCheckerLogic(Method method, Service service, boolean isRequestArg, Map<String, VariableExpr> classMemberVarExprs, VariableExpr requestVarExpr, Message requestMessage, List<VariableExpr> argExprs) {
    VariableExpr actualRequestsVarExpr = VariableExpr.withVariable(Variable.builder().setType(TypeNode.withReference(ConcreteReference.builder().setClazz(List.class).setGenerics(Arrays.asList(TypeNode.STRING.reference())).build())).setName("actualRequests").build());
    List<Expr> methodExprs = new ArrayList<>();
    methodExprs.add(AssignmentExpr.builder().setVariableExpr(actualRequestsVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(MethodInvocationExpr.builder().setExprReferenceExpr(classMemberVarExprs.get(getMockServiceVarName(service))).setMethodName("getRequestPaths").setReturnType(actualRequestsVarExpr.type()).build()).build());
    methodExprs.add(MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("Assert")).setMethodName("assertEquals").setArguments(ValueExpr.withValue(PrimitiveValue.builder().setType(TypeNode.INT).setValue("1").build()), MethodInvocationExpr.builder().setExprReferenceExpr(actualRequestsVarExpr).setMethodName("size").build()).build());
    List<Statement> methodStatements = new ArrayList<>();
    methodStatements.addAll(methodExprs.stream().map(ExprStatement::withExpr).collect(Collectors.toList()));
    methodExprs.clear();
    methodStatements.add(EMPTY_LINE_STATEMENT);
    VariableExpr apiClientHeaderKeyVarExpr = VariableExpr.withVariable(Variable.builder().setType(TypeNode.STRING).setName("apiClientHeaderKey").build());
    AssignmentExpr apiClientHeaderKeyAssignExpr = AssignmentExpr.builder().setVariableExpr(apiClientHeaderKeyVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(MethodInvocationExpr.builder().setMethodName("next").setReturnType(apiClientHeaderKeyVarExpr.type()).setExprReferenceExpr(MethodInvocationExpr.builder().setMethodName("iterator").setExprReferenceExpr(MethodInvocationExpr.builder().setMethodName("get").setArguments(MethodInvocationExpr.builder().setMethodName("getDefaultApiClientHeaderKey").setStaticReferenceType(FIXED_REST_TYPESTORE.get("ApiClientHeaderProvider")).build()).setExprReferenceExpr(MethodInvocationExpr.builder().setMethodName("getRequestHeaders").setExprReferenceExpr(classMemberVarExprs.get(getMockServiceVarName(service))).build()).build()).build()).build()).build();
    methodExprs.add(apiClientHeaderKeyAssignExpr);
    methodExprs.add(MethodInvocationExpr.builder().setMethodName("assertTrue").setStaticReferenceType(FIXED_TYPESTORE.get("Assert")).setArguments(MethodInvocationExpr.builder().setMethodName("matches").setExprReferenceExpr(MethodInvocationExpr.builder().setMethodName("matcher").setArguments(apiClientHeaderKeyVarExpr).setExprReferenceExpr(MethodInvocationExpr.builder().setMethodName("getDefaultApiClientHeaderPattern").setStaticReferenceType(FIXED_REST_TYPESTORE.get("GaxHttpJsonProperties")).build()).build()).build()).build());
    methodStatements.addAll(methodExprs.stream().map(ExprStatement::withExpr).collect(Collectors.toList()));
    methodExprs.clear();
    return methodStatements;
}
Also used : EnumRefExpr(com.google.api.generator.engine.ast.EnumRefExpr) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) Expr(com.google.api.generator.engine.ast.Expr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) Statement(com.google.api.generator.engine.ast.Statement) ArrayList(java.util.ArrayList) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr)

Example 8 with ExprStatement

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

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

the class JavaWriterVisitorTest method writeAnonymousClassExpr_withStatementsMethods.

@Test
public void writeAnonymousClassExpr_withStatementsMethods() {
    ConcreteReference ref = ConcreteReference.withClazz(Runnable.class);
    TypeNode type = TypeNode.withReference(ref);
    // [Constructing] private static final String s = "foo";
    Variable variable = createVariable("s", TypeNode.STRING);
    VariableExpr variableExpr = VariableExpr.builder().setScope(ScopeNode.PRIVATE).setIsDecl(true).setIsFinal(true).setIsStatic(true).setVariable(variable).build();
    ValueExpr valueExpr = ValueExpr.builder().setValue(StringObjectValue.withValue("foo")).build();
    AssignmentExpr assignmentExpr = AssignmentExpr.builder().setVariableExpr(variableExpr).setValueExpr(valueExpr).build();
    ExprStatement exprStatement = ExprStatement.withExpr(assignmentExpr);
    MethodDefinition methodDefinition = MethodDefinition.builder().setName("run").setIsOverride(true).setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.VOID).setBody(Arrays.asList(ExprStatement.withExpr(createAssignmentExpr("x", "3", TypeNode.INT)))).build();
    AnonymousClassExpr anonymousClassExpr = AnonymousClassExpr.builder().setType(type).setStatements(Arrays.asList(exprStatement)).setMethods(Arrays.asList(methodDefinition)).build();
    anonymousClassExpr.accept(writerVisitor);
    String expected = LineFormatter.lines("new Runnable() {\n", "private static final String s = \"foo\";\n", "@Override\n", "public void run() {\n", "int x = 3;\n}\n\n}");
    assertEquals(expected, writerVisitor.write());
}
Also used : ValueExpr(com.google.api.generator.engine.ast.ValueExpr) Variable(com.google.api.generator.engine.ast.Variable) MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr) Test(org.junit.Test)

Example 10 with ExprStatement

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

the class JavaWriterVisitorTest method writeExprStatement.

/**
 * =============================== STATEMENTS ===============================
 */
@Test
public void writeExprStatement() {
    TypeNode someType = TypeNode.withReference(VaporReference.builder().setName("SomeClass").setPakkage("com.google.api.some.pakkage").build());
    MethodInvocationExpr methodExpr = MethodInvocationExpr.builder().setMethodName("foobar").setStaticReferenceType(someType).build();
    ExprStatement exprStatement = ExprStatement.withExpr(methodExpr);
    exprStatement.accept(writerVisitor);
    assertEquals("SomeClass.foobar();\n", writerVisitor.write());
}
Also used : MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) TypeNode(com.google.api.generator.engine.ast.TypeNode) Test(org.junit.Test)

Aggregations

ExprStatement (com.google.api.generator.engine.ast.ExprStatement)10 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)8 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)7 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)6 TypeNode (com.google.api.generator.engine.ast.TypeNode)6 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)6 Expr (com.google.api.generator.engine.ast.Expr)5 Test (org.junit.Test)5 AnonymousClassExpr (com.google.api.generator.engine.ast.AnonymousClassExpr)4 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)4 EnumRefExpr (com.google.api.generator.engine.ast.EnumRefExpr)4 LambdaExpr (com.google.api.generator.engine.ast.LambdaExpr)4 MethodDefinition (com.google.api.generator.engine.ast.MethodDefinition)4 NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)4 Statement (com.google.api.generator.engine.ast.Statement)4 CommentStatement (com.google.api.generator.engine.ast.CommentStatement)2 EmptyLineStatement (com.google.api.generator.engine.ast.EmptyLineStatement)2 ReferenceConstructorExpr (com.google.api.generator.engine.ast.ReferenceConstructorExpr)2 ThrowExpr (com.google.api.generator.engine.ast.ThrowExpr)2 TryCatchStatement (com.google.api.generator.engine.ast.TryCatchStatement)2