Example #1
0
 private final boolean isCoerceableFromParameterizedType(ResolvedType other) {
   if (!other.isParameterizedType()) return false;
   ResolvedType myRawType = (ResolvedType) getRawType();
   ResolvedType theirRawType = (ResolvedType) other.getRawType();
   if (myRawType == theirRawType) {
     if (getTypeParameters().length == other.getTypeParameters().length) {
       // there's a chance it can be done
       ResolvedType[] myTypeParameters = getResolvedTypeParameters();
       ResolvedType[] theirTypeParameters = other.getResolvedTypeParameters();
       for (int i = 0; i < myTypeParameters.length; i++) {
         if (myTypeParameters[i] != theirTypeParameters[i]) {
           // thin ice now... but List<String> may still be coerceable from e.g. List<T>
           if (myTypeParameters[i].isGenericWildcard()) {
             BoundedReferenceType wildcard = (BoundedReferenceType) myTypeParameters[i];
             if (!wildcard.canBeCoercedTo(theirTypeParameters[i])) return false;
           } else if (myTypeParameters[i].isTypeVariableReference()) {
             TypeVariableReferenceType tvrt = (TypeVariableReferenceType) myTypeParameters[i];
             TypeVariable tv = tvrt.getTypeVariable();
             tv.resolve(world);
             if (!tv.canBeBoundTo(theirTypeParameters[i])) return false;
           } else if (theirTypeParameters[i].isTypeVariableReference()) {
             TypeVariableReferenceType tvrt = (TypeVariableReferenceType) theirTypeParameters[i];
             TypeVariable tv = tvrt.getTypeVariable();
             tv.resolve(world);
             if (!tv.canBeBoundTo(myTypeParameters[i])) return false;
           } else {
             return false;
           }
         }
       }
       return true;
     }
   } else {
     // we do this walk for situations like the following:
     // Base<T>, Sub<S,T> extends Base<S>
     // is Sub<Y,Z> coerceable from Base<X> ???
     for (Iterator i = getDirectSupertypes(); i.hasNext(); ) {
       ReferenceType parent = (ReferenceType) i.next();
       if (parent.isCoerceableFromParameterizedType(other)) return true;
     }
   }
   return false;
 }