Skip to content

Commit 138bd7a

Browse files
authored
Replace boxed-primitive constructors with valueOf (CodeQL java/inefficient-boxed-constructor) (#247)
Replace 17 uses of the deprecated boxed-primitive constructors (new Long/Integer/Short/Byte/Character/Float/Double(...)) with the corresponding X.valueOf(...) factory methods across 6 files. valueOf avoids an unnecessary allocation and may reuse cached instances; the change is behavior-preserving (equal values, same equals/hashCode). Verified by compiling persistit/core, persistit/ui and the jaspi jwt-session-module, plus javac for the persistit FindFile Ant example.
1 parent e516274 commit 138bd7a

6 files changed

Lines changed: 18 additions & 17 deletions

File tree

commons/auth-filters/authn-filter/jaspi-modules/jwt-session-module/src/main/java/org/forgerock/jaspi/modules/session/jwt/AbstractJwtSessionModule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ private String buildJwtString(JwtClaimsSet claimsSet, Key publicKey) {
573573
*/
574574
private int getCookieMaxAge(Date now, Date exp) {
575575
if (!browserSessionOnly) {
576-
return new Long((exp.getTime() - now.getTime()) / 1000L).intValue();
576+
return Long.valueOf((exp.getTime() - now.getTime()) / 1000L).intValue();
577577
} else {
578578
return -1;
579579
}

persistit/core/src/main/java/com/persistit/DefaultValueCoder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ private static Accessor lookupAccessor(final Class clazz, final String name) {
379379

380380
final char ch = name.charAt(0);
381381
if (Character.isLetter(ch) && Character.isLowerCase(ch)) {
382-
baseName = new Character(Character.toUpperCase(ch)) + name.substring(1);
382+
baseName = Character.valueOf(Character.toUpperCase(ch)) + name.substring(1);
383383
} else {
384384
baseName = name;
385385
}

persistit/core/src/main/java/com/persistit/ManagementImpl.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ public BufferPoolInfo[] getBufferPoolInfoArray() {
345345
final BufferPoolInfo[] result = new BufferPoolInfo[size];
346346
int index = 0;
347347
for (int bufferSize = Buffer.MIN_BUFFER_SIZE; bufferSize <= Buffer.MAX_BUFFER_SIZE; bufferSize *= 2) {
348-
final BufferPool pool = bufferPoolTable.get(new Integer(bufferSize));
348+
final BufferPool pool = bufferPoolTable.get(Integer.valueOf(bufferSize));
349349

350350
if (pool != null && index < size) {
351351
final BufferPoolInfo info = new BufferPoolInfo();
@@ -1049,7 +1049,7 @@ public synchronized long startTask(final String description, final String owner,
10491049
final long taskId = ++_taskIdCounter;
10501050
task.setPersistit(_persistit);
10511051
task.setup(taskId, description, owner, maximumTime, verbosity);
1052-
_tasks.put(new Long(taskId), task);
1052+
_tasks.put(Long.valueOf(taskId), task);
10531053
task.start();
10541054
return taskId;
10551055
} catch (final Exception ex) {
@@ -1149,7 +1149,7 @@ public synchronized void setTaskSuspended(final long taskId, final boolean suspe
11491149
task.resume();
11501150
}
11511151
} else {
1152-
final Task task = _tasks.get(new Long(taskId));
1152+
final Task task = _tasks.get(Long.valueOf(taskId));
11531153
if (task != null) {
11541154
if (suspend)
11551155
task.suspend();
@@ -1180,11 +1180,11 @@ public synchronized void stopTask(final long taskId, final boolean remove) {
11801180
_tasks.clear();
11811181
}
11821182
} else {
1183-
final Task task = _tasks.get(new Long(taskId));
1183+
final Task task = _tasks.get(Long.valueOf(taskId));
11841184
if (task != null) {
11851185
task.stop();
11861186
if (remove) {
1187-
_tasks.remove(new Long(task._taskId));
1187+
_tasks.remove(Long.valueOf(task._taskId));
11881188
}
11891189
}
11901190
}
@@ -1304,7 +1304,7 @@ public synchronized String launch(final Task task, final String description) thr
13041304
try {
13051305
final long taskId = taskId();
13061306
task.setup(taskId, description, Thread.currentThread().getName(), 0, 5);
1307-
_tasks.put(new Long(taskId), task);
1307+
_tasks.put(Long.valueOf(taskId), task);
13081308
task.start();
13091309
return Long.toString(taskId);
13101310
} catch (final Exception ex) {

persistit/core/src/main/java/com/persistit/Persistit.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1507,7 +1507,7 @@ private Volume getSpecialVolume(final String propName, final String dflt) throws
15071507
* @return the <code>BufferPool</code> for the specific buffer size
15081508
*/
15091509
BufferPool getBufferPool(final int size) {
1510-
return _bufferPoolTable.get(new Integer(size));
1510+
return _bufferPoolTable.get(Integer.valueOf(size));
15111511
}
15121512

15131513
/**

persistit/examples/FindFile/FindFile.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ private void traverseFileNames(String fixed, Pattern pattern, boolean forward) {
300300
KeyFilter filter = new KeyFilter(ex.getKey());
301301
if (fixed.length() != 0) {
302302
String end = fixed.substring(0, fixed.length() - 1)
303-
+ new Character((char) (fixed.charAt(fixed.length() - 1) + 1));
303+
+ Character.valueOf((char) (fixed.charAt(fixed.length() - 1) + 1));
304304
//
305305
// append a Term that selects only the range accepted by the
306306
// fixed portion of the name.

persistit/ui/src/main/java/com/persistit/ui/ValueInspectorTreeNode.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
16+
* Portions Copyrighted 2026 3A Systems, LLC
1617
*/
1718

1819
package com.persistit.ui;
@@ -379,19 +380,19 @@ private ValueInspectorTreeNode getArrayChild(final int index) {
379380
if (elementType == boolean.class) {
380381
element = ((boolean[]) _object)[index] ? Boolean.TRUE : Boolean.FALSE;
381382
} else if (elementType == byte.class) {
382-
element = new Byte(((byte[]) _object)[index]);
383+
element = Byte.valueOf(((byte[]) _object)[index]);
383384
} else if (elementType == short.class) {
384-
element = new Short(((short[]) _object)[index]);
385+
element = Short.valueOf(((short[]) _object)[index]);
385386
} else if (elementType == char.class) {
386-
element = new Character(((char[]) _object)[index]);
387+
element = Character.valueOf(((char[]) _object)[index]);
387388
} else if (elementType == int.class) {
388-
element = new Integer(((int[]) _object)[index]);
389+
element = Integer.valueOf(((int[]) _object)[index]);
389390
} else if (elementType == long.class) {
390-
element = new Long(((long[]) _object)[index]);
391+
element = Long.valueOf(((long[]) _object)[index]);
391392
} else if (elementType == float.class) {
392-
element = new Float(((float[]) _object)[index]);
393+
element = Float.valueOf(((float[]) _object)[index]);
393394
} else if (elementType == double.class) {
394-
element = new Double(((double[]) _object)[index]);
395+
element = Double.valueOf(((double[]) _object)[index]);
395396
} else
396397
throw new RuntimeException();
397398
} else {

0 commit comments

Comments
 (0)