1 /*
   2  * Copyright (c) 2005, 2016, 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 javax.lang.model.util;
  27 
  28 
  29 import java.util.List;
  30 import java.util.Map;
  31 
  32 import javax.lang.model.AnnotatedConstruct;
  33 import javax.lang.model.element.*;
  34 
  35 
  36 /**
  37  * Utility methods for operating on program elements.
  38  *
  39  * <p><b>Compatibility Note:</b> Methods may be added to this interface
  40  * in future releases of the platform.
  41  *
  42  * @author Joseph D. Darcy
  43  * @author Scott Seligman
  44  * @author Peter von der Ah&eacute;
  45  * @see javax.annotation.processing.ProcessingEnvironment#getElementUtils
  46  * @since 1.6
  47  */
  48 public interface Elements {
  49 
  50     /**
  51      * Returns a package given its fully qualified name if the package is unique in the environment.
  52      * If running with modules, all modules in the modules graph are searched for matching packages.
  53      *
  54      * @param name  fully qualified package name, or an empty string for an unnamed package
  55      * @return the specified package, or {@code null} if it cannot be uniquely found
  56      */
  57     PackageElement getPackageElement(CharSequence name);
  58 
  59     /**
  60      * Returns a package given its fully qualified name, as seen from the given module.
  61      *
  62      * @param name  fully qualified package name, or an empty string for an unnamed package
  63      * @param module module relative to which the lookup should happen
  64      * @return the specified package, or {@code null} if it cannot be found
  65      * @since 9
  66      */
  67     PackageElement getPackageElement(ModuleElement module, CharSequence name);
  68 
  69     /**
  70      * Returns a type element given its canonical name if the type element is unique in the environment.
  71      * If running with modules, all modules in the modules graph are searched for matching
  72      * type elements.
  73      *
  74      * @param name  the canonical name
  75      * @return the named type element, or {@code null} if it cannot be uniquely found
  76      */
  77     TypeElement getTypeElement(CharSequence name);
  78 
  79     /**
  80      * Returns a type element given its canonical name, as seen from the given module.
  81      *
  82      * @param name  the canonical name
  83      * @param module module relative to which the lookup should happen
  84      * @return the named type element, or {@code null} if it cannot be found
  85      * @since 9
  86      */
  87     TypeElement getTypeElement(ModuleElement module, CharSequence name);
  88 
  89     /**
  90      * Returns a module element given its fully qualified name.
  91      * If the named module cannot be found, null is returned. One situation where a module
  92      * cannot be found is if the environment does not include modules, such as
  93      * an annotation processing environment configured for
  94      * a {@linkplain
  95      * javax.annotation.processing.ProcessingEnvironment#getSourceVersion
  96      * source version} without modules.
  97      *
  98      * @param name  the name
  99      * @return the named module element, or {@code null} if it cannot be found
 100      * @since 9
 101      * @spec JPMS
 102      */
 103     ModuleElement getModuleElement(CharSequence name);
 104 
 105     /**
 106      * Returns the values of an annotation's elements, including defaults.
 107      *
 108      * @see AnnotationMirror#getElementValues()
 109      * @param a  annotation to examine
 110      * @return the values of the annotation's elements, including defaults
 111      */
 112     Map<? extends ExecutableElement, ? extends AnnotationValue>
 113             getElementValuesWithDefaults(AnnotationMirror a);
 114 
 115     /**
 116      * Returns the text of the documentation (&quot;Javadoc&quot;)
 117      * comment of an element.
 118      *
 119      * <p> A documentation comment of an element is a comment that
 120      * begins with "{@code /**}" , ends with a separate
 121      * "<code>*/</code>", and immediately precedes the element,
 122      * ignoring white space.  Therefore, a documentation comment
 123      * contains at least three"{@code *}" characters.  The text
 124      * returned for the documentation comment is a processed form of
 125      * the comment as it appears in source code.  The leading "{@code
 126      * /**}" and trailing "<code>*/</code>" are removed.  For lines
 127      * of the comment starting after the initial "{@code /**}",
 128      * leading white space characters are discarded as are any
 129      * consecutive "{@code *}" characters appearing after the white
 130      * space or starting the line.  The processed lines are then
 131      * concatenated together (including line terminators) and
 132      * returned.
 133      *
 134      * @param e  the element being examined
 135      * @return the documentation comment of the element, or {@code null}
 136      *          if there is none
 137      * @jls 3.6 White Space
 138      */
 139     String getDocComment(Element e);
 140 
 141     /**
 142      * Returns {@code true} if the element is deprecated, {@code false} otherwise.
 143      *
 144      * @param e  the element being examined
 145      * @return {@code true} if the element is deprecated, {@code false} otherwise
 146      */
 147     boolean isDeprecated(Element e);
 148 
 149     /**
 150      * Returns the <em>origin</em> of the given element.
 151      *
 152      * <p>Note that if this method returns {@link Origin#EXPLICIT
 153      * EXPLICIT} and the element was created from a class file, then
 154      * the element may not, in fact, correspond to an explicitly
 155      * declared construct in source code. This is due to limitations
 156      * of the fidelity of the class file format in preserving
 157      * information from source code. For example, at least some
 158      * versions of the class file format do not preserve whether a
 159      * constructor was explicitly declared by the programmer or was
 160      * implicitly declared as the <em>default constructor</em>.
 161      *
 162      * @implSpec The default implementation of this method returns
 163      * {@link Origin#EXPLICIT EXPLICIT}.
 164      *
 165      * @param e  the element being examined
 166      * @return the origin of the given element
 167      * @since 9
 168      */
 169     default Origin getOrigin(Element e) {
 170         return Origin.EXPLICIT;
 171     }
 172 
 173     /**
 174      * Returns the <em>origin</em> of the given annotation mirror.
 175      *
 176      * An annotation mirror is {@linkplain Origin#MANDATED mandated}
 177      * if it is an implicitly declared <em>container annotation</em>
 178      * used to hold repeated annotations of a repeatable annotation
 179      * type.
 180      *
 181      * <p>Note that if this method returns {@link Origin#EXPLICIT
 182      * EXPLICIT} and the annotation mirror was created from a class
 183      * file, then the element may not, in fact, correspond to an
 184      * explicitly declared construct in source code. This is due to
 185      * limitations of the fidelity of the class file format in
 186      * preserving information from source code. For example, at least
 187      * some versions of the class file format do not preserve whether
 188      * an annotation was explicitly declared by the programmer or was
 189      * implicitly declared as a <em>container annotation</em>.
 190      *
 191      * @implSpec The default implementation of this method returns
 192      * {@link Origin#EXPLICIT EXPLICIT}.
 193      *
 194      * @param c the construct the annotation mirror modifies
 195      * @param a the annotation mirror being examined
 196      * @return the origin of the given annotation mirror
 197      * @jls 9.6.3 Repeatable Annotation Types
 198      * @jls 9.7.5 Multiple Annotations of the Same Type
 199      * @since 9
 200      */
 201     default Origin getOrigin(AnnotatedConstruct c,
 202                              AnnotationMirror a) {
 203         return Origin.EXPLICIT;
 204     }
 205 
 206     /**
 207      * Returns the <em>origin</em> of the given module directive.
 208      *
 209      * <p>Note that if this method returns {@link Origin#EXPLICIT
 210      * EXPLICIT} and the module directive was created from a class
 211      * file, then the module directive may not, in fact, correspond to
 212      * an explicitly declared construct in source code. This is due to
 213      * limitations of the fidelity of the class file format in
 214      * preserving information from source code. For example, at least
 215      * some versions of the class file format do not preserve whether
 216      * a {@code uses} directive was explicitly declared by the
 217      * programmer or was added as a synthetic construct.
 218      *
 219      * <p>Note that an implementation may not be able to reliably
 220      * determine the origin status of the directive if the directive
 221      * is created from a class file due to limitations of the fidelity
 222      * of the class file format in preserving information from source
 223      * code.
 224      *
 225      * @implSpec The default implementation of this method returns
 226      * {@link Origin#EXPLICIT EXPLICIT}.
 227      *
 228      * @param m the module of the directive
 229      * @param directive  the module directive being examined
 230      * @return the origin of the given directive
 231      * @since 9
 232      */
 233     default Origin getOrigin(ModuleElement m,
 234                              ModuleElement.Directive directive) {
 235         return Origin.EXPLICIT;
 236     }
 237 
 238     /**
 239      * The <em>origin</em> of an element or other language model
 240      * item. The origin of an element or item models how a construct
 241      * in a program is declared in the source code, explicitly,
 242      * implicitly, etc.
 243      *
 244      * <p>Note that it is possible additional kinds of origin values
 245      * will be added in future versions of the platform.
 246      *
 247      * @jls 13.1 The Form of a Binary
 248      * @since 9
 249      */
 250     public enum Origin {
 251         /**
 252          * Describes a construct explicitly declared in source code.
 253          */
 254         EXPLICIT,
 255 
 256        /**
 257          * A mandated construct is one that is not explicitly declared
 258          * in the source code, but whose presence is mandated by the
 259          * specification. Such a construct is said to be implicitly
 260          * declared.
 261          *
 262          * One example of a mandated element is a <em>default
 263          * constructor</em> in a class that contains no explicit
 264          * constructor declarations.
 265          *
 266          * Another example of a mandated construct is an implicitly
 267          * declared <em>container annotation</em> used to hold
 268          * multiple annotations of a repeatable annotation type.
 269          *
 270          * @jls 8.8.9 Default Constructor
 271          * @jls 9.6.3 Repeatable Annotation Types
 272          * @jls 9.7.5 Multiple Annotations of the Same Type
 273          */
 274         MANDATED,
 275 
 276        /**
 277          * A synthetic construct is one that is neither implicitly nor
 278          * explicitly declared in the source code. Such a construct is
 279          * typically a translation artifact created by a compiler.
 280          */
 281         SYNTHETIC;
 282 
 283         /**
 284          * Returns {@code true} for values corresponding to constructs
 285          * that are implicitly or explicitly declared, {@code false}
 286          * otherwise.
 287          * @return {@code true} for {@link EXPLICIT} and {@link
 288          * MANDATED}, {@code false} otherwise.
 289          */
 290         public boolean isDeclared() {
 291             return this != SYNTHETIC;
 292         }
 293     }
 294 
 295     /**
 296      * Returns {@code true} if the executable element is a bridge
 297      * method, {@code false} otherwise.
 298      *
 299      * @implSpec The default implementation of this method returns {@code false}.
 300      *
 301      * @param e  the executable being examined
 302      * @return {@code true} if the executable element is a bridge
 303      * method, {@code false} otherwise
 304      * @since 9
 305      */
 306     default boolean isBridge(ExecutableElement e) {
 307         return false;
 308     }
 309 
 310     /**
 311      * Returns the <i>binary name</i> of a type element.
 312      *
 313      * @param type  the type element being examined
 314      * @return the binary name
 315      *
 316      * @see TypeElement#getQualifiedName
 317      * @jls 13.1 The Form of a Binary
 318      */
 319     Name getBinaryName(TypeElement type);
 320 
 321 
 322     /**
 323      * Returns the package of an element.  The package of a package is
 324      * itself.
 325      *
 326      * @param type the element being examined
 327      * @return the package of an element
 328      */
 329     PackageElement getPackageOf(Element type);
 330 
 331     /**
 332      * Returns the module of an element.  The module of a module is
 333      * itself.
 334      * If there is no module for the element, null is returned. One situation where there is
 335      * no module for an element is if the environment does not include modules, such as
 336      * an annotation processing environment configured for
 337      * a {@linkplain
 338      * javax.annotation.processing.ProcessingEnvironment#getSourceVersion
 339      * source version} without modules.
 340      *
 341      * @param type the element being examined
 342      * @return the module of an element
 343      * @since 9
 344      * @spec JPMS
 345      */
 346     ModuleElement getModuleOf(Element type);
 347 
 348     /**
 349      * Returns all members of a type element, whether inherited or
 350      * declared directly.  For a class the result also includes its
 351      * constructors, but not local or anonymous classes.
 352      *
 353      * <p>Note that elements of certain kinds can be isolated using
 354      * methods in {@link ElementFilter}.
 355      *
 356      * @param type  the type being examined
 357      * @return all members of the type
 358      * @see Element#getEnclosedElements
 359      */
 360     List<? extends Element> getAllMembers(TypeElement type);
 361 
 362     /**
 363      * Returns all annotations <i>present</i> on an element, whether
 364      * directly present or present via inheritance.
 365      *
 366      * @param e  the element being examined
 367      * @return all annotations of the element
 368      * @see Element#getAnnotationMirrors
 369      * @see javax.lang.model.AnnotatedConstruct
 370      */
 371     List<? extends AnnotationMirror> getAllAnnotationMirrors(Element e);
 372 
 373     /**
 374      * Tests whether one type, method, or field hides another.
 375      *
 376      * @param hider   the first element
 377      * @param hidden  the second element
 378      * @return {@code true} if and only if the first element hides
 379      *          the second
 380      */
 381     boolean hides(Element hider, Element hidden);
 382 
 383     /**
 384      * Tests whether one method, as a member of a given type,
 385      * overrides another method.
 386      * When a non-abstract method overrides an abstract one, the
 387      * former is also said to <i>implement</i> the latter.
 388      *
 389      * <p> In the simplest and most typical usage, the value of the
 390      * {@code type} parameter will simply be the class or interface
 391      * directly enclosing {@code overrider} (the possibly-overriding
 392      * method).  For example, suppose {@code m1} represents the method
 393      * {@code String.hashCode} and {@code m2} represents {@code
 394      * Object.hashCode}.  We can then ask whether {@code m1} overrides
 395      * {@code m2} within the class {@code String} (it does):
 396      *
 397      * <blockquote>
 398      * {@code assert elements.overrides(m1, m2,
 399      *          elements.getTypeElement("java.lang.String")); }
 400      * </blockquote>
 401      *
 402      * A more interesting case can be illustrated by the following example
 403      * in which a method in type {@code A} does not override a
 404      * like-named method in type {@code B}:
 405      *
 406      * <blockquote>
 407      * {@code class A { public void m() {} } }<br>
 408      * {@code interface B { void m(); } }<br>
 409      * ...<br>
 410      * {@code m1 = ...;  // A.m }<br>
 411      * {@code m2 = ...;  // B.m }<br>
 412      * {@code assert ! elements.overrides(m1, m2,
 413      *          elements.getTypeElement("A")); }
 414      * </blockquote>
 415      *
 416      * When viewed as a member of a third type {@code C}, however,
 417      * the method in {@code A} does override the one in {@code B}:
 418      *
 419      * <blockquote>
 420      * {@code class C extends A implements B {} }<br>
 421      * ...<br>
 422      * {@code assert elements.overrides(m1, m2,
 423      *          elements.getTypeElement("C")); }
 424      * </blockquote>
 425      *
 426      * @param overrider  the first method, possible overrider
 427      * @param overridden  the second method, possibly being overridden
 428      * @param type   the type of which the first method is a member
 429      * @return {@code true} if and only if the first method overrides
 430      *          the second
 431      * @jls 8.4.8 Inheritance, Overriding, and Hiding
 432      * @jls 9.4.1 Inheritance and Overriding
 433      */
 434     boolean overrides(ExecutableElement overrider, ExecutableElement overridden,
 435                       TypeElement type);
 436 
 437     /**
 438      * Returns the text of a <i>constant expression</i> representing a
 439      * primitive value or a string.
 440      * The text returned is in a form suitable for representing the value
 441      * in source code.
 442      *
 443      * @param value  a primitive value or string
 444      * @return the text of a constant expression
 445      * @throws IllegalArgumentException if the argument is not a primitive
 446      *          value or string
 447      *
 448      * @see VariableElement#getConstantValue()
 449      */
 450     String getConstantExpression(Object value);
 451 
 452     /**
 453      * Prints a representation of the elements to the given writer in
 454      * the specified order.  The main purpose of this method is for
 455      * diagnostics.  The exact format of the output is <em>not</em>
 456      * specified and is subject to change.
 457      *
 458      * @param w the writer to print the output to
 459      * @param elements the elements to print
 460      */
 461     void printElements(java.io.Writer w, Element... elements);
 462 
 463     /**
 464      * Return a name with the same sequence of characters as the
 465      * argument.
 466      *
 467      * @param cs the character sequence to return as a name
 468      * @return a name with the same sequence of characters as the argument
 469      */
 470     Name getName(CharSequence cs);
 471 
 472     /**
 473      * Returns {@code true} if the type element is a functional interface, {@code false} otherwise.
 474      *
 475      * @param type the type element being examined
 476      * @return {@code true} if the element is a functional interface, {@code false} otherwise
 477      * @jls 9.8 Functional Interfaces
 478      * @since 1.8
 479      */
 480     boolean isFunctionalInterface(TypeElement type);
 481 }