Skip to content

Commit 4356815

Browse files
committed
Fix quality flaw: Returns empty arrays instead of null
1 parent 284a4c8 commit 4356815

5 files changed

Lines changed: 10 additions & 9 deletions

File tree

java-squid/src/main/java/org/sonar/java/bytecode/loader/FileSystemLoader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ public byte[] loadBytes(String name) {
6262
}
6363
File file = new File(baseDir, name);
6464
if (!file.exists()) {
65-
return null;
65+
return new byte[0];
6666
}
6767
InputStream is = null;
6868
try {
6969
is = new FileInputStream(file);
7070
return IOUtils.toByteArray(is);
7171
} catch (IOException e) {
72-
return null;
72+
return new byte[0];
7373
} finally {
7474
IOUtils.closeQuietly(is);
7575
}

java-squid/src/main/java/org/sonar/java/bytecode/loader/JarLoader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,14 @@ public byte[] loadBytes(String name) {
7070
try {
7171
ZipEntry entry = jarFile.getEntry(name);
7272
if (entry == null) {
73-
return null;
73+
return new byte[0];
7474
}
7575
is = jarFile.getInputStream(entry);
7676
return IOUtils.toByteArray(is);
7777
} catch (IOException e) {
7878
// TODO Godin: not sure that we should silently ignore exception here,
7979
// e.g. it can be thrown if file corrupted
80-
return null;
80+
return new byte[0];
8181
} finally {
8282
IOUtils.closeQuietly(is);
8383
}

java-squid/src/main/java/org/sonar/java/bytecode/loader/SquidClassLoader.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.sonar.java.bytecode.loader;
2121

2222
import com.google.common.collect.Iterators;
23+
import org.apache.commons.lang.ArrayUtils;
2324

2425
import java.io.Closeable;
2526
import java.io.File;
@@ -58,7 +59,7 @@ protected Class findClass(String name) throws ClassNotFoundException {
5859
String resourceName = name.replace('.', '/') + ".class";
5960
for (Loader loader : loaders) {
6061
byte[] classBytes = loader.loadBytes(resourceName);
61-
if (classBytes != null) {
62+
if (ArrayUtils.isNotEmpty(classBytes)) {
6263
// TODO Godin: definePackage ?
6364
return defineClass(name, classBytes, 0, classBytes.length);
6465
}

java-squid/src/test/java/org/sonar/java/bytecode/loader/FileSystemLoaderTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ public void testLoadBytes() throws Exception {
6666
File dir = new File("src/test/files/bytecode/bin/");
6767
FileSystemLoader loader = new FileSystemLoader(dir);
6868

69-
assertThat(loader.loadBytes("notfound")).isNull();
69+
assertThat(loader.loadBytes("notfound")).isEmpty();
7070

71-
assertThat(loader.loadBytes("tags/TagName.class")).isNotNull();
71+
assertThat(loader.loadBytes("tags/TagName.class")).isNotEmpty();
7272

7373
loader.close();
7474

java-squid/src/test/java/org/sonar/java/bytecode/loader/JarLoaderTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ public void testLoadBytes() throws Exception {
7474
File jar = new File("src/test/files/bytecode/lib/hello.jar");
7575
JarLoader loader = new JarLoader(jar);
7676

77-
assertThat(loader.loadBytes("notfound")).isNull();
77+
assertThat(loader.loadBytes("notfound")).isEmpty();
7878

7979
byte[] bytes = loader.loadBytes("META-INF/MANIFEST.MF");
80-
assertThat(bytes).isNotNull();
80+
assertThat(bytes).isNotEmpty();
8181
ByteArrayInputStream is = new ByteArrayInputStream(bytes);
8282
assertThat(IOUtils.readLines(is)).contains("Manifest-Version: 1.0");
8383

0 commit comments

Comments
 (0)