Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ggj][codegen][engx] fix: use Objects.isNull instead of null equality #297

Merged
merged 1 commit into from
Sep 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.google.api.generator.engine.ast.ThisObjectValue;
import com.google.api.generator.engine.ast.ThrowExpr;
import com.google.api.generator.engine.ast.TypeNode;
import com.google.api.generator.engine.ast.UnaryOperationExpr;
import com.google.api.generator.engine.ast.ValueExpr;
import com.google.api.generator.engine.ast.VaporReference;
import com.google.api.generator.engine.ast.Variable;
Expand Down Expand Up @@ -833,9 +834,8 @@ private static MethodDefinition createToStringListMethod(TypeNode thisClassType)
Expr isNullCheck =
MethodInvocationExpr.builder()
.setStaticReferenceType(STATIC_TYPES.get("Objects"))
.setMethodName("equals")
.setArguments(
Arrays.asList(valueVarExpr, ValueExpr.withValue(NullObjectValue.create())))
.setMethodName("isNull")
.setArguments(valueVarExpr)
.setReturnType(TypeNode.BOOLEAN)
.build();
Statement listAddEmptyStringStatement =
Expand Down Expand Up @@ -966,7 +966,6 @@ private static MethodDefinition createGetFieldValuesMapMethod(

// Innermost if-blocks.
List<Statement> tokenIfStatements = new ArrayList<>();
ValueExpr nullValExpr = ValueExpr.withValue(NullObjectValue.create());
for (String token : getTokenSet(tokenHierarchies)) {
VariableExpr tokenVarExpr = patternTokenVarExprs.get(token);
Preconditions.checkNotNull(
Expand All @@ -979,14 +978,14 @@ private static MethodDefinition createGetFieldValuesMapMethod(
.setMethodName("put")
.setArguments(ValueExpr.withValue(tokenStrVal), tokenVarExpr)
.build();
// TODO(miraleung): Use neq operator here.
MethodInvocationExpr notNullCheckExpr =
MethodInvocationExpr.builder()
.setStaticReferenceType(STATIC_TYPES.get("Objects"))
.setMethodName("notTodoEquals")
.setArguments(tokenVarExpr, nullValExpr)
.setReturnType(TypeNode.BOOLEAN)
.build();
Expr notNullCheckExpr =
UnaryOperationExpr.logicalNotWithExpr(
MethodInvocationExpr.builder()
.setStaticReferenceType(STATIC_TYPES.get("Objects"))
.setMethodName("isNull")
.setArguments(tokenVarExpr)
.setReturnType(TypeNode.BOOLEAN)
.build());
tokenIfStatements.add(
IfStatement.builder()
.setConditionExpr(notNullCheckExpr)
Expand Down Expand Up @@ -1017,8 +1016,8 @@ private static MethodDefinition createGetFieldValuesMapMethod(
MethodInvocationExpr fieldValuesMapNullCheckExpr =
MethodInvocationExpr.builder()
.setStaticReferenceType(STATIC_TYPES.get("Objects"))
.setMethodName("equals")
.setArguments(fieldValuesMapVarExpr, nullValExpr)
.setMethodName("isNull")
.setArguments(fieldValuesMapVarExpr)
.setReturnType(TypeNode.BOOLEAN)
.build();
IfStatement fieldValuesMapIfStatement =
Expand Down Expand Up @@ -1101,15 +1100,15 @@ private static MethodDefinition createToStringMethod(
}

VariableExpr fixedValueVarExpr = FIXED_CLASS_VARS.get("fixedValue");
// TODO(miraleung): Use neq operator, then swap the ternary exprs and do the following:
// Code: return fixedValue != null ? fixedValue : pathTemplate.instantiate(getFieldValuesMap())
MethodInvocationExpr fixedValueNullCheck =
MethodInvocationExpr.builder()
.setStaticReferenceType(STATIC_TYPES.get("Objects"))
.setMethodName("equals")
.setArguments(fixedValueVarExpr, ValueExpr.withValue(NullObjectValue.create()))
.setReturnType(TypeNode.BOOLEAN)
.build();
Expr fixedValueNullCheck =
UnaryOperationExpr.logicalNotWithExpr(
MethodInvocationExpr.builder()
.setStaticReferenceType(STATIC_TYPES.get("Objects"))
.setMethodName("isNull")
.setArguments(fixedValueVarExpr)
.setReturnType(TypeNode.BOOLEAN)
.build());

MethodInvocationExpr instantiateExpr =
MethodInvocationExpr.builder()
Expand All @@ -1122,9 +1121,8 @@ private static MethodDefinition createToStringMethod(
TernaryExpr returnExpr =
TernaryExpr.builder()
.setConditionExpr(fixedValueNullCheck)
// TODO(miraleung): Swap these when using the neq operator.
.setThenExpr(instantiateExpr)
.setElseExpr(fixedValueVarExpr)
.setElseExpr(instantiateExpr)
.setThenExpr(fixedValueVarExpr)
.build();

return MethodDefinition.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -524,8 +524,8 @@ private static Expr createPagedListDescriptorAssignExpr(
MethodInvocationExpr.builder()
.setStaticReferenceType(
TypeNode.withReference(ConcreteReference.withClazz(Objects.class)))
.setMethodName("equals")
.setArguments(getResponsesListExpr, ValueExpr.withValue(NullObjectValue.create()))
.setMethodName("isNull")
.setArguments(getResponsesListExpr)
.setReturnType(TypeNode.BOOLEAN)
.build();
Expr thenExpr =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ public void generateResourceNameClass_testingSessionOnePattern() {
+ " public static List<String> toStringList(List<FoobarName> values) {\n"
+ " List<String> list = new ArrayList<>(values.size());\n"
+ " for (FoobarName value : values) {\n"
+ " if (Objects.equals(value, null)) {\n"
+ " if (Objects.isNull(value)) {\n"
+ " list.add(\"\");\n"
+ " } else {\n"
+ " list.add(value.toString());\n"
Expand All @@ -402,18 +402,18 @@ public void generateResourceNameClass_testingSessionOnePattern() {
+ "\n"
+ " @Override\n"
+ " public Map<String, String> getFieldValuesMap() {\n"
+ " if (Objects.equals(fieldValuesMap, null)) {\n"
+ " if (Objects.isNull(fieldValuesMap)) {\n"
+ " synchronized (this) {\n"
+ " if (Objects.equals(fieldValuesMap, null)) {\n"
+ " if (Objects.isNull(fieldValuesMap)) {\n"
+ " ImmutableMap.Builder<String, String> fieldMapBuilder ="
+ " ImmutableMap.builder();\n"
+ " if (Objects.notTodoEquals(project, null)) {\n"
+ " if (!Objects.isNull(project)) {\n"
+ " fieldMapBuilder.put(\"project\", project);\n"
+ " }\n"
+ " if (Objects.notTodoEquals(foobar, null)) {\n"
+ " if (!Objects.isNull(foobar)) {\n"
+ " fieldMapBuilder.put(\"foobar\", foobar);\n"
+ " }\n"
+ " if (Objects.notTodoEquals(variant, null)) {\n"
+ " if (!Objects.isNull(variant)) {\n"
+ " fieldMapBuilder.put(\"variant\", variant);\n"
+ " }\n"
+ " fieldValuesMap = fieldMapBuilder.build();\n"
Expand All @@ -429,9 +429,8 @@ public void generateResourceNameClass_testingSessionOnePattern() {
+ "\n"
+ " @Override\n"
+ " public String toString() {\n"
+ " return Objects.equals(fixedValue, null)\n"
+ " ? pathTemplate.instantiate(getFieldValuesMap())\n"
+ " : fixedValue;\n"
+ " return !Objects.isNull(fixedValue) ? fixedValue :"
+ " pathTemplate.instantiate(getFieldValuesMap());\n"
+ " }\n"
+ "\n"
+ " /** Builder for projects/{project}/foobars/{foobar}. */\n"
Expand Down Expand Up @@ -606,7 +605,7 @@ public void generateResourceNameClass_testingSessionOnePattern() {
+ " public static List<String> toStringList(List<SessionName> values) {\n"
+ " List<String> list = new ArrayList<>(values.size());\n"
+ " for (SessionName value : values) {\n"
+ " if (Objects.equals(value, null)) {\n"
+ " if (Objects.isNull(value)) {\n"
+ " list.add(\"\");\n"
+ " } else {\n"
+ " list.add(value.toString());\n"
Expand All @@ -621,12 +620,12 @@ public void generateResourceNameClass_testingSessionOnePattern() {
+ "\n"
+ " @Override\n"
+ " public Map<String, String> getFieldValuesMap() {\n"
+ " if (Objects.equals(fieldValuesMap, null)) {\n"
+ " if (Objects.isNull(fieldValuesMap)) {\n"
+ " synchronized (this) {\n"
+ " if (Objects.equals(fieldValuesMap, null)) {\n"
+ " if (Objects.isNull(fieldValuesMap)) {\n"
+ " ImmutableMap.Builder<String, String> fieldMapBuilder ="
+ " ImmutableMap.builder();\n"
+ " if (Objects.notTodoEquals(session, null)) {\n"
+ " if (!Objects.isNull(session)) {\n"
+ " fieldMapBuilder.put(\"session\", session);\n"
+ " }\n"
+ " fieldValuesMap = fieldMapBuilder.build();\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ private static List<Service> parseServices(
+ " @Override\n"
+ " public Iterable<EchoResponse> extractResources(PagedExpandResponse"
+ " payload) {\n"
+ " return Objects.equals(payload.getResponsesList(), null)\n"
+ " return Objects.isNull(payload.getResponsesList())\n"
+ " ? ImmutableList.<EchoResponse>of()\n"
+ " : payload.getResponsesList();\n"
+ " }\n"
Expand Down Expand Up @@ -877,7 +877,7 @@ private static List<Service> parseServices(
+ " @Override\n"
+ " public Iterable<LogEntry> extractResources(ListLogEntriesResponse"
+ " payload) {\n"
+ " return Objects.equals(payload.getResponsesList(), null)\n"
+ " return Objects.isNull(payload.getResponsesList())\n"
+ " ? ImmutableList.<LogEntry>of()\n"
+ " : payload.getResponsesList();\n"
+ " }\n"
Expand Down Expand Up @@ -927,7 +927,7 @@ private static List<Service> parseServices(
+ " @Override\n"
+ " public Iterable<MonitoredResourceDescriptor> extractResources(\n"
+ " ListMonitoredResourceDescriptorsResponse payload) {\n"
+ " return Objects.equals(payload.getResponsesList(), null)\n"
+ " return Objects.isNull(payload.getResponsesList())\n"
+ " ? ImmutableList.<MonitoredResourceDescriptor>of()\n"
+ " : payload.getResponsesList();\n"
+ " }\n"
Expand Down Expand Up @@ -967,7 +967,7 @@ private static List<Service> parseServices(
+ "\n"
+ " @Override\n"
+ " public Iterable<String> extractResources(ListLogsResponse payload) {\n"
+ " return Objects.equals(payload.getResponsesList(), null)\n"
+ " return Objects.isNull(payload.getResponsesList())\n"
+ " ? ImmutableList.<String>of()\n"
+ " : payload.getResponsesList();\n"
+ " }\n"
Expand Down Expand Up @@ -1616,7 +1616,7 @@ private static List<Service> parseServices(
+ "\n"
+ " @Override\n"
+ " public Iterable<Topic> extractResources(ListTopicsResponse payload) {\n"
+ " return Objects.equals(payload.getResponsesList(), null)\n"
+ " return Objects.isNull(payload.getResponsesList())\n"
+ " ? ImmutableList.<Topic>of()\n"
+ " : payload.getResponsesList();\n"
+ " }\n"
Expand Down Expand Up @@ -1661,7 +1661,7 @@ private static List<Service> parseServices(
+ " @Override\n"
+ " public Iterable<String> extractResources(ListTopicSubscriptionsResponse"
+ " payload) {\n"
+ " return Objects.equals(payload.getResponsesList(), null)\n"
+ " return Objects.isNull(payload.getResponsesList())\n"
+ " ? ImmutableList.<String>of()\n"
+ " : payload.getResponsesList();\n"
+ " }\n"
Expand Down Expand Up @@ -1703,7 +1703,7 @@ private static List<Service> parseServices(
+ " @Override\n"
+ " public Iterable<String> extractResources(ListTopicSnapshotsResponse"
+ " payload) {\n"
+ " return Objects.equals(payload.getResponsesList(), null)\n"
+ " return Objects.isNull(payload.getResponsesList())\n"
+ " ? ImmutableList.<String>of()\n"
+ " : payload.getResponsesList();\n"
+ " }\n"
Expand Down