예제 #1
0
 private RefactoringStatus checkAccessorDeclarations(IProgressMonitor pm, IMethod existingAccessor)
     throws CoreException {
   RefactoringStatus result = new RefactoringStatus();
   SearchPattern pattern =
       SearchPattern.createPattern(
           existingAccessor,
           IJavaSearchConstants.DECLARATIONS,
           SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE);
   IJavaSearchScope scope = SearchEngine.createHierarchyScope(fField.getDeclaringType());
   SearchResultGroup[] groupDeclarations =
       RefactoringSearchEngine.search(pattern, scope, pm, result);
   Assert.isTrue(groupDeclarations.length > 0);
   if (groupDeclarations.length != 1) {
     String message =
         Messages.format(
             RefactoringCoreMessages.RenameFieldRefactoring_overridden,
             JavaElementUtil.createMethodSignature(existingAccessor));
     result.addError(message);
   } else {
     SearchResultGroup group = groupDeclarations[0];
     Assert.isTrue(group.getSearchResults().length > 0);
     if (group.getSearchResults().length != 1) {
       String message =
           Messages.format(
               RefactoringCoreMessages.RenameFieldRefactoring_overridden_or_overrides,
               JavaElementUtil.createMethodSignature(existingAccessor));
       result.addError(message);
     }
   }
   return result;
 }
 @Override
 public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException {
   pm.beginTask("", 2); // $NON-NLS-1$
   RefactoringStatus result;
   try {
     result = new RefactoringStatus();
     IJavaElement element = (IJavaElement) getModifiedElement();
     // don't check for read-only since we don't go through
     // validate edit.
     result.merge(super.isValid(new SubProgressMonitor(pm, 1)));
     if (result.hasFatalError()) return result;
     if (element != null && element.exists() && element instanceof IPackageFragment) {
       IPackageFragment pack = (IPackageFragment) element;
       if (fRenameSubpackages) {
         IPackageFragment[] allPackages = JavaElementUtil.getPackageAndSubpackages(pack);
         SubProgressMonitor subPm = new SubProgressMonitor(pm, 1);
         subPm.beginTask("", allPackages.length); // $NON-NLS-1$
         for (int i = 0; i < allPackages.length; i++) {
           // don't check for read-only since we don't go through
           // validate edit.
           checkIfModifiable(result, allPackages[i].getResource(), VALIDATE_NOT_DIRTY);
           if (result.hasFatalError()) return result;
           isValid(result, allPackages[i], new SubProgressMonitor(subPm, 1));
         }
       } else {
         isValid(result, pack, new SubProgressMonitor(pm, 1));
       }
     }
   } finally {
     pm.done();
   }
   return result;
 }
 @Override
 protected Change createUndoChange(long stampToRestore) throws CoreException {
   IPackageFragment pack = getPackage();
   if (pack == null) return new NullChange();
   Map<IResource, Long> stamps = new HashMap<>();
   if (!fRenameSubpackages) {
     addStamps(stamps, pack.getCompilationUnits());
   } else {
     IPackageFragment[] allPackages = JavaElementUtil.getPackageAndSubpackages(pack);
     for (int i = 0; i < allPackages.length; i++) {
       IPackageFragment currentPackage = allPackages[i];
       addStamps(stamps, currentPackage.getCompilationUnits());
     }
   }
   return new RenamePackageChange(
       createNewPath(), getNewName(), getOldName(), stampToRestore, stamps, fRenameSubpackages);
   // Note: This reverse change only works if the renamePackage change did
   // not merge the source package into an existing target.
 }
예제 #4
0
  private RefactoringStatus checkNewAccessor(IMethod existingAccessor, String newAccessorName)
      throws CoreException {
    RefactoringStatus result = new RefactoringStatus();
    IMethod accessor =
        JavaModelUtil.findMethod(
            newAccessorName,
            existingAccessor.getParameterTypes(),
            false,
            fField.getDeclaringType());
    if (accessor == null || !accessor.exists()) return null;

    String message =
        Messages.format(
            RefactoringCoreMessages.RenameFieldRefactoring_already_exists,
            new String[] {
              JavaElementUtil.createMethodSignature(accessor),
              BasicElementLabels.getJavaElementName(
                  fField.getDeclaringType().getFullyQualifiedName('.'))
            });
    result.addError(message, JavaStatusContext.create(accessor));
    return result;
  }
  @Override
  protected void doRename(IProgressMonitor pm) throws CoreException {
    IPackageFragment pack = getPackage();
    if (pack == null) return;

    if (!fRenameSubpackages) {
      renamePackage(pack, pm, createNewPath(), getNewName());

    } else {
      IPackageFragment[] allPackages = JavaElementUtil.getPackageAndSubpackages(pack);
      Arrays.sort(
          allPackages,
          new Comparator<IPackageFragment>() {
            @Override
            public int compare(IPackageFragment o1, IPackageFragment o2) {
              String p1 = o1.getElementName();
              String p2 = o2.getElementName();
              return p1.compareTo(p2);
            }
          });
      int count = allPackages.length;
      pm.beginTask("", count); // $NON-NLS-1$
      // When renaming to subpackage (a -> a.b), do it inside-out:
      boolean insideOut = getNewName().startsWith(getOldName());
      try {
        for (int i = 0; i < count; i++) {
          IPackageFragment currentPackage = allPackages[insideOut ? count - i - 1 : i];
          renamePackage(
              currentPackage,
              new SubProgressMonitor(pm, 1),
              createNewPath(currentPackage),
              getNewName(currentPackage));
        }
      } finally {
        pm.done();
      }
    }
  }
 public static IMember[] merge(IMember[] a1, IMember[] a2) {
   return JavaElementUtil.merge(a1, a2);
 }
 private static IMethod[] getMethods(IMember[] members) {
   List<IJavaElement> l =
       Arrays.asList(JavaElementUtil.getElementsOfType(members, IJavaElement.METHOD));
   return l.toArray(new IMethod[l.size()]);
 }