use of com.google.api.generator.engine.ast.MethodInvocationExpr in project gapic-generator-java by googleapis.
the class ServiceStubSettingsClassComposer method createDefaultTransportChannelProviderMethod.
@Override
public MethodDefinition createDefaultTransportChannelProviderMethod() {
TypeNode returnType = FIXED_TYPESTORE.get("TransportChannelProvider");
MethodInvocationExpr transportProviderBuilderExpr = MethodInvocationExpr.builder().setMethodName("defaultGrpcTransportProviderBuilder").build();
transportProviderBuilderExpr = MethodInvocationExpr.builder().setExprReferenceExpr(transportProviderBuilderExpr).setMethodName("build").setReturnType(returnType).build();
return MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setIsStatic(true).setReturnType(returnType).setName("defaultTransportChannelProvider").setReturnExpr(transportProviderBuilderExpr).build();
}
use of com.google.api.generator.engine.ast.MethodInvocationExpr 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.MethodInvocationExpr 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();
}
use of com.google.api.generator.engine.ast.MethodInvocationExpr 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();
}
use of com.google.api.generator.engine.ast.MethodInvocationExpr in project gapic-generator-java by googleapis.
the class DefaultValueComposer method createValue.
public static Expr createValue(Field field, boolean useExplicitInitTypeInGenerics, Map<String, ResourceName> resourceNames, Map<String, Message> messageTypes, Map<String, String> valuePatterns) {
if (field.isRepeated()) {
ConcreteReference.Builder refBuilder = ConcreteReference.builder().setClazz(field.isMap() ? HashMap.class : ArrayList.class);
if (useExplicitInitTypeInGenerics) {
if (field.isMap()) {
refBuilder = refBuilder.setGenerics(field.type().reference().generics().subList(0, 2));
} else {
refBuilder = refBuilder.setGenerics(field.type().reference().generics().get(0));
}
}
TypeNode newType = TypeNode.withReference(refBuilder.build());
return NewObjectExpr.builder().setType(newType).setIsGeneric(true).build();
}
if (field.isEnum()) {
return MethodInvocationExpr.builder().setStaticReferenceType(field.type()).setMethodName("forNumber").setArguments(ValueExpr.withValue(PrimitiveValue.builder().setType(TypeNode.INT).setValue("0").build())).setReturnType(field.type()).build();
}
if (field.isMessage()) {
String nestedFieldName = field.name();
Map<String, String> nestedValuePatterns = new HashMap<>();
for (Map.Entry<String, String> entry : valuePatterns.entrySet()) {
if (entry.getKey().startsWith(nestedFieldName + '.')) {
nestedValuePatterns.put(entry.getKey().substring(nestedFieldName.length() + 1), entry.getValue());
}
}
if (!nestedValuePatterns.isEmpty()) {
Message nestedMessage = messageTypes.get(field.type().reference().fullName());
if (nestedMessage != null) {
return createSimpleMessageBuilderValue(nestedMessage, resourceNames, messageTypes, nestedValuePatterns);
}
}
MethodInvocationExpr newBuilderExpr = MethodInvocationExpr.builder().setStaticReferenceType(field.type()).setMethodName("newBuilder").build();
return MethodInvocationExpr.builder().setExprReferenceExpr(newBuilderExpr).setMethodName("build").setReturnType(field.type()).build();
}
if (field.type().equals(TypeNode.STRING)) {
String javaFieldName = JavaStyle.toLowerCamelCase(field.name());
return ValueExpr.withValue(StringObjectValue.withValue(constructValueMatchingPattern(javaFieldName, valuePatterns.get(javaFieldName))));
}
if (TypeNode.isNumericType(field.type())) {
return ValueExpr.withValue(PrimitiveValue.builder().setType(field.type()).setValue(String.format("%s", field.name().hashCode())).build());
}
if (field.type().equals(TypeNode.BOOLEAN)) {
return ValueExpr.withValue(PrimitiveValue.builder().setType(field.type()).setValue("true").build());
}
if (field.type().equals(TypeNode.BYTESTRING)) {
return VariableExpr.builder().setStaticReferenceType(TypeNode.BYTESTRING).setVariable(Variable.builder().setName("EMPTY").setType(TypeNode.BYTESTRING).build()).build();
}
throw new UnsupportedOperationException(String.format("Default value for field %s with type %s not implemented yet.", field.name(), field.type()));
}
Aggregations