Path: blob/master/src/java.compiler/share/classes/javax/lang/model/element/Element.java
41161 views
/*1* Copyright (c) 2005, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.lang.model.element;262728import java.lang.annotation.Annotation;29import java.lang.annotation.AnnotationTypeMismatchException;30import java.lang.annotation.IncompleteAnnotationException;31import java.util.List;32import java.util.Set;3334import javax.lang.model.type.*;35import javax.lang.model.util.*;3637/**38* Represents a program element such as a module, package, class, or method.39* Each element represents a compile-time language-level construct40* (and not, for example, a runtime construct of the virtual machine).41*42* <p> Elements should be compared using the {@link #equals(Object)}43* method. There is no guarantee that any particular element will44* always be represented by the same object.45*46* <p> To implement operations based on the class of an {@code47* Element} object, either use a {@linkplain ElementVisitor visitor} or48* use the result of the {@link #getKind} method. Using {@code49* instanceof} is <em>not</em> necessarily a reliable idiom for50* determining the effective class of an object in this modeling51* hierarchy since an implementation may choose to have a single object52* implement multiple {@code Element} subinterfaces.53*54* @author Joseph D. Darcy55* @author Scott Seligman56* @author Peter von der Ahé57* @see Elements58* @see TypeMirror59* @since 1.660*/61public interface Element extends javax.lang.model.AnnotatedConstruct {62/**63* {@return the type defined by this element}64*65* @see Types66* @see ExecutableElement#asType67* @see ModuleElement#asType68* @see PackageElement#asType69* @see TypeElement#asType70* @see TypeParameterElement#asType71* @see VariableElement#asType72*/73TypeMirror asType();7475/**76* {@return the {@code kind} of this element}77*78* <ul>79*80* <li> The kind of a {@linkplain PackageElement package} is81* {@link ElementKind#PACKAGE PACKAGE}.82*83* <li> The kind of a {@linkplain ModuleElement module} is {@link84* ElementKind#MODULE MODULE}.85*86* <li> The kind of a {@linkplain TypeElement type element} is one87* of {@link ElementKind#ANNOTATION_TYPE ANNOTATION_TYPE}, {@link88* ElementKind#CLASS CLASS}, {@link ElementKind#ENUM ENUM}, {@link89* ElementKind#INTERFACE INTERFACE}, or {@link ElementKind#RECORD90* RECORD}.91*92* <li> The kind of a {@linkplain VariableElement variable} is one93* of {@link ElementKind#ENUM_CONSTANT ENUM_CONSTANT}, {@link94* ElementKind#EXCEPTION_PARAMETER EXCEPTION_PARAMETER}, {@link95* ElementKind#FIELD FIELD}, {@link ElementKind#LOCAL_VARIABLE96* LOCAL_VARIABLE}, {@link ElementKind#PARAMETER PARAMETER},97* {@link ElementKind#RESOURCE_VARIABLE RESOURCE_VARIABLE}, or98* {@link ElementKind#BINDING_VARIABLE BINDING_VARIABLE}.99*100* <li> The kind of an {@linkplain ExecutableElement executable}101* is one of {@link ElementKind#CONSTRUCTOR CONSTRUCTOR}, {@link102* ElementKind#INSTANCE_INIT INSTANCE_INIT}, {@link103* ElementKind#METHOD METHOD}, or {@link ElementKind#STATIC_INIT104* STATIC_INIT}.105*106* <li> The kind of a {@linkplain TypeParameterElement type parameter} is107* {@link ElementKind#TYPE_PARAMETER TYPE_PARAMETER}.108*109* <li> The kind of a {@linkplain RecordComponentElement record110* component} is {@link ElementKind#RECORD_COMPONENT111* RECORD_COMPONENT}.112*113* </ul>114*/115ElementKind getKind();116117/**118* Returns the modifiers of this element, excluding annotations.119* Implicit modifiers, such as the {@code public} and {@code static}120* modifiers of interface members, are included.121*122* @return the modifiers of this element, or an empty set if there are none123*/124Set<Modifier> getModifiers();125126/**127* {@return the simple (unqualified) name of this element} The128* name of a generic class or interface does not include any129* reference to its formal type parameters.130*131* For example, the simple name of the type element representing132* {@code java.util.Set<E>} is {@code "Set"}.133*134* If this element represents an unnamed {@linkplain135* PackageElement#getSimpleName package} or unnamed {@linkplain136* ModuleElement#getSimpleName module}, an <a137* href=Name.html#empty_name>empty name</a> is returned.138*139* If it represents a {@linkplain ExecutableElement#getSimpleName140* constructor}, the name "{@code <init>}" is returned. If it141* represents a {@linkplain ExecutableElement#getSimpleName static142* initializer}, the name "{@code <clinit>}" is returned.143*144* If it represents an {@linkplain TypeElement#getSimpleName145* anonymous class} or {@linkplain ExecutableElement#getSimpleName146* instance initializer}, an <a href=Name.html#empty_name>empty147* name</a> is returned.148*149* @see PackageElement#getSimpleName150* @see ExecutableElement#getSimpleName151* @see TypeElement#getSimpleName152* @see VariableElement#getSimpleName153* @see ModuleElement#getSimpleName154* @see RecordComponentElement#getSimpleName155* @revised 9156*/157Name getSimpleName();158159/**160* Returns the innermost element161* within which this element is, loosely speaking, enclosed.162* <ul>163* <li> If this element is one whose declaration is lexically enclosed164* immediately within the declaration of another element, that other165* element is returned.166*167* <li> If this is a {@linkplain TypeElement#getEnclosingElement168* top-level class or interface}, its package is returned.169*170* <li> If this is a {@linkplain171* PackageElement#getEnclosingElement package}, its module is172* returned if such a module exists. Otherwise, {@code null} is returned.173*174* <li> If this is a {@linkplain175* TypeParameterElement#getEnclosingElement type parameter},176* {@linkplain TypeParameterElement#getGenericElement the177* generic element} of the type parameter is returned.178*179* <li> If this is a {@linkplain180* VariableElement#getEnclosingElement method or constructor181* parameter}, {@linkplain ExecutableElement the executable182* element} which declares the parameter is returned.183*184* <li> If this is a {@linkplain185* RecordComponentElement#getEnclosingElement record component},186* {@linkplain TypeElement the record class} which declares the187* record component is returned.188*189* <li> If this is a {@linkplain ModuleElement#getEnclosingElement190* module}, {@code null} is returned.191*192* </ul>193*194* @return the enclosing element, or {@code null} if there is none195* @see Elements#getPackageOf196* @revised 9197*/198Element getEnclosingElement();199200/**201* Returns the elements that are, loosely speaking, directly202* enclosed by this element.203*204* A {@linkplain TypeElement#getEnclosedElements class or205* interface} is considered to enclose the fields, methods,206* constructors, record components, and member classes and interfaces that it directly declares.207*208* A {@linkplain PackageElement#getEnclosedElements package}209* encloses the top-level classes and interfaces within it, but is210* not considered to enclose subpackages.211*212* A {@linkplain ModuleElement#getEnclosedElements module}213* encloses packages within it.214*215* Enclosed elements may include implicitly declared {@linkplain216* Elements.Origin#MANDATED mandated} elements.217*218* Other kinds of elements are not currently considered to enclose219* any elements; however, that may change as this API or the220* programming language evolves.221*222* @apiNote Elements of certain kinds can be isolated using223* methods in {@link ElementFilter}.224*225* @return the enclosed elements, or an empty list if none226* @see TypeElement#getEnclosedElements227* @see PackageElement#getEnclosedElements228* @see ModuleElement#getEnclosedElements229* @see Elements#getAllMembers230* @jls 8.8.9 Default Constructor231* @jls 8.9 Enum Classes232* @revised 9233*/234List<? extends Element> getEnclosedElements();235236/**237* {@return {@code true} if the argument represents the same238* element as {@code this}, or {@code false} otherwise}239*240* @apiNote The identity of an element involves implicit state241* not directly accessible from the element's methods, including242* state about the presence of unrelated types. Element objects243* created by different implementations of these interfaces should244* <i>not</i> be expected to be equal even if "the same"245* element is being modeled; this is analogous to the inequality246* of {@code Class} objects for the same class file loaded through247* different class loaders.248*249* @param obj the object to be compared with this element250*/251@Override252boolean equals(Object obj);253254/**255* Obeys the general contract of {@link Object#hashCode Object.hashCode}.256*257* @see #equals258*/259@Override260int hashCode();261262/**263* {@inheritDoc}264*265* <p>To get inherited annotations as well, use {@link266* Elements#getAllAnnotationMirrors(Element)267* getAllAnnotationMirrors}.268*269* <p>Note that any annotations returned by this method are270* declaration annotations.271*272* @since 1.6273*/274@Override275List<? extends AnnotationMirror> getAnnotationMirrors();276277/**278* {@inheritDoc}279*280* <p>Note that any annotation returned by this method is a281* declaration annotation.282*283* @since 1.6284*/285@Override286<A extends Annotation> A getAnnotation(Class<A> annotationType);287288/**289* {@inheritDoc}290*291* <p>Note that any annotations returned by this method are292* declaration annotations.293*294* @since 8295*/296@Override297<A extends Annotation> A[] getAnnotationsByType(Class<A> annotationType);298299/**300* Applies a visitor to this element.301*302* @param <R> the return type of the visitor's methods303* @param <P> the type of the additional parameter to the visitor's methods304* @param v the visitor operating on this element305* @param p additional parameter to the visitor306* @return a visitor-specified result307*/308<R, P> R accept(ElementVisitor<R, P> v, P p);309}310311312