How can I programmatically change the package of a class in Eclipse. In a way to have the same result as when drag-dropping the class into another package. I assume it must be done using AST, but can't find a proper tutorial describing it.
Asked
Active
Viewed 114 times
2 Answers
1
Ok, I will answer to my own question (like a Sir:)). I had to debug into Eclipse and here is how it works:
CompositeChange composite = new DynamicValidationStateChange(RefactoringCoreMessages.ReorgPolicy_move);
MoveCuUpdateCreator creator = new MoveCuUpdateCreator(new ICompilationUnit[] {compUnit}, (IPackageFragment) newContainer);
TextChangeManager fChangeManager = creator.createChangeManager(new SubProgressMonitor(pm, 1), new RefactoringStatus());
composite.merge(new CompositeChange(RefactoringCoreMessages.MoveRefactoring_reorganize_elements, fChangeManager.getAllChanges()));
Change change = new MoveCompilationUnitChange(compUnit, (IPackageFragment) newContainer);
if (change instanceof CompositeChange) {
composite.merge(((CompositeChange) change));
} else {
composite.add(change);
}
composite.perform(pm);

Vahan
- 45
- 8
-1
Select the class declaration and do Alt-Shift-V. Alternately, right click, refactoring, Move...
It should handle updating imports and moving the compilation unit.

roippi
- 25,533
- 4
- 48
- 73
-
What I need is to do it in code. Something similar to [this](http://stackoverflow.com/questions/12968328/how-to-programmatically-rename-a-method-using-jdt), but for moving a class. – Vahan Jul 15 '13 at 16:08
-
Oh, _programmatically_. K. Can't help you there. – roippi Jul 15 '13 at 16:15