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

Wed, 02 Jul 2008 12:56:02 -0700

author
xdono
date
Wed, 02 Jul 2008 12:56:02 -0700
changeset 54
eaf608c64fec
parent 1
9a66ca7c79fa
child 113
eff38cc97183
permissions
-rw-r--r--

6719955: Update copyright year
Summary: Update copyright year for files that have been modified in 2008
Reviewed-by: ohair, tbell

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

mercurial