use of com.google.api.generator.engine.ast.TypeNode in project gapic-generator-java by googleapis.
the class AbstractServiceClientClassComposer method createGetterMethods.
private List<MethodDefinition> createGetterMethods(Service service, TypeStore typeStore, boolean hasLroClient) {
Map<String, TypeNode> methodNameToTypes = new LinkedHashMap<>();
methodNameToTypes.put("getSettings", typeStore.get(ClassNames.getServiceSettingsClassName(service)));
methodNameToTypes.put("getStub", typeStore.get(ClassNames.getServiceStubClassName(service)));
Set<String> getOperationsClientMethodNames = new HashSet<>();
if (hasLroClient) {
Iterator<String> opClientNamesIt = getTransportContext().operationsClientNames().iterator();
Iterator<TypeNode> opClientTypesIt = getTransportContext().operationsClientTypes().iterator();
while (opClientNamesIt.hasNext() && opClientTypesIt.hasNext()) {
String opClientMethodName = String.format("get%s", JavaStyle.toUpperCamelCase(opClientNamesIt.next()));
getOperationsClientMethodNames.add(opClientMethodName);
methodNameToTypes.put(opClientMethodName, opClientTypesIt.next());
}
}
AnnotationNode betaStubAnnotation = AnnotationNode.builder().setType(typeStore.get("BetaApi")).setDescription("A restructuring of stub classes is planned, so this may break in the future").build();
return methodNameToTypes.entrySet().stream().map(e -> {
String methodName = e.getKey();
TypeNode methodReturnType = e.getValue();
String returnVariableName = JavaStyle.toLowerCamelCase(methodName.substring(3));
MethodDefinition.Builder methodBuilder = MethodDefinition.builder();
if (getOperationsClientMethodNames.contains(methodName)) {
methodBuilder = methodBuilder.setHeaderCommentStatements(ServiceClientCommentComposer.GET_OPERATIONS_CLIENT_METHOD_COMMENT);
}
return methodBuilder.setAnnotations(methodName.equals("getStub") ? Arrays.asList(betaStubAnnotation) : Collections.emptyList()).setScope(ScopeNode.PUBLIC).setName(methodName).setIsFinal(!methodName.equals("getStub")).setReturnType(methodReturnType).setReturnExpr(VariableExpr.builder().setVariable(Variable.builder().setName(returnVariableName).setType(methodReturnType).build()).build()).build();
}).collect(Collectors.toList());
}
use of com.google.api.generator.engine.ast.TypeNode 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.TypeNode in project gapic-generator-java by googleapis.
the class AbstractServiceClientClassComposer method buildNestedSetterInvocationExpr.
@VisibleForTesting
static MethodInvocationExpr buildNestedSetterInvocationExpr(MethodArgument argument, Expr referenceExpr) {
String argumentName = JavaStyle.toLowerCamelCase(argument.name());
TypeNode argumentType = argument.type();
Expr argVarExpr = VariableExpr.withVariable(Variable.builder().setName(argumentName).setType(argumentType).build());
if (argument.isResourceNameHelper()) {
Expr nullExpr = ValueExpr.createNullExpr();
Expr isNullCheckExpr = RelationalOperationExpr.equalToWithExprs(argVarExpr, nullExpr);
MethodInvocationExpr toStringExpr = MethodInvocationExpr.builder().setExprReferenceExpr(argVarExpr).setMethodName("toString").setReturnType(TypeNode.STRING).build();
argVarExpr = TernaryExpr.builder().setConditionExpr(isNullCheckExpr).setThenExpr(nullExpr).setElseExpr(toStringExpr).build();
}
String setterMethodName = String.format(typeToSetterMethodName(argumentType), JavaStyle.toUpperCamelCase(argumentName));
return MethodInvocationExpr.builder().setExprReferenceExpr(referenceExpr).setMethodName(setterMethodName).setArguments(argVarExpr).build();
}
use of com.google.api.generator.engine.ast.TypeNode in project gapic-generator-java by googleapis.
the class AbstractServiceSettingsClassComposer method createNestedBuilderApplyToAllUnaryMethod.
private static MethodDefinition createNestedBuilderApplyToAllUnaryMethod(Service service, TypeStore typeStore) {
TypeNode builderType = typeStore.get(BUILDER_CLASS_NAME);
String javaMethodName = "applyToAllUnaryMethods";
TypeNode unaryCallSettingsType = TypeNode.withReference(ConcreteReference.builder().setClazz(UnaryCallSettings.Builder.class).setGenerics(Arrays.asList(TypeNode.WILDCARD_REFERENCE, TypeNode.WILDCARD_REFERENCE)).build());
VariableExpr settingsUpdaterVarExpr = VariableExpr.withVariable(Variable.builder().setName("settingsUpdater").setType(TypeNode.withReference(FIXED_TYPESTORE.get("ApiFunction").reference().copyAndSetGenerics(Arrays.asList(unaryCallSettingsType.reference(), TypeNode.VOID_OBJECT.reference())))).build());
MethodInvocationExpr builderMethodExpr = MethodInvocationExpr.builder().setMethodName("getStubSettingsBuilder").build();
builderMethodExpr = MethodInvocationExpr.builder().setExprReferenceExpr(builderMethodExpr).setMethodName("unaryMethodSettingsBuilders").build();
MethodInvocationExpr applyMethodExpr = MethodInvocationExpr.builder().setExprReferenceExpr(ValueExpr.withValue(SuperObjectValue.withType(TypeNode.withReference(ConcreteReference.withClazz(ClientSettings.Builder.class))))).setMethodName(javaMethodName).setArguments(Arrays.asList(builderMethodExpr, settingsUpdaterVarExpr)).build();
return MethodDefinition.builder().setHeaderCommentStatements(SettingsCommentComposer.APPLY_TO_ALL_UNARY_METHODS_METHOD_COMMENTS).setScope(ScopeNode.PUBLIC).setReturnType(builderType).setName(javaMethodName).setArguments(Arrays.asList(settingsUpdaterVarExpr.toBuilder().setIsDecl(true).build())).setBody(Arrays.asList(ExprStatement.withExpr(applyMethodExpr))).setReturnExpr(ValueExpr.withValue(ThisObjectValue.withType(builderType))).build();
}
use of com.google.api.generator.engine.ast.TypeNode 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);
}
Aggregations