Пример #1
0
  /**
   * Casting an enclosing instance will considered as useful if removing it would actually bind to a
   * different type
   */
  public static void checkNeedForEnclosingInstanceCast(
      BlockScope scope,
      Expression enclosingInstance,
      TypeBinding enclosingInstanceType,
      TypeBinding memberType) {
    if (scope.compilerOptions().getSeverity(CompilerOptions.UnnecessaryTypeCheck)
        == ProblemSeverities.Ignore) return;

    TypeBinding castedExpressionType = ((CastExpression) enclosingInstance).expression.resolvedType;
    if (castedExpressionType == null) return; // cannot do better
    // obvious identity cast
    if (castedExpressionType == enclosingInstanceType) {
      scope.problemReporter().unnecessaryCast((CastExpression) enclosingInstance);
    } else if (castedExpressionType == TypeBinding.NULL) {
      return; // tolerate null enclosing instance cast
    } else {
      TypeBinding alternateEnclosingInstanceType = castedExpressionType;
      if (castedExpressionType.isBaseType() || castedExpressionType.isArrayType())
        return; // error case
      if (memberType
          == scope.getMemberType(
              memberType.sourceName(), (ReferenceBinding) alternateEnclosingInstanceType)) {
        scope.problemReporter().unnecessaryCast((CastExpression) enclosingInstance);
      }
    }
  }
  protected int matchMethod(MethodBinding method, boolean skipImpossibleArg) {
    if (!matchesName(this.pattern.selector, method.selector)) return IMPOSSIBLE_MATCH;

    int level = ACCURATE_MATCH;
    // look at return type only if declaring type is not specified
    if (this.pattern.declaringSimpleName == null) {
      // TODO (frederic) use this call to refine accuracy on return type
      // int newLevel = resolveLevelForType(this.pattern.returnSimpleName,
      // this.pattern.returnQualification, this.pattern.returnTypeArguments, 0, method.returnType);
      int newLevel =
          resolveLevelForType(
              this.pattern.returnSimpleName, this.pattern.returnQualification, method.returnType);
      if (level > newLevel) {
        if (newLevel == IMPOSSIBLE_MATCH) return IMPOSSIBLE_MATCH;
        level = newLevel; // can only be downgraded
      }
    }

    // parameter types
    int parameterCount =
        this.pattern.parameterSimpleNames == null ? -1 : this.pattern.parameterSimpleNames.length;
    if (parameterCount > -1) {
      // global verification
      if (method.parameters == null) return INACCURATE_MATCH;
      if (parameterCount != method.parameters.length) return IMPOSSIBLE_MATCH;
      if (!method.isValidBinding()
          && ((ProblemMethodBinding) method).problemId() == ProblemReasons.Ambiguous) {
        // return inaccurate match for ambiguous call (bug 80890)
        return INACCURATE_MATCH;
      }

      // verify each parameter
      for (int i = 0; i < parameterCount; i++) {
        TypeBinding argType = method.parameters[i];
        int newLevel = IMPOSSIBLE_MATCH;
        if (argType.isMemberType()) {
          // only compare source name for member type (bug 41018)
          newLevel =
              CharOperation.match(
                      this.pattern.parameterSimpleNames[i],
                      argType.sourceName(),
                      this.isCaseSensitive)
                  ? ACCURATE_MATCH
                  : IMPOSSIBLE_MATCH;
        } else {
          // TODO (frederic) use this call to refine accuracy on parameter types
          //				 newLevel = resolveLevelForType(this.pattern.parameterSimpleNames[i],
          // this.pattern.parameterQualifications[i], this.pattern.parametersTypeArguments[i], 0,
          // argType);
          newLevel =
              resolveLevelForType(
                  this.pattern.parameterSimpleNames[i],
                  this.pattern.parameterQualifications[i],
                  argType);
        }
        if (level > newLevel) {
          if (newLevel == IMPOSSIBLE_MATCH) {
            if (skipImpossibleArg) {
              // Do not consider match as impossible while finding declarations and source level >=
              // 1.5
              // (see  bugs https://bugs.eclipse.org/bugs/show_bug.cgi?id=79990, 96761, 96763)
              newLevel = level;
            } else {
              return IMPOSSIBLE_MATCH;
            }
          }
          level = newLevel; // can only be downgraded
        }
      }
    }

    return level;
  }