コード例 #1
0
  private void buildForProject(
      JavaProject project,
      ArrayList potentialSubtypes,
      org.eclipse.jdt.core.ICompilationUnit[] workingCopies,
      HashSet localTypes,
      IProgressMonitor monitor)
      throws JavaModelException {
    // resolve
    int openablesLength = potentialSubtypes.size();
    if (openablesLength > 0) {
      // copy vectors into arrays
      Openable[] openables = new Openable[openablesLength];
      potentialSubtypes.toArray(openables);

      // sort in the order of roots and in reverse alphabetical order for .class file
      // since requesting top level types in the process of caching an enclosing type is
      // not supported by the lookup environment
      IPackageFragmentRoot[] roots = project.getPackageFragmentRoots();
      int rootsLength = roots.length;
      final HashtableOfObjectToInt indexes = new HashtableOfObjectToInt(openablesLength);
      for (int i = 0; i < openablesLength; i++) {
        IJavaElement root = openables[i].getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
        int index;
        for (index = 0; index < rootsLength; index++) {
          if (roots[index].equals(root)) break;
        }
        indexes.put(openables[i], index);
      }
      Arrays.sort(
          openables,
          new Comparator() {
            public int compare(Object a, Object b) {
              int aIndex = indexes.get(a);
              int bIndex = indexes.get(b);
              if (aIndex != bIndex) return aIndex - bIndex;
              return ((Openable) b).getElementName().compareTo(((Openable) a).getElementName());
            }
          });

      IType focusType = getType();
      boolean inProjectOfFocusType =
          focusType != null && focusType.getJavaProject().equals(project);
      org.eclipse.jdt.core.ICompilationUnit[] unitsToLookInside = null;
      if (inProjectOfFocusType) {
        org.eclipse.jdt.core.ICompilationUnit unitToLookInside = focusType.getCompilationUnit();
        if (unitToLookInside != null) {
          int wcLength = workingCopies == null ? 0 : workingCopies.length;
          if (wcLength == 0) {
            unitsToLookInside = new org.eclipse.jdt.core.ICompilationUnit[] {unitToLookInside};
          } else {
            unitsToLookInside = new org.eclipse.jdt.core.ICompilationUnit[wcLength + 1];
            unitsToLookInside[0] = unitToLookInside;
            System.arraycopy(workingCopies, 0, unitsToLookInside, 1, wcLength);
          }
        } else {
          unitsToLookInside = workingCopies;
        }
      }

      SearchableEnvironment searchableEnvironment =
          project.newSearchableNameEnvironment(unitsToLookInside);
      this.nameLookup = searchableEnvironment.nameLookup;
      Map options = project.getOptions(true);
      // disable task tags to speed up parsing
      options.put(JavaCore.COMPILER_TASK_TAGS, ""); // $NON-NLS-1$
      this.hierarchyResolver =
          new HierarchyResolver(searchableEnvironment, options, this, new DefaultProblemFactory());
      if (focusType != null) {
        Member declaringMember = ((Member) focusType).getOuterMostLocalContext();
        if (declaringMember == null) {
          // top level or member type
          if (!inProjectOfFocusType) {
            char[] typeQualifiedName = focusType.getTypeQualifiedName('.').toCharArray();
            String[] packageName = ((PackageFragment) focusType.getPackageFragment()).names;
            if (searchableEnvironment.findType(typeQualifiedName, Util.toCharArrays(packageName))
                == null) {
              // focus type is not visible in this project: no need to go further
              return;
            }
          }
        } else {
          // local or anonymous type
          Openable openable;
          if (declaringMember.isBinary()) {
            openable = (Openable) declaringMember.getClassFile();
          } else {
            openable = (Openable) declaringMember.getCompilationUnit();
          }
          localTypes = new HashSet();
          localTypes.add(openable.getPath().toString());
          this.hierarchyResolver.resolve(new Openable[] {openable}, localTypes, monitor);
          return;
        }
      }
      this.hierarchyResolver.resolve(openables, localTypes, monitor);
    }
  }
コード例 #2
0
ファイル: MethodBinding.java プロジェクト: lyestoo/eclipse
 private JavaElement getUnresolvedJavaElement() {
   IType declaringType = (IType) getDeclaringClass().getJavaElement();
   if (declaringType == null) return null;
   if (!(this.resolver instanceof DefaultBindingResolver)) return null;
   ASTNode node = (ASTNode) ((DefaultBindingResolver) this.resolver).bindingsToAstNodes.get(this);
   if (node != null && declaringType.getParent().getElementType() != IJavaElement.CLASS_FILE) {
     if (node instanceof MethodDeclaration) {
       MethodDeclaration methodDeclaration = (MethodDeclaration) node;
       ArrayList parameterSignatures = new ArrayList();
       Iterator iterator = methodDeclaration.parameters().iterator();
       while (iterator.hasNext()) {
         SingleVariableDeclaration parameter = (SingleVariableDeclaration) iterator.next();
         Type type = parameter.getType();
         String typeSig = Util.getSignature(type);
         int arrayDim = parameter.getExtraDimensions();
         if (parameter.getAST().apiLevel() >= AST.JLS3 && parameter.isVarargs()) {
           arrayDim++;
         }
         if (arrayDim > 0) {
           typeSig = Signature.createArraySignature(typeSig, arrayDim);
         }
         parameterSignatures.add(typeSig);
       }
       int parameterCount = parameterSignatures.size();
       String[] parameters = new String[parameterCount];
       parameterSignatures.toArray(parameters);
       return (JavaElement) declaringType.getMethod(getName(), parameters);
     } else {
       // annotation type member declaration
       AnnotationTypeMemberDeclaration typeMemberDeclaration =
           (AnnotationTypeMemberDeclaration) node;
       return (JavaElement)
           declaringType.getMethod(
               typeMemberDeclaration.getName().getIdentifier(),
               new String[0]); // annotation type members don't have parameters
     }
   } else {
     // case of method not in the created AST, or a binary method
     org.eclipse.jdt.internal.compiler.lookup.MethodBinding original = this.binding.original();
     String selector =
         original.isConstructor() ? declaringType.getElementName() : new String(original.selector);
     TypeBinding[] parameters = original.parameters;
     int length = parameters == null ? 0 : parameters.length;
     String[] parameterSignatures = new String[length];
     for (int i = 0; i < length; i++) {
       parameterSignatures[i] = new String(parameters[i].genericTypeSignature()).replace('/', '.');
     }
     IMethod result = declaringType.getMethod(selector, parameterSignatures);
     if (declaringType.isBinary()) return (JavaElement) result;
     IMethod[] methods = null;
     try {
       methods = declaringType.getMethods();
     } catch (JavaModelException e) {
       // declaring type doesn't exist
       return null;
     }
     IMethod[] candidates = Member.findMethods(result, methods);
     if (candidates == null || candidates.length == 0) return null;
     return (JavaElement) candidates[0];
   }
 }