Using MoveClassesOrPackagesRefactoring to refactor a java file

I am trying to write a code which can refactor an input project. I know the file I want to refactor and I want to create a new package in which I want to move this file. (Although unconventional but the file could contain multiple classes and I would want the refactoring to work with such a file too).


I saw that IntelliJ does this brilliantly through the IDE. I also see that there is an openapi available to use the code of Intellij.


I saw that I could use the class MoveClassesOrPackagesRefactoring to achieve this.


What I can't figure out however, is that how do I give the input project folder to the API and also the output folder where I want the refactored code to be put. If output folder cannot be given, I am ok with refactoring the same input project.

I would really appreciate if you could give me a sample code which I  could use to achieve my purpose, or help me understand how to achieve  moving a java file to another package.
5 comments
Comment actions Permalink

Hi,

MoveClassesOrPackagesRefactoring has com.intellij.refactoring.MoveDestination as an input parameter. You need to provide the destination package there. The project would be the same as IDEA doesn't support move between projects (or you mean module?)

Anna

0
Comment actions Permalink

Thanks for your update. Does openapi jar file export MoveClassesOrPackagesRefactoring API? I want to refactor an input project inside my own Java code. I will explain my requirement below:

I  have an input project X whose all files are available inside a folder  X. Let us say there is a package in the project called P which contains a  file C.java. I want to move C.java from P to P1 (create new package).  Given the folder path, how can I specify to the refactoring code the  project X and how to create com.intellij.refactoring.MoveDestination object for package P1 and also how to specify C.java to the API?

A sample code would really help.

0
Comment actions Permalink

Hi Devashi,

please setup IDEA SDK which contains all code you need, after that please read javadoc for com.intellij.refactoring.MoveDestination, there you'll find how to find the move refactoring as well as how to create destination package.

Thanks,
Anna

0
Comment actions Permalink

I am not able to locate the JavaDoc even in the source of community edition. Can you please give me the link for JavaDoc or some source which uses IntelliJ IDEA SDK?

Thanks.

0

Please sign in to leave a comment.