예제 #1
0
  /** @see edu.buffalo.cse.green.editor.model.commands.DeleteCommand#doDelete() */
  public void doDelete() {
    RootModel root = _typeModel.getRootModel();

    // Remove relationships first
    List<RelationshipModel> rels = root.getRelationships();

    // No iterators here due to CME's (ConcurrentModificationException)
    // Removal of relationships causes modifications to the rels list.
    for (int i = 0; i < rels.size(); i++) {
      IType t = _typeModel.getType();
      RelationshipModel r = rels.get(i);
      if (r.getSourceType() == t || r.getTargetType() == t) {
        DeleteCommand drc = r.getDeleteCommand(DiagramEditor.findProjectEditor(root.getProject()));
        drc.suppressMessage(true);
        drc.execute();
      }
    }

    _typeModel.removeChildren(); // remove fields/methods
    _typeModel.removeFromParent();
    try {
      IType type = _typeModel.getType();
      ICompilationUnit cu = (ICompilationUnit) type.getAncestor(IJavaElement.COMPILATION_UNIT);

      if (type.equals(cu.findPrimaryType())) {
        cu.delete(true, PlugIn.getEmptyProgressMonitor());
      } else {
        type.delete(true, PlugIn.getEmptyProgressMonitor());
      }
    } catch (JavaModelException e) {
      e.printStackTrace();
    }
    root.updateRelationships();
  }
  @Override
  protected boolean initialize(Object element) {
    if (element instanceof IType) {
      IType type = (IType) element;
      IJavaProject javaProject = (IJavaProject) type.getAncestor(IJavaElement.JAVA_PROJECT);
      mProject = javaProject.getProject();
      IResource manifestResource =
          mProject.findMember(AdtConstants.WS_SEP + SdkConstants.FN_ANDROID_MANIFEST_XML);

      if (manifestResource == null
          || !manifestResource.exists()
          || !(manifestResource instanceof IFile)) {
        RefactoringUtil.logInfo(
            "Invalid or missing the "
                + SdkConstants.FN_ANDROID_MANIFEST_XML
                + " in the "
                + mProject.getName()
                + " project.");
        return false;
      }
      mManifestFile = (IFile) manifestResource;
      ManifestData manifestData;
      manifestData = AndroidManifestHelper.parseForData(mManifestFile);
      if (manifestData == null) {
        return false;
      }
      mAppPackage = manifestData.getPackage();
      mOldFqcn = type.getFullyQualifiedName();
      Object destination = getArguments().getDestination();
      if (destination instanceof IPackageFragment) {
        IPackageFragment packageFragment = (IPackageFragment) destination;
        mNewFqcn = packageFragment.getElementName() + "." + type.getElementName();
      } else if (destination instanceof IResource) {
        try {
          IPackageFragment[] fragments = javaProject.getPackageFragments();
          for (IPackageFragment fragment : fragments) {
            IResource resource = fragment.getResource();
            if (resource.equals(destination)) {
              mNewFqcn = fragment.getElementName() + '.' + type.getElementName();
              break;
            }
          }
        } catch (JavaModelException e) {
          // pass
        }
      }
      return mOldFqcn != null && mNewFqcn != null;
    }

    return false;
  }
  /** @see IJavaElementRequestor */
  public void acceptType(IType type) {
    try {
      if (this.unitToSkip != null && this.unitToSkip.equals(type.getCompilationUnit())) {
        return;
      }
      char[] packageName = type.getPackageFragment().getElementName().toCharArray();
      boolean isBinary = type instanceof BinaryType;

      // determine associated access restriction
      AccessRestriction accessRestriction = null;

      if (this.checkAccessRestrictions
          && (isBinary || !type.getJavaProject().equals(this.project))) {
        PackageFragmentRoot root =
            (PackageFragmentRoot) type.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
        ClasspathEntry entry = (ClasspathEntry) this.nameLookup.rootToResolvedEntries.get(root);
        if (entry != null) { // reverse map always contains resolved CP entry
          AccessRuleSet accessRuleSet = entry.getAccessRuleSet();
          if (accessRuleSet != null) {
            // TODO (philippe) improve char[] <-> String conversions to avoid performing them on the
            // fly
            char[][] packageChars = CharOperation.splitOn('.', packageName);
            char[] fileWithoutExtension = type.getElementName().toCharArray();
            accessRestriction =
                accessRuleSet.getViolatedRestriction(
                    CharOperation.concatWith(packageChars, fileWithoutExtension, '/'));
          }
        }
      }
      this.requestor.acceptType(
          packageName,
          type.getElementName().toCharArray(),
          null,
          type.getFlags(),
          accessRestriction);
    } catch (JavaModelException jme) {
      // ignore
    }
  }
  private void collectProposals(
      IJavaProject project, String name, Collection<DefaultClasspathFixProposal> proposals)
      throws CoreException {
    int idx = name.lastIndexOf('.');
    char[] packageName =
        idx != -1 ? name.substring(0, idx).toCharArray() : null; // no package provided
    char[] typeName = name.substring(idx + 1).toCharArray();

    if (typeName.length == 1 && typeName[0] == '*') {
      typeName = null;
    }

    IJavaSearchScope scope = SearchEngine.createWorkspaceScope();
    ArrayList<TypeNameMatch> res = new ArrayList<TypeNameMatch>();
    TypeNameMatchCollector requestor = new TypeNameMatchCollector(res);
    int matchMode = SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE;
    new SearchEngine()
        .searchAllTypeNames(
            packageName,
            matchMode,
            typeName,
            matchMode,
            IJavaSearchConstants.TYPE,
            scope,
            requestor,
            IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH,
            null);

    if (res.isEmpty()) {
      return;
    }
    HashSet<Object> addedClaspaths = new HashSet<Object>();
    for (int i = 0; i < res.size(); i++) {
      TypeNameMatch curr = res.get(i);
      IType type = curr.getType();
      if (type != null) {
        IPackageFragmentRoot root =
            (IPackageFragmentRoot) type.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
        try {
          IClasspathEntry entry = root.getRawClasspathEntry();
          if (entry == null) {
            continue;
          }
          IJavaProject other = root.getJavaProject();
          int entryKind = entry.getEntryKind();
          if ((entry.isExported() || entryKind == IClasspathEntry.CPE_SOURCE)
              && addedClaspaths.add(other)) {
            IClasspathEntry newEntry = JavaCore.newProjectEntry(other.getPath());
            Change change = ClasspathFixProposal.newAddClasspathChange(project, newEntry);
            if (change != null) {
              String[] args = {
                BasicElementLabels.getResourceName(other.getElementName()),
                BasicElementLabels.getResourceName(project.getElementName())
              };
              String label =
                  Messages.format(
                      CorrectionMessages.ReorgCorrectionsSubProcessor_addcp_project_description,
                      args);
              String desc = label;
              DefaultClasspathFixProposal proposal =
                  new DefaultClasspathFixProposal(
                      label, change, desc, IProposalRelevance.ADD_PROJECT_TO_BUILDPATH);
              proposals.add(proposal);
            }
          }
          if (entryKind == IClasspathEntry.CPE_CONTAINER) {
            IPath entryPath = entry.getPath();
            if (isNonProjectSpecificContainer(entryPath)) {
              addLibraryProposal(project, root, entry, addedClaspaths, proposals);
            } else {
              try {
                IClasspathContainer classpathContainer =
                    JavaCore.getClasspathContainer(entryPath, root.getJavaProject());
                if (classpathContainer != null) {
                  IClasspathEntry entryInContainer =
                      JavaModelUtil.findEntryInContainer(classpathContainer, root.getPath());
                  if (entryInContainer != null) {
                    addLibraryProposal(project, root, entryInContainer, addedClaspaths, proposals);
                  }
                }
              } catch (CoreException e) {
                // ignore
              }
            }
          } else if ((entryKind == IClasspathEntry.CPE_LIBRARY
              || entryKind == IClasspathEntry.CPE_VARIABLE)) {
            addLibraryProposal(project, root, entry, addedClaspaths, proposals);
          }
        } catch (JavaModelException e) {
          // ignore
        }
      }
    }
  }