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