Skip to content

Commit

Permalink
Make Context AutoCloseable
Browse files Browse the repository at this point in the history
This will provide developers hints that the Context should be disposed
after creation.

See #394
  • Loading branch information
hinerm committed Aug 14, 2020
1 parent 9970cef commit a63cd8f
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/main/java/org/scijava/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
* @author Curtis Rueden
* @see Service
*/
public class Context implements Disposable {
public class Context implements Disposable, AutoCloseable {

// -- Constants --

Expand Down Expand Up @@ -248,6 +248,13 @@ public Context(final Collection<Class<? extends Service>> serviceClasses,
* those of lower priority). See {@link ServiceHelper#loadServices()} for more
* information.
* </p>
* <p>
* 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}.
* </p>
*
* @param serviceClasses A collection of types that implement the
* {@link Service} interface (e.g., {@code DisplayService.class}).
Expand Down Expand Up @@ -423,6 +430,13 @@ public void dispose() {
}
}

// -- AutoCloseable methods --

@Override
public void close() throws Exception {
dispose();
}

// -- Utility methods --

/**
Expand Down

0 comments on commit a63cd8f

Please sign in to comment.