Skip to content

Commit cbb360d

Browse files
authored
Resolve diagnostics in sdk version file (#7701)
1 parent da4eed8 commit cbb360d

File tree

1 file changed

+63
-49
lines changed

1 file changed

+63
-49
lines changed

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

Lines changed: 63 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,20 @@
1313

1414
import java.io.IOException;
1515
import java.nio.charset.StandardCharsets;
16+
import java.util.Objects;
1617

17-
public class FlutterSdkVersion implements Comparable<FlutterSdkVersion> {
18+
public final class FlutterSdkVersion implements Comparable<FlutterSdkVersion> {
1819
/**
1920
* The version for which the distributed icons can be used.
2021
*/
2122
@VisibleForTesting
23+
@NotNull
2224
public static final FlutterSdkVersion DISTRIBUTED_ICONS = new FlutterSdkVersion("3.1.0");
25+
2326
/**
2427
* The minimum version we suggest people use.
2528
*/
29+
@NotNull
2630
private static final FlutterSdkVersion MIN_SUPPORTED_SDK = new FlutterSdkVersion("0.0.12");
2731

2832
/**
@@ -31,31 +35,37 @@ public class FlutterSdkVersion implements Comparable<FlutterSdkVersion> {
3135
* Before this version there were issues if you ran the app from the command
3236
* line without the flag after running
3337
*/
38+
@NotNull
3439
private static final FlutterSdkVersion MIN_SAFE_TRACK_WIDGET_CREATION_SDK = new FlutterSdkVersion("0.10.2");
3540

3641
/**
3742
* The version that supports --dart-define in the run command.
3843
*/
44+
@NotNull
3945
private static final FlutterSdkVersion MIN_DART_DEFINE_SDK = new FlutterSdkVersion("1.12.0");
4046

4147
/**
4248
* The version of the stable channel that supports --androidx in the create command.
4349
*/
50+
@NotNull
4451
private static final FlutterSdkVersion MIN_PUB_OUTDATED_SDK = new FlutterSdkVersion("1.16.4");
4552

4653
/**
4754
* The version that supports --platform in flutter create.
4855
*/
56+
@NotNull
4957
private static final FlutterSdkVersion MIN_CREATE_PLATFORMS_SDK = new FlutterSdkVersion("1.20.0");
5058

5159
/**
5260
* The version that supports --config-only when configuring Xcode for opening a project.
5361
*/
62+
@NotNull
5463
private static final FlutterSdkVersion MIN_XCODE_CONFIG_ONLY = new FlutterSdkVersion("1.22.0-12.0.pre");
5564

5665
/**
5766
* The last version of stable that does not support --platform in flutter create.
5867
*/
68+
@NotNull
5969
private static final FlutterSdkVersion MAX_STABLE_NO_PLATFORMS_SDK = new FlutterSdkVersion("1.22.6");
6070

6171
/**
@@ -75,16 +85,19 @@ public class FlutterSdkVersion implements Comparable<FlutterSdkVersion> {
7585
/**
7686
* The version that includes the skeleton template.
7787
*/
88+
@NotNull
7889
private static final FlutterSdkVersion MIN_SKELETON_TEMPLATE = new FlutterSdkVersion("2.5.0");
7990

8091
/**
8192
* The version that includes the skeleton template.
8293
*/
94+
@NotNull
8395
private static final FlutterSdkVersion MIN_PLUGIN_FFI_TEMPLATE = new FlutterSdkVersion("3.0.0");
8496

8597
/**
8698
* The version that includes the skeleton template.
8799
*/
100+
@NotNull
88101
private static final FlutterSdkVersion MIN_EMPTY_PROJECT = new FlutterSdkVersion("3.6.0-0.1.pre");
89102

90103
/**
@@ -127,29 +140,29 @@ public class FlutterSdkVersion implements Comparable<FlutterSdkVersion> {
127140
private final String versionText;
128141
@Nullable
129142
private final Version betaVersion;
130-
private final int masterVersion;
143+
private final int mainVersion;
131144

132145
@VisibleForTesting
133146
public FlutterSdkVersion(@Nullable String versionString) {
134147
versionText = versionString;
135148
if (versionString != null) {
136-
final String[] split = versionString.split("-");
149+
final @NotNull String[] split = versionString.split("-");
137150
version = Version.parseVersion(split[0]);
138151

139152
if (split.length > 1) {
140153
betaVersion = Version.parseVersion(split[1]);
141-
final String[] parts = split[1].split("\\.");
142-
masterVersion = parts.length > 3 ? Integer.parseInt(parts[3]) : 0;
154+
final @NotNull String[] parts = split[1].split("\\.");
155+
mainVersion = parts.length > 3 ? Integer.parseInt(parts[3]) : 0;
143156
}
144157
else {
145158
betaVersion = null;
146-
masterVersion = 0;
159+
mainVersion = 0;
147160
}
148161
}
149162
else {
150163
version = null;
151164
betaVersion = null;
152-
masterVersion = 0;
165+
mainVersion = 0;
153166
}
154167
}
155168

@@ -172,7 +185,8 @@ private static FlutterSdkVersion readFromFile(@Nullable VirtualFile file) {
172185
return new FlutterSdkVersion(versionString);
173186
}
174187

175-
private static String readVersionString(VirtualFile file) {
188+
@Nullable
189+
private static String readVersionString(@NotNull VirtualFile file) {
176190
try {
177191
final String data = new String(file.contentsToByteArray(), StandardCharsets.UTF_8);
178192
for (String line : data.split("\n")) {
@@ -191,42 +205,40 @@ private static String readVersionString(VirtualFile file) {
191205
}
192206
}
193207

208+
private boolean supportsVersion(@NotNull FlutterSdkVersion otherVersion) {
209+
return this.compareTo(otherVersion) >= 0;
210+
}
211+
194212
public boolean isMinRecommendedSupported() {
195-
assert (MIN_SUPPORTED_SDK.version != null);
196-
return version != null && version.compareTo(MIN_SUPPORTED_SDK.version) >= 0;
213+
return supportsVersion(MIN_SUPPORTED_SDK);
197214
}
198215

199216
public boolean isTrackWidgetCreationRecommended() {
200-
assert (MIN_SAFE_TRACK_WIDGET_CREATION_SDK.version != null);
201-
return version != null && version.compareTo(MIN_SAFE_TRACK_WIDGET_CREATION_SDK.version) >= 0;
217+
return supportsVersion(MIN_SAFE_TRACK_WIDGET_CREATION_SDK);
202218
}
203219

204220
public boolean isDartDefineSupported() {
205-
//noinspection ConstantConditions
206-
return version != null && version.compareTo(MIN_DART_DEFINE_SDK.version) >= 0;
221+
return supportsVersion(MIN_DART_DEFINE_SDK);
207222
}
208223

209224
public boolean isXcodeConfigOnlySupported() {
210-
//noinspection ConstantConditions
211-
return version != null && version.compareTo(MIN_XCODE_CONFIG_ONLY.version) >= 0;
225+
return supportsVersion(MIN_XCODE_CONFIG_ONLY);
212226
}
213227

214228
public boolean flutterCreateSupportsPlatforms() {
215-
//noinspection ConstantConditions
216-
return version != null && version.compareTo(MIN_CREATE_PLATFORMS_SDK.version) >= 0;
229+
return supportsVersion(MIN_CREATE_PLATFORMS_SDK);
217230
}
218231

219232
public boolean stableChannelSupportsPlatforms() {
220-
//noinspection ConstantConditions
221-
return version != null && version.compareTo(MAX_STABLE_NO_PLATFORMS_SDK.version) > 0;
233+
return supportsVersion(MAX_STABLE_NO_PLATFORMS_SDK);
222234
}
223235

224236
public boolean flutterRunSupportsDevToolsUrl() {
225-
return version != null && this.compareTo(MIN_PASS_DEVTOOLS_SDK) >= 0 && this.compareTo(MIN_OPTIONAL_PASS_DEVTOOLS_SDK) < 0;
237+
return this.compareTo(MIN_PASS_DEVTOOLS_SDK) >= 0 && this.compareTo(MIN_OPTIONAL_PASS_DEVTOOLS_SDK) < 0;
226238
}
227239

228240
public boolean useDaemonForDevTools() {
229-
return version != null && this.compareTo(MIN_PASS_DEVTOOLS_SDK) >= 0;
241+
return supportsVersion(MIN_PASS_DEVTOOLS_SDK);
230242
}
231243

232244
public boolean flutterTestSupportsMachineMode() {
@@ -238,76 +250,77 @@ public boolean flutterTestSupportsFiltering() {
238250
}
239251

240252
public boolean isPubOutdatedSupported() {
241-
//noinspection ConstantConditions
242-
return version != null && version.compareTo(MIN_PUB_OUTDATED_SDK.version) >= 0;
253+
return supportsVersion(MIN_PUB_OUTDATED_SDK);
243254
}
244255

245256
public boolean isSkeletonTemplateAvailable() {
246-
return version != null && version.compareTo(MIN_SKELETON_TEMPLATE.version) >= 0;
257+
return supportsVersion(MIN_SKELETON_TEMPLATE);
247258
}
248259

249260
public boolean isPluginFfiTemplateAvailable() {
250-
return version != null && version.compareTo(MIN_PLUGIN_FFI_TEMPLATE.version) >= 0;
261+
return supportsVersion(MIN_PLUGIN_FFI_TEMPLATE);
251262
}
252263

253264
public boolean isEmptyProjectAvailable() {
254-
return version != null && version.compareTo(MIN_EMPTY_PROJECT.version) >= 0;
265+
return supportsVersion(MIN_EMPTY_PROJECT);
255266
}
256267

257268
public boolean isUriMappingSupportedForWeb() {
258-
return version != null && this.compareTo(MIN_URI_MAPPING_FOR_WEB) >= 0;
269+
return supportsVersion(MIN_URI_MAPPING_FOR_WEB);
259270
}
260271

261272
public boolean isWebPlatformStable() {
262-
return version != null && this.compareTo(MIN_STABLE_WEB_PLATFORM) >= 0;
273+
return supportsVersion(MIN_STABLE_WEB_PLATFORM);
263274
}
264275

265276
public boolean isWindowsPlatformStable() {
266-
return version != null && this.compareTo(MIN_STABLE_WINDOWS_PLATFORM) >= 0;
277+
return supportsVersion(MIN_STABLE_WINDOWS_PLATFORM);
267278
}
268279

269280
public boolean isLinuxPlatformStable() {
270-
return version != null && this.compareTo(MIN_STABLE_LINUX_PLATFORM) >= 0;
281+
return supportsVersion(MIN_STABLE_LINUX_PLATFORM);
271282
}
272283

273284
public boolean isMacOSPlatformStable() {
274-
return version != null && this.compareTo(MIN_STABLE_MACOS_PLATFORM) >= 0;
285+
return supportsVersion(MIN_STABLE_MACOS_PLATFORM);
275286
}
276287

277288
public boolean canUseDistributedIcons() {
278-
return version != null && this.compareTo(DISTRIBUTED_ICONS) >= 0;
289+
return supportsVersion(DISTRIBUTED_ICONS);
279290
}
280291

281292
public boolean canUseDevToolsPathUrls() {
282-
return version != null && this.compareTo(MIN_SUPPORTS_DEVTOOLS_PATH_URLS) >= 0;
293+
return supportsVersion(MIN_SUPPORTS_DEVTOOLS_PATH_URLS);
283294
}
284295

285296
public boolean canUseToolEventStream() {
286-
return version != null && this.compareTo(MIN_SUPPORTS_TOOL_EVENT_STREAM) >= 0;
297+
return supportsVersion(MIN_SUPPORTS_TOOL_EVENT_STREAM);
287298
}
288299

289300
public boolean canUseDeepLinksTool() {
290-
return version != null && this.compareTo(MIN_SUPPORTS_DEEP_LINKS_TOOL) >= 0;
301+
return supportsVersion(MIN_SUPPORTS_DEEP_LINKS_TOOL);
291302
}
292303

293304
public boolean canUseDevToolsMultiEmbed() {
294-
return version != null && this.compareTo(MIN_SUPPORTS_DEVTOOLS_MULTI_EMBED) >= 0;
305+
return supportsVersion(MIN_SUPPORTS_DEVTOOLS_MULTI_EMBED);
295306
}
296307

308+
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
297309
public boolean canUseDtd() {
298-
return version != null && this.compareTo(MIN_SUPPORTS_DTD) >= 0;
310+
return supportsVersion(MIN_SUPPORTS_DTD);
299311
}
300312

301313
public boolean sdkIsSupported() {
302-
return version != null && this.compareTo(MIN_SDK_SUPPORTED) >= 0;
314+
return supportsVersion(MIN_SDK_SUPPORTED);
303315
}
304316

305317
public boolean isValid() {
306318
return version != null;
307319
}
308320

321+
@NotNull
309322
public String fullVersion() {
310-
return version == null ? "unknown version" : version.toString();
323+
return version == null ? "unknown version" : Objects.requireNonNull(version.toString());
311324
}
312325

313326
/**
@@ -319,21 +332,21 @@ public String getVersionText() {
319332
}
320333

321334
@Override
335+
@NotNull
322336
public String toString() {
323-
return version == null ? "unknown version" : version.toCompactString();
337+
return version == null ? "unknown version" : Objects.requireNonNull(version.toCompactString());
324338
}
325339

326340
@Override
327341
public int compareTo(@NotNull FlutterSdkVersion otherVersion) {
328-
// TODO(messick) Remove "version != null" prior to calling this method everywhere in this file.
329342
if (version == null) return -1;
330343
if (otherVersion.version == null) return 1;
331344
final int standardComparisonResult = version.compareTo(otherVersion.version);
332345
if (standardComparisonResult != 0) {
333346
return standardComparisonResult;
334347
}
335348

336-
// Check for beta version strings if standard versions are equivalent.
349+
// If standard versions are equivalent, check for beta version strings.
337350
if (betaVersion == null && otherVersion.betaVersion == null) {
338351
return 0;
339352
}
@@ -350,15 +363,16 @@ else if (otherVersion.betaVersion == null) {
350363
return betaComparisonResult;
351364
}
352365

353-
// Check master version ints if both have master version ints and versions are otherwise equivalent.
354-
// Otherwise, the version without a master version is further ahead.
355-
if (masterVersion != 0 && otherVersion.masterVersion != 0) {
356-
return Integer.compare(masterVersion, otherVersion.masterVersion);
366+
// If both have main version ints and the versions are otherwise equivalent,
367+
// compare the main version ints.
368+
// Otherwise, the version without a main version is further ahead.
369+
if (mainVersion != 0 && otherVersion.mainVersion != 0) {
370+
return Integer.compare(mainVersion, otherVersion.mainVersion);
357371
}
358-
else if (masterVersion != 0) {
372+
else if (mainVersion != 0) {
359373
return -1;
360374
}
361-
else if (otherVersion.masterVersion != 0) {
375+
else if (otherVersion.mainVersion != 0) {
362376
return 1;
363377
}
364378
else {

0 commit comments

Comments
 (0)