Skip to content

Commit a63cd8f

Browse files
committed
Make Context AutoCloseable
This will provide developers hints that the Context should be disposed after creation. See #394
1 parent 9970cef commit a63cd8f

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/main/java/org/scijava/Context.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
* @author Curtis Rueden
5959
* @see Service
6060
*/
61-
public class Context implements Disposable {
61+
public class Context implements Disposable, AutoCloseable {
6262

6363
// -- Constants --
6464

@@ -248,6 +248,13 @@ public Context(final Collection<Class<? extends Service>> serviceClasses,
248248
* those of lower priority). See {@link ServiceHelper#loadServices()} for more
249249
* information.
250250
* </p>
251+
* <p>
252+
* NB: Instiantiation of a Context has an implied requirement of a
253+
* corresponding call to {@link Context#dispose()} at the end of the SciJava
254+
* applicaton's lifecycle. This cleans up any remaining resources and allows
255+
* the JVM to exit gracefully. This is called automatically when constructed as
256+
* an {@link AutoCloseable}.
257+
* </p>
251258
*
252259
* @param serviceClasses A collection of types that implement the
253260
* {@link Service} interface (e.g., {@code DisplayService.class}).
@@ -423,6 +430,13 @@ public void dispose() {
423430
}
424431
}
425432

433+
// -- AutoCloseable methods --
434+
435+
@Override
436+
public void close() throws Exception {
437+
dispose();
438+
}
439+
426440
// -- Utility methods --
427441

428442
/**

0 commit comments

Comments
 (0)