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

Mon, 26 Mar 2012 15:28:49 +0100

author
mcimadamore
date
Mon, 26 Mar 2012 15:28:49 +0100
changeset 1239
2827076dbf64
parent 1127
ca49d50318dc
child 1280
5c0b3faeb0b0
permissions
-rw-r--r--

7133185: Update 292 overload resolution logic to match JLS
Summary: Re-implement special overload resolution support for method handles according to the JLS SE 7 definition
Reviewed-by: jjg, dlsmith, jrose

duke@1 1 /*
jjg@1127 2 * Copyright (c) 2005, 2011, 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
duke@1 28 import java.lang.annotation.Annotation;
duke@1 29 import java.lang.annotation.Inherited;
duke@1 30 import java.util.Map;
duke@1 31 import javax.lang.model.SourceVersion;
duke@1 32 import javax.lang.model.element.*;
duke@1 33 import javax.lang.model.type.DeclaredType;
duke@1 34 import javax.lang.model.util.Elements;
duke@1 35 import javax.tools.JavaFileObject;
duke@1 36 import com.sun.tools.javac.code.*;
duke@1 37 import com.sun.tools.javac.code.Symbol.*;
duke@1 38 import com.sun.tools.javac.code.TypeTags;
duke@1 39 import com.sun.tools.javac.comp.AttrContext;
duke@1 40 import com.sun.tools.javac.comp.Enter;
duke@1 41 import com.sun.tools.javac.comp.Env;
duke@1 42 import com.sun.tools.javac.main.JavaCompiler;
duke@1 43 import com.sun.tools.javac.processing.PrintingProcessor;
duke@1 44 import com.sun.tools.javac.tree.JCTree;
duke@1 45 import com.sun.tools.javac.tree.JCTree.*;
duke@1 46 import com.sun.tools.javac.tree.TreeInfo;
duke@1 47 import com.sun.tools.javac.tree.TreeScanner;
jjg@113 48 import com.sun.tools.javac.util.*;
duke@1 49 import com.sun.tools.javac.util.Name;
duke@1 50
jjg@1127 51 import static com.sun.tools.javac.tree.JCTree.Tag.*;
duke@1 52 import static javax.lang.model.util.ElementFilter.methodsIn;
duke@1 53
duke@1 54 /**
duke@1 55 * Utility methods for operating on program elements.
duke@1 56 *
jjg@581 57 * <p><b>This is NOT part of any supported API.
duke@1 58 * If you write code that depends on this, you do so at your own
duke@1 59 * risk. This code and its internal interfaces are subject to change
duke@1 60 * or deletion without notice.</b></p>
duke@1 61 */
duke@1 62 public class JavacElements implements Elements {
duke@1 63
duke@1 64 private JavaCompiler javaCompiler;
duke@1 65 private Symtab syms;
jjg@113 66 private Names names;
duke@1 67 private Types types;
duke@1 68 private Enter enter;
duke@1 69
duke@1 70 public static JavacElements instance(Context context) {
jjg@706 71 JavacElements instance = context.get(JavacElements.class);
jjg@706 72 if (instance == null)
duke@1 73 instance = new JavacElements(context);
duke@1 74 return instance;
duke@1 75 }
duke@1 76
duke@1 77 /**
duke@1 78 * Public for use only by JavacProcessingEnvironment
duke@1 79 */
jjg@706 80 protected JavacElements(Context context) {
duke@1 81 setContext(context);
duke@1 82 }
duke@1 83
duke@1 84 /**
duke@1 85 * Use a new context. May be called from outside to update
duke@1 86 * internal state for a new annotation-processing round.
duke@1 87 */
duke@1 88 public void setContext(Context context) {
jjg@706 89 context.put(JavacElements.class, this);
duke@1 90 javaCompiler = JavaCompiler.instance(context);
duke@1 91 syms = Symtab.instance(context);
jjg@113 92 names = Names.instance(context);
duke@1 93 types = Types.instance(context);
duke@1 94 enter = Enter.instance(context);
duke@1 95 }
duke@1 96
duke@1 97
duke@1 98 /**
duke@1 99 * An internal-use utility that creates a reified annotation.
duke@1 100 */
duke@1 101 public static <A extends Annotation> A getAnnotation(Symbol annotated,
duke@1 102 Class<A> annoType) {
duke@1 103 if (!annoType.isAnnotation())
duke@1 104 throw new IllegalArgumentException("Not an annotation type: "
duke@1 105 + annoType);
duke@1 106 String name = annoType.getName();
duke@1 107 for (Attribute.Compound anno : annotated.getAnnotationMirrors())
duke@1 108 if (name.equals(anno.type.tsym.flatName().toString()))
duke@1 109 return AnnotationProxyMaker.generateAnnotation(anno, annoType);
duke@1 110 return null;
duke@1 111 }
duke@1 112
duke@1 113 /**
duke@1 114 * An internal-use utility that creates a reified annotation.
duke@1 115 * This overloaded version take annotation inheritance into account.
duke@1 116 */
duke@1 117 public static <A extends Annotation> A getAnnotation(ClassSymbol annotated,
duke@1 118 Class<A> annoType) {
duke@1 119 boolean inherited = annoType.isAnnotationPresent(Inherited.class);
duke@1 120 A result = null;
jjg@113 121 while (annotated.name != annotated.name.table.names.java_lang_Object) {
duke@1 122 result = getAnnotation((Symbol)annotated, annoType);
duke@1 123 if (result != null || !inherited)
duke@1 124 break;
duke@1 125 Type sup = annotated.getSuperclass();
duke@1 126 if (sup.tag != TypeTags.CLASS || sup.isErroneous())
duke@1 127 break;
duke@1 128 annotated = (ClassSymbol) sup.tsym;
duke@1 129 }
duke@1 130 return result;
duke@1 131 }
duke@1 132
duke@1 133
duke@1 134 public PackageSymbol getPackageElement(CharSequence name) {
duke@1 135 String strName = name.toString();
duke@1 136 if (strName.equals(""))
duke@1 137 return syms.unnamedPackage;
duke@1 138 return SourceVersion.isName(strName)
duke@1 139 ? nameToSymbol(strName, PackageSymbol.class)
duke@1 140 : null;
duke@1 141 }
duke@1 142
duke@1 143 public ClassSymbol getTypeElement(CharSequence name) {
duke@1 144 String strName = name.toString();
duke@1 145 return SourceVersion.isName(strName)
duke@1 146 ? nameToSymbol(strName, ClassSymbol.class)
duke@1 147 : null;
duke@1 148 }
duke@1 149
duke@1 150 /**
duke@1 151 * Returns a symbol given the type's or packages's canonical name,
duke@1 152 * or null if the name isn't found.
duke@1 153 */
duke@1 154 private <S extends Symbol> S nameToSymbol(String nameStr, Class<S> clazz) {
duke@1 155 Name name = names.fromString(nameStr);
duke@1 156 // First check cache.
duke@1 157 Symbol sym = (clazz == ClassSymbol.class)
duke@1 158 ? syms.classes.get(name)
duke@1 159 : syms.packages.get(name);
duke@1 160
duke@1 161 try {
duke@1 162 if (sym == null)
duke@1 163 sym = javaCompiler.resolveIdent(nameStr);
duke@1 164
duke@1 165 sym.complete();
duke@1 166
duke@1 167 return (sym.kind != Kinds.ERR &&
duke@1 168 sym.exists() &&
duke@1 169 clazz.isInstance(sym) &&
duke@1 170 name.equals(sym.getQualifiedName()))
duke@1 171 ? clazz.cast(sym)
duke@1 172 : null;
duke@1 173 } catch (CompletionFailure e) {
duke@1 174 return null;
duke@1 175 }
duke@1 176 }
duke@1 177
duke@1 178 public JavacSourcePosition getSourcePosition(Element e) {
duke@1 179 Pair<JCTree, JCCompilationUnit> treeTop = getTreeAndTopLevel(e);
duke@1 180 if (treeTop == null)
duke@1 181 return null;
duke@1 182 JCTree tree = treeTop.fst;
duke@1 183 JCCompilationUnit toplevel = treeTop.snd;
duke@1 184 JavaFileObject sourcefile = toplevel.sourcefile;
duke@1 185 if (sourcefile == null)
duke@1 186 return null;
duke@1 187 return new JavacSourcePosition(sourcefile, tree.pos, toplevel.lineMap);
duke@1 188 }
duke@1 189
duke@1 190 public JavacSourcePosition getSourcePosition(Element e, AnnotationMirror a) {
duke@1 191 Pair<JCTree, JCCompilationUnit> treeTop = getTreeAndTopLevel(e);
duke@1 192 if (treeTop == null)
duke@1 193 return null;
duke@1 194 JCTree tree = treeTop.fst;
duke@1 195 JCCompilationUnit toplevel = treeTop.snd;
duke@1 196 JavaFileObject sourcefile = toplevel.sourcefile;
duke@1 197 if (sourcefile == null)
duke@1 198 return null;
duke@1 199
duke@1 200 JCTree annoTree = matchAnnoToTree(a, e, tree);
duke@1 201 if (annoTree == null)
duke@1 202 return null;
duke@1 203 return new JavacSourcePosition(sourcefile, annoTree.pos,
duke@1 204 toplevel.lineMap);
duke@1 205 }
duke@1 206
duke@1 207 public JavacSourcePosition getSourcePosition(Element e, AnnotationMirror a,
duke@1 208 AnnotationValue v) {
duke@1 209 // TODO: better accuracy in getSourcePosition(... AnnotationValue)
duke@1 210 return getSourcePosition(e, a);
duke@1 211 }
duke@1 212
duke@1 213 /**
duke@1 214 * Returns the tree for an annotation given the annotated element
duke@1 215 * and the element's own tree. Returns null if the tree cannot be found.
duke@1 216 */
duke@1 217 private JCTree matchAnnoToTree(AnnotationMirror findme,
duke@1 218 Element e, JCTree tree) {
duke@1 219 Symbol sym = cast(Symbol.class, e);
duke@1 220 class Vis extends JCTree.Visitor {
duke@1 221 List<JCAnnotation> result = null;
duke@1 222 public void visitTopLevel(JCCompilationUnit tree) {
duke@1 223 result = tree.packageAnnotations;
duke@1 224 }
duke@1 225 public void visitClassDef(JCClassDecl tree) {
duke@1 226 result = tree.mods.annotations;
duke@1 227 }
duke@1 228 public void visitMethodDef(JCMethodDecl tree) {
duke@1 229 result = tree.mods.annotations;
duke@1 230 }
duke@1 231 public void visitVarDef(JCVariableDecl tree) {
duke@1 232 result = tree.mods.annotations;
duke@1 233 }
duke@1 234 }
duke@1 235 Vis vis = new Vis();
duke@1 236 tree.accept(vis);
duke@1 237 if (vis.result == null)
duke@1 238 return null;
duke@1 239 return matchAnnoToTree(cast(Attribute.Compound.class, findme),
duke@1 240 sym.getAnnotationMirrors(),
duke@1 241 vis.result);
duke@1 242 }
duke@1 243
duke@1 244 /**
duke@1 245 * Returns the tree for an annotation given a list of annotations
duke@1 246 * in which to search (recursively) and their corresponding trees.
duke@1 247 * Returns null if the tree cannot be found.
duke@1 248 */
duke@1 249 private JCTree matchAnnoToTree(Attribute.Compound findme,
duke@1 250 List<Attribute.Compound> annos,
duke@1 251 List<JCAnnotation> trees) {
duke@1 252 for (Attribute.Compound anno : annos) {
duke@1 253 for (JCAnnotation tree : trees) {
duke@1 254 JCTree match = matchAnnoToTree(findme, anno, tree);
duke@1 255 if (match != null)
duke@1 256 return match;
duke@1 257 }
duke@1 258 }
duke@1 259 return null;
duke@1 260 }
duke@1 261
duke@1 262 /**
duke@1 263 * Returns the tree for an annotation given an Attribute to
duke@1 264 * search (recursively) and its corresponding tree.
duke@1 265 * Returns null if the tree cannot be found.
duke@1 266 */
duke@1 267 private JCTree matchAnnoToTree(final Attribute.Compound findme,
duke@1 268 final Attribute attr,
duke@1 269 final JCTree tree) {
duke@1 270 if (attr == findme)
duke@1 271 return (tree.type.tsym == findme.type.tsym) ? tree : null;
duke@1 272
duke@1 273 class Vis implements Attribute.Visitor {
duke@1 274 JCTree result = null;
duke@1 275 public void visitConstant(Attribute.Constant value) {
duke@1 276 }
duke@1 277 public void visitClass(Attribute.Class clazz) {
duke@1 278 }
duke@1 279 public void visitCompound(Attribute.Compound anno) {
duke@1 280 for (Pair<MethodSymbol, Attribute> pair : anno.values) {
duke@1 281 JCExpression expr = scanForAssign(pair.fst, tree);
duke@1 282 if (expr != null) {
duke@1 283 JCTree match = matchAnnoToTree(findme, pair.snd, expr);
duke@1 284 if (match != null) {
duke@1 285 result = match;
duke@1 286 return;
duke@1 287 }
duke@1 288 }
duke@1 289 }
duke@1 290 }
duke@1 291 public void visitArray(Attribute.Array array) {
jjg@1127 292 if (tree.hasTag(NEWARRAY) &&
duke@1 293 types.elemtype(array.type).tsym == findme.type.tsym) {
duke@1 294 List<JCExpression> elems = ((JCNewArray) tree).elems;
duke@1 295 for (Attribute value : array.values) {
duke@1 296 if (value == findme) {
duke@1 297 result = elems.head;
duke@1 298 return;
duke@1 299 }
duke@1 300 elems = elems.tail;
duke@1 301 }
duke@1 302 }
duke@1 303 }
duke@1 304 public void visitEnum(Attribute.Enum e) {
duke@1 305 }
duke@1 306 public void visitError(Attribute.Error e) {
duke@1 307 }
duke@1 308 }
duke@1 309 Vis vis = new Vis();
duke@1 310 attr.accept(vis);
duke@1 311 return vis.result;
duke@1 312 }
duke@1 313
duke@1 314 /**
duke@1 315 * Scans for a JCAssign node with a LHS matching a given
duke@1 316 * symbol, and returns its RHS. Does not scan nested JCAnnotations.
duke@1 317 */
duke@1 318 private JCExpression scanForAssign(final MethodSymbol sym,
duke@1 319 final JCTree tree) {
duke@1 320 class TS extends TreeScanner {
duke@1 321 JCExpression result = null;
duke@1 322 public void scan(JCTree t) {
duke@1 323 if (t != null && result == null)
duke@1 324 t.accept(this);
duke@1 325 }
duke@1 326 public void visitAnnotation(JCAnnotation t) {
duke@1 327 if (t == tree)
duke@1 328 scan(t.args);
duke@1 329 }
duke@1 330 public void visitAssign(JCAssign t) {
jjg@1127 331 if (t.lhs.hasTag(IDENT)) {
duke@1 332 JCIdent ident = (JCIdent) t.lhs;
duke@1 333 if (ident.sym == sym)
duke@1 334 result = t.rhs;
duke@1 335 }
duke@1 336 }
duke@1 337 }
duke@1 338 TS scanner = new TS();
duke@1 339 tree.accept(scanner);
duke@1 340 return scanner.result;
duke@1 341 }
duke@1 342
duke@1 343 /**
duke@1 344 * Returns the tree node corresponding to this element, or null
duke@1 345 * if none can be found.
duke@1 346 */
duke@1 347 public JCTree getTree(Element e) {
duke@1 348 Pair<JCTree, ?> treeTop = getTreeAndTopLevel(e);
duke@1 349 return (treeTop != null) ? treeTop.fst : null;
duke@1 350 }
duke@1 351
duke@1 352 public String getDocComment(Element e) {
duke@1 353 // Our doc comment is contained in a map in our toplevel,
duke@1 354 // indexed by our tree. Find our enter environment, which gives
duke@1 355 // us our toplevel. It also gives us a tree that contains our
duke@1 356 // tree: walk it to find our tree. This is painful.
duke@1 357 Pair<JCTree, JCCompilationUnit> treeTop = getTreeAndTopLevel(e);
duke@1 358 if (treeTop == null)
duke@1 359 return null;
duke@1 360 JCTree tree = treeTop.fst;
duke@1 361 JCCompilationUnit toplevel = treeTop.snd;
duke@1 362 if (toplevel.docComments == null)
duke@1 363 return null;
duke@1 364 return toplevel.docComments.get(tree);
duke@1 365 }
duke@1 366
duke@1 367 public PackageElement getPackageOf(Element e) {
duke@1 368 return cast(Symbol.class, e).packge();
duke@1 369 }
duke@1 370
duke@1 371 public boolean isDeprecated(Element e) {
duke@1 372 Symbol sym = cast(Symbol.class, e);
duke@1 373 return (sym.flags() & Flags.DEPRECATED) != 0;
duke@1 374 }
duke@1 375
duke@1 376 public Name getBinaryName(TypeElement type) {
duke@1 377 return cast(TypeSymbol.class, type).flatName();
duke@1 378 }
duke@1 379
duke@1 380 public Map<MethodSymbol, Attribute> getElementValuesWithDefaults(
duke@1 381 AnnotationMirror a) {
duke@1 382 Attribute.Compound anno = cast(Attribute.Compound.class, a);
duke@1 383 DeclaredType annotype = a.getAnnotationType();
duke@1 384 Map<MethodSymbol, Attribute> valmap = anno.getElementValues();
duke@1 385
duke@1 386 for (ExecutableElement ex :
duke@1 387 methodsIn(annotype.asElement().getEnclosedElements())) {
duke@1 388 MethodSymbol meth = (MethodSymbol) ex;
duke@1 389 Attribute defaultValue = meth.getDefaultValue();
duke@1 390 if (defaultValue != null && !valmap.containsKey(meth)) {
duke@1 391 valmap.put(meth, defaultValue);
duke@1 392 }
duke@1 393 }
duke@1 394 return valmap;
duke@1 395 }
duke@1 396
duke@1 397 /**
duke@1 398 * {@inheritDoc}
duke@1 399 */
duke@1 400 public FilteredMemberList getAllMembers(TypeElement element) {
duke@1 401 Symbol sym = cast(Symbol.class, element);
duke@1 402 Scope scope = sym.members().dupUnshared();
duke@1 403 List<Type> closure = types.closure(sym.asType());
duke@1 404 for (Type t : closure)
duke@1 405 addMembers(scope, t);
duke@1 406 return new FilteredMemberList(scope);
duke@1 407 }
duke@1 408 // where
duke@1 409 private void addMembers(Scope scope, Type type) {
duke@1 410 members:
duke@1 411 for (Scope.Entry e = type.asElement().members().elems; e != null; e = e.sibling) {
duke@1 412 Scope.Entry overrider = scope.lookup(e.sym.getSimpleName());
duke@1 413 while (overrider.scope != null) {
duke@1 414 if (overrider.sym.kind == e.sym.kind
duke@1 415 && (overrider.sym.flags() & Flags.SYNTHETIC) == 0)
duke@1 416 {
duke@1 417 if (overrider.sym.getKind() == ElementKind.METHOD
duke@1 418 && overrides((ExecutableElement)overrider.sym, (ExecutableElement)e.sym, (TypeElement)type.asElement())) {
duke@1 419 continue members;
duke@1 420 }
duke@1 421 }
duke@1 422 overrider = overrider.next();
duke@1 423 }
duke@1 424 boolean derived = e.sym.getEnclosingElement() != scope.owner;
duke@1 425 ElementKind kind = e.sym.getKind();
duke@1 426 boolean initializer = kind == ElementKind.CONSTRUCTOR
duke@1 427 || kind == ElementKind.INSTANCE_INIT
duke@1 428 || kind == ElementKind.STATIC_INIT;
duke@1 429 if (!derived || (!initializer && e.sym.isInheritedIn(scope.owner, types)))
duke@1 430 scope.enter(e.sym);
duke@1 431 }
duke@1 432 }
duke@1 433
duke@1 434 /**
duke@1 435 * Returns all annotations of an element, whether
duke@1 436 * inherited or directly present.
duke@1 437 *
duke@1 438 * @param e the element being examined
duke@1 439 * @return all annotations of the element
duke@1 440 */
duke@1 441 public List<Attribute.Compound> getAllAnnotationMirrors(Element e) {
duke@1 442 Symbol sym = cast(Symbol.class, e);
duke@1 443 List<Attribute.Compound> annos = sym.getAnnotationMirrors();
duke@1 444 while (sym.getKind() == ElementKind.CLASS) {
duke@1 445 Type sup = ((ClassSymbol) sym).getSuperclass();
duke@1 446 if (sup.tag != TypeTags.CLASS || sup.isErroneous() ||
duke@1 447 sup.tsym == syms.objectType.tsym) {
duke@1 448 break;
duke@1 449 }
duke@1 450 sym = sup.tsym;
duke@1 451 List<Attribute.Compound> oldAnnos = annos;
duke@1 452 for (Attribute.Compound anno : sym.getAnnotationMirrors()) {
duke@1 453 if (isInherited(anno.type) &&
duke@1 454 !containsAnnoOfType(oldAnnos, anno.type)) {
duke@1 455 annos = annos.prepend(anno);
duke@1 456 }
duke@1 457 }
duke@1 458 }
duke@1 459 return annos;
duke@1 460 }
duke@1 461
duke@1 462 /**
duke@1 463 * Tests whether an annotation type is @Inherited.
duke@1 464 */
duke@1 465 private boolean isInherited(Type annotype) {
duke@1 466 for (Attribute.Compound anno : annotype.tsym.getAnnotationMirrors()) {
duke@1 467 if (anno.type.tsym == syms.inheritedType.tsym)
duke@1 468 return true;
duke@1 469 }
duke@1 470 return false;
duke@1 471 }
duke@1 472
duke@1 473 /**
duke@1 474 * Tests whether a list of annotations contains an annotation
duke@1 475 * of a given type.
duke@1 476 */
duke@1 477 private static boolean containsAnnoOfType(List<Attribute.Compound> annos,
duke@1 478 Type type) {
duke@1 479 for (Attribute.Compound anno : annos) {
duke@1 480 if (anno.type.tsym == type.tsym)
duke@1 481 return true;
duke@1 482 }
duke@1 483 return false;
duke@1 484 }
duke@1 485
duke@1 486 public boolean hides(Element hiderEl, Element hideeEl) {
duke@1 487 Symbol hider = cast(Symbol.class, hiderEl);
duke@1 488 Symbol hidee = cast(Symbol.class, hideeEl);
duke@1 489
duke@1 490 // Fields only hide fields; methods only methods; types only types.
duke@1 491 // Names must match. Nothing hides itself (just try it).
duke@1 492 if (hider == hidee ||
duke@1 493 hider.kind != hidee.kind ||
duke@1 494 hider.name != hidee.name) {
duke@1 495 return false;
duke@1 496 }
duke@1 497
duke@1 498 // Only static methods can hide other methods.
duke@1 499 // Methods only hide methods with matching signatures.
duke@1 500 if (hider.kind == Kinds.MTH) {
duke@1 501 if (!hider.isStatic() ||
duke@1 502 !types.isSubSignature(hider.type, hidee.type)) {
duke@1 503 return false;
duke@1 504 }
duke@1 505 }
duke@1 506
duke@1 507 // Hider must be in a subclass of hidee's class.
duke@1 508 // Note that if M1 hides M2, and M2 hides M3, and M3 is accessible
duke@1 509 // in M1's class, then M1 and M2 both hide M3.
duke@1 510 ClassSymbol hiderClass = hider.owner.enclClass();
duke@1 511 ClassSymbol hideeClass = hidee.owner.enclClass();
duke@1 512 if (hiderClass == null || hideeClass == null ||
duke@1 513 !hiderClass.isSubClass(hideeClass, types)) {
duke@1 514 return false;
duke@1 515 }
duke@1 516
duke@1 517 // Hidee must be accessible in hider's class.
duke@1 518 // The method isInheritedIn is poorly named: it checks only access.
duke@1 519 return hidee.isInheritedIn(hiderClass, types);
duke@1 520 }
duke@1 521
duke@1 522 public boolean overrides(ExecutableElement riderEl,
duke@1 523 ExecutableElement rideeEl, TypeElement typeEl) {
duke@1 524 MethodSymbol rider = cast(MethodSymbol.class, riderEl);
duke@1 525 MethodSymbol ridee = cast(MethodSymbol.class, rideeEl);
duke@1 526 ClassSymbol origin = cast(ClassSymbol.class, typeEl);
duke@1 527
duke@1 528 return rider.name == ridee.name &&
duke@1 529
duke@1 530 // not reflexive as per JLS
duke@1 531 rider != ridee &&
duke@1 532
duke@1 533 // we don't care if ridee is static, though that wouldn't
duke@1 534 // compile
duke@1 535 !rider.isStatic() &&
duke@1 536
duke@1 537 // Symbol.overrides assumes the following
duke@1 538 ridee.isMemberOf(origin, types) &&
duke@1 539
duke@1 540 // check access and signatures; don't check return types
duke@1 541 rider.overrides(ridee, origin, types, false);
duke@1 542 }
duke@1 543
duke@1 544 public String getConstantExpression(Object value) {
duke@1 545 return Constants.format(value);
duke@1 546 }
duke@1 547
duke@1 548 /**
duke@1 549 * Print a representation of the elements to the given writer in
duke@1 550 * the specified order. The main purpose of this method is for
duke@1 551 * diagnostics. The exact format of the output is <em>not</em>
duke@1 552 * specified and is subject to change.
duke@1 553 *
duke@1 554 * @param w the writer to print the output to
duke@1 555 * @param elements the elements to print
duke@1 556 */
duke@1 557 public void printElements(java.io.Writer w, Element... elements) {
duke@1 558 for (Element element : elements)
duke@1 559 (new PrintingProcessor.PrintingElementVisitor(w, this)).visit(element).flush();
duke@1 560 }
duke@1 561
duke@1 562 public Name getName(CharSequence cs) {
jjg@113 563 return names.fromString(cs.toString());
duke@1 564 }
duke@1 565
duke@1 566 /**
duke@1 567 * Returns the tree node and compilation unit corresponding to this
duke@1 568 * element, or null if they can't be found.
duke@1 569 */
duke@1 570 private Pair<JCTree, JCCompilationUnit> getTreeAndTopLevel(Element e) {
duke@1 571 Symbol sym = cast(Symbol.class, e);
duke@1 572 Env<AttrContext> enterEnv = getEnterEnv(sym);
duke@1 573 if (enterEnv == null)
duke@1 574 return null;
duke@1 575 JCTree tree = TreeInfo.declarationFor(sym, enterEnv.tree);
duke@1 576 if (tree == null || enterEnv.toplevel == null)
duke@1 577 return null;
duke@1 578 return new Pair<JCTree,JCCompilationUnit>(tree, enterEnv.toplevel);
duke@1 579 }
duke@1 580
duke@1 581 /**
duke@1 582 * Returns the best approximation for the tree node and compilation unit
duke@1 583 * corresponding to the given element, annotation and value.
duke@1 584 * If the element is null, null is returned.
duke@1 585 * If the annotation is null or cannot be found, the tree node and
duke@1 586 * compilation unit for the element is returned.
duke@1 587 * If the annotation value is null or cannot be found, the tree node and
duke@1 588 * compilation unit for the annotation is returned.
duke@1 589 */
duke@1 590 public Pair<JCTree, JCCompilationUnit> getTreeAndTopLevel(
duke@1 591 Element e, AnnotationMirror a, AnnotationValue v) {
duke@1 592 if (e == null)
duke@1 593 return null;
duke@1 594
duke@1 595 Pair<JCTree, JCCompilationUnit> elemTreeTop = getTreeAndTopLevel(e);
duke@1 596 if (elemTreeTop == null)
duke@1 597 return null;
duke@1 598
duke@1 599 if (a == null)
duke@1 600 return elemTreeTop;
duke@1 601
duke@1 602 JCTree annoTree = matchAnnoToTree(a, e, elemTreeTop.fst);
duke@1 603 if (annoTree == null)
duke@1 604 return elemTreeTop;
duke@1 605
duke@1 606 // 6388543: if v != null, we should search within annoTree to find
duke@1 607 // the tree matching v. For now, we ignore v and return the tree of
duke@1 608 // the annotation.
duke@1 609 return new Pair<JCTree, JCCompilationUnit>(annoTree, elemTreeTop.snd);
duke@1 610 }
duke@1 611
duke@1 612 /**
duke@1 613 * Returns a symbol's enter environment, or null if it has none.
duke@1 614 */
duke@1 615 private Env<AttrContext> getEnterEnv(Symbol sym) {
duke@1 616 // Get enclosing class of sym, or sym itself if it is a class
duke@1 617 // or package.
duke@1 618 TypeSymbol ts = (sym.kind != Kinds.PCK)
duke@1 619 ? sym.enclClass()
duke@1 620 : (PackageSymbol) sym;
duke@1 621 return (ts != null)
duke@1 622 ? enter.getEnv(ts)
duke@1 623 : null;
duke@1 624 }
duke@1 625
duke@1 626 /**
duke@1 627 * Returns an object cast to the specified type.
duke@1 628 * @throws NullPointerException if the object is {@code null}
duke@1 629 * @throws IllegalArgumentException if the object is of the wrong type
duke@1 630 */
duke@1 631 private static <T> T cast(Class<T> clazz, Object o) {
duke@1 632 if (! clazz.isInstance(o))
duke@1 633 throw new IllegalArgumentException(o.toString());
duke@1 634 return clazz.cast(o);
duke@1 635 }
duke@1 636 }

mercurial