public interface Elements
Compatibility Note: Methods may be added to this interface in future releases of the platform.
ProcessingEnvironment.getElementUtils()
Modifier and Type | Interface | Description |
---|---|---|
static class |
Elements.Origin |
The origin of an element or other language model
item.
|
Modifier and Type | Method | Description |
---|---|---|
List<? extends AnnotationMirror> |
getAllAnnotationMirrors(Element e) |
Returns all annotations present on an element, whether
directly present or present via inheritance.
|
List<? extends Element> |
getAllMembers(TypeElement type) |
Returns all members of a type element, whether inherited or
declared directly.
|
Name |
getBinaryName(TypeElement type) |
Returns the binary name of a type element.
|
String |
getConstantExpression(Object value) |
Returns the text of a constant expression representing a
primitive value or a string.
|
String |
getDocComment(Element e) |
Returns the text of the documentation ("Javadoc")
comment of an element.
|
Map<? extends ExecutableElement,? extends AnnotationValue> |
getElementValuesWithDefaults(AnnotationMirror a) |
Returns the values of an annotation's elements, including defaults.
|
ModuleElement |
getModuleElement(CharSequence name) |
Returns a module element given its fully qualified name.
|
ModuleElement |
getModuleOf(Element type) |
Returns the module of an element.
|
Name |
getName(CharSequence cs) |
Return a name with the same sequence of characters as the
argument.
|
default Elements.Origin |
getOrigin(AnnotatedConstruct c,
AnnotationMirror a) |
Returns the origin of the given annotation mirror.
|
default Elements.Origin |
getOrigin(Element e) |
Returns the origin of the given element.
|
default Elements.Origin |
getOrigin(ModuleElement m,
ModuleElement.Directive directive) |
Returns the origin of the given module directive.
|
PackageElement |
getPackageElement(CharSequence name) |
Returns a package given its fully qualified name if the package is unique in the environment.
|
PackageElement |
getPackageElement(ModuleElement module,
CharSequence name) |
Returns a package given its fully qualified name, as seen from the given module.
|
PackageElement |
getPackageOf(Element type) |
Returns the package of an element.
|
TypeElement |
getTypeElement(CharSequence name) |
Returns a type element given its canonical name if the type element is unique in the environment.
|
TypeElement |
getTypeElement(ModuleElement module,
CharSequence name) |
Returns a type element given its canonical name, as seen from the given module.
|
boolean |
hides(Element hider,
Element hidden) |
Tests whether one type, method, or field hides another.
|
default boolean |
isBridge(ExecutableElement e) |
Returns
true if the executable element is a bridge
method, false otherwise. |
boolean |
isDeprecated(Element e) |
Returns
true if the element is deprecated, false otherwise. |
boolean |
isFunctionalInterface(TypeElement type) |
Returns
true if the type element is a functional interface, false otherwise. |
boolean |
overrides(ExecutableElement overrider,
ExecutableElement overridden,
TypeElement type) |
Tests whether one method, as a member of a given type,
overrides another method.
|
void |
printElements(Writer w,
Element... elements) |
Prints a representation of the elements to the given writer in
the specified order.
|
PackageElement getPackageElement(CharSequence name)
name
- fully qualified package name, or an empty string for an unnamed packagenull
if it cannot be uniquely foundPackageElement getPackageElement(ModuleElement module, CharSequence name)
name
- fully qualified package name, or an empty string for an unnamed packagemodule
- module relative to which the lookup should happennull
if it cannot be foundTypeElement getTypeElement(CharSequence name)
name
- the canonical namenull
if it cannot be uniquely foundTypeElement getTypeElement(ModuleElement module, CharSequence name)
name
- the canonical namemodule
- module relative to which the lookup should happennull
if it cannot be foundModuleElement getModuleElement(CharSequence name)
name
- the namenull
if it cannot be foundMap<? extends ExecutableElement,? extends AnnotationValue> getElementValuesWithDefaults(AnnotationMirror a)
a
- annotation to examineAnnotationMirror.getElementValues()
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.
e
- the element being examinednull
if there is noneboolean isDeprecated(Element e)
true
if the element is deprecated, false
otherwise.e
- the element being examinedtrue
if the element is deprecated, false
otherwisedefault 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.
EXPLICIT
.e
- the element being examineddefault 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.
EXPLICIT
.c
- the construct the annotation mirror modifiesa
- the annotation mirror being examineddefault 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.
EXPLICIT
.m
- the module of the directivedirective
- the module directive being examineddefault boolean isBridge(ExecutableElement e)
true
if the executable element is a bridge
method, false
otherwise.false
.e
- the executable being examinedtrue
if the executable element is a bridge
method, false
otherwiseName getBinaryName(TypeElement type)
type
- the type element being examinedTypeElement.getQualifiedName()
PackageElement getPackageOf(Element type)
type
- the element being examinedModuleElement getModuleOf(Element type)
type
- the element being examinedList<? extends Element> getAllMembers(TypeElement type)
Note that elements of certain kinds can be isolated using
methods in ElementFilter
.
type
- the type being examinedElement.getEnclosedElements()
List<? extends AnnotationMirror> getAllAnnotationMirrors(Element e)
e
- the element being examinedElement.getAnnotationMirrors()
,
AnnotatedConstruct
boolean hides(Element hider, Element hidden)
hider
- the first elementhidden
- the second elementtrue
if and only if the first element hides
the secondboolean 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 type A
does not override a
like-named method in type B
:
When viewed as a member of a third typeclass 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"));
overrider
- the first method, possible overrideroverridden
- the second method, possibly being overriddentype
- the type of which the first method is a membertrue
if and only if the first method overrides
the secondString getConstantExpression(Object value)
value
- a primitive value or stringIllegalArgumentException
- if the argument is not a primitive
value or stringVariableElement.getConstantValue()
void printElements(Writer w, Element... elements)
w
- the writer to print the output toelements
- the elements to printName getName(CharSequence cs)
cs
- the character sequence to return as a nameboolean isFunctionalInterface(TypeElement type)
true
if the type element is a functional interface, false
otherwise.type
- the type element being examinedtrue
if the element is a functional interface, false
otherwise Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2017, Oracle and/or its affiliates. 500 Oracle Parkway
Redwood Shores, CA 94065 USA. All rights reserved.
DRAFT 9-Debian+0-9b153-2