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