use of com.google.api.generator.gapic.model.Service 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.gapic.model.Service in project gapic-generator-java by googleapis.
the class AbstractServiceClientClassComposer method createServiceMethods.
private static List<MethodDefinition> createServiceMethods(Service service, Map<String, Message> messageTypes, TypeStore typeStore, Map<String, ResourceName> resourceNames, Map<String, List<String>> grpcRpcToJavaMethodMetadata, List<Sample> samples) {
List<MethodDefinition> javaMethods = new ArrayList<>();
Function<MethodDefinition, String> javaMethodNameFn = m -> m.methodIdentifier().name();
for (Method method : service.methods()) {
if (!grpcRpcToJavaMethodMetadata.containsKey(method.name())) {
grpcRpcToJavaMethodMetadata.put(method.name(), new ArrayList<>());
}
if (method.stream().equals(Stream.NONE)) {
List<MethodDefinition> generatedMethods = createMethodVariants(method, ClassNames.getServiceClientClassName(service), messageTypes, typeStore, resourceNames, samples);
// Collect data for gapic_metadata.json.
grpcRpcToJavaMethodMetadata.get(method.name()).addAll(generatedMethods.stream().map(m -> javaMethodNameFn.apply(m)).collect(Collectors.toList()));
javaMethods.addAll(generatedMethods);
MethodDefinition generatedMethod = createMethodDefaultMethod(method, ClassNames.getServiceClientClassName(service), messageTypes, typeStore, resourceNames, samples);
// Collect data for gapic_metadata.json.
grpcRpcToJavaMethodMetadata.get(method.name()).add(javaMethodNameFn.apply(generatedMethod));
javaMethods.add(generatedMethod);
}
if (method.hasLro()) {
MethodDefinition generatedMethod = createLroCallableMethod(service, method, typeStore, messageTypes, resourceNames, samples);
// Collect data for gapic_metadata.json.
grpcRpcToJavaMethodMetadata.get(method.name()).add(javaMethodNameFn.apply(generatedMethod));
javaMethods.add(generatedMethod);
}
if (method.isPaged()) {
MethodDefinition generatedMethod = createPagedCallableMethod(service, method, typeStore, messageTypes, resourceNames, samples);
// Collect data for gapic_metadata.json.
grpcRpcToJavaMethodMetadata.get(method.name()).add(javaMethodNameFn.apply(generatedMethod));
javaMethods.add(generatedMethod);
}
MethodDefinition generatedMethod = createCallableMethod(service, method, typeStore, messageTypes, resourceNames, samples);
// Collect data for the gapic_metadata.json file.
grpcRpcToJavaMethodMetadata.get(method.name()).add(javaMethodNameFn.apply(generatedMethod));
javaMethods.add(generatedMethod);
}
return javaMethods;
}
use of com.google.api.generator.gapic.model.Service 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.generator.gapic.model.Service in project gapic-generator-java by googleapis.
the class AbstractServiceSettingsClassComposer method createDynamicTypes.
private static TypeStore createDynamicTypes(Service service) {
TypeStore typeStore = new TypeStore();
// ServiceStubSettings class.
typeStore.put(String.format("%s.stub", service.pakkage()), ClassNames.getServiceStubSettingsClassName(service));
// This class.
typeStore.put(service.pakkage(), ClassNames.getServiceSettingsClassName(service));
// Nested Builder class.
typeStore.put(service.pakkage(), BUILDER_CLASS_NAME, true, ClassNames.getServiceSettingsClassName(service));
// Pagination types.
typeStore.putAll(service.pakkage(), service.methods().stream().filter(m -> m.isPaged()).map(m -> String.format(PAGED_RESPONSE_TYPE_NAME_PATTERN, m.name())).collect(Collectors.toList()), true, ClassNames.getServiceClientClassName(service));
return typeStore;
}
use of com.google.api.generator.gapic.model.Service in project gapic-generator-java by googleapis.
the class AbstractServiceStubClassComposer method createTypes.
private static TypeStore createTypes(Service service, Map<String, Message> messageTypes) {
List<Class<?>> concreteClazzes = Arrays.asList(BackgroundResource.class, BetaApi.class, BidiStreamingCallable.class, ClientStreamingCallable.class, Generated.class, Operation.class, OperationCallable.class, ServerStreamingCallable.class, UnaryCallable.class, UnsupportedOperationException.class);
TypeStore typeStore = new TypeStore(concreteClazzes);
typeStore.put("com.google.longrunning.stub", "OperationsStub");
// Pagination types.
typeStore.putAll(service.pakkage(), service.methods().stream().filter(m -> m.isPaged()).map(m -> String.format(PAGED_RESPONSE_TYPE_NAME_PATTERN, m.name())).collect(Collectors.toList()), true, ClassNames.getServiceClientClassName(service));
return typeStore;
}
Aggregations