How to move around methods within a class ?

I am writing to create a plugin which will order methods in a specific way. 

I was able to get the methods array from the PsiClass.getMethods. I was hoping to find a method within the PsiClass to reset or replace the methods with the newly ordered methods. I am not able to find any. 

PsiMethod[] methods = psiClass.getMethods();
//anything like this ? -> psiClass.setMethods(reOrderedMethods)

I have been looking for ways to do this for the past couple of days without any success. Whatever code I find in github related to re-arranging code, does some sort of text replace. I was hoping to do the re-ordered with some built in functionality around the PsiJavaFile or PsiClass. Please show me some pointers on how to achieve this. 



1 comment

You correctly found the rearranger and it would definitely conflict with your plugin if you don't plug there but create your own order. Say we have move refactoring, it would place the method withing the class according to the rules defined in rearranger. If you would define your rules, then they would be automatically applied.

To the question, there is no way to setMethods, you can either delete all methods of the class and then add them in the new order (remember rearranger, it would work here as well), or you can create a class from text and then you can replace the old class with new one. Please note that you need to use the context of the old class.

So I'd advise to plug inside the rearranger, this way you won't need to bother about modification of the psi yourself and won't get conflicts which would bother users of the plugin



Please sign in to leave a comment.