Skip to content

Commit db377ef

Browse files
authored
[CQ] fix nullability problems for flutter/android (#8295)
Fix nullability problems in `src/io/flutter/android/`. See #8291. If we pursue #8292, we could mark `src/io/flutter/android/` as null-"clean". --- - [x] I’ve reviewed the contributor guide and applied the relevant portions to this PR. <details> <summary>Contribution guidelines:</summary><br> - See our [contributor guide]([https://github.com/dart-lang/sdk/blob/main/CONTRIBUTING.md](https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview) for general expectations for PRs. - Larger or significant changes should be discussed in an issue before creating a PR. - Dart contributions to our repos should follow the [Dart style guide](https://dart.dev/guides/language/effective-dart) and use `dart format`. - Java and Kotlin contributions should strive to follow Java and Kotlin best practices ([discussion](#8098)). </details>
1 parent 9c7fa7e commit db377ef

File tree

4 files changed

+26
-13
lines changed

4 files changed

+26
-13
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class FlutterMessages {
2323
private FlutterMessages() {
2424
}
2525

26-
public static void showError(String title, String message, @Nullable Project project) {
26+
public static void showError(String title, @NotNull String message, @Nullable Project project) {
2727
Notifications.Bus.notify(
2828
new Notification(FLUTTER_NOTIFICATION_GROUP_ID,
2929
title,

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import java.lang.reflect.Method;
2828

2929
public class AndroidEmulator {
30-
30+
3131
@NotNull final AndroidSdk androidSdk;
3232
@NotNull final String id;
3333
ProcessAdapter listener;
@@ -86,7 +86,9 @@ public void onTextAvailable(@NotNull ProcessEvent event, @NotNull Key outputType
8686
}
8787

8888
public void processTerminated(@NotNull ProcessEvent event) {
89-
process.removeProcessListener(listener);
89+
if (listener != null) {
90+
process.removeProcessListener(listener);
91+
}
9092
final int exitCode = event.getExitCode();
9193
if (exitCode != 0) {
9294
final String message = stdout.isEmpty()
@@ -100,6 +102,7 @@ public void processTerminated(@NotNull ProcessEvent event) {
100102
process.startNotify();
101103
}
102104
catch (ExecutionException | RuntimeException e) {
105+
//noinspection DataFlowIssue
103106
FlutterMessages.showError("Error Opening Emulator", e.toString(), androidSdk.project);
104107
}
105108
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public void onTextAvailable(@NotNull ProcessEvent event, @NotNull Key outputType
110110
}
111111

112112
final Integer exitCode = process.getExitCode();
113-
if (exitCode == null || process.getExitCode() != 0) {
113+
if (exitCode == null || exitCode != 0) {
114114
return Collections.emptyList();
115115
}
116116

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

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,25 @@ public VirtualFile getHome() {
4949
* Changes the project's Java SDK to this one.
5050
*/
5151
public void setCurrent(@NotNull Project project) {
52-
assert ApplicationManager.getApplication().isWriteAccessAllowed();
52+
var application = ApplicationManager.getApplication();
53+
assert application != null;
54+
assert application.isWriteAccessAllowed();
5355

5456
final ProjectRootManager roots = ProjectRootManager.getInstance(project);
55-
roots.setProjectSdk(sdk);
57+
if (roots != null) {
58+
roots.setProjectSdk(sdk);
59+
}
5660
}
5761

5862
/**
5963
* Returns the Java SDK in the project's configuration, or null if not an Android SDK.
6064
*/
6165
@Nullable
6266
public static IntelliJAndroidSdk fromProject(@NotNull Project project) {
63-
final Sdk candidate = ProjectRootManager.getInstance(project).getProjectSdk();
67+
var manager = ProjectRootManager.getInstance(project);
68+
if (manager == null) return null;
69+
70+
final Sdk candidate = manager.getProjectSdk();
6471
return fromSdk(candidate);
6572
}
6673

@@ -89,7 +96,7 @@ public static IntelliJAndroidSdk fromEnvironment() {
8996
@Nullable
9097
public static IntelliJAndroidSdk fromHome(VirtualFile file) {
9198
for (IntelliJAndroidSdk candidate : findAll()) {
92-
if (Objects.equals(file, candidate.getHome())) {
99+
if (candidate != null && Objects.equals(file, candidate.getHome())) {
93100
return candidate;
94101
}
95102
}
@@ -100,7 +107,7 @@ public static IntelliJAndroidSdk fromHome(VirtualFile file) {
100107
/**
101108
* Returns the best value of the Android SDK location to use, including possibly querying flutter tools for it.
102109
*/
103-
public static String chooseAndroidHome(@Nullable Project project, boolean askFlutterTools) {
110+
public static @Nullable String chooseAndroidHome(@Nullable Project project, boolean askFlutterTools) {
104111
if (project == null) {
105112
return EnvironmentUtil.getValue("ANDROID_HOME");
106113
}
@@ -130,10 +137,13 @@ public static String chooseAndroidHome(@Nullable Project project, boolean askFlu
130137
@NotNull
131138
private static List<IntelliJAndroidSdk> findAll() {
132139
final List<IntelliJAndroidSdk> result = new ArrayList<>();
133-
for (Sdk sdk : ProjectJdkTable.getInstance().getAllJdks()) {
134-
final IntelliJAndroidSdk candidate = IntelliJAndroidSdk.fromSdk(sdk);
135-
if (candidate != null) {
136-
result.add(candidate);
140+
var jdkTable = ProjectJdkTable.getInstance();
141+
if (jdkTable != null) {
142+
for (Sdk sdk : jdkTable.getAllJdks()) {
143+
final IntelliJAndroidSdk candidate = IntelliJAndroidSdk.fromSdk(sdk);
144+
if (candidate != null) {
145+
result.add(candidate);
146+
}
137147
}
138148
}
139149
return result;

0 commit comments

Comments
 (0)