Wed, 23 Jan 2013 13:27:24 -0800
8006775: JSR 308: Compiler changes in JDK8
Reviewed-by: jjg
Contributed-by: mernst@cs.washington.edu, wmdietl@cs.washington.edu, mpapi@csail.mit.edu, mahmood@notnoop.com
1 /*
2 * Copyright (c) 2005, 2013, 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.lang.annotation.Annotation;
29 import java.util.Collections;
30 import java.util.EnumSet;
31 import java.util.LinkedHashSet;
32 import java.util.List;
33 import java.util.Set;
35 import javax.lang.model.element.*;
36 import javax.lang.model.type.*;
38 import com.sun.tools.javac.code.*;
39 import com.sun.tools.javac.code.Symbol.*;
40 import com.sun.tools.javac.util.*;
42 /**
43 * Utility methods for operating on types.
44 *
45 * <p><b>This is NOT part of any supported API.
46 * If you write code that depends on this, you do so at your own
47 * risk. This code and its internal interfaces are subject to change
48 * or deletion without notice.</b></p>
49 */
50 public class JavacTypes implements javax.lang.model.util.Types {
52 private Symtab syms;
53 private Types types;
55 public static JavacTypes instance(Context context) {
56 JavacTypes instance = context.get(JavacTypes.class);
57 if (instance == null)
58 instance = new JavacTypes(context);
59 return instance;
60 }
62 /**
63 * Public for use only by JavacProcessingEnvironment
64 */
65 protected JavacTypes(Context context) {
66 setContext(context);
67 }
69 /**
70 * Use a new context. May be called from outside to update
71 * internal state for a new annotation-processing round.
72 */
73 public void setContext(Context context) {
74 context.put(JavacTypes.class, this);
75 syms = Symtab.instance(context);
76 types = Types.instance(context);
77 }
79 public Element asElement(TypeMirror t) {
80 switch (t.getKind()) {
81 case DECLARED:
82 case INTERSECTION:
83 case ERROR:
84 case TYPEVAR:
85 Type type = cast(Type.class, t);
86 return type.asElement();
87 default:
88 return null;
89 }
90 }
92 public boolean isSameType(TypeMirror t1, TypeMirror t2) {
93 return types.isSameType((Type) t1, (Type) t2);
94 }
96 public boolean isSubtype(TypeMirror t1, TypeMirror t2) {
97 validateTypeNotIn(t1, EXEC_OR_PKG);
98 validateTypeNotIn(t2, EXEC_OR_PKG);
99 return types.isSubtype((Type) t1, (Type) t2);
100 }
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 }
108 public boolean contains(TypeMirror t1, TypeMirror t2) {
109 validateTypeNotIn(t1, EXEC_OR_PKG);
110 validateTypeNotIn(t2, EXEC_OR_PKG);
111 return types.containsType((Type) t1, (Type) t2);
112 }
114 public boolean isSubsignature(ExecutableType m1, ExecutableType m2) {
115 return types.isSubSignature((Type) m1, (Type) m2);
116 }
118 public List<Type> directSupertypes(TypeMirror t) {
119 validateTypeNotIn(t, EXEC_OR_PKG);
120 Type type = (Type) t;
121 Type sup = types.supertype(type);
122 return (sup == Type.noType || sup == type || sup == null)
123 ? types.interfaces(type)
124 : types.interfaces(type).prepend(sup);
125 }
127 public TypeMirror erasure(TypeMirror t) {
128 if (t.getKind() == TypeKind.PACKAGE)
129 throw new IllegalArgumentException(t.toString());
130 return types.erasure((Type) t);
131 }
133 public TypeElement boxedClass(PrimitiveType p) {
134 return types.boxedClass((Type) p);
135 }
137 public PrimitiveType unboxedType(TypeMirror t) {
138 if (t.getKind() != TypeKind.DECLARED)
139 throw new IllegalArgumentException(t.toString());
140 Type unboxed = types.unboxedType((Type) t);
141 if (! unboxed.isPrimitive()) // only true primitives, not void
142 throw new IllegalArgumentException(t.toString());
143 return unboxed;
144 }
146 public TypeMirror capture(TypeMirror t) {
147 validateTypeNotIn(t, EXEC_OR_PKG);
148 return types.capture((Type) t);
149 }
151 public PrimitiveType getPrimitiveType(TypeKind kind) {
152 switch (kind) {
153 case BOOLEAN: return syms.booleanType;
154 case BYTE: return syms.byteType;
155 case SHORT: return syms.shortType;
156 case INT: return syms.intType;
157 case LONG: return syms.longType;
158 case CHAR: return syms.charType;
159 case FLOAT: return syms.floatType;
160 case DOUBLE: return syms.doubleType;
161 default:
162 throw new IllegalArgumentException("Not a primitive type: " + kind);
163 }
164 }
166 public NullType getNullType() {
167 return (NullType) syms.botType;
168 }
170 public NoType getNoType(TypeKind kind) {
171 switch (kind) {
172 case VOID: return syms.voidType;
173 case NONE: return Type.noType;
174 default:
175 throw new IllegalArgumentException(kind.toString());
176 }
177 }
179 public ArrayType getArrayType(TypeMirror componentType) {
180 switch (componentType.getKind()) {
181 case VOID:
182 case EXECUTABLE:
183 case WILDCARD: // heh!
184 case PACKAGE:
185 throw new IllegalArgumentException(componentType.toString());
186 }
187 return new Type.ArrayType((Type) componentType, syms.arrayClass);
188 }
190 public WildcardType getWildcardType(TypeMirror extendsBound,
191 TypeMirror superBound) {
192 BoundKind bkind;
193 Type bound;
194 if (extendsBound == null && superBound == null) {
195 bkind = BoundKind.UNBOUND;
196 bound = syms.objectType;
197 } else if (superBound == null) {
198 bkind = BoundKind.EXTENDS;
199 bound = (Type) extendsBound;
200 } else if (extendsBound == null) {
201 bkind = BoundKind.SUPER;
202 bound = (Type) superBound;
203 } else {
204 throw new IllegalArgumentException(
205 "Extends and super bounds cannot both be provided");
206 }
207 switch (bound.getKind()) {
208 case ARRAY:
209 case DECLARED:
210 case ERROR:
211 case TYPEVAR:
212 return new Type.WildcardType(bound, bkind, syms.boundClass);
213 default:
214 throw new IllegalArgumentException(bound.toString());
215 }
216 }
218 public DeclaredType getDeclaredType(TypeElement typeElem,
219 TypeMirror... typeArgs) {
220 ClassSymbol sym = (ClassSymbol) typeElem;
222 if (typeArgs.length == 0)
223 return (DeclaredType) sym.erasure(types);
224 if (sym.type.getEnclosingType().isParameterized())
225 throw new IllegalArgumentException(sym.toString());
227 return getDeclaredType0(sym.type.getEnclosingType(), sym, typeArgs);
228 }
230 public DeclaredType getDeclaredType(DeclaredType enclosing,
231 TypeElement typeElem,
232 TypeMirror... typeArgs) {
233 if (enclosing == null)
234 return getDeclaredType(typeElem, typeArgs);
236 ClassSymbol sym = (ClassSymbol) typeElem;
237 Type outer = (Type) enclosing;
239 if (outer.tsym != sym.owner.enclClass())
240 throw new IllegalArgumentException(enclosing.toString());
241 if (!outer.isParameterized())
242 return getDeclaredType(typeElem, typeArgs);
244 return getDeclaredType0(outer, sym, typeArgs);
245 }
246 // where
247 private DeclaredType getDeclaredType0(Type outer,
248 ClassSymbol sym,
249 TypeMirror... typeArgs) {
250 if (typeArgs.length != sym.type.getTypeArguments().length())
251 throw new IllegalArgumentException(
252 "Incorrect number of type arguments");
254 ListBuffer<Type> targs = new ListBuffer<Type>();
255 for (TypeMirror t : typeArgs) {
256 if (!(t instanceof ReferenceType || t instanceof WildcardType))
257 throw new IllegalArgumentException(t.toString());
258 targs.append((Type) t);
259 }
260 // TODO: Would like a way to check that type args match formals.
262 return (DeclaredType) new Type.ClassType(outer, targs.toList(), sym);
263 }
265 /**
266 * Returns the type of an element when that element is viewed as
267 * a member of, or otherwise directly contained by, a given type.
268 * For example,
269 * when viewed as a member of the parameterized type {@code Set<String>},
270 * the {@code Set.add} method is an {@code ExecutableType}
271 * whose parameter is of type {@code String}.
272 *
273 * @param containing the containing type
274 * @param element the element
275 * @return the type of the element as viewed from the containing type
276 * @throws IllegalArgumentException if the element is not a valid one
277 * for the given type
278 */
279 public TypeMirror asMemberOf(DeclaredType containing, Element element) {
280 Type site = (Type)containing;
281 Symbol sym = (Symbol)element;
282 if (types.asSuper(site, sym.getEnclosingElement()) == null)
283 throw new IllegalArgumentException(sym + "@" + site);
284 return types.memberType(site, sym);
285 }
288 private static final Set<TypeKind> EXEC_OR_PKG =
289 EnumSet.of(TypeKind.EXECUTABLE, TypeKind.PACKAGE);
291 /**
292 * Throws an IllegalArgumentException if a type's kind is one of a set.
293 */
294 private void validateTypeNotIn(TypeMirror t, Set<TypeKind> invalidKinds) {
295 if (invalidKinds.contains(t.getKind()))
296 throw new IllegalArgumentException(t.toString());
297 }
299 /**
300 * Returns an object cast to the specified type.
301 * @throws NullPointerException if the object is {@code null}
302 * @throws IllegalArgumentException if the object is of the wrong type
303 */
304 private static <T> T cast(Class<T> clazz, Object o) {
305 if (! clazz.isInstance(o))
306 throw new IllegalArgumentException(o.toString());
307 return clazz.cast(o);
308 }
310 public Set<MethodSymbol> getOverriddenMethods(Element elem) {
311 if (elem.getKind() != ElementKind.METHOD
312 || elem.getModifiers().contains(Modifier.STATIC)
313 || elem.getModifiers().contains(Modifier.PRIVATE))
314 return Collections.emptySet();
316 if (!(elem instanceof MethodSymbol))
317 throw new IllegalArgumentException();
319 MethodSymbol m = (MethodSymbol) elem;
320 ClassSymbol origin = (ClassSymbol) m.owner;
322 Set<MethodSymbol> results = new LinkedHashSet<MethodSymbol>();
323 for (Type t : types.closure(origin.type)) {
324 if (t != origin.type) {
325 ClassSymbol c = (ClassSymbol) t.tsym;
326 for (Scope.Entry e = c.members().lookup(m.name); e.scope != null; e = e.next()) {
327 if (e.sym.kind == Kinds.MTH && m.overrides(e.sym, origin, types, true)) {
328 results.add((MethodSymbol) e.sym);
329 }
330 }
331 }
332 }
334 return results;
335 }
337 public List<? extends AnnotationMirror> typeAnnotationsOf(TypeMirror type) {
338 // TODO: these methods can be removed.
339 return null; // ((Type)type).typeAnnotations;
340 }
342 public <A extends Annotation> A typeAnnotationOf(TypeMirror type,
343 Class<A> annotationType) {
344 // TODO: these methods can be removed.
345 return null; // JavacElements.getAnnotation(((Type)type).typeAnnotations, annotationType);
346 }
348 public TypeMirror receiverTypeOf(ExecutableType type) {
349 return ((Type)type).asMethodType().recvtype;
350 }
352 /*
353 public <A extends Annotation> A receiverTypeAnnotationOf(
354 ExecutableType type, Class<A> annotationType) {
355 return JavacElements.getAnnotation(
356 ((Type)type).asMethodType().receiverTypeAnnotations,
357 annotationType);
358 }*/
360 }