Community
Participate
Working Groups
Trying to execute the ShowInHandler programmatically leads to an exception (see the stracktrace below). The code that is run looks like this: IWorkbench workbench = PlatformUI.getWorkbench(); ICommandService commandService = ( ICommandService )workbench.getService( ICommandService.class ); IHandlerService handlerService = ( IHandlerService )workbench.getService( IHandlerService.class ); Command showInCommand = commandService.getCommand( IWorkbenchCommandConstants.NAVIGATE_SHOW_IN ); Map<String,String> parameters = new HashMap<String, String>(); parameters.put( IWorkbenchCommandConstants.NAVIGATE_SHOW_IN_PARM_TARGET, "my.view.id" ); IEvaluationContext contextSnapshot = handlerService.createContextSnapshot( true ); ExecutionEvent event = new ExecutionEvent( showInCommand, parameters, null, contextSnapshot ); showInCommand.executeWithChecks( event ); Stacktrace: org.eclipse.e4.core.di.InjectionException: org.eclipse.core.commands.ExecutionException: No targetId specified at org.eclipse.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java:63) at org.eclipse.e4.core.internal.di.InjectorImpl.invokeUsingClass(InjectorImpl.java:243) at org.eclipse.e4.core.internal.di.InjectorImpl.invoke(InjectorImpl.java:224) at org.eclipse.e4.core.contexts.ContextInjectionFactory.invoke(ContextInjectionFactory.java:132) at org.eclipse.e4.core.commands.internal.HandlerServiceHandler.execute(HandlerServiceHandler.java:167) at org.eclipse.core.commands.Command.executeWithChecks(Command.java:499) at com.codeaffine.xyz.ui.internal.util.ShowInSupport.execute(ShowInSupport.java:56) at com.codeaffine.xyz.ui.internal.util.ShowInSupport.showIn(ShowInSupport.java:30) at com.codeaffine.xyz.ui.internal.util.ShowInSupportPDETest.testShowIn(ShowInSupportPDETest.java:45) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:592) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.pde.internal.junit.runtime.RemotePluginTestRunner.main(RemotePluginTestRunner.java:62) at org.eclipse.pde.internal.junit.runtime.PlatformUITestHarness$1.run(PlatformUITestHarness.java:47) at org.eclipse.swt.widgets.RunnableLock.run(RunnableLock.java:35) at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:135) at org.eclipse.swt.widgets.Display.runAsyncMessages(Display.java:4145) at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3762) at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$9.run(PartRenderingEngine.java:1113) at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332) at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:997) at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:138) at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:610) at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332) at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:567) at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:150) at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:124) at org.eclipse.pde.internal.junit.runtime.NonUIThreadTestApplication.runApp(NonUIThreadTestApplication.java:54) at org.eclipse.pde.internal.junit.runtime.UITestApplication.runApp(UITestApplication.java:47) at org.eclipse.pde.internal.junit.runtime.NonUIThreadTestApplication.start(NonUIThreadTestApplication.java:48) at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196) at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110) at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79) at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:354) at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:181) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:592) at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:636) at org.eclipse.equinox.launcher.Main.basicRun(Main.java:591) at org.eclipse.equinox.launcher.Main.run(Main.java:1450) at org.eclipse.equinox.launcher.Main.main(Main.java:1426) Caused by: org.eclipse.core.commands.ExecutionException: No targetId specified at org.eclipse.ui.internal.ShowInHandler.execute(ShowInHandler.java:61) at org.eclipse.ui.internal.handlers.HandlerProxy.execute(HandlerProxy.java:290) at org.eclipse.ui.internal.handlers.E4HandlerProxy.execute(E4HandlerProxy.java:90) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:592) at org.eclipse.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java:56) ... 63 more
It's considered a best practice to use IHandlerService.executeCommand(*) to execute commands. The Command.executeWithChecks(*) is a low level API used to implement commands in the Eclipse Workbench. That being said, Show In is a parameterized command. Use org.eclipse.core.commands.ParameterizedCommand.generateCommand(Command, Map) to create ParameterizedCommand object with your command and the correct parameter. PW
I made a JUnit test with your code and with Paul's suggestions from comment 1. Bottom line: * Command.executeWithChecks() fails (as reported) * ParameterizedCommand.executeWithChecks() fails * IHandlerService.executeCommand() and IHandlerService.executeCommandInContext() both succeed. I've run the JUnit on 4.4 M2-ish, 4.3 and 3.8. Everything works on 3.8, so you could maintain 3.8/4.x compatibility by doing: ICommandService commandService = (ICommandService) workbench.getService(ICommandService.class); IHandlerService handlerService = (IHandlerService) workbench.getService(IHandlerService.class); Command showInCommand = commandService.getCommand(IWorkbenchCommandConstants.NAVIGATE_SHOW_IN); handlerService.executeCommand( ParameterizedCommand.generateCommand( showInCommand, Collections.singletonMap( IWorkbenchCommandConstants.NAVIGATE_SHOW_IN_PARM_TARGET, "my.view.id")), null); That said, this is a regression from 3.x, and I will look into creating a fix.
Pushed proposed fix to Gerrit for review: https://git.eclipse.org/r/16776
Released as http://git.eclipse.org/c/platform/eclipse.platform.ui.git/commit/?id=5ed832dc05852503be51f3ab7cb9629c409123eb I've added the test to CommandsTestSuite PW
Fixed in master per comment 4
Verified in 4.4.0.I20131029-2000