use of com.google.api.gax.rpc.StubSettings 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.gax.rpc.StubSettings 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.gax.rpc.StubSettings in project gapic-generator-java by googleapis.
the class AbstractServiceSettingsClassComposer method createSettingsGetterMethods.
// TODO(miraleung): Consider merging this with createNestedBuilderSettingsGetterMethods.
private static List<MethodDefinition> createSettingsGetterMethods(Service service, TypeStore typeStore) {
TypeNode stubSettingsType = typeStore.get(ClassNames.getServiceStubSettingsClassName(service));
BiFunction<TypeNode, String, MethodDefinition.Builder> methodMakerFn = (retType, javaMethodName) -> MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setReturnType(retType).setName(javaMethodName).setReturnExpr(MethodInvocationExpr.builder().setExprReferenceExpr(CastExpr.builder().setType(stubSettingsType).setExpr(MethodInvocationExpr.builder().setMethodName("getStubSettings").setReturnType(FIXED_TYPESTORE.get("StubSettings")).build()).build()).setMethodName(javaMethodName).setReturnType(retType).build());
List<MethodDefinition> javaMethods = new ArrayList<>();
for (Method protoMethod : service.methods()) {
String javaStyleName = JavaStyle.toLowerCamelCase(protoMethod.name());
String javaMethodName = String.format("%sSettings", JavaStyle.toLowerCamelCase(protoMethod.name()));
MethodDefinition.Builder methodBuilder = methodMakerFn.apply(getCallSettingsType(protoMethod, typeStore), javaMethodName);
javaMethods.add(methodBuilder.setHeaderCommentStatements(SettingsCommentComposer.createCallSettingsGetterComment(getMethodNameFromSettingsVarName(javaMethodName), protoMethod.isDeprecated())).setAnnotations(protoMethod.isDeprecated() ? Arrays.asList(AnnotationNode.withType(TypeNode.DEPRECATED)) : Collections.emptyList()).build());
if (protoMethod.hasLro()) {
javaMethodName = String.format("%sOperationSettings", javaStyleName);
methodBuilder = methodMakerFn.apply(getOperationCallSettingsType(protoMethod), javaMethodName);
javaMethods.add(methodBuilder.setHeaderCommentStatements(SettingsCommentComposer.createCallSettingsGetterComment(getMethodNameFromSettingsVarName(javaMethodName), protoMethod.isDeprecated())).setAnnotations(protoMethod.isDeprecated() ? Arrays.asList(AnnotationNode.withType(TypeNode.DEPRECATED)) : Collections.emptyList()).build());
}
}
return javaMethods;
}
Aggregations