Use try-with-resources statements

- instead of finally blocks
- in cases of missing try-finally

Change-Id: I94f481a33d8e6a3180c436245d6e95e4d525280c
This commit is contained in:
Urs Wolfer
2015-06-11 21:44:59 +02:00
parent b17ceb8587
commit 5e90c63dea
112 changed files with 727 additions and 1332 deletions

View File

@@ -102,16 +102,11 @@ public final class GerritLauncher {
return "";
}
try {
final JarFile jar = new JarFile(me);
try {
Manifest mf = jar.getManifest();
Attributes att = mf.getMainAttributes();
String val = att.getValue(Attributes.Name.IMPLEMENTATION_VERSION);
return val != null ? val : "";
} finally {
jar.close();
}
try (JarFile jar = new JarFile(me)) {
Manifest mf = jar.getManifest();
Attributes att = mf.getMainAttributes();
String val = att.getValue(Attributes.Name.IMPLEMENTATION_VERSION);
return val != null ? val : "";
} catch (IOException e) {
return "";
}
@@ -202,28 +197,23 @@ public final class GerritLauncher {
}
final SortedMap<String, URL> jars = new TreeMap<>();
try {
final ZipFile zf = new ZipFile(path);
try {
final Enumeration<? extends ZipEntry> e = zf.entries();
while (e.hasMoreElements()) {
final ZipEntry ze = e.nextElement();
if (ze.isDirectory()) {
continue;
}
try (ZipFile zf = new ZipFile(path)) {
final Enumeration<? extends ZipEntry> e = zf.entries();
while (e.hasMoreElements()) {
final ZipEntry ze = e.nextElement();
if (ze.isDirectory()) {
continue;
}
String name = ze.getName();
if (name.startsWith("WEB-INF/lib/")) {
String name = ze.getName();
if (name.startsWith("WEB-INF/lib/")) {
extractJar(zf, ze, jars);
} else if (name.startsWith("WEB-INF/pgm-lib/")) {
// Some Prolog tools are restricted.
if (prologCompiler || !name.startsWith("WEB-INF/pgm-lib/prolog-")) {
extractJar(zf, ze, jars);
} else if (name.startsWith("WEB-INF/pgm-lib/")) {
// Some Prolog tools are restricted.
if (prologCompiler || !name.startsWith("WEB-INF/pgm-lib/prolog-")) {
extractJar(zf, ze, jars);
}
}
}
} finally {
zf.close();
}
} catch (IOException e) {
throw new IOException("Cannot obtain libraries from " + path, e);
@@ -257,20 +247,13 @@ public final class GerritLauncher {
private static void extractJar(ZipFile zf, ZipEntry ze,
SortedMap<String, URL> jars) throws IOException {
File tmp = createTempFile(safeName(ze), ".jar");
FileOutputStream out = new FileOutputStream(tmp);
try {
InputStream in = zf.getInputStream(ze);
try {
byte[] buf = new byte[4096];
int n;
while ((n = in.read(buf, 0, buf.length)) > 0) {
out.write(buf, 0, n);
}
} finally {
in.close();
try (FileOutputStream out = new FileOutputStream(tmp);
InputStream in = zf.getInputStream(ze)) {
byte[] buf = new byte[4096];
int n;
while ((n = in.read(buf, 0, buf.length)) > 0) {
out.write(buf, 0, n);
}
} finally {
out.close();
}
String name = ze.getName();
@@ -363,24 +346,16 @@ public final class GerritLauncher {
final CodeSource src =
GerritLauncher.class.getProtectionDomain().getCodeSource();
if (src != null) {
try {
final InputStream in = src.getLocation().openStream();
try {
final File tmp = createTempFile("gerrit_", ".zip");
final FileOutputStream out = new FileOutputStream(tmp);
try {
final byte[] buf = new byte[4096];
int n;
while ((n = in.read(buf, 0, buf.length)) > 0) {
out.write(buf, 0, n);
}
} finally {
out.close();
try (InputStream in = src.getLocation().openStream()) {
final File tmp = createTempFile("gerrit_", ".zip");
try (FileOutputStream out = new FileOutputStream(tmp)) {
final byte[] buf = new byte[4096];
int n;
while ((n = in.read(buf, 0, buf.length)) > 0) {
out.write(buf, 0, n);
}
return tmp;
} finally {
in.close();
}
return tmp;
} catch (IOException e) {
// Nope, that didn't work.
//