Hot reload JOSM plugin during dev?
Taylor Smock
taylor.smock at kaart.com
Thu Jun 22 11:30:30 UTC 2023
1. Not really. Most competent Java IDEs can hot reload a class, with
some caveats (no new methods, no changed method definitions, etc.).
2. Kind of. The problem is that the /classes/ may still be loaded, even
if we have disabled and unloaded the plugin. This is because we
(currently) don't unload the `PluginClassLoader` for the plugin when we
disable it. As a workaround, you can try the following patch:
```
Index: src/org/openstreetmap/josm/plugins/PluginHandler.java
===================================================================
diff --git a/src/org/openstreetmap/josm/plugins/PluginHandler.java b/src/org/openstreetmap/josm/plugins/PluginHandler.java
--- a/src/org/openstreetmap/josm/plugins/PluginHandler.java (revision 18766)
+++ b/src/org/openstreetmap/josm/plugins/PluginHandler.java (date 1687433130406)
@@ -1368,6 +1368,7 @@
Object tUpdatedPlugin = getPlugin(tInfo.name);
if (tUpdatedPlugin instanceof Destroyable) {
((Destroyable) tUpdatedPlugin).destroy();
+ classLoaders.remove(tInfo.name);
PluginHandler.loadPlugins(getInfoPanel(), Collections.singleton(tInfo),
NullProgressMonitor.INSTANCE);
}
```
Once the classloader leaves memory /and gc happens/, you should be able
to completely reload the plugin. This means that your plugin /must/
clean up after itself in the `destroy` function of the main plugin
class. So all of the plugin listeners /must/ be removed from non-plugin
classes. If you add a listener for layer change events, you must remove
that listener. Otherwise the classloader will persist.
On 6/21/23 10:46 PM, Ben Ritter wrote:
> Hi josm-dev,
>
> I am in the very early stages of building a JOSM plugin that will use the
> osm2streets<https://github.com/BudgieInWA/JOSM2Streets/> rust library to
> display street and lane shapes. (The rust library is loaded via JNI and
> called into from Java). During the prototyping stages of the plugin, and of
> the rust library, the startup time of JOSM is getting in the way. My
> problem is discussed in a little more detail at
> https://github.com/BudgieInWA/JOSM2Streets/issues/1
>
> Is there a mechanism that we can use to either
>
> 1. hot reload a plugin, or
> 2. unload a loaded plugin, (so that the new version can subsequently be
> loaded)?
>
> My other line of investigation is having the plugin itself unload and
> reload some internal class using Class Loaded shenanigans
> <https://web.archive.org/web/20140704120535/http://www.codethesis.com/blog/unload-java-jni-dll>.
> It should be possible to unload and reload the class wrapping the native
> part of the plugin at the press of a button. Has anyone done this sort of
> thing or have reference to some "hot reload" library that provides this
> functionality with a nice API?
>
> Any suggestions as to where I should focus my research on this would be
> appreciated.
>
> Cheers,
> Ben Ritter
> BudgieInWA
>
> ---
>
> https://github.com/BudgieInWA/JOSM2Streets/
> https://web.archive.org/web/20140704120535/http://www.codethesis.com/blog/unload-java-jni-dll
More information about the josm-dev
mailing list