Esempio n. 1
0
  private ASTNode createTargetNode(
      ASTNode first, ASTNode last, boolean isMove, ASTNode replacingNode, TextEditGroup editGroup) {
    if (first == null || last == null) {
      throw new IllegalArgumentException();
    }

    NodeInfoStore nodeStore = this.rewriter.getNodeStore();
    ASTNode placeholder =
        nodeStore.newPlaceholderNode(first.getNodeType()); // revisit: could use list type
    if (placeholder == null) {
      throw new IllegalArgumentException(
          "Creating a target node is not supported for nodes of type"
              + first.getClass().getName()); // $NON-NLS-1$
    }

    Block internalPlaceHolder = nodeStore.createCollapsePlaceholder();
    CopySourceInfo info =
        getRewriteStore()
            .createRangeCopy(
                this.parent,
                this.childProperty,
                first,
                last,
                isMove,
                internalPlaceHolder,
                replacingNode,
                editGroup);
    nodeStore.markAsCopyTarget(placeholder, info);

    return placeholder;
  }
Esempio n. 2
0
 public static void setIsGeneratedFlag(
     org.eclipse.jdt.core.dom.ASTNode domNode,
     org.eclipse.jdt.internal.compiler.ast.ASTNode internalNode)
     throws Exception {
   if (internalNode == null || domNode == null) return;
   boolean isGenerated =
       internalNode.getClass().getField("$generatedBy").get(internalNode) != null;
   if (isGenerated) {
     domNode.getClass().getField("$isGenerated").set(domNode, true);
     domNode.setFlags(domNode.getFlags() & ~org.eclipse.jdt.core.dom.ASTNode.ORIGINAL);
   }
 }
Esempio n. 3
0
 public static String qualifiedName(TypeDeclaration decl) {
   String name = decl.getName().getIdentifier();
   ASTNode parent = decl.getParent();
   // resolve full name e.g.: A.B
   while (parent != null && parent.getClass() == TypeDeclaration.class) {
     name = ((TypeDeclaration) parent).getName().getIdentifier() + "." + name;
     parent = parent.getParent();
   }
   // resolve fully qualified name e.g.: some.package.A.B
   if (decl.getRoot().getClass() == CompilationUnit.class) {
     CompilationUnit root = (CompilationUnit) decl.getRoot();
     if (root.getPackage() != null) {
       PackageDeclaration pack = root.getPackage();
       name = pack.getName().getFullyQualifiedName() + "." + name;
     }
   }
   return name;
 }
Esempio n. 4
0
  public Collection<ConstructorDeclaration> getConstructors() {
    final List<ConstructorDeclaration> results = new ArrayList<ConstructorDeclaration>();
    if (isFromSource()) {
      // need to consult the ast since methods with broken signature
      // do not appear in bindings.
      final ITypeBinding typeBinding = getDeclarationBinding();
      final ASTNode node = _env.getASTNodeForBinding(typeBinding);
      if (node != null) {
        switch (node.getNodeType()) {
          case ASTNode.TYPE_DECLARATION:
          case ASTNode.ANNOTATION_TYPE_DECLARATION:
          case ASTNode.ENUM_DECLARATION:
            AbstractTypeDeclaration typeDecl = (AbstractTypeDeclaration) node;
            // built the ast based methods first.
            getASTConstructor(typeDecl, results);
            break;
          default:
            // the ast node for a type binding should be a AbstractTypeDeclaration.
            throw new IllegalStateException(
                "expecting a AbstractTypeDeclaration but got " //$NON-NLS-1$
                    + node.getClass().getName());
        }
      }
    }
    // build methods for binding type or
    // build the binding based method for source type.

    final IMethodBinding[] methods = getDeclarationBinding().getDeclaredMethods();
    for (IMethodBinding method : methods) {
      if (method.isSynthetic()) continue;
      if (method.isConstructor()) {
        Declaration mirrorDecl = Factory.createDeclaration(method, _env);
        if (mirrorDecl != null) results.add((ConstructorDeclaration) mirrorDecl);
      }
    }
    return results;
  }
Esempio n. 5
0
 public static boolean skipRewritingGeneratedNodes(org.eclipse.jdt.core.dom.ASTNode node)
     throws Exception {
   return ((Boolean) node.getClass().getField("$isGenerated").get(node)).booleanValue();
 }
Esempio n. 6
0
  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;
  }