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