/** @see MultiOperation */
  protected void verify(IJavaElement element) throws JavaModelException {
    if (element == null || !element.exists())
      error(IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST, element);

    if (element.isReadOnly() && (isRename() || isMove()))
      error(IJavaModelStatusConstants.READ_ONLY, element);

    IResource resource = ((JavaElement) element).resource();
    if (resource instanceof IFolder) {
      if (resource.isLinked()) {
        error(IJavaModelStatusConstants.INVALID_RESOURCE, element);
      }
    }

    int elementType = element.getElementType();

    if (elementType == IJavaElement.COMPILATION_UNIT) {
      org.eclipse.jdt.internal.core.CompilationUnit compilationUnit =
          (org.eclipse.jdt.internal.core.CompilationUnit) element;
      if (isMove() && compilationUnit.isWorkingCopy() && !compilationUnit.isPrimary())
        error(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, element);
    } else if (elementType != IJavaElement.PACKAGE_FRAGMENT) {
      error(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, element);
    }

    JavaElement dest = (JavaElement) getDestinationParent(element);
    verifyDestination(element, dest);
    if (this.renamings != null) {
      verifyRenaming(element);
    }
  }
  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");
    }
  }
 /**
  * Sets the deltas to register the changes resulting from this operation for this source element
  * and its destination. If the operation is a cross project operation
  *
  * <ul>
  *   <li>On a copy, the delta should be rooted in the dest project
  *   <li>On a move, two deltas are generated
  *       <ul>
  *         <li>one rooted in the source project
  *         <li>one rooted in the destination project
  *       </ul>
  * </ul>
  *
  * If the operation is rooted in a single project, the delta is rooted in that project
  */
 protected void prepareDeltas(
     IJavaElement sourceElement, IJavaElement destinationElement, boolean isMove) {
   if (Util.isExcluded(sourceElement) || Util.isExcluded(destinationElement)) return;
   IJavaProject destProject = destinationElement.getJavaProject();
   if (isMove) {
     IJavaProject sourceProject = sourceElement.getJavaProject();
     getDeltaFor(sourceProject).movedFrom(sourceElement, destinationElement);
     getDeltaFor(destProject).movedTo(destinationElement, sourceElement);
   } else {
     getDeltaFor(destProject).added(destinationElement);
   }
 }
 private IResource getResource(IJavaElement element) {
   if (element == null) return null;
   if (element.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
     String pkgName = element.getElementName();
     int firstDot = pkgName.indexOf('.');
     if (firstDot != -1) {
       element =
           ((IPackageFragmentRoot) element.getParent())
               .getPackageFragment(pkgName.substring(0, firstDot));
     }
   }
   return element.getResource();
 }
 /**
  * @see MultiOperation This method delegates to <code>processCompilationUnitResource</code> or
  *     <code>processPackageFragmentResource</code>, depending on the type of <code>element</code>.
  */
 protected void processElement(IJavaElement element) throws JavaModelException {
   IJavaElement dest = getDestinationParent(element);
   switch (element.getElementType()) {
     case IJavaElement.COMPILATION_UNIT:
       processCompilationUnitResource((ICompilationUnit) element, (PackageFragment) dest);
       this.createdElements.add(
           ((IPackageFragment) dest).getCompilationUnit(element.getElementName()));
       break;
     case IJavaElement.PACKAGE_FRAGMENT:
       processPackageFragmentResource(
           (PackageFragment) element, (PackageFragmentRoot) dest, getNewNameFor(element));
       break;
     default:
       throw new JavaModelException(
           new JavaModelStatus(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, element));
   }
 }
  /**
   * Returns the children of <code>source</code> which are affected by this operation. If <code>
   * source</code> is a <code>K_SOURCE</code>, these are the <code>.java</code> files, if it is a
   * <code>K_BINARY</code>, they are the <code>.class</code> files.
   */
  private IResource[] collectResourcesOfInterest(IPackageFragment source)
      throws JavaModelException {
    IJavaElement[] children = source.getChildren();
    int childOfInterest = IJavaElement.COMPILATION_UNIT;
    if (source.getKind() == IPackageFragmentRoot.K_BINARY) {
      childOfInterest = IJavaElement.CLASS_FILE;
    }
    ArrayList correctKindChildren = new ArrayList(children.length);
    for (int i = 0; i < children.length; i++) {
      IJavaElement child = children[i];
      if (child.getElementType() == childOfInterest) {
        correctKindChildren.add(((JavaElement) child).resource());
      }
    }
    // Gather non-java resources
    Object[] nonJavaResources = source.getNonJavaResources();
    int actualNonJavaResourceCount = 0;
    for (int i = 0, max = nonJavaResources.length; i < max; i++) {
      if (nonJavaResources[i] instanceof IResource) actualNonJavaResourceCount++;
    }
    IResource[] actualNonJavaResources = new IResource[actualNonJavaResourceCount];
    for (int i = 0, max = nonJavaResources.length, index = 0; i < max; i++) {
      if (nonJavaResources[i] instanceof IResource)
        actualNonJavaResources[index++] = (IResource) nonJavaResources[i];
    }

    if (actualNonJavaResourceCount != 0) {
      int correctKindChildrenSize = correctKindChildren.size();
      IResource[] result = new IResource[correctKindChildrenSize + actualNonJavaResourceCount];
      correctKindChildren.toArray(result);
      System.arraycopy(
          actualNonJavaResources, 0, result, correctKindChildrenSize, actualNonJavaResourceCount);
      return result;
    } else {
      IResource[] result = new IResource[correctKindChildren.size()];
      correctKindChildren.toArray(result);
      return result;
    }
  }
 private ISchedulingRule getSchedulingRule(IJavaElement element) {
   if (element == null) return null;
   IResource sourceResource = getResource(element);
   IResource destContainer = getResource(getDestinationParent(element));
   if (!(destContainer instanceof IContainer)) {
     return null;
   }
   String newName;
   try {
     newName = getNewNameFor(element);
   } catch (JavaModelException e) {
     return null;
   }
   if (newName == null) newName = element.getElementName();
   IResource destResource;
   String sourceEncoding = null;
   if (sourceResource.getType() == IResource.FILE) {
     destResource = ((IContainer) destContainer).getFile(new Path(newName));
     try {
       sourceEncoding = ((IFile) sourceResource).getCharset(false);
     } catch (CoreException ce) {
       // use default encoding
     }
   } else {
     destResource = ((IContainer) destContainer).getFolder(new Path(newName));
   }
   IResourceRuleFactory factory = ResourcesPlugin.getWorkspace().getRuleFactory();
   ISchedulingRule rule;
   if (isMove()) {
     rule = factory.moveRule(sourceResource, destResource);
   } else {
     rule = factory.copyRule(sourceResource, destResource);
   }
   if (sourceEncoding != null) {
     rule = new MultiRule(new ISchedulingRule[] {rule, factory.charsetRule(destResource)});
   }
   return rule;
 }
  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;
  }