/* * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package javax.lang.model.util; import java.util.List; import java.util.Map; import javax.lang.model.AnnotatedConstruct; import javax.lang.model.element.*; /** * Utility methods for operating on program elements. * *

Compatibility Note: Methods may be added to this interface * in future releases of the platform. * * @author Joseph D. Darcy * @author Scott Seligman * @author Peter von der Ahé * @see javax.annotation.processing.ProcessingEnvironment#getElementUtils * @since 1.6 */ public interface Elements { /** * Returns a package given its fully qualified name. * * @param name fully qualified package name, or an empty string for an unnamed package * @return the named package, or {@code null} if it cannot be found */ PackageElement getPackageElement(CharSequence name); /** * Returns a package given its fully qualified name, as seen from the given module. * * @param name fully qualified package name, or an empty string for an unnamed package * @param module module relative to which the lookup should happen * @return the named package, or {@code null} if it cannot be found * @since 9 */ PackageElement getPackageElement(ModuleElement module, CharSequence name); /** * Returns a type element given its canonical name. * * @param name the canonical name * @return the named type element, or {@code null} if it cannot be found */ TypeElement getTypeElement(CharSequence name); /** * Returns a type element given its canonical name, as seen from the given module. * * @param name the canonical name * @param module module relative to which the lookup should happen * @return the named type element, or {@code null} if it cannot be found * @since 9 */ TypeElement getTypeElement(ModuleElement module, CharSequence name); /** * Returns a module element given its fully qualified name. * * @param name the name * @return the named module element, or {@code null} if it cannot be found * @since 9 */ ModuleElement getModuleElement(CharSequence name); /** * Returns the values of an annotation's elements, including defaults. * * @see AnnotationMirror#getElementValues() * @param a annotation to examine * @return the values of the annotation's elements, including defaults */ Map getElementValuesWithDefaults(AnnotationMirror a); /** * Returns the text of the documentation ("Javadoc") * comment of an element. * *

A documentation comment of an element is a comment that * begins with "{@code /**}" , ends with a separate * "*/", and immediately precedes the element, * ignoring white space. Therefore, a documentation comment * contains at least three"{@code *}" characters. The text * returned for the documentation comment is a processed form of * the comment as it appears in source code. The leading "{@code * /**}" and trailing "*/" are removed. For lines * of the comment starting after the initial "{@code /**}", * leading white space characters are discarded as are any * consecutive "{@code *}" characters appearing after the white * space or starting the line. The processed lines are then * concatenated together (including line terminators) and * returned. * * @param e the element being examined * @return the documentation comment of the element, or {@code null} * if there is none * @jls 3.6 White Space */ String getDocComment(Element e); /** * Returns {@code true} if the element is deprecated, {@code false} otherwise. * * @param e the element being examined * @return {@code true} if the element is deprecated, {@code false} otherwise */ boolean isDeprecated(Element e); /** * Returns the naturalness of the given element. * * @implSpec The default implementation of this method returns * {@link Naturalness#NATURAL NATURAL}. * * @param e the element being examined * @return the naturalness of the given element * @since 9 */ default Naturalness getNaturalness(Element e) { return Naturalness.NATURAL; } /** * Returns the naturalness of the given annotation mirror. * * One example of a mandated construct is the * implicitly declared container annotation used to hold * repeated base annotations of a repeatable annotation type. * * @implSpec The default implementation of this method returns * {@link Naturalness#NATURAL NATURAL}. * * @param c the construct the annotation mirror modifies * @param a the annotation mirror being examined * @return the naturalness of the given annotation mirror * @jls 9.6.3 Repeatable Annotation Types * @jls 9.7.5 Multiple Annotations of the Same Type * @since 9 */ default Naturalness getNaturalness(AnnotatedConstruct c, AnnotationMirror a) { return Naturalness.NATURAL; } /** * Returns the naturalness of the given module directive. * * @implSpec The default implementation of this method returns * {@link Naturalness#NATURAL NATURAL}. * * @param m the module of the directive * @param directive the module directive being examined * @return the naturalness of the given directive * @since 9 */ default Naturalness getNaturalness(ModuleElement m, ModuleElement.Directive directive) { return Naturalness.NATURAL; } /** * The naturalness of a construct. The naturalness of a * construct concerns the consistency between how a construct is * declared in source code (explicitly, implicitly, or not at all) * compared to how the construct is represented in this model. * * Note that it is possible additional kinds of naturalness will * be added in future versions of the platform. * * @jls 13.1 The Form of a Binary * @since 9 */ enum Naturalness { /** * A natural construct is explicitly declared in source code. */ NATURAL, /** * A mandated construct is one not explicitly declared in the * source code, but whose presence is mandated by the * specification; such a construct is said to be implicitly declared. * * One example of a mandated element is a default * constructor in a class that contains no explicit * constructor declarations. * * Another example of a mandated construct is the implicitly * declared container annotation used to hold * multiple annotations of a repeatable annotation type. * * @jls 8.8.9 Default Constructor * @jls 9.6.3 Repeatable Annotation Types * @jls 9.7.5 Multiple Annotations of the Same Type */ MANDATED, /** * A synthetic construct is one that is neither implicitly nor * explicitly declared in the source code. Synthetic * constructs are commonly translation artifacts created by * compiler. */ SYNTHETIC; } /** * Returns {@code true} if the executable element is a bridge * method, {@code false} otherwise. * * @implSpec The default implementation of this method returns {@code false}. * * @param e the executable being examined * @return {@code true} if the executable element is a bridge * method, {@code false} otherwise * @since 9 */ default boolean isBridge(ExecutableElement e) { return false; } /** * Returns the binary name of a type element. * * @param type the type element being examined * @return the binary name * * @see TypeElement#getQualifiedName * @jls 13.1 The Form of a Binary */ Name getBinaryName(TypeElement type); /** * Returns the package of an element. The package of a package is * itself. * * @param type the element being examined * @return the package of an element */ PackageElement getPackageOf(Element type); /** * Returns the module of an element. The module of a module is * itself. * * @param type the element being examined * @return the module of an element * @since 9 */ ModuleElement getModuleOf(Element type); /** * Returns all members of a type element, whether inherited or * declared directly. For a class the result also includes its * constructors, but not local or anonymous classes. * *

Note that elements of certain kinds can be isolated using * methods in {@link ElementFilter}. * * @param type the type being examined * @return all members of the type * @see Element#getEnclosedElements */ List getAllMembers(TypeElement type); /** * Returns all annotations present on an element, whether * directly present or present via inheritance. * * @param e the element being examined * @return all annotations of the element * @see Element#getAnnotationMirrors * @see javax.lang.model.AnnotatedConstruct */ List getAllAnnotationMirrors(Element e); /** * Tests whether one type, method, or field hides another. * * @param hider the first element * @param hidden the second element * @return {@code true} if and only if the first element hides * the second */ boolean hides(Element hider, Element hidden); /** * Tests whether one method, as a member of a given type, * overrides another method. * When a non-abstract method overrides an abstract one, the * former is also said to implement the latter. * *

In the simplest and most typical usage, the value of the * {@code type} parameter will simply be the class or interface * directly enclosing {@code overrider} (the possibly-overriding * method). For example, suppose {@code m1} represents the method * {@code String.hashCode} and {@code m2} represents {@code * Object.hashCode}. We can then ask whether {@code m1} overrides * {@code m2} within the class {@code String} (it does): * *

* {@code assert elements.overrides(m1, m2, * elements.getTypeElement("java.lang.String")); } *
* * A more interesting case can be illustrated by the following example * in which a method in type {@code A} does not override a * like-named method in type {@code B}: * *
* {@code class A { public void m() {} } }
* {@code interface B { void m(); } }
* ...
* {@code m1 = ...; // A.m }
* {@code m2 = ...; // B.m }
* {@code assert ! elements.overrides(m1, m2, * elements.getTypeElement("A")); } *
* * When viewed as a member of a third type {@code C}, however, * the method in {@code A} does override the one in {@code B}: * *
* {@code class C extends A implements B {} }
* ...
* {@code assert elements.overrides(m1, m2, * elements.getTypeElement("C")); } *
* * @param overrider the first method, possible overrider * @param overridden the second method, possibly being overridden * @param type the type of which the first method is a member * @return {@code true} if and only if the first method overrides * the second * @jls 8.4.8 Inheritance, Overriding, and Hiding * @jls 9.4.1 Inheritance and Overriding */ boolean overrides(ExecutableElement overrider, ExecutableElement overridden, TypeElement type); /** * Returns the text of a constant expression representing a * primitive value or a string. * The text returned is in a form suitable for representing the value * in source code. * * @param value a primitive value or string * @return the text of a constant expression * @throws IllegalArgumentException if the argument is not a primitive * value or string * * @see VariableElement#getConstantValue() */ String getConstantExpression(Object value); /** * Prints a representation of the elements to the given writer in * the specified order. The main purpose of this method is for * diagnostics. The exact format of the output is not * specified and is subject to change. * * @param w the writer to print the output to * @param elements the elements to print */ void printElements(java.io.Writer w, Element... elements); /** * Return a name with the same sequence of characters as the * argument. * * @param cs the character sequence to return as a name * @return a name with the same sequence of characters as the argument */ Name getName(CharSequence cs); /** * Returns {@code true} if the type element is a functional interface, {@code false} otherwise. * * @param type the type element being examined * @return {@code true} if the element is a functional interface, {@code false} otherwise * @jls 9.8 Functional Interfaces * @since 1.8 */ boolean isFunctionalInterface(TypeElement type); }