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

changeset 1
9a66ca7c79fa
child 554
9d9f26857129
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/javax/lang/model/element/Element.java	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,258 @@
     1.4 +/*
     1.5 + * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Sun designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Sun in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.25 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.26 + * have any questions.
    1.27 + */
    1.28 +
    1.29 +package javax.lang.model.element;
    1.30 +
    1.31 +
    1.32 +import java.lang.annotation.Annotation;
    1.33 +import java.lang.annotation.AnnotationTypeMismatchException;
    1.34 +import java.lang.annotation.IncompleteAnnotationException;
    1.35 +import java.util.List;
    1.36 +import java.util.Set;
    1.37 +
    1.38 +import javax.lang.model.element.Modifier;
    1.39 +import javax.lang.model.type.*;
    1.40 +import javax.lang.model.util.*;
    1.41 +
    1.42 +
    1.43 +/**
    1.44 + * Represents a program element such as a package, class, or method.
    1.45 + * Each element represents a static, language-level construct
    1.46 + * (and not, for example, a runtime construct of the virtual machine).
    1.47 + *
    1.48 + * <p> Elements should be compared using the {@link #equals(Object)}
    1.49 + * method.  There is no guarantee that any particular element will
    1.50 + * always be represented by the same object.
    1.51 + *
    1.52 + * <p> To implement operations based on the class of an {@code
    1.53 + * Element} object, either use a {@linkplain ElementVisitor visitor} or
    1.54 + * use the result of the {@link #getKind} method.  Using {@code
    1.55 + * instanceof} is <em>not</em> necessarily a reliable idiom for
    1.56 + * determining the effective class of an object in this modeling
    1.57 + * hierarchy since an implementation may choose to have a single object
    1.58 + * implement multiple {@code Element} subinterfaces.
    1.59 + *
    1.60 + * @author Joseph D. Darcy
    1.61 + * @author Scott Seligman
    1.62 + * @author Peter von der Ah&eacute;
    1.63 + * @see Elements
    1.64 + * @see TypeMirror
    1.65 + * @since 1.6
    1.66 + */
    1.67 +public interface Element {
    1.68 +
    1.69 +    /**
    1.70 +     * Returns the type defined by this element.
    1.71 +     *
    1.72 +     * <p> A generic element defines a family of types, not just one.
    1.73 +     * If this is a generic element, a <i>prototypical</i> type is
    1.74 +     * returned.  This is the element's invocation on the
    1.75 +     * type variables corresponding to its own formal type parameters.
    1.76 +     * For example,
    1.77 +     * for the generic class element {@code C<N extends Number>},
    1.78 +     * the parameterized type {@code C<N>} is returned.
    1.79 +     * The {@link Types} utility interface has more general methods
    1.80 +     * for obtaining the full range of types defined by an element.
    1.81 +     *
    1.82 +     * @see Types
    1.83 +     *
    1.84 +     * @return the type defined by this element
    1.85 +     */
    1.86 +    TypeMirror asType();
    1.87 +
    1.88 +    /**
    1.89 +     * Returns the {@code kind} of this element.
    1.90 +     *
    1.91 +     * @return the kind of this element
    1.92 +     */
    1.93 +    ElementKind getKind();
    1.94 +
    1.95 +    /**
    1.96 +     * Returns the annotations that are directly present on this element.
    1.97 +     *
    1.98 +     * <p> To get inherited annotations as well, use
    1.99 +     * {@link Elements#getAllAnnotationMirrors(Element) getAllAnnotationMirrors}.
   1.100 +     *
   1.101 +     * @see ElementFilter
   1.102 +     *
   1.103 +     * @return the annotations directly present on this element;
   1.104 +     *          an empty list if there are none
   1.105 +     */
   1.106 +    List<? extends AnnotationMirror> getAnnotationMirrors();
   1.107 +
   1.108 +    /**
   1.109 +     * Returns this element's annotation for the specified type if
   1.110 +     * such an annotation is present, else {@code null}.  The
   1.111 +     * annotation may be either inherited or directly present on this
   1.112 +     * element.
   1.113 +     *
   1.114 +     * <p> The annotation returned by this method could contain an element
   1.115 +     * whose value is of type {@code Class}.
   1.116 +     * This value cannot be returned directly:  information necessary to
   1.117 +     * locate and load a class (such as the class loader to use) is
   1.118 +     * not available, and the class might not be loadable at all.
   1.119 +     * Attempting to read a {@code Class} object by invoking the relevant
   1.120 +     * method on the returned annotation
   1.121 +     * will result in a {@link MirroredTypeException},
   1.122 +     * from which the corresponding {@link TypeMirror} may be extracted.
   1.123 +     * Similarly, attempting to read a {@code Class[]}-valued element
   1.124 +     * will result in a {@link MirroredTypesException}.
   1.125 +     *
   1.126 +     * <blockquote>
   1.127 +     * <i>Note:</i> This method is unlike others in this and related
   1.128 +     * interfaces.  It operates on runtime reflective information &mdash;
   1.129 +     * representations of annotation types currently loaded into the
   1.130 +     * VM &mdash; rather than on the representations defined by and used
   1.131 +     * throughout these interfaces.  Consequently, calling methods on
   1.132 +     * the returned annotation object can throw many of the exceptions
   1.133 +     * that can be thrown when calling methods on an annotation object
   1.134 +     * returned by core reflection.  This method is intended for
   1.135 +     * callers that are written to operate on a known, fixed set of
   1.136 +     * annotation types.
   1.137 +     * </blockquote>
   1.138 +     *
   1.139 +     * @param <A>  the annotation type
   1.140 +     * @param annotationType  the {@code Class} object corresponding to
   1.141 +     *          the annotation type
   1.142 +     * @return this element's annotation for the specified annotation
   1.143 +     *         type if present on this element, else {@code null}
   1.144 +     *
   1.145 +     * @see #getAnnotationMirrors()
   1.146 +     * @see java.lang.reflect.AnnotatedElement#getAnnotation
   1.147 +     * @see EnumConstantNotPresentException
   1.148 +     * @see AnnotationTypeMismatchException
   1.149 +     * @see IncompleteAnnotationException
   1.150 +     * @see MirroredTypeException
   1.151 +     * @see MirroredTypesException
   1.152 +     */
   1.153 +    <A extends Annotation> A getAnnotation(Class<A> annotationType);
   1.154 +
   1.155 +    /**
   1.156 +     * Returns the modifiers of this element, excluding annotations.
   1.157 +     * Implicit modifiers, such as the {@code public} and {@code static}
   1.158 +     * modifiers of interface members, are included.
   1.159 +     *
   1.160 +     * @return the modifiers of this element, or an empty set if there are none
   1.161 +     */
   1.162 +    Set<Modifier> getModifiers();
   1.163 +
   1.164 +    /**
   1.165 +     * Returns the simple (unqualified) name of this element.
   1.166 +     * The name of a generic type does not include any reference
   1.167 +     * to its formal type parameters.
   1.168 +     * For example, the simple name of the type element
   1.169 +     * {@code java.util.Set<E>} is {@code "Set"}.
   1.170 +     * If this element represents an unnamed package, an empty name is
   1.171 +     * returned.  If it represents a constructor, the name "{@code
   1.172 +     * <init>}" is returned.  If it represents a static initializer,
   1.173 +     * the name "{@code <clinit>}" is returned.  If it represents an
   1.174 +     * anonymous class or instance initializer, an empty name is
   1.175 +     * returned.
   1.176 +     *
   1.177 +     * @return the simple name of this element
   1.178 +     */
   1.179 +    Name getSimpleName();
   1.180 +
   1.181 +    /**
   1.182 +     * Returns the innermost element
   1.183 +     * within which this element is, loosely speaking, enclosed.
   1.184 +     * <ul>
   1.185 +     * <li> If this element is one whose declaration is lexically enclosed
   1.186 +     * immediately within the declaration of another element, that other
   1.187 +     * element is returned.
   1.188 +     * <li> If this is a top-level type, its package is returned.
   1.189 +     * <li> If this is a package, {@code null} is returned.
   1.190 +     * <li> If this is a type parameter, {@code null} is returned.
   1.191 +     * </ul>
   1.192 +     *
   1.193 +     * @return the enclosing element, or {@code null} if there is none
   1.194 +     * @see Elements#getPackageOf
   1.195 +     */
   1.196 +    Element getEnclosingElement();
   1.197 +
   1.198 +    /**
   1.199 +     * Returns the elements that are, loosely speaking, directly
   1.200 +     * enclosed by this element.
   1.201 +     *
   1.202 +     * A class or interface is considered to enclose the fields,
   1.203 +     * methods, constructors, and member types that it directly
   1.204 +     * declares.  This includes any (implicit) default constructor and
   1.205 +     * the implicit {@code values} and {@code valueOf} methods of an
   1.206 +     * enum type.
   1.207 +     *
   1.208 +     * A package encloses the top-level classes and interfaces within
   1.209 +     * it, but is not considered to enclose subpackages.
   1.210 +     *
   1.211 +     * Other kinds of elements are not currently considered to enclose
   1.212 +     * any elements; however, that may change as this API or the
   1.213 +     * programming language evolves.
   1.214 +     *
   1.215 +     * <p>Note that elements of certain kinds can be isolated using
   1.216 +     * methods in {@link ElementFilter}.
   1.217 +     *
   1.218 +     * @return the enclosed elements, or an empty list if none
   1.219 +     * @see Elements#getAllMembers
   1.220 +     * @jls3 8.8.9 Default Constructor
   1.221 +     * @jls3 8.9 Enums
   1.222 +     */
   1.223 +    List<? extends Element> getEnclosedElements();
   1.224 +
   1.225 +    /**
   1.226 +     * Returns {@code true} if the argument represents the same
   1.227 +     * element as {@code this}, or {@code false} otherwise.
   1.228 +     *
   1.229 +     * <p>Note that the identity of an element involves implicit state
   1.230 +     * not directly accessible from the element's methods, including
   1.231 +     * state about the presence of unrelated types.  Element objects
   1.232 +     * created by different implementations of these interfaces should
   1.233 +     * <i>not</i> be expected to be equal even if &quot;the same&quot;
   1.234 +     * element is being modeled; this is analogous to the inequality
   1.235 +     * of {@code Class} objects for the same class file loaded through
   1.236 +     * different class loaders.
   1.237 +     *
   1.238 +     * @param obj  the object to be compared with this element
   1.239 +     * @return {@code true} if the specified object represents the same
   1.240 +     *          element as this
   1.241 +     */
   1.242 +    boolean equals(Object obj);
   1.243 +
   1.244 +    /**
   1.245 +     * Obeys the general contract of {@link Object#hashCode Object.hashCode}.
   1.246 +     *
   1.247 +     * @see #equals
   1.248 +     */
   1.249 +    int hashCode();
   1.250 +
   1.251 +    /**
   1.252 +     * Applies a visitor to this element.
   1.253 +     *
   1.254 +     * @param <R> the return type of the visitor's methods
   1.255 +     * @param <P> the type of the additional parameter to the visitor's methods
   1.256 +     * @param v   the visitor operating on this element
   1.257 +     * @param p   additional parameter to the visitor
   1.258 +     * @return a visitor-specified result
   1.259 +     */
   1.260 +    <R, P> R accept(ElementVisitor<R, P> v, P p);
   1.261 +}

mercurial