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

Thu, 02 Oct 2008 19:58:40 -0700

author
xdono
date
Thu, 02 Oct 2008 19:58:40 -0700
changeset 117
24a47c3062fe
parent 113
eff38cc97183
child 554
9d9f26857129
permissions
-rw-r--r--

6754988: Update copyright year
Summary: Update for files that have been modified starting July 2008
Reviewed-by: ohair, tbell

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

mercurial