Search in sources :

Example 36 with VariableExpr

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

the class ResourceNameHelperClassComposer method createGetFieldValuesMapMethod.

private static MethodDefinition createGetFieldValuesMapMethod(ResourceName resourceName, TypeNode thisClassType, Map<String, VariableExpr> patternTokenVarExprs, List<List<String>> tokenHierarchies) {
    Reference strRef = TypeNode.STRING.reference();
    TypeNode mapBuilderType = TypeNode.withReference(ConcreteReference.builder().setClazz(ImmutableMap.Builder.class).setGenerics(Arrays.asList(strRef, strRef)).build());
    VariableExpr fieldMapBuilderVarExpr = VariableExpr.withVariable(Variable.builder().setName("fieldMapBuilder").setType(mapBuilderType).build());
    AssignmentExpr builderAssignExpr = AssignmentExpr.builder().setVariableExpr(fieldMapBuilderVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("ImmutableMap")).setMethodName("builder").setReturnType(mapBuilderType).build()).build();
    // Special-cased PubSub handling.
    List<List<String>> processedTokenHierarchies = tokenHierarchies.stream().filter(tokens -> !tokens.contains(ResourceNameConstants.DELETED_TOPIC_LITERAL)).collect(Collectors.toList());
    // Innermost if-blocks.
    List<Statement> tokenIfStatements = new ArrayList<>();
    for (String token : getTokenSet(processedTokenHierarchies)) {
        VariableExpr tokenVarExpr = patternTokenVarExprs.get(token);
        Preconditions.checkNotNull(tokenVarExpr, String.format("No variable found for %s among %s", token, patternTokenVarExprs.keySet()));
        StringObjectValue tokenStrVal = StringObjectValue.withValue(token);
        MethodInvocationExpr putExpr = MethodInvocationExpr.builder().setExprReferenceExpr(fieldMapBuilderVarExpr).setMethodName("put").setArguments(ValueExpr.withValue(tokenStrVal), tokenVarExpr).build();
        Expr notNullCheckExpr = RelationalOperationExpr.notEqualToWithExprs(tokenVarExpr, ValueExpr.createNullExpr());
        tokenIfStatements.add(IfStatement.builder().setConditionExpr(notNullCheckExpr).setBody(Arrays.asList(ExprStatement.withExpr(putExpr))).build());
    }
    // Put the innermost if-statements and assignment expressions together.
    VariableExpr fieldValuesMapVarExpr = FIXED_CLASS_VARS.get("fieldValuesMap");
    AssignmentExpr fieldValuesMapAssignExpr = AssignmentExpr.builder().setVariableExpr(fieldValuesMapVarExpr).setValueExpr(MethodInvocationExpr.builder().setExprReferenceExpr(fieldMapBuilderVarExpr).setMethodName("build").setReturnType(fieldValuesMapVarExpr.type()).build()).build();
    List<Statement> middleIfBlockStatements = new ArrayList<>();
    middleIfBlockStatements.add(ExprStatement.withExpr(builderAssignExpr));
    middleIfBlockStatements.addAll(tokenIfStatements);
    middleIfBlockStatements.add(ExprStatement.withExpr(fieldValuesMapAssignExpr));
    // Middle if-block, i.e. `if (fieldValuesMap == null)`.
    Expr fieldValuesMapNullCheckExpr = RelationalOperationExpr.equalToWithExprs(fieldValuesMapVarExpr, ValueExpr.createNullExpr());
    IfStatement fieldValuesMapIfStatement = IfStatement.builder().setConditionExpr(fieldValuesMapNullCheckExpr).setBody(middleIfBlockStatements).build();
    // Outer if-block.
    IfStatement outerIfStatement = IfStatement.builder().setConditionExpr(fieldValuesMapNullCheckExpr).setBody(Arrays.asList(SynchronizedStatement.builder().setLock(ThisObjectValue.withType(thisClassType)).setBody(Arrays.asList(fieldValuesMapIfStatement)).build())).build();
    // Put the method together.
    TypeNode mapStringType = fieldValuesMapVarExpr.type();
    return MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setReturnType(mapStringType).setName("getFieldValuesMap").setBody(Arrays.asList(outerIfStatement)).setReturnExpr(fieldValuesMapVarExpr).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) StringObjectValue(com.google.api.generator.engine.ast.StringObjectValue) Reference(com.google.api.generator.engine.ast.Reference) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) 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) List(java.util.List) ArrayList(java.util.ArrayList) TypeNode(com.google.api.generator.engine.ast.TypeNode)

Example 37 with VariableExpr

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

the class ResourceNameHelperClassComposer method createEqualsMethod.

private static MethodDefinition createEqualsMethod(ResourceName resourceName, List<List<String>> tokenHierarchies, TypeStore typeStore) {
    // Create method definition variables.
    Variable oVariable = Variable.builder().setType(TypeNode.withReference(javaObjectReference)).setName("o").build();
    VariableExpr argVarExpr = VariableExpr.builder().setIsDecl(false).setVariable(oVariable).build();
    TypeNode thisClassType = typeStore.get(getThisClassName(resourceName));
    ValueExpr thisValueExpr = ValueExpr.withValue(ThisObjectValue.withType(thisClassType));
    ValueExpr trueValueExpr = ValueExpr.withValue(PrimitiveValue.builder().setType(TypeNode.BOOLEAN).setValue("true").build());
    // Create first if statement's return expression
    ReturnExpr returnTrueExpr = ReturnExpr.withExpr(trueValueExpr);
    // Create second if statement's condition expression
    RelationalOperationExpr oEqualsThisExpr = RelationalOperationExpr.equalToWithExprs(argVarExpr, thisValueExpr);
    RelationalOperationExpr oNotEqualsNullExpr = RelationalOperationExpr.notEqualToWithExprs(argVarExpr, ValueExpr.createNullExpr());
    MethodInvocationExpr getClassMethodInvocationExpr = MethodInvocationExpr.builder().setMethodName("getClass").build();
    RelationalOperationExpr getClassEqualsExpr = RelationalOperationExpr.equalToWithExprs(getClassMethodInvocationExpr, getClassMethodInvocationExpr.toBuilder().setExprReferenceExpr(argVarExpr).build());
    LogicalOperationExpr orLogicalExpr = LogicalOperationExpr.logicalOrWithExprs(oNotEqualsNullExpr, getClassEqualsExpr);
    // Create second if statement's body assignment expression.
    Variable thatVariable = Variable.builder().setName("that").setType(thisClassType).build();
    VariableExpr thatVariableExpr = VariableExpr.builder().setIsDecl(false).setVariable(thatVariable).build();
    CastExpr oCastExpr = CastExpr.builder().setExpr(argVarExpr).setType(thisClassType).build();
    AssignmentExpr thatAssignmentExpr = AssignmentExpr.builder().setVariableExpr(thatVariableExpr.toBuilder().setIsDecl(true).build()).setValueExpr(oCastExpr).build();
    // PubSub special-case handling - exclude _deleted-topic_.
    List<List<String>> processedTokenHierarchies = tokenHierarchies.stream().filter(ts -> !ts.contains(ResourceNameConstants.DELETED_TOPIC_LITERAL)).collect(Collectors.toList());
    // Create return expression in the second if statement's body.
    Set<String> tokenSet = getTokenSet(processedTokenHierarchies);
    Iterator<String> itToken = tokenSet.iterator();
    Expr curTokenExpr = createObjectsEqualsForTokenMethodExpr(thisValueExpr, thatVariableExpr, Variable.builder().setType(TypeNode.STRING).setName(JavaStyle.toLowerCamelCase(itToken.next())).build());
    while (itToken.hasNext()) {
        Expr nextTokenExpr = createObjectsEqualsForTokenMethodExpr(thisValueExpr, thatVariableExpr, Variable.builder().setType(TypeNode.STRING).setName(JavaStyle.toLowerCamelCase(itToken.next())).build());
        curTokenExpr = LogicalOperationExpr.logicalAndWithExprs(curTokenExpr, nextTokenExpr);
    }
    ReturnExpr secondIfReturnExpr = ReturnExpr.withExpr(curTokenExpr);
    // Code: if (o == this) { return true;}
    IfStatement firstIfStatement = IfStatement.builder().setConditionExpr(oEqualsThisExpr).setBody(Arrays.asList(ExprStatement.withExpr(returnTrueExpr))).build();
    // Code: if (o != null || getClass() == o.getClass()) { FoobarName that = ((FoobarName) o);
    // return ..}
    IfStatement secondIfStatement = IfStatement.builder().setConditionExpr(orLogicalExpr).setBody(Arrays.asList(ExprStatement.withExpr(thatAssignmentExpr), ExprStatement.withExpr(secondIfReturnExpr))).build();
    // Create method's return expression.
    ValueExpr falseValueExpr = ValueExpr.withValue(PrimitiveValue.builder().setType(TypeNode.BOOLEAN).setValue("false").build());
    return MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setArguments(argVarExpr.toBuilder().setIsDecl(true).build()).setReturnType(TypeNode.BOOLEAN).setName("equals").setReturnExpr(falseValueExpr).setBody(Arrays.asList(firstIfStatement, secondIfStatement)).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) Variable(com.google.api.generator.engine.ast.Variable) LogicalOperationExpr(com.google.api.generator.engine.ast.LogicalOperationExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) IfStatement(com.google.api.generator.engine.ast.IfStatement) RelationalOperationExpr(com.google.api.generator.engine.ast.RelationalOperationExpr) 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) CastExpr(com.google.api.generator.engine.ast.CastExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) ReturnExpr(com.google.api.generator.engine.ast.ReturnExpr) List(java.util.List) ArrayList(java.util.ArrayList) TypeNode(com.google.api.generator.engine.ast.TypeNode)

Example 38 with VariableExpr

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

the class ResourceNameHelperClassComposer method createGetFieldValueMethod.

private static MethodDefinition createGetFieldValueMethod() {
    VariableExpr fieldNameVarExpr = VariableExpr.withVariable(Variable.builder().setName("fieldName").setType(TypeNode.STRING).build());
    MethodInvocationExpr returnExpr = MethodInvocationExpr.builder().setMethodName("getFieldValuesMap").build();
    returnExpr = MethodInvocationExpr.builder().setExprReferenceExpr(returnExpr).setMethodName("get").setArguments(fieldNameVarExpr).setReturnType(TypeNode.STRING).build();
    return MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.STRING).setName("getFieldValue").setArguments(fieldNameVarExpr.toBuilder().setIsDecl(true).build()).setReturnExpr(returnExpr).build();
}
Also used : MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr)

Example 39 with VariableExpr

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

the class GrpcServiceStubClassComposer method createRequestParamsExtractorBodyForHttpBindings.

private void createRequestParamsExtractorBodyForHttpBindings(Method method, VariableExpr requestVarExpr, List<Statement> bodyStatements, MethodInvocationExpr.Builder returnExprBuilder) {
    TypeNode paramsVarType = TypeNode.withReference(ConcreteReference.builder().setClazz(ImmutableMap.Builder.class).setGenerics(TypeNode.STRING.reference(), TypeNode.STRING.reference()).build());
    VariableExpr paramsVarExpr = VariableExpr.withVariable(Variable.builder().setName("params").setType(paramsVarType).build());
    Expr paramsAssignExpr = AssignmentExpr.builder().setVariableExpr(paramsVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("ImmutableMap")).setMethodName("builder").setReturnType(paramsVarType).build()).build();
    bodyStatements.add(ExprStatement.withExpr(paramsAssignExpr));
    for (HttpBinding httpBindingFieldBinding : method.httpBindings().pathParameters()) {
        MethodInvocationExpr requestBuilderExpr = createRequestFieldGetterExpr(requestVarExpr, httpBindingFieldBinding.name());
        Expr valueOfExpr = MethodInvocationExpr.builder().setStaticReferenceType(TypeNode.STRING).setMethodName("valueOf").setArguments(requestBuilderExpr).build();
        Expr paramsPutExpr = MethodInvocationExpr.builder().setExprReferenceExpr(paramsVarExpr).setMethodName("put").setArguments(ValueExpr.withValue(StringObjectValue.withValue(httpBindingFieldBinding.name())), valueOfExpr).build();
        bodyStatements.add(ExprStatement.withExpr(paramsPutExpr));
    }
    returnExprBuilder.setExprReferenceExpr(paramsVarExpr).setMethodName("build");
}
Also used : EnumRefExpr(com.google.api.generator.engine.ast.EnumRefExpr) LogicalOperationExpr(com.google.api.generator.engine.ast.LogicalOperationExpr) Expr(com.google.api.generator.engine.ast.Expr) RelationalOperationExpr(com.google.api.generator.engine.ast.RelationalOperationExpr) 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) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) RequestParamsBuilder(com.google.api.gax.rpc.RequestParamsBuilder) HttpBinding(com.google.api.generator.gapic.model.HttpBindings.HttpBinding) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode)

Example 40 with VariableExpr

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

the class GrpcServiceStubClassComposer method createMethodDescriptorVariableDecl.

@Override
protected Statement createMethodDescriptorVariableDecl(Service service, Method protoMethod, VariableExpr methodDescriptorVarExpr, Map<String, Message> messageTypes) {
    MethodInvocationExpr methodDescriptorMaker = MethodInvocationExpr.builder().setMethodName("newBuilder").setStaticReferenceType(FIXED_GRPC_TYPE_STORE.get("MethodDescriptor")).setGenerics(methodDescriptorVarExpr.variable().type().reference().generics()).build();
    BiFunction<String, Expr, Function<MethodInvocationExpr, MethodInvocationExpr>> methodMakerFn = (mName, argExpr) -> m -> MethodInvocationExpr.builder().setMethodName(mName).setArguments(Arrays.asList(argExpr)).setExprReferenceExpr(m).build();
    methodDescriptorMaker = methodMakerFn.apply("setType", getMethodDescriptorMethodTypeExpr(protoMethod)).apply(methodDescriptorMaker);
    String codeMethodNameArg = getProtoRpcFullMethodName(service, protoMethod);
    methodDescriptorMaker = methodMakerFn.apply("setFullMethodName", ValueExpr.withValue(StringObjectValue.withValue(codeMethodNameArg))).apply(methodDescriptorMaker);
    Function<MethodInvocationExpr, MethodInvocationExpr> protoUtilsMarshallerFn = m -> MethodInvocationExpr.builder().setStaticReferenceType(FIXED_GRPC_TYPE_STORE.get("ProtoUtils")).setMethodName("marshaller").setArguments(Arrays.asList(m)).build();
    MethodInvocationExpr methodInvocationArg = MethodInvocationExpr.builder().setMethodName("getDefaultInstance").setStaticReferenceType(protoMethod.inputType()).build();
    methodDescriptorMaker = methodMakerFn.apply("setRequestMarshaller", protoUtilsMarshallerFn.apply(methodInvocationArg)).apply(methodDescriptorMaker);
    methodInvocationArg = MethodInvocationExpr.builder().setMethodName("getDefaultInstance").setStaticReferenceType(protoMethod.outputType()).build();
    methodDescriptorMaker = methodMakerFn.apply("setResponseMarshaller", protoUtilsMarshallerFn.apply(methodInvocationArg)).apply(methodDescriptorMaker);
    methodDescriptorMaker = MethodInvocationExpr.builder().setMethodName("build").setExprReferenceExpr(methodDescriptorMaker).setReturnType(methodDescriptorVarExpr.type()).build();
    return ExprStatement.withExpr(AssignmentExpr.builder().setVariableExpr(methodDescriptorVarExpr.toBuilder().setIsDecl(true).setScope(ScopeNode.PRIVATE).setIsStatic(true).setIsFinal(true).build()).setValueExpr(methodDescriptorMaker).build());
}
Also used : AbstractTransportServiceStubClassComposer(com.google.api.generator.gapic.composer.common.AbstractTransportServiceStubClassComposer) IfStatement(com.google.api.generator.engine.ast.IfStatement) Arrays(java.util.Arrays) TypeNode(com.google.api.generator.engine.ast.TypeNode) EnumRefExpr(com.google.api.generator.engine.ast.EnumRefExpr) GrpcStubCallableFactory(com.google.api.gax.grpc.GrpcStubCallableFactory) BiFunction(java.util.function.BiFunction) PathTemplate(com.google.api.pathtemplate.PathTemplate) Variable(com.google.api.generator.engine.ast.Variable) Function(java.util.function.Function) StringObjectValue(com.google.api.generator.engine.ast.StringObjectValue) ArrayList(java.util.ArrayList) HttpBinding(com.google.api.generator.gapic.model.HttpBindings.HttpBinding) HashSet(java.util.HashSet) LogicalOperationExpr(com.google.api.generator.engine.ast.LogicalOperationExpr) Expr(com.google.api.generator.engine.ast.Expr) TypeStore(com.google.api.generator.gapic.composer.store.TypeStore) Method(com.google.api.generator.gapic.model.Method) ImmutableList(com.google.common.collect.ImmutableList) RelationalOperationExpr(com.google.api.generator.engine.ast.RelationalOperationExpr) Map(java.util.Map) MethodDescriptor(io.grpc.MethodDescriptor) Splitter(com.google.common.base.Splitter) ProtoUtils(io.grpc.protobuf.ProtoUtils) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) ImmutableMap(com.google.common.collect.ImmutableMap) Set(java.util.Set) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) ScopeNode(com.google.api.generator.engine.ast.ScopeNode) RequestParamsBuilder(com.google.api.gax.rpc.RequestParamsBuilder) Statement(com.google.api.generator.engine.ast.Statement) Service(com.google.api.generator.gapic.model.Service) List(java.util.List) GrpcCallSettings(com.google.api.gax.grpc.GrpcCallSettings) LambdaExpr(com.google.api.generator.engine.ast.LambdaExpr) JavaStyle(com.google.api.generator.gapic.utils.JavaStyle) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) GrpcOperationsStub(com.google.longrunning.stub.GrpcOperationsStub) Message(com.google.api.generator.gapic.model.Message) RoutingHeaderParam(com.google.api.generator.gapic.model.RoutingHeaderRule.RoutingHeaderParam) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) BiFunction(java.util.function.BiFunction) Function(java.util.function.Function) EnumRefExpr(com.google.api.generator.engine.ast.EnumRefExpr) LogicalOperationExpr(com.google.api.generator.engine.ast.LogicalOperationExpr) Expr(com.google.api.generator.engine.ast.Expr) RelationalOperationExpr(com.google.api.generator.engine.ast.RelationalOperationExpr) 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) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr)

Aggregations

VariableExpr (com.google.api.generator.engine.ast.VariableExpr)217 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)137 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)132 TypeNode (com.google.api.generator.engine.ast.TypeNode)118 Expr (com.google.api.generator.engine.ast.Expr)114 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)107 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)91 Variable (com.google.api.generator.engine.ast.Variable)88 NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)87 ArrayList (java.util.ArrayList)87 Test (org.junit.Test)82 Statement (com.google.api.generator.engine.ast.Statement)80 List (java.util.List)64 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)62 MethodDefinition (com.google.api.generator.engine.ast.MethodDefinition)58 RelationalOperationExpr (com.google.api.generator.engine.ast.RelationalOperationExpr)56 CastExpr (com.google.api.generator.engine.ast.CastExpr)53 CommentStatement (com.google.api.generator.engine.ast.CommentStatement)53 Arrays (java.util.Arrays)48 IfStatement (com.google.api.generator.engine.ast.IfStatement)47