本文用英文写的,主要是考虑到本文分享的内容即使在google上也搜索不到(至少我是没有搜索到)。
My English is at a very low level, don't care about this fact, just focus on the idea shared here.
The idea comes from the source code of
ConfigurationElement which is located in package
org.eclipse.core.internal.registry, If you read through the source code, you can also get it. But I found there is no documentation about this topic, so I wrote this.
Sometimes we need to contribute java class to a extension point. And we can use
ConfigurationElement#
createExecutableExtension(String attributeName) to create an instance of it, if such a class is just a normal class, eclipse will call
class#newInstance(). But there is obvious restriction for using this approach, can't pass parameter in to create instance for example.
There are three different way how eclipse create instance of your class.
- normal, call class#newInstance()
- if your calss implements IExecutableExtension interface, IExecutableExtension#setInitializationData(IConfigurationElement config, String propertyName, Object data) will be called on the instance returned by class#newInstance()
- if your calss implements IExecutableExtensionFactory interface, IExecutableExtensionFactory#create() will be called
When you use the second method or the third method, you can
pass in parameters, check the source code of
ConfigurationElement, you will know how to do that. :D forgive me, I'm lazy.
posted on 2012-04-15 16:33
West Farmer 阅读(319)
评论(0) 编辑 收藏