%classpath config resolver scijava.public https://maven.scijava.org/content/groups/public %classpath add mvn net.imagej imagej 2.0.0-rc-71 net.imagej.ImageJ.class.getResource("/META-INF/json/org.scijava.plugin.Plugin"). openStream().getText().replaceAll("\\}\\{", "}\n{") ij = new net.imagej.ImageJ() "ImageJ v${ij.getVersion()} is ready to go." ctx = ij.context() "There are ${ctx.getPluginIndex().size()} plugins, ${ctx.getServiceIndex().size()} of which are services." ij.event() ctx.service(org.scijava.event.EventService.class) ctx.getService(org.scijava.event.EventService.class) script = """ #@ EventService es "Got an EventService: " + es """ ij.script().run('script.groovy', script, true).get().getReturnValue() import org.scijava.command.Command import org.scijava.log.LogService import org.scijava.plugin.Parameter import org.scijava.plugin.Plugin @Plugin(type = Command.class) public class MyPlugin implements Command { // This @Parameter notation is 'asking' the Context // for an instance of LogService. @Parameter private LogService log; @Parameter private String message; @Override public void run() { // Just use the LogService! // There is no need to construct it, since the Context // has already provided an appropriate instance. log.info(message); } } // Save a reference to the class for later. myPluginClass = MyPlugin.class // TODO: Figure out why the log message in this command is swallowed. // Execute our sample command. ij.command().run(MyPlugin.class, true, "message", "Success!").get() // Manually create a plugin instance. // It is not connected to a Context yet plugin = new MyPlugin() // Inject the plugin instance with our Context. ctx.inject(plugin) // Now that our plugin is injected, we can use it with the // knowledge that its service parameters have been populated. plugin.run() // but message is still null import org.scijava.service.SciJavaService import org.scijava.service.Service import org.scijava.service.AbstractService import org.scijava.app.StatusService import org.scijava.plugin.Plugin import org.scijava.plugin.Parameter // Example Service Interface: public interface HelloService extends SciJavaService { public void sayHello() } // Example implementation: @Plugin(type = Service.class) public class DefaultHelloService extends AbstractService implements HelloService { @Parameter private StatusService status; @Override public void initialize() { // initialize as little as possible here } @Override public void sayHello() { status.showStatus("Howdy!"); } } "HelloService ready" ij.plugin().getPluginsOfType(org.scijava.Gateway.class).stream().map{info -> [ "Class": info.loadClass().getName(), "Location": info.getLocation().replaceAll('.*/(.*\\.jar)$', '$1') ]}.collect() // Create a new SciJava gateway wrapping our existing Context. sj = new org.scijava.SciJava(ctx) // Now bask in the convenience! import org.scijava.service.Service [ "Plugin count" : sj.plugin().getPlugins().size(), "Module count" : sj.module().getModules().size(), "Service count" : sj.plugin().getPluginsOfType(Service.class).size(), "SciJava version" : sj.getVersion(), "Where is SciJava?" : sj.getApp().getLocation() ] // We don't _need_ the gateway; we could use each service directly instead. pluginService = ctx.service(org.scijava.plugin.PluginService.class) moduleService = ctx.service(org.scijava.module.ModuleService.class) import org.scijava.service.Service [ "Plugin count" : pluginService.getPlugins().size(), "Module count" : moduleService.getModules().size(), "Service count" : pluginService.getPluginsOfType(Service.class).size() ]