53
53
import static java .util .Arrays .asList ;
54
54
55
55
public class FlutterSdk {
56
- public static final String FLUTTER_SDK_GLOBAL_LIB_NAME = "Flutter SDK" ;
56
+ public static final @ NotNull String FLUTTER_SDK_GLOBAL_LIB_NAME = "Flutter SDK" ;
57
57
58
- public static final String DART_SDK_SUFFIX = "/bin/cache/dart-sdk" ;
59
- public static final String LINUX_DART_SUFFIX = "/google-dartlang" ;
60
- public static final String LOCAL_DART_SUFFIX = "/google-dartlang-local" ;
61
- public static final String MAC_DART_SUFFIX = "/dart_lang/macos_sdk" ;
58
+ public static final @ NotNull String DART_SDK_SUFFIX = "/bin/cache/dart-sdk" ;
59
+ public static final @ NotNull String LINUX_DART_SUFFIX = "/google-dartlang" ;
60
+ public static final @ NotNull String LOCAL_DART_SUFFIX = "/google-dartlang-local" ;
61
+ public static final @ NotNull String MAC_DART_SUFFIX = "/dart_lang/macos_sdk" ;
62
62
63
- private static final String DART_CORE_SUFFIX = DART_SDK_SUFFIX + "/lib/core" ;
63
+ private static final @ NotNull String DART_CORE_SUFFIX = DART_SDK_SUFFIX + "/lib/core" ;
64
64
65
- private static final Logger LOG = Logger .getInstance (FlutterSdk .class );
65
+ private static final @ NotNull Logger LOG = Logger .getInstance (FlutterSdk .class );
66
66
67
- private static final Map <String , FlutterSdk > projectSdkCache = new HashMap <>();
67
+ private static final @ NotNull Map <String , FlutterSdk > projectSdkCache = new HashMap <>();
68
68
69
69
private final @ NotNull VirtualFile myHome ;
70
70
private final @ NotNull FlutterSdkVersion myVersion ;
71
- private final Map <String , String > cachedConfigValues = new HashMap <>();
71
+ private final @ NotNull Map <String , String > cachedConfigValues = new HashMap <>();
72
72
73
73
private FlutterSdk (@ NotNull final VirtualFile home , @ NotNull final FlutterSdkVersion version ) {
74
74
myHome = home ;
@@ -146,32 +146,35 @@ public static FlutterSdk forPath(@NotNull final String path) {
146
146
}
147
147
148
148
@ NotNull
149
- private static FlutterSdk saveSdkInCache (VirtualFile home ) {
149
+ private static FlutterSdk saveSdkInCache (@ NotNull VirtualFile home ) {
150
150
final String cacheKey = home .getCanonicalPath ();
151
151
synchronized (projectSdkCache ) {
152
152
if (!projectSdkCache .containsKey (cacheKey )) {
153
153
projectSdkCache .put (cacheKey , new FlutterSdk (home , FlutterSdkVersion .readFromSdk (home )));
154
154
}
155
155
}
156
- return projectSdkCache .get (cacheKey );
156
+ return Objects . requireNonNull ( projectSdkCache .get (cacheKey ) );
157
157
}
158
158
159
159
@ Nullable
160
160
private static Library getDartSdkLibrary (@ NotNull Project project ) {
161
- final LibraryTable libraryTable = LibraryTablesRegistrar .getInstance ().getLibraryTable (project );
161
+ LibraryTablesRegistrar registrar = LibraryTablesRegistrar .getInstance ();
162
+ if (registrar == null ) return null ;
163
+ final LibraryTable libraryTable = registrar .getLibraryTable (project );
162
164
for (Library lib : libraryTable .getLibraries ()) {
163
- if ("Dart SDK" .equals (lib .getName ())) {
165
+ if (lib != null && "Dart SDK" .equals (lib .getName ())) {
164
166
return lib ;
165
167
}
166
168
}
167
169
return null ;
168
170
}
169
171
170
172
@ Nullable
171
- private static FlutterSdk getFlutterFromDartSdkLibrary (Library lib ) {
173
+ private static FlutterSdk getFlutterFromDartSdkLibrary (@ NotNull Library lib ) {
174
+ assert OrderRootType .CLASSES != null ;
172
175
final String [] urls = lib .getUrls (OrderRootType .CLASSES );
173
176
for (String url : urls ) {
174
- if (url .endsWith (DART_CORE_SUFFIX )) {
177
+ if (url != null && url .endsWith (DART_CORE_SUFFIX )) {
175
178
final String flutterUrl = url .substring (0 , url .length () - DART_CORE_SUFFIX .length ());
176
179
final VirtualFile home = VirtualFileManager .getInstance ().findFileByUrl (flutterUrl );
177
180
return home == null ? null : saveSdkInCache (home );
@@ -180,6 +183,7 @@ private static FlutterSdk getFlutterFromDartSdkLibrary(Library lib) {
180
183
return null ;
181
184
}
182
185
186
+ @ NotNull
183
187
public FlutterCommand flutterVersion () {
184
188
// TODO(devoncarew): Switch to calling 'flutter --version --machine'. The ouput will look like:
185
189
// Building flutter tool...
@@ -196,19 +200,23 @@ public FlutterCommand flutterVersion() {
196
200
return new FlutterCommand (this , getHome (), FlutterCommand .Type .VERSION );
197
201
}
198
202
203
+ @ NotNull
199
204
public FlutterCommand flutterUpgrade () {
200
205
return new FlutterCommand (this , getHome (), FlutterCommand .Type .UPGRADE );
201
206
}
202
207
208
+ @ NotNull
203
209
public FlutterCommand flutterClean (@ NotNull PubRoot root ) {
204
210
return new FlutterCommand (this , root .getRoot (), FlutterCommand .Type .CLEAN );
205
211
}
206
212
213
+ @ NotNull
207
214
public FlutterCommand flutterDoctor () {
208
215
return new FlutterCommand (this , getHome (), FlutterCommand .Type .DOCTOR );
209
216
}
210
217
211
218
@ NonNls
219
+ @ NotNull
212
220
public FlutterCommand flutterCreate (@ NotNull VirtualFile appDir , @ Nullable FlutterCreateAdditionalSettings additionalSettings ) {
213
221
final List <String > args = new ArrayList <>();
214
222
if (additionalSettings != null ) {
@@ -236,39 +244,48 @@ public FlutterCommand flutterCreate(@NotNull VirtualFile appDir, @Nullable Flutt
236
244
return new FlutterCommand (this , appDir .getParent (), FlutterCommand .Type .CREATE , vargs );
237
245
}
238
246
247
+ @ NotNull
239
248
public FlutterCommand flutterPackagesAdd (@ NotNull PubRoot root , @ NotNull String name , boolean devOnly ) {
240
249
if (devOnly ) name = "dev:" + name ;
241
250
return new FlutterCommand (this , root .getRoot (), FlutterCommand .Type .PUB_ADD , name );
242
251
}
243
252
253
+ @ NotNull
244
254
public FlutterCommand flutterPackagesGet (@ NotNull PubRoot root ) {
245
255
return new FlutterCommand (this , root .getRoot (), FlutterCommand .Type .PUB_GET );
246
256
}
247
257
258
+ @ NotNull
248
259
public FlutterCommand flutterPackagesUpgrade (@ NotNull PubRoot root ) {
249
260
return new FlutterCommand (this , root .getRoot (), FlutterCommand .Type .PUB_UPGRADE );
250
261
}
251
262
263
+ @ NotNull
252
264
public FlutterCommand flutterPackagesOutdated (@ NotNull PubRoot root ) {
253
265
return new FlutterCommand (this , root .getRoot (), FlutterCommand .Type .PUB_OUTDATED );
254
266
}
255
267
268
+ @ NotNull
256
269
public FlutterCommand flutterPub (@ Nullable PubRoot root , String ... args ) {
257
270
return new FlutterCommand (this , root == null ? null : root .getRoot (), FlutterCommand .Type .PUB , args );
258
271
}
259
272
273
+ @ NotNull
260
274
public FlutterCommand flutterBuild (@ NotNull PubRoot root , String ... additionalArgs ) {
261
275
return new FlutterCommand (this , root .getRoot (), FlutterCommand .Type .BUILD , additionalArgs );
262
276
}
263
277
278
+ @ NotNull
264
279
public FlutterCommand flutterConfig (String ... additionalArgs ) {
265
280
return new FlutterCommand (this , getHome (), FlutterCommand .Type .CONFIG , additionalArgs );
266
281
}
267
282
283
+ @ NotNull
268
284
public FlutterCommand flutterChannel () {
269
285
return new FlutterCommand (this , getHome (), FlutterCommand .Type .CHANNEL );
270
286
}
271
287
288
+ @ NotNull
272
289
public FlutterCommand flutterRun (@ NotNull PubRoot root ,
273
290
@ NotNull VirtualFile main ,
274
291
@ NotNull FlutterDevice device ,
@@ -278,7 +295,8 @@ public FlutterCommand flutterRun(@NotNull PubRoot root,
278
295
String ... additionalArgs ) {
279
296
final List <String > args = new ArrayList <>();
280
297
args .add ("--machine" );
281
- if (FlutterSettings .getInstance ().isVerboseLogging ()) {
298
+ FlutterSettings settings = FlutterSettings .getInstance ();
299
+ if (settings != null && settings .isVerboseLogging ()) {
282
300
args .add ("--verbose" );
283
301
}
284
302
@@ -316,6 +334,7 @@ else if (flutterLaunchMode == FlutterLaunchMode.RELEASE) {
316
334
return new FlutterCommand (this , root .getRoot (), FlutterCommand .Type .RUN , args .toArray (new String []{ }));
317
335
}
318
336
337
+ @ NotNull
319
338
public FlutterCommand flutterAttach (@ NotNull PubRoot root , @ NotNull VirtualFile main , @ Nullable FlutterDevice device ,
320
339
@ NotNull FlutterLaunchMode flutterLaunchMode , String ... additionalArgs ) {
321
340
final List <String > args = new ArrayList <>();
@@ -349,6 +368,7 @@ else if (flutterLaunchMode == FlutterLaunchMode.RELEASE) {
349
368
return new FlutterCommand (this , root .getRoot (), FlutterCommand .Type .ATTACH , args .toArray (new String []{ }));
350
369
}
351
370
371
+ @ NotNull
352
372
public FlutterCommand flutterRunOnTester (@ NotNull PubRoot root , @ NotNull String mainPath ) {
353
373
final List <String > args = new ArrayList <>();
354
374
args .add ("--machine" );
@@ -357,6 +377,7 @@ public FlutterCommand flutterRunOnTester(@NotNull PubRoot root, @NotNull String
357
377
return new FlutterCommand (this , root .getRoot (), FlutterCommand .Type .RUN , args .toArray (new String []{ }));
358
378
}
359
379
380
+ @ NotNull
360
381
public FlutterCommand flutterTest (@ NotNull PubRoot root , @ NotNull VirtualFile fileOrDir , @ Nullable String testNameSubstring ,
361
382
@ NotNull RunMode mode , @ Nullable String additionalArgs , TestFields .Scope scope , boolean useRegexp ) {
362
383
@@ -453,6 +474,7 @@ public PubRoot createFiles(@NotNull VirtualFile baseDir, @Nullable Module module
453
474
* <p>
454
475
* Returns the process if successfully started.
455
476
*/
477
+ @ Nullable
456
478
public Process startPubAdd (@ NotNull PubRoot root , @ NotNull Project project , @ NotNull String pkg , boolean devOnly ) {
457
479
final Module module = root .getModule (project );
458
480
if (module == null ) return null ;
@@ -469,6 +491,7 @@ public Process startPubAdd(@NotNull PubRoot root, @NotNull Project project, @Not
469
491
* <p>
470
492
* Returns the process if successfully started.
471
493
*/
494
+ @ Nullable
472
495
public Process startPubGet (@ NotNull PubRoot root , @ NotNull Project project ) {
473
496
final Module module = root .getModule (project );
474
497
if (module == null ) return null ;
@@ -485,6 +508,7 @@ public Process startPubGet(@NotNull PubRoot root, @NotNull Project project) {
485
508
* <p>
486
509
* Returns the process if successfully started.
487
510
*/
511
+ @ Nullable
488
512
public Process startPubUpgrade (@ NotNull PubRoot root , @ NotNull Project project ) {
489
513
final Module module = root .getModule (project );
490
514
if (module == null ) return null ;
@@ -500,6 +524,7 @@ public Process startPubUpgrade(@NotNull PubRoot root, @NotNull Project project)
500
524
* <p>
501
525
* Returns the process if successfully started.
502
526
*/
527
+ @ Nullable
503
528
public Process startPubOutdated (@ NotNull PubRoot root , @ NotNull Project project ) {
504
529
final Module module = root .getModule (project );
505
530
if (module == null ) return null ;
@@ -565,8 +590,9 @@ public FlutterSdkChannel queryFlutterChannel(boolean useCachedValue) {
565
590
return FlutterSdkChannel .fromText (branch );
566
591
}
567
592
593
+ @ NotNull
568
594
@ NonNls
569
- private static final String [] PLATFORMS =
595
+ private static final String @ NotNull [] PLATFORMS =
570
596
new String []{"enable-android" , "enable-ios" , "enable-web" , "enable-linux-desktop" , "enable-macos-desktop" , "enable-windows-desktop" };
571
597
572
598
@ NotNull
@@ -599,12 +625,14 @@ else if ("false".equals(value)) {
599
625
}
600
626
try {
601
627
final JsonElement elem = JsonUtils .parseString (stdout .substring (startJsonIndex ));
602
- if (elem .isJsonNull ()) {
628
+ if (elem == null || elem .isJsonNull ()) {
603
629
FlutterUtils .warn (LOG , FlutterBundle .message ("flutter.sdk.invalid.json.error" ));
604
630
return platforms ;
605
631
}
606
632
607
633
final JsonObject obj = elem .getAsJsonObject ();
634
+ if (obj == null ) return platforms ;
635
+
608
636
for (String key : PLATFORMS ) {
609
637
final JsonPrimitive primitive = obj .getAsJsonPrimitive (key );
610
638
if (primitive != null ) {
@@ -636,12 +664,14 @@ public String queryFlutterConfig(String key, boolean useCachedValue) {
636
664
if (stdout != null ) {
637
665
try {
638
666
final JsonElement elem = JsonUtils .parseString (stdout .substring (stdout .indexOf ('{' )));
639
- if (elem .isJsonNull ()) {
667
+ if (elem == null || elem .isJsonNull ()) {
640
668
FlutterUtils .warn (LOG , FlutterBundle .message ("flutter.sdk.invalid.json.error" ));
641
669
return null ;
642
670
}
643
671
644
672
final JsonObject obj = elem .getAsJsonObject ();
673
+ if (obj == null ) return null ;
674
+
645
675
for (String jsonKey : JsonUtils .getKeySet (obj )) {
646
676
final JsonElement element = obj .get (jsonKey );
647
677
if (element == null || element .isJsonNull ()) {
0 commit comments