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.
  52      *
  53      * @param name  fully qualified package name, or an empty string for an unnamed package
  54      * @return the named package, or {@code null} if it cannot be found
  55      */
  56     PackageElement getPackageElement(CharSequence name);
  57 
  58     /**
  59      * Returns a package given its fully qualified name, as seen from the given module.
  60      *
  61      * @param name  fully qualified package name, or an empty string for an unnamed package
  62      * @param module module relative to which the lookup should happen
  63      * @return the named package, or {@code null} if it cannot be found
  64      * @since 9
  65      */
  66     PackageElement getPackageElement(ModuleElement module, CharSequence name);
  67 
  68     /**
  69      * Returns a type element given its canonical name.
  70      *
  71      * @param name  the canonical name
  72      * @return the named type element, or {@code null} if it cannot be found
  73      */
  74     TypeElement getTypeElement(CharSequence name);
  75 
  76     /**
  77      * Returns a type element given its canonical name, as seen from the given module.
  78      *
  79      * @param name  the canonical name
  80      * @param module module relative to which the lookup should happen
  81      * @return the named type element, or {@code null} if it cannot be found
  82      * @since 9
  83      */
  84     TypeElement getTypeElement(ModuleElement module, CharSequence name);
  85 
  86     /**
  87      * Returns a module element given its fully qualified name.
  88      *
  89      * @param name  the name
  90      * @return the named module element, or {@code null} if it cannot be found
  91      * @since 9
  92      */
  93     ModuleElement getModuleElement(CharSequence name);
  94 
  95     /**
  96      * Returns the values of an annotation's elements, including defaults.
  97      *
  98      * @see AnnotationMirror#getElementValues()
  99      * @param a  annotation to examine
 100      * @return the values of the annotation's elements, including defaults
 101      */
 102     Map<? extends ExecutableElement, ? extends AnnotationValue>
 103             getElementValuesWithDefaults(AnnotationMirror a);
 104 
 105     /**
 106      * Returns the text of the documentation (&quot;Javadoc&quot;)
 107      * comment of an element.
 108      *
 109      * <p> A documentation comment of an element is a comment that
 110      * begins with "{@code /**}" , ends with a separate
 111      * "<code>*/</code>", and immediately precedes the element,
 112      * ignoring white space.  Therefore, a documentation comment
 113      * contains at least three"{@code *}" characters.  The text
 114      * returned for the documentation comment is a processed form of
 115      * the comment as it appears in source code.  The leading "{@code
 116      * /**}" and trailing "<code>*/</code>" are removed.  For lines
 117      * of the comment starting after the initial "{@code /**}",
 118      * leading white space characters are discarded as are any
 119      * consecutive "{@code *}" characters appearing after the white
 120      * space or starting the line.  The processed lines are then
 121      * concatenated together (including line terminators) and
 122      * returned.
 123      *
 124      * @param e  the element being examined
 125      * @return the documentation comment of the element, or {@code null}
 126      *          if there is none
 127      * @jls 3.6 White Space
 128      */
 129     String getDocComment(Element e);
 130 
 131     /**
 132      * Returns {@code true} if the element is deprecated, {@code false} otherwise.
 133      *
 134      * @param e  the element being examined
 135      * @return {@code true} if the element is deprecated, {@code false} otherwise
 136      */
 137     boolean isDeprecated(Element e);
 138 
 139     /**
 140      * Returns {@code true} if the element is mandated, {@code false} otherwise.
 141      *
 142      * A mandated construct is not explicitly declared in the source
 143      * code, but its presence is mandated by the specification to be
 144      * implicitly declared. One example of a mandated construct is a
 145      * <em>default constructor</em> in a class that contains no
 146      * constructor declarations.
 147      *
 148      * @implSpec The default implementation of this method returns {@code false}.
 149      *
 150      * @param e  the element being examined
 151      * @return {@code true} if the element is mandated, {@code false} otherwise
 152      * @jls 8.8.9 Default Constructor 
 153      * @jls 13.1. The Form of a Binary
 154      * @since 9
 155      */
 156     default boolean isMandated(Element e) {
 157         return false;
 158     }
 159 
 160     /**
 161      * Returns {@code true} if the annotation mirror is mandated,
 162      * {@code false} otherwise.
 163      *
 164      * A mandated construct is not explicitly declared in the source
 165      * code, but its presence is mandated by the specification to be
 166      * implicitly declared. One example of a mandated construct is the
 167      * implicitly declared <em>container annotation</em> used to hold
 168      * repeated base annotations of a repeatable annotation type.
 169      *
 170      * @implSpec The default implementation of this method returns {@code false}.
 171      *
 172 
 173      * @param c the construct the annotation mirror modifies
 174      * @param a  the annotation mirror being examined
 175      * @return {@code true} if the element is mandated, {@code false} otherwise
 176      * @jls 9.6.3. Repeatable Annotation Types
 177      * @jls 9.7.5 Multiple Annotations of the Same Type
 178      * @since 9
 179      */
 180     default boolean isMandated(AnnotatedConstruct c, AnnotationMirror a) {
 181         return false;
 182     }
 183 
 184     /**
 185      * Returns {@code true} if the element is synthetic, {@code false} otherwise.
 186      *
 187      * A synthetic element corresponds to a construct neither
 188      * implicitly nor explicitly declared in source code.
 189      *
 190      * @implSpec The default implementation of this method returns {@code false}.
 191      *
 192      * @param e  the element being examined
 193      * @return {@code true} if the element is synthetic, {@code false} otherwise
 194      * @jls 13.1. The Form of a Binary
 195      * @since 9
 196      */
 197     default boolean isSynthetic(Element e) {
 198         return false;
 199     }
 200 
 201     /**
 202      * Returns the <i>binary name</i> of a type element.
 203      *
 204      * @param type  the type element being examined
 205      * @return the binary name
 206      *
 207      * @see TypeElement#getQualifiedName
 208      * @jls 13.1 The Form of a Binary
 209      */
 210     Name getBinaryName(TypeElement type);
 211 
 212 
 213     /**
 214      * Returns the package of an element.  The package of a package is
 215      * itself.
 216      *
 217      * @param type the element being examined
 218      * @return the package of an element
 219      */
 220     PackageElement getPackageOf(Element type);
 221 
 222     /**
 223      * Returns the module of an element.  The module of a module is
 224      * itself.
 225      *
 226      * @param type the element being examined
 227      * @return the module of an element
 228      * @since 9
 229      */
 230     ModuleElement getModuleOf(Element type);
 231 
 232     /**
 233      * Returns all members of a type element, whether inherited or
 234      * declared directly.  For a class the result also includes its
 235      * constructors, but not local or anonymous classes.
 236      *
 237      * <p>Note that elements of certain kinds can be isolated using
 238      * methods in {@link ElementFilter}.
 239      *
 240      * @param type  the type being examined
 241      * @return all members of the type
 242      * @see Element#getEnclosedElements
 243      */
 244     List<? extends Element> getAllMembers(TypeElement type);
 245 
 246     /**
 247      * Returns all annotations <i>present</i> on an element, whether
 248      * directly present or present via inheritance.
 249      *
 250      * @param e  the element being examined
 251      * @return all annotations of the element
 252      * @see Element#getAnnotationMirrors
 253      * @see javax.lang.model.AnnotatedConstruct
 254      */
 255     List<? extends AnnotationMirror> getAllAnnotationMirrors(Element e);
 256 
 257     /**
 258      * Tests whether one type, method, or field hides another.
 259      *
 260      * @param hider   the first element
 261      * @param hidden  the second element
 262      * @return {@code true} if and only if the first element hides
 263      *          the second
 264      */
 265     boolean hides(Element hider, Element hidden);
 266 
 267     /**
 268      * Tests whether one method, as a member of a given type,
 269      * overrides another method.
 270      * When a non-abstract method overrides an abstract one, the
 271      * former is also said to <i>implement</i> the latter.
 272      *
 273      * <p> In the simplest and most typical usage, the value of the
 274      * {@code type} parameter will simply be the class or interface
 275      * directly enclosing {@code overrider} (the possibly-overriding
 276      * method).  For example, suppose {@code m1} represents the method
 277      * {@code String.hashCode} and {@code m2} represents {@code
 278      * Object.hashCode}.  We can then ask whether {@code m1} overrides
 279      * {@code m2} within the class {@code String} (it does):
 280      *
 281      * <blockquote>
 282      * {@code assert elements.overrides(m1, m2,
 283      *          elements.getTypeElement("java.lang.String")); }
 284      * </blockquote>
 285      *
 286      * A more interesting case can be illustrated by the following example
 287      * in which a method in type {@code A} does not override a
 288      * like-named method in type {@code B}:
 289      *
 290      * <blockquote>
 291      * {@code class A { public void m() {} } }<br>
 292      * {@code interface B { void m(); } }<br>
 293      * ...<br>
 294      * {@code m1 = ...;  // A.m }<br>
 295      * {@code m2 = ...;  // B.m }<br>
 296      * {@code assert ! elements.overrides(m1, m2,
 297      *          elements.getTypeElement("A")); }
 298      * </blockquote>
 299      *
 300      * When viewed as a member of a third type {@code C}, however,
 301      * the method in {@code A} does override the one in {@code B}:
 302      *
 303      * <blockquote>
 304      * {@code class C extends A implements B {} }<br>
 305      * ...<br>
 306      * {@code assert elements.overrides(m1, m2,
 307      *          elements.getTypeElement("C")); }
 308      * </blockquote>
 309      *
 310      * @param overrider  the first method, possible overrider
 311      * @param overridden  the second method, possibly being overridden
 312      * @param type   the type of which the first method is a member
 313      * @return {@code true} if and only if the first method overrides
 314      *          the second
 315      * @jls 8.4.8 Inheritance, Overriding, and Hiding
 316      * @jls 9.4.1 Inheritance and Overriding
 317      */
 318     boolean overrides(ExecutableElement overrider, ExecutableElement overridden,
 319                       TypeElement type);
 320 
 321     /**
 322      * Returns the text of a <i>constant expression</i> representing a
 323      * primitive value or a string.
 324      * The text returned is in a form suitable for representing the value
 325      * in source code.
 326      *
 327      * @param value  a primitive value or string
 328      * @return the text of a constant expression
 329      * @throws IllegalArgumentException if the argument is not a primitive
 330      *          value or string
 331      *
 332      * @see VariableElement#getConstantValue()
 333      */
 334     String getConstantExpression(Object value);
 335 
 336     /**
 337      * Prints a representation of the elements to the given writer in
 338      * the specified order.  The main purpose of this method is for
 339      * diagnostics.  The exact format of the output is <em>not</em>
 340      * specified and is subject to change.
 341      *
 342      * @param w the writer to print the output to
 343      * @param elements the elements to print
 344      */
 345     void printElements(java.io.Writer w, Element... elements);
 346 
 347     /**
 348      * Return a name with the same sequence of characters as the
 349      * argument.
 350      *
 351      * @param cs the character sequence to return as a name
 352      * @return a name with the same sequence of characters as the argument
 353      */
 354     Name getName(CharSequence cs);
 355 
 356     /**
 357      * Returns {@code true} if the type element is a functional interface, {@code false} otherwise.
 358      *
 359      * @param type the type element being examined
 360      * @return {@code true} if the element is a functional interface, {@code false} otherwise
 361      * @jls 9.8 Functional Interfaces
 362      * @since 1.8
 363      */
 364     boolean isFunctionalInterface(TypeElement type);
 365 }