Context dependent method creation

In IntelliJ IDEA, If I try to call the method that doesn't exist:

MyClass myClass = new MyClass();
String arg1 = "";
int arg2 = 0;
int c = myClass.nonExistentMethod(arg1, arg2);

the editor offers to create the method with required signature and returning value:

class MyClass{
...
     public int nonExistentMethod(String arg1, int arg2){
          return 0;
     }
}

I badly need to extend this functionality to be able to generate not only the method declaration but, in case when MyClass extends particular interface, to generate the body of the method and to do some other work.
Unfortunately, I don't have any idea even how to approach to that task. So I'd appreciate any help, references to articles on the subject, code snippets etc.

3 comments
Comment actions Permalink

Hello Eugeny,

There is no way to extend the standard quickfix but you can provide your
own intention action that will be displayed next to the standard quickfix
and generate the extra code that you need. Intention actions implement the
IntentionAction interface; you can find plenty of examples of those in the
Community Edition source code and user plugins.

The standard "create method from usage" implementation is in CreateMethodFromUsageFix
in the Community Edition source code.

In IntelliJ IDEA, If I try to call the method that doesn't exist:

MyClass myClass = new MyClass();
String arg1 = "";
int arg2 = 0;
int c = myClass.nonExistentMethod(arg1, arg2);
the editor offers to create the method with required signature and
returning value:

class MyClass{
...
+     public int nonExistentMethod(String arg1, int arg2){+
+          return 0;+
+     }+
}
I badly need to extend this functionality to be able to generate not
only the method declaration but, in case when MyClass extends
particular interface, to generate the body of the method and to do
some other work.

Unfortunately, I don't have any idea even how to approach to that
task. So I'd appreciate any help, references to articles on the
subject, code snippets etc.


--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Comment actions Permalink

Thank you very much. That is exactly what I need.

By the way, could you please provide me with the link where I can download community edition source code :)

0
Comment actions Permalink

Hello Eugeny,

http://www.jetbrains.org/

Thank you very much. That is exactly what I need.

By the way, could you please provide me with the link where I can
download community edition source code


--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0

Please sign in to leave a comment.