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