Example #1
0
  // true iff the statement "this = other" would compile.
  public boolean isAssignableFrom(ResolvedType other, boolean allowMissing) {
    if (other.isPrimitiveType()) {
      if (!world.isInJava5Mode()) return false;
      if (ResolvedType.validBoxing.contains(this.getSignature() + other.getSignature()))
        return true;
    }
    if (this == other) return true;
    if (this.getSignature().equals(ResolvedType.OBJECT.getSignature())) return true;

    if ((this.isRawType() || this.isGenericType()) && other.isParameterizedType()) {
      if (isAssignableFrom((ResolvedType) other.getRawType())) return true;
    }
    if (this.isRawType() && other.isGenericType()) {
      if (isAssignableFrom((ResolvedType) other.getRawType())) return true;
    }
    if (this.isGenericType() && other.isRawType()) {
      if (isAssignableFrom((ResolvedType) other.getGenericType())) return true;
    }

    if (this.isParameterizedType()) {
      // look at wildcards...
      if (((ReferenceType) this.getRawType()).isAssignableFrom(other)) {
        boolean wildcardsAllTheWay = true;
        ResolvedType[] myParameters = this.getResolvedTypeParameters();
        for (int i = 0; i < myParameters.length; i++) {
          if (!myParameters[i].isGenericWildcard()) {
            wildcardsAllTheWay = false;
          } else if (myParameters[i].isExtends() || myParameters[i].isSuper()) {
            wildcardsAllTheWay = false;
          }
        }
        if (wildcardsAllTheWay && !other.isParameterizedType()) return true;
        // we have to match by parameters one at a time
        ResolvedType[] theirParameters = other.getResolvedTypeParameters();
        boolean parametersAssignable = true;
        if (myParameters.length == theirParameters.length) {
          for (int i = 0; i < myParameters.length; i++) {
            if (myParameters[i] == theirParameters[i]) continue;
            if (myParameters[i].isAssignableFrom(theirParameters[i], allowMissing)) {
              continue;
            }
            if (!myParameters[i].isGenericWildcard()) {
              parametersAssignable = false;
              break;
            } else {
              BoundedReferenceType wildcardType = (BoundedReferenceType) myParameters[i];
              if (!wildcardType.alwaysMatches(theirParameters[i])) {
                parametersAssignable = false;
                break;
              }
            }
          }
        } else {
          parametersAssignable = false;
        }
        if (parametersAssignable) return true;
      }
    }

    if (isTypeVariableReference()
        && !other.isTypeVariableReference()) { // eg. this=T  other=Ljava/lang/Object;
      TypeVariable aVar = ((TypeVariableReference) this).getTypeVariable();
      return aVar.resolve(world).canBeBoundTo(other);
    }

    if (other.isTypeVariableReference()) {
      TypeVariableReferenceType otherType = (TypeVariableReferenceType) other;
      if (this instanceof TypeVariableReference) {
        return ((TypeVariableReference) this)
            .getTypeVariable()
            .canBeBoundTo(otherType.getTypeVariable().getFirstBound().resolve(world)); // pr171952
        //       			return
        // ((TypeVariableReference)this).getTypeVariable()==otherType.getTypeVariable();
      } else {
        // FIXME asc should this say canBeBoundTo??
        return this.isAssignableFrom(otherType.getTypeVariable().getFirstBound().resolve(world));
      }
    }

    if (allowMissing && other.isMissing()) return false;

    for (Iterator i = other.getDirectSupertypes(); i.hasNext(); ) {
      if (this.isAssignableFrom((ResolvedType) i.next(), allowMissing)) return true;
    }
    return false;
  }