aoqi@0: /* aoqi@0: * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.tools.javac.model; aoqi@0: aoqi@0: import java.util.Collections; aoqi@0: import java.util.EnumSet; aoqi@0: import java.util.LinkedHashSet; aoqi@0: import java.util.List; aoqi@0: import java.util.Set; aoqi@0: aoqi@0: import javax.lang.model.element.*; aoqi@0: import javax.lang.model.type.*; aoqi@0: aoqi@0: import com.sun.tools.javac.code.*; aoqi@0: import com.sun.tools.javac.code.Symbol.*; aoqi@0: import com.sun.tools.javac.util.*; aoqi@0: aoqi@0: /** aoqi@0: * Utility methods for operating on types. aoqi@0: * aoqi@0: *

This is NOT part of any supported API. aoqi@0: * If you write code that depends on this, you do so at your own aoqi@0: * risk. This code and its internal interfaces are subject to change aoqi@0: * or deletion without notice.

aoqi@0: */ aoqi@0: public class JavacTypes implements javax.lang.model.util.Types { aoqi@0: aoqi@0: private Symtab syms; aoqi@0: private Types types; aoqi@0: aoqi@0: public static JavacTypes instance(Context context) { aoqi@0: JavacTypes instance = context.get(JavacTypes.class); aoqi@0: if (instance == null) aoqi@0: instance = new JavacTypes(context); aoqi@0: return instance; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Public for use only by JavacProcessingEnvironment aoqi@0: */ aoqi@0: protected JavacTypes(Context context) { aoqi@0: setContext(context); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Use a new context. May be called from outside to update aoqi@0: * internal state for a new annotation-processing round. aoqi@0: */ aoqi@0: public void setContext(Context context) { aoqi@0: context.put(JavacTypes.class, this); aoqi@0: syms = Symtab.instance(context); aoqi@0: types = Types.instance(context); aoqi@0: } aoqi@0: aoqi@0: public Element asElement(TypeMirror t) { aoqi@0: switch (t.getKind()) { aoqi@0: case DECLARED: aoqi@0: case INTERSECTION: aoqi@0: case ERROR: aoqi@0: case TYPEVAR: aoqi@0: Type type = cast(Type.class, t); aoqi@0: return type.asElement(); aoqi@0: default: aoqi@0: return null; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public boolean isSameType(TypeMirror t1, TypeMirror t2) { aoqi@0: return types.isSameType((Type) t1, (Type) t2); aoqi@0: } aoqi@0: aoqi@0: public boolean isSubtype(TypeMirror t1, TypeMirror t2) { aoqi@0: validateTypeNotIn(t1, EXEC_OR_PKG); aoqi@0: validateTypeNotIn(t2, EXEC_OR_PKG); aoqi@0: return types.isSubtype((Type) t1, (Type) t2); aoqi@0: } aoqi@0: aoqi@0: public boolean isAssignable(TypeMirror t1, TypeMirror t2) { aoqi@0: validateTypeNotIn(t1, EXEC_OR_PKG); aoqi@0: validateTypeNotIn(t2, EXEC_OR_PKG); aoqi@0: return types.isAssignable((Type) t1, (Type) t2); aoqi@0: } aoqi@0: aoqi@0: public boolean contains(TypeMirror t1, TypeMirror t2) { aoqi@0: validateTypeNotIn(t1, EXEC_OR_PKG); aoqi@0: validateTypeNotIn(t2, EXEC_OR_PKG); aoqi@0: return types.containsType((Type) t1, (Type) t2); aoqi@0: } aoqi@0: aoqi@0: public boolean isSubsignature(ExecutableType m1, ExecutableType m2) { aoqi@0: return types.isSubSignature((Type) m1, (Type) m2); aoqi@0: } aoqi@0: aoqi@0: public List directSupertypes(TypeMirror t) { aoqi@0: validateTypeNotIn(t, EXEC_OR_PKG); aoqi@0: return types.directSupertypes((Type) t); aoqi@0: } aoqi@0: aoqi@0: public TypeMirror erasure(TypeMirror t) { aoqi@0: if (t.getKind() == TypeKind.PACKAGE) aoqi@0: throw new IllegalArgumentException(t.toString()); aoqi@0: return types.erasure((Type) t); aoqi@0: } aoqi@0: aoqi@0: public TypeElement boxedClass(PrimitiveType p) { aoqi@0: return types.boxedClass((Type) p); aoqi@0: } aoqi@0: aoqi@0: public PrimitiveType unboxedType(TypeMirror t) { aoqi@0: if (t.getKind() != TypeKind.DECLARED) aoqi@0: throw new IllegalArgumentException(t.toString()); aoqi@0: Type unboxed = types.unboxedType((Type) t); aoqi@0: if (! unboxed.isPrimitive()) // only true primitives, not void aoqi@0: throw new IllegalArgumentException(t.toString()); aoqi@0: return (PrimitiveType)unboxed; aoqi@0: } aoqi@0: aoqi@0: public TypeMirror capture(TypeMirror t) { aoqi@0: validateTypeNotIn(t, EXEC_OR_PKG); aoqi@0: return types.capture((Type) t); aoqi@0: } aoqi@0: aoqi@0: public PrimitiveType getPrimitiveType(TypeKind kind) { aoqi@0: switch (kind) { aoqi@0: case BOOLEAN: return syms.booleanType; aoqi@0: case BYTE: return syms.byteType; aoqi@0: case SHORT: return syms.shortType; aoqi@0: case INT: return syms.intType; aoqi@0: case LONG: return syms.longType; aoqi@0: case CHAR: return syms.charType; aoqi@0: case FLOAT: return syms.floatType; aoqi@0: case DOUBLE: return syms.doubleType; aoqi@0: default: aoqi@0: throw new IllegalArgumentException("Not a primitive type: " + kind); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public NullType getNullType() { aoqi@0: return (NullType) syms.botType; aoqi@0: } aoqi@0: aoqi@0: public NoType getNoType(TypeKind kind) { aoqi@0: switch (kind) { aoqi@0: case VOID: return syms.voidType; aoqi@0: case NONE: return Type.noType; aoqi@0: default: aoqi@0: throw new IllegalArgumentException(kind.toString()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public ArrayType getArrayType(TypeMirror componentType) { aoqi@0: switch (componentType.getKind()) { aoqi@0: case VOID: aoqi@0: case EXECUTABLE: aoqi@0: case WILDCARD: // heh! aoqi@0: case PACKAGE: aoqi@0: throw new IllegalArgumentException(componentType.toString()); aoqi@0: } aoqi@0: return new Type.ArrayType((Type) componentType, syms.arrayClass); aoqi@0: } aoqi@0: aoqi@0: public WildcardType getWildcardType(TypeMirror extendsBound, aoqi@0: TypeMirror superBound) { aoqi@0: BoundKind bkind; aoqi@0: Type bound; aoqi@0: if (extendsBound == null && superBound == null) { aoqi@0: bkind = BoundKind.UNBOUND; aoqi@0: bound = syms.objectType; aoqi@0: } else if (superBound == null) { aoqi@0: bkind = BoundKind.EXTENDS; aoqi@0: bound = (Type) extendsBound; aoqi@0: } else if (extendsBound == null) { aoqi@0: bkind = BoundKind.SUPER; aoqi@0: bound = (Type) superBound; aoqi@0: } else { aoqi@0: throw new IllegalArgumentException( aoqi@0: "Extends and super bounds cannot both be provided"); aoqi@0: } aoqi@0: switch (bound.getKind()) { aoqi@0: case ARRAY: aoqi@0: case DECLARED: aoqi@0: case ERROR: aoqi@0: case TYPEVAR: aoqi@0: return new Type.WildcardType(bound, bkind, syms.boundClass); aoqi@0: default: aoqi@0: throw new IllegalArgumentException(bound.toString()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public DeclaredType getDeclaredType(TypeElement typeElem, aoqi@0: TypeMirror... typeArgs) { aoqi@0: ClassSymbol sym = (ClassSymbol) typeElem; aoqi@0: aoqi@0: if (typeArgs.length == 0) aoqi@0: return (DeclaredType) sym.erasure(types); aoqi@0: if (sym.type.getEnclosingType().isParameterized()) aoqi@0: throw new IllegalArgumentException(sym.toString()); aoqi@0: aoqi@0: return getDeclaredType0(sym.type.getEnclosingType(), sym, typeArgs); aoqi@0: } aoqi@0: aoqi@0: public DeclaredType getDeclaredType(DeclaredType enclosing, aoqi@0: TypeElement typeElem, aoqi@0: TypeMirror... typeArgs) { aoqi@0: if (enclosing == null) aoqi@0: return getDeclaredType(typeElem, typeArgs); aoqi@0: aoqi@0: ClassSymbol sym = (ClassSymbol) typeElem; aoqi@0: Type outer = (Type) enclosing; aoqi@0: aoqi@0: if (outer.tsym != sym.owner.enclClass()) aoqi@0: throw new IllegalArgumentException(enclosing.toString()); aoqi@0: if (!outer.isParameterized()) aoqi@0: return getDeclaredType(typeElem, typeArgs); aoqi@0: aoqi@0: return getDeclaredType0(outer, sym, typeArgs); aoqi@0: } aoqi@0: // where aoqi@0: private DeclaredType getDeclaredType0(Type outer, aoqi@0: ClassSymbol sym, aoqi@0: TypeMirror... typeArgs) { aoqi@0: if (typeArgs.length != sym.type.getTypeArguments().length()) aoqi@0: throw new IllegalArgumentException( aoqi@0: "Incorrect number of type arguments"); aoqi@0: aoqi@0: ListBuffer targs = new ListBuffer(); aoqi@0: for (TypeMirror t : typeArgs) { aoqi@0: if (!(t instanceof ReferenceType || t instanceof WildcardType)) aoqi@0: throw new IllegalArgumentException(t.toString()); aoqi@0: targs.append((Type) t); aoqi@0: } aoqi@0: // TODO: Would like a way to check that type args match formals. aoqi@0: aoqi@0: return (DeclaredType) new Type.ClassType(outer, targs.toList(), sym); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns the type of an element when that element is viewed as aoqi@0: * a member of, or otherwise directly contained by, a given type. aoqi@0: * For example, aoqi@0: * when viewed as a member of the parameterized type {@code Set}, aoqi@0: * the {@code Set.add} method is an {@code ExecutableType} aoqi@0: * whose parameter is of type {@code String}. aoqi@0: * aoqi@0: * @param containing the containing type aoqi@0: * @param element the element aoqi@0: * @return the type of the element as viewed from the containing type aoqi@0: * @throws IllegalArgumentException if the element is not a valid one aoqi@0: * for the given type aoqi@0: */ aoqi@0: public TypeMirror asMemberOf(DeclaredType containing, Element element) { aoqi@0: Type site = (Type)containing; aoqi@0: Symbol sym = (Symbol)element; aoqi@0: if (types.asSuper(site, sym.getEnclosingElement()) == null) aoqi@0: throw new IllegalArgumentException(sym + "@" + site); aoqi@0: return types.memberType(site, sym); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: private static final Set EXEC_OR_PKG = aoqi@0: EnumSet.of(TypeKind.EXECUTABLE, TypeKind.PACKAGE); aoqi@0: aoqi@0: /** aoqi@0: * Throws an IllegalArgumentException if a type's kind is one of a set. aoqi@0: */ aoqi@0: private void validateTypeNotIn(TypeMirror t, Set invalidKinds) { aoqi@0: if (invalidKinds.contains(t.getKind())) aoqi@0: throw new IllegalArgumentException(t.toString()); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns an object cast to the specified type. aoqi@0: * @throws NullPointerException if the object is {@code null} aoqi@0: * @throws IllegalArgumentException if the object is of the wrong type aoqi@0: */ aoqi@0: private static T cast(Class clazz, Object o) { aoqi@0: if (! clazz.isInstance(o)) aoqi@0: throw new IllegalArgumentException(o.toString()); aoqi@0: return clazz.cast(o); aoqi@0: } aoqi@0: aoqi@0: public Set getOverriddenMethods(Element elem) { aoqi@0: if (elem.getKind() != ElementKind.METHOD aoqi@0: || elem.getModifiers().contains(Modifier.STATIC) aoqi@0: || elem.getModifiers().contains(Modifier.PRIVATE)) aoqi@0: return Collections.emptySet(); aoqi@0: aoqi@0: if (!(elem instanceof MethodSymbol)) aoqi@0: throw new IllegalArgumentException(); aoqi@0: aoqi@0: MethodSymbol m = (MethodSymbol) elem; aoqi@0: ClassSymbol origin = (ClassSymbol) m.owner; aoqi@0: aoqi@0: Set results = new LinkedHashSet(); aoqi@0: for (Type t : types.closure(origin.type)) { aoqi@0: if (t != origin.type) { aoqi@0: ClassSymbol c = (ClassSymbol) t.tsym; aoqi@0: for (Scope.Entry e = c.members().lookup(m.name); e.scope != null; e = e.next()) { aoqi@0: if (e.sym.kind == Kinds.MTH && m.overrides(e.sym, origin, types, true)) { aoqi@0: results.add((MethodSymbol) e.sym); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: return results; aoqi@0: } aoqi@0: }