private void printAST(ASTNode<?> ast, int level) { if (ast != null) { printTab(level); System.out.println(level + ":" + ast.getClass().getName() + "@" + ast.getId()); int n = ast.getNumChild(); for (int i = 0; i < n; i++) printAST(ast.getChild(i), level + 1); } }
private void declare(Variable var, ASTNode expr) { String scopeType = "scope"; String variableType = "variable"; if (expr.getClass() == FieldNode.class) { scopeType = "class"; variableType = "field"; } else if (expr.getClass() == PropertyNode.class) { scopeType = "class"; variableType = "property"; } StringBuilder msg = new StringBuilder(); msg.append("The current ").append(scopeType); msg.append(" already contains a ").append(variableType); msg.append(" of the name ").append(var.getName()); if (currentScope.getDeclaredVariable(var.getName()) != null) { addError(msg.toString(), expr); return; } for (VariableScope scope = currentScope.getParent(); scope != null; scope = scope.getParent()) { // if we are in a class and no variable is declared until // now, then we can break the loop, because we are allowed // to declare a variable of the same name as a class member if (scope.getClassScope() != null) break; if (scope.getDeclaredVariable(var.getName()) != null) { // variable already declared addError(msg.toString(), expr); break; } } // declare the variable even if there was an error to allow more checks currentScope.putDeclaredVariable(var); }
private String getRefDescriptor(ASTNode ref) { if (ref instanceof FieldNode) { FieldNode f = (FieldNode) ref; return "the field " + f.getName() + " "; } else if (ref instanceof PropertyNode) { PropertyNode p = (PropertyNode) ref; return "the property " + p.getName() + " "; } else if (ref instanceof ConstructorNode) { return "the constructor " + ref.getText() + " "; } else if (ref instanceof MethodNode) { return "the method " + ref.getText() + " "; } else if (ref instanceof ClassNode) { return "the super class " + ref + " "; } return "<unknown with class " + ref.getClass() + "> "; }
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; }