Renaming a compilation unit [message #1102214] |
Thu, 05 September 2013 10:17  |
Eclipse User |
|
|
|
So I'm trying write a code in my plugin that renames a given compilation unit. I've tried to use the org.eclipse.jdt.internal.corext.refactoring.rename.RenameCompilationUnitProcessor, but it doesn't work. I'm missing something, but can't figure out what. Also there seems to be no documentation on the topic, even the eclipse code is not properly documented. Can anyone help, please?
Here is a snapshot of my code:
CompositeChange composite = new DynamicValidationStateChange(RefactoringCoreMessages.ReorgPolicy_move);
RenameCompilationUnitProcessor renameProcessor = new RenameCompilationUnitProcessor(compUnit);
renameProcessor.setNewElementName("NewName");
Change renameChange = renameProcessor.createChange(new SubProgressMonitor(pm, 1));
if (renameChange instanceof CompositeChange) {
composite.merge(((CompositeChange) renameChange));
} else {
composite.add(renameChange);
}
composite.perform(pm);
|
|
|
Re: Renaming a compilation unit [message #1107447 is a reply to message #1102214] |
Thu, 12 September 2013 08:09  |
Eclipse User |
|
|
|
OK, here is how I did it
//create move policy and processor for the compilation unit
IMovePolicy movePolicy = ReorgPolicyFactory.createMovePolicy(new IResource[] {compUnit.getResource()}, new IJavaElement[] {compUnit});
JavaMoveProcessor processor = new JavaMoveProcessor(movePolicy);
processor.setDestination(ReorgDestinationFactory.createDestination(newSource));
//the refactoring object
Refactoring refactoring = new ProcessorBasedRefactoring(processor);
//set a refactoring wizard
RefactoringWizard wizard = new ReorgMoveWizard(processor, refactoring);
Shell activeShell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
wizard.setContainer(new RefactoringWizardDialog(activeShell, wizard));
//set reorg queries (no idea what it is used for, but doesn't work without it)
processor.setCreateTargetQueries(new CreateTargetQueries(wizard));
processor.setReorgQueries(new ReorgQueries(activeShell));
//perform the refactoring and return its success result
CreateChangeOperation create = new CreateChangeOperation(new CheckConditionsOperation(ref, CheckConditionsOperation.ALL_CONDITIONS), RefactoringStatus.WARNING);
PerformChangeOperation perform = new PerformChangeOperation(create);
IWorkspace workspace = ResourcesPlugin.getWorkspace();
try {
workspace.run(perform, new NullProgressMonitor());
} catch (CoreException e) {
e.printStackTrace();
}
|
|
|
Powered by
FUDForum. Page generated in 0.04176 seconds