Use try with resource in DelegatingClassLoader

Change-Id: I7cc08ad9cf0516a00d658e9f32f178401a6bb271
This commit is contained in:
Alexandre Philbert
2018-04-15 20:23:12 -04:00
parent d620ad1043
commit a894b51ccd

View File

@@ -31,13 +31,17 @@ public class DelegatingClassLoader extends ClassLoader {
@Override
public Class<?> findClass(String name) throws ClassNotFoundException {
String path = name.replace('.', '/') + ".class";
InputStream resource = target.getResourceAsStream(path);
if (resource != null) {
try {
byte[] bytes = ByteStreams.toByteArray(resource);
return defineClass(name, bytes, 0, bytes.length);
} catch (IOException e) {
try (InputStream resource = target.getResourceAsStream(path)) {
if (resource != null) {
try {
byte[] bytes = ByteStreams.toByteArray(resource);
return defineClass(name, bytes, 0, bytes.length);
} catch (IOException e) {
// throws ClassNotFoundException later
}
}
} catch (IOException e) {
// throws ClassNotFoundException later
}
throw new ClassNotFoundException(name);
}