use of com.google.api.generator.engine.ast.MethodInvocationExpr in project gapic-generator-java by googleapis.
the class ServiceClientTestClassComposer method createStartStaticServerMethod.
@Override
protected MethodDefinition createStartStaticServerMethod(Service service, GapicContext context, Map<String, VariableExpr> classMemberVarExprs, TypeStore typeStore, String newBuilderMethod) {
VariableExpr mockServiceVarExpr = classMemberVarExprs.get(MOCK_SERVICE_VAR_NAME);
VariableExpr clientVarExpr = classMemberVarExprs.get(CLIENT_VAR_NAME);
TypeNode settingsType = typeStore.get(ClassNames.getServiceSettingsClassName(service));
TypeNode transportStubType = typeStore.get(getTransportContext().classNames().getTransportServiceStubClassName(service));
Function<Expr, BiFunction<String, Expr, MethodInvocationExpr>> methodBuilderFn = methodExpr -> (mName, argExpr) -> MethodInvocationExpr.builder().setExprReferenceExpr(methodExpr).setMethodName(mName).setArguments(Arrays.asList(argExpr)).build();
Expr initMockServiceExpr = AssignmentExpr.builder().setVariableExpr(mockServiceVarExpr).setValueExpr(NewObjectExpr.builder().setType(mockServiceVarExpr.type()).setArguments(MethodInvocationExpr.builder().setStaticReferenceType(transportStubType).setMethodName("getMethodDescriptors").build(), MethodInvocationExpr.builder().setStaticReferenceType(settingsType).setMethodName("getDefaultEndpoint").build()).build()).build();
VariableExpr localSettingsVarExpr = VariableExpr.withVariable(Variable.builder().setName("settings").setType(settingsType).build());
Expr settingsBuilderExpr = MethodInvocationExpr.builder().setStaticReferenceType(settingsType).setMethodName(newBuilderMethod).build();
Expr transportChannelProviderExpr = MethodInvocationExpr.builder().setStaticReferenceType(settingsType).setMethodName("defaultHttpJsonTransportProviderBuilder").build();
transportChannelProviderExpr = methodBuilderFn.apply(transportChannelProviderExpr).apply("setHttpTransport", mockServiceVarExpr);
transportChannelProviderExpr = MethodInvocationExpr.builder().setExprReferenceExpr(transportChannelProviderExpr).setMethodName("build").setReturnType(localSettingsVarExpr.type()).build();
settingsBuilderExpr = methodBuilderFn.apply(settingsBuilderExpr).apply("setTransportChannelProvider", transportChannelProviderExpr);
settingsBuilderExpr = methodBuilderFn.apply(settingsBuilderExpr).apply("setCredentialsProvider", MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("NoCredentialsProvider")).setMethodName("create").build());
settingsBuilderExpr = MethodInvocationExpr.builder().setExprReferenceExpr(settingsBuilderExpr).setMethodName("build").setReturnType(localSettingsVarExpr.type()).build();
Expr initLocalSettingsExpr = AssignmentExpr.builder().setVariableExpr(localSettingsVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(settingsBuilderExpr).build();
Expr initClientExpr = AssignmentExpr.builder().setVariableExpr(clientVarExpr).setValueExpr(MethodInvocationExpr.builder().setStaticReferenceType(typeStore.get(ClassNames.getServiceClientClassName(service))).setMethodName("create").setArguments(Arrays.asList(localSettingsVarExpr)).setReturnType(clientVarExpr.type()).build()).build();
return MethodDefinition.builder().setAnnotations(Arrays.asList(AnnotationNode.withType(FIXED_TYPESTORE.get("BeforeClass")))).setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.VOID).setName("startStaticServer").setThrowsExceptions(Arrays.asList(FIXED_TYPESTORE.get("IOException"))).setIsStatic(true).setBody(Arrays.asList(initMockServiceExpr, initLocalSettingsExpr, initClientExpr).stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList())).build();
}
use of com.google.api.generator.engine.ast.MethodInvocationExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeSuperObjectValue_accessFieldAndInvokeMethod.
@Test
public void writeSuperObjectValue_accessFieldAndInvokeMethod() {
VaporReference ref = VaporReference.builder().setName("Student").setPakkage("com.google.example.v1").build();
TypeNode classType = TypeNode.withReference(ref);
SuperObjectValue superObjectValue = SuperObjectValue.withType(classType);
ValueExpr superValueExpr = ValueExpr.withValue(superObjectValue);
Variable subVariable = Variable.builder().setName("name").setType(TypeNode.STRING).build();
VariableExpr superVariableExpr = VariableExpr.builder().setVariable(subVariable).setExprReferenceExpr(superValueExpr).build();
MethodInvocationExpr methodExpr = MethodInvocationExpr.builder().setMethodName("getName").setExprReferenceExpr(ValueExpr.withValue(superObjectValue)).setReturnType(TypeNode.STRING).build();
AssignmentExpr assignmentExpr = AssignmentExpr.builder().setVariableExpr(superVariableExpr).setValueExpr(methodExpr).build();
assignmentExpr.accept(writerVisitor);
assertThat(writerVisitor.write()).isEqualTo("super.name = super.getName()");
}
use of com.google.api.generator.engine.ast.MethodInvocationExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeMethodInvocationExpr_basic.
@Test
public void writeMethodInvocationExpr_basic() {
MethodInvocationExpr methodExpr = MethodInvocationExpr.builder().setMethodName("foobar").build();
methodExpr.accept(writerVisitor);
assertEquals("foobar()", writerVisitor.write());
}
use of com.google.api.generator.engine.ast.MethodInvocationExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeMethodInvocationExpr_chained.
@Test
public void writeMethodInvocationExpr_chained() {
Variable variable = Variable.builder().setType(TypeNode.INT).setName("libraryClient").build();
VariableExpr varExpr = VariableExpr.builder().setVariable(variable).build();
MethodInvocationExpr firstMethodExpr = MethodInvocationExpr.builder().setMethodName("streamBooksCallable").setExprReferenceExpr(varExpr).build();
MethodInvocationExpr secondMethodExpr = MethodInvocationExpr.builder().setMethodName("doAnotherThing").setExprReferenceExpr(firstMethodExpr).build();
MethodInvocationExpr methodExpr = MethodInvocationExpr.builder().setMethodName("call").setExprReferenceExpr(secondMethodExpr).build();
methodExpr.accept(writerVisitor);
assertEquals("libraryClient.streamBooksCallable().doAnotherThing().call()", writerVisitor.write());
}
use of com.google.api.generator.engine.ast.MethodInvocationExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeMethodInvocationExpr_staticRef.
@Test
public void writeMethodInvocationExpr_staticRef() {
TypeNode someType = TypeNode.withReference(VaporReference.builder().setName("SomeClass").setPakkage("com.google.api.some.pakkage").build());
MethodInvocationExpr methodExpr = MethodInvocationExpr.builder().setMethodName("foobar").setStaticReferenceType(someType).build();
methodExpr.accept(writerVisitor);
assertEquals("SomeClass.foobar()", writerVisitor.write());
}
Aggregations