JarScanner: Fix resource leak flagged by infer
This fixes resource leak with JarFile instance acquired by call to ctor of JarScanner. Bug: Issue 5198 Change-Id: I80feabcf9e0bfd683d1d8c47f3fd6c9be3ff65a6
This commit is contained in:
parent
3e82acac08
commit
944b51634a
|
@ -163,20 +163,21 @@ public class SwitchSecureStore extends SiteProgram {
|
||||||
|
|
||||||
private String getNewSecureStoreClassName(Path secureStore)
|
private String getNewSecureStoreClassName(Path secureStore)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
JarScanner scanner = new JarScanner(secureStore);
|
try (JarScanner scanner = new JarScanner(secureStore)) {
|
||||||
List<String> newSecureStores =
|
List<String> newSecureStores =
|
||||||
scanner.findSubClassesOf(SecureStore.class);
|
scanner.findSubClassesOf(SecureStore.class);
|
||||||
if (newSecureStores.isEmpty()) {
|
if (newSecureStores.isEmpty()) {
|
||||||
throw new RuntimeException(String.format(
|
throw new RuntimeException(String.format(
|
||||||
"Cannot find implementation of SecureStore interface in %s",
|
"Cannot find implementation of SecureStore interface in %s",
|
||||||
secureStore.toAbsolutePath()));
|
secureStore.toAbsolutePath()));
|
||||||
|
}
|
||||||
|
if (newSecureStores.size() > 1) {
|
||||||
|
throw new RuntimeException(String.format(
|
||||||
|
"Found too many implementations of SecureStore:\n%s\nin %s", Joiner
|
||||||
|
.on("\n").join(newSecureStores), secureStore.toAbsolutePath()));
|
||||||
|
}
|
||||||
|
return Iterables.getOnlyElement(newSecureStores);
|
||||||
}
|
}
|
||||||
if (newSecureStores.size() > 1) {
|
|
||||||
throw new RuntimeException(String.format(
|
|
||||||
"Found too many implementations of SecureStore:\n%s\nin %s", Joiner
|
|
||||||
.on("\n").join(newSecureStores), secureStore.toAbsolutePath()));
|
|
||||||
}
|
|
||||||
return Iterables.getOnlyElement(newSecureStores);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getCurrentSecureStoreClassName(SitePaths sitePaths) {
|
private String getCurrentSecureStoreClassName(SitePaths sitePaths) {
|
||||||
|
|
|
@ -310,13 +310,12 @@ public class BaseInit extends SiteProgram {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
Path secureStoreLib = Paths.get(secureStore);
|
||||||
Path secureStoreLib = Paths.get(secureStore);
|
if (!Files.exists(secureStoreLib)) {
|
||||||
if (!Files.exists(secureStoreLib)) {
|
throw new InvalidSecureStoreException(String.format(
|
||||||
throw new InvalidSecureStoreException(String.format(
|
"File %s doesn't exist", secureStore));
|
||||||
"File %s doesn't exist", secureStore));
|
}
|
||||||
}
|
try (JarScanner scanner = new JarScanner(secureStoreLib)) {
|
||||||
JarScanner scanner = new JarScanner(secureStoreLib);
|
|
||||||
List<String> secureStores =
|
List<String> secureStores =
|
||||||
scanner.findSubClassesOf(SecureStore.class);
|
scanner.findSubClassesOf(SecureStore.class);
|
||||||
if (secureStores.isEmpty()) {
|
if (secureStores.isEmpty()) {
|
||||||
|
|
|
@ -53,7 +53,7 @@ import java.util.jar.JarEntry;
|
||||||
import java.util.jar.JarFile;
|
import java.util.jar.JarFile;
|
||||||
import java.util.jar.Manifest;
|
import java.util.jar.Manifest;
|
||||||
|
|
||||||
public class JarScanner implements PluginContentScanner {
|
public class JarScanner implements PluginContentScanner, AutoCloseable {
|
||||||
private static final int SKIP_ALL = ClassReader.SKIP_CODE
|
private static final int SKIP_ALL = ClassReader.SKIP_CODE
|
||||||
| ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES;
|
| ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES;
|
||||||
private final JarFile jarFile;
|
private final JarFile jarFile;
|
||||||
|
@ -131,6 +131,11 @@ public class JarScanner implements PluginContentScanner {
|
||||||
return findSubClassesOf(superClass.getName());
|
return findSubClassesOf(superClass.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() throws IOException {
|
||||||
|
jarFile.close();
|
||||||
|
}
|
||||||
|
|
||||||
private List<String> findSubClassesOf(String superClass) throws IOException {
|
private List<String> findSubClassesOf(String superClass) throws IOException {
|
||||||
String name = superClass.replace('.', '/');
|
String name = superClass.replace('.', '/');
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue