< prev index next >

src/jdk.compiler/share/classes/com/sun/tools/javac/model/JavacTypes.java

Print this page




  76             case INTERSECTION:
  77             case ERROR:
  78             case TYPEVAR:
  79                 Type type = cast(Type.class, t);
  80                 return type.asElement();
  81             default:
  82                 return null;
  83         }
  84     }
  85 
  86     @DefinedBy(Api.LANGUAGE_MODEL)
  87     public boolean isSameType(TypeMirror t1, TypeMirror t2) {
  88         if (t1.getKind() == TypeKind.WILDCARD || t2.getKind() == TypeKind.WILDCARD) {
  89             return false;
  90         }
  91         return types.isSameType((Type) t1, (Type) t2);
  92     }
  93 
  94     @DefinedBy(Api.LANGUAGE_MODEL)
  95     public boolean isSubtype(TypeMirror t1, TypeMirror t2) {
  96         validateTypeNotIn(t1, EXEC_OR_PKG);
  97         validateTypeNotIn(t2, EXEC_OR_PKG);
  98         return types.isSubtype((Type) t1, (Type) t2);
  99     }
 100 
 101     @DefinedBy(Api.LANGUAGE_MODEL)
 102     public boolean isAssignable(TypeMirror t1, TypeMirror t2) {
 103         validateTypeNotIn(t1, EXEC_OR_PKG);
 104         validateTypeNotIn(t2, EXEC_OR_PKG);
 105         return types.isAssignable((Type) t1, (Type) t2);
 106     }
 107 
 108     @DefinedBy(Api.LANGUAGE_MODEL)
 109     public boolean contains(TypeMirror t1, TypeMirror t2) {
 110         validateTypeNotIn(t1, EXEC_OR_PKG);
 111         validateTypeNotIn(t2, EXEC_OR_PKG);
 112         return types.containsType((Type) t1, (Type) t2);
 113     }
 114 
 115     @DefinedBy(Api.LANGUAGE_MODEL)
 116     public boolean isSubsignature(ExecutableType m1, ExecutableType m2) {
 117         return types.isSubSignature((Type) m1, (Type) m2);
 118     }
 119 
 120     @DefinedBy(Api.LANGUAGE_MODEL)
 121     public List<Type> directSupertypes(TypeMirror t) {
 122         validateTypeNotIn(t, EXEC_OR_PKG);
 123         Type ty = (Type)t;
 124         return types.directSupertypes(ty).stream()
 125                 .map(Type::stripMetadataIfNeeded)
 126                 .collect(Collectors.toList());
 127     }
 128 
 129     @DefinedBy(Api.LANGUAGE_MODEL)
 130     public TypeMirror erasure(TypeMirror t) {
 131         if (t.getKind() == TypeKind.PACKAGE)

 132             throw new IllegalArgumentException(t.toString());
 133         return types.erasure((Type)t).stripMetadataIfNeeded();
 134     }
 135 
 136     @DefinedBy(Api.LANGUAGE_MODEL)
 137     public TypeElement boxedClass(PrimitiveType p) {
 138         return types.boxedClass((Type) p);
 139     }
 140 
 141     @DefinedBy(Api.LANGUAGE_MODEL)
 142     public PrimitiveType unboxedType(TypeMirror t) {
 143         if (t.getKind() != TypeKind.DECLARED)
 144             throw new IllegalArgumentException(t.toString());
 145         Type unboxed = types.unboxedType((Type) t);
 146         if (! unboxed.isPrimitive())    // only true primitives, not void
 147             throw new IllegalArgumentException(t.toString());
 148         return (PrimitiveType)unboxed;
 149     }
 150 
 151     @DefinedBy(Api.LANGUAGE_MODEL)
 152     public TypeMirror capture(TypeMirror t) {
 153         validateTypeNotIn(t, EXEC_OR_PKG);
 154         return types.capture((Type)t).stripMetadataIfNeeded();
 155     }
 156 
 157     @DefinedBy(Api.LANGUAGE_MODEL)
 158     public PrimitiveType getPrimitiveType(TypeKind kind) {
 159         switch (kind) {
 160         case BOOLEAN:   return syms.booleanType;
 161         case BYTE:      return syms.byteType;
 162         case SHORT:     return syms.shortType;
 163         case INT:       return syms.intType;
 164         case LONG:      return syms.longType;
 165         case CHAR:      return syms.charType;
 166         case FLOAT:     return syms.floatType;
 167         case DOUBLE:    return syms.doubleType;
 168         default:
 169             throw new IllegalArgumentException("Not a primitive type: " + kind);
 170         }
 171     }
 172 
 173     @DefinedBy(Api.LANGUAGE_MODEL)


 175         return (NullType) syms.botType;
 176     }
 177 
 178     @DefinedBy(Api.LANGUAGE_MODEL)
 179     public NoType getNoType(TypeKind kind) {
 180         switch (kind) {
 181         case VOID:      return syms.voidType;
 182         case NONE:      return Type.noType;
 183         default:
 184             throw new IllegalArgumentException(kind.toString());
 185         }
 186     }
 187 
 188     @DefinedBy(Api.LANGUAGE_MODEL)
 189     public ArrayType getArrayType(TypeMirror componentType) {
 190         switch (componentType.getKind()) {
 191         case VOID:
 192         case EXECUTABLE:
 193         case WILDCARD:  // heh!
 194         case PACKAGE:

 195             throw new IllegalArgumentException(componentType.toString());
 196         }
 197         return new Type.ArrayType((Type) componentType, syms.arrayClass);
 198     }
 199 
 200     @DefinedBy(Api.LANGUAGE_MODEL)
 201     public WildcardType getWildcardType(TypeMirror extendsBound,
 202                                         TypeMirror superBound) {
 203         BoundKind bkind;
 204         Type bound;
 205         if (extendsBound == null && superBound == null) {
 206             bkind = BoundKind.UNBOUND;
 207             bound = syms.objectType;
 208         } else if (superBound == null) {
 209             bkind = BoundKind.EXTENDS;
 210             bound = (Type) extendsBound;
 211         } else if (extendsBound == null) {
 212             bkind = BoundKind.SUPER;
 213             bound = (Type) superBound;
 214         } else {


 282      * when viewed as a member of the parameterized type {@code Set<String>},
 283      * the {@code Set.add} method is an {@code ExecutableType}
 284      * whose parameter is of type {@code String}.
 285      *
 286      * @param containing  the containing type
 287      * @param element     the element
 288      * @return the type of the element as viewed from the containing type
 289      * @throws IllegalArgumentException if the element is not a valid one
 290      *          for the given type
 291      */
 292     @DefinedBy(Api.LANGUAGE_MODEL)
 293     public TypeMirror asMemberOf(DeclaredType containing, Element element) {
 294         Type site = (Type)containing;
 295         Symbol sym = (Symbol)element;
 296         if (types.asSuper(site, sym.getEnclosingElement()) == null)
 297             throw new IllegalArgumentException(sym + "@" + site);
 298         return types.memberType(site, sym);
 299     }
 300 
 301 
 302     private static final Set<TypeKind> EXEC_OR_PKG =
 303             EnumSet.of(TypeKind.EXECUTABLE, TypeKind.PACKAGE);
 304 
 305     /**
 306      * Throws an IllegalArgumentException if a type's kind is one of a set.
 307      */
 308     private void validateTypeNotIn(TypeMirror t, Set<TypeKind> invalidKinds) {
 309         if (invalidKinds.contains(t.getKind()))
 310             throw new IllegalArgumentException(t.toString());
 311     }
 312 
 313     /**
 314      * Returns an object cast to the specified type.
 315      * @throws NullPointerException if the object is {@code null}
 316      * @throws IllegalArgumentException if the object is of the wrong type
 317      */
 318     private static <T> T cast(Class<T> clazz, Object o) {
 319         if (! clazz.isInstance(o))
 320             throw new IllegalArgumentException(o.toString());
 321         return clazz.cast(o);
 322     }
 323 




  76             case INTERSECTION:
  77             case ERROR:
  78             case TYPEVAR:
  79                 Type type = cast(Type.class, t);
  80                 return type.asElement();
  81             default:
  82                 return null;
  83         }
  84     }
  85 
  86     @DefinedBy(Api.LANGUAGE_MODEL)
  87     public boolean isSameType(TypeMirror t1, TypeMirror t2) {
  88         if (t1.getKind() == TypeKind.WILDCARD || t2.getKind() == TypeKind.WILDCARD) {
  89             return false;
  90         }
  91         return types.isSameType((Type) t1, (Type) t2);
  92     }
  93 
  94     @DefinedBy(Api.LANGUAGE_MODEL)
  95     public boolean isSubtype(TypeMirror t1, TypeMirror t2) {
  96         validateTypeNotIn(t1, EXEC_OR_PKG_OR_MOD);
  97         validateTypeNotIn(t2, EXEC_OR_PKG_OR_MOD);
  98         return types.isSubtype((Type) t1, (Type) t2);
  99     }
 100 
 101     @DefinedBy(Api.LANGUAGE_MODEL)
 102     public boolean isAssignable(TypeMirror t1, TypeMirror t2) {
 103         validateTypeNotIn(t1, EXEC_OR_PKG_OR_MOD);
 104         validateTypeNotIn(t2, EXEC_OR_PKG_OR_MOD);
 105         return types.isAssignable((Type) t1, (Type) t2);
 106     }
 107 
 108     @DefinedBy(Api.LANGUAGE_MODEL)
 109     public boolean contains(TypeMirror t1, TypeMirror t2) {
 110         validateTypeNotIn(t1, EXEC_OR_PKG_OR_MOD);
 111         validateTypeNotIn(t2, EXEC_OR_PKG_OR_MOD);
 112         return types.containsType((Type) t1, (Type) t2);
 113     }
 114 
 115     @DefinedBy(Api.LANGUAGE_MODEL)
 116     public boolean isSubsignature(ExecutableType m1, ExecutableType m2) {
 117         return types.isSubSignature((Type) m1, (Type) m2);
 118     }
 119 
 120     @DefinedBy(Api.LANGUAGE_MODEL)
 121     public List<Type> directSupertypes(TypeMirror t) {
 122         validateTypeNotIn(t, EXEC_OR_PKG_OR_MOD);
 123         Type ty = (Type)t;
 124         return types.directSupertypes(ty).stream()
 125                 .map(Type::stripMetadataIfNeeded)
 126                 .collect(Collectors.toList());
 127     }
 128 
 129     @DefinedBy(Api.LANGUAGE_MODEL)
 130     public TypeMirror erasure(TypeMirror t) {
 131         TypeKind kind = t.getKind();
 132         if (kind == TypeKind.PACKAGE || kind == TypeKind.MODULE)
 133             throw new IllegalArgumentException(t.toString());
 134         return types.erasure((Type)t).stripMetadataIfNeeded();
 135     }
 136 
 137     @DefinedBy(Api.LANGUAGE_MODEL)
 138     public TypeElement boxedClass(PrimitiveType p) {
 139         return types.boxedClass((Type) p);
 140     }
 141 
 142     @DefinedBy(Api.LANGUAGE_MODEL)
 143     public PrimitiveType unboxedType(TypeMirror t) {
 144         if (t.getKind() != TypeKind.DECLARED)
 145             throw new IllegalArgumentException(t.toString());
 146         Type unboxed = types.unboxedType((Type) t);
 147         if (! unboxed.isPrimitive())    // only true primitives, not void
 148             throw new IllegalArgumentException(t.toString());
 149         return (PrimitiveType)unboxed;
 150     }
 151 
 152     @DefinedBy(Api.LANGUAGE_MODEL)
 153     public TypeMirror capture(TypeMirror t) {
 154         validateTypeNotIn(t, EXEC_OR_PKG_OR_MOD);
 155         return types.capture((Type)t).stripMetadataIfNeeded();
 156     }
 157 
 158     @DefinedBy(Api.LANGUAGE_MODEL)
 159     public PrimitiveType getPrimitiveType(TypeKind kind) {
 160         switch (kind) {
 161         case BOOLEAN:   return syms.booleanType;
 162         case BYTE:      return syms.byteType;
 163         case SHORT:     return syms.shortType;
 164         case INT:       return syms.intType;
 165         case LONG:      return syms.longType;
 166         case CHAR:      return syms.charType;
 167         case FLOAT:     return syms.floatType;
 168         case DOUBLE:    return syms.doubleType;
 169         default:
 170             throw new IllegalArgumentException("Not a primitive type: " + kind);
 171         }
 172     }
 173 
 174     @DefinedBy(Api.LANGUAGE_MODEL)


 176         return (NullType) syms.botType;
 177     }
 178 
 179     @DefinedBy(Api.LANGUAGE_MODEL)
 180     public NoType getNoType(TypeKind kind) {
 181         switch (kind) {
 182         case VOID:      return syms.voidType;
 183         case NONE:      return Type.noType;
 184         default:
 185             throw new IllegalArgumentException(kind.toString());
 186         }
 187     }
 188 
 189     @DefinedBy(Api.LANGUAGE_MODEL)
 190     public ArrayType getArrayType(TypeMirror componentType) {
 191         switch (componentType.getKind()) {
 192         case VOID:
 193         case EXECUTABLE:
 194         case WILDCARD:  // heh!
 195         case PACKAGE:
 196         case MODULE:
 197             throw new IllegalArgumentException(componentType.toString());
 198         }
 199         return new Type.ArrayType((Type) componentType, syms.arrayClass);
 200     }
 201 
 202     @DefinedBy(Api.LANGUAGE_MODEL)
 203     public WildcardType getWildcardType(TypeMirror extendsBound,
 204                                         TypeMirror superBound) {
 205         BoundKind bkind;
 206         Type bound;
 207         if (extendsBound == null && superBound == null) {
 208             bkind = BoundKind.UNBOUND;
 209             bound = syms.objectType;
 210         } else if (superBound == null) {
 211             bkind = BoundKind.EXTENDS;
 212             bound = (Type) extendsBound;
 213         } else if (extendsBound == null) {
 214             bkind = BoundKind.SUPER;
 215             bound = (Type) superBound;
 216         } else {


 284      * when viewed as a member of the parameterized type {@code Set<String>},
 285      * the {@code Set.add} method is an {@code ExecutableType}
 286      * whose parameter is of type {@code String}.
 287      *
 288      * @param containing  the containing type
 289      * @param element     the element
 290      * @return the type of the element as viewed from the containing type
 291      * @throws IllegalArgumentException if the element is not a valid one
 292      *          for the given type
 293      */
 294     @DefinedBy(Api.LANGUAGE_MODEL)
 295     public TypeMirror asMemberOf(DeclaredType containing, Element element) {
 296         Type site = (Type)containing;
 297         Symbol sym = (Symbol)element;
 298         if (types.asSuper(site, sym.getEnclosingElement()) == null)
 299             throw new IllegalArgumentException(sym + "@" + site);
 300         return types.memberType(site, sym);
 301     }
 302 
 303 
 304     private static final Set<TypeKind> EXEC_OR_PKG_OR_MOD =
 305         EnumSet.of(TypeKind.EXECUTABLE, TypeKind.PACKAGE, TypeKind.MODULE);
 306 
 307     /**
 308      * Throws an IllegalArgumentException if a type's kind is one of a set.
 309      */
 310     private void validateTypeNotIn(TypeMirror t, Set<TypeKind> invalidKinds) {
 311         if (invalidKinds.contains(t.getKind()))
 312             throw new IllegalArgumentException(t.toString());
 313     }
 314 
 315     /**
 316      * Returns an object cast to the specified type.
 317      * @throws NullPointerException if the object is {@code null}
 318      * @throws IllegalArgumentException if the object is of the wrong type
 319      */
 320     private static <T> T cast(Class<T> clazz, Object o) {
 321         if (! clazz.isInstance(o))
 322             throw new IllegalArgumentException(o.toString());
 323         return clazz.cast(o);
 324     }
 325 


< prev index next >