@Override
 public Change createChange(IProgressMonitor monitor) throws CoreException {
   try {
     final TextChange[] changes = fChangeManager.getAllChanges();
     final List<TextChange> list = new ArrayList<>(changes.length);
     list.addAll(Arrays.asList(changes));
     String project = null;
     IJavaProject javaProject = fMethod.getJavaProject();
     if (javaProject != null) project = javaProject.getElementName();
     int flags =
         JavaRefactoringDescriptor.JAR_MIGRATION
             | JavaRefactoringDescriptor.JAR_REFACTORING
             | RefactoringDescriptor.STRUCTURAL_CHANGE;
     try {
       if (!Flags.isPrivate(fMethod.getFlags())) flags |= RefactoringDescriptor.MULTI_CHANGE;
     } catch (JavaModelException exception) {
       JavaPlugin.log(exception);
     }
     final IType declaring = fMethod.getDeclaringType();
     try {
       if (declaring.isAnonymous() || declaring.isLocal())
         flags |= JavaRefactoringDescriptor.JAR_SOURCE_ATTACHMENT;
     } catch (JavaModelException exception) {
       JavaPlugin.log(exception);
     }
     final String description =
         Messages.format(
             RefactoringCoreMessages.RenameMethodProcessor_descriptor_description_short,
             BasicElementLabels.getJavaElementName(fMethod.getElementName()));
     final String header =
         Messages.format(
             RefactoringCoreMessages.RenameMethodProcessor_descriptor_description,
             new String[] {
               JavaElementLabels.getTextLabel(fMethod, JavaElementLabels.ALL_FULLY_QUALIFIED),
               BasicElementLabels.getJavaElementName(getNewElementName())
             });
     final String comment = new JDTRefactoringDescriptorComment(project, this, header).asString();
     final RenameJavaElementDescriptor descriptor =
         RefactoringSignatureDescriptorFactory.createRenameJavaElementDescriptor(
             IJavaRefactorings.RENAME_METHOD);
     descriptor.setProject(project);
     descriptor.setDescription(description);
     descriptor.setComment(comment);
     descriptor.setFlags(flags);
     descriptor.setJavaElement(fMethod);
     descriptor.setNewName(getNewElementName());
     descriptor.setUpdateReferences(fUpdateReferences);
     descriptor.setKeepOriginal(fDelegateUpdating);
     descriptor.setDeprecateDelegate(fDelegateDeprecation);
     return new DynamicValidationRefactoringChange(
         descriptor,
         RefactoringCoreMessages.RenameMethodProcessor_change_name,
         list.toArray(new Change[list.size()]));
   } finally {
     monitor.done();
   }
 }
Exemplo n.º 2
0
 /**
  * Overridden by subclasses.
  *
  * @return return the refactoring descriptor for this refactoring
  */
 protected RenameJavaElementDescriptor createRefactoringDescriptor() {
   String project = null;
   IJavaProject javaProject = fField.getJavaProject();
   if (javaProject != null) project = javaProject.getElementName();
   int flags =
       JavaRefactoringDescriptor.JAR_MIGRATION
           | JavaRefactoringDescriptor.JAR_REFACTORING
           | RefactoringDescriptor.STRUCTURAL_CHANGE;
   try {
     if (!Flags.isPrivate(fField.getFlags())) flags |= RefactoringDescriptor.MULTI_CHANGE;
   } catch (JavaModelException exception) {
     JavaPlugin.log(exception);
   }
   final IType declaring = fField.getDeclaringType();
   try {
     if (declaring.isAnonymous() || declaring.isLocal())
       flags |= JavaRefactoringDescriptor.JAR_SOURCE_ATTACHMENT;
   } catch (JavaModelException exception) {
     JavaPlugin.log(exception);
   }
   final String description =
       Messages.format(
           RefactoringCoreMessages.RenameFieldRefactoring_descriptor_description_short,
           BasicElementLabels.getJavaElementName(fField.getElementName()));
   final String header =
       Messages.format(
           RefactoringCoreMessages.RenameFieldProcessor_descriptor_description,
           new String[] {
             BasicElementLabels.getJavaElementName(fField.getElementName()),
             JavaElementLabels.getElementLabel(
                 fField.getParent(), JavaElementLabels.ALL_FULLY_QUALIFIED),
             getNewElementName()
           });
   final JDTRefactoringDescriptorComment comment =
       new JDTRefactoringDescriptorComment(project, this, header);
   if (fRenameGetter)
     comment.addSetting(RefactoringCoreMessages.RenameFieldRefactoring_setting_rename_getter);
   if (fRenameSetter)
     comment.addSetting(RefactoringCoreMessages.RenameFieldRefactoring_setting_rename_settter);
   final RenameJavaElementDescriptor descriptor =
       RefactoringSignatureDescriptorFactory.createRenameJavaElementDescriptor(
           IJavaRefactorings.RENAME_FIELD);
   descriptor.setProject(project);
   descriptor.setDescription(description);
   descriptor.setComment(comment.asString());
   descriptor.setFlags(flags);
   descriptor.setJavaElement(fField);
   descriptor.setNewName(getNewElementName());
   descriptor.setUpdateReferences(fUpdateReferences);
   descriptor.setUpdateTextualOccurrences(fUpdateTextualMatches);
   descriptor.setRenameGetters(fRenameGetter);
   descriptor.setRenameSetters(fRenameSetter);
   descriptor.setKeepOriginal(fDelegateUpdating);
   descriptor.setDeprecateDelegate(fDelegateDeprecation);
   return descriptor;
 }
Exemplo n.º 3
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;
  }