src/share/classes/com/sun/tools/javac/model/JavacTypes.java

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 2050
09301757bb32
parent 0
959103a6100f
permissions
-rw-r--r--

merge

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

mercurial