Skip to content

Commit 252a7f5

Browse files
authored
Fix PMD warnings across UI and database modules (#3978)
1 parent 986f516 commit 252a7f5

17 files changed

+140
-212
lines changed

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,9 @@ public int getPreferredH() {
244244
}
245245
if (preferredW > 0) {
246246
Image i = getIcon();
247-
if (i != null) {
248-
if (preferredW < i.getWidth()) {
249-
return (int)(i.getHeight() * preferredW / (double)i.getWidth());
250-
}
247+
// PMD Fix (CollapsibleIfStatements): Combine the nested icon and width checks into a single conditional.
248+
if (i != null && preferredW < i.getWidth()) {
249+
return (int)(i.getHeight() * preferredW / (double)i.getWidth());
251250
}
252251
}
253252
return super.getPreferredH();
@@ -260,10 +259,9 @@ public int getPreferredW() {
260259
}
261260
if (preferredH > 0) {
262261
Image i = getIcon();
263-
if (i != null) {
264-
if (preferredH < i.getHeight()) {
265-
return (int)(i.getWidth() * preferredH / (double)i.getHeight());
266-
}
262+
// PMD Fix (CollapsibleIfStatements): Merge the icon nullity and height comparison into one condition.
263+
if (i != null && preferredH < i.getHeight()) {
264+
return (int)(i.getWidth() * preferredH / (double)i.getHeight());
267265
}
268266
}
269267
return super.getPreferredW();

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,9 @@ public static void bindProgress(final ConnectionRequest[] sources, final Slider
9191
* {@inheritDoc}
9292
*/
9393
public void actionPerformed(ActionEvent evt) {
94-
if(sources != null) {
95-
if(!sourceVec.contains(evt.getSource())) {
96-
return;
97-
}
94+
// PMD Fix (CollapsibleIfStatements): Collapse the nested source checks into one conditional.
95+
if(sources != null && !sourceVec.contains(evt.getSource())) {
96+
return;
9897
}
9998
NetworkEvent e = (NetworkEvent)evt;
10099
switch(e.getProgressType()) {

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

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -576,11 +576,6 @@ private Inset getMaxDividerInset() {
576576
return getFixedInset(maxInset);
577577
}
578578

579-
private void setDividerInset(String inset) {
580-
getDividerInset().setValueAsString(inset);
581-
clampInset();
582-
}
583-
584579
private Inset getAutoInset(LayeredLayoutConstraint cnst) {
585580
switch (orientation) {
586581
case VERTICAL_SPLIT:
@@ -616,6 +611,7 @@ private LayeredLayoutConstraint initDividerInset(LayeredLayoutConstraint cnst, S
616611

617612

618613

614+
// PMD Fix (UnusedPrivateMethod): Removed unused setDividerInset helper.
619615
private void clampInset() {
620616

621617
int px = getDividerInset().getAbsolutePixels(divider);
@@ -793,14 +789,14 @@ public void expand(boolean force) {
793789
getFixedInset(preferredInset).copyTo(getDividerInset());
794790
clampInset();
795791
isCollapsed = false;
796-
SplitPane.this.animateLayout(300);
797-
} else if (isExpanded) {
798-
// do nothing
799-
} else {
792+
// PMD Fix (UselessQualifiedThis): Call animateLayout directly within the class scope.
793+
animateLayout(300);
794+
} else if (!isExpanded) {
800795
getFixedInset(maxInset).copyTo(getDividerInset());
801796
clampInset();
802797
isExpanded = true;
803-
SplitPane.this.animateLayout(300);
798+
// PMD Fix (UselessQualifiedThis): Call animateLayout directly within the class scope.
799+
animateLayout(300);
804800
}
805801
}
806802

@@ -817,20 +813,22 @@ public void collapse() {
817813
* @param force True to force it to collapse to minimum position (skipping preferred position if it is in expanded state).
818814
*/
819815
public void collapse(boolean force) {
820-
if (isCollapsed) {
821-
// do nothing
822-
} else if (isExpanded && !force) {
823-
getFixedInset(preferredInset).copyTo(getDividerInset());
824-
clampInset();
825-
isExpanded = false;
826-
SplitPane.this.animateLayout(300);
827-
} else {
828-
getFixedInset(minInset).copyTo(getDividerInset());
829-
clampInset();
830-
isCollapsed = true;
831-
SplitPane.this.animateLayout(300);
816+
if (!isCollapsed) {
817+
if (isExpanded && !force) {
818+
getFixedInset(preferredInset).copyTo(getDividerInset());
819+
clampInset();
820+
isExpanded = false;
821+
// PMD Fix (UselessQualifiedThis): Call animateLayout directly within the class scope.
822+
animateLayout(300);
823+
} else {
824+
getFixedInset(minInset).copyTo(getDividerInset());
825+
clampInset();
826+
isCollapsed = true;
827+
// PMD Fix (UselessQualifiedThis): Call animateLayout directly within the class scope.
828+
animateLayout(300);
829+
}
832830
}
833-
831+
834832
}
835833

836834
/**

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -772,8 +772,8 @@ public void actionPerformed(ActionEvent evt) {
772772
}
773773

774774
}
775+
// PMD Fix (UnnecessaryReturn): Removed redundant return at the end of the method.
775776
animationLock = false;
776-
return;
777777
}
778778
};
779779

@@ -836,6 +836,7 @@ public void setValue(boolean value) {
836836
setValue(value, false);
837837
}
838838

839+
// PMD Fix (UnusedPrivateMethod): Removed the unused flip() helper and retained this setter as the single source of truth.
839840
private void setValue(boolean value, boolean fireEvent) {
840841
boolean orig = animationLock;
841842
animationLock = true;
@@ -852,10 +853,6 @@ private void setValue(boolean value, boolean fireEvent) {
852853
animationLock = orig;
853854
}
854855

855-
private void flip() {
856-
setValue(!value);
857-
}
858-
859856
/**
860857
* Checks if switch is in the "on" position.
861858
* @return True if switch is on.

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

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -840,26 +840,23 @@ private ToastBarComponent getToastBarComponent(boolean create) {
840840
updateStatus();
841841
}
842842
Rectangle safeArea = Display.getInstance().getDisplaySafeArea(new Rectangle(0, 0, 0, 0));
843-
if(position == Component.BOTTOM) {
844-
if (f.getInvisibleAreaUnderVKB() > 0) {
845-
Style s = c.getAllStyles();
846-
s.setMarginUnit(Style.UNIT_TYPE_PIXELS);
847-
s.setMarginBottom(f.getInvisibleAreaUnderVKB());
848-
}
849-
int safeBottomMargin = Display.getInstance().getDisplayHeight()
850-
- safeArea.getY()
851-
- safeArea.getHeight();
852-
if (0 < safeBottomMargin) {
853-
Style s = c.getAllStyles();
854-
s.setPaddingUnit(Style.UNIT_TYPE_PIXELS);
855-
s.setPaddingBottom(safeBottomMargin);
856-
}
857-
} else if (position == Component.TOP) {
858-
if (safeArea.getY() > 0) {
859-
Style s = c.getAllStyles();
860-
s.setPaddingUnit(Style.UNIT_TYPE_PIXELS);
861-
s.setPaddingTop(safeArea.getY());
862-
}
843+
// PMD Fix (CollapsibleIfStatements): Combine nested position checks to simplify the layout adjustments.
844+
if(position == Component.BOTTOM && f.getInvisibleAreaUnderVKB() > 0) {
845+
Style s = c.getAllStyles();
846+
s.setMarginUnit(Style.UNIT_TYPE_PIXELS);
847+
s.setMarginBottom(f.getInvisibleAreaUnderVKB());
848+
}
849+
int safeBottomMargin = Display.getInstance().getDisplayHeight()
850+
- safeArea.getY()
851+
- safeArea.getHeight();
852+
if (position == Component.BOTTOM && safeBottomMargin > 0) {
853+
Style s = c.getAllStyles();
854+
s.setPaddingUnit(Style.UNIT_TYPE_PIXELS);
855+
s.setPaddingBottom(safeBottomMargin);
856+
} else if (position == Component.TOP && safeArea.getY() > 0) {
857+
Style s = c.getAllStyles();
858+
s.setPaddingUnit(Style.UNIT_TYPE_PIXELS);
859+
s.setPaddingTop(safeArea.getY());
863860
}
864861

865862
return c;

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,20 +117,19 @@ protected ConnectionRequest createConnectionRequest(final DocumentInfo docInfo,
117117
return new ConnectionRequest() {
118118

119119
protected void buildRequestBody(OutputStream os) throws IOException {
120-
if (isPost()) {
121-
if (docInfo.getParams() != null) {
122-
String enc = docInfo.getEncoding();
123-
if(enc.indexOf('/') > -1) {
124-
if(enc.indexOf("charset=") > -1) {
125-
enc = enc.substring(enc.indexOf("charset=") + 8);
126-
} else {
127-
enc = DocumentInfo.ENCODING_UTF8;
128-
}
120+
// PMD Fix (CollapsibleIfStatements): Merge the post and parameter checks into a single conditional.
121+
if (isPost() && docInfo.getParams() != null) {
122+
String enc = docInfo.getEncoding();
123+
if(enc.indexOf('/') > -1) {
124+
if(enc.indexOf("charset=") > -1) {
125+
enc = enc.substring(enc.indexOf("charset=") + 8);
126+
} else {
127+
enc = DocumentInfo.ENCODING_UTF8;
129128
}
130-
OutputStreamWriter w = new OutputStreamWriter(os, enc);
131-
w.write(docInfo.getParams());
132-
w.flush();
133129
}
130+
OutputStreamWriter w = new OutputStreamWriter(os, enc);
131+
w.write(docInfo.getParams());
132+
w.flush();
134133
}
135134
}
136135

CodenameOne/src/com/codename1/db/Cursor.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,22 @@ public interface Cursor {
4141

4242
/**
4343
* Move the cursor to the first row.
44-
*
45-
* If cursor provides forward-only navigation and is positioned after the
46-
* first row then calling first() method would throw a IOException.
47-
* @return true if succeeded
48-
* @throws IOException
44+
*
45+
* If cursor provides forward-only navigation and is positioned after the
46+
* first row then calling first() method would throw a IOException.
47+
* @return true if succeeded
48+
* @throws IOException
4949
*/
50-
public boolean first() throws IOException;
50+
// PMD Fix (UnnecessaryModifier): Interface methods are implicitly public; remove redundant modifiers.
51+
boolean first() throws IOException;
5152

5253
/**
5354
* Move the cursor to the last row.
5455
*
5556
* @return true if succeeded
5657
* @throws IOException
5758
*/
58-
public boolean last() throws IOException;
59+
boolean last() throws IOException;
5960

6061
/**
6162
* Moves the cursor to the next row.
@@ -64,7 +65,7 @@ public interface Cursor {
6465
* @return true if succeeded
6566
* @throws IOException
6667
*/
67-
public boolean next() throws IOException;
68+
boolean next() throws IOException;
6869

6970
/**
7071
* Moves the cursor to the previous row.
@@ -73,7 +74,7 @@ public interface Cursor {
7374
* @return true if succeeded
7475
* @throws IOException
7576
*/
76-
public boolean prev() throws IOException;
77+
boolean prev() throws IOException;
7778

7879
/**
7980
* Returns the zero-based index for a given column name.
@@ -84,7 +85,7 @@ public interface Cursor {
8485
* @return the index of the column
8586
* @throws IOException
8687
*/
87-
public int getColumnIndex(String columnName) throws IOException;
88+
int getColumnIndex(String columnName) throws IOException;
8889

8990
/**
9091
* Returns the column name at a given zero-based column index.
@@ -96,22 +97,22 @@ public interface Cursor {
9697
*
9798
* @throws IOException
9899
*/
99-
public String getColumnName(int columnIndex) throws IOException;
100+
String getColumnName(int columnIndex) throws IOException;
100101

101102
/**
102103
* Returns the column count
103104
* @return the column count
104105
* @throws IOException
105106
*/
106-
public int getColumnCount() throws IOException;
107+
int getColumnCount() throws IOException;
107108

108109
/**
109110
* Returns the current Cursor position.
110111
*
111112
* @return the cursor position
112113
* @throws IOException
113114
*/
114-
public int getPosition() throws IOException;
115+
int getPosition() throws IOException;
115116

116117
/**
117118
* Move the cursor to an absolute row position
@@ -120,20 +121,20 @@ public interface Cursor {
120121
* @return true if succeeded
121122
* @throws IOException
122123
*/
123-
public boolean position(int row) throws IOException;
124+
boolean position(int row) throws IOException;
124125

125126
/**
126127
* Close the cursor and release its resources
127128
* @throws IOException
128129
*/
129-
public void close() throws IOException;
130+
void close() throws IOException;
130131

131132
/**
132133
* Get the Row data Object.
133134
*
134135
* @return a Row Object
135136
* @throws IOException
136137
*/
137-
public Row getRow() throws IOException;
138+
Row getRow() throws IOException;
138139

139140
}

CodenameOne/src/com/codename1/db/Database.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,9 @@ public static boolean isCustomPathSupported() {
5858
}
5959

6060
private static void validateDatabaseNameArgument(String databaseName) {
61-
if (!isCustomPathSupported()) {
62-
if (databaseName.indexOf("/") != -1 || databaseName.indexOf("\\") != -1) {
63-
throw new IllegalArgumentException("This platform does not support custom database paths. The database name cannot contain file separators.");
64-
}
61+
// PMD Fix (CollapsibleIfStatements): Merge the custom path support and separator checks into one condition.
62+
if (!isCustomPathSupported() && (databaseName.indexOf("/") != -1 || databaseName.indexOf("\\") != -1)) {
63+
throw new IllegalArgumentException("This platform does not support custom database paths. The database name cannot contain file separators.");
6564
}
6665
}
6766

0 commit comments

Comments
 (0)