use of com.google.api.generator.gapic.model.Service in project gapic-generator-java by googleapis.
the class AbstractServiceStubSettingsClassComposer method createBuilderHelperMethods.
protected List<MethodDefinition> createBuilderHelperMethods(Service service, TypeStore typeStore) {
List<MethodDefinition> javaMethods = new ArrayList<>();
final TypeNode builderReturnType = typeStore.get(NESTED_BUILDER_CLASS_NAME);
// Create the newBuilder(ClientContext) method.
Function<Expr, NewObjectExpr> newBuilderFn = argExpr -> NewObjectExpr.builder().setType(builderReturnType).setArguments(argExpr).build();
VariableExpr clientContextVarExpr = VariableExpr.withVariable(Variable.builder().setType(FIXED_TYPESTORE.get("ClientContext")).setName("clientContext").build());
javaMethods.add(MethodDefinition.builder().setHeaderCommentStatements(SettingsCommentComposer.NEW_BUILDER_METHOD_COMMENT).setScope(ScopeNode.PUBLIC).setIsStatic(true).setReturnType(builderReturnType).setName("newBuilder").setArguments(clientContextVarExpr.toBuilder().setIsDecl(true).build()).setReturnExpr(newBuilderFn.apply(clientContextVarExpr)).build());
// Create the toBuilder method.
javaMethods.add(MethodDefinition.builder().setHeaderCommentStatements(SettingsCommentComposer.TO_BUILDER_METHOD_COMMENT).setScope(ScopeNode.PUBLIC).setReturnType(builderReturnType).setName("toBuilder").setReturnExpr(newBuilderFn.apply(ValueExpr.withValue(ThisObjectValue.withType(typeStore.get(ClassNames.getServiceStubClassName(service)))))).build());
return javaMethods;
}
use of com.google.api.generator.gapic.model.Service in project gapic-generator-java by googleapis.
the class AbstractServiceStubSettingsClassComposer method createNestedClassConstructorMethods.
private static List<MethodDefinition> createNestedClassConstructorMethods(Service service, GapicServiceConfig serviceConfig, Map<String, VariableExpr> nestedMethodSettingsMemberVarExprs, TypeStore typeStore) {
TypeNode builderType = typeStore.get(NESTED_BUILDER_CLASS_NAME);
List<MethodDefinition> ctorMethods = new ArrayList<>();
// First argument-less contructor.
ctorMethods.add(MethodDefinition.constructorBuilder().setScope(ScopeNode.PROTECTED).setReturnType(builderType).setBody(Arrays.asList(ExprStatement.withExpr(ReferenceConstructorExpr.thisBuilder().setType(builderType).setArguments(CastExpr.builder().setType(FIXED_TYPESTORE.get("ClientContext")).setExpr(ValueExpr.createNullExpr()).build()).build()))).build());
// Second ctor that takes a clientContext argument.
VariableExpr clientContextVarExpr = VariableExpr.withVariable(Variable.builder().setType(FIXED_TYPESTORE.get("ClientContext")).setName("clientContext").build());
Reference pagedSettingsBuilderRef = ConcreteReference.withClazz(PagedCallSettings.Builder.class);
Reference batchingSettingsBuilderRef = ConcreteReference.withClazz(BatchingCallSettings.Builder.class);
Reference unaryCallSettingsBuilderRef = ConcreteReference.withClazz(UnaryCallSettings.Builder.class);
Function<TypeNode, Boolean> isUnaryCallSettingsBuilderFn = t -> t.reference().copyAndSetGenerics(ImmutableList.of()).equals(unaryCallSettingsBuilderRef);
Function<TypeNode, Boolean> isPagedCallSettingsBuilderFn = t -> t.reference().copyAndSetGenerics(ImmutableList.of()).equals(pagedSettingsBuilderRef);
Function<TypeNode, Boolean> isBatchingCallSettingsBuilderFn = t -> t.reference().copyAndSetGenerics(ImmutableList.of()).equals(batchingSettingsBuilderRef);
Function<TypeNode, TypeNode> builderToCallSettingsFn = t -> TypeNode.withReference(VaporReference.builder().setName(String.join(DOT, t.reference().enclosingClassNames())).setPakkage(t.reference().pakkage()).build());
List<Statement> ctorBodyStatements = new ArrayList<>();
ctorBodyStatements.add(ExprStatement.withExpr(ReferenceConstructorExpr.superBuilder().setType(builderType).setArguments(clientContextVarExpr).build()));
ctorBodyStatements.add(EMPTY_LINE_STATEMENT);
ctorBodyStatements.addAll(nestedMethodSettingsMemberVarExprs.entrySet().stream().map(e -> {
// TODO(miraleung): Extract this into another method.
// Name is fooBarSettings.
VariableExpr varExpr = e.getValue();
TypeNode varType = varExpr.type();
Preconditions.checkState(e.getKey().endsWith(SETTINGS_LITERAL), String.format("%s expected to end with \"Settings\"", e.getKey()));
String methodName = getMethodNameFromSettingsVarName(e.getKey());
if (!isPagedCallSettingsBuilderFn.apply(varType)) {
if (!isBatchingCallSettingsBuilderFn.apply(varType)) {
boolean isUnaryCallSettings = isUnaryCallSettingsBuilderFn.apply(varType);
Expr builderExpr = AssignmentExpr.builder().setVariableExpr(varExpr).setValueExpr(MethodInvocationExpr.builder().setStaticReferenceType(builderToCallSettingsFn.apply(varExpr.type())).setMethodName(isUnaryCallSettings ? "newUnaryCallSettingsBuilder" : "newBuilder").setReturnType(varExpr.type()).build()).build();
return ExprStatement.withExpr(builderExpr);
}
Expr newBatchingSettingsExpr = MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("BatchingSettings")).setMethodName("newBuilder").build();
newBatchingSettingsExpr = MethodInvocationExpr.builder().setExprReferenceExpr(newBatchingSettingsExpr).setMethodName("build").build();
String batchingDescVarName = String.format(BATCHING_DESC_PATTERN, JavaStyle.toUpperSnakeCase(methodName));
Expr batchingSettingsBuilderExpr = MethodInvocationExpr.builder().setStaticReferenceType(builderToCallSettingsFn.apply(varType)).setMethodName("newBuilder").setArguments(VariableExpr.withVariable(Variable.builder().setType(FIXED_TYPESTORE.get("BatchingDescriptor")).setName(batchingDescVarName).build())).build();
batchingSettingsBuilderExpr = MethodInvocationExpr.builder().setExprReferenceExpr(batchingSettingsBuilderExpr).setMethodName("setBatchingSettings").setArguments(newBatchingSettingsExpr).setReturnType(varType).build();
Expr builderExpr = AssignmentExpr.builder().setVariableExpr(varExpr).setValueExpr(batchingSettingsBuilderExpr).build();
return ExprStatement.withExpr(builderExpr);
}
String memberVarName = String.format(PAGED_RESPONSE_FACTORY_PATTERN, JavaStyle.toUpperSnakeCase(methodName));
VariableExpr argVar = VariableExpr.withVariable(Variable.builder().setType(FIXED_TYPESTORE.get("PagedListResponseFactory")).setName(memberVarName).build());
Expr builderExpr = AssignmentExpr.builder().setVariableExpr(varExpr).setValueExpr(MethodInvocationExpr.builder().setStaticReferenceType(builderToCallSettingsFn.apply(varExpr.type())).setMethodName("newBuilder").setArguments(argVar).setReturnType(varExpr.type()).build()).build();
return ExprStatement.withExpr(builderExpr);
}).collect(Collectors.toList()));
Expr unaryMethodSettingsBuildersAssignExpr = AssignmentExpr.builder().setVariableExpr(NESTED_UNARY_METHOD_SETTINGS_BUILDERS_VAR_EXPR).setValueExpr(MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("ImmutableList")).setGenerics(NESTED_UNARY_METHOD_SETTINGS_BUILDERS_VAR_EXPR.type().reference().generics()).setMethodName("of").setArguments(nestedMethodSettingsMemberVarExprs.values().stream().filter(v -> isUnaryCallSettingsBuilderFn.apply(v.type()) || isPagedCallSettingsBuilderFn.apply(v.type()) || isBatchingCallSettingsBuilderFn.apply(v.type())).collect(Collectors.toList())).setReturnType(NESTED_UNARY_METHOD_SETTINGS_BUILDERS_VAR_EXPR.type()).build()).build();
ctorBodyStatements.add(EMPTY_LINE_STATEMENT);
ctorBodyStatements.add(ExprStatement.withExpr(unaryMethodSettingsBuildersAssignExpr));
ctorBodyStatements.add(ExprStatement.withExpr(MethodInvocationExpr.builder().setMethodName("initDefaults").setArguments(ValueExpr.withValue(ThisObjectValue.withType(builderType))).build()));
ctorMethods.add(MethodDefinition.constructorBuilder().setScope(ScopeNode.PROTECTED).setReturnType(builderType).setArguments(clientContextVarExpr.toBuilder().setIsDecl(true).build()).setBody(ctorBodyStatements).build());
// Third constructor that takes a ServiceStubSettings.
TypeNode outerSettingsType = typeStore.get(ClassNames.getServiceStubSettingsClassName(service));
VariableExpr settingsVarExpr = VariableExpr.withVariable(Variable.builder().setType(outerSettingsType).setName("settings").build());
ctorBodyStatements = new ArrayList<>();
ctorBodyStatements.add(ExprStatement.withExpr(ReferenceConstructorExpr.superBuilder().setType(builderType).setArguments(settingsVarExpr).build()));
ctorBodyStatements.add(EMPTY_LINE_STATEMENT);
// TODO(cleanup): Technically this should actually use the outer class's <method>Settings
// members to avoid decoupling variable names.
ctorBodyStatements.addAll(nestedMethodSettingsMemberVarExprs.values().stream().map(v -> ExprStatement.withExpr(AssignmentExpr.builder().setVariableExpr(v).setValueExpr(MethodInvocationExpr.builder().setExprReferenceExpr(VariableExpr.builder().setExprReferenceExpr(settingsVarExpr).setVariable(v.variable()).build()).setMethodName("toBuilder").setReturnType(v.type()).build()).build())).collect(Collectors.toList()));
ctorBodyStatements.add(EMPTY_LINE_STATEMENT);
ctorBodyStatements.add(ExprStatement.withExpr(unaryMethodSettingsBuildersAssignExpr));
ctorMethods.add(MethodDefinition.constructorBuilder().setScope(ScopeNode.PROTECTED).setReturnType(builderType).setArguments(settingsVarExpr.toBuilder().setIsDecl(true).build()).setBody(ctorBodyStatements).build());
return ctorMethods;
}
use of com.google.api.generator.gapic.model.Service 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.gapic.model.Service in project gapic-generator-java by googleapis.
the class AbstractTransportServiceStubClassComposer method createStubOverrideMethods.
private List<MethodDefinition> createStubOverrideMethods(VariableExpr backgroundResourcesVarExpr, Service service) {
Function<String, MethodDefinition.Builder> methodMakerStarterFn = methodName -> MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setName(methodName);
Function<String, MethodDefinition> voidMethodMakerFn = methodName -> methodMakerStarterFn.apply(methodName).setReturnType(TypeNode.VOID).setBody(Arrays.asList(ExprStatement.withExpr(MethodInvocationExpr.builder().setExprReferenceExpr(backgroundResourcesVarExpr).setMethodName(methodName).build()))).build();
Function<String, MethodDefinition> booleanMethodMakerFn = methodName -> methodMakerStarterFn.apply(methodName).setReturnType(TypeNode.BOOLEAN).setReturnExpr(MethodInvocationExpr.builder().setExprReferenceExpr(backgroundResourcesVarExpr).setMethodName(methodName).setReturnType(TypeNode.BOOLEAN).build()).build();
// Generate the close() method:
// @Override
// public final void close() {
// try {
// backgroundResources.close();
// } catch (RuntimeException e) {
// throw e;
// } catch (Exception e) {
// throw new IllegalStateException("Failed to close resource", e);
// }
// }
VariableExpr catchRuntimeExceptionVarExpr = VariableExpr.builder().setVariable(Variable.builder().setType(TypeNode.withExceptionClazz(RuntimeException.class)).setName("e").build()).build();
VariableExpr catchExceptionVarExpr = VariableExpr.builder().setVariable(Variable.builder().setType(TypeNode.withExceptionClazz(Exception.class)).setName("e").build()).build();
List<MethodDefinition> javaMethods = new ArrayList<>();
if (service.operationPollingMethod() != null) {
javaMethods.addAll(createLongRunningClientGetters());
}
javaMethods.add(methodMakerStarterFn.apply("close").setIsFinal(true).setReturnType(TypeNode.VOID).setBody(Arrays.asList(TryCatchStatement.builder().setTryBody(Arrays.asList(ExprStatement.withExpr(MethodInvocationExpr.builder().setExprReferenceExpr(backgroundResourcesVarExpr).setMethodName("close").build()))).addCatch(catchRuntimeExceptionVarExpr.toBuilder().setIsDecl(true).build(), Arrays.asList(ExprStatement.withExpr(ThrowExpr.builder().setThrowExpr(catchRuntimeExceptionVarExpr).build()))).addCatch(catchExceptionVarExpr.toBuilder().setIsDecl(true).build(), Arrays.asList(ExprStatement.withExpr(ThrowExpr.builder().setType(TypeNode.withExceptionClazz(IllegalStateException.class)).setMessageExpr("Failed to close resource").setCauseExpr(catchExceptionVarExpr).build()))).build())).build());
javaMethods.add(voidMethodMakerFn.apply("shutdown"));
javaMethods.add(booleanMethodMakerFn.apply("isShutdown"));
javaMethods.add(booleanMethodMakerFn.apply("isTerminated"));
javaMethods.add(voidMethodMakerFn.apply("shutdownNow"));
List<VariableExpr> awaitTerminationArgs = Arrays.asList(VariableExpr.withVariable(Variable.builder().setName("duration").setType(TypeNode.LONG).build()), VariableExpr.withVariable(Variable.builder().setName("unit").setType(FIXED_TYPESTORE.get("TimeUnit")).build()));
javaMethods.add(methodMakerStarterFn.apply("awaitTermination").setReturnType(TypeNode.BOOLEAN).setArguments(awaitTerminationArgs.stream().map(v -> v.toBuilder().setIsDecl(true).build()).collect(Collectors.toList())).setThrowsExceptions(Arrays.asList(FIXED_TYPESTORE.get("InterruptedException"))).setReturnExpr(MethodInvocationExpr.builder().setExprReferenceExpr(backgroundResourcesVarExpr).setMethodName("awaitTermination").setArguments(awaitTerminationArgs.stream().map(v -> (Expr) v).collect(Collectors.toList())).setReturnType(TypeNode.BOOLEAN).build()).build());
return javaMethods;
}
use of com.google.api.generator.gapic.model.Service 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);
}
Aggregations