Invoke refactoring from plugin

Dear developers,

do you know a way to programmatically invoke a refactoring on a class out of my plugin code? Let's say I have a PsiField and I want to encapsulate the field in a getter method. Is there any API available in IDEA that I can use in my plugin?

Thanks a lot in advance!
Christian

1 comment

Hi Christian,

com.intellij.refactoring.RefactoringActionHandlerFactory.getInstance().createEncapsulateFieldsHandler() gives you access to the refactoring.
You need to open the containing file in an editor and the cursor has to be on the field that will be encapsulated for invoke to work correctly. You can do that by calling navigate(true) on the PsiField instance before invoking the refactoring.
AFAIK you won't be able to prevent the popup window where you can define several settings for the refactoring from showing up.

Cheers,
Robert

0

Please sign in to leave a comment.