Skip to content

Commit ab2f392

Browse files
authored
Fix SpotBugs warnings for lazy init and null checks (#4412)
1 parent 41f61c8 commit ab2f392

File tree

11 files changed

+110
-65
lines changed

11 files changed

+110
-65
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,12 +818,15 @@ def main() -> None:
818818
"NP_BOOLEAN_RETURN_NULL",
819819
"REFLC_REFLECTION_MAY_INCREASE_ACCESSIBILITY_OF_CLASS",
820820
"REC_CATCH_EXCEPTION",
821+
"RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE",
822+
"RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT",
821823
"UI_INHERITANCE_UNSAFE_GETRESOURCE",
822824
"URF_UNREAD_FIELD",
823825
"URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD",
824826
"UUF_UNUSED_FIELD",
825827
"UWF_NULL_FIELD",
826828
"UW_UNCOND_WAIT",
829+
"LI_LAZY_INIT_UPDATE_STATIC",
827830
"SIC_INNER_SHOULD_BE_STATIC_ANON",
828831
"SS_SHOULD_BE_STATIC",
829832
"UPM_UNCALLED_PRIVATE_METHOD",

CodenameOne/src/com/codename1/charts/ChartComponent.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ public void pointerDragged(int[] x, int[] y) {
679679
*/
680680
public boolean isPanEnabled() {
681681
if (chart instanceof XYChart) {
682-
((XYChart) chart).getRenderer().isPanEnabled();
682+
return ((XYChart) chart).getRenderer().isPanEnabled();
683683
}
684684
return panEnabled;
685685
}
@@ -767,7 +767,7 @@ public void clearPanLimits() {
767767
*/
768768
public boolean isZoomEnabled() {
769769
if (chart instanceof XYChart) {
770-
((XYChart) chart).getRenderer().isZoomEnabled();
770+
return ((XYChart) chart).getRenderer().isZoomEnabled();
771771
}
772772
return zoomEnabled;
773773
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3966,7 +3966,10 @@ public Rectangle getDisplaySafeArea(Rectangle rect) {
39663966
* common constants in this class or be a user/implementation defined sound
39673967
*/
39683968
public void playBuiltinSound(String soundIdentifier) {
3969-
playUserSound(soundIdentifier);
3969+
boolean played = playUserSound(soundIdentifier);
3970+
if (!played) {
3971+
return;
3972+
}
39703973
}
39713974

39723975
/**

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -563,11 +563,14 @@ protected void readResponse(InputStream input) throws IOException {
563563
@Override
564564
protected void buildRequestBody(OutputStream os) throws IOException {
565565
DataOutputStream dos = new DataOutputStream(os);
566-
dos.writeUTF(def.name);
566+
WSDefinition localDef = def;
567+
dos.writeUTF(localDef.name);
568+
if (localDef.arguments == null) {
569+
return;
570+
}
567571
int alen = arguments.length;
568572
for (int iter = 0; iter < alen; iter++) {
569-
if (def == null || def.arguments == null) continue;
570-
switch (def.arguments[iter]) {
573+
switch (localDef.arguments[iter]) {
571574
case TYPE_BYTE:
572575
dos.writeByte(((Byte) arguments[iter]).byteValue());
573576
break;

CodenameOne/src/com/codename1/maps/MapComponent.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,9 +488,9 @@ public void run() {
488488
y = y - getAbsoluteY();
489489
Tile t = screenTile();
490490
Coord southWest = t.position(x - 20, t.dimension().getHeight() - y - 20);
491-
Mercator.inverseMercator(southWest.getLatitude(), southWest.getLongitude());
491+
southWest = Mercator.inverseMercator(southWest.getLatitude(), southWest.getLongitude());
492492
Coord northEast = t.position(x + 20, t.dimension().getHeight() - y + 20);
493-
Mercator.inverseMercator(northEast.getLatitude(), northEast.getLongitude());
493+
northEast = Mercator.inverseMercator(northEast.getLatitude(), northEast.getLongitude());
494494

495495
BoundingBox bbox = new BoundingBox(southWest, northEast);
496496
Enumeration e = _layers.elements();

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,13 @@ public static TextArea create(String text) {
297297
}
298298

299299
private static void initInputModes() {
300-
if (inputModes == null) {
300+
if (inputModes != null) {
301+
return;
302+
}
303+
synchronized (TextField.class) {
304+
if (inputModes != null) {
305+
return;
306+
}
301307
firstUppercaseInputMode.addElement("Abc");
302308
inputModes = new Hashtable();
303309
Hashtable upcase = new Hashtable();

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,14 @@ private Transform(Object nativeTransform) {
8383
}
8484

8585
public static Transform IDENTITY() {
86-
if (_IDENTITY == null) {
87-
_IDENTITY = new ImmutableTransform(Display.impl.makeTransformIdentity());
88-
_IDENTITY.type = TYPE_IDENTITY;
86+
if (_IDENTITY != null) {
87+
return _IDENTITY;
88+
}
89+
synchronized (Transform.class) {
90+
if (_IDENTITY == null) {
91+
_IDENTITY = new ImmutableTransform(Display.impl.makeTransformIdentity());
92+
_IDENTITY.type = TYPE_IDENTITY;
93+
}
8994
}
9095
return _IDENTITY;
9196
}

CodenameOne/src/com/codename1/ui/animations/CommonTransitions.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -499,13 +499,18 @@ public void initTransition() {
499499

500500
if (transitionType == TYPE_TIMELINE) {
501501
hideInterformContainers();
502-
Graphics g = buffer.getGraphics();
502+
Image timelineBuffer = buffer;
503+
if (timelineBuffer == null) {
504+
timelineBuffer = createMutableImage(w, h);
505+
buffer = timelineBuffer;
506+
}
507+
Graphics g = timelineBuffer.getGraphics();
503508
g.translate(-source.getAbsoluteX(), -source.getAbsoluteY());
504509

505-
g.setClip(0, 0, buffer.getWidth() + source.getAbsoluteX(), buffer.getHeight() + source.getAbsoluteY());
510+
g.setClip(0, 0, timelineBuffer.getWidth() + source.getAbsoluteX(), timelineBuffer.getHeight() + source.getAbsoluteY());
506511

507-
if (timeline != null && buffer != null && (timeline.getWidth() != buffer.getWidth() || timeline.getHeight() != buffer.getHeight())) {
508-
timeline = timeline.scaled(buffer.getWidth(), buffer.getHeight());
512+
if (timeline != null && (timeline.getWidth() != timelineBuffer.getWidth() || timeline.getHeight() != timelineBuffer.getHeight())) {
513+
timeline = timeline.scaled(timelineBuffer.getWidth(), timelineBuffer.getHeight());
509514
}
510515

511516
if (timeline instanceof Timeline) {
@@ -876,10 +881,13 @@ private void paintShiftFadeHierarchy(Container c, int alpha, Graphics g, boolean
876881
}
877882

878883
private Motion getComponentShiftMotion(Component c, boolean incoming) {
884+
if (c == null) {
885+
return null;
886+
}
879887
Motion m = (Motion) c.getClientProperty("$shm");
880888
if (m == null) {
881889
Component dest = getDestination();
882-
if (dest == null || c == null) {
890+
if (dest == null) {
883891
return m;
884892
}
885893
int travelDestination = dest.getWidth() - c.getWidth() - c.getAbsoluteX();

CodenameOne/src/com/codename1/ui/plaf/CSSBorder.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -318,13 +318,18 @@ private static boolean isTransparent(Color color) {
318318
}
319319

320320
private static Map<String, Byte> styleMap() {
321-
if (styleMap == null) {
322-
styleMap = new HashMap<String, Byte>();
323-
styleMap.put("none", STYLE_NONE);
324-
styleMap.put("hidden", STYLE_HIDDEN);
325-
styleMap.put("dotted", STYLE_DOTTED);
326-
styleMap.put("dashed", STYLE_DASHED);
327-
styleMap.put("solid", STYLE_SOLID);
321+
if (styleMap != null) {
322+
return styleMap;
323+
}
324+
synchronized (CSSBorder.class) {
325+
if (styleMap == null) {
326+
styleMap = new HashMap<String, Byte>();
327+
styleMap.put("none", STYLE_NONE);
328+
styleMap.put("hidden", STYLE_HIDDEN);
329+
styleMap.put("dotted", STYLE_DOTTED);
330+
styleMap.put("dashed", STYLE_DASHED);
331+
styleMap.put("solid", STYLE_SOLID);
332+
}
328333
}
329334
return styleMap;
330335
}

CodenameOne/src/com/codename1/ui/plaf/StyleParser.java

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -807,40 +807,44 @@ static Image parseBgImage(Resources theme, String val) {
807807
}
808808

809809
private static Map<String, Integer> bgTypes() {
810-
if (bgTypes == null) {
811-
bgTypes = new HashMap<String, Integer>();
812-
Object[] types = new Object[]{
813-
"image_aligned_bottom", (int) Style.BACKGROUND_IMAGE_ALIGNED_BOTTOM,
814-
"image_aligned_top", (int) Style.BACKGROUND_IMAGE_ALIGNED_TOP,
815-
"image_aligned_bottom_right", (int) Style.BACKGROUND_IMAGE_ALIGNED_BOTTOM_RIGHT,
816-
"image_aligned_bottom_left", (int) Style.BACKGROUND_IMAGE_ALIGNED_BOTTOM_LEFT,
817-
"image_aligned_top_right", (int) Style.BACKGROUND_IMAGE_ALIGNED_TOP_RIGHT,
818-
"image_aligned_top_left", (int) Style.BACKGROUND_IMAGE_ALIGNED_TOP_LEFT,
819-
"image_aligned_left", (int) Style.BACKGROUND_IMAGE_ALIGNED_LEFT,
820-
"image_aligned_right", (int) Style.BACKGROUND_IMAGE_ALIGNED_RIGHT,
821-
"image_aligned_center", (int) Style.BACKGROUND_IMAGE_ALIGNED_CENTER,
822-
"image_scaled", (int) Style.BACKGROUND_IMAGE_SCALED,
823-
"image_scaled_fill", (int) Style.BACKGROUND_IMAGE_SCALED_FILL,
824-
"image_scaled_fit", (int) Style.BACKGROUND_IMAGE_SCALED_FIT,
825-
"image_tile_both", (int) Style.BACKGROUND_IMAGE_TILE_BOTH,
826-
"image_tile_horizontal", (int) Style.BACKGROUND_IMAGE_TILE_HORIZONTAL,
827-
"image_tile_vertical", (int) Style.BACKGROUND_IMAGE_TILE_VERTICAL,
828-
"image_tile_horizontal_align_bottom", (int) Style.BACKGROUND_IMAGE_TILE_HORIZONTAL_ALIGN_BOTTOM,
829-
"image_tile_horizontal_align_top", (int) Style.BACKGROUND_IMAGE_TILE_HORIZONTAL_ALIGN_TOP,
830-
"image_tile_horizontal_align_center", (int) Style.BACKGROUND_IMAGE_TILE_HORIZONTAL_ALIGN_CENTER,
831-
"image_tile_vertical_align_left", (int) Style.BACKGROUND_IMAGE_TILE_VERTICAL_ALIGN_LEFT,
832-
"image_tile_vertical_align_right", (int) Style.BACKGROUND_IMAGE_TILE_VERTICAL_ALIGN_RIGHT,
833-
"image_tile_vertical_align_center", (int) Style.BACKGROUND_IMAGE_TILE_VERTICAL_ALIGN_CENTER,
834-
"gradient_radial", (int) Style.BACKGROUND_GRADIENT_RADIAL,
835-
"gradient_linear_horizontal", (int) Style.BACKGROUND_GRADIENT_LINEAR_HORIZONTAL,
836-
"gradient_linear_vertical", (int) Style.BACKGROUND_GRADIENT_LINEAR_VERTICAL,
837-
"none", (int) Style.BACKGROUND_NONE
838-
};
839-
int len = types.length;
840-
for (int i = 0; i < len; i += 2) {
841-
bgTypes.put((String) types[i], (Integer) types[i + 1]);
810+
if (bgTypes != null) {
811+
return bgTypes;
812+
}
813+
synchronized (StyleParser.class) {
814+
if (bgTypes == null) {
815+
bgTypes = new HashMap<String, Integer>();
816+
Object[] types = new Object[]{
817+
"image_aligned_bottom", (int) Style.BACKGROUND_IMAGE_ALIGNED_BOTTOM,
818+
"image_aligned_top", (int) Style.BACKGROUND_IMAGE_ALIGNED_TOP,
819+
"image_aligned_bottom_right", (int) Style.BACKGROUND_IMAGE_ALIGNED_BOTTOM_RIGHT,
820+
"image_aligned_bottom_left", (int) Style.BACKGROUND_IMAGE_ALIGNED_BOTTOM_LEFT,
821+
"image_aligned_top_right", (int) Style.BACKGROUND_IMAGE_ALIGNED_TOP_RIGHT,
822+
"image_aligned_top_left", (int) Style.BACKGROUND_IMAGE_ALIGNED_TOP_LEFT,
823+
"image_aligned_left", (int) Style.BACKGROUND_IMAGE_ALIGNED_LEFT,
824+
"image_aligned_right", (int) Style.BACKGROUND_IMAGE_ALIGNED_RIGHT,
825+
"image_aligned_center", (int) Style.BACKGROUND_IMAGE_ALIGNED_CENTER,
826+
"image_scaled", (int) Style.BACKGROUND_IMAGE_SCALED,
827+
"image_scaled_fill", (int) Style.BACKGROUND_IMAGE_SCALED_FILL,
828+
"image_scaled_fit", (int) Style.BACKGROUND_IMAGE_SCALED_FIT,
829+
"image_tile_both", (int) Style.BACKGROUND_IMAGE_TILE_BOTH,
830+
"image_tile_horizontal", (int) Style.BACKGROUND_IMAGE_TILE_HORIZONTAL,
831+
"image_tile_vertical", (int) Style.BACKGROUND_IMAGE_TILE_VERTICAL,
832+
"image_tile_horizontal_align_bottom", (int) Style.BACKGROUND_IMAGE_TILE_HORIZONTAL_ALIGN_BOTTOM,
833+
"image_tile_horizontal_align_top", (int) Style.BACKGROUND_IMAGE_TILE_HORIZONTAL_ALIGN_TOP,
834+
"image_tile_horizontal_align_center", (int) Style.BACKGROUND_IMAGE_TILE_HORIZONTAL_ALIGN_CENTER,
835+
"image_tile_vertical_align_left", (int) Style.BACKGROUND_IMAGE_TILE_VERTICAL_ALIGN_LEFT,
836+
"image_tile_vertical_align_right", (int) Style.BACKGROUND_IMAGE_TILE_VERTICAL_ALIGN_RIGHT,
837+
"image_tile_vertical_align_center", (int) Style.BACKGROUND_IMAGE_TILE_VERTICAL_ALIGN_CENTER,
838+
"gradient_radial", (int) Style.BACKGROUND_GRADIENT_RADIAL,
839+
"gradient_linear_horizontal", (int) Style.BACKGROUND_GRADIENT_LINEAR_HORIZONTAL,
840+
"gradient_linear_vertical", (int) Style.BACKGROUND_GRADIENT_LINEAR_VERTICAL,
841+
"none", (int) Style.BACKGROUND_NONE
842+
};
843+
int len = types.length;
844+
for (int i = 0; i < len; i += 2) {
845+
bgTypes.put((String) types[i], (Integer) types[i + 1]);
846+
}
842847
}
843-
844848
}
845849
return bgTypes;
846850
}

0 commit comments

Comments
 (0)