Skip to content

Commit 527d982

Browse files
parloughCommit Queue
authored andcommitted
[analysis_server] Clean up generated Java code
- Removes the usage of Apache StringUtils (#39377) - Remove now unnecessary guava Lists import - Use instanceof pattern matching instead of casting - Properly mark overridden toJson method as overriden - Resolve TODO in generator code Fixes #39377 Change-Id: I9d1d41af89e37042fd999d2786a2c145a907eb3e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/390629 Reviewed-by: Brian Wilkerson <[email protected]> Auto-Submit: Parker Lougheed <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent ea81fac commit 527d982

File tree

73 files changed

+676
-569
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+676
-569
lines changed

pkg/analysis_server/tool/spec/codegen_java_types.dart

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,11 @@ class CodegenJavaType extends CodegenJavaVisitor {
148148
return 'getAsInt';
149149
} else if (name == 'long' || name == 'Long') {
150150
return 'getAsLong';
151-
} else if (name.startsWith('List')) {
152-
return 'getAsJsonArray';
153-
} else {
154-
// TODO(jwren): cleanup
151+
} else if (name.startsWith('List') || name.endsWith('[]')) {
155152
return 'getAsJsonArray';
156153
}
154+
155+
throw StateError('Unexpected type for _getAsTypeMethodName: $name');
157156
}
158157

159158
String _getEqualsLogicForField(TypeObjectField field, String other) {
@@ -171,11 +170,21 @@ class CodegenJavaType extends CodegenJavaVisitor {
171170
/// for the toString generation.
172171
String _getToStringForField(TypeObjectField field) {
173172
var name = javaName(field.name);
174-
if (isArray(field.type) || isList(field.type)) {
175-
return 'StringUtils.join($name, ", ")';
176-
} else {
177-
return name;
173+
if (isArray(field.type)) {
174+
var combined =
175+
'Arrays.stream($name)'
176+
'.mapToObj(String::valueOf)'
177+
'.collect(Collectors.joining(", "))';
178+
return field.optional ? '$name == null ? "null" : $combined' : combined;
179+
} else if (isList(field.type)) {
180+
var combined =
181+
'$name.stream()'
182+
'.map(String::valueOf)'
183+
'.collect(Collectors.joining(", "))';
184+
return field.optional ? '$name == null ? "null" : $combined' : combined;
178185
}
186+
187+
return name;
179188
}
180189

181190
bool _isTypeFieldInUpdateContentUnionType(
@@ -279,13 +288,12 @@ class CodegenJavaType extends CodegenJavaVisitor {
279288
writeln('import java.util.List;');
280289
writeln('import java.util.Map;');
281290
writeln('import java.util.Objects;');
282-
writeln('import com.google.common.collect.Lists;');
291+
writeln('import java.util.stream.Collectors;');
283292
writeln('import com.google.dart.server.utilities.general.JsonUtilities;');
284293
writeln('import com.google.gson.JsonArray;');
285294
writeln('import com.google.gson.JsonElement;');
286295
writeln('import com.google.gson.JsonObject;');
287296
writeln('import com.google.gson.JsonPrimitive;');
288-
writeln('import org.apache.commons.lang3.StringUtils;');
289297
writeln();
290298
javadocComment(
291299
toHtmlVisitor.collectHtml(() {
@@ -564,8 +572,8 @@ class CodegenJavaType extends CodegenJavaVisitor {
564572
// compute children recursively
565573
List<Outline> childrenList = new ArrayList<>();
566574
JsonElement childrenJsonArray = outlineObject.get("children");
567-
if (childrenJsonArray instanceof JsonArray) {
568-
for (JsonElement jsonElement : (JsonArray) childrenJsonArray) {
575+
if (childrenJsonArray instanceof JsonArray jsonChildren) {
576+
for (JsonElement jsonElement : jsonChildren) {
569577
JsonObject childObject = jsonElement.getAsJsonObject();
570578
childrenList.add(fromJson(outline, childObject));
571579
}
@@ -618,6 +626,9 @@ class CodegenJavaType extends CodegenJavaVisitor {
618626
// }
619627
if (className != 'Outline') {
620628
publicMethod('toJson', () {
629+
if (superclassName != null) {
630+
writeln('@Override');
631+
}
621632
writeln('public JsonObject toJson() {');
622633
indent(() {
623634
writeln('JsonObject jsonObject = new JsonObject();');
@@ -647,9 +658,8 @@ class CodegenJavaType extends CodegenJavaVisitor {
647658
writeln('@Override');
648659
writeln('public boolean equals(Object obj) {');
649660
indent(() {
650-
writeln('if (obj instanceof $className) {');
661+
writeln('if (obj instanceof $className other) {');
651662
indent(() {
652-
writeln('$className other = ($className) obj;');
653663
writeln('return');
654664
indent(() {
655665
var equalsForField = <String>[];
@@ -715,8 +725,14 @@ class CodegenJavaType extends CodegenJavaVisitor {
715725
writeln();
716726
}
717727
for (var i = 0; i < fields.length; i++) {
728+
var field = fields[i];
718729
indent(() {
719-
write(javaName(fields[i].name));
730+
var fieldName = javaName(field.name);
731+
if (isArray(field.type)) {
732+
write('Arrays.hashCode($fieldName)');
733+
} else {
734+
write(fieldName);
735+
}
720736
if (i + 1 != fields.length) {
721737
write(', ');
722738
}
@@ -739,12 +755,11 @@ class CodegenJavaType extends CodegenJavaVisitor {
739755
writeln('builder.append("[");');
740756
for (var i = 0; i < fields.length; i++) {
741757
writeln('builder.append("${javaName(fields[i].name)}=");');
742-
write('builder.append(${_getToStringForField(fields[i])}');
758+
writeln('builder.append(${_getToStringForField(fields[i])});');
743759
if (i + 1 != fields.length) {
744-
// this is not the last field
745-
write(' + ", "');
760+
// This is not the last field, so append a comma.
761+
writeln('builder.append(", ");');
746762
}
747-
writeln(');');
748763
}
749764
writeln('builder.append("]");');
750765
writeln('return builder.toString();');

pkg/analysis_server/tool/spec/generated/java/types/AddContentOverlay.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@
1313
import java.util.List;
1414
import java.util.Map;
1515
import java.util.Objects;
16-
import com.google.common.collect.Lists;
16+
import java.util.stream.Collectors;
1717
import com.google.dart.server.utilities.general.JsonUtilities;
1818
import com.google.gson.JsonArray;
1919
import com.google.gson.JsonElement;
2020
import com.google.gson.JsonObject;
2121
import com.google.gson.JsonPrimitive;
22-
import org.apache.commons.lang3.StringUtils;
2322

2423
/**
2524
* A directive to begin overlaying the contents of a file. The supplied content will be used for
@@ -54,8 +53,7 @@ public AddContentOverlay(String content) {
5453

5554
@Override
5655
public boolean equals(Object obj) {
57-
if (obj instanceof AddContentOverlay) {
58-
AddContentOverlay other = (AddContentOverlay) obj;
56+
if (obj instanceof AddContentOverlay other) {
5957
return
6058
Objects.equals(other.type, type) &&
6159
Objects.equals(other.content, content);
@@ -111,7 +109,8 @@ public String toString() {
111109
StringBuilder builder = new StringBuilder();
112110
builder.append("[");
113111
builder.append("type=");
114-
builder.append(type + ", ");
112+
builder.append(type);
113+
builder.append(", ");
115114
builder.append("content=");
116115
builder.append(content);
117116
builder.append("]");

pkg/analysis_server/tool/spec/generated/java/types/AnalysisError.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@
1313
import java.util.List;
1414
import java.util.Map;
1515
import java.util.Objects;
16-
import com.google.common.collect.Lists;
16+
import java.util.stream.Collectors;
1717
import com.google.dart.server.utilities.general.JsonUtilities;
1818
import com.google.gson.JsonArray;
1919
import com.google.gson.JsonElement;
2020
import com.google.gson.JsonObject;
2121
import com.google.gson.JsonPrimitive;
22-
import org.apache.commons.lang3.StringUtils;
2322

2423
/**
2524
* An indication of an error, warning, or hint that was produced by the analysis.
@@ -105,8 +104,7 @@ public AnalysisError(String severity, String type, Location location, String mes
105104

106105
@Override
107106
public boolean equals(Object obj) {
108-
if (obj instanceof AnalysisError) {
109-
AnalysisError other = (AnalysisError) obj;
107+
if (obj instanceof AnalysisError other) {
110108
return
111109
Objects.equals(other.severity, severity) &&
112110
Objects.equals(other.type, type) &&
@@ -264,21 +262,29 @@ public String toString() {
264262
StringBuilder builder = new StringBuilder();
265263
builder.append("[");
266264
builder.append("severity=");
267-
builder.append(severity + ", ");
265+
builder.append(severity);
266+
builder.append(", ");
268267
builder.append("type=");
269-
builder.append(type + ", ");
268+
builder.append(type);
269+
builder.append(", ");
270270
builder.append("location=");
271-
builder.append(location + ", ");
271+
builder.append(location);
272+
builder.append(", ");
272273
builder.append("message=");
273-
builder.append(message + ", ");
274+
builder.append(message);
275+
builder.append(", ");
274276
builder.append("correction=");
275-
builder.append(correction + ", ");
277+
builder.append(correction);
278+
builder.append(", ");
276279
builder.append("code=");
277-
builder.append(code + ", ");
280+
builder.append(code);
281+
builder.append(", ");
278282
builder.append("url=");
279-
builder.append(url + ", ");
283+
builder.append(url);
284+
builder.append(", ");
280285
builder.append("contextMessages=");
281-
builder.append(StringUtils.join(contextMessages, ", ") + ", ");
286+
builder.append(contextMessages == null ? "null" : contextMessages.stream().map(String::valueOf).collect(Collectors.joining(", ")));
287+
builder.append(", ");
282288
builder.append("hasFix=");
283289
builder.append(hasFix);
284290
builder.append("]");

pkg/analysis_server/tool/spec/generated/java/types/AnalysisErrorFixes.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@
1313
import java.util.List;
1414
import java.util.Map;
1515
import java.util.Objects;
16-
import com.google.common.collect.Lists;
16+
import java.util.stream.Collectors;
1717
import com.google.dart.server.utilities.general.JsonUtilities;
1818
import com.google.gson.JsonArray;
1919
import com.google.gson.JsonElement;
2020
import com.google.gson.JsonObject;
2121
import com.google.gson.JsonPrimitive;
22-
import org.apache.commons.lang3.StringUtils;
2322

2423
/**
2524
* A list of fixes associated with a specific error.
@@ -53,8 +52,7 @@ public AnalysisErrorFixes(AnalysisError error, List<SourceChange> fixes) {
5352

5453
@Override
5554
public boolean equals(Object obj) {
56-
if (obj instanceof AnalysisErrorFixes) {
57-
AnalysisErrorFixes other = (AnalysisErrorFixes) obj;
55+
if (obj instanceof AnalysisErrorFixes other) {
5856
return
5957
Objects.equals(other.error, error) &&
6058
Objects.equals(other.fixes, fixes);
@@ -117,9 +115,10 @@ public String toString() {
117115
StringBuilder builder = new StringBuilder();
118116
builder.append("[");
119117
builder.append("error=");
120-
builder.append(error + ", ");
118+
builder.append(error);
119+
builder.append(", ");
121120
builder.append("fixes=");
122-
builder.append(StringUtils.join(fixes, ", "));
121+
builder.append(fixes.stream().map(String::valueOf).collect(Collectors.joining(", ")));
123122
builder.append("]");
124123
return builder.toString();
125124
}

pkg/analysis_server/tool/spec/generated/java/types/AnalysisOptions.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@
1313
import java.util.List;
1414
import java.util.Map;
1515
import java.util.Objects;
16-
import com.google.common.collect.Lists;
16+
import java.util.stream.Collectors;
1717
import com.google.dart.server.utilities.general.JsonUtilities;
1818
import com.google.gson.JsonArray;
1919
import com.google.gson.JsonElement;
2020
import com.google.gson.JsonObject;
2121
import com.google.gson.JsonPrimitive;
22-
import org.apache.commons.lang3.StringUtils;
2322

2423
/**
2524
* Deprecated: the only reference to this type has been deprecated.
@@ -95,8 +94,7 @@ public AnalysisOptions(Boolean enableAsync, Boolean enableDeferredLoading, Boole
9594

9695
@Override
9796
public boolean equals(Object obj) {
98-
if (obj instanceof AnalysisOptions) {
99-
AnalysisOptions other = (AnalysisOptions) obj;
97+
if (obj instanceof AnalysisOptions other) {
10098
return
10199
Objects.equals(other.enableAsync, enableAsync) &&
102100
Objects.equals(other.enableDeferredLoading, enableDeferredLoading) &&
@@ -233,17 +231,23 @@ public String toString() {
233231
StringBuilder builder = new StringBuilder();
234232
builder.append("[");
235233
builder.append("enableAsync=");
236-
builder.append(enableAsync + ", ");
234+
builder.append(enableAsync);
235+
builder.append(", ");
237236
builder.append("enableDeferredLoading=");
238-
builder.append(enableDeferredLoading + ", ");
237+
builder.append(enableDeferredLoading);
238+
builder.append(", ");
239239
builder.append("enableEnums=");
240-
builder.append(enableEnums + ", ");
240+
builder.append(enableEnums);
241+
builder.append(", ");
241242
builder.append("enableNullAwareOperators=");
242-
builder.append(enableNullAwareOperators + ", ");
243+
builder.append(enableNullAwareOperators);
244+
builder.append(", ");
243245
builder.append("generateDart2jsHints=");
244-
builder.append(generateDart2jsHints + ", ");
246+
builder.append(generateDart2jsHints);
247+
builder.append(", ");
245248
builder.append("generateHints=");
246-
builder.append(generateHints + ", ");
249+
builder.append(generateHints);
250+
builder.append(", ");
247251
builder.append("generateLints=");
248252
builder.append(generateLints);
249253
builder.append("]");

pkg/analysis_server/tool/spec/generated/java/types/AnalysisStatus.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@
1313
import java.util.List;
1414
import java.util.Map;
1515
import java.util.Objects;
16-
import com.google.common.collect.Lists;
16+
import java.util.stream.Collectors;
1717
import com.google.dart.server.utilities.general.JsonUtilities;
1818
import com.google.gson.JsonArray;
1919
import com.google.gson.JsonElement;
2020
import com.google.gson.JsonObject;
2121
import com.google.gson.JsonPrimitive;
22-
import org.apache.commons.lang3.StringUtils;
2322

2423
/**
2524
* An indication of the current state of analysis.
@@ -53,8 +52,7 @@ public AnalysisStatus(boolean isAnalyzing, String analysisTarget) {
5352

5453
@Override
5554
public boolean equals(Object obj) {
56-
if (obj instanceof AnalysisStatus) {
57-
AnalysisStatus other = (AnalysisStatus) obj;
55+
if (obj instanceof AnalysisStatus other) {
5856
return
5957
other.isAnalyzing == isAnalyzing &&
6058
Objects.equals(other.analysisTarget, analysisTarget);
@@ -115,7 +113,8 @@ public String toString() {
115113
StringBuilder builder = new StringBuilder();
116114
builder.append("[");
117115
builder.append("isAnalyzing=");
118-
builder.append(isAnalyzing + ", ");
116+
builder.append(isAnalyzing);
117+
builder.append(", ");
119118
builder.append("analysisTarget=");
120119
builder.append(analysisTarget);
121120
builder.append("]");

pkg/analysis_server/tool/spec/generated/java/types/BulkFix.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@
1313
import java.util.List;
1414
import java.util.Map;
1515
import java.util.Objects;
16-
import com.google.common.collect.Lists;
16+
import java.util.stream.Collectors;
1717
import com.google.dart.server.utilities.general.JsonUtilities;
1818
import com.google.gson.JsonArray;
1919
import com.google.gson.JsonElement;
2020
import com.google.gson.JsonObject;
2121
import com.google.gson.JsonPrimitive;
22-
import org.apache.commons.lang3.StringUtils;
2322

2423
/**
2524
* A description of bulk fixes to a library.
@@ -53,8 +52,7 @@ public BulkFix(String path, List<BulkFixDetail> fixes) {
5352

5453
@Override
5554
public boolean equals(Object obj) {
56-
if (obj instanceof BulkFix) {
57-
BulkFix other = (BulkFix) obj;
55+
if (obj instanceof BulkFix other) {
5856
return
5957
Objects.equals(other.path, path) &&
6058
Objects.equals(other.fixes, fixes);
@@ -117,9 +115,10 @@ public String toString() {
117115
StringBuilder builder = new StringBuilder();
118116
builder.append("[");
119117
builder.append("path=");
120-
builder.append(path + ", ");
118+
builder.append(path);
119+
builder.append(", ");
121120
builder.append("fixes=");
122-
builder.append(StringUtils.join(fixes, ", "));
121+
builder.append(fixes.stream().map(String::valueOf).collect(Collectors.joining(", ")));
123122
builder.append("]");
124123
return builder.toString();
125124
}

0 commit comments

Comments
 (0)