Skip to content

Commit da736bf

Browse files
authored
Fix SpotBugs findings and enforce rules (#4414)
1 parent 4a8a53e commit da736bf

File tree

11 files changed

+83
-55
lines changed

11 files changed

+83
-55
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,12 @@ def main() -> None:
823823
"REC_CATCH_EXCEPTION",
824824
"RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE",
825825
"RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT",
826+
"INT_VACUOUS_COMPARISON",
827+
"DM_STRING_TOSTRING",
828+
"HE_HASHCODE_USE_OBJECT_EQUALS",
829+
"IM_BAD_CHECK_FOR_ODD",
830+
"IT_NO_SUCH_ELEMENT",
831+
"FL_FLOATS_AS_LOOP_COUNTERS",
826832
"UI_INHERITANCE_UNSAFE_GETRESOURCE",
827833
"URF_UNREAD_FIELD",
828834
"URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD",

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@ public boolean isInSegment(double angle) {
5252
double cAngle = angle % 360;
5353
double startAngle = mStartAngle;
5454
double stopAngle = mEndAngle;
55-
while (stopAngle > 360) {
56-
startAngle -= 360;
57-
stopAngle -= 360;
55+
if (stopAngle > 360) {
56+
int rotations = (int) Math.floor(stopAngle / 360d);
57+
startAngle -= 360 * rotations;
58+
stopAngle -= 360 * rotations;
5859
}
5960
return cAngle >= startAngle && cAngle <= stopAngle;
6061
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,9 @@ public void draw(Canvas canvas, int x, int y, int width, int height, Paint paint
112112
paint.setColor(ColorUtil.GRAY);
113113
float thisRad = (float) Math.toRadians(90 - currentAngle);
114114
float nextRad = (float) Math.toRadians(90 - (currentAngle + angle));
115-
for (double level = 0; level <= 1d; level += decCoef) { // PMD Fix: DontUseFloatTypeForLoopIndices switched to double
116-
float levelFactor = (float) level;
115+
int levelSteps = (int) Math.round(1d / decCoef);
116+
for (int level = 0; level <= levelSteps; level++) {
117+
float levelFactor = (float) (level * decCoef);
117118
float thisX = (float) (centerX - Math.sin(thisRad) * radius * levelFactor);
118119
float thisY = (float) (centerY - Math.cos(thisRad) * radius * levelFactor);
119120
float nextX = (float) (centerX - Math.sin(nextRad) * radius * levelFactor);

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,9 @@ protected long calculateContentLength() {
269269
length += key.length();
270270
if (ignoreEncoding.contains(key)) {
271271
try {
272-
length += value.toString().getBytes("UTF-8").length;
272+
length += ((String) value).getBytes("UTF-8").length;
273273
} catch (UnsupportedEncodingException ex) {
274-
length += StringUtil.getBytes(value.toString()).length;
274+
length += StringUtil.getBytes((String) value).length;
275275
}
276276
} else {
277277
if (base64Binaries) {
@@ -287,9 +287,9 @@ protected long calculateContentLength() {
287287
length += key.length();
288288
if (ignoreEncoding.contains(key)) {
289289
try {
290-
length += s.toString().getBytes("UTF-8").length;
290+
length += s.getBytes("UTF-8").length;
291291
} catch (UnsupportedEncodingException ex) {
292-
length += StringUtil.getBytes(value.toString()).length;
292+
length += StringUtil.getBytes(s).length;
293293
}
294294
} else {
295295
if (base64Binaries) {
@@ -534,4 +534,4 @@ public int hashCode() {
534534
result = 31 * result + (args != null ? args.hashCode() : 0);
535535
return result;
536536
}
537-
}
537+
}

CodenameOne/src/com/codename1/io/rest/RequestBuilder.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import java.util.HashMap;
4949
import java.util.List;
5050
import java.util.Map;
51+
import java.util.Objects;
5152

5253
/**
5354
* This class is used to build, invoke the http request and to get the http
@@ -249,8 +250,8 @@ public RequestBuilder queryParam(String key, String[] values) {
249250
*/
250251
public RequestBuilder header(String key, String value) {
251252
checkFetched();
252-
// .toString() is used to trigger an NPE early for null headers
253-
headers.put(key.toString(), value.toString());
253+
headers.put(Objects.requireNonNull(key, "Header key cannot be null"),
254+
Objects.requireNonNull(value, "Header value cannot be null"));
254255
return this;
255256
}
256257

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import java.util.List;
5656
import java.util.Map;
5757
import java.util.Enumeration;
58+
import java.util.NoSuchElementException;
5859

5960
/**
6061
* Maps the properties that are in a class/object and provides access to them so tools such as ORM
@@ -198,9 +199,10 @@ public void remove() {
198199
}
199200

200201
public PropertyBase next() {
201-
int i = off;
202-
off++;
203-
return properties[i];
202+
if (!hasNext()) {
203+
throw new NoSuchElementException();
204+
}
205+
return properties[off++];
204206
}
205207
};
206208
}

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import java.util.Enumeration;
3030
import java.util.Hashtable;
3131
import java.util.Iterator;
32+
import java.util.NoSuchElementException;
33+
import java.util.Objects;
3234
import java.util.Vector;
3335

3436
/**
@@ -295,7 +297,21 @@ public boolean isEmpty() {
295297

296298
@Override
297299
public int hashCode() {
298-
return parent.hashCode();
300+
return Objects.hash(parent, parentElement, index);
301+
}
302+
303+
@Override
304+
public boolean equals(Object obj) {
305+
if (this == obj) {
306+
return true;
307+
}
308+
if (!(obj instanceof PropertyXMLElement)) {
309+
return false;
310+
}
311+
PropertyXMLElement other = (PropertyXMLElement) obj;
312+
return index == other.index
313+
&& Objects.equals(parent, other.parent)
314+
&& Objects.equals(parentElement, other.parentElement);
299315
}
300316

301317
@Override
@@ -330,8 +346,10 @@ public boolean hasNext() {
330346

331347
@Override
332348
public Element next() {
333-
offset++;
334-
return getChildAt(index);
349+
if (!hasNext()) {
350+
throw new NoSuchElementException();
351+
}
352+
return getChildAt(offset++);
335353
}
336354

337355
@Override

CodenameOne/src/com/codename1/ui/geom/Rectangle.java

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -188,16 +188,6 @@ public static void intersection(int rrX, int rrY, int rrW, int rrH, int rtx1, in
188188
tx2 -= tx1;
189189
ty2 -= ty1;
190190

191-
// tx2,ty2 will never overflow (they will never be
192-
// larger than the smallest of the two source w,h)
193-
// they might underflow, though...
194-
if (tx2 < Integer.MIN_VALUE) {
195-
tx2 = Integer.MIN_VALUE;
196-
}
197-
if (ty2 < Integer.MIN_VALUE) {
198-
ty2 = Integer.MIN_VALUE;
199-
}
200-
201191
dest.x = tx1;
202192
dest.y = ty1;
203193
dest.size.setWidth(tx2);
@@ -422,16 +412,6 @@ public Rectangle intersection(int rX, int rY, int rW, int rH) {
422412
}
423413
tx2 -= tx1;
424414
ty2 -= ty1;
425-
// tx2,ty2 will never overflow (they will never be
426-
// larger than the smallest of the two source w,h)
427-
// they might underflow, though...
428-
if (tx2 < Integer.MIN_VALUE) {
429-
tx2 = Integer.MIN_VALUE;
430-
}
431-
if (ty2 < Integer.MIN_VALUE) {
432-
ty2 = Integer.MIN_VALUE;
433-
}
434-
435415
return new Rectangle(tx1, ty1, tx2, ty2);
436416
}
437417

@@ -462,15 +442,6 @@ public void intersection(Rectangle input, Rectangle output) {
462442
}
463443
tx2 -= tx1;
464444
ty2 -= ty1;
465-
// tx2,ty2 will never overflow (they will never be
466-
// larger than the smallest of the two source w,h)
467-
// they might underflow, though...
468-
if (tx2 < Integer.MIN_VALUE) {
469-
tx2 = Integer.MIN_VALUE;
470-
}
471-
if (ty2 < Integer.MIN_VALUE) {
472-
ty2 = Integer.MIN_VALUE;
473-
}
474445
tx2 = Math.max(0, tx2);
475446
ty2 = Math.max(0, ty2);
476447
output.setBounds(tx1, ty1, tx2, ty2);

CodenameOne/src/com/codename1/ui/layouts/mig/ConstraintParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,7 @@ public static UnitValue[] parseInsets(String s, boolean acceptPanel) {
969969
String[] insS = toTrimmedTokens(s, ' ');
970970
UnitValue[] ins = new UnitValue[4];
971971
for (int j = 0; j < 4; j++) {
972-
UnitValue insSz = parseUnitValue(insS[j < insS.length ? j : insS.length - 1], UnitValue.ZERO, j % 2 == 1);
972+
UnitValue insSz = parseUnitValue(insS[j < insS.length ? j : insS.length - 1], UnitValue.ZERO, (j & 1) != 0);
973973
ins[j] = insSz != null ? insSz : PlatformDefaults.getPanelInsets(j);
974974
}
975975
return ins;

CodenameOne/src/com/codename1/ui/layouts/mig/UnitValue.java

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@
3535

3636
import com.codename1.util.MathUtil;
3737

38-
import java.util.ArrayList;
39-
import java.util.HashMap;
38+
import java.util.ArrayList;
39+
import java.util.Arrays;
40+
import java.util.HashMap;
41+
import java.util.Objects;
4042

4143
public final class UnitValue {
4244
/**
@@ -617,7 +619,33 @@ public String getConstraintString() {
617619
return LayoutUtil.getCCString(this);
618620
}
619621

620-
public int hashCode() {
621-
return (int) (value * 12345) + (oper >>> 5) + unit >>> 17;
622-
}
623-
}
622+
public int hashCode() {
623+
int result = 17;
624+
result = 31 * result + Float.floatToIntBits(value);
625+
result = 31 * result + unit;
626+
result = 31 * result + oper;
627+
result = 31 * result + (isHor ? 1 : 0);
628+
result = 31 * result + (unitStr != null ? unitStr.hashCode() : 0);
629+
result = 31 * result + (linkId != null ? linkId.hashCode() : 0);
630+
result = 31 * result + Arrays.hashCode(subUnits);
631+
return result;
632+
}
633+
634+
@Override
635+
public boolean equals(Object obj) {
636+
if (this == obj) {
637+
return true;
638+
}
639+
if (!(obj instanceof UnitValue)) {
640+
return false;
641+
}
642+
UnitValue other = (UnitValue) obj;
643+
return Float.compare(value, other.value) == 0
644+
&& unit == other.unit
645+
&& oper == other.oper
646+
&& isHor == other.isHor
647+
&& Objects.equals(unitStr, other.unitStr)
648+
&& Objects.equals(linkId, other.linkId)
649+
&& Arrays.equals(subUnits, other.subUnits);
650+
}
651+
}

0 commit comments

Comments
 (0)