How do we create a plugin architecture using the Kotlin JSR-223 implementation? In order to compile code (like via
.compile()) it seems like there must be a class loader instantiated somewhere; and a common pattern with plugins is to load code in a new class loader, start a thread, and then set
Thread.currentThread().contextClassLoader = theNewClassLoader to run the plugin. This allows code from the main application to load classes in the plugin, as it might need to do for serialisation.
Apache BEAM is one example of a framework that uses the context class loader to find the user classes. One way to build a plugin for a pluggable BEAM application, is to compile Kotlin code to a Jar and then load the Jar, setting the context class loader as above. Instantiating a new class loader is a natural part of loading a Jar, so it is easy to set the context class loader. It stands to reason that the REPL infrastructure must also have a new class loader somewhere inside it, though. Would reusing this class loader not be better than generating a Jar from the application, only to load it back into the same application?