001    /*
002     * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
003     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004     *
005     * This code is free software; you can redistribute it and/or modify it
006     * under the terms of the GNU General Public License version 2 only, as
007     * published by the Free Software Foundation.  Sun designates this
008     * particular file as subject to the "Classpath" exception as provided
009     * by Sun in the LICENSE file that accompanied this code.
010     *
011     * This code is distributed in the hope that it will be useful, but WITHOUT
012     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013     * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014     * version 2 for more details (a copy is included in the LICENSE file that
015     * accompanied this code).
016     *
017     * You should have received a copy of the GNU General Public License version
018     * 2 along with this work; if not, write to the Free Software Foundation,
019     * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020     *
021     * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022     * CA 95054 USA or visit www.sun.com if you need additional information or
023     * have any questions.
024     */
025    
026    package javax.lang.model.util;
027    
028    
029    import java.util.List;
030    import java.util.Map;
031    
032    import javax.lang.model.element.*;
033    import javax.lang.model.type.*;
034    
035    
036    /**
037     * Utility methods for operating on program elements.
038     *
039     * <p><b>Compatibility Note:</b> Methods may be added to this interface
040     * in future releases of the platform.
041     *
042     * @author Joseph D. Darcy
043     * @author Scott Seligman
044     * @author Peter von der Ah&eacute;
045     * @see javax.annotation.processing.ProcessingEnvironment#getElementUtils
046     * @since 1.6
047     */
048    public interface Elements {
049    
050        /**
051         * Returns a package given its fully qualified name.
052         *
053         * @param name  fully qualified package name, or "" for an unnamed package
054         * @return the named package, or {@code null} if it cannot be found
055         */
056        PackageElement getPackageElement(CharSequence name);
057    
058        /**
059         * Returns a type element given its canonical name.
060         *
061         * @param name  the canonical name
062         * @return the named type element, or {@code null} if it cannot be found
063         */
064        TypeElement getTypeElement(CharSequence name);
065    
066        /**
067         * Returns the values of an annotation's elements, including defaults.
068         *
069         * @see AnnotationMirror#getElementValues()
070         * @param a  annotation to examine
071         * @return the values of the annotation's elements, including defaults
072         */
073        Map<? extends ExecutableElement, ? extends AnnotationValue>
074                getElementValuesWithDefaults(AnnotationMirror a);
075    
076        /**
077         * Returns the text of the documentation (&quot;Javadoc&quot;)
078         * comment of an element.
079         *
080         * @param e  the element being examined
081         * @return the documentation comment of the element, or {@code null}
082         *          if there is none
083         */
084        String getDocComment(Element e);
085    
086        /**
087         * Returns {@code true} if the element is deprecated, {@code false} otherwise.
088         *
089         * @param e  the element being examined
090         * @return {@code true} if the element is deprecated, {@code false} otherwise
091         */
092        boolean isDeprecated(Element e);
093    
094        /**
095         * Returns the <i>binary name</i> of a type element.
096         *
097         * @param type  the type element being examined
098         * @return the binary name
099         *
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    }