Skip to content

Commit 4a5eb90

Browse files
authored
[CQ] fix nullability problems for flutter/console (#8299)
Fix nullability problems in `src/io/flutter/console/`. See #8291. If we pursue #8292, we could mark `src/io/flutter/console/` 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 0661aa4 commit 4a5eb90

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ private FlutterConsole(@NotNull ConsoleView view, @NotNull Content content, @Not
4747

4848
@NotNull
4949
static FlutterConsole create(@NotNull Project project, @Nullable Module module) {
50-
final TextConsoleBuilder builder = TextConsoleBuilderFactory.getInstance().createBuilder(project);
50+
var builderFactory = TextConsoleBuilderFactory.getInstance();
51+
assert builderFactory != null;
52+
final TextConsoleBuilder builder = builderFactory.createBuilder(project);
5153
builder.setViewer(true);
5254
if (module != null) {
5355
builder.addFilter(new FlutterConsoleFilter(module));
@@ -58,7 +60,9 @@ static FlutterConsole create(@NotNull Project project, @Nullable Module module)
5860
panel.setContent(view.getComponent());
5961

6062
final String title = module != null ? "[" + module.getName() + "] Flutter" : "Flutter";
61-
final Content content = ContentFactory.getInstance().createContent(panel.getComponent(), title, true);
63+
ContentFactory contentFactory = ContentFactory.getInstance();
64+
assert contentFactory != null;
65+
final Content content = contentFactory.createContent(panel.getComponent(), title, true);
6266
Disposer.register(content, view);
6367

6468
return new FlutterConsole(view, content, project, module);
@@ -79,7 +83,7 @@ void watchProcess(@NotNull ColoredProcessHandler process) {
7983
// Print exit code.
8084
final ProcessAdapter listener = new ProcessAdapter() {
8185
@Override
82-
public void processTerminated(final ProcessEvent event) {
86+
public void processTerminated(final @NotNull ProcessEvent event) {
8387
view.print(
8488
"Process finished with exit code " + event.getExitCode(),
8589
ConsoleViewContentType.SYSTEM_OUTPUT);

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

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@
4343
*/
4444
public class FlutterConsoleFilter implements Filter {
4545
private static class OpenExternalFileHyperlink implements HyperlinkInfo {
46-
private final String myPath;
46+
private final @NotNull String myPath;
4747

48-
OpenExternalFileHyperlink(VirtualFile file) {
48+
OpenExternalFileHyperlink(@NotNull VirtualFile file) {
4949
myPath = file.getPath();
5050
}
5151

@@ -87,12 +87,13 @@ public VirtualFile fileAtPath(@NotNull String pathPart) {
8787

8888
// We require the pathPart reference to be a file reference, otherwise we'd match things like
8989
// "Build: Running build completed, took 191ms".
90-
if (pathPart.indexOf('.') == -1) {
90+
if (pathPart == null || pathPart.indexOf('.') == -1) {
9191
return null;
9292
}
9393

9494
final VirtualFile[] roots = OpenApiUtils.getContentRoots(module);
9595
for (VirtualFile root : roots) {
96+
if (root == null) continue;
9697
if (!pathPart.isEmpty()) {
9798
final String baseDirPath = root.getPath();
9899
final String path = baseDirPath + "/" + pathPart;
@@ -112,14 +113,14 @@ public VirtualFile fileAtPath(@NotNull String pathPart) {
112113
return null;
113114
}
114115

115-
private static VirtualFile findFile(final String path) {
116+
private static @Nullable VirtualFile findFile(final @NotNull String path) {
116117
final VirtualFile file = LocalFileSystem.getInstance().findFileByPath(path);
117118
return file != null && file.exists() ? file : null;
118119
}
119120

120121
@Override
121122
@Nullable
122-
public Result applyFilter(final String line, final int entireLength) {
123+
public Result applyFilter(final @NotNull String line, final int entireLength) {
123124
if (line.startsWith("Run \"flutter doctor\" for information about installing additional components.")) {
124125
return getFlutterDoctorResult(line, entireLength - line.length());
125126
}
@@ -141,10 +142,12 @@ public Result applyFilter(final String line, final int entireLength) {
141142
final String[] parts = pathPart.split(" ");
142143
if (parts.length > 1) {
143144
pathPart = parts[1];
144-
file = fileAtPath(pathPart);
145-
if (file != null) {
146-
lineStart = entireLength - line.length() + line.indexOf(pathPart);
147-
highlightLength = pathPart.length();
145+
if (pathPart != null) {
146+
file = fileAtPath(pathPart);
147+
if (file != null) {
148+
lineStart = entireLength - line.length() + line.indexOf(pathPart);
149+
highlightLength = pathPart.length();
150+
}
148151
}
149152
}
150153
}
@@ -155,12 +158,13 @@ public Result applyFilter(final String line, final int entireLength) {
155158
final String[] parts = pathPart.split(" ");
156159
for (String part : parts) {
157160
// "(lib/main.dart:49)"
158-
if (part.startsWith("(") && part.endsWith(")")) {
161+
if (part != null && part.startsWith("(") && part.endsWith(")")) {
159162
part = part.substring(1, part.length() - 1);
160163
final String[] split = part.split(":");
161164
if (split.length == 2) {
162165
try {
163166
// Reconcile line number indexing.
167+
//noinspection DataFlowIssue
164168
lineNumber = Math.max(0, Integer.parseInt(split[1]) - 1);
165169
}
166170
catch (NumberFormatException e) {
@@ -175,6 +179,7 @@ else if (split.length == 4 && Objects.equals(split[0], "file")) {
175179
// part = file:///Users/user/AndroidStudioProjects/flutter_app/test/widget_test.dart:23:18
176180
try {
177181
// Reconcile line number indexing.
182+
//noinspection DataFlowIssue
178183
lineNumber = Math.max(0, Integer.parseInt(split[2]) - 1);
179184
}
180185
catch (NumberFormatException e) {
@@ -200,13 +205,15 @@ else if (split.length == 4 && Objects.equals(split[0], "file")) {
200205
if (found) {
201206
final String filePathExpr = "((?:[^/]*/)*)(.*)";
202207
final Pattern pathPattern = Pattern.compile(filePathExpr);
208+
//noinspection DataFlowIssue
203209
final Matcher pathMatcher = pathPattern.matcher(matcher.group(1));
204210
if (pathMatcher.find()) {
205211
final String path = pathMatcher.group(1) + pathMatcher.group(2);
206212
file = fileAtPath(path);
207213
if (file == null) {
208214
return null;
209215
}
216+
//noinspection DataFlowIssue
210217
lineNumber = Integer.parseInt(matcher.group(2));
211218
lineStart = entireLength - line.length();
212219
highlightLength = path.length();
@@ -236,9 +243,11 @@ else if (split.length == 4 && Objects.equals(split[0], "file")) {
236243
return null;
237244
}
238245

239-
private String findRelativePath(String threeSlashFileName) {
246+
private @Nullable String findRelativePath(@Nullable String threeSlashFileName) {
247+
if (threeSlashFileName == null) return null;
240248
final VirtualFile[] roots = OpenApiUtils.getContentRoots(module);
241249
for (VirtualFile root : roots) {
250+
if (root == null) continue;
242251
final String path = root.getPath();
243252
int index = threeSlashFileName.indexOf(path);
244253
if (index > 0) {
@@ -249,7 +258,7 @@ private String findRelativePath(String threeSlashFileName) {
249258
return null;
250259
}
251260

252-
private static Result getFlutterDoctorResult(final String line, final int lineStart) {
261+
private static @NotNull Result getFlutterDoctorResult(final @NotNull String line, final int lineStart) {
253262
final int commandStart = line.indexOf('"') + 1;
254263
final int startOffset = lineStart + commandStart;
255264
final int commandLength = "flutter doctor".length();

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public static void displayMessage(@NotNull Project project, @Nullable Module mod
7070
@NotNull
7171
static FlutterConsole findOrCreate(@NotNull Project project, @Nullable Module module) {
7272
for (Content content : MessageView.getInstance(project).getContentManager().getContents()) {
73+
if (content == null) continue;
7374
final FlutterConsole console = content.getUserData(KEY);
7475
if (console != null && console.module == module) {
7576
assert (project == console.project);

0 commit comments

Comments
 (0)