/** Tests whether t1 is a subtype of t2, with respect to IGJ Subtyping rules */
  @Override
  public boolean isSubtype(AnnotatedTypeMirror sup, AnnotatedTypeMirror sub) {
    // TODO: Check that they are up to the same base
    // Err... cannot handle type variables quite yet
    if (sup.getKind() == TypeKind.TYPEVAR
        || sup.getKind() == TypeKind.WILDCARD
        || sub.getKind() == TypeKind.TYPEVAR
        || sub.getKind() == TypeKind.WILDCARD) return true;

    AnnotatedTypes annoUtils = new AnnotatedTypes(env, factory);

    AnnotatedTypeMirror valueBaseType = annoUtils.asSuper(sub, sup);
    if (valueBaseType == null)
      // For now
      valueBaseType = sub;

    boolean isSubtype = isSubtypeOneLevel(sup, valueBaseType);
    boolean typeArgShouldBeSame = sup.hasAnnotation(MUTABLE);

    if ((sup.getKind() == TypeKind.DECLARED) && (valueBaseType.getKind() == TypeKind.DECLARED)) {
      AnnotatedDeclaredType supDecl = (AnnotatedDeclaredType) sup;
      AnnotatedDeclaredType subDecl = (AnnotatedDeclaredType) valueBaseType;

      //            if (supDecl.getTypeArguments().size() != subDecl.getTypeArguments().size())
      //                System.out.println("Investigate this " + supDecl + " " + subDecl);

      for (int i = 0;
          i < supDecl.getTypeArguments().size() && i < subDecl.getTypeArguments().size();
          ++i) {
        AnnotatedTypeMirror supArg = supDecl.getTypeArguments().get(i);
        AnnotatedTypeMirror subArg = subDecl.getTypeArguments().get(i);
        if (typeArgShouldBeSame) isSubtype &= isSameImmutability(supArg, subArg);
        else isSubtype &= isSubtype(supArg, subArg);
      }
    } else if ((sup.getKind() == TypeKind.ARRAY) && (valueBaseType.getKind() == TypeKind.ARRAY)) {
      AnnotatedArrayType supArr = (AnnotatedArrayType) sup;
      AnnotatedArrayType subArr = (AnnotatedArrayType) valueBaseType;
      if (typeArgShouldBeSame)
        isSubtype &= isSameImmutability(supArr.getComponentType(), subArr.getComponentType());
      else isSubtype &= isSubtype(supArr.getComponentType(), subArr.getComponentType());
    }

    return isSubtype;
  }
Пример #2
0
 @Override
 public final R visitArray(AnnotatedArrayType type, AnnotatedTypeMirror p) {
   assert p instanceof AnnotatedArrayType : p;
   R r = scan(type.getComponentType(), ((AnnotatedArrayType) p).getComponentType());
   return r;
 }