private static String getRenameKind(IJavaElement e) { switch (e.getElementType()) { case IJavaElement.COMPILATION_UNIT: return IJavaRefactorings.RENAME_COMPILATION_UNIT; case IJavaElement.FIELD: return IJavaRefactorings.RENAME_FIELD; case IJavaElement.LOCAL_VARIABLE: return IJavaRefactorings.RENAME_LOCAL_VARIABLE; case IJavaElement.METHOD: return IJavaRefactorings.RENAME_METHOD; case IJavaElement.TYPE: return IJavaRefactorings.RENAME_TYPE; case IJavaElement.TYPE_PARAMETER: return IJavaRefactorings.RENAME_TYPE_PARAMETER; case IJavaElement.PACKAGE_FRAGMENT: return IJavaRefactorings.RENAME_PACKAGE; case IJavaElement.PACKAGE_FRAGMENT_ROOT: return IJavaRefactorings.RENAME_PACKAGE; default: throw new RuntimeException("Unexpected object `" + e.getClass() + "' to rename"); } }
/** * Called when an IJavaElement changes. Used to reflect changes in the editor, which has a view * that is based on the <code>JavaModel</code>. * * @param event - The change that occurred to the <code>JavaModel</code>. */ @SuppressWarnings("unchecked") public void elementChanged(ElementChangedEvent event) { try { /* Goes through these classes looking for any that are added, moved * or removed. Calls methods that updates the editor to reflect any * changes found. */ for (Class type : map.keySet()) { List<IJavaElementDelta> added = findAddedElements(event.getDelta(), type); List<IJavaElementDelta> removed = findRemovedElements(event.getDelta(), type); List<IJavaElementDelta> changed = findChangedElements(event.getDelta(), type); HashMap<IJavaElement, IJavaElement> moved = extractMovedElements(added, removed); // ignore updating the editors if no changes occurred if (added.size() == 0 && removed.size() == 0 && moved.size() == 0 && changed.size() == 0) { continue; } List<DiagramEditor> editors = new ArrayList<DiagramEditor>(DiagramEditor.getEditors()); // handle changes for (DiagramEditor editor : editors) { RootModel root = editor.getRootModel(); // handle moves for (IJavaElement sourceElement : moved.keySet()) { IJavaElement targetElement = moved.get(sourceElement); map.get(sourceElement.getClass()).handleMove(root, sourceElement, targetElement); } // handle removes for (IJavaElementDelta removedElement : removed) { map.get(removedElement.getElement().getClass()) .handleRemove(root, removedElement.getElement()); } // handle adds for (IJavaElementDelta addedElement : added) { map.get(addedElement.getElement().getClass()) .handleAdd(root, addedElement.getElement()); } // handle changes (to modifiers, etc.) for (IJavaElementDelta changedElement : changed) { handleElementChange(changedElement); } editor.forceRefreshRelationships(); } } } catch (Throwable t) { // TODO Incremental exploration throws Null Pointer. Virtually unreproduceable. GreenException.critical(t); } finally { TypeRefactorHandler.REMOVED_TYPE = null; } }
public JavaRefactoringDescriptor buildEclipseDescriptor() throws Exception { System.err.println("[eclipse-rename] Building descriptor..."); IJavaElement element = location.getIJavaElement(); String kind = getRenameKind(element); // [1] BUGFIX was needed: The scripting interface didn't allow VariableDeclarationFragments as // IJavaElements // and thus had to be extended // [2] WORKAROUND for scripting interface bug (see below) if (kind.equals(IJavaRefactorings.RENAME_METHOD)) { IMethod m = (IMethod) element; if (m.isConstructor()) { // Rename the type instead (as the UI would do-- the scripting interface will only rename // the constructor, which is broken) kind = IJavaRefactorings.RENAME_TYPE; element = m.getDeclaringType(); } } System.err.println("[eclipse-rename] Kind = " + kind + ", element = " + element); // [3] Don't test for package fragments now if (kind.equals(IJavaRefactorings.RENAME_PACKAGE)) return null; // don't bother with this now if (element == null) { System.err.println("!!! ABORT: No IJavaElement to represent location"); throw new RuntimeException("!!! ABORT: No IJavaElement for location"); } if (element instanceof ILocalVariable) { System.err.println("element is of type " + element.getClass()); final ILocalVariable fLocalVariable = (ILocalVariable) element; final ISourceRange sourceRange = fLocalVariable.getNameRange(); final CompilationUnit fCompilationUnitNode = location.getCompilationUnit(); ASTNode name = NodeFinder.perform(fCompilationUnitNode, sourceRange); System.err.println("node is of type " + name.getClass()); if (name == null) System.err.println("!!! ILV doesn't have associated name!"); if (name.getParent() instanceof VariableDeclaration) System.err.println("ILV has parent : " + (VariableDeclaration) name.getParent()); else System.err.println( "!!! ILV doesn't have var declaration parent, instead " + name.getParent().getClass()); } System.err.println("Trying to rename a " + kind + ": " + element); if (element instanceof SimpleName) System.err.println(" Name = '" + ((SimpleName) element).getIdentifier() + "'"); if (kind.equals(IJavaRefactorings.RENAME_TYPE)) { System.err.println("(Possibly need a new launch configuration)"); tproject.renameClass((IType) element, new_name); } final RenameJavaElementDescriptor descriptor = (RenameJavaElementDescriptor) getDescriptor(kind); descriptor.setJavaElement(element); descriptor.setNewName(this.new_name); if (element.getElementType() == IJavaElement.TYPE || element.getElementType() == IJavaElement.PACKAGE_FRAGMENT) descriptor.setUpdateQualifiedNames(true); else descriptor.setUpdateQualifiedNames(false); descriptor.setUpdateReferences(true); descriptor.setDeprecateDelegate(false); descriptor.setRenameGetters(false); descriptor.setRenameSetters(false); descriptor.setKeepOriginal(false); descriptor.setUpdateHierarchy(false); descriptor.setUpdateSimilarDeclarations(false); // [3] Fix: Eclipse will complain if the transformation is a no-op, but we don't want that: if (element.getElementName().equals(this.new_name)) throw new NOPException(); System.err.println("[eclipse-rename] Computed descriptor."); return descriptor; }