Search in sources :

Example 56 with MethodInvocationExpr

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

the class JavaWriterVisitorTest method writeRelationalOperationExpr_equalTo.

@Test
public void writeRelationalOperationExpr_equalTo() {
    VariableExpr variableExprLHS = VariableExpr.withVariable(Variable.builder().setType(TypeNode.BOOLEAN_OBJECT).setName("isGood").build());
    MethodInvocationExpr methodInvocationExpr = MethodInvocationExpr.builder().setMethodName("isBad").setReturnType(TypeNode.BOOLEAN).build();
    RelationalOperationExpr equalToOperationExpr = RelationalOperationExpr.equalToWithExprs(variableExprLHS, methodInvocationExpr);
    equalToOperationExpr.accept(writerVisitor);
    assertThat(writerVisitor.write()).isEqualTo("isGood == isBad()");
}
Also used : RelationalOperationExpr(com.google.api.generator.engine.ast.RelationalOperationExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) Test(org.junit.Test)

Example 57 with MethodInvocationExpr

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

the class AbstractServiceClientClassComposer method createStaticCreatorMethods.

private static List<MethodDefinition> createStaticCreatorMethods(Service service, TypeStore typeStore) {
    List<MethodDefinition> methods = new ArrayList<>();
    String thisClientName = ClassNames.getServiceClientClassName(service);
    String settingsName = ClassNames.getServiceSettingsClassName(service);
    TypeNode thisClassType = typeStore.get(thisClientName);
    TypeNode exceptionType = typeStore.get("IOException");
    TypeNode settingsType = typeStore.get(settingsName);
    Preconditions.checkNotNull(settingsType, String.format("Type %s not found", settingsName));
    MethodInvocationExpr newBuilderExpr = MethodInvocationExpr.builder().setMethodName("newBuilder").setStaticReferenceType(settingsType).build();
    MethodInvocationExpr buildExpr = MethodInvocationExpr.builder().setMethodName("build").setExprReferenceExpr(newBuilderExpr).build();
    MethodInvocationExpr createMethodInvocationExpr = MethodInvocationExpr.builder().setMethodName("create").setArguments(Arrays.asList(buildExpr)).setReturnType(typeStore.get(thisClientName)).build();
    MethodDefinition createMethodOne = MethodDefinition.builder().setHeaderCommentStatements(ServiceClientCommentComposer.createMethodNoArgComment(ClassNames.getServiceClientClassName(service))).setScope(ScopeNode.PUBLIC).setIsStatic(true).setIsFinal(true).setReturnType(thisClassType).setName("create").setThrowsExceptions(Arrays.asList(exceptionType)).setReturnExpr(createMethodInvocationExpr).build();
    methods.add(createMethodOne);
    // Second create(ServiceSettings settings) method.
    VariableExpr settingsVarExpr = VariableExpr.withVariable(Variable.builder().setName("settings").setType(typeStore.get(settingsName)).build());
    methods.add(MethodDefinition.builder().setHeaderCommentStatements(ServiceClientCommentComposer.createMethodSettingsArgComment(ClassNames.getServiceClientClassName(service))).setScope(ScopeNode.PUBLIC).setIsStatic(true).setIsFinal(true).setReturnType(thisClassType).setName("create").setThrowsExceptions(Arrays.asList(exceptionType)).setArguments(settingsVarExpr.toBuilder().setIsDecl(true).build()).setReturnExpr(NewObjectExpr.builder().setType(thisClassType).setArguments(settingsVarExpr).build()).build());
    // Third create(ServiceStub stub) method.
    VariableExpr stubVarExpr = VariableExpr.withVariable(Variable.builder().setType(typeStore.get(ClassNames.getServiceStubClassName(service))).setName("stub").build());
    AnnotationNode betaAnnotation = AnnotationNode.builder().setType(typeStore.get("BetaApi")).setDescription("A restructuring of stub classes is planned, so this may break in the future").build();
    methods.add(MethodDefinition.builder().setHeaderCommentStatements(ServiceClientCommentComposer.createCreateMethodStubArgComment(ClassNames.getServiceClientClassName(service), settingsVarExpr.type())).setAnnotations(Arrays.asList(betaAnnotation)).setScope(ScopeNode.PUBLIC).setIsStatic(true).setIsFinal(true).setReturnType(thisClassType).setName("create").setArguments(stubVarExpr.toBuilder().setIsDecl(true).build()).setReturnExpr(NewObjectExpr.builder().setType(thisClassType).setArguments(stubVarExpr).build()).build());
    return methods;
}
Also used : MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) AnnotationNode(com.google.api.generator.engine.ast.AnnotationNode) MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) ArrayList(java.util.ArrayList) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode)

Example 58 with MethodInvocationExpr

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

the class AbstractServiceClientClassComposer method createMethodDefaultMethod.

private static MethodDefinition createMethodDefaultMethod(Method method, String clientName, Map<String, Message> messageTypes, TypeStore typeStore, Map<String, ResourceName> resourceNames, List<Sample> samples) {
    String methodName = JavaStyle.toLowerCamelCase(method.name());
    TypeNode methodInputType = method.inputType();
    TypeNode methodOutputType = method.isPaged() ? typeStore.get(String.format(PAGED_RESPONSE_TYPE_NAME_PATTERN, method.name())) : method.outputType();
    List<AnnotationNode> annotations = new ArrayList<>();
    if (method.hasLro()) {
        LongrunningOperation lro = method.lro();
        methodOutputType = TypeNode.withReference(typeStore.get("OperationFuture").reference().copyAndSetGenerics(Arrays.asList(lro.responseType().reference(), lro.metadataType().reference())));
        if (method.lro().operationServiceStubType() != null) {
            annotations.add(AnnotationNode.withTypeAndDescription(typeStore.get("BetaApi"), "The surface for long-running operations is not stable yet and may change in the" + " future."));
        }
    }
    // Construct the method that accepts a request proto.
    VariableExpr requestArgVarExpr = VariableExpr.builder().setVariable(Variable.builder().setName("request").setType(methodInputType).build()).setIsDecl(true).build();
    String callableMethodName = method.isPaged() ? String.format(PAGED_CALLABLE_NAME_PATTERN, methodName) : String.format(CALLABLE_NAME_PATTERN, methodName);
    if (method.hasLro()) {
        callableMethodName = String.format(OPERATION_CALLABLE_NAME_PATTERN, methodName);
    }
    Optional<Sample> defaultMethodSample = Optional.of(ServiceClientMethodSampleComposer.composeCanonicalSample(method, typeStore.get(clientName), resourceNames, messageTypes));
    Optional<String> defaultMethodDocSample = Optional.empty();
    if (defaultMethodSample.isPresent()) {
        samples.add(defaultMethodSample.get());
        defaultMethodDocSample = Optional.of(SampleCodeWriter.writeInlineSample(defaultMethodSample.get().body()));
    }
    MethodInvocationExpr callableMethodExpr = MethodInvocationExpr.builder().setMethodName(callableMethodName).build();
    callableMethodExpr = MethodInvocationExpr.builder().setMethodName(method.hasLro() ? "futureCall" : "call").setArguments(Arrays.asList(requestArgVarExpr.toBuilder().setIsDecl(false).build())).setExprReferenceExpr(callableMethodExpr).setReturnType(methodOutputType).build();
    MethodDefinition.Builder methodBuilder = MethodDefinition.builder().setHeaderCommentStatements(ServiceClientCommentComposer.createRpcMethodHeaderComment(method, defaultMethodDocSample)).setScope(ScopeNode.PUBLIC).setIsFinal(true).setName(String.format(method.hasLro() ? "%sAsync" : "%s", methodName)).setArguments(Arrays.asList(requestArgVarExpr));
    if (method.isDeprecated()) {
        annotations.add(AnnotationNode.withType(TypeNode.DEPRECATED));
    }
    if (isProtoEmptyType(methodOutputType)) {
        methodBuilder = methodBuilder.setBody(Arrays.asList(ExprStatement.withExpr(callableMethodExpr))).setReturnType(TypeNode.VOID);
    } else {
        methodBuilder = methodBuilder.setReturnExpr(callableMethodExpr).setReturnType(methodOutputType);
    }
    methodBuilder.setAnnotations(annotations);
    return methodBuilder.build();
}
Also used : LongrunningOperation(com.google.api.generator.gapic.model.LongrunningOperation) Sample(com.google.api.generator.gapic.model.Sample) ArrayList(java.util.ArrayList) AnnotationNode(com.google.api.generator.engine.ast.AnnotationNode) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode)

Example 59 with MethodInvocationExpr

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

the class AbstractServiceStubSettingsClassComposer method createDefaultCredentialsProviderBuilderMethod.

protected MethodDefinition createDefaultCredentialsProviderBuilderMethod() {
    TypeNode returnType = TypeNode.withReference(ConcreteReference.withClazz(GoogleCredentialsProvider.Builder.class));
    MethodInvocationExpr credsProviderBuilderExpr = MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("GoogleCredentialsProvider")).setMethodName("newBuilder").build();
    credsProviderBuilderExpr = MethodInvocationExpr.builder().setExprReferenceExpr(credsProviderBuilderExpr).setMethodName("setScopesToApply").setArguments(DEFAULT_SERVICE_SCOPES_VAR_EXPR).setReturnType(returnType).build();
    // enable self signed JWT.
    credsProviderBuilderExpr = MethodInvocationExpr.builder().setExprReferenceExpr(credsProviderBuilderExpr).setMethodName("setUseJwtAccessWithScope").setArguments(ValueExpr.withValue(PrimitiveValue.builder().setType(TypeNode.BOOLEAN).setValue("true").build())).setReturnType(returnType).build();
    return MethodDefinition.builder().setHeaderCommentStatements(SettingsCommentComposer.DEFAULT_CREDENTIALS_PROVIDER_BUILDER_METHOD_COMMENT).setScope(ScopeNode.PUBLIC).setIsStatic(true).setReturnType(returnType).setName("defaultCredentialsProviderBuilder").setReturnExpr(credsProviderBuilderExpr).build();
}
Also used : MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) RequestBuilder(com.google.api.gax.batching.RequestBuilder) TypeNode(com.google.api.generator.engine.ast.TypeNode)

Example 60 with MethodInvocationExpr

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

the class AbstractServiceStubSettingsClassComposer method createClassStatements.

private static List<Statement> createClassStatements(Service service, GapicServiceConfig serviceConfig, Map<String, VariableExpr> methodSettingsMemberVarExprs, Map<String, Message> messageTypes, TypeStore typeStore) {
    Function<Expr, Statement> exprToStatementFn = e -> ExprStatement.withExpr(e);
    List<Statement> statements = new ArrayList<>();
    // Assign DEFAULT_SERVICE_SCOPES.
    statements.add(SettingsCommentComposer.DEFAULT_SCOPES_COMMENT);
    VariableExpr defaultServiceScopesDeclVarExpr = DEFAULT_SERVICE_SCOPES_VAR_EXPR.toBuilder().setIsDecl(true).setScope(ScopeNode.PRIVATE).setIsStatic(true).setIsFinal(true).build();
    MethodInvocationExpr listBuilderExpr = MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("ImmutableList")).setGenerics(Arrays.asList(ConcreteReference.withClazz(String.class))).setMethodName("builder").build();
    for (String serviceScope : service.oauthScopes()) {
        listBuilderExpr = MethodInvocationExpr.builder().setExprReferenceExpr(listBuilderExpr).setMethodName("add").setArguments(ValueExpr.withValue(StringObjectValue.withValue(serviceScope))).build();
    }
    listBuilderExpr = MethodInvocationExpr.builder().setExprReferenceExpr(listBuilderExpr).setMethodName("build").setReturnType(DEFAULT_SERVICE_SCOPES_VAR_EXPR.type()).build();
    statements.add(exprToStatementFn.apply(AssignmentExpr.builder().setVariableExpr(defaultServiceScopesDeclVarExpr).setValueExpr(listBuilderExpr).build()));
    // Declare settings members.
    statements.addAll(methodSettingsMemberVarExprs.values().stream().map(v -> exprToStatementFn.apply(v.toBuilder().setIsDecl(true).setScope(ScopeNode.PRIVATE).setIsFinal(true).build())).collect(Collectors.toList()));
    statements.add(EMPTY_LINE_STATEMENT);
    for (Expr pagingAssignExpr : createPagingStaticAssignExprs(service, serviceConfig, messageTypes, typeStore)) {
        statements.add(exprToStatementFn.apply(pagingAssignExpr));
        statements.add(EMPTY_LINE_STATEMENT);
    }
    for (Method method : service.methods()) {
        Optional<GapicBatchingSettings> batchingSettingOpt = Objects.isNull(serviceConfig) ? Optional.empty() : serviceConfig.getBatchingSetting(service, method);
        if (batchingSettingOpt.isPresent()) {
            statements.add(exprToStatementFn.apply(BatchingDescriptorComposer.createBatchingDescriptorFieldDeclExpr(method, batchingSettingOpt.get(), messageTypes)));
        }
        statements.add(EMPTY_LINE_STATEMENT);
    }
    return statements;
}
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) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) EmptyLineStatement(com.google.api.generator.engine.ast.EmptyLineStatement) IfStatement(com.google.api.generator.engine.ast.IfStatement) CommentStatement(com.google.api.generator.engine.ast.CommentStatement) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) Statement(com.google.api.generator.engine.ast.Statement) ArrayList(java.util.ArrayList) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) Method(com.google.api.generator.gapic.model.Method) GapicBatchingSettings(com.google.api.generator.gapic.model.GapicBatchingSettings)

Aggregations

MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)103 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)76 TypeNode (com.google.api.generator.engine.ast.TypeNode)72 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)59 Expr (com.google.api.generator.engine.ast.Expr)50 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)48 ArrayList (java.util.ArrayList)41 Variable (com.google.api.generator.engine.ast.Variable)39 NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)38 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)36 Statement (com.google.api.generator.engine.ast.Statement)33 List (java.util.List)31 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)29 Test (org.junit.Test)29 RelationalOperationExpr (com.google.api.generator.engine.ast.RelationalOperationExpr)27 MethodDefinition (com.google.api.generator.engine.ast.MethodDefinition)26 Arrays (java.util.Arrays)26 JavaStyle (com.google.api.generator.gapic.utils.JavaStyle)25 Map (java.util.Map)25 Function (java.util.function.Function)24