Exemplo n.º 1
0
  /**
   * Add the initial set of compilation units into the loop -> build compilation unit declarations,
   * their bindings and record their results.
   */
  protected void internalBeginToCompile(ICompilationUnit[] sourceUnits, int maxUnits) {
    if (!this.useSingleThread && maxUnits >= ReadManager.THRESHOLD)
      this.parser.readManager = new ReadManager(sourceUnits, maxUnits);

    // Switch the current policy and compilation result for this unit to the requested one.
    for (int i = 0; i < maxUnits; i++) {
      CompilationResult unitResult = null;
      try {
        if (this.options.verbose) {
          this.out.println(
              Messages.bind(
                  Messages.compilation_request,
                  new String[] {
                    String.valueOf(i + 1),
                    String.valueOf(maxUnits),
                    new String(sourceUnits[i].getFileName())
                  }));
        }
        // diet parsing for large collection of units
        CompilationUnitDeclaration parsedUnit;
        unitResult =
            new CompilationResult(sourceUnits[i], i, maxUnits, this.options.maxProblemsPerUnit);
        long parseStart = System.currentTimeMillis();
        if (this.totalUnits < this.parseThreshold) {
          parsedUnit = this.parser.parse(sourceUnits[i], unitResult);
        } else {
          parsedUnit = this.parser.dietParse(sourceUnits[i], unitResult);
        }
        long resolveStart = System.currentTimeMillis();
        this.stats.parseTime += resolveStart - parseStart;
        // initial type binding creation
        this.lookupEnvironment.buildTypeBindings(parsedUnit, null /*no access restriction*/);
        this.stats.resolveTime += System.currentTimeMillis() - resolveStart;
        addCompilationUnit(sourceUnits[i], parsedUnit);
        ImportReference currentPackage = parsedUnit.currentPackage;
        if (currentPackage != null) {
          unitResult.recordPackageName(currentPackage.tokens);
        }
        // } catch (AbortCompilationUnit e) {
        //	requestor.acceptResult(unitResult.tagAsAccepted());
      } catch (AbortCompilation a) {
        // best effort to find a way for reporting this problem:
        if (a.compilationResult == null) a.compilationResult = unitResult;
        throw a;
      } finally {
        sourceUnits[i] = null; // no longer hold onto the unit
      }
    }
    if (this.parser.readManager != null) {
      this.parser.readManager.shutdown();
      this.parser.readManager = null;
    }
    // binding resolution
    this.lookupEnvironment.completeTypeBindings();
  }
Exemplo n.º 2
0
  /*
   * Compiler recovery in case of internal AbortCompilation event
   */
  protected void handleInternalException(
      AbortCompilation abortException, CompilationUnitDeclaration unit) {

    /* special treatment for SilentAbort: silently cancelling the compilation process */
    if (abortException.isSilent) {
      if (abortException.silentException == null) {
        return;
      }
      throw abortException.silentException;
    }

    /* uncomment following line to see where the abort came from */
    // abortException.printStackTrace();

    // Exception may tell which compilation result it is related, and which problem caused it
    CompilationResult result = abortException.compilationResult;
    if (result == null && unit != null) {
      result = unit.compilationResult; // current unit being processed ?
    }
    // Lookup environment may be in middle of connecting types
    if (result == null && this.lookupEnvironment.unitBeingCompleted != null) {
      result = this.lookupEnvironment.unitBeingCompleted.compilationResult;
    }
    if (result == null) {
      synchronized (this) {
        if (this.unitsToProcess != null && this.totalUnits > 0)
          result = this.unitsToProcess[this.totalUnits - 1].compilationResult;
      }
    }
    // last unit in beginToCompile ?
    if (result != null && !result.hasBeenAccepted) {
      /* distant problem which could not be reported back there? */
      if (abortException.problem != null) {
        recordDistantProblem:
        {
          CategorizedProblem distantProblem = abortException.problem;
          CategorizedProblem[] knownProblems = result.problems;
          for (int i = 0; i < result.problemCount; i++) {
            if (knownProblems[i] == distantProblem) { // already recorded
              break recordDistantProblem;
            }
          }
          if (distantProblem
              instanceof
              DefaultProblem) { // fixup filename TODO (philippe) should improve API to make this
                                // official
            ((DefaultProblem) distantProblem).setOriginatingFileName(result.getFileName());
          }
          result.record(distantProblem, unit);
        }
      } else {
        /* distant internal exception which could not be reported back there */
        if (abortException.exception != null) {
          this.handleInternalException(abortException.exception, null, result);
          return;
        }
      }
      /* hand back the compilation result */
      if (!result.hasBeenAccepted) {
        this.requestor.acceptResult(result.tagAsAccepted());
      }
    } else {
      abortException.printStackTrace();
    }
  }
  public IJavaElement[] getVisibleElements(String typeSignature) {
    if (this.assistScope == null) return new IJavaElement[0];

    if (!this.hasComputedVisibleElementBindings) {
      computeVisibleElementBindings();
    }

    TypeBinding assignableTypeBinding = null;
    if (typeSignature != null) {
      assignableTypeBinding = getTypeFromSignature(typeSignature, this.assistScope);
      if (assignableTypeBinding == null) return new IJavaElement[0];
    }

    int length =
        this.visibleLocalVariables.size() + this.visibleFields.size() + this.visibleMethods.size();
    if (length == 0) return new IJavaElement[0];

    IJavaElement[] result = new IJavaElement[length];

    int elementCount = 0;

    int size = this.visibleLocalVariables.size();
    if (size > 0) {
      next:
      for (int i = 0; i < size; i++) {
        try {
          LocalVariableBinding binding =
              (LocalVariableBinding) this.visibleLocalVariables.elementAt(i);
          if (binding.type == null
              || (assignableTypeBinding != null
                  && !binding.type.isCompatibleWith(assignableTypeBinding))) continue next;
          JavaElement localVariable = getJavaElement(binding);
          if (localVariable != null) result[elementCount++] = localVariable;
        } catch (AbortCompilation e) {
          // log the exception and proceed
          Util.logRepeatedMessage(e.getKey(), e);
        }
      }
    }
    size = this.visibleFields.size();
    if (size > 0) {
      next:
      for (int i = 0; i < size; i++) {
        try {
          FieldBinding binding = (FieldBinding) this.visibleFields.elementAt(i);
          if (assignableTypeBinding != null
              && !binding.type.isCompatibleWith(assignableTypeBinding)) continue next;
          if (this.assistScope.isDefinedInSameUnit(binding.declaringClass)) {
            JavaElement field = getJavaElementOfCompilationUnit(binding);
            if (field != null) result[elementCount++] = field;
          } else {
            JavaElement field = Util.getUnresolvedJavaElement(binding, this.owner, EmptyNodeMap);
            if (field != null) result[elementCount++] = field.resolved(binding);
          }
        } catch (AbortCompilation e) {
          // log the exception and proceed
          Util.logRepeatedMessage(e.getKey(), e);
        }
      }
    }
    size = this.visibleMethods.size();
    if (size > 0) {
      next:
      for (int i = 0; i < size; i++) {
        try {
          MethodBinding binding = (MethodBinding) this.visibleMethods.elementAt(i);
          if (assignableTypeBinding != null
              && !binding.returnType.isCompatibleWith(assignableTypeBinding)) continue next;
          if (this.assistScope.isDefinedInSameUnit(binding.declaringClass)) {
            JavaElement method = getJavaElementOfCompilationUnit(binding);
            if (method != null) result[elementCount++] = method;
          } else {
            JavaElement method = Util.getUnresolvedJavaElement(binding, this.owner, EmptyNodeMap);
            if (method != null) result[elementCount++] = method.resolved(binding);
          }
        } catch (AbortCompilation e) {
          // log the exception and proceed
          Util.logRepeatedMessage(e.getKey(), e);
        }
      }
    }

    if (elementCount != result.length) {
      System.arraycopy(result, 0, result = new IJavaElement[elementCount], 0, elementCount);
    }

    return result;
  }