Search in sources :

Example 1 with NewObjectExpr

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

the class AbstractServiceStubSettingsClassComposer method createBuilderHelperMethods.

protected List<MethodDefinition> createBuilderHelperMethods(Service service, TypeStore typeStore) {
    List<MethodDefinition> javaMethods = new ArrayList<>();
    final TypeNode builderReturnType = typeStore.get(NESTED_BUILDER_CLASS_NAME);
    // Create the newBuilder(ClientContext) method.
    Function<Expr, NewObjectExpr> newBuilderFn = argExpr -> NewObjectExpr.builder().setType(builderReturnType).setArguments(argExpr).build();
    VariableExpr clientContextVarExpr = VariableExpr.withVariable(Variable.builder().setType(FIXED_TYPESTORE.get("ClientContext")).setName("clientContext").build());
    javaMethods.add(MethodDefinition.builder().setHeaderCommentStatements(SettingsCommentComposer.NEW_BUILDER_METHOD_COMMENT).setScope(ScopeNode.PUBLIC).setIsStatic(true).setReturnType(builderReturnType).setName("newBuilder").setArguments(clientContextVarExpr.toBuilder().setIsDecl(true).build()).setReturnExpr(newBuilderFn.apply(clientContextVarExpr)).build());
    // Create the toBuilder method.
    javaMethods.add(MethodDefinition.builder().setHeaderCommentStatements(SettingsCommentComposer.TO_BUILDER_METHOD_COMMENT).setScope(ScopeNode.PUBLIC).setReturnType(builderReturnType).setName("toBuilder").setReturnExpr(newBuilderFn.apply(ValueExpr.withValue(ThisObjectValue.withType(typeStore.get(ClassNames.getServiceStubClassName(service)))))).build());
    return javaMethods;
}
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) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) ArrayList(java.util.ArrayList) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode)

Example 2 with NewObjectExpr

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

the class HttpJsonServiceCallableFactoryClassComposer method createOperationCallableMethod.

@Override
protected MethodDefinition createOperationCallableMethod(Service service, TypeStore typeStore) {
    String methodVariantName = "Operation";
    String requestTemplateName = "RequestT";
    String responseTemplateName = "ResponseT";
    List<String> methodTemplateNames = Arrays.asList(requestTemplateName, responseTemplateName, "MetadataT");
    // Always add @BetaApi annotation to the generated createOperationCallable() method for now,
    // until LRO is fully implemented.
    // 
    // Remove the @BetaApi annotation once the LRO feature is fully implemented and stabilized.
    AnnotationNode betaAnnotation = AnnotationNode.withTypeAndDescription(typeStore.get("BetaApi"), "The surface for long-running operations is not stable yet and may change in the" + " future.");
    // Generate generic method without the body
    TypeNode operationType = service.operationType();
    if (operationType == null) {
        operationType = DEFAULT_OPERATION_TYPE;
    }
    MethodDefinition method = createGenericCallableMethod(service, typeStore, /*methodTemplateNames=*/
    methodTemplateNames, /*returnCallableKindName=*/
    methodVariantName, /*returnCallableTemplateNames=*/
    methodTemplateNames, /*methodVariantName=*/
    methodVariantName, /*httpJsonCallSettingsTemplateObjects=*/
    Arrays.asList(requestTemplateName, operationType), /*callSettingsVariantName=*/
    methodVariantName, /*callSettingsTemplateObjects=*/
    methodTemplateNames.stream().map(n -> (Object) n).collect(Collectors.toList()), Arrays.asList(betaAnnotation));
    List<Statement> createOperationCallableBody = new ArrayList<>();
    List<VariableExpr> arguments = new ArrayList<>(method.arguments());
    Variable httpJsonCallSettingsVar = arguments.get(0).variable();
    Variable operationCallSettingsVar = arguments.get(1).variable();
    Variable clientContextVar = arguments.get(2).variable();
    Variable operationsStubVar = arguments.get(3).variable();
    // Generate innerCallable
    VariableExpr innerCallableVarExpr = VariableExpr.builder().setVariable(Variable.builder().setName("innerCallable").setType(TypeNode.withReference(ConcreteReference.withClazz(UnaryCallable.class))).build()).setTemplateObjects(Arrays.asList(requestTemplateName, methodVariantName)).build();
    MethodInvocationExpr getInitialCallSettingsExpr = MethodInvocationExpr.builder().setExprReferenceExpr(VariableExpr.withVariable(operationCallSettingsVar)).setMethodName("getInitialCallSettings").build();
    MethodInvocationExpr createBaseUnaryCallableExpr = MethodInvocationExpr.builder().setStaticReferenceType(TypeNode.withReference(ConcreteReference.withClazz(HttpJsonCallableFactory.class))).setMethodName("createBaseUnaryCallable").setArguments(VariableExpr.withVariable(httpJsonCallSettingsVar), getInitialCallSettingsExpr, VariableExpr.withVariable(clientContextVar)).setReturnType(TypeNode.withReference(ConcreteReference.withClazz(UnaryCallable.class))).build();
    AssignmentExpr innerCallableAssignExpr = AssignmentExpr.builder().setVariableExpr(innerCallableVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(createBaseUnaryCallableExpr).build();
    createOperationCallableBody.add(ExprStatement.withExpr(innerCallableAssignExpr));
    // This is a temporary solution
    VaporReference requestT = VaporReference.builder().setName("RequestT").setPakkage(service.pakkage() + ".stub").build();
    TypeNode initialCallableType = TypeNode.withReference(ConcreteReference.builder().setClazz(HttpJsonOperationSnapshotCallable.class).setGenerics(requestT, operationType.reference()).build());
    // Generate initialCallable
    VariableExpr initialCallableVarExpr = VariableExpr.builder().setVariable(Variable.builder().setName("initialCallable").setType(initialCallableType).build()).build();
    MethodInvocationExpr getMethodDescriptorExpr = MethodInvocationExpr.builder().setExprReferenceExpr(VariableExpr.withVariable(httpJsonCallSettingsVar)).setMethodName("getMethodDescriptor").build();
    MethodInvocationExpr getOperationSnapshotFactoryExpr = MethodInvocationExpr.builder().setExprReferenceExpr(getMethodDescriptorExpr).setMethodName("getOperationSnapshotFactory").build();
    TypeNode operationSnapshotCallableType = TypeNode.withReference(ConcreteReference.builder().setClazz(HttpJsonOperationSnapshotCallable.class).setGenerics(requestT, operationType.reference()).build());
    NewObjectExpr initialCallableObject = NewObjectExpr.builder().setType(operationSnapshotCallableType).setIsGeneric(true).setArguments(innerCallableVarExpr, getOperationSnapshotFactoryExpr).build();
    AssignmentExpr initialCallableAssignExpr = AssignmentExpr.builder().setVariableExpr(initialCallableVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(initialCallableObject).build();
    createOperationCallableBody.add(ExprStatement.withExpr(initialCallableAssignExpr));
    // Generate return statement
    MethodInvocationExpr longRunningClient = MethodInvocationExpr.builder().setExprReferenceExpr(VariableExpr.withVariable(operationsStubVar)).setMethodName("longRunningClient").build();
    MethodInvocationExpr createOperationCallable = MethodInvocationExpr.builder().setStaticReferenceType(TypeNode.withReference(ConcreteReference.withClazz(HttpJsonCallableFactory.class))).setMethodName("createOperationCallable").setArguments(VariableExpr.withVariable(operationCallSettingsVar), VariableExpr.withVariable(clientContextVar), longRunningClient, initialCallableVarExpr).setReturnType(TypeNode.withReference(ConcreteReference.withClazz(OperationCallable.class))).build();
    // Add body and return statement to method
    return method.toBuilder().setBody(createOperationCallableBody).setReturnExpr(createOperationCallable).build();
}
Also used : Variable(com.google.api.generator.engine.ast.Variable) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) Statement(com.google.api.generator.engine.ast.Statement) UnaryCallable(com.google.api.gax.rpc.UnaryCallable) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) OperationCallable(com.google.api.gax.rpc.OperationCallable) ArrayList(java.util.ArrayList) VaporReference(com.google.api.generator.engine.ast.VaporReference) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) AnnotationNode(com.google.api.generator.engine.ast.AnnotationNode) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) HttpJsonOperationSnapshotCallable(com.google.api.gax.httpjson.HttpJsonOperationSnapshotCallable) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode)

Example 3 with NewObjectExpr

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

the class JavaWriterVisitorTest method writeNewObjectExpr_basic.

@Test
public void writeNewObjectExpr_basic() {
    // isGeneric() is true, but generics() is empty.
    ConcreteReference ref = ConcreteReference.withClazz(List.class);
    TypeNode type = TypeNode.withReference(ref);
    NewObjectExpr newObjectExpr = NewObjectExpr.builder().setIsGeneric(true).setType(type).build();
    newObjectExpr.accept(writerVisitor);
    assertEquals("new List<>()", writerVisitor.write());
}
Also used : NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) Test(org.junit.Test)

Example 4 with NewObjectExpr

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

the class ImportWriterVisitorTest method writeNewObjectExprImports_withArgs.

@Test
public void writeNewObjectExprImports_withArgs() {
    // [Constructing] `new FileOutputStream(File file)` and the argument needs to be imported.
    ConcreteReference fileOutputStreamRef = ConcreteReference.withClazz(FileOutputStream.class);
    ConcreteReference fileRef = ConcreteReference.withClazz(File.class);
    Variable fileVar = Variable.builder().setName("file").setType(TypeNode.withReference(fileRef)).build();
    VariableExpr fileExpr = VariableExpr.builder().setVariable(fileVar).build();
    NewObjectExpr newObjectExpr = NewObjectExpr.builder().setType(TypeNode.withReference(fileOutputStreamRef)).setArguments(Arrays.asList(fileExpr)).build();
    newObjectExpr.accept(writerVisitor);
    assertEquals(LineFormatter.lines("import java.io.File;\n", "import java.io.FileOutputStream;\n\n"), writerVisitor.write());
}
Also used : Variable(com.google.api.generator.engine.ast.Variable) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) Test(org.junit.Test)

Example 5 with NewObjectExpr

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

the class HttpJsonServiceStubClassComposer method createLongRunningClient.

@Override
protected List<Statement> createLongRunningClient(Service service, TypeStore typeStore) {
    Method pollingMethod = service.operationPollingMethod();
    if (pollingMethod != null) {
        Expr thisExpr = ValueExpr.withValue(ThisObjectValue.withType(typeStore.get(getTransportContext().classNames().getTransportServiceStubClassName(service))));
        VariableExpr callable = VariableExpr.withVariable(Variable.builder().setName(pollingMethod.name().toLowerCase() + "Callable").setType(TypeNode.withReference(ConcreteReference.withClazz(UnaryCallable.class))).build());
        VariableExpr methodDescriptor = VariableExpr.withVariable(Variable.builder().setName(pollingMethod.name().toLowerCase() + "MethodDescriptor").setType(TypeNode.withReference(ConcreteReference.withClazz(ApiMethodDescriptor.class))).build());
        TypeNode httpJsonLongRunningClientType = TypeNode.withReference(ConcreteReference.builder().setClazz(HttpJsonLongRunningClient.class).setGenerics(Arrays.asList(pollingMethod.inputType().reference(), pollingMethod.outputType().reference())).build());
        NewObjectExpr HttpJsonLongRunningClient = NewObjectExpr.builder().setType(httpJsonLongRunningClientType).setArguments(Arrays.asList(callable, MethodInvocationExpr.builder().setExprReferenceExpr(methodDescriptor).setMethodName("getOperationSnapshotFactory").build(), MethodInvocationExpr.builder().setExprReferenceExpr(methodDescriptor).setMethodName("getPollingRequestFactory").build())).build();
        AssignmentExpr assignLongRunningClient = AssignmentExpr.builder().setVariableExpr(VariableExpr.builder().setExprReferenceExpr(thisExpr).setVariable(Variable.builder().setName("longRunningClient").setType(TypeNode.withReference(ConcreteReference.withClazz(LongRunningClient.class))).build()).build()).setValueExpr(HttpJsonLongRunningClient).build();
        return Arrays.asList(ExprStatement.withExpr(assignLongRunningClient));
    } else {
        return Collections.emptyList();
    }
}
Also used : ValueExpr(com.google.api.generator.engine.ast.ValueExpr) EnumRefExpr(com.google.api.generator.engine.ast.EnumRefExpr) 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) LambdaExpr(com.google.api.generator.engine.ast.LambdaExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) HttpJsonLongRunningClient(com.google.api.gax.httpjson.HttpJsonLongRunningClient) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) Method(com.google.api.generator.gapic.model.Method) TypeNode(com.google.api.generator.engine.ast.TypeNode) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr)

Aggregations

NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)12 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)9 TypeNode (com.google.api.generator.engine.ast.TypeNode)8 Variable (com.google.api.generator.engine.ast.Variable)8 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)7 Test (org.junit.Test)7 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)5 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)5 VaporReference (com.google.api.generator.engine.ast.VaporReference)3 ArrayList (java.util.ArrayList)3 UnaryCallable (com.google.api.gax.rpc.UnaryCallable)2 AnnotationNode (com.google.api.generator.engine.ast.AnnotationNode)2 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)2 MethodDefinition (com.google.api.generator.engine.ast.MethodDefinition)2 Statement (com.google.api.generator.engine.ast.Statement)2 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)2 IOException (java.io.IOException)2 MonitoredResourceDescriptor (com.google.api.MonitoredResourceDescriptor)1 ApiFunction (com.google.api.core.ApiFunction)1 ApiFuture (com.google.api.core.ApiFuture)1