From: Shlomy R. <sre...@gm...> - 2009-07-31 14:19:09
|
Hi, Before I dive into details, please avoid checking the types of edit bus messages of other plugins using "instanceof", such as: void handleMessage(EBMessage message) { if (message instanceof ProjectUpdate) ... } Instead, use something like: void handleMessage(EBMessage message) { if (message.getClass().getName().endsWith("ProjectUpdate")) ... } Why? The "instanceof" version can cause the other plugin to be loaded - i.e. if the plugin which defines the message type (ProjectViewer in this case) is not loaded, the JARClassLoader will be invoked to load it. And one of the first things that JARClassLoader does is activate the plugin. All this can happen just because the type of the edit bus message was checked - not because the plugin really had to be loaded (or activated). The problem is a lot worse than just wasting resources. In the above example, ProjectViewer will be loaded (if it wasn't loaded before) and activated. Part of the activation is instantiating the plugin and calling its "start" method. In the case of ProjectViewer, this will also cause an edit bus message (ProjectUpdate) to be sent. Then, the above 'handleMessage' will be called again, recursively, and will check again if the message is "instanceof ProjectUpdate". Since the JARClassLoader that was invoked to load the ProjectViewer hasn't returned yet, the ProjectViewer is considered as currently being loaded, but not loaded yet. So this recursive invocation of "handleMessage" will generate a "ClassCircularityError" exception, and I am not sure what the consequences are. Instead of using "instanceof", which might load and activate the plugin, it should be safe to use "message.getClass().getName()" and check if the name is the type we expect. This will not cause the plugin to be loaded. Perhaps at some point we'll add something to the JARClassLoader to take care of this (if possible), but until then, please avoid using "instanceof" with classes of other plugins. Note that the danger is only for plugins that might not yet be loaded when this edit bus message is sent. If you are sure that the other plugin is already loaded, there is no problem with using "instanceof". Thanks, Shlomy |