static boolean areTypesEqual(TypeBinding one, TypeBinding two) {
    if (TypeBinding.equalsEquals(one, two)) return true;
    // https://bugs.eclipse.org/bugs/show_bug.cgi?id=329584
    switch (one.kind()) {
      case Binding.TYPE:
        switch (two.kind()) {
          case Binding.PARAMETERIZED_TYPE:
          case Binding.RAW_TYPE:
            if (TypeBinding.equalsEquals(one, two.erasure())) return true;
        }
        break;
      case Binding.RAW_TYPE:
      case Binding.PARAMETERIZED_TYPE:
        switch (two.kind()) {
          case Binding.TYPE:
            if (TypeBinding.equalsEquals(one.erasure(), two)) return true;
        }
    }

    // need to consider X<?> and X<? extends Object> as the same 'type'
    if (one.isParameterizedType() && two.isParameterizedType())
      return one.isEquivalentTo(two) && two.isEquivalentTo(one);

    // Can skip this since we resolved each method before comparing it, see
    // computeSubstituteMethod()
    //	if (one instanceof UnresolvedReferenceBinding)
    //		return ((UnresolvedReferenceBinding) one).resolvedType == two;
    //	if (two instanceof UnresolvedReferenceBinding)
    //		return ((UnresolvedReferenceBinding) two).resolvedType == one;
    return false; // all other type bindings are identical
  }
Exemple #2
0
  /* Answer true if the type use is considered deprecated.
   * An access in the same compilation unit is allowed.
   */
  public final boolean isTypeUseDeprecated(TypeBinding type, Scope scope) {

    if (type.isArrayType()) {
      type = ((ArrayBinding) type).leafComponentType;
    }
    if (type.isBaseType()) return false;

    ReferenceBinding refType = (ReferenceBinding) type;
    // ignore references insing Javadoc comments
    if ((this.bits & ASTNode.InsideJavadoc) == 0
        && refType.isOrEnclosedByPrivateType()
        && !scope.isDefinedInType(refType)) {
      // ignore cases where type is used from inside itself
      ((ReferenceBinding) refType.erasure()).modifiers |= ExtraCompilerModifiers.AccLocallyUsed;
    }

    if (refType.hasRestrictedAccess()) {
      AccessRestriction restriction = scope.environment().getAccessRestriction(type.erasure());
      if (restriction != null) {
        scope
            .problemReporter()
            .forbiddenReference(
                type,
                this,
                restriction.classpathEntryType,
                restriction.classpathEntryName,
                restriction.getProblemId());
      }
    }

    // force annotations resolution before deciding whether the type may be deprecated
    refType.initializeDeprecatedAnnotationTagBits();

    if (!refType.isViewedAsDeprecated()) return false;

    // inside same unit - no report
    if (scope.isDefinedInSameUnit(refType)) return false;

    // if context is deprecated, may avoid reporting
    if (!scope.compilerOptions().reportDeprecationInsideDeprecatedCode
        && scope.isInsideDeprecatedCode()) return false;
    return true;
  }
  private ReferenceBinding typeToRecord(TypeBinding type) {
    if (type == null) return null;
    while (type.isArrayType()) type = ((ArrayBinding) type).leafComponentType();

    switch (type.kind()) {
      case Binding.BASE_TYPE:
      case Binding.TYPE_PARAMETER:
      case Binding.WILDCARD_TYPE:
      case Binding.INTERSECTION_TYPE:
      case Binding.INTERSECTION_TYPE18: // constituents would have been recorded.
      case Binding.POLY_TYPE: // not a real type, will mutate into one, hopefully soon.
        return null;
      case Binding.PARAMETERIZED_TYPE:
      case Binding.RAW_TYPE:
        type = type.erasure();
    }
    ReferenceBinding refType = (ReferenceBinding) type;
    if (refType.isLocalType()) return null;
    return refType;
  }
Exemple #4
0
  public final boolean canBeSeenBy(
      TypeBinding receiverType, InvocationSite invocationSite, Scope scope) {
    if (isPublic()) return true;

    SourceTypeBinding invocationType = scope.enclosingSourceType();
    if (invocationType == this.declaringClass && invocationType == receiverType) return true;

    if (invocationType == null) // static import call
    return !isPrivate() && scope.getCurrentPackage() == this.declaringClass.fPackage;

    if (isProtected()) {
      // answer true if the invocationType is the declaringClass or they are in the same package
      // OR the invocationType is a subclass of the declaringClass
      //    AND the receiverType is the invocationType or its subclass
      //    OR the method is a static method accessed directly through a type
      //    OR previous assertions are true for one of the enclosing type
      if (invocationType == this.declaringClass) return true;
      if (invocationType.fPackage == this.declaringClass.fPackage) return true;

      ReferenceBinding currentType = invocationType;
      int depth = 0;
      ReferenceBinding receiverErasure = (ReferenceBinding) receiverType.erasure();
      ReferenceBinding declaringErasure = (ReferenceBinding) this.declaringClass.erasure();
      do {
        if (currentType.findSuperTypeOriginatingFrom(declaringErasure) != null) {
          if (invocationSite.isSuperAccess()) return true;
          // receiverType can be an array binding in one case... see if you can change it
          if (receiverType instanceof ArrayBinding) return false;
          if (isStatic()) {
            if (depth > 0) invocationSite.setDepth(depth);
            return true; // see 1FMEPDL - return invocationSite.isTypeAccess();
          }
          if (currentType == receiverErasure
              || receiverErasure.findSuperTypeOriginatingFrom(currentType) != null) {
            if (depth > 0) invocationSite.setDepth(depth);
            return true;
          }
        }
        depth++;
        currentType = currentType.enclosingType();
      } while (currentType != null);
      return false;
    }

    if (isPrivate()) {
      // answer true if the receiverType is the declaringClass
      // AND the invocationType and the declaringClass have a common enclosingType
      receiverCheck:
      {
        if (receiverType != this.declaringClass) {
          // special tolerance for type variable direct bounds
          if (receiverType.isTypeVariable()
              && ((TypeVariableBinding) receiverType)
                  .isErasureBoundTo(this.declaringClass.erasure())) break receiverCheck;
          return false;
        }
      }

      if (invocationType != this.declaringClass) {
        ReferenceBinding outerInvocationType = invocationType;
        ReferenceBinding temp = outerInvocationType.enclosingType();
        while (temp != null) {
          outerInvocationType = temp;
          temp = temp.enclosingType();
        }

        ReferenceBinding outerDeclaringClass = (ReferenceBinding) this.declaringClass.erasure();
        temp = outerDeclaringClass.enclosingType();
        while (temp != null) {
          outerDeclaringClass = temp;
          temp = temp.enclosingType();
        }
        if (outerInvocationType != outerDeclaringClass) return false;
      }
      return true;
    }

    // isDefault()
    PackageBinding declaringPackage = this.declaringClass.fPackage;
    if (invocationType.fPackage != declaringPackage) return false;

    // receiverType can be an array binding in one case... see if you can change it
    if (receiverType instanceof ArrayBinding) return false;
    TypeBinding originalDeclaringClass = this.declaringClass.original();
    ReferenceBinding currentType = (ReferenceBinding) receiverType;
    do {
      if (originalDeclaringClass == currentType.original()) return true;
      PackageBinding currentPackage = currentType.fPackage;
      // package could be null for wildcards/intersection types, ignore and recurse in superclass
      if (currentPackage != null && currentPackage != declaringPackage) return false;
    } while ((currentType = currentType.superclass()) != null);
    return false;
  }