1 /*
   2  * Copyright 2005-2006 Sun Microsystems, Inc.  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.  Sun designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22  * CA 95054 USA or visit www.sun.com if you need additional information or
  23  * have any 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      * @param e  the element being examined
  81      * @return the documentation comment of the element, or {@code null}
  82      *          if there is none
  83      */
  84     String getDocComment(Element e);
  85 
  86     /**
  87      * Returns {@code true} if the element is deprecated, {@code false} otherwise.
  88      *
  89      * @param e  the element being examined
  90      * @return {@code true} if the element is deprecated, {@code false} otherwise
  91      */
  92     boolean isDeprecated(Element e);
  93 
  94     /**
  95      * Returns the <i>binary name</i> of a type element.
  96      *
  97      * @param type  the type element being examined
  98      * @return the binary name
  99      *
 100      * @see TypeElement#getQualifiedName
 101      * @jls3 13.1 The Form of a Binary
 102      */
 103     Name getBinaryName(TypeElement type);
 104 
 105 
 106     /**
 107      * Returns the package of an element.  The package of a package is
 108      * itself.
 109      *
 110      * @param type the element being examined
 111      * @return the package of an element
 112      */
 113     PackageElement getPackageOf(Element type);
 114 
 115     /**
 116      * Returns all members of a type element, whether inherited or
 117      * declared directly.  For a class the result also includes its
 118      * constructors, but not local or anonymous classes.
 119      *
 120      * <p>Note that elements of certain kinds can be isolated using
 121      * methods in {@link ElementFilter}.
 122      *
 123      * @param type  the type being examined
 124      * @return all members of the type
 125      * @see Element#getEnclosedElements
 126      */
 127     List<? extends Element> getAllMembers(TypeElement type);
 128 
 129     /**
 130      * Returns all annotations of an element, whether
 131      * inherited or directly present.
 132      *
 133      * @param e  the element being examined
 134      * @return all annotations of the element
 135      * @see Element#getAnnotationMirrors
 136      */
 137     List<? extends AnnotationMirror> getAllAnnotationMirrors(Element e);
 138 
 139     /**
 140      * Tests whether one type, method, or field hides another.
 141      *
 142      * @param hider   the first element
 143      * @param hidden  the second element
 144      * @return {@code true} if and only if the first element hides
 145      *          the second
 146      */
 147     boolean hides(Element hider, Element hidden);
 148 
 149     /**
 150      * Tests whether one method, as a member of a given type,
 151      * overrides another method.
 152      * When a non-abstract method overrides an abstract one, the
 153      * former is also said to <i>implement</i> the latter.
 154      *
 155      * <p> In the simplest and most typical usage, the value of the
 156      * {@code type} parameter will simply be the class or interface
 157      * directly enclosing {@code overrider} (the possibly-overriding
 158      * method).  For example, suppose {@code m1} represents the method
 159      * {@code String.hashCode} and {@code m2} represents {@code
 160      * Object.hashCode}.  We can then ask whether {@code m1} overrides
 161      * {@code m2} within the class {@code String} (it does):
 162      *
 163      * <blockquote>
 164      * {@code assert elements.overrides(m1, m2,
 165      *          elements.getTypeElement("java.lang.String")); }
 166      * </blockquote>
 167      *
 168      * A more interesting case can be illustrated by the following example
 169      * in which a method in type {@code A} does not override a
 170      * like-named method in type {@code B}:
 171      *
 172      * <blockquote>
 173      * {@code class A { public void m() {} } }<br>
 174      * {@code interface B { void m(); } }<br>
 175      * ...<br>
 176      * {@code m1 = ...;  // A.m }<br>
 177      * {@code m2 = ...;  // B.m }<br>
 178      * {@code assert ! elements.overrides(m1, m2,
 179      *          elements.getTypeElement("A")); }
 180      * </blockquote>
 181      *
 182      * When viewed as a member of a third type {@code C}, however,
 183      * the method in {@code A} does override the one in {@code B}:
 184      *
 185      * <blockquote>
 186      * {@code class C extends A implements B {} }<br>
 187      * ...<br>
 188      * {@code assert elements.overrides(m1, m2,
 189      *          elements.getTypeElement("C")); }
 190      * </blockquote>
 191      *
 192      * @param overrider  the first method, possible overrider
 193      * @param overridden  the second method, possibly being overridden
 194      * @param type   the type of which the first method is a member
 195      * @return {@code true} if and only if the first method overrides
 196      *          the second
 197      * @jls3 8.4.8 Inheritance, Overriding, and Hiding
 198      * @jls3 9.4.1 Inheritance and Overriding
 199      */
 200     boolean overrides(ExecutableElement overrider, ExecutableElement overridden,
 201                       TypeElement type);
 202 
 203     /**
 204      * Returns the text of a <i>constant expression</i> representing a
 205      * primitive value or a string.
 206      * The text returned is in a form suitable for representing the value
 207      * in source code.
 208      *
 209      * @param value  a primitive value or string
 210      * @return the text of a constant expression
 211      * @throws IllegalArgumentException if the argument is not a primitive
 212      *          value or string
 213      *
 214      * @see VariableElement#getConstantValue()
 215      */
 216     String getConstantExpression(Object value);
 217 
 218     /**
 219      * Prints a representation of the elements to the given writer in
 220      * the specified order.  The main purpose of this method is for
 221      * diagnostics.  The exact format of the output is <em>not</em>
 222      * specified and is subject to change.
 223      *
 224      * @param w the writer to print the output to
 225      * @param elements the elements to print
 226      */
 227     void printElements(java.io.Writer w, Element... elements);
 228 
 229     /**
 230      * Return a name with the same sequence of characters as the
 231      * argument.
 232      *
 233      * @param cs the character sequence to return as a name
 234      */
 235     Name getName(CharSequence cs);
 236 }