Interface Elements
public interface Elements
Compatibility Note: Methods may be added to this interface in future releases of the platform.
- Since:
- 1.6
- See Also:
Nested Class Summary
Modifier and Type | Interface | Description |
---|---|---|
static enum |
Elements.Origin |
The origin of an element or other language model item. |
Method Summary
Modifier and Type | Method | Description |
---|---|---|
List<? extends AnnotationMirror> |
getAllAnnotationMirrors |
Returns all annotations present on an element, whether directly present or present via inheritance. |
List<? extends Element> |
getAllMembers |
Returns all members of a type element, whether inherited or declared directly. |
default Set<? extends ModuleElement> |
getAllModuleElements() |
Returns all module elements in the current environment. |
default Set<? extends PackageElement> |
getAllPackageElements |
Returns all package elements with the given canonical name. |
default Set<? extends TypeElement> |
getAllTypeElements |
Returns all type elements with the given canonical name. |
Name |
getBinaryName |
Returns the binary name of a type element. |
String |
getConstantExpression |
Returns the text of a constant expression representing a primitive value or a string. |
String |
getDocComment |
Returns the text of the documentation ("Javadoc") comment of an element. |
Map<? extends ExecutableElement, |
getElementValuesWithDefaults |
Returns the values of an annotation's elements, including defaults. |
default ModuleElement |
getModuleElement |
Returns a module element given its fully qualified name. |
default ModuleElement |
getModuleOf |
Returns the module of an element. |
Name |
getName |
Returns a name with the same sequence of characters as the argument. |
default Elements.Origin |
getOrigin |
Returns the origin of the given annotation mirror. |
default Elements.Origin |
getOrigin |
Returns the origin of the given element. |
default Elements.Origin |
getOrigin |
Returns the origin of the given module directive. |
PackageElement |
getPackageElement |
Returns a package given its fully qualified name if the package is uniquely determinable in the environment. |
default PackageElement |
getPackageElement |
Returns a package given its fully qualified name, as seen from the given module. |
PackageElement |
getPackageOf |
Returns the package of an element. |
TypeElement |
getTypeElement |
Returns a type element given its canonical name if the type element is uniquely determinable in the environment. |
default TypeElement |
getTypeElement |
Returns a type element given its canonical name, as seen from the given module. |
boolean |
hides |
Tests whether one type, method, or field hides another. |
default boolean |
isAutomaticModule |
Returns true if the module element is an automatic module, false otherwise. |
default boolean |
isBridge |
Returns true if the executable element is a bridge method, false otherwise. |
boolean |
isDeprecated |
Returns true if the element is deprecated, false otherwise. |
boolean |
isFunctionalInterface |
Returns true if the type element is a functional interface, false otherwise. |
boolean |
overrides |
Tests whether one method, as a member of a given class or interface, overrides another method. |
void |
printElements |
Prints a representation of the elements to the given writer in the specified order. |
default RecordComponentElement |
recordComponentFor |
Returns the record component for the given accessor. |
Method Details
getPackageElement
PackageElement getPackageElement(CharSequence name)
- find non-empty packages with the given name returned by
getPackageElement(ModuleElement, CharSequence)
, where the provided ModuleSymbol is any root module, - if the above yields an empty list, search
all modules
for observable packages with the given name
- Parameters:
-
name
- fully qualified package name, or an empty string for an unnamed package - Returns:
- the specified package, or
null
if no package can be uniquely determined.
getPackageElement
default PackageElement getPackageElement(ModuleElement module, CharSequence name)
- Implementation Requirements:
- The default implementation of this method returns
null
. - Parameters:
-
name
- fully qualified package name, or an empty string for an unnamed package -
module
- module relative to which the lookup should happen - Returns:
- the specified package, or
null
if it cannot be found - Since:
- 9
- See Also:
getAllPackageElements
default Set<? extends PackageElement> getAllPackageElements(CharSequence name)
- Implementation Requirements:
- The default implementation of this method calls
getAllModuleElements
and stores the result. If the set of modules is empty,getPackageElement(name)
is called passing through the name argument. IfgetPackageElement(name)
isnull
, an empty set of package elements is returned; otherwise, a single-element set with the found package element is returned. If the set of modules is nonempty, the modules are iterated over and any non-null
results ofgetPackageElement(module, name)
are accumulated into a set. The set is then returned. - Parameters:
-
name
- the canonical name - Returns:
- the package elements, or an empty set if no package with the name can be found
- Since:
- 9
- See Also:
getTypeElement
TypeElement getTypeElement(CharSequence name)
- find type elements with the given name returned by
getTypeElement(ModuleElement, CharSequence)
, where the provided ModuleSymbol is any root module, - if the above yields an empty list, search
all modules
for observable type elements with the given name
- Parameters:
-
name
- the canonical name - Returns:
- the named type element, or
null
if no type element can be uniquely determined.
getTypeElement
default TypeElement getTypeElement(ModuleElement module, CharSequence name)
- Implementation Requirements:
- The default implementation of this method returns
null
. - Parameters:
-
name
- the canonical name -
module
- module relative to which the lookup should happen - Returns:
- the named type element, or
null
if it cannot be found - Since:
- 9
- See Also:
getAllTypeElements
default Set<? extends TypeElement> getAllTypeElements(CharSequence name)
- Implementation Requirements:
- The default implementation of this method calls
getAllModuleElements
and stores the result. If the set of modules is empty,getTypeElement(name)
is called passing through the name argument. IfgetTypeElement(name)
isnull
, an empty set of type elements is returned; otherwise, a single-element set with the found type element is returned. If the set of modules is nonempty, the modules are iterated over and any non-null
results ofgetTypeElement(module, name)
are accumulated into a set. The set is then returned. - Parameters:
-
name
- the canonical name - Returns:
- the type elements, or an empty set if no type with the name can be found
- Since:
- 9
- See Also:
getModuleElement
default ModuleElement getModuleElement(CharSequence name)
null
is returned. One situation where a module cannot be found is if the environment does not include modules, such as an annotation processing environment configured for a source version without modules.- Implementation Requirements:
- The default implementation of this method returns
null
. - Parameters:
-
name
- the name, or an empty string for an unnamed module - Returns:
- the named module element, or
null
if it cannot be found - Since:
- 9
- See Also:
getAllModuleElements
default Set<? extends ModuleElement> getAllModuleElements()
- Implementation Requirements:
- The default implementation of this method returns an empty set.
- Returns:
- the known module elements, or an empty set if there are no modules
- Since:
- 9
- See Also:
getElementValuesWithDefaults
Map<? extends ExecutableElement,? extends AnnotationValue> getElementValuesWithDefaults(AnnotationMirror a)
- Parameters:
-
a
- annotation to examine - Returns:
- the values of an annotation's elements, including defaults
- See Also:
getDocComment
String getDocComment(Element e)
A documentation comment of an element is a comment that begins with "/**
", ends with a separate "*/
", and immediately precedes the element, ignoring white space. Therefore, a documentation comment contains at least three "*
" characters. The text returned for the documentation comment is a processed form of the comment as it appears in source code. The leading "/**
" and trailing "*/
" are removed. For lines of the comment starting after the initial "/**
", leading white space characters are discarded as are any consecutive "*
" characters appearing after the white space or starting the line. The processed lines are then concatenated together (including line terminators) and returned.
- Parameters:
-
e
- the element being examined - Returns:
- the documentation comment of the element, or
null
if there is none - See Java Language Specification:
- 3.6 White Space
isDeprecated
boolean isDeprecated(Element e)
true
if the element is deprecated, false
otherwise.- Parameters:
-
e
- the element being examined - Returns:
-
true
if the element is deprecated,false
otherwise
getOrigin
default Elements.Origin getOrigin(Element e)
Note that if this method returns EXPLICIT
and the element was created from a class file, then the element may not, in fact, correspond to an explicitly declared construct in source code. This is due to limitations of the fidelity of the class file format in preserving information from source code. For example, at least some versions of the class file format do not preserve whether a constructor was explicitly declared by the programmer or was implicitly declared as the default constructor.
- Implementation Requirements:
- The default implementation of this method returns
EXPLICIT
. - Parameters:
-
e
- the element being examined - Returns:
- the origin of the given element
- Since:
- 9
getOrigin
default Elements.Origin getOrigin(AnnotatedConstruct c, AnnotationMirror a)
Note that if this method returns EXPLICIT
and the annotation mirror was created from a class file, then the element may not, in fact, correspond to an explicitly declared construct in source code. This is due to limitations of the fidelity of the class file format in preserving information from source code. For example, at least some versions of the class file format do not preserve whether an annotation was explicitly declared by the programmer or was implicitly declared as a container annotation.
- Implementation Requirements:
- The default implementation of this method returns
EXPLICIT
. - Parameters:
-
c
- the construct the annotation mirror modifies -
a
- the annotation mirror being examined - Returns:
- the origin of the given annotation mirror
- See Java Language Specification:
- 9.6.3 Repeatable Annotation Types
9.7.5 Multiple Annotations of the Same Interface
- Since:
- 9
getOrigin
default Elements.Origin getOrigin(ModuleElement m, ModuleElement.Directive directive)
Note that if this method returns EXPLICIT
and the module directive was created from a class file, then the module directive may not, in fact, correspond to an explicitly declared construct in source code. This is due to limitations of the fidelity of the class file format in preserving information from source code. For example, at least some versions of the class file format do not preserve whether a uses
directive was explicitly declared by the programmer or was added as a synthetic construct.
Note that an implementation may not be able to reliably determine the origin status of the directive if the directive is created from a class file due to limitations of the fidelity of the class file format in preserving information from source code.
- Implementation Requirements:
- The default implementation of this method returns
EXPLICIT
. - Parameters:
-
m
- the module of the directive -
directive
- the module directive being examined - Returns:
- the origin of the given module directive
- Since:
- 9
isBridge
default boolean isBridge(ExecutableElement e)
true
if the executable element is a bridge method, false
otherwise.- Implementation Requirements:
- The default implementation of this method returns
false
. - Parameters:
-
e
- the executable being examined - Returns:
-
true
if the executable element is a bridge method,false
otherwise - Since:
- 9
getBinaryName
Name getBinaryName(TypeElement type)
- Parameters:
-
type
- the type element being examined - Returns:
- the binary name of a type element
- See Java Language Specification:
- 13.1 The Form of a Binary
- See Also:
getPackageOf
PackageElement getPackageOf(Element e)
null
. The package of a top-level class or interface is its enclosing package. Otherwise, the package of an element is equal to the package of the enclosing element.- Parameters:
-
e
- the element being examined - Returns:
- the package of an element
getModuleOf
default ModuleElement getModuleOf(Element e)
null
, null
is returned for the package's module. (One situation where a package may have a null
module is if the environment does not include modules, such as an annotation processing environment configured for a source version without modules.) Otherwise, the module of an element is equal to the module of the package of the element.- Implementation Requirements:
- The default implementation of this method returns
null
. - Parameters:
-
e
- the element being examined - Returns:
- the module of an element
- Since:
- 9
getAllMembers
List<? extends Element> getAllMembers(TypeElement type)
- API Note:
- Elements of certain kinds can be isolated using methods in
ElementFilter
. - Parameters:
-
type
- the type being examined - Returns:
- all members of the type
- See Also:
getAllAnnotationMirrors
List<? extends AnnotationMirror> getAllAnnotationMirrors(Element e)
Note that any annotations returned by this method are declaration annotations.
- Parameters:
-
e
- the element being examined - Returns:
- all annotations of the element
- See Also:
hides
boolean hides(Element hider, Element hidden)
- Parameters:
-
hider
- the first element -
hidden
- the second element - Returns:
-
true
if and only if the first element hides the second - See Java Language Specification:
- 8.4.8 Inheritance, Overriding, and Hiding
overrides
boolean overrides(ExecutableElement overrider, ExecutableElement overridden, TypeElement type)
In the simplest and most typical usage, the value of the type
parameter will simply be the class or interface directly enclosing overrider
(the possibly-overriding method). For example, suppose m1
represents the method String.hashCode
and m2
represents
Object.hashCode
. We can then ask whether m1
overrides m2
within the class String
(it does):
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 class A
does not override a like-named method in interface B
: When viewed as a member of a third classclass A { public void m() {} }
interface B { void m(); }
...
m1 = ...; // A.m
m2 = ...; // B.m
assert ! elements.overrides(m1, m2, elements.getTypeElement("A"));
C
, however, the method in A
does override the one in B
: class C extends A implements B {}
...
assert elements.overrides(m1, m2, elements.getTypeElement("C"));
- Parameters:
-
overrider
- the first method, possible overrider -
overridden
- the second method, possibly being overridden -
type
- the class or interface of which the first method is a member - Returns:
-
true
if and only if the first method overrides the second - See Java Language Specification:
- 8.4.8 Inheritance, Overriding, and Hiding
9.4.1 Inheritance and Overriding
getConstantExpression
String getConstantExpression(Object value)
- Parameters:
-
value
- a primitive value or string - Returns:
- the text of a constant expression
- Throws:
-
IllegalArgumentException
- if the argument is not a primitive value or string - See Also:
printElements
void printElements(Writer w, Element... elements)
- Parameters:
-
w
- the writer to print the output to -
elements
- the elements to print
getName
Name getName(CharSequence cs)
- Parameters:
-
cs
- the character sequence to return as a name - Returns:
- a name with the same sequence of characters as the argument
isFunctionalInterface
boolean isFunctionalInterface(TypeElement type)
true
if the type element is a functional interface, false
otherwise.- Parameters:
-
type
- the type element being examined - Returns:
-
true
if the type element is a functional interface,false
otherwise - See Java Language Specification:
- 9.8 Functional Interfaces
- Since:
- 1.8
isAutomaticModule
default boolean isAutomaticModule(ModuleElement module)
true
if the module element is an automatic module, false
otherwise.- Implementation Requirements:
- The default implementation of this method returns
false
. - Parameters:
-
module
- the module element being examined - Returns:
-
true
if the module element is an automatic module,false
otherwise - See Java Language Specification:
- 7.7.1 Dependences
- Since:
- 17
recordComponentFor
default RecordComponentElement recordComponentFor(ExecutableElement accessor)
- Implementation Requirements:
- The default implementation of this method checks if the element enclosing the accessor has kind
RECORD
if that is the case, then all the record components on the accessor's enclosing element are retrieved by invokingElementFilter.recordComponentsIn(Iterable)
. If the accessor of at least one of the record components retrieved happen to be equal to the accessor passed as a parameter to this method, then that record component is returned, in any other casenull
is returned. - Parameters:
-
accessor
- the method for which the record component should be found. - Returns:
- the record component, or null if the given method is not an record component accessor
- Since:
- 16
© 1993, 2021, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/17/docs/api/java.compiler/javax/lang/model/util/Elements.html