Skip to content

Commit

Permalink
Use try-with-resources to close a URLClassLoader.
Browse files Browse the repository at this point in the history
Closes #7301.

RELNOTES=n/a
PiperOrigin-RevId: 651126936
  • Loading branch information
dk2k authored and Google Java Core Libraries committed Jul 11, 2024
1 parent 2f6d618 commit 797c730
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -285,17 +285,16 @@ static class DecoupledLoader implements FinalizerLoader {
@Override
@CheckForNull
public Class<?> loadFinalizer() {
try {
/*
* We use URLClassLoader because it's the only concrete class loader implementation in the
* JDK. If we used our own ClassLoader subclass, Finalizer would indirectly reference this
* class loader:
*
* Finalizer.class -> CustomClassLoader -> CustomClassLoader.class -> This class loader
*
* System class loader will (and must) be the parent.
*/
ClassLoader finalizerLoader = newLoader(getBaseUrl());
/*
* We use URLClassLoader because it's the only concrete class loader implementation in the
* JDK. If we used our own ClassLoader subclass, Finalizer would indirectly reference this
* class loader:
*
* Finalizer.class -> CustomClassLoader -> CustomClassLoader.class -> This class loader
*
* System class loader will (and must) be the parent.
*/
try (URLClassLoader finalizerLoader = newLoader(getBaseUrl())) {
return finalizerLoader.loadClass(FINALIZER_CLASS_NAME);
} catch (Exception e) {
logger.log(Level.WARNING, LOADING_ERROR, e);
Expand Down
21 changes: 10 additions & 11 deletions guava/src/com/google/common/base/FinalizableReferenceQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -285,17 +285,16 @@ static class DecoupledLoader implements FinalizerLoader {
@Override
@CheckForNull
public Class<?> loadFinalizer() {
try {
/*
* We use URLClassLoader because it's the only concrete class loader implementation in the
* JDK. If we used our own ClassLoader subclass, Finalizer would indirectly reference this
* class loader:
*
* Finalizer.class -> CustomClassLoader -> CustomClassLoader.class -> This class loader
*
* System class loader will (and must) be the parent.
*/
ClassLoader finalizerLoader = newLoader(getBaseUrl());
/*
* We use URLClassLoader because it's the only concrete class loader implementation in the
* JDK. If we used our own ClassLoader subclass, Finalizer would indirectly reference this
* class loader:
*
* Finalizer.class -> CustomClassLoader -> CustomClassLoader.class -> This class loader
*
* System class loader will (and must) be the parent.
*/
try (URLClassLoader finalizerLoader = newLoader(getBaseUrl())) {
return finalizerLoader.loadClass(FINALIZER_CLASS_NAME);
} catch (Exception e) {
logger.log(Level.WARNING, LOADING_ERROR, e);
Expand Down

0 comments on commit 797c730

Please sign in to comment.