Search in sources :

Example 61 with MethodInvocationExpr

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

the class AbstractServiceCallableFactoryClassComposer method createGenericCallableMethod.

protected MethodDefinition createGenericCallableMethod(Service service, TypeStore typeStore, List<String> methodTemplateNames, String returnCallableKindName, List<String> returnCallableTemplateNames, String methodVariantName, List<Object> transportCallSettingsTemplateObjects, String callSettingsVariantName, List<Object> callSettingsTemplateObjects, List<AnnotationNode> annotations) {
    String methodName = String.format("create%sCallable", methodVariantName);
    String callSettingsTypeName = String.format("%sCallSettings", callSettingsVariantName);
    String returnTypeName = String.format("%sCallable", returnCallableKindName);
    boolean isOperationCallable = methodVariantName.equals("Operation");
    List<VariableExpr> arguments = new ArrayList<>();
    arguments.add(VariableExpr.builder().setVariable(Variable.builder().setName(getTransportContext().transportCallSettingsName()).setType(getTransportContext().transportCallSettingsType()).build()).setIsDecl(true).setTemplateObjects(transportCallSettingsTemplateObjects).build());
    arguments.add(VariableExpr.builder().setVariable(Variable.builder().setName("callSettings").setType(typeStore.get(callSettingsTypeName)).build()).setIsDecl(true).setTemplateObjects(callSettingsTemplateObjects).build());
    arguments.add(VariableExpr.builder().setVariable(Variable.builder().setName("clientContext").setType(typeStore.get("ClientContext")).build()).setIsDecl(true).build());
    if (isOperationCallable) {
        arguments.add(VariableExpr.builder().setVariable(Variable.builder().setName("operationsStub").setType(getOperationsStubType(service)).build()).setIsDecl(true).build());
    }
    TypeNode returnType = typeStore.get(returnTypeName);
    MethodInvocationExpr returnExpr = MethodInvocationExpr.builder().setMethodName(methodName).setStaticReferenceType(getTransportContext().transportCallableFactoryType()).setArguments(arguments.stream().map(v -> v.toBuilder().setIsDecl(false).build()).collect(Collectors.toList())).setReturnType(returnType).build();
    return MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setTemplateNames(methodTemplateNames).setReturnType(returnType).setReturnTemplateNames(returnCallableTemplateNames).setName(methodName).setArguments(arguments).setReturnExpr(returnExpr).setAnnotations(annotations).build();
}
Also used : MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ArrayList(java.util.ArrayList) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode)

Example 62 with MethodInvocationExpr

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

the class AbstractServiceClientClassComposer method createNestedRpcPagedResponseClass.

private static ClassDefinition createNestedRpcPagedResponseClass(Method method, TypeNode repeatedResponseType, Map<String, Message> messageTypes, TypeStore typeStore) {
    Preconditions.checkState(method.isPaged(), String.format("Expected method %s to be paged", method.name()));
    String className = String.format("%sPagedResponse", JavaStyle.toUpperCamelCase(method.name()));
    TypeNode thisClassType = typeStore.get(className);
    String upperJavaMethodName = JavaStyle.toUpperCamelCase(method.name());
    TypeNode methodPageType = typeStore.get(String.format("%sPage", upperJavaMethodName));
    TypeNode classExtendsType = TypeNode.withReference(ConcreteReference.builder().setClazz(AbstractPagedListResponse.class).setGenerics(Arrays.asList(method.inputType(), method.outputType(), repeatedResponseType, methodPageType, typeStore.get(String.format("%sFixedSizeCollection", upperJavaMethodName))).stream().map(t -> t.reference()).collect(Collectors.toList())).build());
    // createAsync method - variables.
    VariableExpr contextVarExpr = VariableExpr.withVariable(Variable.builder().setName("context").setType(TypeNode.withReference(ConcreteReference.builder().setClazz(PageContext.class).setGenerics(Arrays.asList(method.inputType(), method.outputType(), repeatedResponseType).stream().map(t -> t.reference()).collect(Collectors.toList())).build())).build());
    VariableExpr futureResponseVarExpr = VariableExpr.withVariable(Variable.builder().setName("futureResponse").setType(TypeNode.withReference(ConcreteReference.builder().setClazz(ApiFuture.class).setGenerics(Arrays.asList(method.outputType().reference())).build())).build());
    VariableExpr futurePageVarExpr = VariableExpr.withVariable(Variable.builder().setName("futurePage").setType(TypeNode.withReference(ConcreteReference.builder().setClazz(ApiFuture.class).setGenerics(Arrays.asList(methodPageType.reference())).build())).build());
    // createAsync method - assignment expression.
    MethodInvocationExpr createPageAsyncExpr = MethodInvocationExpr.builder().setStaticReferenceType(methodPageType).setMethodName("createEmptyPage").build();
    createPageAsyncExpr = MethodInvocationExpr.builder().setExprReferenceExpr(createPageAsyncExpr).setMethodName("createPageAsync").setArguments(contextVarExpr, futureResponseVarExpr).setReturnType(futurePageVarExpr.type()).build();
    AssignmentExpr futurePageAssignExpr = AssignmentExpr.builder().setVariableExpr(futurePageVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(createPageAsyncExpr).build();
    // createAsync method - anonymous class expression.
    VariableExpr inputVarExpr = VariableExpr.withVariable(Variable.builder().setName("input").setType(methodPageType).build());
    // Overrides ApiFunction.apply.
    // (https://github.com/googleapis/api-common-java/blob/debf25960dea0367b0d3b5e16d57d76c1d01947e/src/main/java/com/google/api/core/ApiFunction.java).
    Expr pageToTransformExpr = LambdaExpr.builder().setArguments(inputVarExpr.toBuilder().setIsDecl(true).build()).setReturnExpr(NewObjectExpr.builder().setType(thisClassType).setArguments(inputVarExpr).build()).build();
    // createAsync method - return expression.
    TypeNode returnType = TypeNode.withReference(ConcreteReference.builder().setClazz(ApiFuture.class).setGenerics(Arrays.asList(typeStore.get(className).reference())).build());
    Expr returnExpr = MethodInvocationExpr.builder().setStaticReferenceType(typeStore.get("ApiFutures")).setMethodName("transform").setArguments(futurePageVarExpr, pageToTransformExpr, MethodInvocationExpr.builder().setStaticReferenceType(typeStore.get("MoreExecutors")).setMethodName("directExecutor").build()).setReturnType(returnType).build();
    MethodDefinition createAsyncMethod = MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setIsStatic(true).setReturnType(returnType).setName("createAsync").setArguments(Arrays.asList(contextVarExpr, futureResponseVarExpr).stream().map(e -> e.toBuilder().setIsDecl(true).build()).collect(Collectors.toList())).setBody(Arrays.asList(ExprStatement.withExpr(futurePageAssignExpr))).setReturnExpr(returnExpr).build();
    // Private constructor.
    VariableExpr pageVarExpr = VariableExpr.withVariable(Variable.builder().setName("page").setType(methodPageType).build());
    MethodDefinition privateCtor = MethodDefinition.constructorBuilder().setScope(ScopeNode.PRIVATE).setReturnType(thisClassType).setArguments(pageVarExpr.toBuilder().setIsDecl(true).build()).setBody(Arrays.asList(ExprStatement.withExpr(// Shortcut.
    ReferenceConstructorExpr.superBuilder().setType(methodPageType).setArguments(pageVarExpr, MethodInvocationExpr.builder().setStaticReferenceType(typeStore.get(String.format("%sFixedSizeCollection", upperJavaMethodName))).setMethodName("createEmptyCollection").build()).build()))).build();
    List<MethodDefinition> javaMethods = new ArrayList<>();
    javaMethods.add(createAsyncMethod);
    javaMethods.add(privateCtor);
    return ClassDefinition.builder().setIsNested(true).setScope(ScopeNode.PUBLIC).setIsStatic(true).setExtendsType(classExtendsType).setName(className).setMethods(javaMethods).build();
}
Also used : Arrays(java.util.Arrays) ServiceClientHeaderSampleComposer(com.google.api.generator.gapic.composer.samplecode.ServiceClientHeaderSampleComposer) OperationFuture(com.google.api.gax.longrunning.OperationFuture) BiFunction(java.util.function.BiFunction) Reference(com.google.api.generator.engine.ast.Reference) 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) Field(com.google.api.generator.gapic.model.Field) 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) MethodArgument(com.google.api.generator.gapic.model.MethodArgument) PrimitiveValue(com.google.api.generator.engine.ast.PrimitiveValue) LongrunningOperation(com.google.api.generator.gapic.model.LongrunningOperation) Stream(com.google.api.generator.gapic.model.Method.Stream) Map(java.util.Map) ServiceClientMethodSampleComposer(com.google.api.generator.gapic.composer.samplecode.ServiceClientMethodSampleComposer) ServerStreamingCallable(com.google.api.gax.rpc.ServerStreamingCallable) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) ApiFutures(com.google.api.core.ApiFutures) PageContext(com.google.api.gax.rpc.PageContext) Status(com.google.rpc.Status) Set(java.util.Set) ScopeNode(com.google.api.generator.engine.ast.ScopeNode) ServiceClientCommentComposer(com.google.api.generator.gapic.composer.comment.ServiceClientCommentComposer) Collectors(java.util.stream.Collectors) ApiFuture(com.google.api.core.ApiFuture) Objects(java.util.Objects) ClassNames(com.google.api.generator.gapic.composer.utils.ClassNames) List(java.util.List) ServiceClientCallableMethodSampleComposer(com.google.api.generator.gapic.composer.samplecode.ServiceClientCallableMethodSampleComposer) SampleCodeWriter(com.google.api.generator.gapic.composer.samplecode.SampleCodeWriter) Optional(java.util.Optional) GapicContext(com.google.api.generator.gapic.model.GapicContext) ApiFunction(com.google.api.core.ApiFunction) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) 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) ClassDefinition(com.google.api.generator.engine.ast.ClassDefinition) PackageChecker(com.google.api.generator.gapic.composer.utils.PackageChecker) HashMap(java.util.HashMap) GapicMetadata(com.google.gapic.metadata.GapicMetadata) Operation(com.google.longrunning.Operation) Function(java.util.function.Function) ArrayList(java.util.ArrayList) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) Strings(com.google.common.base.Strings) Expr(com.google.api.generator.engine.ast.Expr) TernaryExpr(com.google.api.generator.engine.ast.TernaryExpr) RelationalOperationExpr(com.google.api.generator.engine.ast.RelationalOperationExpr) ClientStreamingCallable(com.google.api.gax.rpc.ClientStreamingCallable) TriFunction(com.google.api.generator.util.TriFunction) AbstractFixedSizeCollection(com.google.api.gax.paging.AbstractFixedSizeCollection) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) Iterator(java.util.Iterator) AbstractPage(com.google.api.gax.paging.AbstractPage) 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) AnnotationNode(com.google.api.generator.engine.ast.AnnotationNode) Sample(com.google.api.generator.gapic.model.Sample) GapicClass(com.google.api.generator.gapic.model.GapicClass) ResourceName(com.google.api.generator.gapic.model.ResourceName) Trie(com.google.api.generator.util.Trie) 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) LambdaExpr(com.google.api.generator.engine.ast.LambdaExpr) SuperObjectValue(com.google.api.generator.engine.ast.SuperObjectValue) SampleComposerUtil(com.google.api.generator.gapic.composer.samplecode.SampleComposerUtil) JavaStyle(com.google.api.generator.gapic.utils.JavaStyle) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Message(com.google.api.generator.gapic.model.Message) Collections(java.util.Collections) AbstractPagedListResponse(com.google.api.gax.paging.AbstractPagedListResponse) ApiFuture(com.google.api.core.ApiFuture) 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) 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) CastExpr(com.google.api.generator.engine.ast.CastExpr) LambdaExpr(com.google.api.generator.engine.ast.LambdaExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) AbstractPagedListResponse(com.google.api.gax.paging.AbstractPagedListResponse) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) 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) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr)

Example 63 with MethodInvocationExpr

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

the class AbstractServiceClientClassComposer method createCallableMethod.

private static MethodDefinition createCallableMethod(Service service, Method method, CallableMethodKind callableMethodKind, TypeStore typeStore, Map<String, Message> messageTypes, Map<String, ResourceName> resourceNames, List<Sample> samples) {
    TypeNode rawCallableReturnType = null;
    if (callableMethodKind.equals(CallableMethodKind.LRO)) {
        rawCallableReturnType = typeStore.get("OperationCallable");
    } else {
        switch(method.stream()) {
            case CLIENT:
                rawCallableReturnType = typeStore.get("ClientStreamingCallable");
                break;
            case SERVER:
                rawCallableReturnType = typeStore.get("ServerStreamingCallable");
                break;
            case BIDI:
                rawCallableReturnType = typeStore.get("BidiStreamingCallable");
                break;
            case NONE:
            // Fall through.
            default:
                rawCallableReturnType = typeStore.get("UnaryCallable");
        }
    }
    // Set generics.
    TypeNode returnType = TypeNode.withReference(rawCallableReturnType.reference().copyAndSetGenerics(getGenericsForCallable(callableMethodKind, method, typeStore)));
    String rawMethodName = JavaStyle.toLowerCamelCase(method.name());
    String methodName = getCallableName(callableMethodKind, rawMethodName);
    TypeNode stubType = typeStore.get(ClassNames.getServiceStubClassName(service));
    MethodInvocationExpr returnExpr = MethodInvocationExpr.builder().setExprReferenceExpr(VariableExpr.builder().setVariable(Variable.builder().setName("stub").setType(stubType).build()).build()).setMethodName(methodName).setReturnType(returnType).build();
    Optional<Sample> sampleCode = Optional.empty();
    if (callableMethodKind.equals(CallableMethodKind.LRO)) {
        sampleCode = Optional.of(ServiceClientCallableMethodSampleComposer.composeLroCallableMethod(method, typeStore.get(ClassNames.getServiceClientClassName(service)), resourceNames, messageTypes));
    } else if (callableMethodKind.equals(CallableMethodKind.PAGED)) {
        sampleCode = Optional.of(ServiceClientCallableMethodSampleComposer.composePagedCallableMethod(method, typeStore.get(ClassNames.getServiceClientClassName(service)), resourceNames, messageTypes));
    } else if (callableMethodKind.equals(CallableMethodKind.REGULAR)) {
        if (method.stream().equals(Stream.NONE)) {
            sampleCode = Optional.of(ServiceClientCallableMethodSampleComposer.composeRegularCallableMethod(method, typeStore.get(ClassNames.getServiceClientClassName(service)), resourceNames, messageTypes));
        } else {
            sampleCode = Optional.of(ServiceClientCallableMethodSampleComposer.composeStreamCallableMethod(method, typeStore.get(ClassNames.getServiceClientClassName(service)), resourceNames, messageTypes));
        }
    }
    Optional<String> sampleDocCode = Optional.empty();
    if (sampleCode.isPresent()) {
        samples.add(sampleCode.get());
        sampleDocCode = Optional.of(SampleCodeWriter.writeInlineSample(sampleCode.get().body()));
    }
    MethodDefinition.Builder methodDefBuilder = MethodDefinition.builder();
    if (method.isDeprecated()) {
        methodDefBuilder = methodDefBuilder.setAnnotations(Arrays.asList(AnnotationNode.withType(TypeNode.DEPRECATED)));
    }
    return methodDefBuilder.setHeaderCommentStatements(ServiceClientCommentComposer.createRpcCallableMethodHeaderComment(method, sampleDocCode)).setScope(ScopeNode.PUBLIC).setIsFinal(true).setName(methodName).setReturnType(returnType).setReturnExpr(returnExpr).build();
}
Also used : MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) Sample(com.google.api.generator.gapic.model.Sample) MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) TypeNode(com.google.api.generator.engine.ast.TypeNode)

Example 64 with MethodInvocationExpr

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

the class JavaWriterVisitor method visit.

@Override
public void visit(MethodInvocationExpr methodInvocationExpr) {
    // Expression or static reference.
    if (methodInvocationExpr.exprReferenceExpr() != null) {
        methodInvocationExpr.exprReferenceExpr().accept(this);
        buffer.append(DOT);
    } else if (methodInvocationExpr.staticReferenceType() != null) {
        methodInvocationExpr.staticReferenceType().accept(this);
        buffer.append(DOT);
    }
    if (methodInvocationExpr.isGeneric()) {
        leftAngle();
        int numGenerics = methodInvocationExpr.generics().size();
        for (int i = 0; i < numGenerics; i++) {
            buffer.append(methodInvocationExpr.generics().get(i).name());
            if (i < numGenerics - 1) {
                buffer.append(COMMA);
                space();
            }
        }
        rightAngle();
    }
    methodInvocationExpr.methodIdentifier().accept(this);
    leftParen();
    int numArguments = methodInvocationExpr.arguments().size();
    for (int i = 0; i < numArguments; i++) {
        Expr argExpr = methodInvocationExpr.arguments().get(i);
        argExpr.accept(this);
        if (i < numArguments - 1) {
            buffer.append(COMMA);
            space();
        }
    }
    rightParen();
}
Also used : ReferenceConstructorExpr(com.google.api.generator.engine.ast.ReferenceConstructorExpr) LogicalOperationExpr(com.google.api.generator.engine.ast.LogicalOperationExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) EnumRefExpr(com.google.api.generator.engine.ast.EnumRefExpr) ReturnExpr(com.google.api.generator.engine.ast.ReturnExpr) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) Expr(com.google.api.generator.engine.ast.Expr) TernaryExpr(com.google.api.generator.engine.ast.TernaryExpr) 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) InstanceofExpr(com.google.api.generator.engine.ast.InstanceofExpr) CastExpr(com.google.api.generator.engine.ast.CastExpr) ArithmeticOperationExpr(com.google.api.generator.engine.ast.ArithmeticOperationExpr) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr) LambdaExpr(com.google.api.generator.engine.ast.LambdaExpr) AssignmentOperationExpr(com.google.api.generator.engine.ast.AssignmentOperationExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ThrowExpr(com.google.api.generator.engine.ast.ThrowExpr)

Example 65 with MethodInvocationExpr

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

the class ResourceNameHelperClassComposer method createOfOrFormatMethodHelper.

private static List<MethodDefinition> createOfOrFormatMethodHelper(ResourceName resourceName, Map<String, VariableExpr> patternTokenVarExprs, List<List<String>> tokenHierarchies, TypeStore typeStore, boolean isFormatMethod) {
    List<MethodDefinition> javaMethods = new ArrayList<>();
    String methodNameFormat = isFormatMethod ? "format%s" : "of%s";
    String newBuilderMethodNameFormat = "new%s";
    String setMethodNameFormat = "set%s";
    String buildMethodName = "build";
    String toStringMethodName = "toString";
    AnnotationNode betaAnnotation = AnnotationNode.builder().setType(FIXED_TYPESTORE.get("BetaApi")).setDescription(String.format("The static %s methods are not stable yet and may be changed in the future.", isFormatMethod ? "format" : "create")).build();
    List<AnnotationNode> annotations = Arrays.asList(betaAnnotation);
    TypeNode thisClassType = typeStore.get(getThisClassName(resourceName));
    TypeNode returnType = isFormatMethod ? TypeNode.STRING : thisClassType;
    // Create the newBuilder and variation methods here.
    // Variation example: newProjectLocationAutoscalingPolicyBuilder().
    boolean hasVariants = tokenHierarchies.size() > 1;
    for (int i = 0; i < tokenHierarchies.size(); i++) {
        List<String> tokens = tokenHierarchies.get(i);
        // PubSub special-case handling.
        if (tokens.contains(ResourceNameConstants.DELETED_TOPIC_LITERAL)) {
            Expr deletedTopicStringValExpr = ValueExpr.withValue(StringObjectValue.withValue(ResourceNameConstants.DELETED_TOPIC_LITERAL));
            // Simply return `new TopicName("_deleted-topic_")` or the string value itself.
            javaMethods.add(MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setIsStatic(true).setAnnotations(annotations).setReturnType(returnType).setName(String.format(methodNameFormat, concatToUpperCamelCaseName(tokens) + "Name")).setReturnExpr(returnType.equals(TypeNode.STRING) ? deletedTopicStringValExpr : NewObjectExpr.builder().setType(returnType).setArguments(deletedTopicStringValExpr).build()).build());
            continue;
        }
        String builderMethodName = String.format(newBuilderMethodNameFormat, getBuilderTypeName(tokenHierarchies, i));
        MethodInvocationExpr returnExpr = MethodInvocationExpr.builder().setMethodName(builderMethodName).build();
        for (String token : tokens) {
            String javaTokenVarName = JavaStyle.toLowerCamelCase(token);
            returnExpr = MethodInvocationExpr.builder().setExprReferenceExpr(returnExpr).setMethodName(String.format(setMethodNameFormat, JavaStyle.toUpperCamelCase(token))).setArguments(Arrays.asList(VariableExpr.withVariable(Variable.builder().setName(javaTokenVarName).setType(TypeNode.STRING).build()))).build();
        }
        returnExpr = MethodInvocationExpr.builder().setExprReferenceExpr(returnExpr).setMethodName(buildMethodName).setReturnType(thisClassType).build();
        if (isFormatMethod) {
            returnExpr = MethodInvocationExpr.builder().setExprReferenceExpr(returnExpr).setMethodName(toStringMethodName).setReturnType(TypeNode.STRING).build();
        }
        List<VariableExpr> methodArgs = tokens.stream().map(t -> patternTokenVarExprs.get(t).toBuilder().setIsDecl(true).build()).collect(Collectors.toList());
        javaMethods.add(MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setIsStatic(true).setAnnotations(i == 0 ? Collections.emptyList() : annotations).setReturnType(returnType).setName(String.format(methodNameFormat, i == 0 ? "" : concatToUpperCamelCaseName(tokens) + "Name")).setArguments(methodArgs).setReturnExpr(returnExpr).build());
        if (i == 0 && hasVariants) {
            javaMethods.add(MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setIsStatic(true).setAnnotations(annotations).setReturnType(returnType).setName(String.format(methodNameFormat, concatToUpperCamelCaseName(tokenHierarchies.get(i)) + "Name")).setArguments(methodArgs).setReturnExpr(returnExpr).build());
        }
    }
    return javaMethods;
}
Also used : Arrays(java.util.Arrays) Reference(com.google.api.generator.engine.ast.Reference) PathTemplate(com.google.api.pathtemplate.PathTemplate) ThisObjectValue(com.google.api.generator.engine.ast.ThisObjectValue) Variable(com.google.api.generator.engine.ast.Variable) StringObjectValue(com.google.api.generator.engine.ast.StringObjectValue) LogicalOperationExpr(com.google.api.generator.engine.ast.LogicalOperationExpr) Generated(javax.annotation.Generated) MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) TypeStore(com.google.api.generator.gapic.composer.store.TypeStore) PrimitiveValue(com.google.api.generator.engine.ast.PrimitiveValue) CommentComposer(com.google.api.generator.gapic.composer.comment.CommentComposer) Map(java.util.Map) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) ImmutableMap(com.google.common.collect.ImmutableMap) ForStatement(com.google.api.generator.engine.ast.ForStatement) Set(java.util.Set) ScopeNode(com.google.api.generator.engine.ast.ScopeNode) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) ResourceNameConstants(com.google.api.generator.gapic.utils.ResourceNameConstants) List(java.util.List) GapicContext(com.google.api.generator.gapic.model.GapicContext) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) IfStatement(com.google.api.generator.engine.ast.IfStatement) BetaApi(com.google.api.core.BetaApi) TypeNode(com.google.api.generator.engine.ast.TypeNode) ClassDefinition(com.google.api.generator.engine.ast.ClassDefinition) ReturnExpr(com.google.api.generator.engine.ast.ReturnExpr) HashMap(java.util.HashMap) 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) Expr(com.google.api.generator.engine.ast.Expr) TernaryExpr(com.google.api.generator.engine.ast.TernaryExpr) RelationalOperationExpr(com.google.api.generator.engine.ast.RelationalOperationExpr) SynchronizedStatement(com.google.api.generator.engine.ast.SynchronizedStatement) ValidationException(com.google.api.pathtemplate.ValidationException) LinkedHashSet(java.util.LinkedHashSet) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) Iterator(java.util.Iterator) CastExpr(com.google.api.generator.engine.ast.CastExpr) CommentStatement(com.google.api.generator.engine.ast.CommentStatement) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) AnnotationNode(com.google.api.generator.engine.ast.AnnotationNode) GapicClass(com.google.api.generator.gapic.model.GapicClass) ResourceName(com.google.api.generator.gapic.model.ResourceName) Statement(com.google.api.generator.engine.ast.Statement) JavaStyle(com.google.api.generator.gapic.utils.JavaStyle) AssignmentOperationExpr(com.google.api.generator.engine.ast.AssignmentOperationExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ThrowExpr(com.google.api.generator.engine.ast.ThrowExpr) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) ArrayList(java.util.ArrayList) LogicalOperationExpr(com.google.api.generator.engine.ast.LogicalOperationExpr) 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) 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) CastExpr(com.google.api.generator.engine.ast.CastExpr) AssignmentOperationExpr(com.google.api.generator.engine.ast.AssignmentOperationExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ThrowExpr(com.google.api.generator.engine.ast.ThrowExpr) 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)

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