Refactoring HowTo

SonarJ allows the user to define virtual refactorings, which result in tasks for developers. Now I am writing the SonarJ plugin and it displays a list of tasks for a specific user. Now of corse, if it is a refactoring I allow the user to execute it in Idea by clicking a button.

Now the smartness of Idea gets in the way. Here is an example: lets assume the task is to rename class com. x.A into com.x.B. The .sonarj file contains a description of the refactoring with the fully qualified name of A, but the file must not be changed by the refactoring operation. But if I use JavaRefactoringFactory.createRename the implementation will find all usages inclusive the one in the .sonarj file. Using "run" on the rename refactoring will therefore change the .sonarj file and all Java and other files, where A is referenced.

The question is how to filter out certain usages and ignore them in the refactoring. If I call findUsages() individually I have to write the whole control flow myself and basically replicate parts of class BaseRefactoringProcessor if I also want the user to be able to preview usages. I cannot simply create an adapter that filters the results of findUsages() because the refactoring processor would never call my adapter.  To make things worse, the preview feature only is usable if you call "run()", which means that there is no programmatic control about which usage positions should be refactored.

Any ideas how to solve that problem would be highly appreciated.

1 comment
Comment actions Permalink

Well, don't use the run() method. This won't programatical control, instead will expect input from the user. Try out the following:

JavaRefactoringFactory factory = JavaRefactoringFactory.getInstance(project); JavaRenameRefactoring rename = factory.createRename(psiElement, renameString); UsageInfo[] usages = rename.findUsages(); //alter the 'usages' array, remove all the usages that you don't want to refactor rename.doRefactoring(usages); // modified 'usages' array


This will perform refactoring without any inputs from the user.

0

Please sign in to leave a comment.