Using Intellijidea refactoring programtically

Hello,

I would ask if some one has already used intellijidea programatically and if he is I would like get an overview about how he does this.

An other question : how to get PsiMethod ?

23 comments
Comment actions Permalink

Yes, we, the developers of IntelliJ IDEA, have used it programmatically. What exactly are you trying to accomplish?

0
Comment actions Permalink

Thank you for your fast reply,
I woul like to implement a program trasnformation by composing the refactoring operations that contains IntellijIdea.  I would like to be able to write this trasnformation, get the elements of the program to transform and call each refactoring operation with the right arguments in a plugin development.

0
Comment actions Permalink

You can use the JavaRefactoringFactory interface to invoke Java refactorings programmatically.

As for getting a PsiMethod instance, please refer to http://confluence.jetbrains.net/display/IDEADEV/IntelliJ+IDEA+Architectural+Overview

0
Comment actions Permalink

I tried already to use it but i does not contain all refactoring operations. For example in the following I try to get an instance of it, then when I try to manipulate this instance I find only createMoveHandler, createRenamedHandler and createsafeDeleteHandler :

RefactoringActionHandlerFactory fact = RefactoringActionHandlerFactory.getInstance();
         RefactoringActionHandler move = fact.createMoveHandler();


Also, when we invoke for example createMoveHandler(), how it will know the right arguments (the method to move, the destination...) ?

0
Comment actions Permalink

You need to use the JavaRefactoringFactory interface, not RefactoringActionHandlerFactory. It contains most of the Java refactorings and allows you to specify parameters for the refactorings.

0
Comment actions Permalink

Thank you, I tried but I didn't find some operations like
for example changeMethodSignature or extractSuperClass that I need in my transformatio
n. Do you have any idea about how to add these operations or where to find them please ?

0
Comment actions Permalink

For these refactorings you need to work with the underlying implementation classes (ChangeSignatureProcessor, ExtractSuperClassProcessor) directly.

0
Comment actions Permalink

Thank a lot, but just I would ask If you have already a simple example to start with and that demonstrates exactly how to use the JavaRefactoring Instances and dierct use of the underlying classes that you show to me.


I have already also used Eclipse JDT to do the same thing but IntellijIdea contains what I need that is why I prefer to implment my trasnformation with it.

0
Comment actions Permalink

Unfortunately we don't have any isolated simple examples. You can look at how these classes are used in the IntelliJ IDEA codebase, and in particular in the tests.

0
Comment actions Permalink

Thank you very much

0
Comment actions Permalink

Hello Jemerov,
I started to use refactoring operations programatically, it was ok for two operations but I am stopped with the Class MyExtractMethodProcessor and exactly how to enter the good arguments, did you have a description for each argument or a link that describes these arguments especially how to choose the second argument of type Editor and what does it mean ?

And thanks

0
Comment actions Permalink

Editor is the editor in which the refactoring is invoked.

0
Comment actions Permalink

Thanks for your fast replies,

I have another thing to say : when I try to IntroduceParameterObject to a method that does not contain any parameter, the operation does not work, is this a bug ?

0
Comment actions Permalink

By definition, this refactoring replaces a number of parameters with a single object that encapsulates all of these parameters. There is no reasonable way for it to work for methods which don't have any parameters.

0
Comment actions Permalink

Good morning,

But when I use the same operation that exists in Eclipse, I could use it with a method without parameters. Any way, I remark also that when I use the operation ChangeSignature programatically, I don't find the setting parameter that permits to me to add a parameter since this operation offers this option in the UI operation.

0
Comment actions Permalink

So what does the refactoring do in Eclipse in this case - create an empty class? Why does this have any value?

Adding parameters in the Change Signature refactoring is done by adding ParameterInfoImpl objects for the new parameters in the parameterInfo parameter of ChangeSignatureProcesssor.

0
Comment actions Permalink

Yes, i creates a new empty class, I use this when I like to move a method to a new class so this operation allows to create this new class and then I could move the method to the target class.

For the operation Change Signature, it fails also when I apply it to a method that does not contain any parameter (of corse I talk here about its use programatically).

0
Comment actions Permalink

There are much more straightforward methods of creating a new class.

The refactoring fails because you're not using it correctly.

0
Comment actions Permalink

Hello Jemerov,

when I use MoveMembersProcessor to move methods, the operation does not care about the change of the old references to be updated to the new conditions. Example :

   class A {
    public void m(B b){
       System.out.print("display this class"+this);
    }

When I aplly the indicated operation programatically, I get :

   class B {

     public void m(B b){
       System.out.print("display this class"+this);
    }
   }

Instead of :

class B {

     public void m(A a){
       System.out.print("display this class"+a);
    }
   }

0
Comment actions Permalink

I don't get from your explanation what the problem is. The Move Members refactoring moves a method from one class to another. It doesn't change the parameter types, regardless of whether you invoke it programmatically or from the UI.

0
Comment actions Permalink

The problem here is that the moved method m should look like  :

  public void m(A a){
    System.out.print("dipaly"+a);
  }
in the target class B.

Because the call to this in the source class has as Type A and when the method is moved to B the reference this becomes a call to an instance of the class A, but in my case (maybe me I use it in the wrong way) the method m still look like :

public void m(B b){
    System.out.print("dipaly"+this);
  }
in the class B and this is wrong because the method is defined to display an instance of the class A not B independently in where it will be moved.

0
Comment actions Permalink

Once again, the Move Members refactoring simply moves a piece of code from one place to another; it doesn't change what the code does. If it referenced an instance of some class before you moved it, it will do the exactly same thing after the move.

0
Comment actions Permalink

Hello,

I am using now the operation move correctly, but I have a problem with the operation extractMethod : the problem is that I cnnot choose the right parameter elements and what it does it mean in this context. I tryed to affect to elements the children of a statement of method but it does not work. Have you any idea about this please ?

0

Please sign in to leave a comment.