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

Wed, 17 Jul 2013 14:11:41 +0100

author
mcimadamore
date
Wed, 17 Jul 2013 14:11:41 +0100
changeset 1898
a204cf7aab7e
parent 1853
831467c4c6a7
child 2050
09301757bb32
permissions
-rw-r--r--

8012238: Nested method capture and inference
8008200: java/lang/Class/asSubclass/BasicUnit.java fails to compile
Summary: Inference support should be more flexible w.r.t. nested method calls returning captured types
Reviewed-by: jjg, vromero

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

mercurial