Skip to content

Commit 2d57c02

Browse files
committed
Fix some 'bugs' and 'code smells' found by Sonarqube.
1 parent 573452d commit 2d57c02

File tree

3 files changed

+23
-36
lines changed

3 files changed

+23
-36
lines changed

asm-test/src/main/java/org/objectweb/asm/test/AsmTest.java

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -88,20 +88,20 @@ public abstract class AsmTest {
8888
* MethodSource name to be used in parameterized tests that must be instantiated for all possible
8989
* (precompiled class, api) pairs.
9090
*/
91-
public final String ALL_CLASSES_AND_ALL_APIS = "allClassesAndAllApis";
91+
public static final String ALL_CLASSES_AND_ALL_APIS = "allClassesAndAllApis";
9292

9393
/**
9494
* MethodSource name to be used in parameterized tests that must be instantiated for all
9595
* precompiled classes, with the latest api.
9696
*/
97-
public final String ALL_CLASSES_AND_LATEST_API = "allClassesAndLatestApi";
97+
public static final String ALL_CLASSES_AND_LATEST_API = "allClassesAndLatestApi";
9898

9999
/**
100100
* A precompiled class, hand-crafted to contain some set of class file structures. These classes
101101
* are not compiled as part of the build. Instead, they have been compiled beforehand, with the
102102
* appropriate JDKs (including some now very hard to download and install).
103103
*/
104-
public static enum PrecompiledClass {
104+
public enum PrecompiledClass {
105105
DEFAULT_PACKAGE("DefaultPackage"),
106106
JDK3_ALL_INSTRUCTIONS("jdk3.AllInstructions"),
107107
JDK3_ALL_STRUCTURES("jdk3.AllStructures"),
@@ -150,10 +150,7 @@ public boolean isMoreRecentThan(Api api) {
150150
if (name.startsWith("jdk8") && api.value() < Api.ASM5.value()) {
151151
return true;
152152
}
153-
if (name.startsWith("jdk9") && api.value() < Api.ASM6.value()) {
154-
return true;
155-
}
156-
return false;
153+
return name.startsWith("jdk9") && api.value() < Api.ASM6.value();
157154
}
158155

159156
/**
@@ -165,18 +162,17 @@ public boolean isMoreRecentThan(Api api) {
165162
*/
166163
public boolean isMoreRecentThanCurrentJdk() {
167164
if (name.startsWith("jdk9")) {
168-
final String V9 = "1.9";
165+
final String v9 = "1.9";
169166
String javaVersion = System.getProperty("java.version");
170-
return javaVersion.substring(V9.length()).compareTo(V9) < 0;
167+
return javaVersion.substring(v9.length()).compareTo(v9) < 0;
171168
}
172169
return false;
173170
}
174171

175172
/** @return the content of this class. */
176173
public byte[] getBytes() {
177-
InputStream inputStream = null;
178-
try {
179-
inputStream = ClassLoader.getSystemResourceAsStream(name.replace('.', '/') + ".class");
174+
String resourceName = name.replace('.', '/') + ".class";
175+
try (InputStream inputStream = ClassLoader.getSystemResourceAsStream(resourceName)) {
180176
assertNotNull(inputStream, "Class not found " + name);
181177
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
182178
byte[] data = new byte[inputStream.available()];
@@ -188,14 +184,7 @@ public byte[] getBytes() {
188184
return outputStream.toByteArray();
189185
} catch (IOException e) {
190186
fail("Can't read " + name);
191-
return null;
192-
} finally {
193-
if (inputStream != null) {
194-
try {
195-
inputStream.close();
196-
} catch (IOException ignored) {
197-
}
198-
}
187+
return new byte[0];
199188
}
200189
}
201190

@@ -206,7 +195,7 @@ public String toString() {
206195
}
207196

208197
/** An ASM API version. */
209-
public static enum Api {
198+
public enum Api {
210199
ASM4("ASM4", 4 << 16),
211200
ASM5("ASM5", 5 << 16),
212201
ASM6("ASM6", 6 << 16);
@@ -354,7 +343,7 @@ static boolean doLoadAndInstantiate(String className, byte[] classContent) {
354343
Class<?> clazz = byteClassLoader.loadClass(className);
355344
if (!clazz.isEnum() && (clazz.getModifiers() & Modifier.ABSTRACT) == 0) {
356345
Constructor<?> constructor = clazz.getDeclaredConstructors()[0];
357-
ArrayList<Object> arguments = new ArrayList<Object>();
346+
ArrayList<Object> arguments = new ArrayList<>();
358347
for (Class<?> parameterType : constructor.getParameterTypes()) {
359348
arguments.add(Array.get(Array.newInstance(parameterType, 1), 0));
360349
}
@@ -364,20 +353,14 @@ static boolean doLoadAndInstantiate(String className, byte[] classContent) {
364353
} catch (ClassNotFoundException e) {
365354
// Should never happen given the ByteClassLoader implementation.
366355
fail("Can't find class " + className);
367-
} catch (InstantiationException e) {
368-
// Should never happen since we don't try to instantiate classes
369-
// that can't be instantiated (abstract and enum classes).
370-
fail("Can't instantiate class " + className);
371-
} catch (IllegalAccessException e) {
372-
// Should never happen since we use setAccessible(true).
373-
fail("Can't instantiate class " + className);
374-
} catch (IllegalArgumentException e) {
375-
// Should never happen since we create the appropriate constructor
376-
// arguments for the expected constructor parameters.
356+
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException e) {
357+
// Should never happen since we don't try to instantiate classes that can't be instantiated
358+
// (abstract and enum classes), we use setAccessible(true), and we create the appropriate
359+
// constructor arguments for the expected constructor parameters.
377360
fail("Can't instantiate class " + className);
378361
} catch (InvocationTargetException e) {
379-
// If an exception occurs in the invoked constructor, it means the
380-
// class was successfully verified first.
362+
// If an exception occurs in the invoked constructor, it means the class was successfully
363+
// verified first.
381364
}
382365
return byteClassLoader.classLoaded();
383366
}

asm-test/src/main/java/org/objectweb/asm/test/Assertions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public static class ExecutableSubject {
5555

5656
public <T extends Throwable> ExecutableOutcomeSubject<T> succeedsOrThrows(
5757
Class<T> expectedType) {
58-
return new ExecutableOutcomeSubject<T>(executable, expectedType);
58+
return new ExecutableOutcomeSubject<>(executable, expectedType);
5959
}
6060
}
6161

asm-test/src/main/java/org/objectweb/asm/test/ClassDump.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2131,7 +2131,7 @@ CpInfo getCpInfo(int cpIndex) {
21312131
@Override
21322132
public <C extends CpInfo> C getCpInfo(int cpIndex, Class<C> cpInfoType) {
21332133
Object cpInfo = get(CP_INFO_KEY | cpIndex);
2134-
if (!cpInfoType.isInstance(cpInfo)) {
2134+
if (cpInfo != null && !cpInfoType.isInstance(cpInfo)) {
21352135
throw new RuntimeException(
21362136
"Invalid constant pool type :"
21372137
+ cpInfo.getClass().getName()
@@ -2293,6 +2293,10 @@ void sortByContent() {
22932293
public int compareTo(Builder builder) {
22942294
return name.compareTo(builder.name);
22952295
}
2296+
2297+
public boolean equals(Object other) {
2298+
return (other instanceof Builder) && name.equals(((Builder) other).name);
2299+
}
22962300
}
22972301

22982302
/** An {@link AbstractBuilder} which sorts its children by name before building. */

0 commit comments

Comments
 (0)