Skip to content

Commit 32f317a

Browse files
committed
Code analysis fixes: Refactor code for consistency and readability, including assertion simplifications and formatting improvements
1 parent e6f2812 commit 32f317a

24 files changed

Lines changed: 84 additions & 94 deletions

affinity-test/src/test/java/net/openhft/affinity/osgi/OSGiBundleTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void checkInject() {
6464
public void checkBundleState() {
6565
final Bundle bundle = findBundle(context, "net.openhft.affinity");
6666
assertNotNull(bundle);
67-
assertEquals(bundle.getState(), Bundle.ACTIVE);
67+
assertEquals(Bundle.ACTIVE, bundle.getState());
6868
}
6969

7070
@Test

affinity-test/src/test/java/net/openhft/affinity/osgi/OSGiTestBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class OSGiTestBase {
2929

3030
public static Option workspaceBundle(String projectName) {
3131
String baseDir = System.getProperty("main.basedir");
32-
String bundleDir = null;
32+
String bundleDir;
3333

3434
bundleDir = String.format("%s/%s/target/classes", baseDir, projectName);
3535
if (new File(bundleDir).exists()) {

affinity/src/main/java/net/openhft/affinity/Affinity.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,10 @@ public static void setThreadId() {
190190
public static boolean isJNAAvailable() {
191191
if (JNAAvailable == null) {
192192
int majorVersion = Integer.parseInt(Native.VERSION.split("\\.")[0]);
193-
if(majorVersion < 5) {
193+
if (majorVersion < 5) {
194194
LOGGER.warn("Affinity library requires JNA version >= 5");
195195
JNAAvailable = false;
196-
}
197-
else {
196+
} else {
198197
try {
199198
Class.forName("com.sun.jna.Platform");
200199
JNAAvailable = true;

affinity/src/main/java/net/openhft/affinity/AffinityLock.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ private static BitSet getReservedAffinity0() {
131131
reservedAffinity = reservedAffinity.trim();
132132
long[] longs = new long[1 + (reservedAffinity.length() - 1) / 16];
133133
int end = reservedAffinity.length();
134-
for(int i = 0; i < longs.length ; i++) {
134+
for (int i = 0; i < longs.length; i++) {
135135
int begin = Math.max(0, end - 16);
136136
longs[i] = Long.parseLong(reservedAffinity.substring(begin, end), 16);
137137
end = begin;
@@ -186,6 +186,9 @@ public static AffinityLock acquireLock(boolean bind) {
186186
* @return A handle for an affinity lock.
187187
*/
188188
public static AffinityLock acquireLock(int cpuId) {
189+
if (cpuId < 0 || cpuId >= PROCESSORS) {
190+
throw new IllegalArgumentException("cpuId must be between 0 and " + (PROCESSORS - 1) + ": " + cpuId);
191+
}
189192
return acquireLock(true, cpuId, AffinityStrategies.ANY);
190193
}
191194

@@ -199,17 +202,18 @@ public static AffinityLock acquireLock(int cpuId) {
199202
* @return A handle for an affinity lock, or nolock if no available CPU in the array
200203
*/
201204
public static AffinityLock acquireLock(int[] cpus) {
202-
for( int cpu : cpus )
203-
{
205+
for (int cpu : cpus) {
206+
if (cpu < 0 || cpu >= PROCESSORS) {
207+
throw new IllegalArgumentException("cpuId must be between 0 and " + (PROCESSORS - 1) + ": " + cpu);
208+
}
204209
AffinityLock lock = tryAcquireLock(true, cpu);
205-
if(lock != null)
206-
{
210+
if (lock != null) {
207211
LOGGER.info("Acquired lock on CPU {}", cpu);
208212
return lock;
209213
}
210214
}
211215

212-
LOGGER.warn("Failed to lock any CPU in explicit list " + Arrays.toString(cpus));
216+
LOGGER.warn("Failed to lock any CPU in explicit list {}", Arrays.toString(cpus));
213217
return LOCK_INVENTORY.noLock();
214218
}
215219

@@ -228,7 +232,7 @@ public static AffinityLock acquireLockLastMinus(int n) {
228232
* <ul>
229233
* <li>"N" being a positive integer means allocate this CPU,</li>
230234
* <li>"last" or "last-N" means allocate from the end,</li>
231-
* <li>"csv:1,2,5,6 eg means allocate first free core from the provided</li>
235+
* <li>"csv:1,2,5,6" eg means allocate first free core from the provided</li>
232236
* <li>"any" means allow any</li>
233237
* <li>"none" or null means</li>
234238
* <li>"0" is not allowed</li>
@@ -281,7 +285,7 @@ public static AffinityLock acquireLock(String desc) {
281285
}
282286
}
283287
if (cpuId <= 0) {
284-
System.err.println("Cannot allocate 0 or negative cpuIds '" + desc + "'");
288+
LOGGER.warn("Cannot allocate 0 or negative cpuIds '{}'", desc);
285289
return LOCK_INVENTORY.noLock();
286290
}
287291
return acquireLock(cpuId);
@@ -309,7 +313,7 @@ private static AffinityLock acquireLock(boolean bind, int cpuId, @NotNull Affini
309313
* Try to acquire a lock on the specified core
310314
* Returns lock if successful, or null if cpu cannot be acquired
311315
*
312-
* @param bind - if true, bind the current thread; if false, reserve a cpu which can be bound later
316+
* @param bind - if true, bind the current thread; if false, reserve a cpu which can be bound later
313317
* @param cpuId - the cpu to lock
314318
* @return - A handle to an affinity lock on success; null if failed to lock
315319
*/
@@ -332,7 +336,9 @@ public static String dumpLocks() {
332336

333337
private static boolean areAssertionsEnabled() {
334338
boolean debug = false;
339+
//noinspection AssertWithSideEffects
335340
assert debug = true;
341+
//noinspection ConstantValue
336342
return debug;
337343
}
338344

@@ -450,7 +456,7 @@ public void close() {
450456
@Override
451457
protected void finalize() throws Throwable {
452458
if (bound) {
453-
LOGGER.warn("Affinity lock for " + assignedThread + " was discarded rather than release()d in a controlled manner.", boundHere);
459+
LOGGER.warn("Affinity lock for {} was discarded rather than release()d in a controlled manner.", assignedThread, boundHere);
454460
release();
455461
}
456462
super.finalize();

affinity/src/main/java/net/openhft/affinity/AffinityThreadFactory.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,11 @@ public AffinityThreadFactory(String name, boolean daemon, @NotNull AffinityStrat
5353
public synchronized Thread newThread(@NotNull final Runnable r) {
5454
String name2 = id <= 1 ? name : (name + '-' + id);
5555
id++;
56-
Thread t = new Thread(new Runnable() {
57-
@Override
58-
public void run() {
59-
try (AffinityLock ignored = acquireLockBasedOnLast()) {
60-
assert ignored != null;
61-
r.run();
62-
}
56+
Thread t = new Thread(() -> {
57+
try (AffinityLock ignored = acquireLockBasedOnLast()) {
58+
//noinspection ConstantValue
59+
assert ignored != null;
60+
r.run();
6361
}
6462
}, name2);
6563
t.setDaemon(daemon);

affinity/src/main/java/net/openhft/affinity/BootClassPath.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package net.openhft.affinity;
1919

20+
import org.jetbrains.annotations.NotNull;
2021
import org.slf4j.Logger;
2122
import org.slf4j.LoggerFactory;
2223

@@ -87,15 +88,15 @@ private static Set<String> findResourcesInDirectory(final Path path, final Logge
8788
try {
8889
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
8990
@Override
90-
public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
91+
public @NotNull FileVisitResult visitFile(final @NotNull Path file, final @NotNull BasicFileAttributes attrs) throws IOException {
9192
if (file.getFileName().toString().endsWith(".class")) {
9293
dirResources.add(path.relativize(file).toString());
9394
}
9495
return super.visitFile(file, attrs);
9596
}
9697
});
9798
} catch (IOException e) {
98-
logger.warn("Error walking dir: " + path, e);
99+
logger.warn("Error walking dir: {}", path, e);
99100
}
100101

101102
return dirResources;

affinity/src/main/java/net/openhft/affinity/LockInventory.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static String dumpLocks(@NotNull AffinityLock[] locks) {
5353
for (int i = 0; i < locks.length; i++) {
5454
AffinityLock al = locks[i];
5555
sb.append(i).append(": ");
56-
sb.append(al.toString());
56+
sb.append(al);
5757
sb.append('\n');
5858
}
5959
return sb.toString();
@@ -90,7 +90,7 @@ private static boolean updateLockForCurrentThread(final boolean bind, final Affi
9090
throw e;
9191

9292
} catch (IOException e) {
93-
LOGGER.info("Error occurred acquiring lock, trying another " + e);
93+
LOGGER.info("Error occurred acquiring lock, trying another {}", String.valueOf(e));
9494
}
9595
return false;
9696
}
@@ -127,7 +127,7 @@ public final synchronized void set(CpuLayout cpuLayout) {
127127
private void shrink(NavigableMap<Integer, AffinityLock[]> physicalCoreLocks) {
128128
for (Map.Entry<Integer, AffinityLock[]> e : physicalCoreLocks.entrySet()) {
129129
final AffinityLock[] locks = e.getValue();
130-
for (int i=0; i<locks.length; i++) {
130+
for (int i = 0; i < locks.length; i++) {
131131
if (locks[i] == null) {
132132
final AffinityLock[] locks2 = new AffinityLock[i];
133133
System.arraycopy(locks, 0, locks2, 0, i);
@@ -215,9 +215,9 @@ public final synchronized AffinityLock acquireCore(boolean bind, int cpuId, Affi
215215

216216
final AffinityLock al = als[0];
217217
try {
218-
if (updateLockForCurrentThread(bind, al, true)) {
219-
return al;
220-
}
218+
if (updateLockForCurrentThread(bind, al, true)) {
219+
return al;
220+
}
221221
} catch (ClosedByInterruptException e) {
222222
Thread.currentThread().interrupt();
223223
return noLock();

affinity/src/main/java/net/openhft/affinity/MicroJitterSampler.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public class MicroJitterSampler {
4040
private static void pause() throws InterruptedException {
4141
if (BUSYWAIT) {
4242
long now = System.nanoTime();
43+
//noinspection StatementWithEmptyBody
4344
while (System.nanoTime() - now < 1_000_000) ;
4445
} else {
4546
Thread.sleep(1);

affinity/src/main/java/net/openhft/affinity/impl/LinuxJNAAffinity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public enum LinuxJNAAffinity implements IAffinity {
5252
loaded = true;
5353
} catch (NoClassDefFoundError | UnsatisfiedLinkError e) {
5454
if (IS_LINUX)
55-
LOGGER.warn("Unable to load jna library {}", e);
55+
LOGGER.warn("Unable to load jna library", e);
5656
}
5757
LOADED = loaded;
5858
}

affinity/src/main/java/net/openhft/affinity/impl/NullAffinity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public BitSet getAffinity() {
3838

3939
@Override
4040
public void setAffinity(final BitSet affinity) {
41-
LOGGER.trace("unable to set mask to {} as the JNIa nd JNA libraries and not loaded", Utilities.toHexString(affinity));
41+
LOGGER.trace("unable to set mask to {} as the JNI and JNA libraries not loaded", Utilities.toHexString(affinity));
4242
}
4343

4444
@Override

0 commit comments

Comments
 (0)