Skip to content

Commit f7f6ace

Browse files
Fix redundant null checks (#4326)
* Fix redundant null checks identified by SpotBugs * Fix redundant nullcheck warnings from SpotBugs Removed redundant null checks and comparisons in multiple files as identified by SpotBugs. - Socket.java: Removed check for connection != null. - GroupLayout.java: Removed check for name != null. - Form.java: Removed checks for focused != null and atXY != null. - Resources.java: Removed checks for mediaRules != null. - Component.java: Removed check for cmp != null. - Command.java: Removed check for obj != null. - ComponentSelector.java: Removed check for mgr != null. - PropertyIndex.java: Removed check for p != null. - Container.java: Removed check for next != null. - Display.java: Removed check for dest != null. - CommonTransitions.java: Removed checks for sourceForm != null and dest != null. - ResourceThreadQueue.java: Removed check for img == null. - Other minor cleanup. * Fix redundant nullcheck warnings from SpotBugs - Remove redundant null checks in multiple files (Socket.java, Form.java, Component.java, etc.) where values are guaranteed to be non-null or null. - Update `generate-quality-report.py` to enforce zero tolerance for `RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE` and `RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE` warnings. - Fix compilation issues in Socket.java syntax. - Verify that the build passes with the new quality gate. * Fix redundant nullcheck warnings and enforce quality gate - Update generate-quality-report.py to fail build on RCN_REDUNDANT_NULLCHECK violations. - Remove redundant null checks in multiple files (Objects.java, Component.java, Form.java, etc.). - Revert unsafe null check removal in Socket.java that caused test failures. - Add suppression for Socket.java in spotbugs-exclude.xml. - Cleanup dead code in DefaultLookAndFeel.java and Inflate.java. --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent d010b3c commit f7f6ace

35 files changed

+92
-189
lines changed

.github/scripts/generate-quality-report.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,23 @@ def main() -> None:
754754
if not generate_html_only:
755755
REPORT_PATH.write_text(report + "\n", encoding="utf-8")
756756

757+
# Enforce quality gates
758+
spotbugs, _, _ = parse_spotbugs()
759+
if spotbugs:
760+
forbidden_rules = {
761+
"RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE",
762+
"RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE"
763+
}
764+
violations = [
765+
f for f in spotbugs.findings
766+
if f.rule in forbidden_rules
767+
]
768+
if violations:
769+
print("\n❌ Build failed due to forbidden SpotBugs violations:")
770+
for v in violations:
771+
print(f" - {v.rule}: {v.location} - {v.message}")
772+
exit(1)
773+
757774

758775
if __name__ == "__main__":
759776
main()

CodenameOne/src/com/codename1/compat/java/util/Objects.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static boolean equals(Object a, Object b) {
4848
if (a == b) {
4949
return true;
5050
}
51-
return a == null ? b == null : a.equals(b);
51+
return a != null && a.equals(b);
5252
}
5353

5454
/**

CodenameOne/src/com/codename1/io/ConnectionRequest.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2747,9 +2747,6 @@ public void run() {
27472747
if (fs.exists(file)) {
27482748
try {
27492749
EncodedImage img = EncodedImage.create(fs.openInputStream(file), (int) fs.getLength(file));
2750-
if (img == null) {
2751-
throw new IOException("Failed to load image at " + file);
2752-
}
27532750
CallbackDispatcher.dispatchSuccess(onSuccess, img);
27542751
} catch (Exception ex) {
27552752
CallbackDispatcher.dispatchError(onFail, ex);
@@ -2763,9 +2760,6 @@ public void run() {
27632760
if (fs.exists(file)) {
27642761
try {
27652762
EncodedImage img = EncodedImage.create(fs.createInputStream(file), fs.entrySize(file));
2766-
if (img == null) {
2767-
throw new IOException("Failed to load image at " + file);
2768-
}
27692763
CallbackDispatcher.dispatchSuccess(onSuccess, img);
27702764
} catch (Exception ex) {
27712765
CallbackDispatcher.dispatchError(onFail, ex);

CodenameOne/src/com/codename1/io/gzip/Inflate.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,6 @@ int inflateInit(int w) {
151151
inflateEnd();
152152
return Z_STREAM_ERROR;
153153
}
154-
if (blocks != null && wbits != w) {
155-
blocks.free();
156-
blocks = null;
157-
}
158154

159155
// set window size
160156
wbits = w;

CodenameOne/src/com/codename1/l10n/SimpleDateFormat.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -521,9 +521,7 @@ public Date parse(String source) throws ParseException {
521521

522522
throwInvalid("timezone", startIndex);
523523
}
524-
if (res != null) {
525-
parsedTimeZone = res.timeZone;
526-
}
524+
parsedTimeZone = res.timeZone;
527525
tzMinutes = ((tzMinutes == -1) ? 0 : tzMinutes) + v;
528526
break;
529527
case YEAR_LETTER:
@@ -844,10 +842,11 @@ String readAmPmMarker(String source, int ofs) {
844842
if (i == -1) {
845843
i = source.length();
846844
}
847-
String fragment = readSubstring(source, ofs, i).toLowerCase();
845+
String fragment = readSubstring(source, ofs, i);
848846
if (fragment == null) {
849847
return null;
850848
}
849+
fragment = fragment.toLowerCase();
851850
DateFormatSymbols ds = getDateFormatSymbols();
852851
String[] markers = ds.getAmPmStrings();
853852
for (String marker : markers) {

CodenameOne/src/com/codename1/processing/ContainsEvaluator.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,6 @@ private String[] _getLeftValue(StructuredContent element, String lvalue) {
7373
*/
7474
protected Object evaluateLeftContainsRight(StructuredContent element, String lvalue, String rvalue) {
7575
String[] lvalues = _getLeftValue(element, lvalue);
76-
if (lvalues == null) {
77-
return null;
78-
}
7976
// if the rvalue is wrapped with "()", the caller explicitly expects the lvalue to be an array of values,
8077
// otherwise try to do a "string contains" match first if there's only one lvalue
8178
if (rvalue.indexOf("(") == -1 && lvalues.length == 1) {

CodenameOne/src/com/codename1/processing/TextEvaluator.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,6 @@ private String[] _getLeftValue(StructuredContent element, String lvalue) {
8080
protected Object evaluateLeftLessRight(StructuredContent element,
8181
String lvalue, String rvalue) {
8282
String[] v = _getLeftValue(element, lvalue);
83-
if (v == null) {
84-
return null;
85-
}
8683
int vlen = v.length;
8784
for (int i = 0; i < vlen; i++) {
8885
if (isNumeric(rvalue) && isNumeric(v[i])) {
@@ -112,9 +109,6 @@ protected Object evaluateLeftLessRight(StructuredContent element,
112109
protected Object evaluateLeftGreaterRight(StructuredContent element,
113110
String lvalue, String rvalue) {
114111
String[] v = _getLeftValue(element, lvalue);
115-
if (v == null) {
116-
return null;
117-
}
118112
int vlen = v.length;
119113
for (int i = 0; i < vlen; i++) {
120114
if (isNumeric(rvalue) && isNumeric(v[i])) {
@@ -144,9 +138,6 @@ protected Object evaluateLeftGreaterRight(StructuredContent element,
144138
protected Object evaluateLeftEqualsRight(StructuredContent element,
145139
String lvalue, String rvalue) {
146140
String[] v = _getLeftValue(element, lvalue);
147-
if (v == null) {
148-
return null;
149-
}
150141
int vlen = v.length;
151142
for (int i = 0; i < vlen; i++) {
152143
if (isNumeric(rvalue) && isNumeric(v[i])) {

CodenameOne/src/com/codename1/properties/PropertyIndex.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,9 +372,7 @@ public void populateFromMap(Map<String, Object> m, Class<? extends PropertyBusin
372372
if (val instanceof List) {
373373
if (p instanceof CollectionProperty) {
374374
if (recursiveType != null) {
375-
if (p != null) {
376-
((CollectionProperty) p).clear();
377-
}
375+
((CollectionProperty) p).clear();
378376
for (Object e : (Collection) val) {
379377
if (e instanceof Map) {
380378
Class eType = p.getGenericType();

CodenameOne/src/com/codename1/properties/PropertyXMLElement.java

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,13 @@ public boolean contains(Element element) {
6161
return true;
6262
}
6363
Vector children = getChildren();
64-
if (children != null) {
65-
int i = 0;
66-
while (i < children.size()) {
67-
Element child = (Element) children.get(i);
68-
if (child.contains(element)) {
69-
return true;
70-
}
71-
i++;
64+
int i = 0;
65+
while (i < children.size()) {
66+
Element child = (Element) children.get(i);
67+
if (child.contains(element)) {
68+
return true;
7269
}
70+
i++;
7371
}
7472
return false;
7573
}
@@ -193,17 +191,14 @@ public Element getElementById(String id) {
193191
return this;
194192
}
195193
Vector children = getChildren();
196-
if (children != null) {
197-
int i = 0;
198-
while (i < children.size()) {
199-
Element child = (Element) children.get(i);
200-
Element match = child.getElementById(id);
201-
if (match != null) {
202-
return match;
203-
}
204-
i++;
194+
int i = 0;
195+
while (i < children.size()) {
196+
Element child = (Element) children.get(i);
197+
Element match = child.getElementById(id);
198+
if (match != null) {
199+
return match;
205200
}
206-
201+
i++;
207202
}
208203
return null;
209204
}
@@ -389,10 +384,8 @@ public String toString(String spacing) {
389384
}
390385
str += ">\n";
391386
Vector children = getChildren();
392-
if (children != null) {
393-
for (int i = 0; i < children.size(); i++) {
394-
str += ((Element) children.get(i)).toString(spacing + ' ');
395-
}
387+
for (int i = 0; i < children.size(); i++) {
388+
str += ((Element) children.get(i)).toString(spacing + ' ');
396389
}
397390
str += spacing + "</" + getTagName() + ">\n";
398391
return str;

CodenameOne/src/com/codename1/ui/BrowserComponent.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,14 +1263,12 @@ public void execute(String js, Object[] params) {
12631263
* @return the string returned from the Javascript call
12641264
*/
12651265
public String executeAndReturnString(String javaScript) {
1266-
if (internal == null) {
1267-
while (internal == null) {
1268-
CN.invokeAndBlock(new Runnable() {
1269-
public void run() {
1270-
Util.sleep(50);
1271-
}
1272-
});
1273-
}
1266+
while (internal == null) {
1267+
CN.invokeAndBlock(new Runnable() {
1268+
public void run() {
1269+
Util.sleep(50);
1270+
}
1271+
});
12741272
}
12751273
if (Display.impl.supportsBrowserExecuteAndReturnString(internal)) {
12761274
return Display.impl.browserExecuteAndReturnString(internal, javaScript);

0 commit comments

Comments
 (0)