Search in sources :

Example 6 with VariableExpr

use of com.google.api.generator.engine.ast.VariableExpr in project gapic-generator-java by googleapis.

the class AbstractServiceStubSettingsClassComposer method createPagedListDescriptorAssignExpr.

private static Expr createPagedListDescriptorAssignExpr(VariableExpr pagedListDescVarExpr, Method method, TypeNode repeatedResponseType, String repeatedFieldName, Map<String, Message> messageTypes, TypeStore typeStore) {
    MethodDefinition.Builder methodStarterBuilder = MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC);
    List<MethodDefinition> anonClassMethods = new ArrayList<>();
    // Create emptyToken method.
    anonClassMethods.add(methodStarterBuilder.setReturnType(TypeNode.STRING).setName("emptyToken").setReturnExpr(ValueExpr.withValue(StringObjectValue.withValue(""))).build());
    // Create injectToken method.
    VariableExpr payloadVarExpr = VariableExpr.withVariable(Variable.builder().setType(method.inputType()).setName("payload").build());
    VariableExpr strTokenVarExpr = VariableExpr.withVariable(Variable.builder().setType(TypeNode.STRING).setName("token").build());
    TypeNode returnType = method.inputType();
    Expr newBuilderExpr = MethodInvocationExpr.builder().setStaticReferenceType(method.inputType()).setMethodName("newBuilder").setArguments(payloadVarExpr).build();
    Expr returnExpr = MethodInvocationExpr.builder().setExprReferenceExpr(newBuilderExpr).setMethodName("setPageToken").setArguments(strTokenVarExpr).build();
    returnExpr = MethodInvocationExpr.builder().setExprReferenceExpr(returnExpr).setMethodName("build").setReturnType(returnType).build();
    anonClassMethods.add(methodStarterBuilder.setReturnType(method.inputType()).setName("injectToken").setArguments(Arrays.asList(payloadVarExpr, strTokenVarExpr).stream().map(v -> v.toBuilder().setIsDecl(true).build()).collect(Collectors.toList())).setReturnExpr(returnExpr).build());
    // Create injectPageSize method.
    VariableExpr pageSizeVarExpr = VariableExpr.withVariable(Variable.builder().setType(TypeNode.INT).setName("pageSize").build());
    // Re-declare for clarity and easier readability.
    returnType = method.inputType();
    returnExpr = MethodInvocationExpr.builder().setExprReferenceExpr(newBuilderExpr).setMethodName("set" + JavaStyle.toUpperCamelCase(method.pageSizeFieldName())).setArguments(pageSizeVarExpr).build();
    returnExpr = MethodInvocationExpr.builder().setExprReferenceExpr(returnExpr).setMethodName("build").setReturnType(returnType).build();
    anonClassMethods.add(methodStarterBuilder.setReturnType(method.inputType()).setName("injectPageSize").setArguments(Arrays.asList(payloadVarExpr, pageSizeVarExpr).stream().map(v -> v.toBuilder().setIsDecl(true).build()).collect(Collectors.toList())).setReturnExpr(returnExpr).build());
    // TODO(miraleung): Test the edge cases where these proto fields aren't present.
    // Create extractPageSize method.
    returnType = TypeNode.INT_OBJECT;
    anonClassMethods.add(methodStarterBuilder.setReturnType(returnType).setName("extractPageSize").setArguments(payloadVarExpr.toBuilder().setIsDecl(true).build()).setReturnExpr(MethodInvocationExpr.builder().setExprReferenceExpr(payloadVarExpr).setMethodName("get" + JavaStyle.toUpperCamelCase(method.pageSizeFieldName())).setReturnType(returnType).build()).build());
    // Create extractNextToken method.
    returnType = TypeNode.STRING;
    payloadVarExpr = VariableExpr.withVariable(Variable.builder().setType(method.outputType()).setName("payload").build());
    anonClassMethods.add(methodStarterBuilder.setReturnType(returnType).setName("extractNextToken").setArguments(payloadVarExpr.toBuilder().setIsDecl(true).build()).setReturnExpr(MethodInvocationExpr.builder().setExprReferenceExpr(payloadVarExpr).setMethodName("getNextPageToken").setReturnType(returnType).build()).build());
    // Create extractResources method.
    returnType = TypeNode.withReference(ConcreteReference.builder().setClazz(Iterable.class).setGenerics(Arrays.asList(repeatedResponseType.reference())).build());
    Expr getResponsesExpr;
    Expr elseExpr;
    Expr thenExpr;
    if (repeatedResponseType.reference() != null && "java.util.Map.Entry".equals(repeatedResponseType.reference().fullName())) {
        getResponsesExpr = MethodInvocationExpr.builder().setExprReferenceExpr(payloadVarExpr).setMethodName(String.format("get%sMap", JavaStyle.toUpperCamelCase(repeatedFieldName))).setReturnType(returnType).build();
        thenExpr = MethodInvocationExpr.builder().setStaticReferenceType(TypeNode.withReference(ConcreteReference.withClazz(Collections.class))).setGenerics(Arrays.asList(repeatedResponseType.reference())).setMethodName("emptySet").setReturnType(returnType).build();
        elseExpr = MethodInvocationExpr.builder().setMethodName("entrySet").setExprReferenceExpr(MethodInvocationExpr.builder().setExprReferenceExpr(payloadVarExpr).setMethodName(String.format("get%sMap", JavaStyle.toUpperCamelCase(repeatedFieldName))).build()).setReturnType(returnType).build();
    } else {
        getResponsesExpr = MethodInvocationExpr.builder().setExprReferenceExpr(payloadVarExpr).setMethodName(String.format("get%sList", JavaStyle.toUpperCamelCase(repeatedFieldName))).setReturnType(returnType).build();
        thenExpr = MethodInvocationExpr.builder().setStaticReferenceType(TypeNode.withReference(ConcreteReference.withClazz(ImmutableList.class))).setGenerics(Arrays.asList(repeatedResponseType.reference())).setMethodName("of").setReturnType(returnType).build();
        elseExpr = getResponsesExpr;
    }
    // While protobufs should not be null, this null-check is needed to protect against NPEs
    // in paged iteration on clients that use legacy HTTP/JSON types, as these clients can
    // actually return null instead of an empty list.
    // Context:
    // Original issue: https://github.com/googleapis/google-cloud-java/issues/3736
    // Relevant discussion where this check was first added:
    // https://github.com/googleapis/google-cloud-java/pull/4499#discussion_r257057409
    Expr conditionExpr = RelationalOperationExpr.equalToWithExprs(getResponsesExpr, ValueExpr.createNullExpr());
    returnExpr = TernaryExpr.builder().setConditionExpr(conditionExpr).setThenExpr(thenExpr).setElseExpr(elseExpr).build();
    anonClassMethods.add(methodStarterBuilder.setReturnType(returnType).setName("extractResources").setArguments(payloadVarExpr.toBuilder().setIsDecl(true).build()).setReturnExpr(returnExpr).build());
    // Create the anonymous class.
    AnonymousClassExpr pagedListDescAnonClassExpr = AnonymousClassExpr.builder().setType(pagedListDescVarExpr.type()).setMethods(anonClassMethods).build();
    // Declare and assign the variable.
    return AssignmentExpr.builder().setVariableExpr(pagedListDescVarExpr.toBuilder().setIsDecl(true).setScope(ScopeNode.PRIVATE).setIsStatic(true).setIsFinal(true).build()).setValueExpr(pagedListDescAnonClassExpr).build();
}
Also used : Arrays(java.util.Arrays) BatchingDescriptor(com.google.api.gax.rpc.BatchingDescriptor) GapicServiceConfig(com.google.api.generator.gapic.model.GapicServiceConfig) ServerStreamingCallSettings(com.google.api.gax.rpc.ServerStreamingCallSettings) ReferenceConstructorExpr(com.google.api.generator.engine.ast.ReferenceConstructorExpr) StringObjectValue(com.google.api.generator.engine.ast.StringObjectValue) PagedCallSettings(com.google.api.gax.rpc.PagedCallSettings) Empty(com.google.protobuf.Empty) MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) TypeStore(com.google.api.generator.gapic.composer.store.TypeStore) Method(com.google.api.generator.gapic.model.Method) PrimitiveValue(com.google.api.generator.engine.ast.PrimitiveValue) Stream(com.google.api.generator.gapic.model.Method.Stream) Map(java.util.Map) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) PartitionKey(com.google.api.gax.batching.PartitionKey) ApiClientHeaderProvider(com.google.api.gax.rpc.ApiClientHeaderProvider) OperationSnapshot(com.google.api.gax.longrunning.OperationSnapshot) Set(java.util.Set) ScopeNode(com.google.api.generator.engine.ast.ScopeNode) EmptyLineStatement(com.google.api.generator.engine.ast.EmptyLineStatement) ApiFuture(com.google.api.core.ApiFuture) Duration(org.threeten.bp.Duration) LimitExceededBehavior(com.google.api.gax.batching.FlowController.LimitExceededBehavior) GoogleCredentialsProvider(com.google.api.gax.core.GoogleCredentialsProvider) BatchedRequestIssuer(com.google.api.gax.rpc.BatchedRequestIssuer) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) VaporReference(com.google.api.generator.engine.ast.VaporReference) IfStatement(com.google.api.generator.engine.ast.IfStatement) BetaApi(com.google.api.core.BetaApi) TypeNode(com.google.api.generator.engine.ast.TypeNode) ClientContext(com.google.api.gax.rpc.ClientContext) ReturnExpr(com.google.api.generator.engine.ast.ReturnExpr) ArrayList(java.util.ArrayList) RetrySettings(com.google.api.gax.retrying.RetrySettings) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) LinkedHashMap(java.util.LinkedHashMap) Expr(com.google.api.generator.engine.ast.Expr) Lists(com.google.common.collect.Lists) StubSettings(com.google.api.gax.rpc.StubSettings) BatchingCallSettings(com.google.api.gax.rpc.BatchingCallSettings) Nullable(javax.annotation.Nullable) CastExpr(com.google.api.generator.engine.ast.CastExpr) CommentStatement(com.google.api.generator.engine.ast.CommentStatement) IOException(java.io.IOException) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) InstantiatingExecutorProvider(com.google.api.gax.core.InstantiatingExecutorProvider) GapicBatchingSettings(com.google.api.generator.gapic.model.GapicBatchingSettings) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr) GapicClass(com.google.api.generator.gapic.model.GapicClass) SuperObjectValue(com.google.api.generator.engine.ast.SuperObjectValue) SampleComposerUtil(com.google.api.generator.gapic.composer.samplecode.SampleComposerUtil) Preconditions(com.google.common.base.Preconditions) StatusCode(com.google.api.gax.rpc.StatusCode) MonitoredResourceDescriptor(com.google.api.MonitoredResourceDescriptor) RequestBuilder(com.google.api.gax.batching.RequestBuilder) BiFunction(java.util.function.BiFunction) Reference(com.google.api.generator.engine.ast.Reference) ThisObjectValue(com.google.api.generator.engine.ast.ThisObjectValue) Variable(com.google.api.generator.engine.ast.Variable) Field(com.google.api.generator.gapic.model.Field) Generated(javax.annotation.Generated) StreamingCallSettings(com.google.api.gax.rpc.StreamingCallSettings) ImmutableSet(com.google.common.collect.ImmutableSet) SettingsSampleComposer(com.google.api.generator.gapic.composer.samplecode.SettingsSampleComposer) ImmutableMap(com.google.common.collect.ImmutableMap) PageContext(com.google.api.gax.rpc.PageContext) BatchingSettings(com.google.api.gax.batching.BatchingSettings) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) ClassNames(com.google.api.generator.gapic.composer.utils.ClassNames) List(java.util.List) OperationCallSettings(com.google.api.gax.rpc.OperationCallSettings) SampleCodeWriter(com.google.api.generator.gapic.composer.samplecode.SampleCodeWriter) TransportChannelProvider(com.google.api.gax.rpc.TransportChannelProvider) Optional(java.util.Optional) GapicContext(com.google.api.generator.gapic.model.GapicContext) ApiFunction(com.google.api.core.ApiFunction) SettingsCommentComposer(com.google.api.generator.gapic.composer.comment.SettingsCommentComposer) ApiCallContext(com.google.api.gax.rpc.ApiCallContext) FlowControlSettings(com.google.api.gax.batching.FlowControlSettings) ClassDefinition(com.google.api.generator.engine.ast.ClassDefinition) PackageChecker(com.google.api.generator.gapic.composer.utils.PackageChecker) Operation(com.google.longrunning.Operation) Function(java.util.function.Function) PagedListResponseFactory(com.google.api.gax.rpc.PagedListResponseFactory) HashSet(java.util.HashSet) GaxProperties(com.google.api.gax.core.GaxProperties) UnaryCallSettings(com.google.api.gax.rpc.UnaryCallSettings) TernaryExpr(com.google.api.generator.engine.ast.TernaryExpr) ImmutableList(com.google.common.collect.ImmutableList) RelationalOperationExpr(com.google.api.generator.engine.ast.RelationalOperationExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) Iterator(java.util.Iterator) AnnotationNode(com.google.api.generator.engine.ast.AnnotationNode) Sample(com.google.api.generator.gapic.model.Sample) UnaryCallable(com.google.api.gax.rpc.UnaryCallable) Statement(com.google.api.generator.engine.ast.Statement) Service(com.google.api.generator.gapic.model.Service) OperationTimedPollAlgorithm(com.google.api.gax.longrunning.OperationTimedPollAlgorithm) PagedListDescriptor(com.google.api.gax.rpc.PagedListDescriptor) JavaStyle(com.google.api.generator.gapic.utils.JavaStyle) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ThrowExpr(com.google.api.generator.engine.ast.ThrowExpr) Message(com.google.api.generator.gapic.model.Message) Collections(java.util.Collections) ReferenceConstructorExpr(com.google.api.generator.engine.ast.ReferenceConstructorExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) ReturnExpr(com.google.api.generator.engine.ast.ReturnExpr) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) Expr(com.google.api.generator.engine.ast.Expr) CastExpr(com.google.api.generator.engine.ast.CastExpr) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr) TernaryExpr(com.google.api.generator.engine.ast.TernaryExpr) RelationalOperationExpr(com.google.api.generator.engine.ast.RelationalOperationExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ThrowExpr(com.google.api.generator.engine.ast.ThrowExpr) MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) Collections(java.util.Collections) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr)

Example 7 with VariableExpr

use of com.google.api.generator.engine.ast.VariableExpr in project gapic-generator-java by googleapis.

the class AbstractServiceStubSettingsClassComposer method createNestedBuilderClass.

private ClassDefinition createNestedBuilderClass(Service service, @Nullable GapicServiceConfig serviceConfig, TypeStore typeStore) {
    // TODO(miraleung): Robustify this against a null serviceConfig.
    String thisClassName = ClassNames.getServiceStubSettingsClassName(service);
    String className = "Builder";
    TypeNode extendsType = TypeNode.withReference(ConcreteReference.builder().setClazz(StubSettings.Builder.class).setGenerics(Arrays.asList(typeStore.get(thisClassName), typeStore.get(className)).stream().map(t -> t.reference()).collect(Collectors.toList())).build());
    Set<String> nestedDeprecatedSettingVarNames = new HashSet<>();
    Map<String, VariableExpr> nestedMethodSettingsMemberVarExprs = createMethodSettingsClassMemberVarExprs(service, serviceConfig, typeStore, /* isNestedClass= */
    true, nestedDeprecatedSettingVarNames);
    // TODO(miraleung): Fill this out.
    return ClassDefinition.builder().setIsNested(true).setHeaderCommentStatements(SettingsCommentComposer.createBuilderClassComment(ClassNames.getServiceStubSettingsClassName(service))).setScope(ScopeNode.PUBLIC).setIsStatic(true).setName(className).setExtendsType(extendsType).setStatements(createNestedClassStatements(service, serviceConfig, nestedMethodSettingsMemberVarExprs)).setMethods(createNestedClassMethods(service, serviceConfig, extendsType, nestedMethodSettingsMemberVarExprs, nestedDeprecatedSettingVarNames, typeStore)).build();
}
Also used : RequestBuilder(com.google.api.gax.batching.RequestBuilder) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) HashSet(java.util.HashSet)

Example 8 with VariableExpr

use of com.google.api.generator.engine.ast.VariableExpr in project gapic-generator-java by googleapis.

the class AbstractTransportServiceStubClassComposer method createStaticCreatorMethods.

protected List<MethodDefinition> createStaticCreatorMethods(Service service, TypeStore typeStore, String newBuilderMethod) {
    TypeNode creatorMethodReturnType = typeStore.get(getTransportContext().classNames().getTransportServiceStubClassName(service));
    Function<List<VariableExpr>, MethodDefinition.Builder> creatorMethodStarterFn = argList -> MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setIsStatic(true).setIsFinal(true).setReturnType(creatorMethodReturnType).setName("create").setArguments(argList.stream().map(v -> v.toBuilder().setIsDecl(true).build()).collect(Collectors.toList())).setThrowsExceptions(Arrays.asList(TypeNode.withReference(ConcreteReference.withClazz(IOException.class))));
    Function<List<Expr>, Expr> instantiatorExprFn = argList -> NewObjectExpr.builder().setType(creatorMethodReturnType).setArguments(argList).build();
    TypeNode stubSettingsType = typeStore.get(getTransportContext().classNames().getServiceStubSettingsClassName(service));
    VariableExpr settingsVarExpr = VariableExpr.withVariable(Variable.builder().setName("settings").setType(stubSettingsType).build());
    TypeNode clientContextType = FIXED_TYPESTORE.get("ClientContext");
    VariableExpr clientContextVarExpr = VariableExpr.withVariable(Variable.builder().setName("clientContext").setType(clientContextType).build());
    VariableExpr callableFactoryVarExpr = VariableExpr.withVariable(Variable.builder().setName("callableFactory").setType(getTransportContext().stubCallableFactoryType()).build());
    MethodInvocationExpr clientContextCreateMethodExpr = MethodInvocationExpr.builder().setMethodName("create").setStaticReferenceType(clientContextType).setArguments(Arrays.asList(settingsVarExpr)).build();
    MethodInvocationExpr settingsBuilderMethodExpr = MethodInvocationExpr.builder().setMethodName(newBuilderMethod).setStaticReferenceType(stubSettingsType).build();
    settingsBuilderMethodExpr = MethodInvocationExpr.builder().setMethodName("build").setExprReferenceExpr(settingsBuilderMethodExpr).build();
    return Arrays.asList(creatorMethodStarterFn.apply(Arrays.asList(settingsVarExpr)).setReturnExpr(instantiatorExprFn.apply(Arrays.asList(settingsVarExpr, clientContextCreateMethodExpr))).build(), creatorMethodStarterFn.apply(Arrays.asList(clientContextVarExpr)).setReturnExpr(instantiatorExprFn.apply(Arrays.asList(settingsBuilderMethodExpr, clientContextVarExpr))).build(), creatorMethodStarterFn.apply(Arrays.asList(clientContextVarExpr, callableFactoryVarExpr)).setReturnExpr(instantiatorExprFn.apply(Arrays.asList(settingsBuilderMethodExpr, clientContextVarExpr, callableFactoryVarExpr))).build());
}
Also used : Arrays(java.util.Arrays) GapicServiceConfig(com.google.api.generator.gapic.model.GapicServiceConfig) BiFunction(java.util.function.BiFunction) ReferenceConstructorExpr(com.google.api.generator.engine.ast.ReferenceConstructorExpr) ThisObjectValue(com.google.api.generator.engine.ast.ThisObjectValue) Variable(com.google.api.generator.engine.ast.Variable) BackgroundResource(com.google.api.gax.core.BackgroundResource) Generated(javax.annotation.Generated) MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) TypeStore(com.google.api.generator.gapic.composer.store.TypeStore) Method(com.google.api.generator.gapic.model.Method) Map(java.util.Map) ServerStreamingCallable(com.google.api.gax.rpc.ServerStreamingCallable) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) LongRunningClient(com.google.api.gax.rpc.LongRunningClient) ImmutableMap(com.google.common.collect.ImmutableMap) OperationSnapshot(com.google.api.gax.longrunning.OperationSnapshot) ScopeNode(com.google.api.generator.engine.ast.ScopeNode) EmptyLineStatement(com.google.api.generator.engine.ast.EmptyLineStatement) Collectors(java.util.stream.Collectors) List(java.util.List) RequestParamsExtractor(com.google.api.gax.rpc.RequestParamsExtractor) Optional(java.util.Optional) GapicContext(com.google.api.generator.gapic.model.GapicContext) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) VaporReference(com.google.api.generator.engine.ast.VaporReference) BetaApi(com.google.api.core.BetaApi) TypeNode(com.google.api.generator.engine.ast.TypeNode) BidiStreamingCallable(com.google.api.gax.rpc.BidiStreamingCallable) Kind(com.google.api.generator.gapic.model.GapicClass.Kind) ClientContext(com.google.api.gax.rpc.ClientContext) ClassDefinition(com.google.api.generator.engine.ast.ClassDefinition) PackageChecker(com.google.api.generator.gapic.composer.utils.PackageChecker) Operation(com.google.longrunning.Operation) JavaDocComment(com.google.api.generator.engine.ast.JavaDocComment) Function(java.util.function.Function) ArrayList(java.util.ArrayList) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) LinkedHashMap(java.util.LinkedHashMap) Expr(com.google.api.generator.engine.ast.Expr) StubCommentComposer(com.google.api.generator.gapic.composer.comment.StubCommentComposer) ImmutableList(com.google.common.collect.ImmutableList) ClientStreamingCallable(com.google.api.gax.rpc.ClientStreamingCallable) TryCatchStatement(com.google.api.generator.engine.ast.TryCatchStatement) Nullable(javax.annotation.Nullable) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) BackgroundResourceAggregation(com.google.api.gax.core.BackgroundResourceAggregation) CommentStatement(com.google.api.generator.engine.ast.CommentStatement) IOException(java.io.IOException) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) AnnotationNode(com.google.api.generator.engine.ast.AnnotationNode) GapicClass(com.google.api.generator.gapic.model.GapicClass) TimeUnit(java.util.concurrent.TimeUnit) OperationCallable(com.google.api.gax.rpc.OperationCallable) UnaryCallable(com.google.api.gax.rpc.UnaryCallable) Statement(com.google.api.generator.engine.ast.Statement) Service(com.google.api.generator.gapic.model.Service) JavaStyle(com.google.api.generator.gapic.utils.JavaStyle) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ThrowExpr(com.google.api.generator.engine.ast.ThrowExpr) Preconditions(com.google.common.base.Preconditions) Message(com.google.api.generator.gapic.model.Message) Collections(java.util.Collections) ReferenceConstructorExpr(com.google.api.generator.engine.ast.ReferenceConstructorExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) Expr(com.google.api.generator.engine.ast.Expr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ThrowExpr(com.google.api.generator.engine.ast.ThrowExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) IOException(java.io.IOException)

Example 9 with VariableExpr

use of com.google.api.generator.engine.ast.VariableExpr in project gapic-generator-java by googleapis.

the class AbstractTransportServiceStubClassComposer method createCallableClassMembers.

private Map<String, VariableExpr> createCallableClassMembers(Service service, TypeStore typeStore) {
    Map<String, VariableExpr> callableClassMembers = new LinkedHashMap<>();
    // Using a for-loop because the output cardinality is not a 1:1 mapping to the input set.
    for (Method protoMethod : service.methods()) {
        String javaStyleProtoMethodName = JavaStyle.toLowerCamelCase(protoMethod.name());
        String callableName = String.format(CALLABLE_CLASS_MEMBER_PATTERN, javaStyleProtoMethodName);
        callableClassMembers.put(callableName, VariableExpr.withVariable(Variable.builder().setName(callableName).setType(getCallableType(protoMethod)).build()));
        if (protoMethod.hasLro()) {
            callableName = String.format(OPERATION_CALLABLE_CLASS_MEMBER_PATTERN, javaStyleProtoMethodName);
            callableClassMembers.put(callableName, VariableExpr.withVariable(Variable.builder().setName(callableName).setType(TypeNode.withReference(ConcreteReference.builder().setClazz(OperationCallable.class).setGenerics(Arrays.asList(protoMethod.inputType().reference(), protoMethod.lro().responseType().reference(), protoMethod.lro().metadataType().reference())).build())).build()));
        }
        if (protoMethod.isPaged()) {
            callableName = String.format(PAGED_CALLABLE_CLASS_MEMBER_PATTERN, javaStyleProtoMethodName);
            callableClassMembers.put(callableName, VariableExpr.withVariable(Variable.builder().setName(callableName).setType(TypeNode.withReference(getCallableType(protoMethod).reference().copyAndSetGenerics(Arrays.asList(protoMethod.inputType().reference(), typeStore.get(String.format(PAGED_RESPONSE_TYPE_NAME_PATTERN, protoMethod.name())).reference())))).build()));
        }
    }
    return callableClassMembers;
}
Also used : OperationCallable(com.google.api.gax.rpc.OperationCallable) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) Method(com.google.api.generator.gapic.model.Method) LinkedHashMap(java.util.LinkedHashMap)

Example 10 with VariableExpr

use of com.google.api.generator.engine.ast.VariableExpr in project gapic-generator-java by googleapis.

the class AbstractTransportServiceStubClassComposer method generate.

@Override
public GapicClass generate(GapicContext context, Service service) {
    String pakkage = service.pakkage() + ".stub";
    TypeStore typeStore = createDynamicTypes(service, pakkage);
    String className = getTransportContext().classNames().getTransportServiceStubClassName(service);
    GapicClass.Kind kind = Kind.STUB;
    Map<String, VariableExpr> protoMethodNameToDescriptorVarExprs = createProtoMethodNameToDescriptorClassMembers(service, getTransportContext().methodDescriptorClass());
    Map<String, VariableExpr> callableClassMemberVarExprs = createCallableClassMembers(service, typeStore);
    Map<String, VariableExpr> classMemberVarExprs = new LinkedHashMap<>();
    classMemberVarExprs.put(BACKGROUND_RESOURCES_MEMBER_NAME, VariableExpr.withVariable(Variable.builder().setName(BACKGROUND_RESOURCES_MEMBER_NAME).setType(FIXED_TYPESTORE.get("BackgroundResource")).build()));
    if (generateOperationsStubLogic(service)) {
        // Transport-specific service stub may have only one element of the following, thus get(0).
        TypeNode operationsStubType = getTransportOperationsStubType(service);
        classMemberVarExprs.put(getTransportContext().transportOperationsStubNames().get(0), VariableExpr.withVariable(Variable.builder().setName(getTransportContext().transportOperationsStubNames().get(0)).setType(operationsStubType).build()));
    }
    boolean operationPollingMethod = checkOperationPollingMethod(service);
    if (operationPollingMethod) {
        VariableExpr longRunningVarExpr = declareLongRunningClient();
        if (longRunningVarExpr != null) {
            classMemberVarExprs.put("longRunningClient", longRunningVarExpr);
        }
    }
    classMemberVarExprs.put(CALLABLE_FACTORY_MEMBER_NAME, VariableExpr.withVariable(Variable.builder().setName(CALLABLE_FACTORY_MEMBER_NAME).setType(getTransportContext().stubCallableFactoryType()).build()));
    Map<String, Message> messageTypes = context.messages();
    List<Statement> classStatements = createClassStatements(service, protoMethodNameToDescriptorVarExprs, callableClassMemberVarExprs, classMemberVarExprs, messageTypes);
    StubCommentComposer commentComposer = new StubCommentComposer(getTransportContext().transportName());
    ClassDefinition classDef = ClassDefinition.builder().setPackageString(pakkage).setHeaderCommentStatements(commentComposer.createTransportServiceStubClassHeaderComments(service.name(), service.isDeprecated())).setAnnotations(createClassAnnotations(service)).setScope(ScopeNode.PUBLIC).setName(className).setExtendsType(typeStore.get(getTransportContext().classNames().getServiceStubClassName(service))).setMethods(createClassMethods(context, service, typeStore, classMemberVarExprs, callableClassMemberVarExprs, protoMethodNameToDescriptorVarExprs, classStatements)).setStatements(classStatements).build();
    return GapicClass.create(kind, classDef);
}
Also used : Message(com.google.api.generator.gapic.model.Message) EmptyLineStatement(com.google.api.generator.engine.ast.EmptyLineStatement) TryCatchStatement(com.google.api.generator.engine.ast.TryCatchStatement) CommentStatement(com.google.api.generator.engine.ast.CommentStatement) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) Statement(com.google.api.generator.engine.ast.Statement) ClassDefinition(com.google.api.generator.engine.ast.ClassDefinition) LinkedHashMap(java.util.LinkedHashMap) StubCommentComposer(com.google.api.generator.gapic.composer.comment.StubCommentComposer) GapicClass(com.google.api.generator.gapic.model.GapicClass) TypeStore(com.google.api.generator.gapic.composer.store.TypeStore) Kind(com.google.api.generator.gapic.model.GapicClass.Kind) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode)

Aggregations

VariableExpr (com.google.api.generator.engine.ast.VariableExpr)217 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)137 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)132 TypeNode (com.google.api.generator.engine.ast.TypeNode)118 Expr (com.google.api.generator.engine.ast.Expr)114 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)107 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)91 Variable (com.google.api.generator.engine.ast.Variable)88 NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)87 ArrayList (java.util.ArrayList)87 Test (org.junit.Test)82 Statement (com.google.api.generator.engine.ast.Statement)80 List (java.util.List)64 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)62 MethodDefinition (com.google.api.generator.engine.ast.MethodDefinition)58 RelationalOperationExpr (com.google.api.generator.engine.ast.RelationalOperationExpr)56 CastExpr (com.google.api.generator.engine.ast.CastExpr)53 CommentStatement (com.google.api.generator.engine.ast.CommentStatement)53 Arrays (java.util.Arrays)48 IfStatement (com.google.api.generator.engine.ast.IfStatement)47