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

Thu, 25 Oct 2012 11:09:36 -0700

author
jjg
date
Thu, 25 Oct 2012 11:09:36 -0700
changeset 1374
c002fdee76fd
parent 1280
5c0b3faeb0b0
child 1491
9f42a06a49c0
permissions
-rw-r--r--

7200915: convert TypeTags from a series of small ints to an enum
Reviewed-by: jjg, mcimadamore
Contributed-by: vicente.romero@oracle.com

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

mercurial