use of com.google.api.generator.engine.ast.MethodInvocationExpr in project gapic-generator-java by googleapis.
the class AbstractServiceStubSettingsClassComposer method createApiClientHeaderProviderBuilderMethod.
protected MethodDefinition createApiClientHeaderProviderBuilderMethod(Service service, TypeStore typeStore, String methodName, TypeNode gaxPropertiesType, String getTokenMethodName, String getVersionMethodName) {
TypeNode returnType = TypeNode.withReference(ConcreteReference.withClazz(ApiClientHeaderProvider.Builder.class));
MethodInvocationExpr returnExpr = MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("ApiClientHeaderProvider")).setMethodName("newBuilder").build();
MethodInvocationExpr versionArgExpr = MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("GaxProperties")).setMethodName("getLibraryVersion").setArguments(VariableExpr.builder().setVariable(Variable.builder().setType(TypeNode.CLASS_OBJECT).setName("class").build()).setStaticReferenceType(typeStore.get(ClassNames.getServiceStubSettingsClassName(service))).build()).build();
returnExpr = MethodInvocationExpr.builder().setExprReferenceExpr(returnExpr).setMethodName("setGeneratedLibToken").setArguments(ValueExpr.withValue(StringObjectValue.withValue("gapic")), versionArgExpr).build();
returnExpr = MethodInvocationExpr.builder().setExprReferenceExpr(returnExpr).setMethodName("setTransportToken").setArguments(MethodInvocationExpr.builder().setStaticReferenceType(gaxPropertiesType).setMethodName(getTokenMethodName).build(), MethodInvocationExpr.builder().setStaticReferenceType(gaxPropertiesType).setMethodName(getVersionMethodName).build()).setReturnType(returnType).build();
AnnotationNode annotation = AnnotationNode.builder().setType(FIXED_TYPESTORE.get("BetaApi")).setDescription("The surface for customizing headers is not stable yet and may change in the" + " future.").build();
return MethodDefinition.builder().setAnnotations(Arrays.asList(annotation)).setScope(ScopeNode.PUBLIC).setIsStatic(true).setReturnType(returnType).setName(methodName).setReturnExpr(returnExpr).build();
}
use of com.google.api.generator.engine.ast.MethodInvocationExpr in project gapic-generator-java by googleapis.
the class AbstractServiceStubSettingsClassComposer method createDefaultTransportTransportProviderBuilderMethods.
protected List<MethodDefinition> createDefaultTransportTransportProviderBuilderMethods() {
// Create the defaultGrpcTransportProviderBuilder method.
Iterator<Class<?>> providerClassIt = getTransportContext().instantiatingChannelProviderClasses().iterator();
Iterator<Class<?>> providerBuilderClassIt = getTransportContext().instantiatingChannelProviderBuilderClasses().iterator();
Iterator<String> builderNamesIt = getTransportContext().defaultTransportProviderBuilderNames().iterator();
List<MethodDefinition> methods = new ArrayList<>();
while (providerClassIt.hasNext() && providerBuilderClassIt.hasNext() && builderNamesIt.hasNext()) {
TypeNode returnType = TypeNode.withReference(ConcreteReference.withClazz(providerBuilderClassIt.next()));
TypeNode channelProviderType = TypeNode.withReference(ConcreteReference.withClazz(providerClassIt.next()));
MethodInvocationExpr transportChannelProviderBuilderExpr = MethodInvocationExpr.builder().setStaticReferenceType(channelProviderType).setMethodName("newBuilder").setReturnType(returnType).build();
Expr returnExpr = initializeTransportProviderBuilder(transportChannelProviderBuilderExpr, returnType);
MethodDefinition method = MethodDefinition.builder().setHeaderCommentStatements(SettingsCommentComposer.DEFAULT_TRANSPORT_PROVIDER_BUILDER_METHOD_COMMENT).setScope(ScopeNode.PUBLIC).setIsStatic(true).setReturnType(returnType).setName(builderNamesIt.next()).setReturnExpr(returnExpr).build();
methods.add(method);
}
return methods;
}
use of com.google.api.generator.engine.ast.MethodInvocationExpr in project gapic-generator-java by googleapis.
the class AbstractTransportServiceStubClassComposer method createStaticCreatorMethods.
protected List<MethodDefinition> createStaticCreatorMethods(Service service, TypeStore typeStore, String newBuilderMethod) {
TypeNode creatorMethodReturnType = typeStore.get(getTransportContext().classNames().getTransportServiceStubClassName(service));
Function<List<VariableExpr>, MethodDefinition.Builder> creatorMethodStarterFn = argList -> MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setIsStatic(true).setIsFinal(true).setReturnType(creatorMethodReturnType).setName("create").setArguments(argList.stream().map(v -> v.toBuilder().setIsDecl(true).build()).collect(Collectors.toList())).setThrowsExceptions(Arrays.asList(TypeNode.withReference(ConcreteReference.withClazz(IOException.class))));
Function<List<Expr>, Expr> instantiatorExprFn = argList -> NewObjectExpr.builder().setType(creatorMethodReturnType).setArguments(argList).build();
TypeNode stubSettingsType = typeStore.get(getTransportContext().classNames().getServiceStubSettingsClassName(service));
VariableExpr settingsVarExpr = VariableExpr.withVariable(Variable.builder().setName("settings").setType(stubSettingsType).build());
TypeNode clientContextType = FIXED_TYPESTORE.get("ClientContext");
VariableExpr clientContextVarExpr = VariableExpr.withVariable(Variable.builder().setName("clientContext").setType(clientContextType).build());
VariableExpr callableFactoryVarExpr = VariableExpr.withVariable(Variable.builder().setName("callableFactory").setType(getTransportContext().stubCallableFactoryType()).build());
MethodInvocationExpr clientContextCreateMethodExpr = MethodInvocationExpr.builder().setMethodName("create").setStaticReferenceType(clientContextType).setArguments(Arrays.asList(settingsVarExpr)).build();
MethodInvocationExpr settingsBuilderMethodExpr = MethodInvocationExpr.builder().setMethodName(newBuilderMethod).setStaticReferenceType(stubSettingsType).build();
settingsBuilderMethodExpr = MethodInvocationExpr.builder().setMethodName("build").setExprReferenceExpr(settingsBuilderMethodExpr).build();
return Arrays.asList(creatorMethodStarterFn.apply(Arrays.asList(settingsVarExpr)).setReturnExpr(instantiatorExprFn.apply(Arrays.asList(settingsVarExpr, clientContextCreateMethodExpr))).build(), creatorMethodStarterFn.apply(Arrays.asList(clientContextVarExpr)).setReturnExpr(instantiatorExprFn.apply(Arrays.asList(settingsBuilderMethodExpr, clientContextVarExpr))).build(), creatorMethodStarterFn.apply(Arrays.asList(clientContextVarExpr, callableFactoryVarExpr)).setReturnExpr(instantiatorExprFn.apply(Arrays.asList(settingsBuilderMethodExpr, clientContextVarExpr, callableFactoryVarExpr))).build());
}
use of com.google.api.generator.engine.ast.MethodInvocationExpr in project gapic-generator-java by googleapis.
the class AbstractTransportServiceStubClassComposer method createConstructorMethods.
protected List<MethodDefinition> createConstructorMethods(GapicContext context, Service service, TypeStore typeStore, Map<String, VariableExpr> classMemberVarExprs, Map<String, VariableExpr> callableClassMemberVarExprs, Map<String, VariableExpr> protoMethodNameToDescriptorVarExprs, List<Statement> classStatements) {
TypeNode stubSettingsType = typeStore.get(getTransportContext().classNames().getServiceStubSettingsClassName(service));
VariableExpr settingsVarExpr = VariableExpr.withVariable(Variable.builder().setName("settings").setType(stubSettingsType).build());
TypeNode clientContextType = FIXED_TYPESTORE.get("ClientContext");
VariableExpr clientContextVarExpr = VariableExpr.withVariable(Variable.builder().setName("clientContext").setType(clientContextType).build());
VariableExpr callableFactoryVarExpr = VariableExpr.withVariable(Variable.builder().setName("callableFactory").setType(getTransportContext().stubCallableFactoryType()).build());
TypeNode thisClassType = typeStore.get(getTransportContext().classNames().getTransportServiceStubClassName(service));
TypeNode ioExceptionType = TypeNode.withReference(ConcreteReference.withClazz(IOException.class));
BiFunction<List<VariableExpr>, List<Statement>, MethodDefinition> ctorMakerFn = (args, body) -> MethodDefinition.constructorBuilder().setScope(ScopeNode.PROTECTED).setReturnType(thisClassType).setHeaderCommentStatements(Arrays.asList(createProtectedCtorComment(service))).setArguments(args.stream().map(v -> v.toBuilder().setIsDecl(true).build()).collect(Collectors.toList())).setThrowsExceptions(Arrays.asList(ioExceptionType)).setBody(body).build();
// First constructor method.
MethodDefinition firstCtor = ctorMakerFn.apply(Arrays.asList(settingsVarExpr, clientContextVarExpr), Arrays.asList(ExprStatement.withExpr(ReferenceConstructorExpr.thisBuilder().setType(thisClassType).setArguments(settingsVarExpr, clientContextVarExpr, NewObjectExpr.builder().setType(typeStore.get(getTransportContext().classNames().getTransportServiceCallableFactoryClassName(service))).build()).build())));
Expr thisExpr = ValueExpr.withValue(ThisObjectValue.withType(typeStore.get(getTransportContext().classNames().getTransportServiceStubClassName(service))));
// Body of the second constructor method.
List<Statement> secondCtorStatements = new ArrayList<>();
List<Expr> secondCtorExprs = new ArrayList<>();
secondCtorExprs.add(AssignmentExpr.builder().setVariableExpr(classMemberVarExprs.get("callableFactory").toBuilder().setExprReferenceExpr(thisExpr).build()).setValueExpr(callableFactoryVarExpr).build());
VariableExpr operationsStubClassVarExpr = classMemberVarExprs.get(getTransportContext().transportOperationsStubNames().get(0));
// TODO: refactor this
if (generateOperationsStubLogic(service)) {
secondCtorExprs.addAll(createOperationsStubInitExpr(service, thisExpr, operationsStubClassVarExpr, clientContextVarExpr, callableFactoryVarExpr));
}
secondCtorStatements.addAll(secondCtorExprs.stream().map(ExprStatement::withExpr).collect(Collectors.toList()));
secondCtorExprs.clear();
secondCtorStatements.add(EMPTY_LINE_STATEMENT);
// Transport settings local variables.
Map<String, VariableExpr> javaStyleMethodNameToTransportSettingsVarExprs = service.methods().stream().collect(Collectors.toMap(m -> JavaStyle.toLowerCamelCase(m.name()), m -> VariableExpr.withVariable(Variable.builder().setName(String.format("%sTransportSettings", JavaStyle.toLowerCamelCase(m.name()))).setType(TypeNode.withReference(ConcreteReference.builder().setClazz(getTransportContext().callSettingsClass()).setGenerics(Arrays.asList(m.inputType().reference(), m.outputType().reference())).build())).build())));
secondCtorExprs.addAll(service.methods().stream().map(m -> createTransportSettingsInitExpr(m, javaStyleMethodNameToTransportSettingsVarExprs.get(JavaStyle.toLowerCamelCase(m.name())), protoMethodNameToDescriptorVarExprs.get(m.name()), classStatements)).collect(Collectors.toList()));
secondCtorStatements.addAll(secondCtorExprs.stream().map(ExprStatement::withExpr).collect(Collectors.toList()));
secondCtorExprs.clear();
secondCtorStatements.add(EMPTY_LINE_STATEMENT);
// Initialize <method>Callable variables.
secondCtorExprs.addAll(callableClassMemberVarExprs.entrySet().stream().map(e -> createCallableInitExpr(context, service, e.getKey(), e.getValue(), callableFactoryVarExpr, settingsVarExpr, clientContextVarExpr, operationsStubClassVarExpr, thisExpr, javaStyleMethodNameToTransportSettingsVarExprs)).collect(Collectors.toList()));
secondCtorStatements.addAll(secondCtorExprs.stream().map(ExprStatement::withExpr).collect(Collectors.toList()));
secondCtorExprs.clear();
secondCtorStatements.add(EMPTY_LINE_STATEMENT);
secondCtorStatements.addAll(createLongRunningClient(service, typeStore));
// Instantiate backgroundResources.
MethodInvocationExpr getBackgroundResourcesMethodExpr = MethodInvocationExpr.builder().setExprReferenceExpr(clientContextVarExpr).setMethodName("getBackgroundResources").build();
VariableExpr backgroundResourcesVarExpr = classMemberVarExprs.get("backgroundResources");
secondCtorExprs.add(AssignmentExpr.builder().setVariableExpr(backgroundResourcesVarExpr.toBuilder().setExprReferenceExpr(thisExpr).build()).setValueExpr(NewObjectExpr.builder().setType(FIXED_TYPESTORE.get("BackgroundResourceAggregation")).setArguments(Arrays.asList(getBackgroundResourcesMethodExpr)).build()).build());
secondCtorStatements.addAll(secondCtorExprs.stream().map(ExprStatement::withExpr).collect(Collectors.toList()));
secondCtorExprs.clear();
// Second constructor method.
MethodDefinition secondCtor = ctorMakerFn.apply(Arrays.asList(settingsVarExpr, clientContextVarExpr, callableFactoryVarExpr), secondCtorStatements);
return Arrays.asList(firstCtor, secondCtor);
}
use of com.google.api.generator.engine.ast.MethodInvocationExpr in project gapic-generator-java by googleapis.
the class AbstractServiceClientClassComposer method createMethodVariants.
private static List<MethodDefinition> createMethodVariants(Method method, String clientName, Map<String, Message> messageTypes, TypeStore typeStore, Map<String, ResourceName> resourceNames, List<Sample> samples) {
List<MethodDefinition> javaMethods = new ArrayList<>();
String methodName = JavaStyle.toLowerCamelCase(method.name());
TypeNode methodInputType = method.inputType();
TypeNode methodOutputType = method.isPaged() ? typeStore.get(String.format(PAGED_RESPONSE_TYPE_NAME_PATTERN, method.name())) : method.outputType();
if (method.hasLro()) {
LongrunningOperation lro = method.lro();
methodOutputType = TypeNode.withReference(typeStore.get("OperationFuture").reference().copyAndSetGenerics(Arrays.asList(lro.responseType().reference(), lro.metadataType().reference())));
}
for (List<MethodArgument> signature : method.methodSignatures()) {
// Get the argument list.
List<VariableExpr> arguments = signature.stream().map(methodArg -> VariableExpr.builder().setVariable(Variable.builder().setName(JavaStyle.toLowerCamelCase(methodArg.name())).setType(methodArg.type()).build()).setIsDecl(true).build()).collect(Collectors.toList());
// Request proto builder.
VariableExpr requestVarExpr = VariableExpr.builder().setVariable(Variable.builder().setName("request").setType(methodInputType).build()).setIsDecl(true).build();
Expr requestBuilderExpr = createRequestBuilderExpr(method, signature, typeStore);
AssignmentExpr requestAssignmentExpr = AssignmentExpr.builder().setVariableExpr(requestVarExpr).setValueExpr(requestBuilderExpr).build();
List<Statement> statements = new ArrayList<>();
statements.add(ExprStatement.withExpr(requestAssignmentExpr));
MethodInvocationExpr rpcInvocationExpr = MethodInvocationExpr.builder().setMethodName(String.format(method.hasLro() ? "%sAsync" : "%s", methodName)).setArguments(Arrays.asList(requestVarExpr.toBuilder().setIsDecl(false).build())).setReturnType(methodOutputType).build();
Optional<Sample> methodSample = Optional.of(ServiceClientHeaderSampleComposer.composeShowcaseMethodSample(method, typeStore.get(clientName), signature, resourceNames, messageTypes));
Optional<String> methodDocSample = Optional.empty();
if (methodSample.isPresent()) {
samples.add(methodSample.get());
methodDocSample = Optional.of(SampleCodeWriter.writeInlineSample(methodSample.get().body()));
}
MethodDefinition.Builder methodVariantBuilder = MethodDefinition.builder().setHeaderCommentStatements(ServiceClientCommentComposer.createRpcMethodHeaderComment(method, signature, methodDocSample)).setScope(ScopeNode.PUBLIC).setIsFinal(true).setName(String.format(method.hasLro() ? "%sAsync" : "%s", methodName)).setArguments(arguments);
if (isProtoEmptyType(methodOutputType)) {
statements.add(ExprStatement.withExpr(rpcInvocationExpr));
methodVariantBuilder = methodVariantBuilder.setReturnType(TypeNode.VOID);
} else {
methodVariantBuilder = methodVariantBuilder.setReturnType(methodOutputType).setReturnExpr(rpcInvocationExpr);
}
if (method.isDeprecated()) {
methodVariantBuilder = methodVariantBuilder.setAnnotations(Arrays.asList(AnnotationNode.withType(TypeNode.DEPRECATED)));
}
methodVariantBuilder = methodVariantBuilder.setBody(statements);
javaMethods.add(methodVariantBuilder.build());
}
return javaMethods;
}
Aggregations