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

Sun, 17 Feb 2013 16:44:55 -0500

author
dholmes
date
Sun, 17 Feb 2013 16:44:55 -0500
changeset 1571
af8417e590f4
parent 1521
71f35e4b93a5
child 1644
40adaf938847
permissions
-rw-r--r--

Merge

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

mercurial