JavacTypes.java revision 2646:ff1998c1ecab
1/*
2 * Copyright (c) 2005, 2014, 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 */
25
26package com.sun.tools.javac.model;
27
28import java.util.Collections;
29import java.util.EnumSet;
30import java.util.LinkedHashSet;
31import java.util.List;
32import java.util.Set;
33
34import javax.lang.model.element.*;
35import javax.lang.model.type.*;
36
37import com.sun.tools.javac.code.*;
38import com.sun.tools.javac.code.Symbol.*;
39import com.sun.tools.javac.util.*;
40import com.sun.tools.javac.util.DefinedBy.Api;
41
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 */
50public class JavacTypes implements javax.lang.model.util.Types {
51
52    private final Symtab syms;
53    private final Types types;
54
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    }
61
62    protected JavacTypes(Context context) {
63        context.put(JavacTypes.class, this);
64        syms = Symtab.instance(context);
65        types = Types.instance(context);
66    }
67
68    @DefinedBy(Api.LANGUAGE_MODEL)
69    public Element asElement(TypeMirror t) {
70        switch (t.getKind()) {
71            case DECLARED:
72            case INTERSECTION:
73            case ERROR:
74            case TYPEVAR:
75                Type type = cast(Type.class, t);
76                return type.asElement();
77            default:
78                return null;
79        }
80    }
81
82    @DefinedBy(Api.LANGUAGE_MODEL)
83    public boolean isSameType(TypeMirror t1, TypeMirror t2) {
84        return types.isSameType((Type) t1, (Type) t2);
85    }
86
87    @DefinedBy(Api.LANGUAGE_MODEL)
88    public boolean isSubtype(TypeMirror t1, TypeMirror t2) {
89        validateTypeNotIn(t1, EXEC_OR_PKG);
90        validateTypeNotIn(t2, EXEC_OR_PKG);
91        return types.isSubtype((Type) t1, (Type) t2);
92    }
93
94    @DefinedBy(Api.LANGUAGE_MODEL)
95    public boolean isAssignable(TypeMirror t1, TypeMirror t2) {
96        validateTypeNotIn(t1, EXEC_OR_PKG);
97        validateTypeNotIn(t2, EXEC_OR_PKG);
98        return types.isAssignable((Type) t1, (Type) t2);
99    }
100
101    @DefinedBy(Api.LANGUAGE_MODEL)
102    public boolean contains(TypeMirror t1, TypeMirror t2) {
103        validateTypeNotIn(t1, EXEC_OR_PKG);
104        validateTypeNotIn(t2, EXEC_OR_PKG);
105        return types.containsType((Type) t1, (Type) t2);
106    }
107
108    @DefinedBy(Api.LANGUAGE_MODEL)
109    public boolean isSubsignature(ExecutableType m1, ExecutableType m2) {
110        return types.isSubSignature((Type) m1, (Type) m2);
111    }
112
113    @DefinedBy(Api.LANGUAGE_MODEL)
114    public List<Type> directSupertypes(TypeMirror t) {
115        validateTypeNotIn(t, EXEC_OR_PKG);
116        return types.directSupertypes((Type) t);
117    }
118
119    @DefinedBy(Api.LANGUAGE_MODEL)
120    public TypeMirror erasure(TypeMirror t) {
121        if (t.getKind() == TypeKind.PACKAGE)
122            throw new IllegalArgumentException(t.toString());
123        return types.erasure((Type) t);
124    }
125
126    @DefinedBy(Api.LANGUAGE_MODEL)
127    public TypeElement boxedClass(PrimitiveType p) {
128        return types.boxedClass((Type) p);
129    }
130
131    @DefinedBy(Api.LANGUAGE_MODEL)
132    public PrimitiveType unboxedType(TypeMirror t) {
133        if (t.getKind() != TypeKind.DECLARED)
134            throw new IllegalArgumentException(t.toString());
135        Type unboxed = types.unboxedType((Type) t);
136        if (! unboxed.isPrimitive())    // only true primitives, not void
137            throw new IllegalArgumentException(t.toString());
138        return (PrimitiveType)unboxed;
139    }
140
141    @DefinedBy(Api.LANGUAGE_MODEL)
142    public TypeMirror capture(TypeMirror t) {
143        validateTypeNotIn(t, EXEC_OR_PKG);
144        return types.capture((Type) t);
145    }
146
147    @DefinedBy(Api.LANGUAGE_MODEL)
148    public PrimitiveType getPrimitiveType(TypeKind kind) {
149        switch (kind) {
150        case BOOLEAN:   return syms.booleanType;
151        case BYTE:      return syms.byteType;
152        case SHORT:     return syms.shortType;
153        case INT:       return syms.intType;
154        case LONG:      return syms.longType;
155        case CHAR:      return syms.charType;
156        case FLOAT:     return syms.floatType;
157        case DOUBLE:    return syms.doubleType;
158        default:
159            throw new IllegalArgumentException("Not a primitive type: " + kind);
160        }
161    }
162
163    @DefinedBy(Api.LANGUAGE_MODEL)
164    public NullType getNullType() {
165        return (NullType) syms.botType;
166    }
167
168    @DefinedBy(Api.LANGUAGE_MODEL)
169    public NoType getNoType(TypeKind kind) {
170        switch (kind) {
171        case VOID:      return syms.voidType;
172        case NONE:      return Type.noType;
173        default:
174            throw new IllegalArgumentException(kind.toString());
175        }
176    }
177
178    @DefinedBy(Api.LANGUAGE_MODEL)
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    }
189
190    @DefinedBy(Api.LANGUAGE_MODEL)
191    public WildcardType getWildcardType(TypeMirror extendsBound,
192                                        TypeMirror superBound) {
193        BoundKind bkind;
194        Type bound;
195        if (extendsBound == null && superBound == null) {
196            bkind = BoundKind.UNBOUND;
197            bound = syms.objectType;
198        } else if (superBound == null) {
199            bkind = BoundKind.EXTENDS;
200            bound = (Type) extendsBound;
201        } else if (extendsBound == null) {
202            bkind = BoundKind.SUPER;
203            bound = (Type) superBound;
204        } else {
205            throw new IllegalArgumentException(
206                    "Extends and super bounds cannot both be provided");
207        }
208        switch (bound.getKind()) {
209        case ARRAY:
210        case DECLARED:
211        case ERROR:
212        case TYPEVAR:
213            return new Type.WildcardType(bound, bkind, syms.boundClass);
214        default:
215            throw new IllegalArgumentException(bound.toString());
216        }
217    }
218
219    @DefinedBy(Api.LANGUAGE_MODEL)
220    public DeclaredType getDeclaredType(TypeElement typeElem,
221                                        TypeMirror... typeArgs) {
222        ClassSymbol sym = (ClassSymbol) typeElem;
223
224        if (typeArgs.length == 0)
225            return (DeclaredType) sym.erasure(types);
226        if (sym.type.getEnclosingType().isParameterized())
227            throw new IllegalArgumentException(sym.toString());
228
229        return getDeclaredType0(sym.type.getEnclosingType(), sym, typeArgs);
230    }
231
232    @DefinedBy(Api.LANGUAGE_MODEL)
233    public DeclaredType getDeclaredType(DeclaredType enclosing,
234                                        TypeElement typeElem,
235                                        TypeMirror... typeArgs) {
236        if (enclosing == null)
237            return getDeclaredType(typeElem, typeArgs);
238
239        ClassSymbol sym = (ClassSymbol) typeElem;
240        Type outer = (Type) enclosing;
241
242        if (outer.tsym != sym.owner.enclClass())
243            throw new IllegalArgumentException(enclosing.toString());
244        if (!outer.isParameterized())
245            return getDeclaredType(typeElem, typeArgs);
246
247        return getDeclaredType0(outer, sym, typeArgs);
248    }
249    // where
250        private DeclaredType getDeclaredType0(Type outer,
251                                              ClassSymbol sym,
252                                              TypeMirror... typeArgs) {
253            if (typeArgs.length != sym.type.getTypeArguments().length())
254                throw new IllegalArgumentException(
255                "Incorrect number of type arguments");
256
257            ListBuffer<Type> targs = new ListBuffer<>();
258            for (TypeMirror t : typeArgs) {
259                if (!(t instanceof ReferenceType || t instanceof WildcardType))
260                    throw new IllegalArgumentException(t.toString());
261                targs.append((Type) t);
262            }
263            // TODO: Would like a way to check that type args match formals.
264
265            return (DeclaredType) new Type.ClassType(outer, targs.toList(), sym);
266        }
267
268    /**
269     * Returns the type of an element when that element is viewed as
270     * a member of, or otherwise directly contained by, a given type.
271     * For example,
272     * when viewed as a member of the parameterized type {@code Set<String>},
273     * the {@code Set.add} method is an {@code ExecutableType}
274     * whose parameter is of type {@code String}.
275     *
276     * @param containing  the containing type
277     * @param element     the element
278     * @return the type of the element as viewed from the containing type
279     * @throws IllegalArgumentException if the element is not a valid one
280     *          for the given type
281     */
282    @DefinedBy(Api.LANGUAGE_MODEL)
283    public TypeMirror asMemberOf(DeclaredType containing, Element element) {
284        Type site = (Type)containing;
285        Symbol sym = (Symbol)element;
286        if (types.asSuper(site, sym.getEnclosingElement()) == null)
287            throw new IllegalArgumentException(sym + "@" + site);
288        return types.memberType(site, sym);
289    }
290
291
292    private static final Set<TypeKind> EXEC_OR_PKG =
293            EnumSet.of(TypeKind.EXECUTABLE, TypeKind.PACKAGE);
294
295    /**
296     * Throws an IllegalArgumentException if a type's kind is one of a set.
297     */
298    private void validateTypeNotIn(TypeMirror t, Set<TypeKind> invalidKinds) {
299        if (invalidKinds.contains(t.getKind()))
300            throw new IllegalArgumentException(t.toString());
301    }
302
303    /**
304     * Returns an object cast to the specified type.
305     * @throws NullPointerException if the object is {@code null}
306     * @throws IllegalArgumentException if the object is of the wrong type
307     */
308    private static <T> T cast(Class<T> clazz, Object o) {
309        if (! clazz.isInstance(o))
310            throw new IllegalArgumentException(o.toString());
311        return clazz.cast(o);
312    }
313
314    public Set<MethodSymbol> getOverriddenMethods(Element elem) {
315        if (elem.getKind() != ElementKind.METHOD
316                || elem.getModifiers().contains(Modifier.STATIC)
317                || elem.getModifiers().contains(Modifier.PRIVATE))
318            return Collections.emptySet();
319
320        if (!(elem instanceof MethodSymbol))
321            throw new IllegalArgumentException();
322
323        MethodSymbol m = (MethodSymbol) elem;
324        ClassSymbol origin = (ClassSymbol) m.owner;
325
326        Set<MethodSymbol> results = new LinkedHashSet<>();
327        for (Type t : types.closure(origin.type)) {
328            if (t != origin.type) {
329                ClassSymbol c = (ClassSymbol) t.tsym;
330                for (Symbol sym : c.members().getSymbolsByName(m.name)) {
331                    if (sym.kind == Kinds.MTH && m.overrides(sym, origin, types, true)) {
332                        results.add((MethodSymbol) sym);
333                    }
334                }
335            }
336        }
337
338        return results;
339    }
340}
341