Programatically calling eclipse Refactoring API - to rename types used and method invocations [message #1705142] |
Wed, 12 August 2015 07:50 |
Eclipse User |
|
|
|
My requirement is to identify deprecated class usage (either as field declaration/used as method parameter/within a method as statement) and have it renamed programatically.
Similarly to also identify deprecated method usage(all method invocations of a deprecated class) and rename the method with new method name+ new argument list.
I am trying to use the RefactoringContribution and RenameJavaElementDescriptor APIs and setting IJavaRefactorings.RENAME_TYPE.
This fails in checkInitialConditions of refactoring. Given below the code piece used.
Pl advice if Rename Type is the correct one for my requirement.
code snippet
------------------
RefactoringContribution contribution =
RefactoringCore.getRefactoringContribution(IJavaRefactorings.RENAME_RENAME_TYPE);
RenameJavaElementDescriptor descriptor =
(RenameJavaElementDescriptor) contribution.createDescriptor();
descriptor.setJavaElement(field);// field declaration containing deprecated class definition
descriptor.setProject(cu.getResource().getProject().getName( ));
descriptor.setNewName("java.io.BufferedReader"); // new type name for a deprecated Class
RefactoringStatus status = new RefactoringStatus();
try {
Refactoring refactoring = descriptor.createRefactoring(status);
System.out.println("Refactoring instance created..." + refactoring.getName());
IProgressMonitor monitor = new NullProgressMonitor();
//for testing
//IProgressMonitor monitor = null;
refactoring.checkInitialConditions(monitor);
refactoring.checkFinalConditions(monitor);
System.out.println("Refactoring conditions created... start change...");
Change change = refactoring.createChange(monitor);
change.perform(monitor);
System.out.println("Refactoring ... change completed...");
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
logMsg.append("CoreException: " + e.getMessage());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
logMsg.append("Exception: " + e.getCause());
}
Thanks in advance
Srila
|
|
|
Powered by
FUDForum. Page generated in 0.02689 seconds