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();
}
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();
}
Aggregations