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

Tue, 05 Oct 2010 11:34:43 -0700

author
jjg
date
Tue, 05 Oct 2010 11:34:43 -0700
changeset 706
971c8132f5b2
parent 581
f2fdd52e4e87
child 798
4868a36f6fd8
permissions
-rw-r--r--

6988836: A new JavacElements is created for each round of annotation processing
Reviewed-by: darcy

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

mercurial