How to use IJavaRefactorings.DELETE refactoring? [message #255218] |
Thu, 24 July 2008 03:48 |
Eclipse User |
|
|
|
I've managed to do this, but it looks a little hackish, because I
have to look into JavaDeleteProcessor and indetify which attributes must
be filled and even can not reference these attributes are constants, and
have to copy these constants into my source.
Is there any better solution?
private static final String ATTRIBUTE_RESOURCES = "resources";
private static final String ATTRIBUTE_ELEMENTS = "elements";
private static final String ATTRIBUTE_SUGGEST_ACCESSORS = "accessors";
private static final String ATTRIBUTE_DELETE_SUBPACKAGES = "subPackages";
/**
* Deletes {@link ICompilationUnit}.
*/
protected static void deleteCompilationUnit(ICompilationUnit
compilationUnit) throws Exception {
IProgressMonitor monitor = new NullProgressMonitor();
// prepare RefactoringDescriptor
DeleteDescriptor refactoringDescriptor;
{
Map<Object, Object> arguments = Maps.newHashMap();
arguments.put(ATTRIBUTE_DELETE_SUBPACKAGES, "false");
arguments.put(ATTRIBUTE_SUGGEST_ACCESSORS, "false");
arguments.put(ATTRIBUTE_RESOURCES, "0");
arguments.put(ATTRIBUTE_ELEMENTS, "1");
arguments.put("element1", compilationUnit.getHandleIdentifier());
refactoringDescriptor =
(DeleteDescriptor)
RefactoringCore.getRefactoringContribution(IJavaRefactorings .DELETE).createDescriptor(
IJavaRefactorings.DELETE,
compilationUnit.getJavaProject().getElementName(),
"Delete " + compilationUnit.getElementName(),
"",
arguments,
0);
}
// perform refactoring
RefactoringStatus refactoringStatus = new RefactoringStatus();
Refactoring refactoring =
refactoringDescriptor.createRefactoring(refactoringStatus);
refactoring.checkAllConditions(monitor);
Change change = refactoring.createChange(monitor);
change.perform(monitor);
}
For comparision here is also using IJavaRefactorings.MOVE which
provide configuration method setMoveResources(), but I can not find such
method for DeleteDescriptor...
/**
* Moves {@link ICompilationUnit} into new package.
*/
protected static void moveCompilationUnit(ICompilationUnit
compilationUnit, IPackageFragment newPackage)
throws Exception {
IProgressMonitor monitor = new NullProgressMonitor();
// prepare RefactoringDescriptor
MoveDescriptor refactoringDescriptor;
{
refactoringDescriptor =
(MoveDescriptor)
RefactoringCore.getRefactoringContribution(IJavaRefactorings .MOVE).createDescriptor();
refactoringDescriptor.setMoveResources(
new IFile[]{},
new IFolder[]{},
new ICompilationUnit[]{compilationUnit});
refactoringDescriptor.setDestination(newPackage);
refactoringDescriptor.setUpdateReferences(true);
}
// perform refactoring
RefactoringStatus refactoringStatus = new RefactoringStatus();
Refactoring refactoring =
refactoringDescriptor.createRefactoring(refactoringStatus);
refactoring.checkAllConditions(monitor);
Change change = refactoring.createChange(monitor);
change.perform(monitor);
}
|
|
|
Powered by
FUDForum. Page generated in 0.03993 seconds