Search in sources :

Example 1 with StringObjectValue

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

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

the class DefaultValueComposer method createAnonymousResourceNameClassValue.

@VisibleForTesting
static AnonymousClassExpr createAnonymousResourceNameClassValue(String fieldOrMessageName) {
    TypeNode stringMapType = TypeNode.withReference(ConcreteReference.builder().setClazz(Map.class).setGenerics(Arrays.asList(ConcreteReference.withClazz(String.class), ConcreteReference.withClazz(String.class))).build());
    // Method code:
    // @Override
    // public Map<String, String> getFieldValuesMap() {
    // Map<String, String> fieldValuesMap = new HashMap<>();
    // fieldValuesMap.put("resource", "resource-12345");
    // return fieldValuesMap;
    // }
    VariableExpr fieldValuesMapVarExpr = VariableExpr.withVariable(Variable.builder().setType(stringMapType).setName("fieldValuesMap").build());
    StringObjectValue fieldOrMessageStringValue = StringObjectValue.withValue(String.format("%s%s", fieldOrMessageName, fieldOrMessageName.hashCode()));
    List<Expr> bodyExprs = Arrays.asList(AssignmentExpr.builder().setVariableExpr(fieldValuesMapVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(NewObjectExpr.builder().setType(TypeNode.withReference(ConcreteReference.withClazz(HashMap.class))).setIsGeneric(true).build()).build(), MethodInvocationExpr.builder().setExprReferenceExpr(fieldValuesMapVarExpr).setMethodName("put").setArguments(ValueExpr.withValue(StringObjectValue.withValue(fieldOrMessageName)), ValueExpr.withValue(fieldOrMessageStringValue)).build());
    MethodDefinition getFieldValuesMapMethod = MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setReturnType(stringMapType).setName("getFieldValuesMap").setBody(bodyExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList())).setReturnExpr(fieldValuesMapVarExpr).build();
    // Method code:
    // @Override
    // public String getFieldValue(String fieldName) {
    // return getFieldValuesMap().get(fieldName);
    // }
    VariableExpr fieldNameVarExpr = VariableExpr.withVariable(Variable.builder().setType(TypeNode.STRING).setName("fieldName").build());
    MethodDefinition getFieldValueMethod = MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.STRING).setName("getFieldValue").setArguments(fieldNameVarExpr.toBuilder().setIsDecl(true).build()).setReturnExpr(MethodInvocationExpr.builder().setExprReferenceExpr(MethodInvocationExpr.builder().setMethodName("getFieldValuesMap").build()).setMethodName("get").setArguments(fieldNameVarExpr).setReturnType(TypeNode.STRING).build()).build();
    return AnonymousClassExpr.builder().setType(TypeNode.withReference(ConcreteReference.withClazz(com.google.api.resourcenames.ResourceName.class))).setMethods(Arrays.asList(getFieldValuesMapMethod, getFieldValueMethod)).build();
}
Also used : StringObjectValue(com.google.api.generator.engine.ast.StringObjectValue) 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) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) HashMap(java.util.HashMap) MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) HashMap(java.util.HashMap) Map(java.util.Map) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)2 Expr (com.google.api.generator.engine.ast.Expr)2 MethodDefinition (com.google.api.generator.engine.ast.MethodDefinition)2 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)2 NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)2 StringObjectValue (com.google.api.generator.engine.ast.StringObjectValue)2 TypeNode (com.google.api.generator.engine.ast.TypeNode)2 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)2 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)2 BetaApi (com.google.api.core.BetaApi)1 AnnotationNode (com.google.api.generator.engine.ast.AnnotationNode)1 AnonymousClassExpr (com.google.api.generator.engine.ast.AnonymousClassExpr)1 AssignmentOperationExpr (com.google.api.generator.engine.ast.AssignmentOperationExpr)1 CastExpr (com.google.api.generator.engine.ast.CastExpr)1 ClassDefinition (com.google.api.generator.engine.ast.ClassDefinition)1 CommentStatement (com.google.api.generator.engine.ast.CommentStatement)1 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)1 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)1 ForStatement (com.google.api.generator.engine.ast.ForStatement)1 IfStatement (com.google.api.generator.engine.ast.IfStatement)1