use of com.google.api.generator.gapic.composer.store.TypeStore in project gapic-generator-java by googleapis.
the class AbstractServiceClientClassComposer method createNestedRpcPageClass.
private static ClassDefinition createNestedRpcPageClass(Method method, TypeNode repeatedResponseType, Map<String, Message> messageTypes, TypeStore typeStore) {
Preconditions.checkState(method.isPaged(), String.format("Expected method %s to be paged", method.name()));
String upperJavaMethodName = JavaStyle.toUpperCamelCase(method.name());
String className = String.format("%sPage", upperJavaMethodName);
TypeNode classType = typeStore.get(className);
TypeNode classExtendsType = TypeNode.withReference(ConcreteReference.builder().setClazz(AbstractPage.class).setGenerics(Arrays.asList(method.inputType(), method.outputType(), repeatedResponseType, classType).stream().map(t -> t.reference()).collect(Collectors.toList())).build());
// Private constructor.
VariableExpr contextVarExpr = VariableExpr.withVariable(Variable.builder().setName("context").setType(TypeNode.withReference(ConcreteReference.builder().setClazz(PageContext.class).setGenerics(Arrays.asList(method.inputType(), method.outputType(), repeatedResponseType).stream().map(t -> t.reference()).collect(Collectors.toList())).build())).build());
VariableExpr responseVarExpr = VariableExpr.withVariable(Variable.builder().setName("response").setType(method.outputType()).build());
MethodDefinition privateCtor = MethodDefinition.constructorBuilder().setScope(ScopeNode.PRIVATE).setReturnType(classType).setArguments(Arrays.asList(contextVarExpr, responseVarExpr).stream().map(e -> e.toBuilder().setIsDecl(true).build()).collect(Collectors.toList())).setBody(Arrays.asList(ExprStatement.withExpr(ReferenceConstructorExpr.superBuilder().setType(classExtendsType).setArguments(contextVarExpr, responseVarExpr).build()))).build();
// createEmptyPage method.
ValueExpr nullExpr = ValueExpr.createNullExpr();
MethodDefinition createEmptyPageMethod = MethodDefinition.builder().setScope(ScopeNode.PRIVATE).setIsStatic(true).setReturnType(classType).setName("createEmptyPage").setReturnExpr(NewObjectExpr.builder().setType(classType).setArguments(nullExpr, nullExpr).build()).build();
// createPage method.
MethodDefinition createPageMethod = MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PROTECTED).setReturnType(classType).setName("createPage").setArguments(Arrays.asList(contextVarExpr, responseVarExpr).stream().map(e -> e.toBuilder().setIsDecl(true).build()).collect(Collectors.toList())).setReturnExpr(NewObjectExpr.builder().setType(classType).setArguments(contextVarExpr, responseVarExpr).build()).build();
// createPageAsync method.
Function<TypeNode, TypeNode> futureTypeFn = t -> TypeNode.withReference(ConcreteReference.builder().setClazz(ApiFuture.class).setGenerics(Arrays.asList(t.reference())).build());
VariableExpr futureResponseVarExpr = VariableExpr.withVariable(Variable.builder().setName("futureResponse").setType(futureTypeFn.apply(method.outputType())).build());
TypeNode futurePageType = futureTypeFn.apply(classType);
MethodDefinition createPageAsyncMethod = MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setReturnType(futurePageType).setName("createPageAsync").setArguments(Arrays.asList(contextVarExpr, futureResponseVarExpr).stream().map(e -> e.toBuilder().setIsDecl(true).build()).collect(Collectors.toList())).setReturnExpr(MethodInvocationExpr.builder().setExprReferenceExpr(ValueExpr.withValue(SuperObjectValue.withType(classExtendsType))).setMethodName("createPageAsync").setArguments(contextVarExpr, futureResponseVarExpr).setReturnType(futurePageType).build()).build();
// Build the class.
List<MethodDefinition> javaMethods = new ArrayList<>();
javaMethods.add(privateCtor);
javaMethods.add(createEmptyPageMethod);
javaMethods.add(createPageMethod);
javaMethods.add(createPageAsyncMethod);
return ClassDefinition.builder().setIsNested(true).setScope(ScopeNode.PUBLIC).setIsStatic(true).setExtendsType(classExtendsType).setName(className).setMethods(javaMethods).build();
}
use of com.google.api.generator.gapic.composer.store.TypeStore in project gapic-generator-java by googleapis.
the class AbstractServiceClientClassComposer method createVaporTypes.
private static void createVaporTypes(Service service, TypeStore typeStore) {
// Client stub typeStore.
typeStore.putAll(String.format("%s.stub", service.pakkage()), Arrays.asList(ClassNames.getServiceStubClassName(service), ClassNames.getServiceStubSettingsClassName(service)));
// Client ServiceClient and ServiceSettings typeStore.
typeStore.putAll(service.pakkage(), Arrays.asList(ClassNames.getServiceClientClassName(service), ClassNames.getServiceSettingsClassName(service)));
// Nested class typeStore.
for (Method method : service.methods()) {
if (!method.isPaged()) {
continue;
}
typeStore.putAll(service.pakkage(), Arrays.asList("%sPagedResponse", "%sPage", "%sFixedSizeCollection").stream().map(p -> String.format(p, JavaStyle.toUpperCamelCase(method.name()))).collect(Collectors.toList()), true, ClassNames.getServiceClientClassName(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));
}
use of com.google.api.generator.gapic.composer.store.TypeStore in project gapic-generator-java by googleapis.
the class AbstractServiceClientTestClassComposer method generate.
protected GapicClass generate(String className, GapicContext context, Service service) {
Map<String, ResourceName> resourceNames = context.helperResourceNames();
String pakkage = service.pakkage();
TypeStore typeStore = new TypeStore();
addDynamicTypes(context, service, typeStore);
GapicClass.Kind kind = Kind.MAIN;
Map<String, VariableExpr> classMemberVarExprs = createClassMemberVarExprs(service, context, typeStore);
ClassDefinition classDef = ClassDefinition.builder().setPackageString(pakkage).setAnnotations(createClassAnnotations()).setScope(ScopeNode.PUBLIC).setName(className).setStatements(createClassMemberFieldDecls(classMemberVarExprs)).setMethods(createClassMethods(service, context, classMemberVarExprs, typeStore, resourceNames)).build();
return GapicClass.create(kind, classDef);
}
use of com.google.api.generator.gapic.composer.store.TypeStore in project gapic-generator-java by googleapis.
the class AbstractServiceClientTestClassComposer method addDynamicTypes.
private void addDynamicTypes(GapicContext context, Service service, TypeStore typeStore) {
typeStore.putAll(service.pakkage(), Arrays.asList(ClassNames.getMockServiceClassName(service), ClassNames.getServiceClientClassName(service), ClassNames.getServiceSettingsClassName(service)));
String stubPakkage = String.format("%s.stub", service.pakkage());
typeStore.put(stubPakkage, getTransportContext().classNames().getTransportServiceStubClassName(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));
for (Service mixinService : context.mixinServices()) {
typeStore.put(mixinService.pakkage(), ClassNames.getMockServiceClassName(mixinService));
for (Method mixinMethod : mixinService.methods()) {
if (!mixinMethod.isPaged()) {
continue;
}
typeStore.put(service.pakkage(), String.format(PAGED_RESPONSE_TYPE_NAME_PATTERN, mixinMethod.name()), true, ClassNames.getServiceClientClassName(service));
}
}
}
use of com.google.api.generator.gapic.composer.store.TypeStore in project gapic-generator-java by googleapis.
the class AbstractServiceStubSettingsClassComposer method createDynamicTypes.
private TypeStore createDynamicTypes(Service service, String pakkage) {
TypeStore typeStore = new TypeStore();
// This type.
String thisClassName = ClassNames.getServiceStubSettingsClassName(service);
typeStore.putAll(pakkage, Arrays.asList(thisClassName, ClassNames.getServiceStubSettingsClassName(service), ClassNames.getServiceStubClassName(service)));
typeStore.putAll(pakkage, getTransportContext().classNames().getTransportServiceStubClassNames(service));
// Nested builder class.
typeStore.put(pakkage, NESTED_BUILDER_CLASS_NAME, true, thisClassName);
// 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