Registering actions programatically with ActionManager

Answered

Hello All,

 

I am migrating the plugin from the deprecated component-way to the modern services-listeners-extensions one.

I used to create actions for the context menu as it was described at https://confluence.jetbrains.com/pages/viewpage.action?pageId=36017181

// If you register the MyPluginRegistration class in the <application-components> section of
// the plugin.xml file, this method is called on IDEA start-up.
    public void initComponent() {
        ActionManager am = ActionManager.getInstance();
        TextBoxes action = new TextBoxes();
      // Passes an instance of your custom TextBoxes class to the registerAction method of the ActionManager class.
        am.registerAction("MyPluginAction", action);


However, with latest IDEA this initComponent() method is not invoked anymore for the plugin and there is a link in API to follow:

components are deprecated... Please see
* http://www.jetbrains.org/intellij/sdk/docs/basics/plugin_structure/plugin_components.html for guidelines on migrating to other APIs.



I tried to move the actions-specific code into listeners and services, but none of them allow to execute the line ActionManager.getInstance():

Instead they throw:

java.lang.NullPointerException
at com.intellij.openapi.actionSystem.ActionManager.getInstance(ActionManager.java:39)
at my.plugin.PluginService.initActions(InstantPatchRemotePluginService.java:32)
at my.plugin.AppLifecycleListener.toolWindowRegistered(InstantPatchRemoteAppLifecycleListener.java:25)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at com.intellij.util.messages.impl.MessageBusImpl.invokeListener(MessageBusImpl.java:546)
at com.intellij.util.messages.impl.MessageBusConnectionImpl.deliverMessage(MessageBusConnectionImpl.java:139)
at com.intellij.u...

Is there some specific point in the plugin lifecycle where the actions may be instantiated now?

Thanks

0
3 comments
Avatar
Permanently deleted user

I will add an update:

the reason of the java.lang.NullPointerException is that ApplicationManager.getApplication() returns null.

The threads that happens in are either

"ApplicationImpl pooled thread 7" for instance of com.intellij.ide.AppLifecycleListener

or

"AWT-EventQueue-0" for instance of  com.intellij.openapi.wm.ex.ToolWindowManagerListener

 

There are also few warnings in the console when I run the runIde gradle task (may be they have some consequences)

> Task :runIde

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.intellij.util.ReflectionUtil to method sun.java2d.SunGraphicsEnvironment.isUIScaleEnabled()
WARNING: Please consider reporting this to the maintainers of com.intellij.util.ReflectionUtil
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
0

In the first post, you're referring to the outdated docs - please use https://www.jetbrains.org/intellij/sdk/docs/basics/getting_started.html instead.

Please also follow the Dynamic Plugins for more information about the migration and troubleshooting.

0
Avatar
Permanently deleted user

Using older approach is essentially what I described in my question, and I attempted to migrate to the newer one.

After the migration there are Actions which can not be instantiated due to 

ApplicationManager.getApplication() returns null.

Could you suggest how to overcome that please?

 

0

Please sign in to leave a comment.