use of com.google.api.generator.engine.ast.MethodDefinition in project gapic-generator-java by googleapis.
the class AbstractServiceStubSettingsClassComposer method createClassConstructor.
private static MethodDefinition createClassConstructor(Service service, Map<String, VariableExpr> methodSettingsMemberVarExprs, TypeStore typeStore) {
TypeNode thisType = typeStore.get(ClassNames.getServiceStubSettingsClassName(service));
final VariableExpr settingsBuilderVarExpr = VariableExpr.withVariable(Variable.builder().setType(typeStore.get(NESTED_BUILDER_CLASS_NAME)).setName("settingsBuilder").build());
Expr superCtorExpr = ReferenceConstructorExpr.superBuilder().setType(FIXED_TYPESTORE.get("StubSettings")).setArguments(settingsBuilderVarExpr).build();
List<Statement> bodyStatements = new ArrayList<>();
bodyStatements.add(ExprStatement.withExpr(superCtorExpr));
bodyStatements.add(EMPTY_LINE_STATEMENT);
Function<Map.Entry<String, VariableExpr>, AssignmentExpr> varInitExprFn = e -> AssignmentExpr.builder().setVariableExpr(e.getValue()).setValueExpr(MethodInvocationExpr.builder().setExprReferenceExpr(MethodInvocationExpr.builder().setExprReferenceExpr(settingsBuilderVarExpr).setMethodName(e.getKey()).build()).setMethodName("build").setReturnType(e.getValue().type()).build()).build();
bodyStatements.addAll(methodSettingsMemberVarExprs.entrySet().stream().map(e -> ExprStatement.withExpr(varInitExprFn.apply(e))).collect(Collectors.toList()));
return MethodDefinition.constructorBuilder().setScope(ScopeNode.PROTECTED).setReturnType(thisType).setArguments(settingsBuilderVarExpr.toBuilder().setIsDecl(true).build()).setThrowsExceptions(Arrays.asList(TypeNode.withExceptionClazz(IOException.class))).setBody(bodyStatements).build();
}
use of com.google.api.generator.engine.ast.MethodDefinition in project gapic-generator-java by googleapis.
the class AbstractServiceStubSettingsClassComposer method createPagedListResponseFactoryAssignExpr.
private static Expr createPagedListResponseFactoryAssignExpr(VariableExpr pageStrDescVarExpr, Method method, TypeNode repeatedResponseType, TypeStore typeStore) {
Preconditions.checkState(method.isPaged(), String.format("Method %s is not paged", method.name()));
// Create the PagedListResponseFactory.
TypeNode pagedResponseType = typeStore.get(getPagedResponseTypeName(method.name()));
TypeNode apiFutureType = TypeNode.withReference(ConcreteReference.builder().setClazz(ApiFuture.class).setGenerics(Arrays.asList(pagedResponseType.reference())).build());
VariableExpr callableVarExpr = VariableExpr.withVariable(Variable.builder().setType(TypeNode.withReference(ConcreteReference.builder().setClazz(UnaryCallable.class).setGenerics(Arrays.asList(method.inputType().reference(), method.outputType().reference())).build())).setName("callable").build());
VariableExpr requestVarExpr = VariableExpr.withVariable(Variable.builder().setType(method.inputType()).setName("request").build());
VariableExpr contextVarExpr = VariableExpr.withVariable(Variable.builder().setType(FIXED_TYPESTORE.get("ApiCallContext")).setName("context").build());
VariableExpr futureResponseVarExpr = VariableExpr.withVariable(Variable.builder().setType(TypeNode.withReference(ConcreteReference.builder().setClazz(ApiFuture.class).setGenerics(Arrays.asList(method.outputType().reference())).build())).setName("futureResponse").build());
TypeNode pageContextType = TypeNode.withReference(ConcreteReference.builder().setClazz(PageContext.class).setGenerics(Arrays.asList(method.inputType(), method.outputType(), repeatedResponseType).stream().map(t -> t.reference()).collect(Collectors.toList())).build());
VariableExpr pageContextVarExpr = VariableExpr.withVariable(Variable.builder().setType(pageContextType).setName("pageContext").build());
AssignmentExpr pageContextAssignExpr = AssignmentExpr.builder().setVariableExpr(pageContextVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("PageContext")).setMethodName("create").setArguments(callableVarExpr, pageStrDescVarExpr, requestVarExpr, contextVarExpr).setReturnType(pageContextVarExpr.type()).build()).build();
Expr returnExpr = MethodInvocationExpr.builder().setStaticReferenceType(typeStore.get(getPagedResponseTypeName(method.name()))).setMethodName("createAsync").setArguments(pageContextVarExpr, futureResponseVarExpr).setReturnType(apiFutureType).build();
MethodDefinition getFuturePagedResponseMethod = MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setReturnType(apiFutureType).setName("getFuturePagedResponse").setArguments(Arrays.asList(callableVarExpr, requestVarExpr, contextVarExpr, futureResponseVarExpr).stream().map(v -> v.toBuilder().setIsDecl(true).build()).collect(Collectors.toList())).setBody(Arrays.asList(ExprStatement.withExpr(pageContextAssignExpr))).setReturnExpr(returnExpr).build();
// Create the variable.
TypeNode pagedResponseFactoryType = TypeNode.withReference(ConcreteReference.builder().setClazz(PagedListResponseFactory.class).setGenerics(Arrays.asList(method.inputType(), method.outputType(), typeStore.get(getPagedResponseTypeName(method.name()))).stream().map(t -> t.reference()).collect(Collectors.toList())).build());
String varName = String.format(PAGED_RESPONSE_FACTORY_PATTERN, JavaStyle.toUpperSnakeCase(method.name()));
VariableExpr pagedListResponseFactoryVarExpr = VariableExpr.withVariable(Variable.builder().setType(pagedResponseFactoryType).setName(varName).build());
AnonymousClassExpr factoryAnonClassExpr = AnonymousClassExpr.builder().setType(pagedResponseFactoryType).setMethods(Arrays.asList(getFuturePagedResponseMethod)).build();
return AssignmentExpr.builder().setVariableExpr(pagedListResponseFactoryVarExpr.toBuilder().setIsDecl(true).setScope(ScopeNode.PRIVATE).setIsStatic(true).setIsFinal(true).build()).setValueExpr(factoryAnonClassExpr).build();
}
use of com.google.api.generator.engine.ast.MethodDefinition in project gapic-generator-java by googleapis.
the class AbstractServiceStubSettingsClassComposer method createNestedClassSettingsBuilderGetterMethods.
private static List<MethodDefinition> createNestedClassSettingsBuilderGetterMethods(Map<String, VariableExpr> nestedMethodSettingsMemberVarExprs, Set<String> nestedDeprecatedSettingVarNames) {
Reference operationCallSettingsBuilderRef = ConcreteReference.withClazz(OperationCallSettings.Builder.class);
Function<TypeNode, Boolean> isOperationCallSettingsBuilderFn = t -> t.reference().copyAndSetGenerics(ImmutableList.of()).equals(operationCallSettingsBuilderRef);
AnnotationNode lroBetaAnnotation = AnnotationNode.builder().setType(FIXED_TYPESTORE.get("BetaApi")).setDescription("The surface for use by generated code is not stable yet and may change in the" + " future.").build();
AnnotationNode deprecatedAnnotation = AnnotationNode.withType(TypeNode.DEPRECATED);
List<MethodDefinition> javaMethods = new ArrayList<>();
for (Map.Entry<String, VariableExpr> settingsVarEntry : nestedMethodSettingsMemberVarExprs.entrySet()) {
String varName = settingsVarEntry.getKey();
VariableExpr settingsVarExpr = settingsVarEntry.getValue();
boolean isOperationCallSettings = isOperationCallSettingsBuilderFn.apply(settingsVarExpr.type());
List<AnnotationNode> annotations = new ArrayList<>();
if (isOperationCallSettings) {
annotations.add(lroBetaAnnotation);
}
boolean isDeprecated = nestedDeprecatedSettingVarNames.contains(varName);
if (isDeprecated) {
annotations.add(deprecatedAnnotation);
}
javaMethods.add(MethodDefinition.builder().setHeaderCommentStatements(SettingsCommentComposer.createCallSettingsBuilderGetterComment(getMethodNameFromSettingsVarName(varName), isDeprecated)).setAnnotations(annotations).setScope(ScopeNode.PUBLIC).setReturnType(settingsVarExpr.type()).setName(settingsVarExpr.variable().identifier().name()).setReturnExpr(settingsVarExpr).build());
}
return javaMethods;
}
use of com.google.api.generator.engine.ast.MethodDefinition in project gapic-generator-java by googleapis.
the class AbstractServiceClientClassComposer method createNestedRpcPagedResponseClass.
private static ClassDefinition createNestedRpcPagedResponseClass(Method method, TypeNode repeatedResponseType, Map<String, Message> messageTypes, TypeStore typeStore) {
Preconditions.checkState(method.isPaged(), String.format("Expected method %s to be paged", method.name()));
String className = String.format("%sPagedResponse", JavaStyle.toUpperCamelCase(method.name()));
TypeNode thisClassType = typeStore.get(className);
String upperJavaMethodName = JavaStyle.toUpperCamelCase(method.name());
TypeNode methodPageType = typeStore.get(String.format("%sPage", upperJavaMethodName));
TypeNode classExtendsType = TypeNode.withReference(ConcreteReference.builder().setClazz(AbstractPagedListResponse.class).setGenerics(Arrays.asList(method.inputType(), method.outputType(), repeatedResponseType, methodPageType, typeStore.get(String.format("%sFixedSizeCollection", upperJavaMethodName))).stream().map(t -> t.reference()).collect(Collectors.toList())).build());
// createAsync method - variables.
VariableExpr contextVarExpr = VariableExpr.withVariable(Variable.builder().setName("context").setType(TypeNode.withReference(ConcreteReference.builder().setClazz(PageContext.class).setGenerics(Arrays.asList(method.inputType(), method.outputType(), repeatedResponseType).stream().map(t -> t.reference()).collect(Collectors.toList())).build())).build());
VariableExpr futureResponseVarExpr = VariableExpr.withVariable(Variable.builder().setName("futureResponse").setType(TypeNode.withReference(ConcreteReference.builder().setClazz(ApiFuture.class).setGenerics(Arrays.asList(method.outputType().reference())).build())).build());
VariableExpr futurePageVarExpr = VariableExpr.withVariable(Variable.builder().setName("futurePage").setType(TypeNode.withReference(ConcreteReference.builder().setClazz(ApiFuture.class).setGenerics(Arrays.asList(methodPageType.reference())).build())).build());
// createAsync method - assignment expression.
MethodInvocationExpr createPageAsyncExpr = MethodInvocationExpr.builder().setStaticReferenceType(methodPageType).setMethodName("createEmptyPage").build();
createPageAsyncExpr = MethodInvocationExpr.builder().setExprReferenceExpr(createPageAsyncExpr).setMethodName("createPageAsync").setArguments(contextVarExpr, futureResponseVarExpr).setReturnType(futurePageVarExpr.type()).build();
AssignmentExpr futurePageAssignExpr = AssignmentExpr.builder().setVariableExpr(futurePageVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(createPageAsyncExpr).build();
// createAsync method - anonymous class expression.
VariableExpr inputVarExpr = VariableExpr.withVariable(Variable.builder().setName("input").setType(methodPageType).build());
// Overrides ApiFunction.apply.
// (https://github.com/googleapis/api-common-java/blob/debf25960dea0367b0d3b5e16d57d76c1d01947e/src/main/java/com/google/api/core/ApiFunction.java).
Expr pageToTransformExpr = LambdaExpr.builder().setArguments(inputVarExpr.toBuilder().setIsDecl(true).build()).setReturnExpr(NewObjectExpr.builder().setType(thisClassType).setArguments(inputVarExpr).build()).build();
// createAsync method - return expression.
TypeNode returnType = TypeNode.withReference(ConcreteReference.builder().setClazz(ApiFuture.class).setGenerics(Arrays.asList(typeStore.get(className).reference())).build());
Expr returnExpr = MethodInvocationExpr.builder().setStaticReferenceType(typeStore.get("ApiFutures")).setMethodName("transform").setArguments(futurePageVarExpr, pageToTransformExpr, MethodInvocationExpr.builder().setStaticReferenceType(typeStore.get("MoreExecutors")).setMethodName("directExecutor").build()).setReturnType(returnType).build();
MethodDefinition createAsyncMethod = MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setIsStatic(true).setReturnType(returnType).setName("createAsync").setArguments(Arrays.asList(contextVarExpr, futureResponseVarExpr).stream().map(e -> e.toBuilder().setIsDecl(true).build()).collect(Collectors.toList())).setBody(Arrays.asList(ExprStatement.withExpr(futurePageAssignExpr))).setReturnExpr(returnExpr).build();
// Private constructor.
VariableExpr pageVarExpr = VariableExpr.withVariable(Variable.builder().setName("page").setType(methodPageType).build());
MethodDefinition privateCtor = MethodDefinition.constructorBuilder().setScope(ScopeNode.PRIVATE).setReturnType(thisClassType).setArguments(pageVarExpr.toBuilder().setIsDecl(true).build()).setBody(Arrays.asList(ExprStatement.withExpr(// Shortcut.
ReferenceConstructorExpr.superBuilder().setType(methodPageType).setArguments(pageVarExpr, MethodInvocationExpr.builder().setStaticReferenceType(typeStore.get(String.format("%sFixedSizeCollection", upperJavaMethodName))).setMethodName("createEmptyCollection").build()).build()))).build();
List<MethodDefinition> javaMethods = new ArrayList<>();
javaMethods.add(createAsyncMethod);
javaMethods.add(privateCtor);
return ClassDefinition.builder().setIsNested(true).setScope(ScopeNode.PUBLIC).setIsStatic(true).setExtendsType(classExtendsType).setName(className).setMethods(javaMethods).build();
}
use of com.google.api.generator.engine.ast.MethodDefinition in project gapic-generator-java by googleapis.
the class AbstractServiceClientClassComposer method createCallableMethod.
private static MethodDefinition createCallableMethod(Service service, Method method, CallableMethodKind callableMethodKind, TypeStore typeStore, Map<String, Message> messageTypes, Map<String, ResourceName> resourceNames, List<Sample> samples) {
TypeNode rawCallableReturnType = null;
if (callableMethodKind.equals(CallableMethodKind.LRO)) {
rawCallableReturnType = typeStore.get("OperationCallable");
} else {
switch(method.stream()) {
case CLIENT:
rawCallableReturnType = typeStore.get("ClientStreamingCallable");
break;
case SERVER:
rawCallableReturnType = typeStore.get("ServerStreamingCallable");
break;
case BIDI:
rawCallableReturnType = typeStore.get("BidiStreamingCallable");
break;
case NONE:
// Fall through.
default:
rawCallableReturnType = typeStore.get("UnaryCallable");
}
}
// Set generics.
TypeNode returnType = TypeNode.withReference(rawCallableReturnType.reference().copyAndSetGenerics(getGenericsForCallable(callableMethodKind, method, typeStore)));
String rawMethodName = JavaStyle.toLowerCamelCase(method.name());
String methodName = getCallableName(callableMethodKind, rawMethodName);
TypeNode stubType = typeStore.get(ClassNames.getServiceStubClassName(service));
MethodInvocationExpr returnExpr = MethodInvocationExpr.builder().setExprReferenceExpr(VariableExpr.builder().setVariable(Variable.builder().setName("stub").setType(stubType).build()).build()).setMethodName(methodName).setReturnType(returnType).build();
Optional<Sample> sampleCode = Optional.empty();
if (callableMethodKind.equals(CallableMethodKind.LRO)) {
sampleCode = Optional.of(ServiceClientCallableMethodSampleComposer.composeLroCallableMethod(method, typeStore.get(ClassNames.getServiceClientClassName(service)), resourceNames, messageTypes));
} else if (callableMethodKind.equals(CallableMethodKind.PAGED)) {
sampleCode = Optional.of(ServiceClientCallableMethodSampleComposer.composePagedCallableMethod(method, typeStore.get(ClassNames.getServiceClientClassName(service)), resourceNames, messageTypes));
} else if (callableMethodKind.equals(CallableMethodKind.REGULAR)) {
if (method.stream().equals(Stream.NONE)) {
sampleCode = Optional.of(ServiceClientCallableMethodSampleComposer.composeRegularCallableMethod(method, typeStore.get(ClassNames.getServiceClientClassName(service)), resourceNames, messageTypes));
} else {
sampleCode = Optional.of(ServiceClientCallableMethodSampleComposer.composeStreamCallableMethod(method, typeStore.get(ClassNames.getServiceClientClassName(service)), resourceNames, messageTypes));
}
}
Optional<String> sampleDocCode = Optional.empty();
if (sampleCode.isPresent()) {
samples.add(sampleCode.get());
sampleDocCode = Optional.of(SampleCodeWriter.writeInlineSample(sampleCode.get().body()));
}
MethodDefinition.Builder methodDefBuilder = MethodDefinition.builder();
if (method.isDeprecated()) {
methodDefBuilder = methodDefBuilder.setAnnotations(Arrays.asList(AnnotationNode.withType(TypeNode.DEPRECATED)));
}
return methodDefBuilder.setHeaderCommentStatements(ServiceClientCommentComposer.createRpcCallableMethodHeaderComment(method, sampleDocCode)).setScope(ScopeNode.PUBLIC).setIsFinal(true).setName(methodName).setReturnType(returnType).setReturnExpr(returnExpr).build();
}
Aggregations