Skip to content

Commit

Permalink
[ggj][ast] feat: add ValueExpr.createNullExpr() convenience wrapper (#…
Browse files Browse the repository at this point in the history
…584)

* fix: fix dep ordering in Bazel dedupe rules

* fix: replace Objects.isNull() with equality check for Java 7 compat

* feat: add ValueExpr.createNullExpr() wrapper

* fix: formatting
  • Loading branch information
miraleung authored Dec 3, 2020
1 parent 2db0bc0 commit 747fddc
Show file tree
Hide file tree
Showing 15 changed files with 54 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public static ValueExpr withValue(Value value) {
return builder().setValue(value).build();
}

public static ValueExpr createNullExpr() {
return withValue(NullObjectValue.create());
}

public static Builder builder() {
return new AutoValue_ValueExpr.Builder();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import com.google.api.generator.engine.ast.MethodDefinition;
import com.google.api.generator.engine.ast.MethodInvocationExpr;
import com.google.api.generator.engine.ast.NewObjectExpr;
import com.google.api.generator.engine.ast.NullObjectValue;
import com.google.api.generator.engine.ast.PrimitiveValue;
import com.google.api.generator.engine.ast.Reference;
import com.google.api.generator.engine.ast.RelationalOperationExpr;
Expand Down Expand Up @@ -205,7 +204,7 @@ private static MethodDefinition createGetRequestBuilderMethod(
IfStatement.builder()
.setConditionExpr(
RelationalOperationExpr.equalToWithExprs(
builderVarExpr, ValueExpr.withValue(NullObjectValue.create())))
builderVarExpr, ValueExpr.createNullExpr()))
.setBody(Arrays.asList(ExprStatement.withExpr(toBuilderExpr)))
.setElseBody(Arrays.asList(ExprStatement.withExpr(addAllExpr)))
.build()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import com.google.api.generator.engine.ast.MethodDefinition;
import com.google.api.generator.engine.ast.MethodInvocationExpr;
import com.google.api.generator.engine.ast.NewObjectExpr;
import com.google.api.generator.engine.ast.NullObjectValue;
import com.google.api.generator.engine.ast.PrimitiveValue;
import com.google.api.generator.engine.ast.Reference;
import com.google.api.generator.engine.ast.RelationalOperationExpr;
Expand Down Expand Up @@ -288,7 +287,7 @@ private static List<MethodDefinition> createConstructorMethods(
boolean hasVariants = tokenHierarchies.size() > 1;

List<MethodDefinition> javaMethods = new ArrayList<>();
final ValueExpr nullExpr = ValueExpr.withValue(NullObjectValue.create());
final ValueExpr nullExpr = ValueExpr.createNullExpr();
Function<String, AssignmentExpr> assignTokenToNullExpr =
t ->
AssignmentExpr.builder()
Expand Down Expand Up @@ -622,8 +621,7 @@ private static MethodDefinition createParseMethod(
.build())
.setBody(
Arrays.asList(
ExprStatement.withExpr(
ReturnExpr.withExpr(ValueExpr.withValue(NullObjectValue.create())))))
ExprStatement.withExpr(ReturnExpr.withExpr(ValueExpr.createNullExpr()))))
.build());

List<Expr> formattedStringArgList = Arrays.asList(formattedStringArgExpr);
Expand Down Expand Up @@ -864,8 +862,7 @@ private static MethodDefinition createToStringListMethod(TypeNode thisClassType)
Variable.builder().setName("value").setType(thisClassType).build());
// We use an equality check instead of Objects.isNull() for Java 7 compatibility.
Expr isNullCheck =
RelationalOperationExpr.equalToWithExprs(
valueVarExpr, ValueExpr.withValue(NullObjectValue.create()));
RelationalOperationExpr.equalToWithExprs(valueVarExpr, ValueExpr.createNullExpr());
Statement listAddEmptyStringStatement =
ExprStatement.withExpr(
MethodInvocationExpr.builder()
Expand Down Expand Up @@ -1002,8 +999,7 @@ private static MethodDefinition createGetFieldValuesMapMethod(
.setArguments(ValueExpr.withValue(tokenStrVal), tokenVarExpr)
.build();
Expr notNullCheckExpr =
RelationalOperationExpr.notEqualToWithExprs(
tokenVarExpr, ValueExpr.withValue(NullObjectValue.create()));
RelationalOperationExpr.notEqualToWithExprs(tokenVarExpr, ValueExpr.createNullExpr());
tokenIfStatements.add(
IfStatement.builder()
.setConditionExpr(notNullCheckExpr)
Expand Down Expand Up @@ -1031,8 +1027,7 @@ private static MethodDefinition createGetFieldValuesMapMethod(

// Middle if-block, i.e. `if (fieldValuesMap == null)`.
Expr fieldValuesMapNullCheckExpr =
RelationalOperationExpr.equalToWithExprs(
fieldValuesMapVarExpr, ValueExpr.withValue(NullObjectValue.create()));
RelationalOperationExpr.equalToWithExprs(fieldValuesMapVarExpr, ValueExpr.createNullExpr());
IfStatement fieldValuesMapIfStatement =
IfStatement.builder()
.setConditionExpr(fieldValuesMapNullCheckExpr)
Expand Down Expand Up @@ -1125,8 +1120,7 @@ private static MethodDefinition createToStringMethod(
VariableExpr fixedValueVarExpr = FIXED_CLASS_VARS.get("fixedValue");
// Code: return fixedValue != null ? fixedValue : pathTemplate.instantiate(getFieldValuesMap())
Expr fixedValueNullCheck =
RelationalOperationExpr.notEqualToWithExprs(
fixedValueVarExpr, ValueExpr.withValue(NullObjectValue.create()));
RelationalOperationExpr.notEqualToWithExprs(fixedValueVarExpr, ValueExpr.createNullExpr());

MethodInvocationExpr instantiateExpr =
MethodInvocationExpr.builder()
Expand Down Expand Up @@ -1171,8 +1165,7 @@ private static MethodDefinition createEqualsMethod(
RelationalOperationExpr oEqualsThisExpr =
RelationalOperationExpr.equalToWithExprs(argVarExpr, thisValueExpr);
RelationalOperationExpr oNotEqualsNullExpr =
RelationalOperationExpr.notEqualToWithExprs(
argVarExpr, ValueExpr.withValue(NullObjectValue.create()));
RelationalOperationExpr.notEqualToWithExprs(argVarExpr, ValueExpr.createNullExpr());
MethodInvocationExpr getClassMethodInvocationExpr =
MethodInvocationExpr.builder().setMethodName("getClass").build();
RelationalOperationExpr getClassEqualsExpr =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import com.google.api.generator.engine.ast.Expr;
import com.google.api.generator.engine.ast.ExprStatement;
import com.google.api.generator.engine.ast.MethodInvocationExpr;
import com.google.api.generator.engine.ast.NullObjectValue;
import com.google.api.generator.engine.ast.PrimitiveValue;
import com.google.api.generator.engine.ast.StringObjectValue;
import com.google.api.generator.engine.ast.TypeNode;
Expand Down Expand Up @@ -108,7 +107,7 @@ public static BlockStatement createRetryParamDefinitionsBlock(
bodyExprs.add(
AssignmentExpr.builder()
.setVariableExpr(settingsVarExpr.toBuilder().setIsDecl(true).build())
.setValueExpr(ValueExpr.withValue(NullObjectValue.create()))
.setValueExpr(ValueExpr.createNullExpr())
.build());

// Build the settings object for each config.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import com.google.api.generator.engine.ast.MethodDefinition;
import com.google.api.generator.engine.ast.MethodInvocationExpr;
import com.google.api.generator.engine.ast.NewObjectExpr;
import com.google.api.generator.engine.ast.NullObjectValue;
import com.google.api.generator.engine.ast.PrimitiveValue;
import com.google.api.generator.engine.ast.Reference;
import com.google.api.generator.engine.ast.ReferenceConstructorExpr;
Expand Down Expand Up @@ -390,7 +389,7 @@ private static List<MethodDefinition> createConstructorMethods(
ctorAssignmentExprs.add(
AssignmentExpr.builder()
.setVariableExpr(settingsVarExpr.toBuilder().setExprReferenceExpr(thisExpr).build())
.setValueExpr(ValueExpr.withValue(NullObjectValue.create()))
.setValueExpr(ValueExpr.createNullExpr())
.build());
ctorAssignmentExprs.add(
AssignmentExpr.builder()
Expand Down Expand Up @@ -1125,7 +1124,7 @@ private static ClassDefinition createNestedRpcPageClass(
.build();

// createEmptyPage method.
ValueExpr nullExpr = ValueExpr.withValue(NullObjectValue.create());
ValueExpr nullExpr = ValueExpr.createNullExpr();
MethodDefinition createEmptyPageMethod =
MethodDefinition.builder()
.setScope(ScopeNode.PRIVATE)
Expand Down Expand Up @@ -1276,7 +1275,7 @@ private static ClassDefinition createNestedRpcFixedSizeCollectionClass(
NewObjectExpr.builder()
.setType(classType)
.setArguments(
ValueExpr.withValue(NullObjectValue.create()),
ValueExpr.createNullExpr(),
ValueExpr.withValue(
PrimitiveValue.builder().setType(TypeNode.INT).setValue("0").build()))
.build())
Expand Down Expand Up @@ -1348,7 +1347,7 @@ static MethodInvocationExpr buildNestedSetterInvocationExpr(
VariableExpr.withVariable(
Variable.builder().setName(argumentName).setType(argumentType).build());
if (argument.isResourceNameHelper()) {
Expr nullExpr = ValueExpr.withValue(NullObjectValue.create());
Expr nullExpr = ValueExpr.createNullExpr();
Expr isNullCheckExpr = RelationalOperationExpr.equalToWithExprs(argVarExpr, nullExpr);
MethodInvocationExpr toStringExpr =
MethodInvocationExpr.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import com.google.api.generator.engine.ast.MethodDefinition;
import com.google.api.generator.engine.ast.MethodInvocationExpr;
import com.google.api.generator.engine.ast.NewObjectExpr;
import com.google.api.generator.engine.ast.NullObjectValue;
import com.google.api.generator.engine.ast.Reference;
import com.google.api.generator.engine.ast.ReferenceConstructorExpr;
import com.google.api.generator.engine.ast.ScopeNode;
Expand Down Expand Up @@ -452,7 +451,7 @@ private static List<MethodDefinition> createNestedBuilderConstructorMethods(
.setArguments(
CastExpr.builder()
.setType(staticTypes.get("ClientContext"))
.setExpr(ValueExpr.withValue(NullObjectValue.create()))
.setExpr(ValueExpr.createNullExpr())
.build())
.build())))
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
import com.google.api.generator.engine.ast.MethodDefinition;
import com.google.api.generator.engine.ast.MethodInvocationExpr;
import com.google.api.generator.engine.ast.NewObjectExpr;
import com.google.api.generator.engine.ast.NullObjectValue;
import com.google.api.generator.engine.ast.Reference;
import com.google.api.generator.engine.ast.ReferenceConstructorExpr;
import com.google.api.generator.engine.ast.RelationalOperationExpr;
Expand Down Expand Up @@ -546,8 +545,7 @@ private static Expr createPagedListDescriptorAssignExpr(
.setReturnType(returnType)
.build();
Expr conditionExpr =
RelationalOperationExpr.equalToWithExprs(
getResponsesListExpr, ValueExpr.withValue(NullObjectValue.create()));
RelationalOperationExpr.equalToWithExprs(getResponsesListExpr, ValueExpr.createNullExpr());
Expr thenExpr =
MethodInvocationExpr.builder()
.setStaticReferenceType(
Expand Down Expand Up @@ -1345,7 +1343,7 @@ private static List<MethodDefinition> createNestedClassConstructorMethods(
.setArguments(
CastExpr.builder()
.setType(STATIC_TYPES.get("ClientContext"))
.setExpr(ValueExpr.withValue(NullObjectValue.create()))
.setExpr(ValueExpr.createNullExpr())
.build())
.build())))
.build());
Expand Down Expand Up @@ -1597,7 +1595,7 @@ private static MethodDefinition createNestedClassCreateDefaultMethod(
.setArguments(
CastExpr.builder()
.setType(STATIC_TYPES.get("ClientContext"))
.setExpr(ValueExpr.withValue(NullObjectValue.create()))
.setExpr(ValueExpr.createNullExpr())
.build())
.build())
.build()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void concatOperator_validWithNull() {
// Type-checking for String variable x and null object value.
VariableExpr lhsExpr =
VariableExpr.withVariable(Variable.builder().setType(TypeNode.STRING).setName("x").build());
ValueExpr rhsExpr = ValueExpr.withValue(NullObjectValue.create());
ValueExpr rhsExpr = ValueExpr.createNullExpr();
ArithmeticOperationExpr.concatWithExprs(lhsExpr, rhsExpr);
// No exception thrown, so we succeeded.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ public void invalidMultiplyAndAssignmentOperationExpr_floatBoxedWithObjectType()
public void invalidMultiplyAndAssignmentOperationExpr_floatBoxedWithNullType() {
// No need swap case.
VariableExpr lhsExpr = createVariableExpr(TypeNode.FLOAT_OBJECT, "x");
ValueExpr rhsExpr = ValueExpr.withValue(NullObjectValue.create());
ValueExpr rhsExpr = ValueExpr.createNullExpr();
assertThrows(
IllegalStateException.class,
() -> AssignmentOperationExpr.multiplyAssignmentWithExprs(lhsExpr, rhsExpr));
Expand Down Expand Up @@ -463,7 +463,7 @@ public void invalidMultiplyAndAssignmentOperationExpr_doubleBoxedWithReferenceTy
public void invalidMultiplyAndAssignmentOperationExpr_doubleBoxedWithNullType() {
// No need swap case.
VariableExpr lhsExpr = createVariableExpr(TypeNode.DOUBLE_OBJECT, "x");
ValueExpr valueExprExpr = ValueExpr.withValue(NullObjectValue.create());
ValueExpr valueExprExpr = ValueExpr.createNullExpr();
assertThrows(
IllegalStateException.class,
() -> AssignmentOperationExpr.multiplyAssignmentWithExprs(lhsExpr, valueExprExpr));
Expand Down Expand Up @@ -533,7 +533,7 @@ public void invalidMultiplyAndAssignmentOperationExpr_longBoxedWithDoubleType()
public void invalidMultiplyAndAssignmentOperationExpr_longBoxedWithNullType() {
// Swap test case in "invalidMultiplyAndAssignmentOperationExpr_nullWithLongBoxedType".
VariableExpr lhsExpr = createVariableExpr(TypeNode.LONG_OBJECT, "x");
ValueExpr rhsExpr = ValueExpr.withValue(NullObjectValue.create());
ValueExpr rhsExpr = ValueExpr.createNullExpr();
assertThrows(
IllegalStateException.class,
() -> AssignmentOperationExpr.multiplyAssignmentWithExprs(lhsExpr, rhsExpr));
Expand Down Expand Up @@ -715,7 +715,7 @@ public void invalidXorAssignmentOperationExpr_booleanWithReferencedTypes() {
public void invalidXorAssignmentOperationExpr_booleanWithNullTypes() {
// No valid swap case.
VariableExpr lhsExpr = createVariableExpr(TypeNode.BOOLEAN, "x");
ValueExpr rhsExpr = ValueExpr.withValue(NullObjectValue.create());
ValueExpr rhsExpr = ValueExpr.createNullExpr();
assertThrows(
IllegalStateException.class,
() -> AssignmentOperationExpr.xorAssignmentWithExprs(lhsExpr, rhsExpr));
Expand Down Expand Up @@ -894,7 +894,7 @@ public void invalidXorAssignmentOperationExpr_integerWithBooleanTypes() {
public void invalidXorAssignmentOperationExpr_integerWithNullTypes() {
// No need swap case.
VariableExpr lhsExpr = createVariableExpr(TypeNode.INT, "x");
ValueExpr rhsExpr = ValueExpr.withValue(NullObjectValue.create());
ValueExpr rhsExpr = ValueExpr.createNullExpr();
assertThrows(
IllegalStateException.class,
() -> AssignmentOperationExpr.xorAssignmentWithExprs(lhsExpr, rhsExpr));
Expand Down Expand Up @@ -1091,7 +1091,7 @@ public void invalidXorAssignmentOperationExpr_integerBoxedTypeWithBooleanTypes()
public void invalidXorAssignmentOperationExpr_integerBoxedWithNullTypes() {
// No need swap case.
VariableExpr lhsExpr = createVariableExpr(TypeNode.INT_OBJECT, "x");
ValueExpr rhsExpr = ValueExpr.withValue(NullObjectValue.create());
ValueExpr rhsExpr = ValueExpr.createNullExpr();
assertThrows(
IllegalStateException.class,
() -> AssignmentOperationExpr.xorAssignmentWithExprs(lhsExpr, rhsExpr));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void validCastExpr_basicNull() {
VariableExpr variableExpr = VariableExpr.builder().setVariable(variable).build();
CastExpr.builder()
.setType(TypeNode.withReference(ConcreteReference.withClazz(Object.class)))
.setExpr(ValueExpr.withValue(NullObjectValue.create()))
.setExpr(ValueExpr.createNullExpr())
.build();
// No exception thrown, so we succeeded.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void logicalOr_invalidNullType() {
VariableExpr lhsExpr =
VariableExpr.withVariable(
Variable.builder().setType(TypeNode.BOOLEAN).setName("x").build());
ValueExpr rhsExpr = ValueExpr.withValue(NullObjectValue.create());
ValueExpr rhsExpr = ValueExpr.createNullExpr();
assertThrows(
IllegalStateException.class,
() -> LogicalOperationExpr.logicalOrWithExprs(lhsExpr, rhsExpr));
Expand Down
Loading

0 comments on commit 747fddc

Please sign in to comment.