Search in sources :

Example 96 with Expr

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

use of com.google.api.generator.engine.ast.Expr 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)

Example 98 with Expr

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

the class ResourceNameHelperClassComposer method createParseMethod.

private static MethodDefinition createParseMethod(TypeNode thisClassType, List<VariableExpr> templateFinalVarExprs, List<List<String>> tokenHierarchies, TypeStore typeStore) {
    String formattedStringArgName = "formattedString";
    VariableExpr formattedStringArgExpr = VariableExpr.withVariable(Variable.builder().setName(formattedStringArgName).setType(TypeNode.STRING).build());
    String exceptionMessageString = String.format("%s.parse: %s not in valid format", thisClassType.reference().name(), formattedStringArgName);
    ValueExpr exceptionMessageExpr = ValueExpr.withValue(StringObjectValue.withValue(exceptionMessageString));
    TypeNode mapStringType = TypeNode.withReference(ConcreteReference.builder().setClazz(Map.class).setGenerics(Arrays.asList(ConcreteReference.withClazz(String.class), ConcreteReference.withClazz(String.class))).build());
    VariableExpr matchMapVarExpr = VariableExpr.withVariable(Variable.builder().setName("matchMap").setType(mapStringType).build());
    List<Statement> body = new ArrayList<>();
    body.add(IfStatement.builder().setConditionExpr(MethodInvocationExpr.builder().setExprReferenceExpr(formattedStringArgExpr).setMethodName("isEmpty").setReturnType(TypeNode.BOOLEAN).build()).setBody(Arrays.asList(ExprStatement.withExpr(ReturnExpr.withExpr(ValueExpr.createNullExpr())))).build());
    List<Expr> formattedStringArgList = Arrays.asList(formattedStringArgExpr);
    List<VariableExpr> formattedStringArgDeclList = Arrays.asList(formattedStringArgExpr.toBuilder().setIsDecl(true).build());
    boolean hasVariants = tokenHierarchies.size() > 1;
    if (!hasVariants) {
        List<Expr> methodArgs = Arrays.asList(formattedStringArgExpr, exceptionMessageExpr);
        MethodInvocationExpr validatedMatchExpr = MethodInvocationExpr.builder().setExprReferenceExpr(templateFinalVarExprs.get(0)).setMethodName("validatedMatch").setArguments(methodArgs).setReturnType(mapStringType).build();
        AssignmentExpr matchMapAssignExpr = AssignmentExpr.builder().setVariableExpr(matchMapVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(validatedMatchExpr).build();
        body.add(ExprStatement.withExpr(matchMapAssignExpr));
        List<Expr> ofMethodArgExprs = tokenHierarchies.get(0).stream().map(t -> MethodInvocationExpr.builder().setExprReferenceExpr(matchMapVarExpr).setMethodName("get").setArguments(Arrays.asList(ValueExpr.withValue(StringObjectValue.withValue(t)))).build()).collect(Collectors.toList());
        MethodInvocationExpr ofMethodExpr = MethodInvocationExpr.builder().setMethodName("of").setArguments(ofMethodArgExprs).setReturnType(thisClassType).build();
        return MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setIsStatic(true).setReturnType(thisClassType).setName("parse").setArguments(formattedStringArgDeclList).setBody(body).setReturnExpr(ofMethodExpr).build();
    }
    IfStatement.Builder ifStatementBuilder = IfStatement.builder();
    String ofMethodNamePattern = "of%sName";
    for (int i = 0; i < tokenHierarchies.size(); i++) {
        // PubSub special-case handling for the "_deleted-topic_" pattern.
        boolean isDeletedTopicPattern = tokenHierarchies.get(i).contains(ResourceNameConstants.DELETED_TOPIC_LITERAL);
        VariableExpr templateVarExpr = templateFinalVarExprs.get(i);
        MethodInvocationExpr conditionExpr = MethodInvocationExpr.builder().setExprReferenceExpr(templateVarExpr).setMethodName(isDeletedTopicPattern ? "equals" : "matches").setArguments(formattedStringArgList).setReturnType(TypeNode.BOOLEAN).build();
        MethodInvocationExpr matchValueExpr = MethodInvocationExpr.builder().setExprReferenceExpr(templateVarExpr).setMethodName("match").setArguments(formattedStringArgList).setReturnType(mapStringType).build();
        AssignmentExpr matchMapAssignExpr = AssignmentExpr.builder().setVariableExpr(matchMapVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(matchValueExpr).build();
        List<String> tokens = tokenHierarchies.get(i);
        MethodInvocationExpr ofMethodExpr = MethodInvocationExpr.builder().setMethodName(String.format(ofMethodNamePattern, concatToUpperCamelCaseName(tokens))).setArguments(tokens.stream().map(t -> MethodInvocationExpr.builder().setExprReferenceExpr(matchMapVarExpr).setMethodName("get").setArguments(Arrays.asList(ValueExpr.withValue(StringObjectValue.withValue(t)))).build()).collect(Collectors.toList())).setReturnType(thisClassType).build();
        ReturnExpr subReturnExpr = ReturnExpr.withExpr(ofMethodExpr);
        List<Statement> ifStatements = Arrays.asList(matchMapAssignExpr, subReturnExpr).stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList());
        if (i == 0) {
            ifStatementBuilder = ifStatementBuilder.setConditionExpr(conditionExpr).setBody(ifStatements);
        } else {
            // _deleted-topic_.
            if (isDeletedTopicPattern) {
                ifStatements.clear();
                ifStatements.add(ExprStatement.withExpr(ReturnExpr.withExpr(NewObjectExpr.builder().setType(thisClassType).setArguments(ValueExpr.withValue(StringObjectValue.withValue(ResourceNameConstants.DELETED_TOPIC_LITERAL))).build())));
            }
            ifStatementBuilder = ifStatementBuilder.addElseIf(conditionExpr, ifStatements);
        }
    }
    body.add(ifStatementBuilder.build());
    body.add(ExprStatement.withExpr(ThrowExpr.builder().setType(FIXED_TYPESTORE.get("ValidationException")).setMessageExpr(exceptionMessageString).build()));
    return MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setIsStatic(true).setReturnType(thisClassType).setName("parse").setArguments(formattedStringArgDeclList).setBody(body).build();
}
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) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) ForStatement(com.google.api.generator.engine.ast.ForStatement) IfStatement(com.google.api.generator.engine.ast.IfStatement) SynchronizedStatement(com.google.api.generator.engine.ast.SynchronizedStatement) 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) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) IfStatement(com.google.api.generator.engine.ast.IfStatement) 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) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) ReturnExpr(com.google.api.generator.engine.ast.ReturnExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap)

Example 99 with Expr

use of com.google.api.generator.engine.ast.Expr 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)

Example 100 with Expr

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

the class BatchingDescriptorComposer method createGetBatchPartitionKeyMethod.

private static MethodDefinition createGetBatchPartitionKeyMethod(Method method, GapicBatchingSettings batchingSettings, Map<String, Message> messageTypes) {
    String methodInputTypeName = method.inputType().reference().fullName();
    Message inputMessage = messageTypes.get(methodInputTypeName);
    Preconditions.checkNotNull(inputMessage, String.format("Message %s not found for RPC method %s", methodInputTypeName, method.name()));
    VariableExpr requestVarExpr = VariableExpr.withVariable(Variable.builder().setType(method.inputType()).setName("request").build());
    List<Expr> partitionKeyArgExprs = new ArrayList<>();
    for (String discriminatorFieldName : batchingSettings.discriminatorFieldNames()) {
        Preconditions.checkNotNull(inputMessage.fieldMap().get(discriminatorFieldName), String.format("Batching discriminator field %s not found in message %s", discriminatorFieldName, inputMessage.name()));
        String getterMethodName = String.format("get%s", JavaStyle.toUpperCamelCase(discriminatorFieldName));
        partitionKeyArgExprs.add(MethodInvocationExpr.builder().setExprReferenceExpr(requestVarExpr).setMethodName(getterMethodName).build());
    }
    Expr returnExpr = NewObjectExpr.builder().setType(PARTITION_KEY_TYPE).setArguments(partitionKeyArgExprs).build();
    return MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setReturnType(PARTITION_KEY_TYPE).setName("getBatchPartitionKey").setArguments(requestVarExpr.toBuilder().setIsDecl(true).build()).setReturnExpr(returnExpr).build();
}
Also used : Message(com.google.api.generator.gapic.model.Message) 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) ArrayList(java.util.ArrayList) VariableExpr(com.google.api.generator.engine.ast.VariableExpr)

Aggregations

Expr (com.google.api.generator.engine.ast.Expr)151 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)130 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)126 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)124 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)114 NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)96 TypeNode (com.google.api.generator.engine.ast.TypeNode)80 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)72 ArrayList (java.util.ArrayList)72 RelationalOperationExpr (com.google.api.generator.engine.ast.RelationalOperationExpr)67 Statement (com.google.api.generator.engine.ast.Statement)64 CastExpr (com.google.api.generator.engine.ast.CastExpr)59 TernaryExpr (com.google.api.generator.engine.ast.TernaryExpr)59 AnonymousClassExpr (com.google.api.generator.engine.ast.AnonymousClassExpr)58 List (java.util.List)58 EnumRefExpr (com.google.api.generator.engine.ast.EnumRefExpr)55 Variable (com.google.api.generator.engine.ast.Variable)54 ThrowExpr (com.google.api.generator.engine.ast.ThrowExpr)51 ReferenceConstructorExpr (com.google.api.generator.engine.ast.ReferenceConstructorExpr)49 ReturnExpr (com.google.api.generator.engine.ast.ReturnExpr)47