public void testRegion() throws Exception {
    IJavaProject jproj = createJavaProject("RegionTest");
    IProject proj = jproj.getProject();
    IPath projPath = proj.getFullPath();
    IPath root = projPath.append("src");

    for (int idx = 0; idx < 1000; idx++) {
      this.env.addClass(
          root, "test", "Foo" + idx, "package test;\n\n" + "public class Foo" + idx + " {\n" + "}");
    }

    this.env.fullBuild();

    for (int idx = 0; idx < 10; idx++) {
      startMeasuring();
      Region region = new Region();
      IPackageFragment[] fragments = jproj.getPackageFragments();

      for (IPackageFragment next : fragments) {
        IJavaElement[] children = next.getChildren();

        for (IJavaElement nextChild : children) {
          region.add(nextChild);
        }
      }
      stopMeasuring();
    }

    // Commit
    commitMeasurements();
    assertPerformance();
  }
 private static void tryDeletingAllJavaChildren(IPackageFragment pack) throws CoreException {
   IJavaElement[] kids = pack.getChildren();
   for (int i = 0; i < kids.length; i++) {
     if (kids[i] instanceof ISourceManipulation) {
       if (kids[i].exists() && !kids[i].isReadOnly()) JavaProjectHelper.delete(kids[i]);
     }
   }
 }
  /**
   * This method passes through all the java packages and calls the filter callback passed on each
   * package found.
   *
   * <p>If true is returned on the callback, the children of each package (classes) will also be
   * visited, otherwise, they'll be skipped.
   */
  private void filterJavaPackages(IFilter filter) {
    IClasspathEntry[] rawClasspath;
    try {
      rawClasspath = this.javaProject.getRawClasspath();
      FastStringBuffer buffer = new FastStringBuffer();
      for (IClasspathEntry entry : rawClasspath) {
        int entryKind = entry.getEntryKind();
        IClasspathEntry resolvedClasspathEntry = JavaCore.getResolvedClasspathEntry(entry);
        if (entryKind != IClasspathEntry.CPE_CONTAINER) {
          // ignore if it's in the system classpath...
          IPackageFragmentRoot[] roots =
              javaProject.findPackageFragmentRoots(resolvedClasspathEntry);

          // get the package roots
          for (IPackageFragmentRoot root : roots) {
            IJavaElement[] children = root.getChildren();

            // get the actual packages
            for (IJavaElement child : children) {
              IPackageFragment childPackage = (IPackageFragment) child;
              String elementName = childPackage.getElementName();

              // and if the java package is 'accepted'
              if (filter.accept(elementName, root, childPackage)) {
                buffer.clear();
                buffer.append(elementName);
                int packageNameLen = buffer.length();
                if (packageNameLen > 0) {
                  buffer.append('.');
                  packageNameLen++;
                }

                // traverse its classes
                for (IJavaElement class_ : childPackage.getChildren()) {
                  buffer.append(FullRepIterable.getFirstPart(class_.getElementName()));
                  filter.accept(buffer.toString(), root, class_);
                  buffer.setCount(
                      packageNameLen); // leave only the package part for the next append
                }
              }
            }
          }
        }
      }
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
Ejemplo n.º 4
0
  public IJavaElement getElementAtConsideringSibling(int position) throws JavaModelException {
    IPackageFragment fragment = (IPackageFragment) getParent();
    PackageFragmentRoot root =
        (PackageFragmentRoot) fragment.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
    SourceMapper mapper = root.getSourceMapper();
    if (mapper == null) {
      return null;
    } else {
      int index = this.name.indexOf('$');
      int prefixLength = index < 0 ? this.name.length() : index;

      IType type = null;
      int start = -1;
      int end = Integer.MAX_VALUE;
      IJavaElement[] children = fragment.getChildren();
      for (int i = 0; i < children.length; i++) {
        String childName = children[i].getElementName();

        int childIndex = childName.indexOf('$');
        int childPrefixLength = childIndex < 0 ? childName.indexOf('.') : childIndex;
        if (prefixLength == childPrefixLength
            && this.name.regionMatches(0, childName, 0, prefixLength)) {
          IClassFile classFile = (IClassFile) children[i];

          // ensure this class file's buffer is open so that source ranges are computed
          classFile.getBuffer();

          SourceRange range = mapper.getSourceRange(classFile.getType());
          if (range == SourceMapper.UNKNOWN_RANGE) continue;
          int newStart = range.getOffset();
          int newEnd = newStart + range.getLength() - 1;
          if (newStart > start && newEnd < end && newStart <= position && newEnd >= position) {
            type = classFile.getType();
            start = newStart;
            end = newEnd;
          }
        }
      }
      if (type != null) {
        return findElement(type, position, mapper);
      }
      return null;
    }
  }
  /**
   * 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;
    }
  }
Ejemplo n.º 6
0
  private void checkForJavaDependencies(IPluginModelBase[] models, IProgressMonitor monitor) {
    try {
      if (!fProject.hasNature(JavaCore.NATURE_ID)) return;

      IJavaProject jProject = JavaCore.create(fProject);
      IPackageFragment[] packageFragments =
          PluginJavaSearchUtil.collectPackageFragments(models, jProject, true);
      monitor.beginTask("", packageFragments.length); // $NON-NLS-1$
      SearchEngine engine = new SearchEngine();
      for (int i = 0; i < packageFragments.length; i++) {
        if (monitor.isCanceled()) break;
        IPackageFragment pkgFragment = packageFragments[i];
        monitor.subTask(
            PDEUIMessages.DependencyExtentOperation_inspecting
                + " "
                + pkgFragment.getElementName()); // $NON-NLS-1$
        if (pkgFragment.hasChildren()) {
          IJavaElement[] children = pkgFragment.getChildren();
          for (int j = 0; j < children.length; j++) {
            if (monitor.isCanceled()) break;
            IJavaElement child = children[j];
            IType[] types = new IType[0];
            if (child instanceof IClassFile) {
              types = new IType[] {((IClassFile) child).getType()};
            } else if (child instanceof ICompilationUnit) {
              types = ((ICompilationUnit) child).getTypes();
            }
            if (types.length > 0)
              searchForTypesUsed(
                  engine, child, types, PluginJavaSearchUtil.createSeachScope(jProject));
          }
        }
        monitor.worked(1);
      }
    } catch (CoreException e) {
    } finally {
      monitor.done();
    }
  }
Ejemplo n.º 7
0
 protected IPath componentPathForPackage(IPackageFragment _selection) {
   try {
     LocatePlugin locate = LocatePlugin.getDefault();
     for (IJavaElement element : _selection.getChildren()) {
       String componentName = locate.fileNameWithoutExtension(element.getElementName());
       LocalizedComponentsLocateResult result =
           locate.getLocalizedComponentsLocateResult(
               _selection.getJavaProject().getProject(), componentName);
       IFolder[] components = result.getComponents();
       if (components.length > 0) {
         IContainer selectionPath = components[0].getParent();
         return selectionPath.getFullPath();
       }
     }
   } catch (CoreException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   } catch (LocateException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
   return null;
 }
  /**
   * Recursively traverses all children of fragment. Looks for files that end in the cal extension
   * (.cal). If a file is found, it is added to a set and returned.
   *
   * @param fragment
   * @return the set of cal files below this package fragment
   */
  private Set<IStorage> internalGetSources(IPackageFragment fragment) {
    Set<IStorage> calSources = new HashSet<IStorage>();

    try {
      Object[] nonJavaChildren = fragment.getNonJavaResources();
      for (final Object child : nonJavaChildren) {
        if (child instanceof IStorage && ((IStorage) child).getName().endsWith(CAL_EXTENSION)) {
          calSources.add((IStorage) child);
        }
      }

      IJavaElement[] children = fragment.getChildren();
      for (final IJavaElement child : children) {
        if (child.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
          calSources.addAll(internalGetSources((IPackageFragment) child));
        }
      }
    } catch (JavaModelException e) {
      Util.log(e, "Error getting CAL Sources");
    }

    return calSources;
  }