|
org.netbeans.modules.refactoring.api 1.25.0 1 | |||||||||
| PREV NEXT | FRAMES NO FRAMES | |||||||||
See:
Description
| Refactoring API | |
|---|---|
| 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:
Added the method remove to the refactoring Context.
Added support to copy multiple files at once.
Scope is used to limit the WhereUsedQuery to a specific scope.
An instance is added to the context of WhereUsedQuery to limit the
scope. A custom scope can be any combination of source roots, folders
and files.
Added RefactoringCommit and ModificationResult SPI classes.
ExplorerContext is refactoring specific context, which is passed via Lookup
to ContextAwareActions in following cases:
1. Instant rename in Explorer
2. Nodes are transfered using DnD.
3. Nodes are moved using Cut/Paste.
4. Nodes are copied using Copy/Paste.
5. Nodes are deleted from Explorer.
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 NetBeans Mercurial repositories.
Read more about the implementation in the answers to architecture questions.
|
org.netbeans.modules.refactoring.api 1.25.0 1 | |||||||||
| PREV NEXT | FRAMES NO FRAMES | |||||||||