Skip to content

Commit 1e8bc6d

Browse files
authored
[CQ] nullability: make equality checks safe w/ Objects.equals() (#7997)
Get null-safe comparisons when comparators might be null using [`Object.equals`](https://docs.oracle.com/javase/8/docs/api/java/util/Objects.html#equals-java.lang.Object-java.lang.Object-).
1 parent 4e61dc8 commit 1e8bc6d

28 files changed

+66
-63
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ private static boolean isInTestOrSourceRoot(@Nullable Module module, @NotNull Da
222222
}
223223

224224
public static boolean isIntegrationTestingMode() {
225-
return System.getProperty("idea.required.plugins.id", "").equals("io.flutter.tests.gui.flutter-gui-tests");
225+
return Objects.equals(System.getProperty("idea.required.plugins.id", ""), "io.flutter.tests.gui.flutter-gui-tests");
226226
}
227227

228228
@Nullable
@@ -561,7 +561,7 @@ public static EmbeddedBrowser embeddedBrowser(Project project) {
561561
}
562562

563563
public static boolean embeddedBrowserAvailable(JxBrowserStatus status) {
564-
return status.equals(JxBrowserStatus.INSTALLED) || status.equals(JxBrowserStatus.INSTALLATION_SKIPPED) && FlutterSettings.getInstance()
564+
return Objects.equals(status, JxBrowserStatus.INSTALLED) || status.equals(JxBrowserStatus.INSTALLATION_SKIPPED) && FlutterSettings.getInstance()
565565
.isEnableJcefBrowser();
566566
}
567567
}

flutter-idea/src/io/flutter/android/IntelliJAndroidSdk.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import java.util.ArrayList;
2222
import java.util.List;
23+
import java.util.Objects;
2324

2425
/**
2526
* An Android SDK and its home directory; this references an IntelliJ @{@link Sdk} instance.
@@ -90,7 +91,7 @@ public static IntelliJAndroidSdk fromEnvironment() {
9091
@Nullable
9192
public static IntelliJAndroidSdk fromHome(VirtualFile file) {
9293
for (IntelliJAndroidSdk candidate : findAll()) {
93-
if (file.equals(candidate.getHome())) {
94+
if (Objects.equals(file, candidate.getHome())) {
9495
return candidate;
9596
}
9697
}

flutter-idea/src/io/flutter/console/FlutterConsoleFilter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.jetbrains.annotations.Nullable;
3131

3232
import java.awt.*;
33+
import java.util.Objects;
3334
import java.util.regex.Matcher;
3435
import java.util.regex.Pattern;
3536

@@ -170,7 +171,7 @@ public Result applyFilter(final String line, final int entireLength) {
170171
highlightLength = pathPart.length();
171172
break;
172173
}
173-
else if (split.length == 4 && split[0].equals("file")) {
174+
else if (split.length == 4 && Objects.equals(split[0], "file")) {
174175
// part = file:///Users/user/AndroidStudioProjects/flutter_app/test/widget_test.dart:23:18
175176
try {
176177
// Reconcile line number indexing.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ private void processResponse(JsonObject response) {
287287
private void processNotification(JsonObject response, @NotNull JsonElement eventName) {
288288
// If we add code to handle more event types below, update the filter in processString().
289289
final String event = eventName.getAsString();
290-
if (event.equals(FLUTTER_NOTIFICATION_OUTLINE)) {
290+
if (Objects.equals(event, FLUTTER_NOTIFICATION_OUTLINE)) {
291291
final JsonObject paramsObject = response.get("params").getAsJsonObject();
292292
final String file = paramsObject.get("file").getAsString();
293293

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.awt.*;
2323
import java.util.Arrays;
2424
import java.util.List;
25+
import java.util.Objects;
2526

2627
import static io.flutter.dart.DartPsiUtil.getNewExprFromType;
2728
import static io.flutter.dart.DartPsiUtil.topmostReferenceExpression;
@@ -136,7 +137,7 @@ else if (parent.getNode().getElementType() == DartTokenTypes.SIMPLE_TYPE) {
136137

137138
@Nullable
138139
private PsiElement resolveReferencedElement(@NotNull PsiElement element) {
139-
if (element instanceof DartCallExpression && element.getFirstChild().getText().equals("Color")) {
140+
if (element instanceof DartCallExpression && Objects.equals(element.getFirstChild().getText(), "Color")) {
140141
return element;
141142
}
142143
final PsiElement symbol = element.getLastChild();
@@ -216,7 +217,7 @@ private Color parseColorText(@NotNull String text, @NotNull String platform) {
216217
public void setColorTo(@NotNull PsiElement element, @NotNull Color color) {
217218
// Not trying to look up Material or Cupertino colors.
218219
// Unfortunately, there is no way to prevent the color picker from showing (if clicked) for those expressions.
219-
if (!element.getText().equals("Color")) return;
220+
if (!Objects.equals(element.getText(), "Color")) return;
220221
final Document document = PsiDocumentManager.getInstance(element.getProject()).getDocument(element.getContainingFile());
221222
final Runnable command = () -> {
222223
final PsiElement refExpr = topmostReferenceExpression(element);

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import org.dartlang.analysis.server.protocol.FlutterOutline;
1313
import org.jetbrains.annotations.Nullable;
1414

15+
import java.util.Objects;
16+
1517
class TextRangeTracker {
1618
private final TextRange rawRange;
1719
private RangeMarker marker;
@@ -96,7 +98,7 @@ public boolean isConsistentEndingWord() {
9698
// to update marker locations has hit a bad edge case as sometimes
9799
// happens when there is a large document edit due to running a
98100
// code formatter.
99-
endingWord.equals(TextRangeTracker.getCurrentWord(marker.getDocument(), marker.getEndOffset() - 1));
101+
Objects.equals(endingWord, TextRangeTracker.getCurrentWord(marker.getDocument(), marker.getEndOffset() - 1));
100102
}
101103
}
102104

flutter-idea/src/io/flutter/jxbrowser/EmbeddedJxBrowser.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ private EmbeddedJxBrowser(@NotNull Project project) {
160160
}
161161

162162
JxBrowserManager.installation.thenAccept((JxBrowserStatus status) -> {
163-
if (status.equals(JxBrowserStatus.INSTALLED)) {
163+
if (Objects.equals(status, JxBrowserStatus.INSTALLED)) {
164164
engineRef.compareAndSet(null, EmbeddedBrowserEngine.getInstance().getEngine());
165165
}
166166
});
@@ -268,7 +268,7 @@ protected void handleUpdatedJxBrowserStatusOnEventThread(JxBrowserStatus jxBrows
268268
}
269269

270270
protected void handleUpdatedJxBrowserStatus(JxBrowserStatus jxBrowserStatus, ContentManager contentManager) {
271-
if (jxBrowserStatus.equals(JxBrowserStatus.INSTALLED)) {
271+
if (Objects.equals(jxBrowserStatus, JxBrowserStatus.INSTALLED)) {
272272
return;
273273
} else if (jxBrowserStatus.equals(JxBrowserStatus.INSTALLATION_FAILED)) {
274274
handleJxBrowserInstallationFailed(contentManager);
@@ -286,7 +286,7 @@ protected void handleJxBrowserInstallationFailed(ContentManager contentManager)
286286
if (!jxBrowserUtils.licenseIsSet()) {
287287
// If the license isn't available, allow the user to open the equivalent page in a non-embedded browser window.
288288
inputs.add(new LabelInput("The JxBrowser license could not be found."));
289-
} else if (latestFailureReason != null && latestFailureReason.failureType.equals(FailureType.SYSTEM_INCOMPATIBLE)) {
289+
} else if (latestFailureReason != null && Objects.equals(latestFailureReason.failureType, FailureType.SYSTEM_INCOMPATIBLE)) {
290290
// If we know the system is incompatible, skip retry link and offer to open in browser.
291291
inputs.add(new LabelInput(latestFailureReason.detail));
292292
}

flutter-idea/src/io/flutter/jxbrowser/JxBrowserManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public void setUp(@NotNull String projectName) {
197197
return;
198198
}
199199

200-
if (!jxBrowserUtils.skipInstallation() && status.get().equals(JxBrowserStatus.INSTALLATION_SKIPPED)) {
200+
if (!jxBrowserUtils.skipInstallation() && Objects.equals(status.get(), JxBrowserStatus.INSTALLATION_SKIPPED)) {
201201
// This check returns status to NOT_INSTALLED so that JxBrowser can be downloaded and installed in cases where it is enabled after being disabled.
202202
status.compareAndSet(JxBrowserStatus.INSTALLATION_SKIPPED, JxBrowserStatus.NOT_INSTALLED);
203203
}

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@
2626
import org.jetbrains.annotations.Nullable;
2727

2828
import javax.swing.*;
29-
import java.util.ArrayList;
30-
import java.util.List;
31-
import java.util.Map;
32-
import java.util.Set;
29+
import java.util.*;
3330
import java.util.concurrent.CompletableFuture;
3431
import java.util.function.BiConsumer;
3532

@@ -81,7 +78,7 @@ public DiagnosticsNode(JsonObject json,
8178
@Override
8279
public boolean equals(Object other) {
8380
if (other instanceof DiagnosticsNode otherNode) {
84-
return getDartDiagnosticRef().equals(otherNode.getDartDiagnosticRef());
81+
return Objects.equals(getDartDiagnosticRef(), otherNode.getDartDiagnosticRef());
8582
}
8683
return false;
8784
}
@@ -653,10 +650,10 @@ public boolean identicalDisplay(DiagnosticsNode node) {
653650
}
654651
for (Map.Entry<String, JsonElement> entry : entries) {
655652
final String key = entry.getKey();
656-
if (key.equals("objectId") || key.equals("valueId")) {
653+
if (Objects.equals(key, "objectId") || key.equals("valueId")) {
657654
continue;
658655
}
659-
if (!entry.getValue().equals(node.json.get(key))) {
656+
if (!Objects.equals(entry.getValue(), node.json.get(key))) {
660657
return false;
661658
}
662659
}

flutter-idea/src/io/flutter/pub/PubRoot.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.io.File;
2727
import java.util.List;
2828
import java.util.Map;
29+
import java.util.Objects;
2930

3031
/**
3132
* A snapshot of the root directory of a pub package.
@@ -427,7 +428,7 @@ public boolean hasAndroidModule(Project project) {
427428

428429
for (Module module : ModuleManager.getInstance(project).getModules()) {
429430
for (VirtualFile contentRoot : ModuleRootManager.getInstance(module).getContentRoots()) {
430-
if (contentRoot.equals(androidDir)) {
431+
if (Objects.equals(contentRoot, androidDir)) {
431432
return true;
432433
}
433434
}

flutter-idea/src/io/flutter/run/bazel/FlutterBazelRunConfigurationType.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import io.flutter.utils.FlutterModuleUtils;
2121
import org.jetbrains.annotations.NotNull;
2222

23+
import java.util.Objects;
24+
2325
public class FlutterBazelRunConfigurationType extends ConfigurationTypeBase {
2426
@VisibleForTesting final Factory factory = new Factory(this);
2527

@@ -75,7 +77,7 @@ private boolean isNewlyGeneratedName(String name) {
7577
// This is a hack based on what the code does in RunConfigurable.createUniqueName().
7678
// If it fails to match, the new run config still works, just without any defaults set.
7779
final String baseName = ExecutionBundle.message("run.configuration.unnamed.name.prefix");
78-
return name.equals(baseName) || name.startsWith(baseName + " (");
80+
return Objects.equals(name, baseName) || name.startsWith(baseName + " (");
7981
}
8082

8183
@Override

flutter-idea/src/io/flutter/run/bazelTest/FlutterBazelTestConfigurationEditorForm.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import javax.swing.*;
2323
import java.awt.event.ActionEvent;
24+
import java.util.Objects;
2425

2526
import static io.flutter.run.bazelTest.BazelTestFields.Scope.*;
2627

@@ -163,7 +164,7 @@ private void render(Scope next) {
163164

164165
// Because the scope of the underlying fields is calculated based on which parameters are assigned,
165166
// we remove fields that aren't part of the selected scope.
166-
if (next.equals(Scope.TARGET_PATTERN)) {
167+
if (Objects.equals(next, Scope.TARGET_PATTERN)) {
167168
myTestName.setText("");
168169
myEntryFile.setText("");
169170
}

flutter-idea/src/io/flutter/run/common/RunMode.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import org.jetbrains.annotations.NonNls;
1414
import org.jetbrains.annotations.NotNull;
1515

16+
import java.util.Objects;
17+
1618
/**
1719
* The IntelliJ launch mode.
1820
*/
@@ -59,7 +61,7 @@ public static RunMode fromEnv(@NotNull ExecutionEnvironment env) throws Executio
5961
else if (DefaultDebugExecutor.EXECUTOR_ID.equals(mode)) {
6062
return DEBUG;
6163
}
62-
else if (COVERAGE.myModeString.equals(mode)) {
64+
else if (Objects.equals(COVERAGE.myModeString, mode)) {
6365
return COVERAGE;
6466
}
6567
else if (LaunchState.ANDROID_PROFILER_EXECUTOR_ID.equals(mode)) {

flutter-idea/src/io/flutter/run/daemon/DevToolsService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ private void tryParseStartupText(@NotNull String text) {
242242

243243
final JsonObject obj = element.getAsJsonObject();
244244

245-
if (JsonUtils.getStringMember(obj, "event").equals("server.started")) {
245+
if (Objects.equals(JsonUtils.getStringMember(obj, "event"), "server.started")) {
246246
final JsonObject params = obj.getAsJsonObject("params");
247247
final String host = JsonUtils.getStringMember(params, "host");
248248
final int port = JsonUtils.getIntMember(params, "port");

flutter-idea/src/io/flutter/run/daemon/DeviceDaemon.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,8 @@ public void onDeviceAdded(@NotNull DaemonEvent.DeviceAdded event) {
384384
final FlutterDevice newDevice = new FlutterDevice(event.id,
385385
event.emulatorId == null
386386
? (event.name == null ? event.id : event.name)
387-
: (event.platformType.equals("android") ? event.emulatorId : event.name),
387+
: (java.util.Objects.equals(event.platformType, "android")
388+
? event.emulatorId : event.name),
388389
event.platform,
389390
event.emulator,
390391
event.category,

flutter-idea/src/io/flutter/run/test/DartTestLocationProviderZ.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.ArrayList;
3434
import java.util.Collections;
3535
import java.util.List;
36+
import java.util.Objects;
3637

3738
public class DartTestLocationProviderZ implements SMTestLocator, DumbAware {
3839
@SuppressWarnings("rawtypes")
@@ -122,11 +123,11 @@ protected List<Location> getLocationByGroupAndTestNames(final PsiFile psiFile, f
122123
public boolean execute(@NotNull final PsiElement element) {
123124
if (element instanceof DartCallExpression expression) {
124125
if (isTest(expression) || TestUtil.isGroup(expression)) {
125-
if (nodes.get(nodes.size() - 1).equals(getTestLabel(expression))) {
126+
if (Objects.equals(nodes.get(nodes.size() - 1), getTestLabel(expression))) {
126127
boolean matches = true;
127128
for (int i = nodes.size() - 2; i >= 0 && matches; --i) {
128129
expression = getGroup(expression);
129-
if (expression == null || !nodes.get(i).equals(getTestLabel(expression))) {
130+
if (expression == null || !Objects.equals(nodes.get(i), getTestLabel(expression))) {
130131
matches = false;
131132
}
132133
}

flutter-idea/src/io/flutter/sdk/AndroidEmulatorManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ private void fireChangeEvent(final List<AndroidEmulator> newEmulators, final Lis
105105
if (project.isDisposed()) return;
106106

107107
// Don't fire if the list of devices is unchanged.
108-
if (cachedEmulators.equals(newEmulators)) {
108+
if (Objects.equals(cachedEmulators, newEmulators)) {
109109
return;
110110
}
111111

flutter-idea/src/io/flutter/sdk/FlutterSdk.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ public FlutterCommand flutterTest(@NotNull PubRoot root, @NotNull VirtualFile fi
365365

366366
// Starting the app paused so the IDE can catch early errors is ideal. However, we don't have a way to resume for multiple test files
367367
// yet, so we want to exclude directory scope tests from starting paused. See https://github.com/flutter/flutter-intellij/issues/4737.
368-
if (mode == RunMode.DEBUG || (mode == RunMode.RUN && !scope.equals(TestFields.Scope.DIRECTORY))) {
368+
if (mode == RunMode.DEBUG || (mode == RunMode.RUN && !Objects.equals(scope, TestFields.Scope.DIRECTORY))) {
369369
args.add("--start-paused");
370370
}
371371
if (FlutterSettings.getInstance().isVerboseLogging()) {

flutter-idea/src/io/flutter/sdk/FlutterSettingsConfigurable.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import javax.swing.text.JTextComponent;
4848
import java.awt.datatransfer.StringSelection;
4949
import java.util.List;
50+
import java.util.Objects;
5051
import java.util.concurrent.Semaphore;
5152

5253
// Note: when updating the settings here, update FlutterSearchableOptionContributor as well.
@@ -221,7 +222,7 @@ public boolean isModified() {
221222
return true;
222223
}
223224

224-
if (!settings.getFontPackages().equals(myFontPackagesTextArea.getText())) {
225+
if (!Objects.equals(settings.getFontPackages(), myFontPackagesTextArea.getText())) {
225226
return true;
226227
}
227228

flutter-idea/src/io/flutter/test/DartTestEventsConverterZ.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@
1818

1919
import java.lang.reflect.Type;
2020
import java.text.ParseException;
21-
import java.util.ArrayList;
22-
import java.util.HashMap;
23-
import java.util.List;
24-
import java.util.Map;
21+
import java.util.*;
2522
import java.util.regex.Matcher;
2623
import java.util.regex.Pattern;
2724

@@ -246,9 +243,9 @@ private static boolean shouldTestBeHiddenIfPassed(@NotNull final Test test) {
246243
final Group group = test.getParent();
247244
return group == null && (test.getName().startsWith(LOADING_PREFIX) || test.getName().startsWith(COMPILING_PREFIX))
248245
||
249-
group != null && group.getDoneTestsCount() == 0 && test.getBaseName().equals(SET_UP_ALL_VIRTUAL_TEST_NAME)
246+
group != null && group.getDoneTestsCount() == 0 && Objects.equals(test.getBaseName(), SET_UP_ALL_VIRTUAL_TEST_NAME)
250247
||
251-
group != null && group.getDoneTestsCount() > 0 && test.getBaseName().equals(TEAR_DOWN_ALL_VIRTUAL_TEST_NAME);
248+
group != null && group.getDoneTestsCount() > 0 && Objects.equals(test.getBaseName(), TEAR_DOWN_ALL_VIRTUAL_TEST_NAME);
252249
}
253250

254251
private boolean handleTestDone(JsonObject obj) throws ParseException {
@@ -381,7 +378,7 @@ private boolean handlePrint(JsonObject obj) throws ParseException {
381378
boolean result = true;
382379

383380
if (!test.myTestStartReported) {
384-
if (test.getBaseName().equals(SET_UP_ALL_VIRTUAL_TEST_NAME) || test.getBaseName().equals(TEAR_DOWN_ALL_VIRTUAL_TEST_NAME)) {
381+
if (Objects.equals(test.getBaseName(), SET_UP_ALL_VIRTUAL_TEST_NAME) || Objects.equals(test.getBaseName(), TEAR_DOWN_ALL_VIRTUAL_TEST_NAME)) {
385382
return true; // output in successfully passing setUpAll/tearDownAll is not important enough to make these nodes visible
386383
}
387384

flutter-idea/src/io/flutter/view/EmbeddedBrowser.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@
2525

2626
import javax.swing.*;
2727
import java.awt.*;
28-
import java.util.ArrayList;
29-
import java.util.HashMap;
28+
import java.util.*;
3029
import java.util.List;
31-
import java.util.Map;
3230
import java.util.concurrent.CompletableFuture;
3331
import java.util.concurrent.atomic.AtomicBoolean;
3432
import java.util.function.Consumer;
@@ -126,7 +124,7 @@ public void openPanel(ToolWindow toolWindow, String tabName, DevToolsUrl devTool
126124
tab.contentManager.removeAllContents(false);
127125

128126
for (final String otherTabName : tabs.keySet()) {
129-
if (otherTabName.equals(tabName)) {
127+
if (Objects.equals(otherTabName, tabName)) {
130128
continue;
131129
}
132130
final BrowserTab browserTab = tabs.get(otherTabName);

0 commit comments

Comments
 (0)