1 /*
   2  * Copyright (c) 2005, 2013, 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.element.*;
  33 import javax.lang.model.type.*;
  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 "" 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 type element given its canonical name.
  60      *
  61      * @param name  the canonical name
  62      * @return the named type element, or {@code null} if it cannot be found
  63      */
  64     TypeElement getTypeElement(CharSequence name);
  65 
  66     /**
  67      * Returns the values of an annotation's elements, including defaults.
  68      *
  69      * @see AnnotationMirror#getElementValues()
  70      * @param a  annotation to examine
  71      * @return the values of the annotation's elements, including defaults
  72      */
  73     Map<? extends ExecutableElement, ? extends AnnotationValue>
  74             getElementValuesWithDefaults(AnnotationMirror a);
  75 
  76     /**
  77      * Returns the text of the documentation (&quot;Javadoc&quot;)
  78      * comment of an element.
  79      *
  80      * <p> A documentation comment of an element is a comment that
  81      * begins with "{@code /**}" , ends with a separate
  82      * "<code>*&#47;</code>", and immediately precedes the element,
  83      * ignoring white space.  Therefore, a documentation comment
  84      * contains at least three"{@code *}" characters.  The text
  85      * returned for the documentation comment is a processed form of
  86      * the comment as it appears in source code.  The leading "{@code
  87      * /**}" and trailing "<code>*&#47;</code>" are removed.  For lines
  88      * of the comment starting after the initial "{@code /**}",
  89      * leading white space characters are discarded as are any
  90      * consecutive "{@code *}" characters appearing after the white
  91      * space or starting the line.  The processed lines are then
  92      * concatenated together (including line terminators) and
  93      * returned.
  94      *
  95      * @param e  the element being examined
  96      * @return the documentation comment of the element, or {@code null}
  97      *          if there is none
  98      * @jls 3.6 White Space
  99      */
 100     String getDocComment(Element e);
 101 
 102     /**
 103      * Returns {@code true} if the element is deprecated, {@code false} otherwise.
 104      *
 105      * @param e  the element being examined
 106      * @return {@code true} if the element is deprecated, {@code false} otherwise
 107      */
 108     boolean isDeprecated(Element e);
 109 
 110     /**
 111      * Returns the <i>binary name</i> of a type element.
 112      *
 113      * @param type  the type element being examined
 114      * @return the binary name
 115      *
 116      * @see TypeElement#getQualifiedName
 117      * @jls 13.1 The Form of a Binary
 118      */
 119     Name getBinaryName(TypeElement type);
 120 
 121 
 122     /**
 123      * Returns the package of an element.  The package of a package is
 124      * itself.
 125      *
 126      * @param type the element being examined
 127      * @return the package of an element
 128      */
 129     PackageElement getPackageOf(Element type);
 130 
 131     /**
 132      * Returns all members of a type element, whether inherited or
 133      * declared directly.  For a class the result also includes its
 134      * constructors, but not local or anonymous classes.
 135      *
 136      * <p>Note that elements of certain kinds can be isolated using
 137      * methods in {@link ElementFilter}.
 138      *
 139      * @param type  the type being examined
 140      * @return all members of the type
 141      * @see Element#getEnclosedElements
 142      */
 143     List<? extends Element> getAllMembers(TypeElement type);
 144 
 145     /**
 146      * Returns all annotations <i>present</i> on an element, whether
 147      * directly present or present via inheritance.
 148      *
 149      * @param e  the element being examined
 150      * @return all annotations of the element
 151      * @see Element#getAnnotationMirrors
 152      * @see javax.lang.model.AnnotatedConstruct 
 153      */
 154     List<? extends AnnotationMirror> getAllAnnotationMirrors(Element e);
 155 
 156     /**
 157      * Tests whether one type, method, or field hides another.
 158      *
 159      * @param hider   the first element
 160      * @param hidden  the second element
 161      * @return {@code true} if and only if the first element hides
 162      *          the second
 163      */
 164     boolean hides(Element hider, Element hidden);
 165 
 166     /**
 167      * Tests whether one method, as a member of a given type,
 168      * overrides another method.
 169      * When a non-abstract method overrides an abstract one, the
 170      * former is also said to <i>implement</i> the latter.
 171      *
 172      * <p> In the simplest and most typical usage, the value of the
 173      * {@code type} parameter will simply be the class or interface
 174      * directly enclosing {@code overrider} (the possibly-overriding
 175      * method).  For example, suppose {@code m1} represents the method
 176      * {@code String.hashCode} and {@code m2} represents {@code
 177      * Object.hashCode}.  We can then ask whether {@code m1} overrides
 178      * {@code m2} within the class {@code String} (it does):
 179      *
 180      * <blockquote>
 181      * {@code assert elements.overrides(m1, m2,
 182      *          elements.getTypeElement("java.lang.String")); }
 183      * </blockquote>
 184      *
 185      * A more interesting case can be illustrated by the following example
 186      * in which a method in type {@code A} does not override a
 187      * like-named method in type {@code B}:
 188      *
 189      * <blockquote>
 190      * {@code class A { public void m() {} } }<br>
 191      * {@code interface B { void m(); } }<br>
 192      * ...<br>
 193      * {@code m1 = ...;  // A.m }<br>
 194      * {@code m2 = ...;  // B.m }<br>
 195      * {@code assert ! elements.overrides(m1, m2,
 196      *          elements.getTypeElement("A")); }
 197      * </blockquote>
 198      *
 199      * When viewed as a member of a third type {@code C}, however,
 200      * the method in {@code A} does override the one in {@code B}:
 201      *
 202      * <blockquote>
 203      * {@code class C extends A implements B {} }<br>
 204      * ...<br>
 205      * {@code assert elements.overrides(m1, m2,
 206      *          elements.getTypeElement("C")); }
 207      * </blockquote>
 208      *
 209      * @param overrider  the first method, possible overrider
 210      * @param overridden  the second method, possibly being overridden
 211      * @param type   the type of which the first method is a member
 212      * @return {@code true} if and only if the first method overrides
 213      *          the second
 214      * @jls 8.4.8 Inheritance, Overriding, and Hiding
 215      * @jls 9.4.1 Inheritance and Overriding
 216      */
 217     boolean overrides(ExecutableElement overrider, ExecutableElement overridden,
 218                       TypeElement type);
 219 
 220     /**
 221      * Returns the text of a <i>constant expression</i> representing a
 222      * primitive value or a string.
 223      * The text returned is in a form suitable for representing the value
 224      * in source code.
 225      *
 226      * @param value  a primitive value or string
 227      * @return the text of a constant expression
 228      * @throws IllegalArgumentException if the argument is not a primitive
 229      *          value or string
 230      *
 231      * @see VariableElement#getConstantValue()
 232      */
 233     String getConstantExpression(Object value);
 234 
 235     /**
 236      * Prints a representation of the elements to the given writer in
 237      * the specified order.  The main purpose of this method is for
 238      * diagnostics.  The exact format of the output is <em>not</em>
 239      * specified and is subject to change.
 240      *
 241      * @param w the writer to print the output to
 242      * @param elements the elements to print
 243      */
 244     void printElements(java.io.Writer w, Element... elements);
 245 
 246     /**
 247      * Return a name with the same sequence of characters as the
 248      * argument.
 249      *
 250      * @param cs the character sequence to return as a name
 251      * @return a name with the same sequence of characters as the argument
 252      */
 253     Name getName(CharSequence cs);
 254 
 255     /**
 256      * Returns {@code true} if the type element is a functional interface, {@code false} otherwise.
 257      *
 258      * @param type the type element being examined
 259      * @return {@code true} if the element is a functional interface, {@code false} otherwise
 260      * @jls 9.8 Functional Interfaces
 261      * @since 1.8
 262      */
 263     boolean isFunctionalInterface(TypeElement type);
 264 }