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

Tue, 05 Oct 2010 11:34:43 -0700

author
jjg
date
Tue, 05 Oct 2010 11:34:43 -0700
changeset 706
971c8132f5b2
parent 675
12d8f7e417fd
child 798
4868a36f6fd8
permissions
-rw-r--r--

6988836: A new JavacElements is created for each round of annotation processing
Reviewed-by: darcy

duke@1 1 /*
ohair@554 2 * Copyright (c) 2005, 2006, Oracle and/or its affiliates. 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
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle 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 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * 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 *
jjg@581 40 * <p><b>This is NOT part of any supported API.
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 public static JavacTypes instance(Context context) {
jjg@706 51 JavacTypes instance = context.get(JavacTypes.class);
jjg@706 52 if (instance == null)
duke@1 53 instance = new JavacTypes(context);
duke@1 54 return instance;
duke@1 55 }
duke@1 56
duke@1 57 /**
duke@1 58 * Public for use only by JavacProcessingEnvironment
duke@1 59 */
jjg@706 60 protected JavacTypes(Context context) {
duke@1 61 setContext(context);
duke@1 62 }
duke@1 63
duke@1 64 /**
duke@1 65 * Use a new context. May be called from outside to update
duke@1 66 * internal state for a new annotation-processing round.
duke@1 67 */
duke@1 68 public void setContext(Context context) {
jjg@706 69 context.put(JavacTypes.class, this);
duke@1 70 syms = Symtab.instance(context);
duke@1 71 types = Types.instance(context);
duke@1 72 }
duke@1 73
duke@1 74 public Element asElement(TypeMirror t) {
duke@1 75 Type type = cast(Type.class, t);
duke@1 76 if (type.tag != TypeTags.CLASS && type.tag != TypeTags.TYPEVAR)
duke@1 77 return null;
duke@1 78 return type.asElement();
duke@1 79 }
duke@1 80
duke@1 81 public boolean isSameType(TypeMirror t1, TypeMirror t2) {
duke@1 82 return types.isSameType((Type) t1, (Type) t2);
duke@1 83 }
duke@1 84
duke@1 85 public boolean isSubtype(TypeMirror t1, TypeMirror t2) {
duke@1 86 validateTypeNotIn(t1, EXEC_OR_PKG);
duke@1 87 validateTypeNotIn(t2, EXEC_OR_PKG);
duke@1 88 return types.isSubtype((Type) t1, (Type) t2);
duke@1 89 }
duke@1 90
duke@1 91 public boolean isAssignable(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.isAssignable((Type) t1, (Type) t2);
duke@1 95 }
duke@1 96
duke@1 97 public boolean contains(TypeMirror t1, TypeMirror t2) {
duke@1 98 validateTypeNotIn(t1, EXEC_OR_PKG);
duke@1 99 validateTypeNotIn(t2, EXEC_OR_PKG);
mcimadamore@675 100 return types.containsType((Type) t1, (Type) t2);
duke@1 101 }
duke@1 102
duke@1 103 public boolean isSubsignature(ExecutableType m1, ExecutableType m2) {
duke@1 104 return types.isSubSignature((Type) m1, (Type) m2);
duke@1 105 }
duke@1 106
duke@1 107 public List<Type> directSupertypes(TypeMirror t) {
duke@1 108 validateTypeNotIn(t, EXEC_OR_PKG);
duke@1 109 Type type = (Type) t;
duke@1 110 Type sup = types.supertype(type);
duke@1 111 return (sup == Type.noType || sup == type || sup == null)
duke@1 112 ? types.interfaces(type)
duke@1 113 : types.interfaces(type).prepend(sup);
duke@1 114 }
duke@1 115
duke@1 116 public TypeMirror erasure(TypeMirror t) {
duke@1 117 if (t.getKind() == TypeKind.PACKAGE)
duke@1 118 throw new IllegalArgumentException(t.toString());
duke@1 119 return types.erasure((Type) t);
duke@1 120 }
duke@1 121
duke@1 122 public TypeElement boxedClass(PrimitiveType p) {
duke@1 123 return types.boxedClass((Type) p);
duke@1 124 }
duke@1 125
duke@1 126 public PrimitiveType unboxedType(TypeMirror t) {
duke@1 127 if (t.getKind() != TypeKind.DECLARED)
duke@1 128 throw new IllegalArgumentException(t.toString());
duke@1 129 Type unboxed = types.unboxedType((Type) t);
duke@1 130 if (! unboxed.isPrimitive()) // only true primitives, not void
duke@1 131 throw new IllegalArgumentException(t.toString());
duke@1 132 return unboxed;
duke@1 133 }
duke@1 134
duke@1 135 public TypeMirror capture(TypeMirror t) {
duke@1 136 validateTypeNotIn(t, EXEC_OR_PKG);
duke@1 137 return types.capture((Type) t);
duke@1 138 }
duke@1 139
duke@1 140 public PrimitiveType getPrimitiveType(TypeKind kind) {
duke@1 141 switch (kind) {
duke@1 142 case BOOLEAN: return syms.booleanType;
duke@1 143 case BYTE: return syms.byteType;
duke@1 144 case SHORT: return syms.shortType;
duke@1 145 case INT: return syms.intType;
duke@1 146 case LONG: return syms.longType;
duke@1 147 case CHAR: return syms.charType;
duke@1 148 case FLOAT: return syms.floatType;
duke@1 149 case DOUBLE: return syms.doubleType;
duke@1 150 default:
duke@1 151 throw new IllegalArgumentException("Not a primitive type: " + kind);
duke@1 152 }
duke@1 153 }
duke@1 154
duke@1 155 public NullType getNullType() {
duke@1 156 return (NullType) syms.botType;
duke@1 157 }
duke@1 158
duke@1 159 public NoType getNoType(TypeKind kind) {
duke@1 160 switch (kind) {
duke@1 161 case VOID: return syms.voidType;
duke@1 162 case NONE: return Type.noType;
duke@1 163 default:
duke@1 164 throw new IllegalArgumentException(kind.toString());
duke@1 165 }
duke@1 166 }
duke@1 167
duke@1 168 public ArrayType getArrayType(TypeMirror componentType) {
duke@1 169 switch (componentType.getKind()) {
duke@1 170 case VOID:
duke@1 171 case EXECUTABLE:
duke@1 172 case WILDCARD: // heh!
duke@1 173 case PACKAGE:
duke@1 174 throw new IllegalArgumentException(componentType.toString());
duke@1 175 }
duke@1 176 return new Type.ArrayType((Type) componentType, syms.arrayClass);
duke@1 177 }
duke@1 178
duke@1 179 public WildcardType getWildcardType(TypeMirror extendsBound,
duke@1 180 TypeMirror superBound) {
duke@1 181 BoundKind bkind;
duke@1 182 Type bound;
duke@1 183 if (extendsBound == null && superBound == null) {
duke@1 184 bkind = BoundKind.UNBOUND;
duke@1 185 bound = syms.objectType;
duke@1 186 } else if (superBound == null) {
duke@1 187 bkind = BoundKind.EXTENDS;
duke@1 188 bound = (Type) extendsBound;
duke@1 189 } else if (extendsBound == null) {
duke@1 190 bkind = BoundKind.SUPER;
duke@1 191 bound = (Type) superBound;
duke@1 192 } else {
duke@1 193 throw new IllegalArgumentException(
duke@1 194 "Extends and super bounds cannot both be provided");
duke@1 195 }
duke@1 196 switch (bound.getKind()) {
duke@1 197 case ARRAY:
duke@1 198 case DECLARED:
duke@1 199 case ERROR:
duke@1 200 case TYPEVAR:
duke@1 201 return new Type.WildcardType(bound, bkind, syms.boundClass);
duke@1 202 default:
duke@1 203 throw new IllegalArgumentException(bound.toString());
duke@1 204 }
duke@1 205 }
duke@1 206
duke@1 207 public DeclaredType getDeclaredType(TypeElement typeElem,
duke@1 208 TypeMirror... typeArgs) {
duke@1 209 ClassSymbol sym = (ClassSymbol) typeElem;
duke@1 210
duke@1 211 if (typeArgs.length == 0)
duke@1 212 return (DeclaredType) sym.erasure(types);
duke@1 213 if (sym.type.getEnclosingType().isParameterized())
duke@1 214 throw new IllegalArgumentException(sym.toString());
duke@1 215
duke@1 216 return getDeclaredType0(sym.type.getEnclosingType(), sym, typeArgs);
duke@1 217 }
duke@1 218
duke@1 219 public DeclaredType getDeclaredType(DeclaredType enclosing,
duke@1 220 TypeElement typeElem,
duke@1 221 TypeMirror... typeArgs) {
duke@1 222 if (enclosing == null)
duke@1 223 return getDeclaredType(typeElem, typeArgs);
duke@1 224
duke@1 225 ClassSymbol sym = (ClassSymbol) typeElem;
duke@1 226 Type outer = (Type) enclosing;
duke@1 227
duke@1 228 if (outer.tsym != sym.owner.enclClass())
duke@1 229 throw new IllegalArgumentException(enclosing.toString());
duke@1 230 if (!outer.isParameterized())
duke@1 231 return getDeclaredType(typeElem, typeArgs);
duke@1 232
duke@1 233 return getDeclaredType0(outer, sym, typeArgs);
duke@1 234 }
duke@1 235 // where
duke@1 236 private DeclaredType getDeclaredType0(Type outer,
duke@1 237 ClassSymbol sym,
duke@1 238 TypeMirror... typeArgs) {
duke@1 239 if (typeArgs.length != sym.type.getTypeArguments().length())
duke@1 240 throw new IllegalArgumentException(
duke@1 241 "Incorrect number of type arguments");
duke@1 242
duke@1 243 ListBuffer<Type> targs = new ListBuffer<Type>();
duke@1 244 for (TypeMirror t : typeArgs) {
duke@1 245 if (!(t instanceof ReferenceType || t instanceof WildcardType))
duke@1 246 throw new IllegalArgumentException(t.toString());
duke@1 247 targs.append((Type) t);
duke@1 248 }
duke@1 249 // TODO: Would like a way to check that type args match formals.
duke@1 250
duke@1 251 return (DeclaredType) new Type.ClassType(outer, targs.toList(), sym);
duke@1 252 }
duke@1 253
duke@1 254 /**
duke@1 255 * Returns the type of an element when that element is viewed as
duke@1 256 * a member of, or otherwise directly contained by, a given type.
duke@1 257 * For example,
duke@1 258 * when viewed as a member of the parameterized type {@code Set<String>},
duke@1 259 * the {@code Set.add} method is an {@code ExecutableType}
duke@1 260 * whose parameter is of type {@code String}.
duke@1 261 *
duke@1 262 * @param containing the containing type
duke@1 263 * @param element the element
duke@1 264 * @return the type of the element as viewed from the containing type
duke@1 265 * @throws IllegalArgumentException if the element is not a valid one
duke@1 266 * for the given type
duke@1 267 */
duke@1 268 public TypeMirror asMemberOf(DeclaredType containing, Element element) {
duke@1 269 Type site = (Type)containing;
duke@1 270 Symbol sym = (Symbol)element;
duke@1 271 if (types.asSuper(site, sym.getEnclosingElement()) == null)
duke@1 272 throw new IllegalArgumentException(sym + "@" + site);
duke@1 273 return types.memberType(site, sym);
duke@1 274 }
duke@1 275
duke@1 276
duke@1 277 private static final Set<TypeKind> EXEC_OR_PKG =
duke@1 278 EnumSet.of(TypeKind.EXECUTABLE, TypeKind.PACKAGE);
duke@1 279
duke@1 280 /**
duke@1 281 * Throws an IllegalArgumentException if a type's kind is one of a set.
duke@1 282 */
duke@1 283 private void validateTypeNotIn(TypeMirror t, Set<TypeKind> invalidKinds) {
duke@1 284 if (invalidKinds.contains(t.getKind()))
duke@1 285 throw new IllegalArgumentException(t.toString());
duke@1 286 }
duke@1 287
duke@1 288 /**
duke@1 289 * Returns an object cast to the specified type.
duke@1 290 * @throws NullPointerException if the object is {@code null}
duke@1 291 * @throws IllegalArgumentException if the object is of the wrong type
duke@1 292 */
duke@1 293 private static <T> T cast(Class<T> clazz, Object o) {
duke@1 294 if (! clazz.isInstance(o))
duke@1 295 throw new IllegalArgumentException(o.toString());
duke@1 296 return clazz.cast(o);
duke@1 297 }
duke@1 298 }

mercurial