See: Description
Package | Description |
---|---|
org.netbeans.modules.refactoring.api | |
org.netbeans.modules.refactoring.api.ui | |
org.netbeans.modules.refactoring.spi | |
org.netbeans.modules.refactoring.spi.ui |
The Refactoring API module provides UI and API framework for easy and uniform implementation of refactoring features. The Refactoring module provides:
When a ScopeProvider should be displayed, it returns true from the initialize method, but it will not work or work as expected the ScopeProvider can return a Problem. Added the method getProblem() to ScopeProvider and getScopeProvider() to ScopePanel.
Added the property dependencies in class Scope to include dependencies of the specified scope, like libraries, used by WhereUsedQuery.
Added the method setSelected, to introduce a way to update a filter's selected state after creation.
RefactoringCommit now implements the interface ProgressProvider to notify the progress of the commit.
Added an ExpandableTreeElement and a declarative way to add new scopes.
Refactoring API Example:
Intention: Programatically rename java filecom/company/Test.java
to com/company/RenamedTest.java
and update references.
FileObject fo = ...com/company/Test.java... RefactoringSession renameSession = RefactoringSession.create("Rename Class"); refactoring = new RenameRefactoring(fo); Problem pre = refactoring.preCheck(); if (pre!=null && pre.isFatal()) { //fatal problem in precheck return; } refactoring.setNewName("RenamedTest"); Problem p = refactoring.prepare(renameSession); if (p!=null && p.isFatal()) { //fatal problem in precheck return; } renameSession.doRefactoring(true /* saveAll */);
Refactoring SPI permit other modules to plug into existing refactorings and allow them to participate.
Client of SPI must implement factory class RefactoringPluginFactory
and register this class into Lookup.
Intention: Create a plugin for RenameRefactoring, which will participate in existing refactoring (let say in java refactoring) and renames references in XML files.
//implementation of factory class public class J2EERefactoringFactory implements RefactoringPluginFactory { public RefactoringPlugin createInstance(AbstractRefactoring refactoring) { if (refactoring instanceof RenameRefactoring) { //return our custom instance for RenameRefactoring if (wantToParticipate(refactoring.getRefactoredObject()) return new J2EERenameRefactoringPlugin((RenameRefactoring) refactoring); } if (refactoring instanceof ... { ... } return null; } }It is necessary to register
J2EERefactoringFactory
in the lookup:
META-INF/services/org.netbeans.modules.refactoring.spi.RefactoringPluginFactory
and implement RefactoringPlugin interface:
//implementation of RefactoringPlugin public class J2EERenameRefactoringPlugin implements RefactoringPlugin { private RenameRefactoring refactoring; public J2EERenameRefactoringPlugin(RenameRefactoring refactoring) { this.refactoring = refactoring; } public Problem preCheck() { ... } public Problem checkParameters() { ... } public Problem fastCheckParameters() { ... } public void cancelRequest() { ... } public Problem prepare(RefactoringElementsBag refactoringElements) { RenameRefactoring renameRefactor = ((RenameRefactoring)refactoring); Object element = renameRefactor.getRefactoredObject(); if (...) { ... //lets add our RefactoringElements for usages found in XML files refactoringElements.add(refactoring, new XMLRenameRefactoringElement()); } return null; } public class XMLRenameRefactoringElement implements RefactoringElementImplementation { public void performChange() { //do change } }Refactoring SPI Example 2: Intention: Create a module, which will add Rename... to html files First you must create your ActionsImplementationProvider:
public class MyProvider extends ActionsImplementationProvider { public boolean canRename(Lookup lookup) { Node[] nodes = lookup.lookupAll(Node.class); if (..one node selected and the node belongs to html...) return true; else return fals; } public void doRename(Lookup selectedNodes) { Node[] nodes = lookup.lookupAll(Node.class); final FileObject fo = getFileFromNode(nodes[0]); return new Runnable() { public void run() { UI.openRefactoringUI(new RenameRefactoringUI(fo); } } } }And of course your own RefactoringPlugin and RefactoringPluginFactory see Refactoring SPI Example 1 and Refactoring SPI Example 2
public TreeElement getTreeElement(Object o) { . . if (o instanceof SourceGroup) { return new SourceGroupTreeElement((SourceGroup)o); } else if (o instanceof SomethingFromJava) { return new SomethingFromJavaTreeElement((SomethingFromJava) o); }TreeElement is then displayed in refactoring preview panel.
|
|
|
|
The sources for the module are in the Apache Git repositories or in the GitHub repositories.
Read more about the implementation in the answers to architecture questions.