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
Please sign in to leave a comment.
PsiElementFactory.createMethod() followed by class.add(method). Seems crude, but it works.
--Dave Griffith
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 ?
Hugo Palma wrote:
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)
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
codeStyleManager.shortenClassReferences(element); should do the job.