use of com.google.api.generator.engine.ast.RelationalOperationExpr 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.RelationalOperationExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeRelationalOperationExpr_lessThan.
@Test
public void writeRelationalOperationExpr_lessThan() {
VariableExpr lhsExpr = VariableExpr.withVariable(createVariable("i", TypeNode.INT));
MethodInvocationExpr rhsExpr = MethodInvocationExpr.builder().setMethodName("getMaxNumber").setReturnType(TypeNode.INT).build();
RelationalOperationExpr lessThanWithExprs = RelationalOperationExpr.lessThanWithExprs(lhsExpr, rhsExpr);
lessThanWithExprs.accept(writerVisitor);
assertThat(writerVisitor.write()).isEqualTo("i < getMaxNumber()");
}
use of com.google.api.generator.engine.ast.RelationalOperationExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeRelationOperationExpr_notEqualTo.
@Test
public void writeRelationOperationExpr_notEqualTo() {
TypeNode someType = TypeNode.withReference(VaporReference.builder().setName("SomeClass").setPakkage("com.google.api.generator.engine").build());
MethodInvocationExpr lhsExpr = MethodInvocationExpr.builder().setMethodName("getName").setStaticReferenceType(someType).setReturnType(TypeNode.STRING).build();
ValueExpr rhsExpr = ValueExpr.createNullExpr();
RelationalOperationExpr notEqualToOperationExpr = RelationalOperationExpr.notEqualToWithExprs(lhsExpr, rhsExpr);
notEqualToOperationExpr.accept(writerVisitor);
assertThat(writerVisitor.write()).isEqualTo("SomeClass.getName() != null");
}
use of com.google.api.generator.engine.ast.RelationalOperationExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeRelationalOperationExpr_equalTo.
@Test
public void writeRelationalOperationExpr_equalTo() {
VariableExpr variableExprLHS = VariableExpr.withVariable(Variable.builder().setType(TypeNode.BOOLEAN_OBJECT).setName("isGood").build());
MethodInvocationExpr methodInvocationExpr = MethodInvocationExpr.builder().setMethodName("isBad").setReturnType(TypeNode.BOOLEAN).build();
RelationalOperationExpr equalToOperationExpr = RelationalOperationExpr.equalToWithExprs(variableExprLHS, methodInvocationExpr);
equalToOperationExpr.accept(writerVisitor);
assertThat(writerVisitor.write()).isEqualTo("isGood == isBad()");
}
use of com.google.api.generator.engine.ast.RelationalOperationExpr in project gapic-generator-java by googleapis.
the class GrpcServiceStubClassComposer method fieldValuesNotNullConditionExpr.
private Expr fieldValuesNotNullConditionExpr(VariableExpr requestVarExpr, List<String> fieldNames) {
MethodInvocationExpr.Builder requestFieldGetterExprBuilder = MethodInvocationExpr.builder().setExprReferenceExpr(requestVarExpr);
Expr fieldValuesNotNullExpr = null;
for (int i = 0; i < fieldNames.size() - 1; i++) {
String currFieldName = fieldNames.get(i);
String bindingFieldMethodName = String.format("get%s", JavaStyle.toUpperCamelCase(currFieldName));
requestFieldGetterExprBuilder = requestFieldGetterExprBuilder.setMethodName(bindingFieldMethodName);
// set return type of each method invocation to String just to pass the validation for
// RelationalOperationExpr that both side of relational operation needs to be a valid equality
// type
MethodInvocationExpr requestGetterExpr = requestFieldGetterExprBuilder.setReturnType(TypeNode.STRING).build();
Expr currentValueNotNullExpr = RelationalOperationExpr.notEqualToWithExprs(requestGetterExpr, ValueExpr.createNullExpr());
if (fieldValuesNotNullExpr == null) {
fieldValuesNotNullExpr = currentValueNotNullExpr;
} else {
fieldValuesNotNullExpr = LogicalOperationExpr.logicalAndWithExprs(fieldValuesNotNullExpr, currentValueNotNullExpr);
}
requestFieldGetterExprBuilder = MethodInvocationExpr.builder().setExprReferenceExpr(requestGetterExpr);
}
return fieldValuesNotNullExpr;
}
Aggregations