Use Java 7 style exception handling in IoUtil
- The try-with-resources statement ensures that the URLClassReader
is always closed, and allows to remove the suppression of Eclipse's
warning about an unclosed resource.
- Catching multiple exception types in the same block reduces the
amount of duplicated code.
Change-Id: Iead344269bc0b985b2cd2176f15a764e6a0abb70
diff --git a/gerrit-pgm/src/main/java/com/google/gerrit/pgm/util/IoUtil.java b/gerrit-pgm/src/main/java/com/google/gerrit/pgm/util/IoUtil.java
index 1c3d1e3..05c057f 100644
--- a/gerrit-pgm/src/main/java/com/google/gerrit/pgm/util/IoUtil.java
+++ b/gerrit-pgm/src/main/java/com/google/gerrit/pgm/util/IoUtil.java
@@ -58,35 +58,31 @@
throw noAddURL("Not loaded by URLClassLoader", null);
}
- @SuppressWarnings("resource")
- URLClassLoader urlClassLoader = (URLClassLoader) cl;
-
- Method addURL;
- try {
- addURL = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
- addURL.setAccessible(true);
- } catch (SecurityException e) {
- throw noAddURL("Method addURL not available", e);
- } catch (NoSuchMethodException e) {
- throw noAddURL("Method addURL not available", e);
- }
-
- Set<URL> have = Sets.newHashSet(Arrays.asList(urlClassLoader.getURLs()));
- for (File path : jars) {
+ try (URLClassLoader urlClassLoader = (URLClassLoader) cl) {
+ Method addURL;
try {
- URL url = path.toURI().toURL();
- if (have.add(url)) {
- addURL.invoke(cl, url);
- }
- } catch (MalformedURLException e) {
- throw noAddURL("addURL " + path + " failed", e);
- } catch (IllegalArgumentException e) {
- throw noAddURL("addURL " + path + " failed", e);
- } catch (IllegalAccessException e) {
- throw noAddURL("addURL " + path + " failed", e);
- } catch (InvocationTargetException e) {
- throw noAddURL("addURL " + path + " failed", e.getCause());
+ addURL = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
+ addURL.setAccessible(true);
+ } catch (SecurityException | NoSuchMethodException e) {
+ throw noAddURL("Method addURL not available", e);
}
+
+ Set<URL> have = Sets.newHashSet(Arrays.asList(urlClassLoader.getURLs()));
+ for (File path : jars) {
+ try {
+ URL url = path.toURI().toURL();
+ if (have.add(url)) {
+ addURL.invoke(cl, url);
+ }
+ } catch (MalformedURLException | IllegalArgumentException |
+ IllegalAccessException e) {
+ throw noAddURL("addURL " + path + " failed", e);
+ } catch (InvocationTargetException e) {
+ throw noAddURL("addURL " + path + " failed", e.getCause());
+ }
+ }
+ } catch (IOException ioe) {
+ throw noAddURL("Error closing URLClassLoader", ioe);
}
}