src/share/classes/javax/lang/model/element/Element.java

Mon, 14 Jan 2013 19:52:36 +0100

author
jfranck
date
Mon, 14 Jan 2013 19:52:36 +0100
changeset 1491
9f42a06a49c0
parent 1484
7612fe48be90
child 1494
f805b5e3c9d1
permissions
-rw-r--r--

7193719: Support repeating annotations in javax.lang.model
Reviewed-by: jjg

     1 /*
     2  * Copyright (c) 2005, 2013, 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  */
    26 package javax.lang.model.element;
    29 import java.lang.annotation.Annotation;
    30 import java.lang.annotation.AnnotationTypeMismatchException;
    31 import java.lang.annotation.IncompleteAnnotationException;
    32 import java.util.List;
    33 import java.util.Set;
    35 import javax.lang.model.type.*;
    36 import javax.lang.model.util.*;
    39 /**
    40  * Represents a program element such as a package, class, or method.
    41  * Each element represents a static, language-level construct
    42  * (and not, for example, a runtime construct of the virtual machine).
    43  *
    44  * <p> Elements should be compared using the {@link #equals(Object)}
    45  * method.  There is no guarantee that any particular element will
    46  * always be represented by the same object.
    47  *
    48  * <p> To implement operations based on the class of an {@code
    49  * Element} object, either use a {@linkplain ElementVisitor visitor} or
    50  * use the result of the {@link #getKind} method.  Using {@code
    51  * instanceof} is <em>not</em> necessarily a reliable idiom for
    52  * determining the effective class of an object in this modeling
    53  * hierarchy since an implementation may choose to have a single object
    54  * implement multiple {@code Element} subinterfaces.
    55  *
    56  * @author Joseph D. Darcy
    57  * @author Scott Seligman
    58  * @author Peter von der Ah&eacute;
    59  * @see Elements
    60  * @see TypeMirror
    61  * @since 1.6
    62  */
    63 public interface Element {
    65     /**
    66      * Returns the type defined by this element.
    67      *
    68      * <p> A generic element defines a family of types, not just one.
    69      * If this is a generic element, a <i>prototypical</i> type is
    70      * returned.  This is the element's invocation on the
    71      * type variables corresponding to its own formal type parameters.
    72      * For example,
    73      * for the generic class element {@code C<N extends Number>},
    74      * the parameterized type {@code C<N>} is returned.
    75      * The {@link Types} utility interface has more general methods
    76      * for obtaining the full range of types defined by an element.
    77      *
    78      * @see Types
    79      *
    80      * @return the type defined by this element
    81      */
    82     TypeMirror asType();
    84     /**
    85      * Returns the {@code kind} of this element.
    86      *
    87      * @return the kind of this element
    88      */
    89     ElementKind getKind();
    91     /**
    92      * Returns the annotations that are directly present on this element.
    93      *
    94      * <p> To get inherited annotations as well, use
    95      * {@link Elements#getAllAnnotationMirrors(Element) getAllAnnotationMirrors}.
    96      *
    97      * @see ElementFilter
    98      *
    99      * @return the annotations directly present on this element;
   100      *          an empty list if there are none
   101      */
   102     List<? extends AnnotationMirror> getAnnotationMirrors();
   104     /**
   105      * Returns this element's annotation for the specified type if
   106      * such an annotation is present, else {@code null}.  The
   107      * annotation may be either inherited or directly present on this
   108      * element.
   109      *
   110      * <p> The annotation returned by this method could contain an element
   111      * whose value is of type {@code Class}.
   112      * This value cannot be returned directly:  information necessary to
   113      * locate and load a class (such as the class loader to use) is
   114      * not available, and the class might not be loadable at all.
   115      * Attempting to read a {@code Class} object by invoking the relevant
   116      * method on the returned annotation
   117      * will result in a {@link MirroredTypeException},
   118      * from which the corresponding {@link TypeMirror} may be extracted.
   119      * Similarly, attempting to read a {@code Class[]}-valued element
   120      * will result in a {@link MirroredTypesException}.
   121      *
   122      * <blockquote>
   123      * <i>Note:</i> This method is unlike others in this and related
   124      * interfaces.  It operates on runtime reflective information &mdash;
   125      * representations of annotation types currently loaded into the
   126      * VM &mdash; rather than on the representations defined by and used
   127      * throughout these interfaces.  Consequently, calling methods on
   128      * the returned annotation object can throw many of the exceptions
   129      * that can be thrown when calling methods on an annotation object
   130      * returned by core reflection.  This method is intended for
   131      * callers that are written to operate on a known, fixed set of
   132      * annotation types.
   133      * </blockquote>
   134      *
   135      * @param <A>  the annotation type
   136      * @param annotationType  the {@code Class} object corresponding to
   137      *          the annotation type
   138      * @return this element's annotation for the specified annotation
   139      *         type if present on this element, else {@code null}
   140      *
   141      * @see #getAnnotationMirrors()
   142      * @see java.lang.reflect.AnnotatedElement#getAnnotation
   143      * @see EnumConstantNotPresentException
   144      * @see AnnotationTypeMismatchException
   145      * @see IncompleteAnnotationException
   146      * @see MirroredTypeException
   147      * @see MirroredTypesException
   148      */
   149     <A extends Annotation> A getAnnotation(Class<A> annotationType);
   151     /**
   152      * Returns an array of all of this element's annotation for the
   153      * specified type if such annotations are present, else an empty
   154      * array.  The annotation may be either inherited or directly
   155      * present on this element. This method will look through a container
   156      * annotation (if present) if the supplied annotation type is
   157      * repeatable.
   158      *
   159      * <p> The annotations returned by this method could contain an element
   160      * whose value is of type {@code Class}.
   161      * This value cannot be returned directly:  information necessary to
   162      * locate and load a class (such as the class loader to use) is
   163      * not available, and the class might not be loadable at all.
   164      * Attempting to read a {@code Class} object by invoking the relevant
   165      * method on the returned annotation
   166      * will result in a {@link MirroredTypeException},
   167      * from which the corresponding {@link TypeMirror} may be extracted.
   168      * Similarly, attempting to read a {@code Class[]}-valued element
   169      * will result in a {@link MirroredTypesException}.
   170      *
   171      * <blockquote>
   172      * <i>Note:</i> This method is unlike others in this and related
   173      * interfaces.  It operates on runtime reflective information &mdash;
   174      * representations of annotation types currently loaded into the
   175      * VM &mdash; rather than on the representations defined by and used
   176      * throughout these interfaces.  Consequently, calling methods on
   177      * the returned annotation object can throw many of the exceptions
   178      * that can be thrown when calling methods on an annotation object
   179      * returned by core reflection.  This method is intended for
   180      * callers that are written to operate on a known, fixed set of
   181      * annotation types.
   182      * </blockquote>
   183      *
   184      * @param <A>  the annotation type
   185      * @param annotationType  the {@code Class} object corresponding to
   186      *          the annotation type
   187      * @return this element's annotations for the specified annotation
   188      *         type if present on this element, else an empty array
   189      *
   190      * @see #getAnnotationMirrors()
   191      * @see #getAnnotation()
   192      * @see java.lang.reflect.AnnotatedElement#getAnnotations
   193      * @see EnumConstantNotPresentException
   194      * @see AnnotationTypeMismatchException
   195      * @see IncompleteAnnotationException
   196      * @see MirroredTypeException
   197      * @see MirroredTypesException
   198      */
   199     <A extends Annotation> A[] getAnnotations(Class<A> annotationType);
   201     /**
   202      * Returns the modifiers of this element, excluding annotations.
   203      * Implicit modifiers, such as the {@code public} and {@code static}
   204      * modifiers of interface members, are included.
   205      *
   206      * @return the modifiers of this element, or an empty set if there are none
   207      */
   208     Set<Modifier> getModifiers();
   210     /**
   211      * Returns the simple (unqualified) name of this element.  The
   212      * name of a generic type does not include any reference to its
   213      * formal type parameters.
   214      *
   215      * For example, the simple name of the type element {@code
   216      * java.util.Set<E>} is {@code "Set"}.
   217      *
   218      * If this element represents an unnamed {@linkplain
   219      * PackageElement#getSimpleName package}, an empty name is
   220      * returned.
   221      *
   222      * If it represents a {@linkplain ExecutableElement#getSimpleName
   223      * constructor}, the name "{@code <init>}" is returned.  If it
   224      * represents a {@linkplain ExecutableElement#getSimpleName static
   225      * initializer}, the name "{@code <clinit>}" is returned.
   226      *
   227      * If it represents an {@linkplain TypeElement#getSimpleName
   228      * anonymous class} or {@linkplain ExecutableElement#getSimpleName
   229      * instance initializer}, an empty name is returned.
   230      *
   231      * @return the simple name of this element
   232      * @see PackageElement#getSimpleName
   233      * @see ExecutableElement#getSimpleName
   234      * @see TypeElement#getSimpleName
   235      * @see VariableElement#getSimpleName
   236      */
   237     Name getSimpleName();
   239     /**
   240      * Returns the innermost element
   241      * within which this element is, loosely speaking, enclosed.
   242      * <ul>
   243      * <li> If this element is one whose declaration is lexically enclosed
   244      * immediately within the declaration of another element, that other
   245      * element is returned.
   246      *
   247      * <li> If this is a {@linkplain TypeElement#getEnclosingElement
   248      * top-level type}, its package is returned.
   249      *
   250      * <li> If this is a {@linkplain
   251      * PackageElement#getEnclosingElement package}, {@code null} is
   252      * returned.
   253      *
   254      * <li> If this is a {@linkplain
   255      * TypeParameterElement#getEnclosingElement type parameter},
   256      * {@linkplain TypeParameterElement#getGenericElement the
   257      * generic element} of the type parameter is returned.
   258      *
   259      * <li> If this is a {@linkplain
   260      * VariableElement#getEnclosingElement method or constructor
   261      * parameter}, {@linkplain ExecutableElement the executable
   262      * element} which declares the parameter is returned.
   263      *
   264      * </ul>
   265      *
   266      * @return the enclosing element, or {@code null} if there is none
   267      * @see Elements#getPackageOf
   268      */
   269     Element getEnclosingElement();
   271     /**
   272      * Returns the elements that are, loosely speaking, directly
   273      * enclosed by this element.
   274      *
   275      * A {@linkplain TypeElement#getEnclosedElements class or
   276      * interface} is considered to enclose the fields, methods,
   277      * constructors, and member types that it directly declares.
   278      *
   279      * A {@linkplain PackageElement#getEnclosedElements package}
   280      * encloses the top-level classes and interfaces within it, but is
   281      * not considered to enclose subpackages.
   282      *
   283      * Other kinds of elements are not currently considered to enclose
   284      * any elements; however, that may change as this API or the
   285      * programming language evolves.
   286      *
   287      * <p>Note that elements of certain kinds can be isolated using
   288      * methods in {@link ElementFilter}.
   289      *
   290      * @return the enclosed elements, or an empty list if none
   291      * @see PackageElement#getEnclosedElements
   292      * @see TypeElement#getEnclosedElements
   293      * @see Elements#getAllMembers
   294      * @jls 8.8.9 Default Constructor
   295      * @jls 8.9 Enums
   296      */
   297     List<? extends Element> getEnclosedElements();
   299     /**
   300      * Returns {@code true} if the argument represents the same
   301      * element as {@code this}, or {@code false} otherwise.
   302      *
   303      * <p>Note that the identity of an element involves implicit state
   304      * not directly accessible from the element's methods, including
   305      * state about the presence of unrelated types.  Element objects
   306      * created by different implementations of these interfaces should
   307      * <i>not</i> be expected to be equal even if &quot;the same&quot;
   308      * element is being modeled; this is analogous to the inequality
   309      * of {@code Class} objects for the same class file loaded through
   310      * different class loaders.
   311      *
   312      * @param obj  the object to be compared with this element
   313      * @return {@code true} if the specified object represents the same
   314      *          element as this
   315      */
   316     boolean equals(Object obj);
   318     /**
   319      * Obeys the general contract of {@link Object#hashCode Object.hashCode}.
   320      *
   321      * @see #equals
   322      */
   323     int hashCode();
   325     /**
   326      * Applies a visitor to this element.
   327      *
   328      * @param <R> the return type of the visitor's methods
   329      * @param <P> the type of the additional parameter to the visitor's methods
   330      * @param v   the visitor operating on this element
   331      * @param p   additional parameter to the visitor
   332      * @return a visitor-specified result
   333      */
   334     <R, P> R accept(ElementVisitor<R, P> v, P p);
   335 }

mercurial