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