Skip to content

Commit 39cdb83

Browse files
authored
Fix warnings for NullPointerException in flutter-idea/src/io/flutter/editor/ExpressionParsingUtils.java (#7922)
1 parent 1d64b36 commit 39cdb83

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

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

+26-10
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@
55
*/
66
package io.flutter.editor;
77

8+
import org.jetbrains.annotations.Nullable;
9+
810
import java.awt.*;
911

1012
public class ExpressionParsingUtils {
11-
public static Integer parseNumberFromCallParam(String callText, String prefix) {
12-
if (callText.startsWith(prefix) && callText.endsWith(")")) {
13+
public static @Nullable Integer parseNumberFromCallParam(String callText, String prefix) {
14+
if (prefix == null) {
15+
return null;
16+
}
17+
if (callText != null && callText.startsWith(prefix) && callText.endsWith(")")) {
1318
String val = callText.substring(prefix.length(), callText.length() - 1).trim();
1419
final int index = val.indexOf(',');
1520
if (index != -1) {
@@ -23,32 +28,40 @@ public static Integer parseNumberFromCallParam(String callText, String prefix) {
2328
catch (NumberFormatException ignored) {
2429
}
2530
}
26-
2731
return null;
2832
}
2933

30-
public static Color parseColor(String text) {
34+
public static @Nullable Color parseColor(String text) {
35+
if (text == null) {
36+
return null;
37+
}
3138
final Color color = parseColor(text, "const Color(");
3239
if (color != null) return color;
3340

3441
return parseColor(text, "Color(");
3542
}
3643

37-
public static Color parseColor(String text, String colorText) {
44+
public static @Nullable Color parseColor(String text, String colorText) {
45+
if (text == null || colorText == null) {
46+
return null;
47+
}
3848
final Integer val = parseNumberFromCallParam(text, colorText);
3949
if (val == null) return null;
4050

4151
try {
4252
final int value = val;
4353
//noinspection UseJBColor
44-
return new Color((int)(value >> 16) & 0xFF, (int)(value >> 8) & 0xFF, (int)value & 0xFF, (int)(value >> 24) & 0xFF);
54+
return new Color((value >> 16) & 0xFF, (value >> 8) & 0xFF, value & 0xFF, (value >> 24) & 0xFF);
4555
}
4656
catch (IllegalArgumentException e) {
4757
return null;
4858
}
4959
}
5060

51-
public static Color parseColorComponents(String callText, String prefix, boolean isARGB) {
61+
public static @Nullable Color parseColorComponents(String callText, String prefix, boolean isARGB) {
62+
if (callText == null || prefix == null) {
63+
return null;
64+
}
5265
if (callText.startsWith(prefix) && callText.endsWith(")")) {
5366
final String colorString = callText.substring(prefix.length(), callText.length() - 1).trim();
5467
final String[] maybeNumbers = colorString.split(",");
@@ -60,8 +73,8 @@ public static Color parseColorComponents(String callText, String prefix, boolean
6073
return null;
6174
}
6275

63-
private static Color parseARGBColorComponents(String[] maybeNumbers) {
64-
if (maybeNumbers.length < 4) {
76+
private static @Nullable Color parseARGBColorComponents(String[] maybeNumbers) {
77+
if (maybeNumbers == null || maybeNumbers.length < 4) {
6578
return null;
6679
}
6780

@@ -90,7 +103,10 @@ private static Color parseARGBColorComponents(String[] maybeNumbers) {
90103
}
91104
}
92105

93-
private static Color parseRGBOColorComponents(String[] maybeNumbers) {
106+
private static @Nullable Color parseRGBOColorComponents(String[] maybeNumbers) {
107+
if (maybeNumbers == null) {
108+
return null;
109+
}
94110
final float[] rgbo = new float[4];
95111
for (int i = 0; i < 4; ++i) {
96112
final String maybeNumber = maybeNumbers[i].trim();

0 commit comments

Comments
 (0)