From a63cd8ff51938186c7a5ca3a0a4c0a2fae1493ba Mon Sep 17 00:00:00 2001
From: Mark Hiner
Date: Fri, 14 Aug 2020 13:18:46 -0500
Subject: [PATCH] Make Context AutoCloseable
This will provide developers hints that the Context should be disposed
after creation.
See https://github.com/scijava/scijava-common/pull/394
---
src/main/java/org/scijava/Context.java | 16 +++++++++++++++-
1 file changed, 15 insertions(+), 1 deletion(-)
diff --git a/src/main/java/org/scijava/Context.java b/src/main/java/org/scijava/Context.java
index e4d2bda14..2576b4c37 100644
--- a/src/main/java/org/scijava/Context.java
+++ b/src/main/java/org/scijava/Context.java
@@ -58,7 +58,7 @@
* @author Curtis Rueden
* @see Service
*/
-public class Context implements Disposable {
+public class Context implements Disposable, AutoCloseable {
// -- Constants --
@@ -248,6 +248,13 @@ public Context(final Collection> serviceClasses,
* those of lower priority). See {@link ServiceHelper#loadServices()} for more
* information.
*
+ *
+ * NB: Instiantiation of a Context has an implied requirement of a
+ * corresponding call to {@link Context#dispose()} at the end of the SciJava
+ * applicaton's lifecycle. This cleans up any remaining resources and allows
+ * the JVM to exit gracefully. This is called automatically when constructed as
+ * an {@link AutoCloseable}.
+ *
*
* @param serviceClasses A collection of types that implement the
* {@link Service} interface (e.g., {@code DisplayService.class}).
@@ -423,6 +430,13 @@ public void dispose() {
}
}
+ // -- AutoCloseable methods --
+
+ @Override
+ public void close() throws Exception {
+ dispose();
+ }
+
// -- Utility methods --
/**