Skip to content

Commit 18b8904

Browse files
parloughCommit Queue
authored andcommitted
[analysis_server] Reduce use of external libraries in generated Java code
Change-Id: Id0013dbbd028ad4ca6450efb27d899bceacaa7af Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/390021 Commit-Queue: Brian Wilkerson <[email protected]> Auto-Submit: Parker Lougheed <[email protected]> Reviewed-by: Jaime Wren <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 6efb5eb commit 18b8904

File tree

73 files changed

+1087
-1302
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

+1087
-1302
lines changed

pkg/analysis_server/tool/spec/codegen_java_types.dart

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ class CodegenJavaType extends CodegenJavaVisitor {
154154
} else if (isArray(field.type)) {
155155
return 'Arrays.equals(other.$name, $name)';
156156
} else {
157-
return 'ObjectUtilities.equals($other.$name, $name)';
157+
return 'Objects.equals($other.$name, $name)';
158158
}
159159
}
160160

@@ -259,18 +259,16 @@ class CodegenJavaType extends CodegenJavaVisitor {
259259

260260
void _writeTypeObject(TypeDecl type, dom.Element? html) {
261261
writeln('import java.util.Arrays;');
262+
writeln('import java.util.ArrayList;');
262263
writeln('import java.util.List;');
263264
writeln('import java.util.Map;');
265+
writeln('import java.util.Objects;');
264266
writeln('import com.google.common.collect.Lists;');
265267
writeln('import com.google.dart.server.utilities.general.JsonUtilities;');
266-
writeln('import com.google.dart.server.utilities.general.ObjectUtilities;');
267268
writeln('import com.google.gson.JsonArray;');
268269
writeln('import com.google.gson.JsonElement;');
269270
writeln('import com.google.gson.JsonObject;');
270271
writeln('import com.google.gson.JsonPrimitive;');
271-
writeln('import org.apache.commons.lang3.builder.HashCodeBuilder;');
272-
writeln('import java.util.ArrayList;');
273-
writeln('import java.util.Iterator;');
274272
writeln('import org.apache.commons.lang3.StringUtils;');
275273
writeln();
276274
javadocComment(toHtmlVisitor.collectHtml(() {
@@ -299,8 +297,7 @@ class CodegenJavaType extends CodegenJavaVisitor {
299297
// public static final "EMPTY_LIST" field
300298
//
301299
publicField(javaName('EMPTY_LIST'), () {
302-
writeln(
303-
'public static final List<$className> EMPTY_LIST = Lists.newArrayList();');
300+
writeln('public static final List<$className> EMPTY_LIST = List.of();');
304301
});
305302

306303
//
@@ -335,7 +332,7 @@ class CodegenJavaType extends CodegenJavaVisitor {
335332
if (className == 'NavigationRegion') {
336333
privateField(javaName('targetObjects'), () {
337334
writeln(
338-
'private final List<NavigationTarget> targetObjects = Lists.newArrayList();');
335+
'private final List<NavigationTarget> targetObjects = new ArrayList<>();');
339336
});
340337
}
341338
if (className == 'NavigationTarget') {
@@ -434,8 +431,7 @@ class CodegenJavaType extends CodegenJavaVisitor {
434431
publicMethod('lookupTargets', () {
435432
writeln(
436433
'public void lookupTargets(List<NavigationTarget> allTargets) {');
437-
writeln(' for (int i = 0; i < targets.length; i++) {');
438-
writeln(' int targetIndex = targets[i];');
434+
writeln(' for (int targetIndex : targets) {');
439435
writeln(' NavigationTarget target = allTargets.get(targetIndex);');
440436
writeln(' targetObjects.add(target);');
441437
writeln(' }');
@@ -529,12 +525,11 @@ class CodegenJavaType extends CodegenJavaVisitor {
529525
Outline outline = new Outline(parent, element, offset, length, codeOffset, codeLength);
530526
531527
// compute children recursively
532-
List<Outline> childrenList = Lists.newArrayList();
528+
List<Outline> childrenList = = new ArrayList<>();
533529
JsonElement childrenJsonArray = outlineObject.get("children");
534530
if (childrenJsonArray instanceof JsonArray) {
535-
Iterator<JsonElement> childrenElementIterator = ((JsonArray) childrenJsonArray).iterator();
536-
while (childrenElementIterator.hasNext()) {
537-
JsonObject childObject = childrenElementIterator.next().getAsJsonObject();
531+
for (JsonElement jsonElement : (JsonArray) childrenJsonArray) {
532+
JsonObject childObject = jsonElement.getAsJsonObject();
538533
childrenList.add(fromJson(outline, childObject));
539534
}
540535
}
@@ -563,10 +558,9 @@ class CodegenJavaType extends CodegenJavaVisitor {
563558
writeln(' return EMPTY_LIST;');
564559
writeln('}');
565560
writeln(
566-
'ArrayList<$className> list = new ArrayList<$className>(jsonArray.size());');
567-
writeln('Iterator<JsonElement> iterator = jsonArray.iterator();');
568-
writeln('while (iterator.hasNext()) {');
569-
writeln(' list.add(fromJson(iterator.next().getAsJsonObject()));');
561+
'List<$className> list = new ArrayList<>(jsonArray.size());');
562+
writeln('for (final JsonElement element : jsonArray) {');
563+
writeln(' list.add(fromJson(element.getAsJsonObject()));');
570564
writeln('}');
571565
writeln('return list;');
572566
});
@@ -676,11 +670,20 @@ class CodegenJavaType extends CodegenJavaVisitor {
676670
writeln('@Override');
677671
writeln('public int hashCode() {');
678672
indent(() {
679-
writeln('HashCodeBuilder builder = new HashCodeBuilder();');
673+
write('return Objects.hash(');
674+
if (fields.isNotEmpty) {
675+
writeln();
676+
}
680677
for (var i = 0; i < fields.length; i++) {
681-
writeln('builder.append(${javaName(fields[i].name)});');
678+
indent(() {
679+
write(javaName(fields[i].name));
680+
if (i + 1 != fields.length) {
681+
write(', ');
682+
}
683+
writeln();
684+
});
682685
}
683-
writeln('return builder.toHashCode();');
686+
writeln(');');
684687
});
685688
writeln('}');
686689
});

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

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,16 @@
99
package org.dartlang.analysis.server.protocol;
1010

1111
import java.util.Arrays;
12+
import java.util.ArrayList;
1213
import java.util.List;
1314
import java.util.Map;
15+
import java.util.Objects;
1416
import com.google.common.collect.Lists;
1517
import com.google.dart.server.utilities.general.JsonUtilities;
16-
import com.google.dart.server.utilities.general.ObjectUtilities;
1718
import com.google.gson.JsonArray;
1819
import com.google.gson.JsonElement;
1920
import com.google.gson.JsonObject;
2021
import com.google.gson.JsonPrimitive;
21-
import org.apache.commons.lang3.builder.HashCodeBuilder;
22-
import java.util.ArrayList;
23-
import java.util.Iterator;
2422
import org.apache.commons.lang3.StringUtils;
2523

2624
/**
@@ -37,7 +35,7 @@ public class AddContentOverlay {
3735

3836
public static final AddContentOverlay[] EMPTY_ARRAY = new AddContentOverlay[0];
3937

40-
public static final List<AddContentOverlay> EMPTY_LIST = Lists.newArrayList();
38+
public static final List<AddContentOverlay> EMPTY_LIST = List.of();
4139

4240
private final String type;
4341

@@ -59,8 +57,8 @@ public boolean equals(Object obj) {
5957
if (obj instanceof AddContentOverlay) {
6058
AddContentOverlay other = (AddContentOverlay) obj;
6159
return
62-
ObjectUtilities.equals(other.type, type) &&
63-
ObjectUtilities.equals(other.content, content);
60+
Objects.equals(other.type, type) &&
61+
Objects.equals(other.content, content);
6462
}
6563
return false;
6664
}
@@ -75,10 +73,9 @@ public static List<AddContentOverlay> fromJsonArray(JsonArray jsonArray) {
7573
if (jsonArray == null) {
7674
return EMPTY_LIST;
7775
}
78-
ArrayList<AddContentOverlay> list = new ArrayList<AddContentOverlay>(jsonArray.size());
79-
Iterator<JsonElement> iterator = jsonArray.iterator();
80-
while (iterator.hasNext()) {
81-
list.add(fromJson(iterator.next().getAsJsonObject()));
76+
List<AddContentOverlay> list = new ArrayList<>(jsonArray.size());
77+
for (final JsonElement element : jsonArray) {
78+
list.add(fromJson(element.getAsJsonObject()));
8279
}
8380
return list;
8481
}
@@ -96,10 +93,10 @@ public String getType() {
9693

9794
@Override
9895
public int hashCode() {
99-
HashCodeBuilder builder = new HashCodeBuilder();
100-
builder.append(type);
101-
builder.append(content);
102-
return builder.toHashCode();
96+
return Objects.hash(
97+
type,
98+
content
99+
);
103100
}
104101

105102
public JsonObject toJson() {

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

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,16 @@
99
package org.dartlang.analysis.server.protocol;
1010

1111
import java.util.Arrays;
12+
import java.util.ArrayList;
1213
import java.util.List;
1314
import java.util.Map;
15+
import java.util.Objects;
1416
import com.google.common.collect.Lists;
1517
import com.google.dart.server.utilities.general.JsonUtilities;
16-
import com.google.dart.server.utilities.general.ObjectUtilities;
1718
import com.google.gson.JsonArray;
1819
import com.google.gson.JsonElement;
1920
import com.google.gson.JsonObject;
2021
import com.google.gson.JsonPrimitive;
21-
import org.apache.commons.lang3.builder.HashCodeBuilder;
22-
import java.util.ArrayList;
23-
import java.util.Iterator;
2422
import org.apache.commons.lang3.StringUtils;
2523

2624
/**
@@ -33,7 +31,7 @@ public class AnalysisError {
3331

3432
public static final AnalysisError[] EMPTY_ARRAY = new AnalysisError[0];
3533

36-
public static final List<AnalysisError> EMPTY_LIST = Lists.newArrayList();
34+
public static final List<AnalysisError> EMPTY_LIST = List.of();
3735

3836
/**
3937
* The severity of the error.
@@ -110,15 +108,15 @@ public boolean equals(Object obj) {
110108
if (obj instanceof AnalysisError) {
111109
AnalysisError other = (AnalysisError) obj;
112110
return
113-
ObjectUtilities.equals(other.severity, severity) &&
114-
ObjectUtilities.equals(other.type, type) &&
115-
ObjectUtilities.equals(other.location, location) &&
116-
ObjectUtilities.equals(other.message, message) &&
117-
ObjectUtilities.equals(other.correction, correction) &&
118-
ObjectUtilities.equals(other.code, code) &&
119-
ObjectUtilities.equals(other.url, url) &&
120-
ObjectUtilities.equals(other.contextMessages, contextMessages) &&
121-
ObjectUtilities.equals(other.hasFix, hasFix);
111+
Objects.equals(other.severity, severity) &&
112+
Objects.equals(other.type, type) &&
113+
Objects.equals(other.location, location) &&
114+
Objects.equals(other.message, message) &&
115+
Objects.equals(other.correction, correction) &&
116+
Objects.equals(other.code, code) &&
117+
Objects.equals(other.url, url) &&
118+
Objects.equals(other.contextMessages, contextMessages) &&
119+
Objects.equals(other.hasFix, hasFix);
122120
}
123121
return false;
124122
}
@@ -140,10 +138,9 @@ public static List<AnalysisError> fromJsonArray(JsonArray jsonArray) {
140138
if (jsonArray == null) {
141139
return EMPTY_LIST;
142140
}
143-
ArrayList<AnalysisError> list = new ArrayList<AnalysisError>(jsonArray.size());
144-
Iterator<JsonElement> iterator = jsonArray.iterator();
145-
while (iterator.hasNext()) {
146-
list.add(fromJson(iterator.next().getAsJsonObject()));
141+
List<AnalysisError> list = new ArrayList<>(jsonArray.size());
142+
for (final JsonElement element : jsonArray) {
143+
list.add(fromJson(element.getAsJsonObject()));
147144
}
148145
return list;
149146
}
@@ -223,17 +220,17 @@ public String getUrl() {
223220

224221
@Override
225222
public int hashCode() {
226-
HashCodeBuilder builder = new HashCodeBuilder();
227-
builder.append(severity);
228-
builder.append(type);
229-
builder.append(location);
230-
builder.append(message);
231-
builder.append(correction);
232-
builder.append(code);
233-
builder.append(url);
234-
builder.append(contextMessages);
235-
builder.append(hasFix);
236-
return builder.toHashCode();
223+
return Objects.hash(
224+
severity,
225+
type,
226+
location,
227+
message,
228+
correction,
229+
code,
230+
url,
231+
contextMessages,
232+
hasFix
233+
);
237234
}
238235

239236
public JsonObject toJson() {

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

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,16 @@
99
package org.dartlang.analysis.server.protocol;
1010

1111
import java.util.Arrays;
12+
import java.util.ArrayList;
1213
import java.util.List;
1314
import java.util.Map;
15+
import java.util.Objects;
1416
import com.google.common.collect.Lists;
1517
import com.google.dart.server.utilities.general.JsonUtilities;
16-
import com.google.dart.server.utilities.general.ObjectUtilities;
1718
import com.google.gson.JsonArray;
1819
import com.google.gson.JsonElement;
1920
import com.google.gson.JsonObject;
2021
import com.google.gson.JsonPrimitive;
21-
import org.apache.commons.lang3.builder.HashCodeBuilder;
22-
import java.util.ArrayList;
23-
import java.util.Iterator;
2422
import org.apache.commons.lang3.StringUtils;
2523

2624
/**
@@ -33,7 +31,7 @@ public class AnalysisErrorFixes {
3331

3432
public static final AnalysisErrorFixes[] EMPTY_ARRAY = new AnalysisErrorFixes[0];
3533

36-
public static final List<AnalysisErrorFixes> EMPTY_LIST = Lists.newArrayList();
34+
public static final List<AnalysisErrorFixes> EMPTY_LIST = List.of();
3735

3836
/**
3937
* The error with which the fixes are associated.
@@ -58,8 +56,8 @@ public boolean equals(Object obj) {
5856
if (obj instanceof AnalysisErrorFixes) {
5957
AnalysisErrorFixes other = (AnalysisErrorFixes) obj;
6058
return
61-
ObjectUtilities.equals(other.error, error) &&
62-
ObjectUtilities.equals(other.fixes, fixes);
59+
Objects.equals(other.error, error) &&
60+
Objects.equals(other.fixes, fixes);
6361
}
6462
return false;
6563
}
@@ -74,10 +72,9 @@ public static List<AnalysisErrorFixes> fromJsonArray(JsonArray jsonArray) {
7472
if (jsonArray == null) {
7573
return EMPTY_LIST;
7674
}
77-
ArrayList<AnalysisErrorFixes> list = new ArrayList<AnalysisErrorFixes>(jsonArray.size());
78-
Iterator<JsonElement> iterator = jsonArray.iterator();
79-
while (iterator.hasNext()) {
80-
list.add(fromJson(iterator.next().getAsJsonObject()));
75+
List<AnalysisErrorFixes> list = new ArrayList<>(jsonArray.size());
76+
for (final JsonElement element : jsonArray) {
77+
list.add(fromJson(element.getAsJsonObject()));
8178
}
8279
return list;
8380
}
@@ -98,10 +95,10 @@ public List<SourceChange> getFixes() {
9895

9996
@Override
10097
public int hashCode() {
101-
HashCodeBuilder builder = new HashCodeBuilder();
102-
builder.append(error);
103-
builder.append(fixes);
104-
return builder.toHashCode();
98+
return Objects.hash(
99+
error,
100+
fixes
101+
);
105102
}
106103

107104
public JsonObject toJson() {

0 commit comments

Comments
 (0)