Câu trả lời dưới đây là rất tốt, nhưng tôi đã trả lời với một viễn cảnh rộng lớn hơn đối với những người cần một cuộc khủng hoảng cồng kềnh hơn và ngon của tuyệt vời cake
này:
RefactoringStatus status = new RefactoringStatus();
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IWorkspaceRoot root = workspace.getRoot();
IProject[] projects = root.getProjects();
thì:
for (ICompilationUnit unit : mypackage.getCompilationUnits()) {
IType primary = unit.findPrimaryType();
IMethod[] methods = primary.getMethods();
int i = 1;
for (IMethod method : methods) {
if (method.isConstructor()) {
continue;
}
makeChangetoMethods(status, method,"changedMethodVersion_" + i);
++i;
}
}
Sau đó:
IProgressMonitor monitor = new NullProgressMonitor();
status = new RefactoringStatus();
Refactoring refactoring = performMethodsRefactoring(status, methodToRename, newName);
thì:
Change change = refactoring.createChange(monitor);
change.perform(monitor);
tìm thấy bên dưới đoạn code để thiết lập các descriptor
:
String id = IJavaRefactorings.RENAME_METHOD;
RefactoringContribution contrib = RefactoringCore.getRefactoringContribution(id);
RenameJavaElementDescriptor desc = contrib.createDescriptor();
desc.setUpdateReferences(true);
desc.setJavaElement(methodToRename);
desc.setNewName(newName);
desc.createRefactoring(status);
Tôi nghĩ rằng một tập tin trong thư mục eclipse của bạn: Core và UI Một lớp con của org.eclipse.ltk.core.refactoring.Refactoring phải luôn luôn được tạo ra. Kiểm tra văn bản tại Core và UI và "Action!" –
Các API LTK cho phép cấu trúc lại của nó được cấu hình với một đối tượng trái ngược với giao diện người dùng. Trong khi nó cần phải là một plugin eclipse, bạn có thể viết một IApplication không đầu mà có thể điền vào cấu hình tái cấu trúc từ dòng lệnh và thực hiện nó chống lại không gian làm việc của bạn. –
http://www.eclipse.org/articles/article.php?file=Article-Unleashing-the-Power-of-Refactoring/index.html là cuộc thảo luận về API tái cấu trúc. Các 'RefactoringDescriptors' có thể được sử dụng để lập trình thiết lập một sự thay đổi và thực hiện nó trong một hoạt động mà không cần liên quan đến GUI hoặc trình thủ thuật. –