ISelection -- How can I get ClassLoader for the selected class in the navigator
I am new to Eclipse plugin development, and I am trying to develop a plugin where I am required to load a class which is sel开发者_开发技巧ected in the Navigator.
Can you please instruct me how can I load a class or create a classloader from eclipse plugin, to load a class in the eclipse workbench which is using my plugin.
Thanks in advance.
Regards Gillani
You need to create a URLClassLoader. You may want it to be parented or not, depending on your security concerns. If parented by an OSGi bundle class loader, then the user will have access to all of the classes in that particular bundle and this may be a back door to the user getting runtime access to the entire Eclipse platform (and access the plugin registry and the OSGi services, etc). The user can also call System.exit()
. This may not be a problem on a single user system, but it is something to think about, especially if the user may be downloading unverified scripts from the internet.
That being said, you need to do something like this:
- Instantiate a URLClassLoader and add the URLs of the directories containing the class files you will want to load.
- if you want the classes to have access to the Eclipse runtime, then set the parent loader to be
((BundleHost) Activator.getBundleContext().getBundle()).getLoaderProxy().getBundleLoader()
- Add all of the directories corresponding to URLs where the user can load classes from. But, you must include all dependencies in order to load the classes.
- This should be enough to load the classes you require.
精彩评论