症状:
运行RCP application后,在Project Explorer里面右键某个项目,可以看到context menu里面有Run As这么个选项,可是当第二次右键同一个项目时,Run As却消失了,不管你有没有真正地Run这个application,Run As都只会在context menu中出现一次。
调查:
在org.eclipse.debug.ui这个插件的extension point中有这么一段逻辑
<extension point="org.eclipse.ui.popupMenus">
...
<objectContribution
objectClass="java.lang.Object"
id="org.eclipse.debug.ui.contextualLaunch.run">
<action
label="%RunContextMenu.label"
style="pulldown"
class="org.eclipse.debug.internal.ui.actions.RunContextualLaunchAction"
menubarPath="additions"
enablesFor="+"
id="org.eclipse.debug.ui.contextualLaunch.run.submenu">
</action>
<enablement>
<or>
<and>
<not><with variable="org.eclipse.core.runtime.Platform">
<test property="org.eclipse.core.runtime.bundleState" args="org.eclipse.debug.core" value="ACTIVE"/>
</with></not>
<adapt type="org.eclipse.core.resources.IResource"/>
<and>
<with variable="org.eclipse.core.runtime.Platform">
<test property="org.eclipse.core.runtime.bundleState" args="org.eclipse.debug.core" value="ACTIVE"/>
</with>
<test property="org.eclipse.debug.core.launchable" value="run"/>
</and>
</or>
</enablement>
</objectContribution>
...
</extension>
仔细分析这段逻辑,可以看出,org.eclipse.core.runtime.bundleState这个属性是否为ACTIVE,即这个plugin是否已经被load,会对Run As这个context menu是否enable有不同的影响。
在bundleState没有ACTIVE的情况下,只要是可适应(adaptable to)org.eclipse.core.resources.IResource的都会显示出来;在bundleState ACTIVE的情况下,会通过执行org.eclipse.debug.core.launchable这个测试属性来判断。org.eclipse.debug.core.launchable定义于org.eclipse.debug.core,在org.eclipse.debug.internal.core.LaunchablePropertyTester 中执行一个测试方法。这个方法会检测是否有 run 这个launch mode存在,并且选中的这个项目或者文件是否有org.eclipse.debug.ui.actions.ILaunchable这个adaptor。
解决方法:
把ILaunchable这个adaptor加到相应的文件或者项目中去
即,在plugin.xml中加上这么一段
<extension point="org.eclipse.core.runtime.adapters">
<factory class="org.eclipse.core.runtime.IAdaptable"
adaptableType="org.eclipse.core.resources.IResource">
<adapter type="org.eclipse.debug.ui.actions.ILaunchable"/>
</factory>
</extension>