Search in sources :

Example 1 with AnonymousClassExpr

use of com.google.api.generator.engine.ast.AnonymousClassExpr 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 2 with AnonymousClassExpr

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

the class BatchingDescriptorComposer method createGetRequestBuilderMethod.

private static MethodDefinition createGetRequestBuilderMethod(Method method, GapicBatchingSettings batchingSettings) {
    TypeNode builderType = TypeNode.withReference(VaporReference.builder().setEnclosingClassNames(method.inputType().reference().name()).setName("Builder").setPakkage(method.inputType().reference().pakkage()).build());
    VariableExpr builderVarExpr = VariableExpr.withVariable(Variable.builder().setType(builderType).setName("builder").build());
    VariableExpr requestVarExpr = VariableExpr.withVariable(Variable.builder().setType(method.inputType()).setName("request").build());
    Expr toBuilderExpr = AssignmentExpr.builder().setVariableExpr(builderVarExpr).setValueExpr(MethodInvocationExpr.builder().setExprReferenceExpr(requestVarExpr).setMethodName("toBuilder").setReturnType(builderType).build()).build();
    String upperBatchedFieldName = JavaStyle.toUpperCamelCase(batchingSettings.batchedFieldName());
    String getFooListMethodName = String.format(GET_LIST_METHOD_PATTERN, upperBatchedFieldName);
    Expr getFooListExpr = MethodInvocationExpr.builder().setExprReferenceExpr(requestVarExpr).setMethodName(getFooListMethodName).build();
    String addAllMethodName = String.format(ADD_ALL_METHOD_PATTERN, upperBatchedFieldName);
    Expr addAllExpr = MethodInvocationExpr.builder().setExprReferenceExpr(builderVarExpr).setMethodName(addAllMethodName).setArguments(getFooListExpr).build();
    MethodDefinition appendRequestMethod = MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.VOID).setName("appendRequest").setArguments(requestVarExpr.toBuilder().setIsDecl(true).build()).setBody(Arrays.asList(IfStatement.builder().setConditionExpr(RelationalOperationExpr.equalToWithExprs(builderVarExpr, ValueExpr.createNullExpr())).setBody(Arrays.asList(ExprStatement.withExpr(toBuilderExpr))).setElseBody(Arrays.asList(ExprStatement.withExpr(addAllExpr))).build())).build();
    MethodDefinition buildMethod = MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setReturnType(method.inputType()).setName("build").setReturnExpr(MethodInvocationExpr.builder().setExprReferenceExpr(builderVarExpr).setMethodName("build").setReturnType(method.inputType()).build()).build();
    TypeNode anonClassType = toType(REQUEST_BUILDER_REF, method.inputType());
    AnonymousClassExpr requestBuilderAnonClassExpr = AnonymousClassExpr.builder().setType(anonClassType).setStatements(Arrays.asList(ExprStatement.withExpr(builderVarExpr.toBuilder().setIsDecl(true).setScope(ScopeNode.PRIVATE).build()))).setMethods(Arrays.asList(appendRequestMethod, buildMethod)).build();
    return MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setReturnType(anonClassType).setName("getRequestBuilder").setReturnExpr(requestBuilderAnonClassExpr).build();
}
Also used : NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) Expr(com.google.api.generator.engine.ast.Expr) RelationalOperationExpr(com.google.api.generator.engine.ast.RelationalOperationExpr) UnaryOperationExpr(com.google.api.generator.engine.ast.UnaryOperationExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr)

Example 3 with AnonymousClassExpr

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

the class ImportWriterVisitorTest method writeAnonymousClassExprImports.

@Test
public void writeAnonymousClassExprImports() {
    // [Constructing] Function<List<IOException>, MethodDefinition>
    ConcreteReference exceptionListRef = ConcreteReference.builder().setClazz(List.class).setGenerics(Arrays.asList(ConcreteReference.withClazz(IOException.class))).build();
    ConcreteReference methodDefinitionRef = ConcreteReference.withClazz(MethodDefinition.class);
    ConcreteReference ref = ConcreteReference.builder().setClazz(Function.class).setGenerics(Arrays.asList(exceptionListRef, methodDefinitionRef)).build();
    TypeNode type = TypeNode.withReference(ref);
    // [Constructing] HashMap<String, Integer> map;
    ConcreteReference mapRef = ConcreteReference.builder().setClazz(HashMap.class).setGenerics(Arrays.asList(ConcreteReference.withClazz(String.class), ConcreteReference.withClazz(Integer.class))).build();
    VariableExpr mapExpr = VariableExpr.builder().setVariable(Variable.builder().setName("map").setType(TypeNode.withReference(mapRef)).build()).setIsDecl(true).build();
    ExprStatement exprStatement = ExprStatement.withExpr(mapExpr);
    // [Constructing] an input argument whose type is `List<IOException>`
    VariableExpr arg = VariableExpr.builder().setVariable(Variable.builder().setName("arg").setType(TypeNode.withReference(exceptionListRef)).build()).setIsDecl(true).build();
    // [Constructing] a return variable expression whose type is `MethodDefinition`
    VariableExpr returnArg = VariableExpr.builder().setVariable(Variable.builder().setName("returnArg").setType(TypeNode.withReference(methodDefinitionRef)).build()).build();
    MethodDefinition method = MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.withReference(methodDefinitionRef)).setArguments(Arrays.asList(arg)).setReturnExpr(returnArg).setName("apply").build();
    AnonymousClassExpr anonymousClassExpr = AnonymousClassExpr.builder().setType(type).setMethods(Arrays.asList(method)).setStatements(Arrays.asList(exprStatement)).build();
    anonymousClassExpr.accept(writerVisitor);
    assertEquals(LineFormatter.lines("import com.google.api.generator.engine.ast.MethodDefinition;\n", "import com.google.common.base.Function;\n", "import java.io.IOException;\n", "import java.util.HashMap;\n", "import java.util.List;\n\n"), writerVisitor.write());
}
Also used : MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) IOException(java.io.IOException) TypeNode(com.google.api.generator.engine.ast.TypeNode) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr) Test(org.junit.Test)

Example 4 with AnonymousClassExpr

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

the class JavaWriterVisitorTest method writeAnonymousClassExpr_basic.

@Test
public void writeAnonymousClassExpr_basic() {
    ConcreteReference ref = ConcreteReference.withClazz(Runnable.class);
    TypeNode type = TypeNode.withReference(ref);
    AssignmentExpr assignmentExpr = createAssignmentExpr("foobar", "false", TypeNode.BOOLEAN);
    ExprStatement statement = ExprStatement.withExpr(assignmentExpr);
    MethodDefinition method = MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.VOID).setName("run").setIsOverride(true).setBody(Arrays.asList(statement)).build();
    AnonymousClassExpr anonymousClassExpr = AnonymousClassExpr.builder().setType(type).setMethods(Arrays.asList(method)).build();
    anonymousClassExpr.accept(writerVisitor);
    assertEquals(LineFormatter.lines("new Runnable() {\n", "@Override\n", "public void run() {\n", "boolean foobar = false;\n}\n\n}"), writerVisitor.write());
}
Also used : MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) TypeNode(com.google.api.generator.engine.ast.TypeNode) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr) Test(org.junit.Test)

Example 5 with AnonymousClassExpr

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

the class AbstractServiceStubSettingsClassComposer method createPagedListResponseFactoryAssignExpr.

private static Expr createPagedListResponseFactoryAssignExpr(VariableExpr pageStrDescVarExpr, Method method, TypeNode repeatedResponseType, TypeStore typeStore) {
    Preconditions.checkState(method.isPaged(), String.format("Method %s is not paged", method.name()));
    // Create the PagedListResponseFactory.
    TypeNode pagedResponseType = typeStore.get(getPagedResponseTypeName(method.name()));
    TypeNode apiFutureType = TypeNode.withReference(ConcreteReference.builder().setClazz(ApiFuture.class).setGenerics(Arrays.asList(pagedResponseType.reference())).build());
    VariableExpr callableVarExpr = VariableExpr.withVariable(Variable.builder().setType(TypeNode.withReference(ConcreteReference.builder().setClazz(UnaryCallable.class).setGenerics(Arrays.asList(method.inputType().reference(), method.outputType().reference())).build())).setName("callable").build());
    VariableExpr requestVarExpr = VariableExpr.withVariable(Variable.builder().setType(method.inputType()).setName("request").build());
    VariableExpr contextVarExpr = VariableExpr.withVariable(Variable.builder().setType(FIXED_TYPESTORE.get("ApiCallContext")).setName("context").build());
    VariableExpr futureResponseVarExpr = VariableExpr.withVariable(Variable.builder().setType(TypeNode.withReference(ConcreteReference.builder().setClazz(ApiFuture.class).setGenerics(Arrays.asList(method.outputType().reference())).build())).setName("futureResponse").build());
    TypeNode pageContextType = TypeNode.withReference(ConcreteReference.builder().setClazz(PageContext.class).setGenerics(Arrays.asList(method.inputType(), method.outputType(), repeatedResponseType).stream().map(t -> t.reference()).collect(Collectors.toList())).build());
    VariableExpr pageContextVarExpr = VariableExpr.withVariable(Variable.builder().setType(pageContextType).setName("pageContext").build());
    AssignmentExpr pageContextAssignExpr = AssignmentExpr.builder().setVariableExpr(pageContextVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("PageContext")).setMethodName("create").setArguments(callableVarExpr, pageStrDescVarExpr, requestVarExpr, contextVarExpr).setReturnType(pageContextVarExpr.type()).build()).build();
    Expr returnExpr = MethodInvocationExpr.builder().setStaticReferenceType(typeStore.get(getPagedResponseTypeName(method.name()))).setMethodName("createAsync").setArguments(pageContextVarExpr, futureResponseVarExpr).setReturnType(apiFutureType).build();
    MethodDefinition getFuturePagedResponseMethod = MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setReturnType(apiFutureType).setName("getFuturePagedResponse").setArguments(Arrays.asList(callableVarExpr, requestVarExpr, contextVarExpr, futureResponseVarExpr).stream().map(v -> v.toBuilder().setIsDecl(true).build()).collect(Collectors.toList())).setBody(Arrays.asList(ExprStatement.withExpr(pageContextAssignExpr))).setReturnExpr(returnExpr).build();
    // Create the variable.
    TypeNode pagedResponseFactoryType = TypeNode.withReference(ConcreteReference.builder().setClazz(PagedListResponseFactory.class).setGenerics(Arrays.asList(method.inputType(), method.outputType(), typeStore.get(getPagedResponseTypeName(method.name()))).stream().map(t -> t.reference()).collect(Collectors.toList())).build());
    String varName = String.format(PAGED_RESPONSE_FACTORY_PATTERN, JavaStyle.toUpperSnakeCase(method.name()));
    VariableExpr pagedListResponseFactoryVarExpr = VariableExpr.withVariable(Variable.builder().setType(pagedResponseFactoryType).setName(varName).build());
    AnonymousClassExpr factoryAnonClassExpr = AnonymousClassExpr.builder().setType(pagedResponseFactoryType).setMethods(Arrays.asList(getFuturePagedResponseMethod)).build();
    return AssignmentExpr.builder().setVariableExpr(pagedListResponseFactoryVarExpr.toBuilder().setIsDecl(true).setScope(ScopeNode.PRIVATE).setIsStatic(true).setIsFinal(true).build()).setValueExpr(factoryAnonClassExpr).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) ApiFuture(com.google.api.core.ApiFuture) PagedListResponseFactory(com.google.api.gax.rpc.PagedListResponseFactory) 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) UnaryCallable(com.google.api.gax.rpc.UnaryCallable) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) PageContext(com.google.api.gax.rpc.PageContext) TypeNode(com.google.api.generator.engine.ast.TypeNode) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr)

Aggregations

AnonymousClassExpr (com.google.api.generator.engine.ast.AnonymousClassExpr)11 MethodDefinition (com.google.api.generator.engine.ast.MethodDefinition)10 TypeNode (com.google.api.generator.engine.ast.TypeNode)10 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)8 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)8 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)6 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)6 Expr (com.google.api.generator.engine.ast.Expr)5 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)5 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)5 NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)4 IOException (java.io.IOException)4 Test (org.junit.Test)4 Variable (com.google.api.generator.engine.ast.Variable)3 ArrayList (java.util.ArrayList)3 MonitoredResourceDescriptor (com.google.api.MonitoredResourceDescriptor)2 ApiFunction (com.google.api.core.ApiFunction)2 ApiFuture (com.google.api.core.ApiFuture)2 BetaApi (com.google.api.core.BetaApi)2 BatchingSettings (com.google.api.gax.batching.BatchingSettings)2