1 /*
   2  * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.tools.javac.model;
  27 
  28 import java.util.Collections;
  29 import java.util.HashSet;
  30 import java.util.LinkedHashSet;
  31 import java.util.Map;
  32 import java.util.Set;
  33 import java.util.stream.Collectors;
  34 
  35 import javax.lang.model.AnnotatedConstruct;
  36 import javax.lang.model.SourceVersion;
  37 import javax.lang.model.element.*;
  38 import javax.lang.model.type.DeclaredType;
  39 import javax.lang.model.util.Elements;
  40 import javax.tools.JavaFileObject;
  41 import static javax.lang.model.util.ElementFilter.methodsIn;
  42 
  43 import com.sun.source.util.JavacTask;
  44 import com.sun.tools.javac.api.JavacTaskImpl;
  45 import com.sun.tools.javac.code.*;
  46 import com.sun.tools.javac.code.Attribute.Compound;
  47 import com.sun.tools.javac.code.Directive.ExportsDirective;
  48 import com.sun.tools.javac.code.Directive.ExportsFlag;
  49 import com.sun.tools.javac.code.Directive.OpensDirective;
  50 import com.sun.tools.javac.code.Directive.OpensFlag;
  51 import com.sun.tools.javac.code.Directive.RequiresDirective;
  52 import com.sun.tools.javac.code.Directive.RequiresFlag;
  53 import com.sun.tools.javac.code.Scope.WriteableScope;
  54 import com.sun.tools.javac.code.Source.Feature;
  55 import com.sun.tools.javac.code.Symbol.*;
  56 import com.sun.tools.javac.comp.AttrContext;
  57 import com.sun.tools.javac.comp.Enter;
  58 import com.sun.tools.javac.comp.Env;
  59 import com.sun.tools.javac.main.JavaCompiler;
  60 import com.sun.tools.javac.processing.PrintingProcessor;
  61 import com.sun.tools.javac.tree.JCTree;
  62 import com.sun.tools.javac.tree.JCTree.*;
  63 import com.sun.tools.javac.tree.TreeInfo;
  64 import com.sun.tools.javac.tree.TreeScanner;
  65 import com.sun.tools.javac.util.*;
  66 import com.sun.tools.javac.util.DefinedBy.Api;
  67 import com.sun.tools.javac.util.Name;
  68 import static com.sun.tools.javac.code.Kinds.Kind.*;
  69 import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE;
  70 import static com.sun.tools.javac.code.TypeTag.CLASS;
  71 import com.sun.tools.javac.comp.Modules;
  72 import com.sun.tools.javac.comp.Resolve;
  73 import com.sun.tools.javac.comp.Resolve.RecoveryLoadClass;
  74 import com.sun.tools.javac.resources.CompilerProperties.Notes;
  75 import static com.sun.tools.javac.tree.JCTree.Tag.*;
  76 
  77 /**
  78  * Utility methods for operating on program elements.
  79  *
  80  * <p><b>This is NOT part of any supported API.
  81  * If you write code that depends on this, you do so at your own
  82  * risk.  This code and its internal interfaces are subject to change
  83  * or deletion without notice.</b></p>
  84  */
  85 public class JavacElements implements Elements {
  86 
  87     private final JavaCompiler javaCompiler;
  88     private final Symtab syms;
  89     private final Modules modules;
  90     private final Names names;
  91     private final Types types;
  92     private final Enter enter;
  93     private final Resolve resolve;
  94     private final JavacTaskImpl javacTaskImpl;
  95     private final Log log;
  96     private final boolean allowModules;
  97 
  98     public static JavacElements instance(Context context) {
  99         JavacElements instance = context.get(JavacElements.class);
 100         if (instance == null)
 101             instance = new JavacElements(context);
 102         return instance;
 103     }
 104 
 105     protected JavacElements(Context context) {
 106         context.put(JavacElements.class, this);
 107         javaCompiler = JavaCompiler.instance(context);
 108         syms = Symtab.instance(context);
 109         modules = Modules.instance(context);
 110         names = Names.instance(context);
 111         types = Types.instance(context);
 112         enter = Enter.instance(context);
 113         resolve = Resolve.instance(context);
 114         JavacTask t = context.get(JavacTask.class);
 115         javacTaskImpl = t instanceof JavacTaskImpl ? (JavacTaskImpl) t : null;
 116         log = Log.instance(context);
 117         Source source = Source.instance(context);
 118         allowModules = Feature.MODULES.allowedInSource(source);
 119     }
 120 
 121     @Override @DefinedBy(Api.LANGUAGE_MODEL)
 122     public Set<? extends ModuleElement> getAllModuleElements() {
 123         if (allowModules)
 124             return Collections.unmodifiableSet(modules.allModules());
 125         else
 126             return Collections.emptySet();
 127     }
 128 
 129     @Override @DefinedBy(Api.LANGUAGE_MODEL)
 130     public ModuleSymbol getModuleElement(CharSequence name) {
 131         ensureEntered("getModuleElement");
 132         if (modules.getDefaultModule() == syms.noModule)
 133             return null;
 134         String strName = name.toString();
 135         if (strName.equals(""))
 136             return syms.unnamedModule;
 137         return modules.getObservableModule(names.fromString(strName));
 138     }
 139 
 140     @Override @DefinedBy(Api.LANGUAGE_MODEL)
 141     public PackageSymbol getPackageElement(CharSequence name) {
 142         return doGetPackageElement(null, name);
 143     }
 144 
 145     @Override @DefinedBy(Api.LANGUAGE_MODEL)
 146     public PackageSymbol getPackageElement(ModuleElement module, CharSequence name) {
 147         module.getClass();
 148         return doGetPackageElement(module, name);
 149     }
 150 
 151     private PackageSymbol doGetPackageElement(ModuleElement module, CharSequence name) {
 152         ensureEntered("getPackageElement");
 153         return doGetElement(module, "getPackageElement", name, PackageSymbol.class);
 154     }
 155 
 156     @Override @DefinedBy(Api.LANGUAGE_MODEL)
 157     public ClassSymbol getTypeElement(CharSequence name) {
 158         return doGetTypeElement(null, name);
 159     }
 160 
 161     @Override @DefinedBy(Api.LANGUAGE_MODEL)
 162     public ClassSymbol getTypeElement(ModuleElement module, CharSequence name) {
 163         module.getClass();
 164 
 165         return doGetTypeElement(module, name);
 166     }
 167 
 168     private ClassSymbol doGetTypeElement(ModuleElement module, CharSequence name) {
 169         ensureEntered("getTypeElement");
 170         return doGetElement(module, "getTypeElement", name, ClassSymbol.class);
 171     }
 172 
 173     private <S extends Symbol> S doGetElement(ModuleElement module, String methodName,
 174                                               CharSequence name, Class<S> clazz) {
 175         String strName = name.toString();
 176         if (!SourceVersion.isName(strName) && (!strName.isEmpty() || clazz == ClassSymbol.class)) {
 177             return null;
 178         }
 179         if (module == null) {
 180             return unboundNameToSymbol(methodName, strName, clazz);
 181         } else {
 182             return nameToSymbol((ModuleSymbol) module, strName, clazz);
 183         }
 184     }
 185 
 186     private final Set<String> alreadyWarnedDuplicates = new HashSet<>();
 187 
 188     private <S extends Symbol> S unboundNameToSymbol(String methodName,
 189                                                      String nameStr,
 190                                                      Class<S> clazz) {
 191         if (modules.getDefaultModule() == syms.noModule) { //not a modular mode:
 192             return nameToSymbol(syms.noModule, nameStr, clazz);
 193         }
 194 
 195         Set<S> found = new LinkedHashSet<>();
 196 
 197         for (ModuleSymbol msym : modules.allModules()) {
 198             S sym = nameToSymbol(msym, nameStr, clazz);
 199 
 200             if (sym == null)
 201                 continue;
 202 
 203             if (clazz == ClassSymbol.class) {
 204                 // Always include classes
 205                 found.add(sym);
 206             } else if (clazz == PackageSymbol.class) {
 207                 // In module mode, ignore the "spurious" empty packages that "enclose" module-specific packages.
 208                 // For example, if a module contains classes or package info in package p.q.r, it will also appear
 209                 // to have additional packages p.q and p, even though these packages have no content other
 210                 // than the subpackage.  We don't want those empty packages showing up in searches for p or p.q.
 211                 if (!sym.members().isEmpty() || ((PackageSymbol) sym).package_info != null) {
 212                     found.add(sym);
 213                 }
 214             }
 215         }
 216 
 217         if (found.size() == 1) {
 218             return found.iterator().next();
 219         } else if (found.size() > 1) {
 220             //more than one element found, produce a note:
 221             if (alreadyWarnedDuplicates.add(methodName + ":" + nameStr)) {
 222                 String moduleNames = found.stream()
 223                                           .map(s -> s.packge().modle)
 224                                           .map(m -> m.toString())
 225                                           .collect(Collectors.joining(", "));
 226                 log.note(Notes.MultipleElements(methodName, nameStr, moduleNames));
 227             }
 228             return null;
 229         } else {
 230             //not found, or more than one element found:
 231             return null;
 232         }
 233     }
 234 
 235     /**
 236      * Returns a symbol given the type's or package's canonical name,
 237      * or null if the name isn't found.
 238      */
 239     private <S extends Symbol> S nameToSymbol(ModuleSymbol module, String nameStr, Class<S> clazz) {
 240         Name name = names.fromString(nameStr);
 241         // First check cache.
 242         Symbol sym = (clazz == ClassSymbol.class)
 243                     ? syms.getClass(module, name)
 244                     : syms.lookupPackage(module, name);
 245 
 246         try {
 247             if (sym == null)
 248                 sym = javaCompiler.resolveIdent(module, nameStr);
 249 
 250             if (clazz.isInstance(sym)) {
 251                 sym.complete();
 252                 if (sym.kind != ERR &&
 253                     sym.exists() &&
 254                     name.equals(sym.getQualifiedName())) {
 255                     return clazz.cast(sym);
 256                 }
 257             }
 258             return null;
 259         } catch (CompletionFailure cf) {
 260             cf.dcfh.handleAPICompletionFailure(cf);
 261             return null;
 262         }
 263     }
 264 
 265     /**
 266      * Returns the tree for an annotation given the annotated element
 267      * and the element's own tree.  Returns null if the tree cannot be found.
 268      */
 269     private JCTree matchAnnoToTree(AnnotationMirror findme,
 270                                    Element e, JCTree tree) {
 271         Symbol sym = cast(Symbol.class, e);
 272         class Vis extends JCTree.Visitor {
 273             List<JCAnnotation> result = null;
 274             public void visitPackageDef(JCPackageDecl tree) {
 275                 result = tree.annotations;
 276             }
 277             public void visitClassDef(JCClassDecl tree) {
 278                 result = tree.mods.annotations;
 279             }
 280             public void visitMethodDef(JCMethodDecl tree) {
 281                 result = tree.mods.annotations;
 282             }
 283             public void visitVarDef(JCVariableDecl tree) {
 284                 result = tree.mods.annotations;
 285             }
 286             @Override
 287             public void visitTypeParameter(JCTypeParameter tree) {
 288                 result = tree.annotations;
 289             }
 290         }
 291         Vis vis = new Vis();
 292         tree.accept(vis);
 293         if (vis.result == null)
 294             return null;
 295 
 296         List<Attribute.Compound> annos = sym.getAnnotationMirrors();
 297         return matchAnnoToTree(cast(Attribute.Compound.class, findme),
 298                                annos,
 299                                vis.result);
 300     }
 301 
 302     /**
 303      * Returns the tree for an annotation given a list of annotations
 304      * in which to search (recursively) and their corresponding trees.
 305      * Returns null if the tree cannot be found.
 306      */
 307     private JCTree matchAnnoToTree(Attribute.Compound findme,
 308                                    List<Attribute.Compound> annos,
 309                                    List<JCAnnotation> trees) {
 310         for (Attribute.Compound anno : annos) {
 311             for (JCAnnotation tree : trees) {
 312                 if (tree.type.tsym != anno.type.tsym)
 313                     continue;
 314                 JCTree match = matchAttributeToTree(findme, anno, tree);
 315                 if (match != null)
 316                     return match;
 317             }
 318         }
 319         return null;
 320     }
 321 
 322     /**
 323      * Returns the tree for an attribute given an enclosing attribute to
 324      * search (recursively) and the enclosing attribute's corresponding tree.
 325      * Returns null if the tree cannot be found.
 326      */
 327     private JCTree matchAttributeToTree(final Attribute findme,
 328                                         final Attribute attr,
 329                                         final JCTree tree) {
 330         if (attr == findme)
 331             return tree;
 332 
 333         class Vis implements Attribute.Visitor {
 334             JCTree result = null;
 335             public void visitConstant(Attribute.Constant value) {
 336             }
 337             public void visitClass(Attribute.Class clazz) {
 338             }
 339             public void visitCompound(Attribute.Compound anno) {
 340                 for (Pair<MethodSymbol, Attribute> pair : anno.values) {
 341                     JCExpression expr = scanForAssign(pair.fst, tree);
 342                     if (expr != null) {
 343                         JCTree match = matchAttributeToTree(findme, pair.snd, expr);
 344                         if (match != null) {
 345                             result = match;
 346                             return;
 347                         }
 348                     }
 349                 }
 350             }
 351             public void visitArray(Attribute.Array array) {
 352                 if (tree.hasTag(NEWARRAY)) {
 353                     List<JCExpression> elems = ((JCNewArray)tree).elems;
 354                     for (Attribute value : array.values) {
 355                         JCTree match = matchAttributeToTree(findme, value, elems.head);
 356                         if (match != null) {
 357                             result = match;
 358                             return;
 359                         }
 360                         elems = elems.tail;
 361                     }
 362                 } else if (array.values.length == 1) {
 363                     // the tree may not be a NEWARRAY for single-element array initializers
 364                     result = matchAttributeToTree(findme, array.values[0], tree);
 365                 }
 366             }
 367             public void visitEnum(Attribute.Enum e) {
 368             }
 369             public void visitError(Attribute.Error e) {
 370             }
 371         }
 372         Vis vis = new Vis();
 373         attr.accept(vis);
 374         return vis.result;
 375     }
 376 
 377     /**
 378      * Scans for a JCAssign node with a LHS matching a given
 379      * symbol, and returns its RHS.  Does not scan nested JCAnnotations.
 380      */
 381     private JCExpression scanForAssign(final MethodSymbol sym,
 382                                        final JCTree tree) {
 383         class TS extends TreeScanner {
 384             JCExpression result = null;
 385             public void scan(JCTree t) {
 386                 if (t != null && result == null)
 387                     t.accept(this);
 388             }
 389             public void visitAnnotation(JCAnnotation t) {
 390                 if (t == tree)
 391                     scan(t.args);
 392             }
 393             public void visitAssign(JCAssign t) {
 394                 if (t.lhs.hasTag(IDENT)) {
 395                     JCIdent ident = (JCIdent) t.lhs;
 396                     if (ident.sym == sym)
 397                         result = t.rhs;
 398                 }
 399             }
 400         }
 401         TS scanner = new TS();
 402         tree.accept(scanner);
 403         return scanner.result;
 404     }
 405 
 406     /**
 407      * Returns the tree node corresponding to this element, or null
 408      * if none can be found.
 409      */
 410     public JCTree getTree(Element e) {
 411         Pair<JCTree, ?> treeTop = getTreeAndTopLevel(e);
 412         return (treeTop != null) ? treeTop.fst : null;
 413     }
 414 
 415     @DefinedBy(Api.LANGUAGE_MODEL)
 416     public String getDocComment(Element e) {
 417         // Our doc comment is contained in a map in our toplevel,
 418         // indexed by our tree.  Find our enter environment, which gives
 419         // us our toplevel.  It also gives us a tree that contains our
 420         // tree:  walk it to find our tree.  This is painful.
 421         Pair<JCTree, JCCompilationUnit> treeTop = getTreeAndTopLevel(e);
 422         if (treeTop == null)
 423             return null;
 424         JCTree tree = treeTop.fst;
 425         JCCompilationUnit toplevel = treeTop.snd;
 426         if (toplevel.docComments == null)
 427             return null;
 428         return toplevel.docComments.getCommentText(tree);
 429     }
 430 
 431     @DefinedBy(Api.LANGUAGE_MODEL)
 432     public PackageElement getPackageOf(Element e) {
 433         return cast(Symbol.class, e).packge();
 434     }
 435 
 436     @DefinedBy(Api.LANGUAGE_MODEL)
 437     public ModuleElement getModuleOf(Element e) {
 438         Symbol sym = cast(Symbol.class, e);
 439         if (modules.getDefaultModule() == syms.noModule)
 440             return null;
 441         return (sym.kind == MDL) ? ((ModuleElement) e) : sym.packge().modle;
 442     }
 443 
 444     @DefinedBy(Api.LANGUAGE_MODEL)
 445     public boolean isDeprecated(Element e) {
 446         Symbol sym = cast(Symbol.class, e);
 447         sym.apiComplete();
 448         return sym.isDeprecated();
 449     }
 450 
 451     @Override @DefinedBy(Api.LANGUAGE_MODEL)
 452     public Origin getOrigin(Element e) {
 453         Symbol sym = cast(Symbol.class, e);
 454         if ((sym.flags() & Flags.GENERATEDCONSTR) != 0)
 455             return Origin.MANDATED;
 456         //TypeElement.getEnclosedElements does not return synthetic elements,
 457         //and most synthetic elements are not read from the classfile anyway:
 458         return Origin.EXPLICIT;
 459     }
 460 
 461     @Override @DefinedBy(Api.LANGUAGE_MODEL)
 462     public Origin getOrigin(AnnotatedConstruct c, AnnotationMirror a) {
 463         Compound ac = cast(Compound.class, a);
 464         if (ac.isSynthesized())
 465             return Origin.MANDATED;
 466         return Origin.EXPLICIT;
 467     }
 468 
 469     @Override @DefinedBy(Api.LANGUAGE_MODEL)
 470     public Origin getOrigin(ModuleElement m, ModuleElement.Directive directive) {
 471         switch (directive.getKind()) {
 472             case REQUIRES:
 473                 RequiresDirective rd = cast(RequiresDirective.class, directive);
 474                 if (rd.flags.contains(RequiresFlag.MANDATED))
 475                     return Origin.MANDATED;
 476                 if (rd.flags.contains(RequiresFlag.SYNTHETIC))
 477                     return Origin.SYNTHETIC;
 478                 return Origin.EXPLICIT;
 479             case EXPORTS:
 480                 ExportsDirective ed = cast(ExportsDirective.class, directive);
 481                 if (ed.flags.contains(ExportsFlag.MANDATED))
 482                     return Origin.MANDATED;
 483                 if (ed.flags.contains(ExportsFlag.SYNTHETIC))
 484                     return Origin.SYNTHETIC;
 485                 return Origin.EXPLICIT;
 486             case OPENS:
 487                 OpensDirective od = cast(OpensDirective.class, directive);
 488                 if (od.flags.contains(OpensFlag.MANDATED))
 489                     return Origin.MANDATED;
 490                 if (od.flags.contains(OpensFlag.SYNTHETIC))
 491                     return Origin.SYNTHETIC;
 492                 return Origin.EXPLICIT;
 493         }
 494         return Origin.EXPLICIT;
 495     }
 496 
 497     @DefinedBy(Api.LANGUAGE_MODEL)
 498     public Name getBinaryName(TypeElement type) {
 499         return cast(TypeSymbol.class, type).flatName();
 500     }
 501 
 502     @DefinedBy(Api.LANGUAGE_MODEL)
 503     public Map<MethodSymbol, Attribute> getElementValuesWithDefaults(
 504                                                         AnnotationMirror a) {
 505         Attribute.Compound anno = cast(Attribute.Compound.class, a);
 506         DeclaredType annotype = a.getAnnotationType();
 507         Map<MethodSymbol, Attribute> valmap = anno.getElementValues();
 508 
 509         for (ExecutableElement ex :
 510                  methodsIn(annotype.asElement().getEnclosedElements())) {
 511             MethodSymbol meth = (MethodSymbol) ex;
 512             Attribute defaultValue = meth.getDefaultValue();
 513             if (defaultValue != null && !valmap.containsKey(meth)) {
 514                 valmap.put(meth, defaultValue);
 515             }
 516         }
 517         return valmap;
 518     }
 519 
 520     /**
 521      * {@inheritDoc}
 522      */
 523     @DefinedBy(Api.LANGUAGE_MODEL)
 524     public FilteredMemberList getAllMembers(TypeElement element) {
 525         Symbol sym = cast(Symbol.class, element);
 526         WriteableScope scope = sym.members().dupUnshared();
 527         List<Type> closure = types.closure(sym.asType());
 528         for (Type t : closure)
 529             addMembers(scope, t);
 530         return new FilteredMemberList(scope);
 531     }
 532     // where
 533         private void addMembers(WriteableScope scope, Type type) {
 534             members:
 535             for (Symbol e : type.asElement().members().getSymbols(NON_RECURSIVE)) {
 536                 for (Symbol overrider : scope.getSymbolsByName(e.getSimpleName())) {
 537                     if (overrider.kind == e.kind && (overrider.flags() & Flags.SYNTHETIC) == 0) {
 538                         if (overrider.getKind() == ElementKind.METHOD &&
 539                                 overrides((ExecutableElement)overrider, (ExecutableElement)e, (TypeElement)type.asElement())) {
 540                             continue members;
 541                         }
 542                     }
 543                 }
 544                 boolean derived = e.getEnclosingElement() != scope.owner;
 545                 ElementKind kind = e.getKind();
 546                 boolean initializer = kind == ElementKind.CONSTRUCTOR
 547                     || kind == ElementKind.INSTANCE_INIT
 548                     || kind == ElementKind.STATIC_INIT;
 549                 if (!derived || (!initializer && e.isInheritedIn(scope.owner, types)))
 550                     scope.enter(e);
 551             }
 552         }
 553 
 554     /**
 555      * Returns all annotations of an element, whether
 556      * inherited or directly present.
 557      *
 558      * @param e  the element being examined
 559      * @return all annotations of the element
 560      */
 561     @Override @DefinedBy(Api.LANGUAGE_MODEL)
 562     public List<Attribute.Compound> getAllAnnotationMirrors(Element e) {
 563         Symbol sym = cast(Symbol.class, e);
 564         List<Attribute.Compound> annos = sym.getAnnotationMirrors();
 565         while (sym.getKind() == ElementKind.CLASS) {
 566             Type sup = ((ClassSymbol) sym).getSuperclass();
 567             if (!sup.hasTag(CLASS) || sup.isErroneous() ||
 568                     sup.tsym == syms.objectType.tsym) {
 569                 break;
 570             }
 571             sym = sup.tsym;
 572             List<Attribute.Compound> oldAnnos = annos;
 573             List<Attribute.Compound> newAnnos = sym.getAnnotationMirrors();
 574             for (Attribute.Compound anno : newAnnos) {
 575                 if (isInherited(anno.type) &&
 576                         !containsAnnoOfType(oldAnnos, anno.type)) {
 577                     annos = annos.prepend(anno);
 578                 }
 579             }
 580         }
 581         return annos;
 582     }
 583 
 584     /**
 585      * Tests whether an annotation type is @Inherited.
 586      */
 587     private boolean isInherited(Type annotype) {
 588         return annotype.tsym.attribute(syms.inheritedType.tsym) != null;
 589     }
 590 
 591     /**
 592      * Tests whether a list of annotations contains an annotation
 593      * of a given type.
 594      */
 595     private static boolean containsAnnoOfType(List<Attribute.Compound> annos,
 596                                               Type type) {
 597         for (Attribute.Compound anno : annos) {
 598             if (anno.type.tsym == type.tsym)
 599                 return true;
 600         }
 601         return false;
 602     }
 603 
 604     @DefinedBy(Api.LANGUAGE_MODEL)
 605     public boolean hides(Element hiderEl, Element hideeEl) {
 606         Symbol hider = cast(Symbol.class, hiderEl);
 607         Symbol hidee = cast(Symbol.class, hideeEl);
 608 
 609         // Fields only hide fields; methods only methods; types only types.
 610         // Names must match.  Nothing hides itself (just try it).
 611         if (hider == hidee ||
 612                 hider.kind != hidee.kind ||
 613                 hider.name != hidee.name) {
 614             return false;
 615         }
 616 
 617         // Only static methods can hide other methods.
 618         // Methods only hide methods with matching signatures.
 619         if (hider.kind == MTH) {
 620             if (!hider.isStatic() ||
 621                         !types.isSubSignature(hider.type, hidee.type)) {
 622                 return false;
 623             }
 624         }
 625 
 626         // Hider must be in a subclass of hidee's class.
 627         // Note that if M1 hides M2, and M2 hides M3, and M3 is accessible
 628         // in M1's class, then M1 and M2 both hide M3.
 629         ClassSymbol hiderClass = hider.owner.enclClass();
 630         ClassSymbol hideeClass = hidee.owner.enclClass();
 631         if (hiderClass == null || hideeClass == null ||
 632                 !hiderClass.isSubClass(hideeClass, types)) {
 633             return false;
 634         }
 635 
 636         // Hidee must be accessible in hider's class.
 637         return hidee.isAccessibleIn(hiderClass, types);
 638     }
 639 
 640     @DefinedBy(Api.LANGUAGE_MODEL)
 641     public boolean overrides(ExecutableElement riderEl,
 642                              ExecutableElement rideeEl, TypeElement typeEl) {
 643         MethodSymbol rider = cast(MethodSymbol.class, riderEl);
 644         MethodSymbol ridee = cast(MethodSymbol.class, rideeEl);
 645         ClassSymbol origin = cast(ClassSymbol.class, typeEl);
 646 
 647         return rider.name == ridee.name &&
 648 
 649                // not reflexive as per JLS
 650                rider != ridee &&
 651 
 652                // we don't care if ridee is static, though that wouldn't
 653                // compile
 654                !rider.isStatic() &&
 655 
 656                // Symbol.overrides assumes the following
 657                ridee.isMemberOf(origin, types) &&
 658 
 659                // check access and signatures; don't check return types
 660                rider.overrides(ridee, origin, types, false);
 661     }
 662 
 663     @DefinedBy(Api.LANGUAGE_MODEL)
 664     public String getConstantExpression(Object value) {
 665         return Constants.format(value);
 666     }
 667 
 668     /**
 669      * Print a representation of the elements to the given writer in
 670      * the specified order.  The main purpose of this method is for
 671      * diagnostics.  The exact format of the output is <em>not</em>
 672      * specified and is subject to change.
 673      *
 674      * @param w the writer to print the output to
 675      * @param elements the elements to print
 676      */
 677     @DefinedBy(Api.LANGUAGE_MODEL)
 678     public void printElements(java.io.Writer w, Element... elements) {
 679         for (Element element : elements)
 680             (new PrintingProcessor.PrintingElementVisitor(w, this)).visit(element).flush();
 681     }
 682 
 683     @DefinedBy(Api.LANGUAGE_MODEL)
 684     public Name getName(CharSequence cs) {
 685         return names.fromString(cs.toString());
 686     }
 687 
 688     @Override @DefinedBy(Api.LANGUAGE_MODEL)
 689     public boolean isFunctionalInterface(TypeElement element) {
 690         if (element.getKind() != ElementKind.INTERFACE)
 691             return false;
 692         else {
 693             TypeSymbol tsym = cast(TypeSymbol.class, element);
 694             return types.isFunctionalInterface(tsym);
 695         }
 696     }
 697 
 698     /**
 699      * Returns the tree node and compilation unit corresponding to this
 700      * element, or null if they can't be found.
 701      */
 702     private Pair<JCTree, JCCompilationUnit> getTreeAndTopLevel(Element e) {
 703         Symbol sym = cast(Symbol.class, e);
 704         Env<AttrContext> enterEnv = getEnterEnv(sym);
 705         if (enterEnv == null)
 706             return null;
 707         JCTree tree = TreeInfo.declarationFor(sym, enterEnv.tree);
 708         if (tree == null || enterEnv.toplevel == null)
 709             return null;
 710         return new Pair<>(tree, enterEnv.toplevel);
 711     }
 712 
 713     /**
 714      * Returns the best approximation for the tree node and compilation unit
 715      * corresponding to the given element, annotation and value.
 716      * If the element is null, null is returned.
 717      * If the annotation is null or cannot be found, the tree node and
 718      * compilation unit for the element is returned.
 719      * If the annotation value is null or cannot be found, the tree node and
 720      * compilation unit for the annotation is returned.
 721      */
 722     public Pair<JCTree, JCCompilationUnit> getTreeAndTopLevel(
 723                       Element e, AnnotationMirror a, AnnotationValue v) {
 724         if (e == null)
 725             return null;
 726 
 727         Pair<JCTree, JCCompilationUnit> elemTreeTop = getTreeAndTopLevel(e);
 728         if (elemTreeTop == null)
 729             return null;
 730 
 731         if (a == null)
 732             return elemTreeTop;
 733 
 734         JCTree annoTree = matchAnnoToTree(a, e, elemTreeTop.fst);
 735         if (annoTree == null)
 736             return elemTreeTop;
 737 
 738         if (v == null)
 739             return new Pair<>(annoTree, elemTreeTop.snd);
 740 
 741         JCTree valueTree = matchAttributeToTree(
 742                 cast(Attribute.class, v), cast(Attribute.class, a), annoTree);
 743         if (valueTree == null)
 744             return new Pair<>(annoTree, elemTreeTop.snd);
 745 
 746         return new Pair<>(valueTree, elemTreeTop.snd);
 747     }
 748 
 749     /**
 750      * Returns a symbol's enter environment, or null if it has none.
 751      */
 752     private Env<AttrContext> getEnterEnv(Symbol sym) {
 753         // Get enclosing class of sym, or sym itself if it is a class
 754         // package, or module.
 755         TypeSymbol ts = null;
 756         switch (sym.kind) {
 757             case PCK:
 758                 ts = (PackageSymbol)sym;
 759                 break;
 760             case MDL:
 761                 ts = (ModuleSymbol)sym;
 762                 break;
 763             default:
 764                 ts = sym.enclClass();
 765         }
 766         return (ts != null)
 767                 ? enter.getEnv(ts)
 768                 : null;
 769     }
 770 
 771     private void ensureEntered(String methodName) {
 772         if (javacTaskImpl != null) {
 773             javacTaskImpl.ensureEntered();
 774         }
 775         if (!javaCompiler.isEnterDone()) {
 776             throw new IllegalStateException("Cannot use Elements." + methodName + " before the TaskEvent.Kind.ENTER finished event.");
 777         }
 778     }
 779 
 780     /**
 781      * Returns an object cast to the specified type.
 782      * @throws NullPointerException if the object is {@code null}
 783      * @throws IllegalArgumentException if the object is of the wrong type
 784      */
 785     private static <T> T cast(Class<T> clazz, Object o) {
 786         if (! clazz.isInstance(o))
 787             throw new IllegalArgumentException(o.toString());
 788         return clazz.cast(o);
 789     }
 790 }