Skip to content

Commit a801049

Browse files
authored
Migrate Java syntax and APIs for new features in Java (#7513)
This includes fixes from the IntelliJ inspections for Java language level migrations, switch statements, lambdas, etc.
1 parent 725a610 commit a801049

File tree

69 files changed

+186
-364
lines changed

Some content is hidden

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

69 files changed

+186
-364
lines changed

flutter-idea/src/io/flutter/FlutterInitializer.java

-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
import io.flutter.run.daemon.DeviceService;
4747
import io.flutter.sdk.FlutterPluginsLibraryManager;
4848
import io.flutter.sdk.FlutterSdk;
49-
import io.flutter.sdk.FlutterSdkVersion;
5049
import io.flutter.settings.FlutterSettings;
5150
import io.flutter.survey.FlutterSurveyNotifications;
5251
import io.flutter.utils.FlutterModuleUtils;

flutter-idea/src/io/flutter/actions/DeviceSelectorAction.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,7 @@ private void updateActions(@NotNull Project project, Presentation presentation)
181181
if (SystemInfo.isMac) {
182182
boolean simulatorOpen = false;
183183
for (AnAction action : actions) {
184-
if (action instanceof SelectDeviceAction) {
185-
final SelectDeviceAction deviceAction = (SelectDeviceAction)action;
184+
if (action instanceof SelectDeviceAction deviceAction) {
186185
final FlutterDevice device = deviceAction.device;
187186
if (device.isIOS() && device.emulator()) {
188187
simulatorOpen = true;

flutter-idea/src/io/flutter/actions/OpenInAndroidStudioAction.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,7 @@ private static Editor getCurrentEditor(@NotNull Project project, @Nullable Virtu
116116
return null;
117117
}
118118
final FileEditor fileEditor = fileEditorManager.getSelectedEditor(file);
119-
if (fileEditor instanceof TextEditor) {
120-
final TextEditor textEditor = (TextEditor)fileEditor;
119+
if (fileEditor instanceof TextEditor textEditor) {
121120
final Editor editor = textEditor.getEditor();
122121
if (!editor.isDisposed()) {
123122
return editor;

flutter-idea/src/io/flutter/analytics/FlutterAnalysisServerListener.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -527,12 +527,11 @@ private void logFileAnalysisTime(@NotNull String kind, String path, long analysi
527527
*/
528528
void onPropertyChange(@NotNull PropertyChangeEvent propertyChangeEvent) {
529529
Object newValue = propertyChangeEvent.getNewValue();
530-
if (!(newValue instanceof LookupImpl)) {
530+
if (!(newValue instanceof LookupImpl lookup)) {
531531
return;
532532
}
533533

534534
setLookupSelectionHandler();
535-
LookupImpl lookup = (LookupImpl)newValue;
536535
lookup.addLookupListener(lookupSelectionHandler);
537536
}
538537

flutter-idea/src/io/flutter/analytics/UnifiedAnalytics.java

+6-10
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import com.intellij.openapi.application.ApplicationManager;
1616
import com.intellij.openapi.diagnostic.Logger;
1717
import com.intellij.openapi.project.Project;
18-
import com.jetbrains.lang.dart.ide.toolingDaemon.DartToolingDaemonConsumer;
1918
import com.jetbrains.lang.dart.ide.toolingDaemon.DartToolingDaemonService;
2019
import de.roderick.weberknecht.WebSocketException;
2120
import io.flutter.sdk.FlutterSdkUtil;
@@ -75,16 +74,13 @@ private CompletableFuture<JsonObject> makeUnifiedAnalyticsRequest(String request
7574
@NotNull JsonObject params) {
7675
CompletableFuture<JsonObject> finalResult = new CompletableFuture<>();
7776
try {
78-
service.sendRequest("UnifiedAnalytics." + requestName, params, false, new DartToolingDaemonConsumer() {
79-
@Override
80-
public void received(@NotNull JsonObject object) {
81-
JsonObject result = object.getAsJsonObject("result");
82-
if (result == null) {
83-
finalResult.completeExceptionally(new Exception(requestName + " JSON result is malformed: " + object));
84-
return;
85-
}
86-
finalResult.complete(result);
77+
service.sendRequest("UnifiedAnalytics." + requestName, params, false, object -> {
78+
JsonObject result = object.getAsJsonObject("result");
79+
if (result == null) {
80+
finalResult.completeExceptionally(new Exception(requestName + " JSON result is malformed: " + object));
81+
return;
8782
}
83+
finalResult.complete(result);
8884
});
8985
}
9086
catch (WebSocketException e) {

flutter-idea/src/io/flutter/bazel/PluginConfig.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,7 @@ String getUpdatedIosRunMessage() {
9595

9696
@Override
9797
public boolean equals(Object obj) {
98-
if (!(obj instanceof PluginConfig)) return false;
99-
final PluginConfig other = (PluginConfig)obj;
98+
if (!(obj instanceof PluginConfig other)) return false;
10099
return Objects.equal(fields, other.fields);
101100
}
102101

@@ -277,8 +276,7 @@ private static class Fields {
277276

278277
@Override
279278
public boolean equals(Object obj) {
280-
if (!(obj instanceof Fields)) return false;
281-
final Fields other = (Fields)obj;
279+
if (!(obj instanceof Fields other)) return false;
282280
return Objects.equal(daemonScript, other.daemonScript)
283281
&& Objects.equal(devToolsScript, other.devToolsScript)
284282
&& Objects.equal(doctorScript, other.doctorScript)

flutter-idea/src/io/flutter/bazel/Workspace.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,7 @@ public Set<String> getDependencies() {
247247

248248
@Override
249249
public boolean equals(Object obj) {
250-
if (!(obj instanceof Workspace)) return false;
251-
final Workspace other = (Workspace)obj;
250+
if (!(obj instanceof Workspace other)) return false;
252251
return Objects.equal(root, other.root) && Objects.equal(config, other.config);
253252
}
254253

flutter-idea/src/io/flutter/dart/DartSyntax.java

+5-8
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class DartSyntax {
2828
*/
2929
@Nullable
3030
public static DartCallExpression findEnclosingFunctionCall(@NotNull PsiElement elt, @NotNull String functionName) {
31-
return findEnclosingFunctionCall(elt, functionName, new Equator<String, String>() {
31+
return findEnclosingFunctionCall(elt, functionName, new Equator<>() {
3232
@Override
3333
boolean equate(@NotNull String first, @NotNull String second) {
3434
return Objects.equals(first, second);
@@ -43,7 +43,7 @@ boolean equate(@NotNull String first, @NotNull String second) {
4343
*/
4444
@Nullable
4545
public static DartCallExpression findEnclosingFunctionCall(@NotNull PsiElement elt, @NotNull Pattern functionRegex) {
46-
return findEnclosingFunctionCall(elt, functionRegex, new Equator<Pattern, String>() {
46+
return findEnclosingFunctionCall(elt, functionRegex, new Equator<>() {
4747
@Override
4848
boolean equate(@NotNull Pattern first, @NotNull String second) {
4949
return first.matcher(second).matches();
@@ -54,8 +54,7 @@ boolean equate(@NotNull Pattern first, @NotNull String second) {
5454
private static <T> DartCallExpression findEnclosingFunctionCall(
5555
@NotNull PsiElement elt, @NotNull T functionDescriptor, @NotNull Equator<T, String> equator) {
5656
while (elt != null) {
57-
if (elt instanceof DartCallExpression) {
58-
final DartCallExpression call = (DartCallExpression)elt;
57+
if (elt instanceof DartCallExpression call) {
5958
final String name = getCalledFunctionName(call);
6059
if (name != null && equator.equate(functionDescriptor, name)) {
6160
return call;
@@ -72,8 +71,7 @@ private static <T> DartCallExpression findEnclosingFunctionCall(
7271
@Nullable
7372
public static DartCallExpression findClosestEnclosingFunctionCall(@Nullable PsiElement element) {
7473
while (element != null) {
75-
if (element instanceof DartCallExpression) {
76-
final DartCallExpression call = (DartCallExpression)element;
74+
if (element instanceof DartCallExpression call) {
7775
final String name = getCalledFunctionName(call);
7876
if (name != null) {
7977
return call;
@@ -181,8 +179,7 @@ public static String unquote(@NotNull DartStringLiteralExpression lit) {
181179
if (lit.getFirstChild() == null) return null;
182180
final PsiElement second = lit.getFirstChild().getNextSibling();
183181
if (second.getNextSibling() != lit.getLastChild()) return null; // not three items
184-
if (!(second instanceof LeafPsiElement)) return null;
185-
final LeafPsiElement leaf = (LeafPsiElement)second;
182+
if (!(second instanceof LeafPsiElement leaf)) return null;
186183

187184
if (leaf.getElementType() != DartTokenTypes.REGULAR_STRING_PART) return null;
188185
return leaf.getText();

flutter-idea/src/io/flutter/dart/FlutterRequestUtilities.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,15 @@ else if (object instanceof Number) {
8484
else if (object instanceof String) {
8585
return new JsonPrimitive((String)object);
8686
}
87-
else if (object instanceof List<?>) {
88-
final List<?> list = (List<?>)object;
87+
else if (object instanceof List<?> list) {
8988
final JsonArray jsonArray = new JsonArray();
9089
for (Object item : list) {
9190
final JsonElement jsonItem = buildJsonElement(item);
9291
jsonArray.add(jsonItem);
9392
}
9493
return jsonArray;
9594
}
96-
else if (object instanceof Map<?, ?>) {
97-
final Map<?, ?> map = (Map<?, ?>)object;
95+
else if (object instanceof Map<?, ?> map) {
9896
final JsonObject jsonObject = new JsonObject();
9997
for (Map.Entry<?, ?> entry : map.entrySet()) {
10098
final Object key = entry.getKey();

flutter-idea/src/io/flutter/deeplinks/DeepLinksViewFactory.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,7 @@ public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindo
6666
ApplicationManager.getApplication().invokeLater(() -> {
6767
Optional.ofNullable(
6868
FlutterUtils.embeddedBrowser(project))
69-
.ifPresent(embeddedBrowser -> embeddedBrowser.openPanel(toolWindow, "Deep Links", devToolsUrl, (String err) -> {
70-
System.out.println(err);
71-
}));
69+
.ifPresent(embeddedBrowser -> embeddedBrowser.openPanel(toolWindow, "Deep Links", devToolsUrl, System.out::println));
7270
});
7371
}
7472
);

flutter-idea/src/io/flutter/editor/ActiveEditorsOutlineService.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,7 @@ public List<EditorEx> getActiveDartEditors() {
9393
final FileEditor[] editors = fileEditorManager.getSelectedEditors();
9494
final List<EditorEx> dartEditors = new ArrayList<>();
9595
for (FileEditor fileEditor : editors) {
96-
if (!(fileEditor instanceof TextEditor)) continue;
97-
final TextEditor textEditor = (TextEditor)fileEditor;
96+
if (!(fileEditor instanceof TextEditor textEditor)) continue;
9897
final Editor editor = textEditor.getEditor();
9998
if (editor instanceof EditorEx && !editor.isDisposed() && FlutterUtils.isDartFile(((EditorEx)editor).getVirtualFile())) {
10099
dartEditors.add((EditorEx)editor);

flutter-idea/src/io/flutter/editor/EditorEventServiceBase.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public EditorEventServiceBase(Project project) {
3131
}
3232

3333
protected void invokeAll(InvokeListener<L> invoke, Editor editor) {
34-
if (!(editor instanceof EditorEx)) {
34+
if (!(editor instanceof EditorEx editorEx)) {
3535
return;
3636
}
3737
final ArrayList<EditorEx> disposedEditors = new ArrayList<>();
@@ -43,7 +43,6 @@ protected void invokeAll(InvokeListener<L> invoke, Editor editor) {
4343
for (EditorEx e : disposedEditors) {
4444
listeners.removeAll(e);
4545
}
46-
final EditorEx editorEx = (EditorEx)editor;
4746
final Set<L> matches = listeners.get(editorEx);
4847
if (matches == null || matches.isEmpty()) return;
4948
for (L listener : matches) {

flutter-idea/src/io/flutter/editor/FlutterIconLineMarkerProvider.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -451,8 +451,7 @@ public void visitVarAccessDeclaration(@NotNull DartVarAccessDeclaration o) {
451451
String className = null;
452452
DartArguments arguments = null;
453453
DartType type = null;
454-
if (expression instanceof DartNewExpression) {
455-
final DartNewExpression newExpr = (DartNewExpression)expression;
454+
if (expression instanceof DartNewExpression newExpr) {
456455
type = newExpr.getType();
457456
if (type != null) {
458457
className = type.getText();

flutter-idea/src/io/flutter/editor/OutlineLocation.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,7 @@ public int hashCode() {
191191
@Override
192192
public boolean equals(Object o) {
193193
if (this == o) return true;
194-
if (!(o instanceof OutlineLocation)) return false;
195-
final OutlineLocation other = (OutlineLocation)o;
194+
if (!(o instanceof OutlineLocation other)) return false;
196195
return line == other.line &&
197196
column == other.column &&
198197
indent == other.indent &&

flutter-idea/src/io/flutter/editor/PropertyEditorPanel.java

+2-8
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,7 @@ public EnumValueWrapper getSelectedItem() {
136136

137137
@Override
138138
public void setSelectedItem(Object item) {
139-
if (item instanceof String) {
140-
final String expression = (String)item;
139+
if (item instanceof String expression) {
141140
for (EnumValueWrapper e : myList) {
142141
if (Objects.equals(e.expression, expression)) {
143142
mySelected = e;
@@ -551,12 +550,7 @@ protected void rebuildUi() {
551550
}
552551

553552
if (name.equals("data")) {
554-
if (documentation != null) {
555-
field.setToolTipText(documentation);
556-
}
557-
else {
558-
field.setToolTipText("data");
559-
}
553+
field.setToolTipText(Objects.requireNonNullElse(documentation, "data"));
560554
add(field, "span, growx");
561555
}
562556
else {

flutter-idea/src/io/flutter/editor/WidgetIndentHitTester.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ public int hashCode() {
5656
@Override
5757
public boolean equals(Object o) {
5858
if (this == o) return true;
59-
if (!(o instanceof WidgetIndentHitTester)) return false;
60-
final WidgetIndentHitTester other = (WidgetIndentHitTester)o;
59+
if (!(o instanceof WidgetIndentHitTester other)) return false;
6160
return Arrays.equals(lines, other.lines);
6261
}
6362

flutter-idea/src/io/flutter/editor/WidgetIndentsHighlightingPass.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -527,8 +527,7 @@ public static int compare(@NotNull TextRangeDescriptorPair r, @NotNull RangeHigh
527527
return answer;
528528
}
529529
final CustomHighlighterRenderer renderer = h.getCustomRenderer();
530-
if (renderer instanceof WidgetCustomHighlighterRenderer) {
531-
final WidgetCustomHighlighterRenderer widgetRenderer = (WidgetCustomHighlighterRenderer)renderer;
530+
if (renderer instanceof WidgetCustomHighlighterRenderer widgetRenderer) {
532531
return widgetRenderer.descriptor.compareTo(r.descriptor);
533532
}
534533
return -1;
@@ -550,9 +549,7 @@ public static void onCaretPositionChanged(EditorEx editor, Caret caret) {
550549
final WidgetIndentsPassData data = getIndentsPassData(editor);
551550
if (data == null || data.highlighters == null) return;
552551
for (RangeHighlighter h : data.highlighters) {
553-
if (h.getCustomRenderer() instanceof WidgetIndentsHighlightingPass.WidgetCustomHighlighterRenderer) {
554-
final WidgetIndentsHighlightingPass.WidgetCustomHighlighterRenderer renderer =
555-
(WidgetIndentsHighlightingPass.WidgetCustomHighlighterRenderer)h.getCustomRenderer();
552+
if (h.getCustomRenderer() instanceof WidgetCustomHighlighterRenderer renderer) {
556553
final boolean changed = renderer.updateSelected(editor, h, caret);
557554
if (changed) {
558555
editor.repaint(h.getStartOffset(), h.getEndOffset());

flutter-idea/src/io/flutter/editor/WidgetIndentsHighlightingPassFactory.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ public void caretPositionChanged(@NotNull CaretEvent event) {
9494
final Editor editor = event.getEditor();
9595
if (editor.getProject() != project) return;
9696
if (editor.isDisposed() || project.isDisposed()) return;
97-
if (!(editor instanceof EditorEx)) return;
98-
final EditorEx editorEx = (EditorEx)editor;
97+
if (!(editor instanceof EditorEx editorEx)) return;
9998
WidgetIndentsHighlightingPass.onCaretPositionChanged(editorEx, event.getCaret());
10099
}
101100
}, this);
@@ -233,8 +232,7 @@ public TextEditorHighlightingPass createHighlightingPass(@NotNull PsiFile file,
233232
FilteredIndentsHighlightingPass.cleanupHighlighters(e);
234233
}
235234

236-
if (!(e instanceof EditorEx)) return highlightingPass;
237-
final EditorEx editor = (EditorEx)e;
235+
if (!(e instanceof EditorEx editor)) return highlightingPass;
238236

239237
final VirtualFile virtualFile = editor.getVirtualFile();
240238
if (!FlutterUtils.couldContainWidgets(virtualFile)) {

flutter-idea/src/io/flutter/inspector/DiagnosticsNode.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ public DiagnosticsNode(JsonObject json,
8888

8989
@Override
9090
public boolean equals(Object other) {
91-
if (other instanceof DiagnosticsNode) {
92-
final DiagnosticsNode otherNode = (DiagnosticsNode)other;
91+
if (other instanceof DiagnosticsNode otherNode) {
9392
return getDartDiagnosticRef().equals(otherNode.getDartDiagnosticRef());
9493
}
9594
return false;

flutter-idea/src/io/flutter/inspector/InspectorInstanceRef.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ public InspectorInstanceRef(String id) {
2323

2424
@Override
2525
public boolean equals(Object other) {
26-
if (other instanceof InspectorInstanceRef) {
27-
final InspectorInstanceRef otherRef = (InspectorInstanceRef)other;
26+
if (other instanceof InspectorInstanceRef otherRef) {
2827
return Objects.equals(id, otherRef.id);
2928
}
3029
return false;

flutter-idea/src/io/flutter/inspector/InspectorService.java

+11-19
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,7 @@ public static InspectorService.Location outlineToLocation(Project project,
139139
PsiElement match = null;
140140
if (callExpression != null) {
141141
final DartExpression expression = callExpression.getExpression();
142-
if (expression instanceof DartReferenceExpression) {
143-
final DartReferenceExpression referenceExpression = (DartReferenceExpression)expression;
142+
if (expression instanceof DartReferenceExpression referenceExpression) {
144143
final PsiElement[] children = referenceExpression.getChildren();
145144
if (children.length > 1) {
146145
// This case handles expressions like 'ClassName.namedConstructor'
@@ -171,8 +170,7 @@ public static InspectorService.Location outlineToLocation(Project project,
171170
*/
172171
@Nullable
173172
public static InspectorService.Location outlineToLocation(Editor editor, FlutterOutline outline) {
174-
if (!(editor instanceof EditorEx)) return null;
175-
final EditorEx editorEx = (EditorEx)editor;
173+
if (!(editor instanceof EditorEx editorEx)) return null;
176174
return outlineToLocation(editor.getProject(), editorEx.getVirtualFile(), outline, editor.getDocument());
177175
}
178176
}
@@ -652,13 +650,10 @@ public CompletableFuture<XSourcePosition> getPropertyLocationHelper(ClassRef cla
652650
public CompletableFuture<DiagnosticsNode> getRoot(FlutterTreeType type) {
653651
// There is no excuse to call this method on a disposed group.
654652
assert (!disposed);
655-
switch (type) {
656-
case widget:
657-
return getRootWidget();
658-
case renderObject:
659-
return getRootRenderObject();
660-
}
661-
throw new RuntimeException("Unexpected FlutterTreeType");
653+
return switch (type) {
654+
case widget -> getRootWidget();
655+
case renderObject -> getRootRenderObject();
656+
};
662657
}
663658

664659
/**
@@ -1297,14 +1292,11 @@ public CompletableFuture<DiagnosticsNode> getSelection(DiagnosticsNode previousS
12971292
CompletableFuture<DiagnosticsNode> result = null;
12981293
final InspectorInstanceRef previousSelectionRef = previousSelection != null ? previousSelection.getDartDiagnosticRef() : null;
12991294

1300-
switch (treeType) {
1301-
case widget:
1302-
result = invokeServiceMethodReturningNode(localOnly ? "getSelectedSummaryWidget" : "getSelectedWidget", previousSelectionRef);
1303-
break;
1304-
case renderObject:
1305-
result = invokeServiceMethodReturningNode("getSelectedRenderObject", previousSelectionRef);
1306-
break;
1307-
}
1295+
result = switch (treeType) {
1296+
case widget ->
1297+
invokeServiceMethodReturningNode(localOnly ? "getSelectedSummaryWidget" : "getSelectedWidget", previousSelectionRef);
1298+
case renderObject -> invokeServiceMethodReturningNode("getSelectedRenderObject", previousSelectionRef);
1299+
};
13081300
return result.thenApplyAsync((DiagnosticsNode newSelection) -> nullValueIfDisposed(() -> {
13091301
if (newSelection != null && newSelection.getDartDiagnosticRef().equals(previousSelectionRef)) {
13101302
return previousSelection;

flutter-idea/src/io/flutter/inspector/TreeScrollAnimator.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -323,10 +323,9 @@ public String toString() {
323323

324324
@Override
325325
public boolean equals(Object o) {
326-
if (!(o instanceof Interval)) {
326+
if (!(o instanceof Interval interval)) {
327327
return false;
328328
}
329-
final Interval interval = (Interval)o;
330329
return interval.start == start && interval.length == length;
331330
}
332331
}

0 commit comments

Comments
 (0)