Skip to content

Commit 7af2bce

Browse files
Fix SpotBugs UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR warnings (#4340)
* Fix SpotBugs UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR warnings and enforce rule. Addressed multiple null dereference issues reported by SpotBugs across several files: - CodenameOneImplementation.java: Added local variable assignment and null check for logger. - NetworkManager.java: Added local variable assignment and null check for threadInstance. - Tree.java: Added null checks for stat_desc. - FontImage.java: Added null checks for fnt. - Node.java: Added null checks for scene and camera. - WebServiceProxyCall.java: Added null checks for def and arguments. - PreferencesObject.java: Added null check for bo and propertyIndex. - ShareService.java: Added null check for original form. - Inflate.java: Added null checks for blocks. - StyleParser.java: Added null check for color. - CommonTransitions.java: Added null checks for timeline, buffer, motion, and motion2. - XYMultiSeriesTransition.java: Added null check for seriesTransitions. - AnimationObject.java: Added setTimeNotNull method and usage. Updated .github/scripts/generate-quality-report.py to treat UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR as an error. * Fix UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR SpotBugs warnings. Added null checks to dereferenced fields in: - CodenameOneImplementation.java - NetworkManager.java - Tree.java - FontImage.java - Node.java - WebServiceProxyCall.java - PreferencesObject.java - ShareService.java - Inflate.java - StyleParser.java - CommonTransitions.java - XYMultiSeriesTransition.java - XYSeriesTransition.java - XYValueSeriesTransition.java - XYChart.java - AnimationObject.java - Component.java Updated .github/scripts/generate-quality-report.py to enforce this rule. * Fix remaining SpotBugs UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR violations. Addressed missed null checks in: - RequestBuilder.java: Added null checks for response and list. - DateSpinner.java: Added null check for day field. This builds upon previous fixes to enforce the rule in CI. --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent 675f9f7 commit 7af2bce

26 files changed

+196
-96
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,8 @@ def main() -> None:
759759
if spotbugs:
760760
forbidden_rules = {
761761
"RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE",
762-
"RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE"
762+
"RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE",
763+
"UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR"
763764
}
764765
violations = [
765766
f for f in spotbugs.findings

CodenameOne/src/com/codename1/charts/transitions/XYMultiSeriesTransition.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,11 @@ public XYMultiSeriesTransition(ChartComponent chart, XYMultipleSeriesDataset dat
6969
protected void initTransition() {
7070

7171
getBuffer(); // initializes the buffer and seriesTranslations
72-
int len = seriesTransitions.length;
73-
for (int i = 0; i < len; i++) {
74-
seriesTransitions[i].initTransition();
72+
if (seriesTransitions != null) {
73+
int len = seriesTransitions.length;
74+
for (int i = 0; i < len; i++) {
75+
seriesTransitions[i].initTransition();
76+
}
7577
}
7678
super.initTransition();
7779

@@ -86,9 +88,11 @@ protected void initTransition() {
8688
@Override
8789
protected void update(int progress) {
8890
getBuffer(); // initializes the buffer and seriesTranslations
89-
int len = seriesTransitions.length;
90-
for (int i = 0; i < len; i++) {
91-
seriesTransitions[i].update(progress);
91+
if (seriesTransitions != null) {
92+
int len = seriesTransitions.length;
93+
for (int i = 0; i < len; i++) {
94+
seriesTransitions[i].update(progress);
95+
}
9296
}
9397
}
9498

@@ -99,9 +103,11 @@ protected void update(int progress) {
99103
protected void cleanup() {
100104
super.cleanup();
101105
getBuffer(); // initializes the buffer and seriesTranslations
102-
int len = seriesTransitions.length;
103-
for (int i = 0; i < len; i++) {
104-
seriesTransitions[i].cleanup();
106+
if (seriesTransitions != null) {
107+
int len = seriesTransitions.length;
108+
for (int i = 0; i < len; i++) {
109+
seriesTransitions[i].cleanup();
110+
}
105111
}
106112
}
107113

CodenameOne/src/com/codename1/charts/transitions/XYSeriesTransition.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ private void copyValues(XYSeries source, XYSeries target) {
108108
@Override
109109
protected void cleanup() {
110110
super.cleanup();
111-
this.cachedSeries.clear();
111+
if (cachedSeries != null) {
112+
this.cachedSeries.clear();
113+
}
112114
}
113115

114116

@@ -119,6 +121,7 @@ protected void cleanup() {
119121
*/
120122
protected void update(int progress) {
121123
double dProgress = progress;
124+
if (endVals == null || startVals == null) return;
122125
int len = endVals.getItemCount(); // PMD Fix: UnusedLocalVariable removed unused endindex
123126
for (int i = 0; i < len; i++) {
124127
double x = endVals.getX(i);

CodenameOne/src/com/codename1/charts/transitions/XYValueSeriesTransition.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ private void copyValues(XYValueSeries source, XYValueSeries target) {
108108
@Override
109109
protected void cleanup() {
110110
super.cleanup();
111-
this.cachedSeries.clear();
111+
if (cachedSeries != null) {
112+
this.cachedSeries.clear();
113+
}
112114
}
113115

114116

@@ -119,6 +121,7 @@ protected void cleanup() {
119121
*/
120122
protected void update(int progress) {
121123
double dProgress = progress;
124+
if (endVals == null || startVals == null) return;
122125
int len = endVals.getItemCount(); // PMD Fix: UnusedLocalVariable removed unused endindex
123126
for (int i = 0; i < len; i++) {
124127
double x = endVals.getX(i);

CodenameOne/src/com/codename1/charts/views/XYChart.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,7 @@ protected void drawText(Canvas canvas, String text, float x, float y, Paint pain
687687
* @param inverse if the inverse transform needs to be applied
688688
*/
689689
private void transform(Canvas canvas, float angle, boolean inverse) {
690+
if (mCenter == null) return;
690691
if (inverse) {
691692
canvas.scale(1 / mScale, mScale);
692693
canvas.translate(mTranslate, -mTranslate);

CodenameOne/src/com/codename1/components/InfiniteScrollAdapter.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,14 @@ public static void continueFetching(Container cnt) {
133133
}
134134

135135
void reachedEnd() {
136-
infiniteContainer.removeComponent(endMarker);
137-
infiniteContainer.addComponent(ip);
138-
infiniteContainer.revalidate();
139-
Display.getInstance().callSerially(fetchMore);
136+
if (infiniteContainer != null) {
137+
infiniteContainer.removeComponent(endMarker);
138+
infiniteContainer.addComponent(ip);
139+
infiniteContainer.revalidate();
140+
}
141+
if (fetchMore != null) {
142+
Display.getInstance().callSerially(fetchMore);
143+
}
140144
}
141145

142146
/**
@@ -147,6 +151,9 @@ void reachedEnd() {
147151
* @param areThereMore whether additional components exist
148152
*/
149153
public void addMoreComponents(Component[] components, boolean areThereMore) {
154+
if (infiniteContainer == null) {
155+
return;
156+
}
150157
infiniteContainer.removeComponent(ip);
151158
infiniteContainer.removeComponent(endMarker);
152159
for (Component c : components) {
@@ -177,7 +184,7 @@ public void addMoreComponents(Component[] components, boolean areThereMore) {
177184
* user interaction see https://github.com/codenameone/CodenameOne/issues/2721
178185
*/
179186
public void continueFetching() {
180-
if (endMarker.getParent() == null) {
187+
if (endMarker.getParent() == null && fetchMore != null) {
181188
fetchMore.run();
182189
}
183190
}

CodenameOne/src/com/codename1/facebook/FaceBookAccess.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1292,7 +1292,9 @@ public void search(String objectType, String query, DefaultListModel results, Ac
12921292
* Kills the current request.
12931293
*/
12941294
public void killCurrentRequest() {
1295-
current.kill();
1295+
if (current != null) {
1296+
current.kill();
1297+
}
12961298
}
12971299

12981300
/**

CodenameOne/src/com/codename1/impl/CodenameOneImplementation.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5636,7 +5636,10 @@ protected boolean isLogged() {
56365636
* @param content content of the message
56375637
*/
56385638
protected void log(String content) {
5639-
logger.actionPerformed(new ActionEvent(content, ActionEvent.Type.Log));
5639+
ActionListener l = logger;
5640+
if(l != null) {
5641+
l.actionPerformed(new ActionEvent(content, ActionEvent.Type.Log));
5642+
}
56405643
}
56415644

56425645
/**

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,9 @@ public void shutdown() {
325325
running = false;
326326
if (networkThreads != null) {
327327
for (NetworkThread n : networkThreads) {
328-
n.stopped = true;
328+
if(n != null) {
329+
n.stopped = true;
330+
}
329331
}
330332
}
331333
networkThreads = null;
@@ -793,7 +795,10 @@ public ConnectionRequest getCurrentRequest() {
793795

794796
public void join() {
795797
try {
796-
threadInstance.join();
798+
Thread t = threadInstance;
799+
if(t != null) {
800+
t.join();
801+
}
797802
} catch (InterruptedException ex) {
798803
ex.printStackTrace();
799804
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ protected void handleException(Exception err) {
298298
protected void readResponse(InputStream input) throws IOException {
299299
DataInputStream dis = new DataInputStream(input);
300300

301+
if (def == null) return;
301302
switch (def.returnType) {
302303
case TYPE_VOID:
303304
return;
@@ -512,6 +513,7 @@ protected void buildRequestBody(OutputStream os) throws IOException {
512513
dos.writeUTF(def.name);
513514
int alen = arguments.length;
514515
for (int iter = 0; iter < alen; iter++) {
516+
if (def == null || def.arguments == null) continue;
515517
switch (def.arguments[iter]) {
516518
case TYPE_BYTE:
517519
dos.writeByte(((Byte) arguments[iter]).byteValue());

0 commit comments

Comments
 (0)