Search in sources :

Example 16 with ValueExpr

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

the class RetrySettingsComposer method createSimpleBuilderSettingsExpr.

public static Expr createSimpleBuilderSettingsExpr(Service service, GapicServiceConfig serviceConfig, Method method, VariableExpr builderVarExpr, VariableExpr retryableCodeDefsVarExpr, VariableExpr retryParamDefsVarExpr) {
    String codeName = serviceConfig.getRetryCodeName(service, method);
    String retryParamName = serviceConfig.getRetryParamsName(service, method);
    String settingsGetterMethodName = String.format("%sSettings", JavaStyle.toLowerCamelCase(method.name()));
    Expr builderSettingsExpr = MethodInvocationExpr.builder().setExprReferenceExpr(builderVarExpr).setMethodName(settingsGetterMethodName).build();
    Function<String, ValueExpr> strValExprFn = s -> ValueExpr.withValue(StringObjectValue.withValue(s));
    builderSettingsExpr = MethodInvocationExpr.builder().setExprReferenceExpr(builderSettingsExpr).setMethodName("setRetryableCodes").setArguments(MethodInvocationExpr.builder().setExprReferenceExpr(retryableCodeDefsVarExpr).setMethodName("get").setArguments(strValExprFn.apply(codeName)).build()).build();
    builderSettingsExpr = MethodInvocationExpr.builder().setExprReferenceExpr(builderSettingsExpr).setMethodName("setRetrySettings").setArguments(MethodInvocationExpr.builder().setExprReferenceExpr(retryParamDefsVarExpr).setMethodName("get").setArguments(strValExprFn.apply(retryParamName)).build()).build();
    return builderSettingsExpr;
}
Also used : BlockStatement(com.google.api.generator.engine.ast.BlockStatement) Arrays(java.util.Arrays) TypeNode(com.google.api.generator.engine.ast.TypeNode) GapicLroRetrySettings(com.google.api.generator.gapic.model.GapicLroRetrySettings) GapicServiceConfig(com.google.api.generator.gapic.model.GapicServiceConfig) ProtoOperationTransformers(com.google.api.gax.grpc.ProtoOperationTransformers) EnumRefExpr(com.google.api.generator.engine.ast.EnumRefExpr) FlowControlSettings(com.google.api.gax.batching.FlowControlSettings) FlowController(com.google.api.gax.batching.FlowController) RetryPolicy(io.grpc.serviceconfig.MethodConfig.RetryPolicy) Variable(com.google.api.generator.engine.ast.Variable) Function(java.util.function.Function) StringObjectValue(com.google.api.generator.engine.ast.StringObjectValue) Durations(com.google.protobuf.util.Durations) ArrayList(java.util.ArrayList) RetrySettings(com.google.api.gax.retrying.RetrySettings) UnaryCallSettings(com.google.api.gax.rpc.UnaryCallSettings) 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) Lists(com.google.common.collect.Lists) PrimitiveValue(com.google.api.generator.engine.ast.PrimitiveValue) Map(java.util.Map) Code(com.google.rpc.Code) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) BatchingSettings(com.google.api.gax.batching.BatchingSettings) OperationSnapshot(com.google.api.gax.longrunning.OperationSnapshot) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) GapicBatchingSettings(com.google.api.generator.gapic.model.GapicBatchingSettings) Collectors(java.util.stream.Collectors) GapicRetrySettings(com.google.api.generator.gapic.model.GapicRetrySettings) Service(com.google.api.generator.gapic.model.Service) Duration(com.google.protobuf.Duration) List(java.util.List) OperationTimedPollAlgorithm(com.google.api.gax.longrunning.OperationTimedPollAlgorithm) JavaStyle(com.google.api.generator.gapic.utils.JavaStyle) Optional(java.util.Optional) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) Preconditions(com.google.common.base.Preconditions) StatusCode(com.google.api.gax.rpc.StatusCode) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) EnumRefExpr(com.google.api.generator.engine.ast.EnumRefExpr) Expr(com.google.api.generator.engine.ast.Expr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr)

Example 17 with ValueExpr

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

the class ResourceNameHelperClassComposer method createConstructorMethods.

private static List<MethodDefinition> createConstructorMethods(ResourceName resourceName, List<VariableExpr> templateFinalVarExprs, Map<String, VariableExpr> patternTokenVarExprs, List<List<String>> tokenHierarchies, TypeStore typeStore) {
    String thisClassName = getThisClassName(resourceName);
    TypeNode thisClassType = typeStore.get(thisClassName);
    boolean hasVariants = tokenHierarchies.size() > 1;
    List<MethodDefinition> javaMethods = new ArrayList<>();
    final ValueExpr nullExpr = ValueExpr.createNullExpr();
    Function<String, AssignmentExpr> assignTokenToNullExpr = t -> AssignmentExpr.builder().setVariableExpr(patternTokenVarExprs.get(t)).setValueExpr(nullExpr).build();
    // Special-cased PubSub handling.
    List<List<String>> processedTokenHierarchies = tokenHierarchies.stream().filter(tokens -> !tokens.contains(ResourceNameConstants.DELETED_TOPIC_LITERAL)).collect(Collectors.toList());
    boolean hasDeletedTopicPattern = tokenHierarchies.size() > processedTokenHierarchies.size();
    // First deprecated constructor.
    javaMethods.add(MethodDefinition.constructorBuilder().setAnnotations(Arrays.asList(AnnotationNode.withType(TypeNode.withReference(ConcreteReference.withClazz(Deprecated.class))))).setScope(ScopeNode.PROTECTED).setReturnType(thisClassType).setBody(getTokenSet(processedTokenHierarchies).stream().map(t -> ExprStatement.withExpr(assignTokenToNullExpr.apply(t))).collect(Collectors.toList())).build());
    for (int i = 0; i < processedTokenHierarchies.size(); i++) {
        List<String> tokens = processedTokenHierarchies.get(i);
        List<Expr> bodyExprs = new ArrayList<>();
        TypeNode argType = getBuilderType(typeStore, processedTokenHierarchies, i);
        VariableExpr builderArgExpr = VariableExpr.withVariable(Variable.builder().setName("builder").setType(argType).build());
        for (String token : tokens) {
            MethodInvocationExpr checkNotNullExpr = MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("Preconditions")).setMethodName("checkNotNull").setReturnType(TypeNode.STRING).setArguments(Arrays.asList(MethodInvocationExpr.builder().setExprReferenceExpr(builderArgExpr).setMethodName(String.format("get%s", JavaStyle.toUpperCamelCase(token))).build())).build();
            bodyExprs.add(AssignmentExpr.builder().setVariableExpr(patternTokenVarExprs.get(token)).setValueExpr(checkNotNullExpr).build());
        }
        // Initialize the rest to null.
        for (String token : getTokenSet(processedTokenHierarchies)) {
            if (tokens.contains(token)) {
                continue;
            }
            bodyExprs.add(assignTokenToNullExpr.apply(token));
        }
        if (hasVariants) {
            AssignmentExpr pathTemplateAssignExpr = AssignmentExpr.builder().setVariableExpr(FIXED_CLASS_VARS.get("pathTemplate")).setValueExpr(templateFinalVarExprs.get(i)).build();
            bodyExprs.add(pathTemplateAssignExpr);
        }
        // Private constructor.
        javaMethods.add(MethodDefinition.constructorBuilder().setScope(ScopeNode.PRIVATE).setReturnType(thisClassType).setArguments(Arrays.asList(builderArgExpr.toBuilder().setIsDecl(true).build())).setBody(bodyExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList())).build());
    }
    if (hasDeletedTopicPattern) {
        Expr thisExpr = ValueExpr.withValue(ThisObjectValue.withType(thisClassType));
        // PubSub special-case handling for the _deleted-topic_ pattern - add an extra constructor.
        // private TopicName(String fixedValue) {
        // this.fixedValue = fixedValue;
        // fieldValuesMap = ImmutableMap.of("", fixedValue);
        // }
        VariableExpr fixedValueVarExpr = VariableExpr.withVariable(Variable.builder().setName("fixedValue").setType(TypeNode.STRING).build());
        List<Expr> specialCtorBodyExprs = new ArrayList<>();
        specialCtorBodyExprs.add(AssignmentExpr.builder().setVariableExpr(VariableExpr.builder().setExprReferenceExpr(thisExpr).setVariable(fixedValueVarExpr.variable()).build()).setValueExpr(fixedValueVarExpr).build());
        specialCtorBodyExprs.add(AssignmentExpr.builder().setVariableExpr(FIXED_CLASS_VARS.get("fieldValuesMap")).setValueExpr(MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("ImmutableMap")).setMethodName("of").setArguments(ValueExpr.withValue(StringObjectValue.withValue("")), fixedValueVarExpr).setReturnType(FIXED_TYPESTORE.get("ImmutableMap")).build()).build());
        specialCtorBodyExprs.addAll(getTokenSet(processedTokenHierarchies).stream().map(t -> assignTokenToNullExpr.apply(t)).collect(Collectors.toList()));
        javaMethods.add(MethodDefinition.constructorBuilder().setScope(ScopeNode.PRIVATE).setReturnType(thisClassType).setArguments(Arrays.asList(fixedValueVarExpr.toBuilder().setIsDecl(true).build())).setBody(specialCtorBodyExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList())).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) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) ArrayList(java.util.ArrayList) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) 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) MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) List(java.util.List) ArrayList(java.util.ArrayList) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode)

Example 18 with ValueExpr

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

the class ResourceNameHelperClassComposer method createNestedBuilderClass.

private static ClassDefinition createNestedBuilderClass(TypeNode outerClassType, List<String> tokens, VariableExpr templateFinalVarExpr, String resourceNamePattern, TypeStore typeStore, boolean hasVariants, boolean isDefaultClass) {
    String className = isDefaultClass ? "Builder" : getBuilderTypeName(tokens);
    // Class member declarations.
    List<VariableExpr> classMemberVarExprs = tokens.stream().map(t -> VariableExpr.withVariable(Variable.builder().setName(JavaStyle.toLowerCamelCase(t)).setType(TypeNode.STRING).build())).collect(Collectors.toList());
    List<Statement> classMemberDecls = classMemberVarExprs.stream().map(v -> ExprStatement.withExpr(v.toBuilder().setIsDecl(true).setScope(ScopeNode.PRIVATE).build())).collect(Collectors.toList());
    // Constructor.
    List<MethodDefinition> nestedClassMethods = new ArrayList<>();
    TypeNode thisClassType = typeStore.get(className);
    MethodDefinition ctor = MethodDefinition.constructorBuilder().setScope(ScopeNode.PROTECTED).setReturnType(thisClassType).build();
    nestedClassMethods.add(ctor);
    // Getters and setters.
    List<MethodDefinition> getterMethods = new ArrayList<>();
    List<MethodDefinition> setterMethods = new ArrayList<>();
    ValueExpr thisExpr = ValueExpr.withValue(ThisObjectValue.withType(thisClassType));
    for (int i = 0; i < tokens.size(); i++) {
        String token = tokens.get(i);
        String upperCamelTokenName = JavaStyle.toUpperCamelCase(token);
        VariableExpr currClassTokenVarExpr = classMemberVarExprs.get(i);
        // Getter.
        MethodDefinition getterMethod = MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.STRING).setName(String.format("get%s", upperCamelTokenName)).setReturnExpr(currClassTokenVarExpr).build();
        getterMethods.add(getterMethod);
        // Setter.
        VariableExpr tokenArgVarExpr = currClassTokenVarExpr;
        AssignmentExpr fieldAssignExpr = AssignmentExpr.builder().setVariableExpr(currClassTokenVarExpr.toBuilder().setExprReferenceExpr(thisExpr).build()).setValueExpr(tokenArgVarExpr).build();
        MethodDefinition setterMethod = MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setReturnType(thisClassType).setName(String.format("set%s", upperCamelTokenName)).setArguments(classMemberVarExprs.get(i).toBuilder().setIsDecl(true).build()).setBody(Arrays.asList(ExprStatement.withExpr(fieldAssignExpr))).setReturnExpr(thisExpr).build();
        setterMethods.add(setterMethod);
    }
    nestedClassMethods.addAll(getterMethods);
    nestedClassMethods.addAll(setterMethods);
    // Private builder constructor method.
    if (isDefaultClass) {
        VariableExpr outerClassVarExpr = VariableExpr.withVariable(Variable.builder().setName(JavaStyle.toLowerCamelCase(outerClassType.reference().name())).setType(outerClassType).build());
        List<Expr> builderCtorBodyExprs = new ArrayList<>();
        if (hasVariants) {
            // TODO(miraleung): Use eq operator instead.
            MethodInvocationExpr equalsCheckExpr = MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("Objects")).setMethodName("equals").setArguments(FIXED_CLASS_VARS.get("pathTemplate").toBuilder().setExprReferenceExpr(outerClassVarExpr).build(), templateFinalVarExpr).build();
            builderCtorBodyExprs.add(MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("Preconditions")).setMethodName("checkArgument").setArguments(equalsCheckExpr, ValueExpr.withValue(StringObjectValue.withValue(String.format("toBuilder is only supported when %s has the pattern of %s", outerClassType.reference().name(), resourceNamePattern)))).build());
        }
        for (VariableExpr memberVarExpr : classMemberVarExprs) {
            VariableExpr currClassTokenVarExpr = memberVarExpr.toBuilder().setExprReferenceExpr(thisExpr).build();
            builderCtorBodyExprs.add(AssignmentExpr.builder().setVariableExpr(currClassTokenVarExpr).setValueExpr(currClassTokenVarExpr.toBuilder().setExprReferenceExpr(outerClassVarExpr).build()).build());
        }
        MethodDefinition fromOuterTypeCtor = MethodDefinition.constructorBuilder().setScope(ScopeNode.PRIVATE).setReturnType(thisClassType).setArguments(outerClassVarExpr.toBuilder().setIsDecl(true).build()).setBody(builderCtorBodyExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList())).build();
        nestedClassMethods.add(fromOuterTypeCtor);
    }
    // Last build() method.
    MethodDefinition buildMethod = MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setReturnType(outerClassType).setName("build").setReturnExpr(NewObjectExpr.builder().setType(outerClassType).setArguments(thisExpr).build()).build();
    nestedClassMethods.add(buildMethod);
    // Return the class.
    AnnotationNode betaAnnotation = AnnotationNode.builder().setType(FIXED_TYPESTORE.get("BetaApi")).setDescription("The per-pattern Builders are not stable yet and may be changed in the future.").build();
    List<AnnotationNode> classAnnotations = isDefaultClass ? Collections.emptyList() : Arrays.asList(betaAnnotation);
    return ClassDefinition.builder().setHeaderCommentStatements(CommentStatement.withComment(JavaDocComment.withComment(String.format(BUILDER_CLASS_HEADER_PATTERN, resourceNamePattern)))).setAnnotations(classAnnotations).setIsNested(true).setScope(ScopeNode.PUBLIC).setIsStatic(true).setName(className).setStatements(classMemberDecls).setMethods(nestedClassMethods).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) 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) AnnotationNode(com.google.api.generator.engine.ast.AnnotationNode) 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 19 with ValueExpr

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

the class ResourceNameHelperClassComposer method createHashCodeMethod.

private static MethodDefinition createHashCodeMethod(List<List<String>> tokenHierarchies) {
    List<Statement> assignmentBody = new ArrayList<>();
    // code: int h = 1;
    Variable hVar = Variable.builder().setType(TypeNode.INT).setName("h").build();
    VariableExpr hVarExpr = VariableExpr.builder().setVariable(hVar).build();
    ValueExpr hValueExpr = ValueExpr.withValue(PrimitiveValue.builder().setType(TypeNode.INT).setValue("1").build());
    AssignmentExpr hAssignmentExpr = AssignmentExpr.builder().setVariableExpr(hVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(hValueExpr).build();
    assignmentBody.add(ExprStatement.withExpr(hAssignmentExpr));
    // code: h *= 1000003;
    // code: h ^= Objects.hashCode(...);
    ValueExpr numValueExpr = ValueExpr.withValue(PrimitiveValue.builder().setType(TypeNode.INT).setValue("1000003").build());
    AssignmentOperationExpr multiplyAssignmentOpExpr = AssignmentOperationExpr.multiplyAssignmentWithExprs(hVarExpr, numValueExpr);
    // PubSub special-case handling - exclude _deleted-topic_.
    List<List<String>> processedTokenHierarchies = tokenHierarchies.stream().filter(ts -> !ts.contains(ResourceNameConstants.DELETED_TOPIC_LITERAL)).collect(Collectors.toList());
    // If it has variants, add the multiply and xor assignment operation exprs for fixedValue.
    boolean hasVariants = processedTokenHierarchies.size() > 1;
    if (hasVariants) {
        VariableExpr fixedValueVarExpr = FIXED_CLASS_VARS.get("fixedValue");
        assignmentBody.add(ExprStatement.withExpr(multiplyAssignmentOpExpr));
        assignmentBody.add(ExprStatement.withExpr(AssignmentOperationExpr.xorAssignmentWithExprs(hVarExpr, createObjectsHashCodeForVarMethod(fixedValueVarExpr))));
    }
    // Add the multiply and xor assignment operation exprs for tokens.
    Set<String> tokenSet = getTokenSet(processedTokenHierarchies);
    tokenSet.stream().forEach(token -> {
        VariableExpr tokenVarExpr = VariableExpr.withVariable(Variable.builder().setName(JavaStyle.toLowerCamelCase(token)).setType(TypeNode.STRING).build());
        assignmentBody.add(ExprStatement.withExpr(multiplyAssignmentOpExpr));
        assignmentBody.add(ExprStatement.withExpr(AssignmentOperationExpr.xorAssignmentWithExprs(hVarExpr, createObjectsHashCodeForVarMethod(tokenVarExpr))));
    });
    return MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.INT).setName("hashCode").setBody(assignmentBody).setReturnExpr(hVarExpr).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) 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) AssignmentOperationExpr(com.google.api.generator.engine.ast.AssignmentOperationExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) List(java.util.List) ArrayList(java.util.ArrayList)

Example 20 with ValueExpr

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

the class GrpcServiceStubClassComposer method createPathTemplateClassStatement.

private Statement createPathTemplateClassStatement(RoutingHeaderParam routingHeaderParam, TypeNode pathTemplateType, Variable pathTemplateVar) {
    VariableExpr pathTemplateVarExpr = VariableExpr.builder().setVariable(pathTemplateVar).setIsDecl(true).setIsStatic(true).setIsFinal(true).setScope(ScopeNode.PRIVATE).build();
    ValueExpr valueExpr = ValueExpr.withValue(StringObjectValue.withValue(routingHeaderParam.pattern()));
    Expr pathTemplateExpr = AssignmentExpr.builder().setVariableExpr(pathTemplateVarExpr).setValueExpr(MethodInvocationExpr.builder().setStaticReferenceType(pathTemplateType).setMethodName("create").setArguments(valueExpr).setReturnType(pathTemplateType).build()).build();
    return ExprStatement.withExpr(pathTemplateExpr);
}
Also used : ValueExpr(com.google.api.generator.engine.ast.ValueExpr) 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) VariableExpr(com.google.api.generator.engine.ast.VariableExpr)

Aggregations

ValueExpr (com.google.api.generator.engine.ast.ValueExpr)32 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)27 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)24 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)24 Expr (com.google.api.generator.engine.ast.Expr)19 RelationalOperationExpr (com.google.api.generator.engine.ast.RelationalOperationExpr)18 Test (org.junit.Test)18 NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)17 Variable (com.google.api.generator.engine.ast.Variable)16 LogicalOperationExpr (com.google.api.generator.engine.ast.LogicalOperationExpr)15 PrimitiveValue (com.google.api.generator.engine.ast.PrimitiveValue)15 TernaryExpr (com.google.api.generator.engine.ast.TernaryExpr)15 AssignmentOperationExpr (com.google.api.generator.engine.ast.AssignmentOperationExpr)14 CastExpr (com.google.api.generator.engine.ast.CastExpr)14 ReturnExpr (com.google.api.generator.engine.ast.ReturnExpr)14 StringObjectValue (com.google.api.generator.engine.ast.StringObjectValue)14 ThisObjectValue (com.google.api.generator.engine.ast.ThisObjectValue)14 ThrowExpr (com.google.api.generator.engine.ast.ThrowExpr)14 TypeNode (com.google.api.generator.engine.ast.TypeNode)14 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)13