use of com.google.api.generator.engine.ast.TypeNode 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.engine.ast.TypeNode 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.TypeNode in project gapic-generator-java by googleapis.
the class AbstractServiceStubSettingsClassComposer method createNestedClassApplyToAllUnaryMethodsMethod.
private static MethodDefinition createNestedClassApplyToAllUnaryMethodsMethod(TypeNode superType, TypeStore typeStore) {
List<Reference> apiFunctionTypeGenerics = new ArrayList<>();
apiFunctionTypeGenerics.addAll(NESTED_UNARY_METHOD_SETTINGS_BUILDERS_VAR_EXPR.type().reference().generics());
apiFunctionTypeGenerics.add(TypeNode.VOID_OBJECT.reference());
TypeNode settingsUpdaterType = TypeNode.withReference(ConcreteReference.builder().setClazz(ApiFunction.class).setGenerics(apiFunctionTypeGenerics).build());
VariableExpr settingsUpdaterVarExpr = VariableExpr.withVariable(Variable.builder().setType(settingsUpdaterType).setName("settingsUpdater").build());
String methodName = "applyToAllUnaryMethods";
Expr superApplyExpr = MethodInvocationExpr.builder().setExprReferenceExpr(ValueExpr.withValue(SuperObjectValue.withType(superType))).setMethodName(methodName).setArguments(NESTED_UNARY_METHOD_SETTINGS_BUILDERS_VAR_EXPR, settingsUpdaterVarExpr).build();
TypeNode returnType = typeStore.get(NESTED_BUILDER_CLASS_NAME);
Expr returnExpr = ValueExpr.withValue(ThisObjectValue.withType(returnType));
return MethodDefinition.builder().setHeaderCommentStatements(SettingsCommentComposer.APPLY_TO_ALL_UNARY_METHODS_METHOD_COMMENTS).setScope(ScopeNode.PUBLIC).setReturnType(returnType).setName(methodName).setArguments(settingsUpdaterVarExpr.toBuilder().setIsDecl(true).build()).setBody(Arrays.asList(ExprStatement.withExpr(superApplyExpr))).setReturnExpr(returnExpr).build();
}
use of com.google.api.generator.engine.ast.TypeNode in project gapic-generator-java by googleapis.
the class AbstractServiceStubSettingsClassComposer method createCreateStubMethod.
private MethodDefinition createCreateStubMethod(Service service, TypeStore typeStore) {
// Set up the if-statement.
List<Statement> bodyStatements = new ArrayList<>();
Expr getTransportNameExpr = MethodInvocationExpr.builder().setMethodName("getTransportChannelProvider").build();
getTransportNameExpr = MethodInvocationExpr.builder().setExprReferenceExpr(getTransportNameExpr).setMethodName("getTransportName").build();
Iterator<TypeNode> channelTypesIt = getTransportContext().transportChannelTypes().iterator();
Iterator<String> getterNameIt = getTransportContext().transportGetterNames().iterator();
Iterator<String> serivceStubClassNameIt = getTransportContext().classNames().getTransportServiceStubClassNames(service).iterator();
while (channelTypesIt.hasNext() && getterNameIt.hasNext()) {
TypeNode channelType = channelTypesIt.next();
String getterName = getterNameIt.next();
String serivceStubClassName = serivceStubClassNameIt.next();
Expr tRansportNameExpr = MethodInvocationExpr.builder().setStaticReferenceType(channelType).setMethodName(getterName).build();
Expr ifConditionExpr = MethodInvocationExpr.builder().setExprReferenceExpr(getTransportNameExpr).setMethodName("equals").setArguments(tRansportNameExpr).setReturnType(TypeNode.BOOLEAN).build();
Expr createExpr = MethodInvocationExpr.builder().setStaticReferenceType(typeStore.get(serivceStubClassName)).setMethodName("create").setArguments(ValueExpr.withValue(ThisObjectValue.withType(typeStore.get(ClassNames.getServiceStubSettingsClassName(service))))).build();
IfStatement ifStatement = IfStatement.builder().setConditionExpr(ifConditionExpr).setBody(Arrays.asList(ExprStatement.withExpr(ReturnExpr.withExpr(createExpr)))).build();
bodyStatements.add(ifStatement);
}
// Set up exception throwing.
Expr errorMessageExpr = MethodInvocationExpr.builder().setStaticReferenceType(TypeNode.STRING).setMethodName("format").setArguments(ValueExpr.withValue(StringObjectValue.withValue("Transport not supported: %s")), getTransportNameExpr).setReturnType(TypeNode.STRING).build();
TypeNode exceptionType = TypeNode.withExceptionClazz(UnsupportedOperationException.class);
Statement throwStatement = ExprStatement.withExpr(ThrowExpr.builder().setType(exceptionType).setMessageExpr(errorMessageExpr).build());
bodyStatements.add(throwStatement);
// Put the method together.
TypeNode returnType = typeStore.get(ClassNames.getServiceStubClassName(service));
AnnotationNode annotation = AnnotationNode.builder().setType(FIXED_TYPESTORE.get("BetaApi")).setDescription("A restructuring of stub classes is planned, so this may break in the future").build();
return MethodDefinition.builder().setAnnotations(Arrays.asList(annotation)).setScope(ScopeNode.PUBLIC).setReturnType(returnType).setName("createStub").setThrowsExceptions(Arrays.asList(TypeNode.withExceptionClazz(IOException.class))).setBody(bodyStatements).build();
}
use of com.google.api.generator.engine.ast.TypeNode 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;
}
Aggregations