예제 #1
0
  /** @see IClassFile */
  public IJavaElement getElementAt(int position) throws JavaModelException {
    IJavaElement parentElement = getParent();
    while (parentElement.getElementType() != IJavaElement.PACKAGE_FRAGMENT_ROOT) {
      parentElement = parentElement.getParent();
    }
    PackageFragmentRoot root = (PackageFragmentRoot) parentElement;
    SourceMapper mapper = root.getSourceMapper();
    if (mapper == null) {
      return null;
    } else {
      // ensure this class file's buffer is open so that source ranges are computed
      getBuffer();

      IType type = getType();
      return findElement(type, position, mapper);
    }
  }
예제 #2
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;
    }
  }