When to choose “Generate an activator…” when creating a new Eclipse plugin project

笑着哭i 提交于 2019-12-04 04:43:37

One way to find out is to look at the generated class. Turns out that it is a subclass of AbstractUIPlugin. Check out the JavaDoc, it provides services like preference management, image registry and the like. If you need any of this, you may want to use it. It is a subclass of Plugin, which kind of makes sense.

Also, it implements BundleActivator, which has some useful JavaDoc. This provides you with stubs for start() and stop(), which allows you to hook your own code in here. It also generates a static convenience method getDefault(), which gives you the Activator. And that's all there is to it.

From Eclipse itself (context sensitive help for the dialog) it says this, which is marginally useful.

"An activator is a Java class that controls the plug-in's life cycle. It is only needed if you require to do work upon the startup or shutdown of your plug-in."

When turning this option ON, an Activator.java class is auto-generated for your new project.

So, it sounds like if (being somewhat a novice) you have no idea why or what additional work you have to do at plugin startup/shutdown, you can safely leave this OFF. Just one less .java file showing up in your project source folder.

If you really want to know, take a look at the OSGi specification; release 4 is the current version. Since Eclipse 3, every plugin is an OSGi bundle. The bundle activator is notified when the bundle is started and stopped, which usually happens when Eclipse starts and shuts down. You can also install listeners that are notified when other bundles (i.e. plugins) are started or register OSGi services.

For example, I use a listener to start certain operations after my bundle has completed its startup; otherwise I may run into classloader issues. You may also need the activator to store the BundleContext, which lets you load classes and gives you access to the bundle's name and version.

Here's the closest thing to an explanation I've found:

http://dev.eclipse.org/newslists/news.eclipse.platform.rcp/msg23445.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!