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