use of com.google.api.generator.engine.ast.MethodDefinition 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;
}
use of com.google.api.generator.engine.ast.MethodDefinition in project gapic-generator-java by googleapis.
the class AbstractServiceClientClassComposer method createServiceMethods.
private static List<MethodDefinition> createServiceMethods(Service service, Map<String, Message> messageTypes, TypeStore typeStore, Map<String, ResourceName> resourceNames, Map<String, List<String>> grpcRpcToJavaMethodMetadata, List<Sample> samples) {
List<MethodDefinition> javaMethods = new ArrayList<>();
Function<MethodDefinition, String> javaMethodNameFn = m -> m.methodIdentifier().name();
for (Method method : service.methods()) {
if (!grpcRpcToJavaMethodMetadata.containsKey(method.name())) {
grpcRpcToJavaMethodMetadata.put(method.name(), new ArrayList<>());
}
if (method.stream().equals(Stream.NONE)) {
List<MethodDefinition> generatedMethods = createMethodVariants(method, ClassNames.getServiceClientClassName(service), messageTypes, typeStore, resourceNames, samples);
// Collect data for gapic_metadata.json.
grpcRpcToJavaMethodMetadata.get(method.name()).addAll(generatedMethods.stream().map(m -> javaMethodNameFn.apply(m)).collect(Collectors.toList()));
javaMethods.addAll(generatedMethods);
MethodDefinition generatedMethod = createMethodDefaultMethod(method, ClassNames.getServiceClientClassName(service), messageTypes, typeStore, resourceNames, samples);
// Collect data for gapic_metadata.json.
grpcRpcToJavaMethodMetadata.get(method.name()).add(javaMethodNameFn.apply(generatedMethod));
javaMethods.add(generatedMethod);
}
if (method.hasLro()) {
MethodDefinition generatedMethod = createLroCallableMethod(service, method, typeStore, messageTypes, resourceNames, samples);
// Collect data for gapic_metadata.json.
grpcRpcToJavaMethodMetadata.get(method.name()).add(javaMethodNameFn.apply(generatedMethod));
javaMethods.add(generatedMethod);
}
if (method.isPaged()) {
MethodDefinition generatedMethod = createPagedCallableMethod(service, method, typeStore, messageTypes, resourceNames, samples);
// Collect data for gapic_metadata.json.
grpcRpcToJavaMethodMetadata.get(method.name()).add(javaMethodNameFn.apply(generatedMethod));
javaMethods.add(generatedMethod);
}
MethodDefinition generatedMethod = createCallableMethod(service, method, typeStore, messageTypes, resourceNames, samples);
// Collect data for the gapic_metadata.json file.
grpcRpcToJavaMethodMetadata.get(method.name()).add(javaMethodNameFn.apply(generatedMethod));
javaMethods.add(generatedMethod);
}
return javaMethods;
}
use of com.google.api.generator.engine.ast.MethodDefinition in project gapic-generator-java by googleapis.
the class AbstractServiceSettingsClassComposer method createNestedBuilderConstructorMethods.
private static List<MethodDefinition> createNestedBuilderConstructorMethods(Service service, TypeStore typeStore) {
TypeNode builderType = typeStore.get(BUILDER_CLASS_NAME);
MethodDefinition noArgCtor = MethodDefinition.constructorBuilder().setScope(ScopeNode.PROTECTED).setReturnType(builderType).setThrowsExceptions(Arrays.asList(FIXED_TYPESTORE.get("IOException"))).setBody(Arrays.asList(ExprStatement.withExpr(ReferenceConstructorExpr.thisBuilder().setType(builderType).setArguments(CastExpr.builder().setType(FIXED_TYPESTORE.get("ClientContext")).setExpr(ValueExpr.createNullExpr()).build()).build()))).build();
BiFunction<VariableExpr, Expr, MethodDefinition> ctorMakerFn = (ctorArg, superArg) -> MethodDefinition.constructorBuilder().setScope(ScopeNode.PROTECTED).setReturnType(builderType).setArguments(Arrays.asList(ctorArg.toBuilder().setIsDecl(true).build())).setBody(Arrays.asList(ExprStatement.withExpr(ReferenceConstructorExpr.superBuilder().setType(FIXED_TYPESTORE.get("ClientSettings")).setArguments(superArg).build()))).build();
VariableExpr clientContextVarExpr = VariableExpr.withVariable(Variable.builder().setName("clientContext").setType(FIXED_TYPESTORE.get("ClientContext")).build());
MethodDefinition clientContextCtor = ctorMakerFn.apply(clientContextVarExpr, MethodInvocationExpr.builder().setStaticReferenceType(typeStore.get(ClassNames.getServiceStubSettingsClassName(service))).setMethodName("newBuilder").setArguments(Arrays.asList(clientContextVarExpr)).build());
VariableExpr settingsVarExpr = VariableExpr.withVariable(Variable.builder().setName("settings").setType(typeStore.get(ClassNames.getServiceSettingsClassName(service))).build());
MethodInvocationExpr settingsBuilderMethodExpr = MethodInvocationExpr.builder().setExprReferenceExpr(settingsVarExpr).setMethodName("getStubSettings").build();
settingsBuilderMethodExpr = MethodInvocationExpr.builder().setExprReferenceExpr(settingsBuilderMethodExpr).setMethodName("toBuilder").build();
MethodDefinition settingsCtor = ctorMakerFn.apply(settingsVarExpr, settingsBuilderMethodExpr);
TypeNode stubSettingsBuilderType = getStubSettingsBuilderType(service);
VariableExpr stubSettingsVarExpr = VariableExpr.withVariable(Variable.builder().setName("stubSettings").setType(stubSettingsBuilderType).build());
MethodDefinition stubSettingsCtor = ctorMakerFn.apply(stubSettingsVarExpr, stubSettingsVarExpr);
return Arrays.asList(noArgCtor, clientContextCtor, settingsCtor, stubSettingsCtor);
}
use of com.google.api.generator.engine.ast.MethodDefinition in project gapic-generator-java by googleapis.
the class AbstractServiceStubClassComposer method createClassMethods.
private List<MethodDefinition> createClassMethods(Service service, Map<String, Message> messageTypes, TypeStore typeStore) {
List<MethodDefinition> methods = new ArrayList<>();
if (service.hasStandardLroMethods()) {
TypeNode operationsStubType = service.operationServiceStubType();
methods.addAll(createOperationsStubGetters(typeStore, operationsStubType));
}
if (service.operationPollingMethod() != null) {
methods.addAll(createLongRunningClientGetters(typeStore));
}
methods.addAll(createCallableGetters(service, messageTypes, typeStore));
methods.addAll(createBackgroundResourceMethodOverrides());
return methods;
}
use of com.google.api.generator.engine.ast.MethodDefinition in project gapic-generator-java by googleapis.
the class AbstractServiceClientClassComposer method createBackgroundResourceMethods.
private static List<MethodDefinition> createBackgroundResourceMethods(Service service, TypeStore typeStore) {
List<MethodDefinition> methods = new ArrayList<>();
VariableExpr stubVarExpr = VariableExpr.withVariable(Variable.builder().setType(typeStore.get(ClassNames.getServiceStubClassName(service))).setName("stub").build());
MethodDefinition closeMethod = MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setIsFinal(true).setReturnType(TypeNode.VOID).setName("close").setBody(Arrays.asList(ExprStatement.withExpr(MethodInvocationExpr.builder().setExprReferenceExpr(stubVarExpr).setMethodName("close").build()))).build();
methods.add(closeMethod);
MethodDefinition shutdownMethod = MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.VOID).setName("shutdown").setBody(Arrays.asList(ExprStatement.withExpr(MethodInvocationExpr.builder().setExprReferenceExpr(stubVarExpr).setMethodName("shutdown").build()))).build();
methods.add(shutdownMethod);
MethodDefinition isShutdownMethod = MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.BOOLEAN).setName("isShutdown").setReturnExpr(MethodInvocationExpr.builder().setExprReferenceExpr(stubVarExpr).setMethodName("isShutdown").setReturnType(TypeNode.BOOLEAN).build()).build();
methods.add(isShutdownMethod);
MethodDefinition isTerminatedMethod = MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.BOOLEAN).setName("isTerminated").setReturnExpr(MethodInvocationExpr.builder().setExprReferenceExpr(stubVarExpr).setMethodName("isTerminated").setReturnType(TypeNode.BOOLEAN).build()).build();
methods.add(isTerminatedMethod);
MethodDefinition shutdownNowMethod = MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.VOID).setName("shutdownNow").setBody(Arrays.asList(ExprStatement.withExpr(MethodInvocationExpr.builder().setExprReferenceExpr(stubVarExpr).setMethodName("shutdownNow").build()))).build();
methods.add(shutdownNowMethod);
List<VariableExpr> arguments = Arrays.asList(VariableExpr.builder().setVariable(Variable.builder().setName("duration").setType(TypeNode.LONG).build()).build(), VariableExpr.builder().setVariable(Variable.builder().setName("unit").setType(typeStore.get("TimeUnit")).build()).build());
MethodDefinition awaitTerminationMethod = MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.BOOLEAN).setName("awaitTermination").setArguments(arguments.stream().map(v -> v.toBuilder().setIsDecl(true).build()).collect(Collectors.toList())).setThrowsExceptions(Arrays.asList(typeStore.get("InterruptedException"))).setReturnExpr(MethodInvocationExpr.builder().setExprReferenceExpr(stubVarExpr).setMethodName("awaitTermination").setArguments(arguments.stream().map(v -> (Expr) v).collect(Collectors.toList())).setReturnType(TypeNode.BOOLEAN).build()).build();
methods.add(awaitTerminationMethod);
return methods;
}
Aggregations