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

Sat, 01 Dec 2007 00:00:00 +0000

author
duke
date
Sat, 01 Dec 2007 00:00:00 +0000
changeset 1
9a66ca7c79fa
child 554
9d9f26857129
permissions
-rw-r--r--

Initial load

duke@1 1 /*
duke@1 2 * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
duke@1 7 * published by the Free Software Foundation. Sun designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
duke@1 9 * by Sun in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
duke@1 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@1 22 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@1 23 * have any questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.javac.model;
duke@1 27
duke@1 28 import java.util.List;
duke@1 29 import java.util.Set;
duke@1 30 import java.util.EnumSet;
duke@1 31 import javax.lang.model.element.*;
duke@1 32 import javax.lang.model.type.*;
duke@1 33 import com.sun.tools.javac.code.*;
duke@1 34 import com.sun.tools.javac.code.Symbol.*;
duke@1 35 import com.sun.tools.javac.util.*;
duke@1 36
duke@1 37 /**
duke@1 38 * Utility methods for operating on types.
duke@1 39 *
duke@1 40 * <p><b>This is NOT part of any API supported by Sun Microsystems.
duke@1 41 * If you write code that depends on this, you do so at your own
duke@1 42 * risk. This code and its internal interfaces are subject to change
duke@1 43 * or deletion without notice.</b></p>
duke@1 44 */
duke@1 45 public class JavacTypes implements javax.lang.model.util.Types {
duke@1 46
duke@1 47 private Symtab syms;
duke@1 48 private Types types;
duke@1 49
duke@1 50 private static final Context.Key<JavacTypes> KEY =
duke@1 51 new Context.Key<JavacTypes>();
duke@1 52
duke@1 53 public static JavacTypes instance(Context context) {
duke@1 54 JavacTypes instance = context.get(KEY);
duke@1 55 if (instance == null) {
duke@1 56 instance = new JavacTypes(context);
duke@1 57 context.put(KEY, instance);
duke@1 58 }
duke@1 59 return instance;
duke@1 60 }
duke@1 61
duke@1 62 /**
duke@1 63 * Public for use only by JavacProcessingEnvironment
duke@1 64 */
duke@1 65 // TODO JavacTypes constructor should be protected
duke@1 66 public JavacTypes(Context context) {
duke@1 67 setContext(context);
duke@1 68 }
duke@1 69
duke@1 70 /**
duke@1 71 * Use a new context. May be called from outside to update
duke@1 72 * internal state for a new annotation-processing round.
duke@1 73 * This instance is *not* then registered with the new context.
duke@1 74 */
duke@1 75 public void setContext(Context context) {
duke@1 76 syms = Symtab.instance(context);
duke@1 77 types = Types.instance(context);
duke@1 78 }
duke@1 79
duke@1 80 public Element asElement(TypeMirror t) {
duke@1 81 Type type = cast(Type.class, t);
duke@1 82 if (type.tag != TypeTags.CLASS && type.tag != TypeTags.TYPEVAR)
duke@1 83 return null;
duke@1 84 return type.asElement();
duke@1 85 }
duke@1 86
duke@1 87 public boolean isSameType(TypeMirror t1, TypeMirror t2) {
duke@1 88 return types.isSameType((Type) t1, (Type) t2);
duke@1 89 }
duke@1 90
duke@1 91 public boolean isSubtype(TypeMirror t1, TypeMirror t2) {
duke@1 92 validateTypeNotIn(t1, EXEC_OR_PKG);
duke@1 93 validateTypeNotIn(t2, EXEC_OR_PKG);
duke@1 94 return types.isSubtype((Type) t1, (Type) t2);
duke@1 95 }
duke@1 96
duke@1 97 public boolean isAssignable(TypeMirror t1, TypeMirror t2) {
duke@1 98 validateTypeNotIn(t1, EXEC_OR_PKG);
duke@1 99 validateTypeNotIn(t2, EXEC_OR_PKG);
duke@1 100 return types.isAssignable((Type) t1, (Type) t2);
duke@1 101 }
duke@1 102
duke@1 103 public boolean contains(TypeMirror t1, TypeMirror t2) {
duke@1 104 validateTypeNotIn(t1, EXEC_OR_PKG);
duke@1 105 validateTypeNotIn(t2, EXEC_OR_PKG);
duke@1 106 return ((Type) t1).contains((Type) t2);
duke@1 107 }
duke@1 108
duke@1 109 public boolean isSubsignature(ExecutableType m1, ExecutableType m2) {
duke@1 110 return types.isSubSignature((Type) m1, (Type) m2);
duke@1 111 }
duke@1 112
duke@1 113 public List<Type> directSupertypes(TypeMirror t) {
duke@1 114 validateTypeNotIn(t, EXEC_OR_PKG);
duke@1 115 Type type = (Type) t;
duke@1 116 Type sup = types.supertype(type);
duke@1 117 return (sup == Type.noType || sup == type || sup == null)
duke@1 118 ? types.interfaces(type)
duke@1 119 : types.interfaces(type).prepend(sup);
duke@1 120 }
duke@1 121
duke@1 122 public TypeMirror erasure(TypeMirror t) {
duke@1 123 if (t.getKind() == TypeKind.PACKAGE)
duke@1 124 throw new IllegalArgumentException(t.toString());
duke@1 125 return types.erasure((Type) t);
duke@1 126 }
duke@1 127
duke@1 128 public TypeElement boxedClass(PrimitiveType p) {
duke@1 129 return types.boxedClass((Type) p);
duke@1 130 }
duke@1 131
duke@1 132 public PrimitiveType unboxedType(TypeMirror t) {
duke@1 133 if (t.getKind() != TypeKind.DECLARED)
duke@1 134 throw new IllegalArgumentException(t.toString());
duke@1 135 Type unboxed = types.unboxedType((Type) t);
duke@1 136 if (! unboxed.isPrimitive()) // only true primitives, not void
duke@1 137 throw new IllegalArgumentException(t.toString());
duke@1 138 return unboxed;
duke@1 139 }
duke@1 140
duke@1 141 public TypeMirror capture(TypeMirror t) {
duke@1 142 validateTypeNotIn(t, EXEC_OR_PKG);
duke@1 143 return types.capture((Type) t);
duke@1 144 }
duke@1 145
duke@1 146 public PrimitiveType getPrimitiveType(TypeKind kind) {
duke@1 147 switch (kind) {
duke@1 148 case BOOLEAN: return syms.booleanType;
duke@1 149 case BYTE: return syms.byteType;
duke@1 150 case SHORT: return syms.shortType;
duke@1 151 case INT: return syms.intType;
duke@1 152 case LONG: return syms.longType;
duke@1 153 case CHAR: return syms.charType;
duke@1 154 case FLOAT: return syms.floatType;
duke@1 155 case DOUBLE: return syms.doubleType;
duke@1 156 default:
duke@1 157 throw new IllegalArgumentException("Not a primitive type: " + kind);
duke@1 158 }
duke@1 159 }
duke@1 160
duke@1 161 public NullType getNullType() {
duke@1 162 return (NullType) syms.botType;
duke@1 163 }
duke@1 164
duke@1 165 public NoType getNoType(TypeKind kind) {
duke@1 166 switch (kind) {
duke@1 167 case VOID: return syms.voidType;
duke@1 168 case NONE: return Type.noType;
duke@1 169 default:
duke@1 170 throw new IllegalArgumentException(kind.toString());
duke@1 171 }
duke@1 172 }
duke@1 173
duke@1 174 public ArrayType getArrayType(TypeMirror componentType) {
duke@1 175 switch (componentType.getKind()) {
duke@1 176 case VOID:
duke@1 177 case EXECUTABLE:
duke@1 178 case WILDCARD: // heh!
duke@1 179 case PACKAGE:
duke@1 180 throw new IllegalArgumentException(componentType.toString());
duke@1 181 }
duke@1 182 return new Type.ArrayType((Type) componentType, syms.arrayClass);
duke@1 183 }
duke@1 184
duke@1 185 public WildcardType getWildcardType(TypeMirror extendsBound,
duke@1 186 TypeMirror superBound) {
duke@1 187 BoundKind bkind;
duke@1 188 Type bound;
duke@1 189 if (extendsBound == null && superBound == null) {
duke@1 190 bkind = BoundKind.UNBOUND;
duke@1 191 bound = syms.objectType;
duke@1 192 } else if (superBound == null) {
duke@1 193 bkind = BoundKind.EXTENDS;
duke@1 194 bound = (Type) extendsBound;
duke@1 195 } else if (extendsBound == null) {
duke@1 196 bkind = BoundKind.SUPER;
duke@1 197 bound = (Type) superBound;
duke@1 198 } else {
duke@1 199 throw new IllegalArgumentException(
duke@1 200 "Extends and super bounds cannot both be provided");
duke@1 201 }
duke@1 202 switch (bound.getKind()) {
duke@1 203 case ARRAY:
duke@1 204 case DECLARED:
duke@1 205 case ERROR:
duke@1 206 case TYPEVAR:
duke@1 207 return new Type.WildcardType(bound, bkind, syms.boundClass);
duke@1 208 default:
duke@1 209 throw new IllegalArgumentException(bound.toString());
duke@1 210 }
duke@1 211 }
duke@1 212
duke@1 213 public DeclaredType getDeclaredType(TypeElement typeElem,
duke@1 214 TypeMirror... typeArgs) {
duke@1 215 ClassSymbol sym = (ClassSymbol) typeElem;
duke@1 216
duke@1 217 if (typeArgs.length == 0)
duke@1 218 return (DeclaredType) sym.erasure(types);
duke@1 219 if (sym.type.getEnclosingType().isParameterized())
duke@1 220 throw new IllegalArgumentException(sym.toString());
duke@1 221
duke@1 222 return getDeclaredType0(sym.type.getEnclosingType(), sym, typeArgs);
duke@1 223 }
duke@1 224
duke@1 225 public DeclaredType getDeclaredType(DeclaredType enclosing,
duke@1 226 TypeElement typeElem,
duke@1 227 TypeMirror... typeArgs) {
duke@1 228 if (enclosing == null)
duke@1 229 return getDeclaredType(typeElem, typeArgs);
duke@1 230
duke@1 231 ClassSymbol sym = (ClassSymbol) typeElem;
duke@1 232 Type outer = (Type) enclosing;
duke@1 233
duke@1 234 if (outer.tsym != sym.owner.enclClass())
duke@1 235 throw new IllegalArgumentException(enclosing.toString());
duke@1 236 if (!outer.isParameterized())
duke@1 237 return getDeclaredType(typeElem, typeArgs);
duke@1 238
duke@1 239 return getDeclaredType0(outer, sym, typeArgs);
duke@1 240 }
duke@1 241 // where
duke@1 242 private DeclaredType getDeclaredType0(Type outer,
duke@1 243 ClassSymbol sym,
duke@1 244 TypeMirror... typeArgs) {
duke@1 245 if (typeArgs.length != sym.type.getTypeArguments().length())
duke@1 246 throw new IllegalArgumentException(
duke@1 247 "Incorrect number of type arguments");
duke@1 248
duke@1 249 ListBuffer<Type> targs = new ListBuffer<Type>();
duke@1 250 for (TypeMirror t : typeArgs) {
duke@1 251 if (!(t instanceof ReferenceType || t instanceof WildcardType))
duke@1 252 throw new IllegalArgumentException(t.toString());
duke@1 253 targs.append((Type) t);
duke@1 254 }
duke@1 255 // TODO: Would like a way to check that type args match formals.
duke@1 256
duke@1 257 return (DeclaredType) new Type.ClassType(outer, targs.toList(), sym);
duke@1 258 }
duke@1 259
duke@1 260 /**
duke@1 261 * Returns the type of an element when that element is viewed as
duke@1 262 * a member of, or otherwise directly contained by, a given type.
duke@1 263 * For example,
duke@1 264 * when viewed as a member of the parameterized type {@code Set<String>},
duke@1 265 * the {@code Set.add} method is an {@code ExecutableType}
duke@1 266 * whose parameter is of type {@code String}.
duke@1 267 *
duke@1 268 * @param containing the containing type
duke@1 269 * @param element the element
duke@1 270 * @return the type of the element as viewed from the containing type
duke@1 271 * @throws IllegalArgumentException if the element is not a valid one
duke@1 272 * for the given type
duke@1 273 */
duke@1 274 public TypeMirror asMemberOf(DeclaredType containing, Element element) {
duke@1 275 Type site = (Type)containing;
duke@1 276 Symbol sym = (Symbol)element;
duke@1 277 if (types.asSuper(site, sym.getEnclosingElement()) == null)
duke@1 278 throw new IllegalArgumentException(sym + "@" + site);
duke@1 279 return types.memberType(site, sym);
duke@1 280 }
duke@1 281
duke@1 282
duke@1 283 private static final Set<TypeKind> EXEC_OR_PKG =
duke@1 284 EnumSet.of(TypeKind.EXECUTABLE, TypeKind.PACKAGE);
duke@1 285
duke@1 286 /**
duke@1 287 * Throws an IllegalArgumentException if a type's kind is one of a set.
duke@1 288 */
duke@1 289 private void validateTypeNotIn(TypeMirror t, Set<TypeKind> invalidKinds) {
duke@1 290 if (invalidKinds.contains(t.getKind()))
duke@1 291 throw new IllegalArgumentException(t.toString());
duke@1 292 }
duke@1 293
duke@1 294 /**
duke@1 295 * Returns an object cast to the specified type.
duke@1 296 * @throws NullPointerException if the object is {@code null}
duke@1 297 * @throws IllegalArgumentException if the object is of the wrong type
duke@1 298 */
duke@1 299 private static <T> T cast(Class<T> clazz, Object o) {
duke@1 300 if (! clazz.isInstance(o))
duke@1 301 throw new IllegalArgumentException(o.toString());
duke@1 302 return clazz.cast(o);
duke@1 303 }
duke@1 304 }

mercurial