Generate implemented methods

I'm generating a class using the new PsiDirectory.createClass method. I then add some interfaces to the implements list of the class using PsiClass.getImplementsList().add.

All this works fine, but what i don't know how to do is also generate the method body of all the methods that are declared in the implemented interfaces. My goal is to generate a compiling class so i need to also generate the method bodies.


Thanks

5 comments

PsiElementFactory.createMethod() followed by class.add(method). Seems crude, but it works.

--Dave Griffith

0

Thanks, that worked great except for one small detail.

I had to add the parameters with getParameterList().add() method, which is not a problem by itself. The problem is that the parameter gets generated with the fqn instead of just the class name and an import.

Any ideas how i can do this ?

0

Hugo Palma wrote:

I had to add the parameters with getParameterList().add() method, which is not a problem by itself. The problem is that the parameter gets generated with the fqn instead of just the class name and an import.
Any ideas how i can do this ?

I have pretty little experience with plugin development myself, but I think this should work:
CodeStyleManager styleManager = psiManager.getCodeStyleManager();
styleManager.reformat(method);
(or simply for the class as a whole)

0

It's not exactly that but you gave me a push in the right direction.

It's styleManager.shortenClassReferences(method) that does the trick.

Thanks

0

codeStyleManager.shortenClassReferences(element); should do the job.

0

Please sign in to leave a comment.