Wed, 20 Jun 2012 13:23:26 -0700
7174143: encapsulate doc comment table
Reviewed-by: ksrini, mcimadamore
duke@1 | 1 | /* |
jjg@928 | 2 | * Copyright (c) 2005, 2011, 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) { |
jjg@988 | 75 | switch (t.getKind()) { |
jjg@988 | 76 | case DECLARED: |
jjg@988 | 77 | case ERROR: |
jjg@988 | 78 | case TYPEVAR: |
jjg@988 | 79 | Type type = cast(Type.class, t); |
jjg@928 | 80 | return type.asElement(); |
jjg@928 | 81 | default: |
jjg@928 | 82 | return null; |
jjg@928 | 83 | } |
duke@1 | 84 | } |
duke@1 | 85 | |
duke@1 | 86 | public boolean isSameType(TypeMirror t1, TypeMirror t2) { |
duke@1 | 87 | return types.isSameType((Type) t1, (Type) t2); |
duke@1 | 88 | } |
duke@1 | 89 | |
duke@1 | 90 | public boolean isSubtype(TypeMirror t1, TypeMirror t2) { |
duke@1 | 91 | validateTypeNotIn(t1, EXEC_OR_PKG); |
duke@1 | 92 | validateTypeNotIn(t2, EXEC_OR_PKG); |
duke@1 | 93 | return types.isSubtype((Type) t1, (Type) t2); |
duke@1 | 94 | } |
duke@1 | 95 | |
duke@1 | 96 | public boolean isAssignable(TypeMirror t1, TypeMirror t2) { |
duke@1 | 97 | validateTypeNotIn(t1, EXEC_OR_PKG); |
duke@1 | 98 | validateTypeNotIn(t2, EXEC_OR_PKG); |
duke@1 | 99 | return types.isAssignable((Type) t1, (Type) t2); |
duke@1 | 100 | } |
duke@1 | 101 | |
duke@1 | 102 | public boolean contains(TypeMirror t1, TypeMirror t2) { |
duke@1 | 103 | validateTypeNotIn(t1, EXEC_OR_PKG); |
duke@1 | 104 | validateTypeNotIn(t2, EXEC_OR_PKG); |
mcimadamore@675 | 105 | return types.containsType((Type) t1, (Type) t2); |
duke@1 | 106 | } |
duke@1 | 107 | |
duke@1 | 108 | public boolean isSubsignature(ExecutableType m1, ExecutableType m2) { |
duke@1 | 109 | return types.isSubSignature((Type) m1, (Type) m2); |
duke@1 | 110 | } |
duke@1 | 111 | |
duke@1 | 112 | public List<Type> directSupertypes(TypeMirror t) { |
duke@1 | 113 | validateTypeNotIn(t, EXEC_OR_PKG); |
duke@1 | 114 | Type type = (Type) t; |
duke@1 | 115 | Type sup = types.supertype(type); |
duke@1 | 116 | return (sup == Type.noType || sup == type || sup == null) |
duke@1 | 117 | ? types.interfaces(type) |
duke@1 | 118 | : types.interfaces(type).prepend(sup); |
duke@1 | 119 | } |
duke@1 | 120 | |
duke@1 | 121 | public TypeMirror erasure(TypeMirror t) { |
duke@1 | 122 | if (t.getKind() == TypeKind.PACKAGE) |
duke@1 | 123 | throw new IllegalArgumentException(t.toString()); |
duke@1 | 124 | return types.erasure((Type) t); |
duke@1 | 125 | } |
duke@1 | 126 | |
duke@1 | 127 | public TypeElement boxedClass(PrimitiveType p) { |
duke@1 | 128 | return types.boxedClass((Type) p); |
duke@1 | 129 | } |
duke@1 | 130 | |
duke@1 | 131 | public PrimitiveType unboxedType(TypeMirror t) { |
duke@1 | 132 | if (t.getKind() != TypeKind.DECLARED) |
duke@1 | 133 | throw new IllegalArgumentException(t.toString()); |
duke@1 | 134 | Type unboxed = types.unboxedType((Type) t); |
duke@1 | 135 | if (! unboxed.isPrimitive()) // only true primitives, not void |
duke@1 | 136 | throw new IllegalArgumentException(t.toString()); |
duke@1 | 137 | return unboxed; |
duke@1 | 138 | } |
duke@1 | 139 | |
duke@1 | 140 | public TypeMirror capture(TypeMirror t) { |
duke@1 | 141 | validateTypeNotIn(t, EXEC_OR_PKG); |
duke@1 | 142 | return types.capture((Type) t); |
duke@1 | 143 | } |
duke@1 | 144 | |
duke@1 | 145 | public PrimitiveType getPrimitiveType(TypeKind kind) { |
duke@1 | 146 | switch (kind) { |
duke@1 | 147 | case BOOLEAN: return syms.booleanType; |
duke@1 | 148 | case BYTE: return syms.byteType; |
duke@1 | 149 | case SHORT: return syms.shortType; |
duke@1 | 150 | case INT: return syms.intType; |
duke@1 | 151 | case LONG: return syms.longType; |
duke@1 | 152 | case CHAR: return syms.charType; |
duke@1 | 153 | case FLOAT: return syms.floatType; |
duke@1 | 154 | case DOUBLE: return syms.doubleType; |
duke@1 | 155 | default: |
duke@1 | 156 | throw new IllegalArgumentException("Not a primitive type: " + kind); |
duke@1 | 157 | } |
duke@1 | 158 | } |
duke@1 | 159 | |
duke@1 | 160 | public NullType getNullType() { |
duke@1 | 161 | return (NullType) syms.botType; |
duke@1 | 162 | } |
duke@1 | 163 | |
duke@1 | 164 | public NoType getNoType(TypeKind kind) { |
duke@1 | 165 | switch (kind) { |
duke@1 | 166 | case VOID: return syms.voidType; |
duke@1 | 167 | case NONE: return Type.noType; |
duke@1 | 168 | default: |
duke@1 | 169 | throw new IllegalArgumentException(kind.toString()); |
duke@1 | 170 | } |
duke@1 | 171 | } |
duke@1 | 172 | |
duke@1 | 173 | public ArrayType getArrayType(TypeMirror componentType) { |
duke@1 | 174 | switch (componentType.getKind()) { |
duke@1 | 175 | case VOID: |
duke@1 | 176 | case EXECUTABLE: |
duke@1 | 177 | case WILDCARD: // heh! |
duke@1 | 178 | case PACKAGE: |
duke@1 | 179 | throw new IllegalArgumentException(componentType.toString()); |
duke@1 | 180 | } |
duke@1 | 181 | return new Type.ArrayType((Type) componentType, syms.arrayClass); |
duke@1 | 182 | } |
duke@1 | 183 | |
duke@1 | 184 | public WildcardType getWildcardType(TypeMirror extendsBound, |
duke@1 | 185 | TypeMirror superBound) { |
duke@1 | 186 | BoundKind bkind; |
duke@1 | 187 | Type bound; |
duke@1 | 188 | if (extendsBound == null && superBound == null) { |
duke@1 | 189 | bkind = BoundKind.UNBOUND; |
duke@1 | 190 | bound = syms.objectType; |
duke@1 | 191 | } else if (superBound == null) { |
duke@1 | 192 | bkind = BoundKind.EXTENDS; |
duke@1 | 193 | bound = (Type) extendsBound; |
duke@1 | 194 | } else if (extendsBound == null) { |
duke@1 | 195 | bkind = BoundKind.SUPER; |
duke@1 | 196 | bound = (Type) superBound; |
duke@1 | 197 | } else { |
duke@1 | 198 | throw new IllegalArgumentException( |
duke@1 | 199 | "Extends and super bounds cannot both be provided"); |
duke@1 | 200 | } |
duke@1 | 201 | switch (bound.getKind()) { |
duke@1 | 202 | case ARRAY: |
duke@1 | 203 | case DECLARED: |
duke@1 | 204 | case ERROR: |
duke@1 | 205 | case TYPEVAR: |
duke@1 | 206 | return new Type.WildcardType(bound, bkind, syms.boundClass); |
duke@1 | 207 | default: |
duke@1 | 208 | throw new IllegalArgumentException(bound.toString()); |
duke@1 | 209 | } |
duke@1 | 210 | } |
duke@1 | 211 | |
duke@1 | 212 | public DeclaredType getDeclaredType(TypeElement typeElem, |
duke@1 | 213 | TypeMirror... typeArgs) { |
duke@1 | 214 | ClassSymbol sym = (ClassSymbol) typeElem; |
duke@1 | 215 | |
duke@1 | 216 | if (typeArgs.length == 0) |
duke@1 | 217 | return (DeclaredType) sym.erasure(types); |
duke@1 | 218 | if (sym.type.getEnclosingType().isParameterized()) |
duke@1 | 219 | throw new IllegalArgumentException(sym.toString()); |
duke@1 | 220 | |
duke@1 | 221 | return getDeclaredType0(sym.type.getEnclosingType(), sym, typeArgs); |
duke@1 | 222 | } |
duke@1 | 223 | |
duke@1 | 224 | public DeclaredType getDeclaredType(DeclaredType enclosing, |
duke@1 | 225 | TypeElement typeElem, |
duke@1 | 226 | TypeMirror... typeArgs) { |
duke@1 | 227 | if (enclosing == null) |
duke@1 | 228 | return getDeclaredType(typeElem, typeArgs); |
duke@1 | 229 | |
duke@1 | 230 | ClassSymbol sym = (ClassSymbol) typeElem; |
duke@1 | 231 | Type outer = (Type) enclosing; |
duke@1 | 232 | |
duke@1 | 233 | if (outer.tsym != sym.owner.enclClass()) |
duke@1 | 234 | throw new IllegalArgumentException(enclosing.toString()); |
duke@1 | 235 | if (!outer.isParameterized()) |
duke@1 | 236 | return getDeclaredType(typeElem, typeArgs); |
duke@1 | 237 | |
duke@1 | 238 | return getDeclaredType0(outer, sym, typeArgs); |
duke@1 | 239 | } |
duke@1 | 240 | // where |
duke@1 | 241 | private DeclaredType getDeclaredType0(Type outer, |
duke@1 | 242 | ClassSymbol sym, |
duke@1 | 243 | TypeMirror... typeArgs) { |
duke@1 | 244 | if (typeArgs.length != sym.type.getTypeArguments().length()) |
duke@1 | 245 | throw new IllegalArgumentException( |
duke@1 | 246 | "Incorrect number of type arguments"); |
duke@1 | 247 | |
duke@1 | 248 | ListBuffer<Type> targs = new ListBuffer<Type>(); |
duke@1 | 249 | for (TypeMirror t : typeArgs) { |
duke@1 | 250 | if (!(t instanceof ReferenceType || t instanceof WildcardType)) |
duke@1 | 251 | throw new IllegalArgumentException(t.toString()); |
duke@1 | 252 | targs.append((Type) t); |
duke@1 | 253 | } |
duke@1 | 254 | // TODO: Would like a way to check that type args match formals. |
duke@1 | 255 | |
duke@1 | 256 | return (DeclaredType) new Type.ClassType(outer, targs.toList(), sym); |
duke@1 | 257 | } |
duke@1 | 258 | |
duke@1 | 259 | /** |
duke@1 | 260 | * Returns the type of an element when that element is viewed as |
duke@1 | 261 | * a member of, or otherwise directly contained by, a given type. |
duke@1 | 262 | * For example, |
duke@1 | 263 | * when viewed as a member of the parameterized type {@code Set<String>}, |
duke@1 | 264 | * the {@code Set.add} method is an {@code ExecutableType} |
duke@1 | 265 | * whose parameter is of type {@code String}. |
duke@1 | 266 | * |
duke@1 | 267 | * @param containing the containing type |
duke@1 | 268 | * @param element the element |
duke@1 | 269 | * @return the type of the element as viewed from the containing type |
duke@1 | 270 | * @throws IllegalArgumentException if the element is not a valid one |
duke@1 | 271 | * for the given type |
duke@1 | 272 | */ |
duke@1 | 273 | public TypeMirror asMemberOf(DeclaredType containing, Element element) { |
duke@1 | 274 | Type site = (Type)containing; |
duke@1 | 275 | Symbol sym = (Symbol)element; |
duke@1 | 276 | if (types.asSuper(site, sym.getEnclosingElement()) == null) |
duke@1 | 277 | throw new IllegalArgumentException(sym + "@" + site); |
duke@1 | 278 | return types.memberType(site, sym); |
duke@1 | 279 | } |
duke@1 | 280 | |
duke@1 | 281 | |
duke@1 | 282 | private static final Set<TypeKind> EXEC_OR_PKG = |
duke@1 | 283 | EnumSet.of(TypeKind.EXECUTABLE, TypeKind.PACKAGE); |
duke@1 | 284 | |
duke@1 | 285 | /** |
duke@1 | 286 | * Throws an IllegalArgumentException if a type's kind is one of a set. |
duke@1 | 287 | */ |
duke@1 | 288 | private void validateTypeNotIn(TypeMirror t, Set<TypeKind> invalidKinds) { |
duke@1 | 289 | if (invalidKinds.contains(t.getKind())) |
duke@1 | 290 | throw new IllegalArgumentException(t.toString()); |
duke@1 | 291 | } |
duke@1 | 292 | |
duke@1 | 293 | /** |
duke@1 | 294 | * Returns an object cast to the specified type. |
duke@1 | 295 | * @throws NullPointerException if the object is {@code null} |
duke@1 | 296 | * @throws IllegalArgumentException if the object is of the wrong type |
duke@1 | 297 | */ |
duke@1 | 298 | private static <T> T cast(Class<T> clazz, Object o) { |
duke@1 | 299 | if (! clazz.isInstance(o)) |
duke@1 | 300 | throw new IllegalArgumentException(o.toString()); |
duke@1 | 301 | return clazz.cast(o); |
duke@1 | 302 | } |
duke@1 | 303 | } |