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

Sat, 01 Dec 2007 00:00:00 +0000

author
duke
date
Sat, 01 Dec 2007 00:00:00 +0000
changeset 1
9a66ca7c79fa
child 554
9d9f26857129
permissions
-rw-r--r--

Initial load

duke@1 1 /*
duke@1 2 * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
duke@1 7 * published by the Free Software Foundation. Sun designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
duke@1 9 * by Sun in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
duke@1 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@1 22 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@1 23 * have any questions.
duke@1 24 */
duke@1 25
duke@1 26 package javax.lang.model.element;
duke@1 27
duke@1 28
duke@1 29 import java.lang.annotation.Annotation;
duke@1 30 import java.lang.annotation.AnnotationTypeMismatchException;
duke@1 31 import java.lang.annotation.IncompleteAnnotationException;
duke@1 32 import java.util.List;
duke@1 33 import java.util.Set;
duke@1 34
duke@1 35 import javax.lang.model.element.Modifier;
duke@1 36 import javax.lang.model.type.*;
duke@1 37 import javax.lang.model.util.*;
duke@1 38
duke@1 39
duke@1 40 /**
duke@1 41 * Represents a program element such as a package, class, or method.
duke@1 42 * Each element represents a static, language-level construct
duke@1 43 * (and not, for example, a runtime construct of the virtual machine).
duke@1 44 *
duke@1 45 * <p> Elements should be compared using the {@link #equals(Object)}
duke@1 46 * method. There is no guarantee that any particular element will
duke@1 47 * always be represented by the same object.
duke@1 48 *
duke@1 49 * <p> To implement operations based on the class of an {@code
duke@1 50 * Element} object, either use a {@linkplain ElementVisitor visitor} or
duke@1 51 * use the result of the {@link #getKind} method. Using {@code
duke@1 52 * instanceof} is <em>not</em> necessarily a reliable idiom for
duke@1 53 * determining the effective class of an object in this modeling
duke@1 54 * hierarchy since an implementation may choose to have a single object
duke@1 55 * implement multiple {@code Element} subinterfaces.
duke@1 56 *
duke@1 57 * @author Joseph D. Darcy
duke@1 58 * @author Scott Seligman
duke@1 59 * @author Peter von der Ah&eacute;
duke@1 60 * @see Elements
duke@1 61 * @see TypeMirror
duke@1 62 * @since 1.6
duke@1 63 */
duke@1 64 public interface Element {
duke@1 65
duke@1 66 /**
duke@1 67 * Returns the type defined by this element.
duke@1 68 *
duke@1 69 * <p> A generic element defines a family of types, not just one.
duke@1 70 * If this is a generic element, a <i>prototypical</i> type is
duke@1 71 * returned. This is the element's invocation on the
duke@1 72 * type variables corresponding to its own formal type parameters.
duke@1 73 * For example,
duke@1 74 * for the generic class element {@code C<N extends Number>},
duke@1 75 * the parameterized type {@code C<N>} is returned.
duke@1 76 * The {@link Types} utility interface has more general methods
duke@1 77 * for obtaining the full range of types defined by an element.
duke@1 78 *
duke@1 79 * @see Types
duke@1 80 *
duke@1 81 * @return the type defined by this element
duke@1 82 */
duke@1 83 TypeMirror asType();
duke@1 84
duke@1 85 /**
duke@1 86 * Returns the {@code kind} of this element.
duke@1 87 *
duke@1 88 * @return the kind of this element
duke@1 89 */
duke@1 90 ElementKind getKind();
duke@1 91
duke@1 92 /**
duke@1 93 * Returns the annotations that are directly present on this element.
duke@1 94 *
duke@1 95 * <p> To get inherited annotations as well, use
duke@1 96 * {@link Elements#getAllAnnotationMirrors(Element) getAllAnnotationMirrors}.
duke@1 97 *
duke@1 98 * @see ElementFilter
duke@1 99 *
duke@1 100 * @return the annotations directly present on this element;
duke@1 101 * an empty list if there are none
duke@1 102 */
duke@1 103 List<? extends AnnotationMirror> getAnnotationMirrors();
duke@1 104
duke@1 105 /**
duke@1 106 * Returns this element's annotation for the specified type if
duke@1 107 * such an annotation is present, else {@code null}. The
duke@1 108 * annotation may be either inherited or directly present on this
duke@1 109 * element.
duke@1 110 *
duke@1 111 * <p> The annotation returned by this method could contain an element
duke@1 112 * whose value is of type {@code Class}.
duke@1 113 * This value cannot be returned directly: information necessary to
duke@1 114 * locate and load a class (such as the class loader to use) is
duke@1 115 * not available, and the class might not be loadable at all.
duke@1 116 * Attempting to read a {@code Class} object by invoking the relevant
duke@1 117 * method on the returned annotation
duke@1 118 * will result in a {@link MirroredTypeException},
duke@1 119 * from which the corresponding {@link TypeMirror} may be extracted.
duke@1 120 * Similarly, attempting to read a {@code Class[]}-valued element
duke@1 121 * will result in a {@link MirroredTypesException}.
duke@1 122 *
duke@1 123 * <blockquote>
duke@1 124 * <i>Note:</i> This method is unlike others in this and related
duke@1 125 * interfaces. It operates on runtime reflective information &mdash;
duke@1 126 * representations of annotation types currently loaded into the
duke@1 127 * VM &mdash; rather than on the representations defined by and used
duke@1 128 * throughout these interfaces. Consequently, calling methods on
duke@1 129 * the returned annotation object can throw many of the exceptions
duke@1 130 * that can be thrown when calling methods on an annotation object
duke@1 131 * returned by core reflection. This method is intended for
duke@1 132 * callers that are written to operate on a known, fixed set of
duke@1 133 * annotation types.
duke@1 134 * </blockquote>
duke@1 135 *
duke@1 136 * @param <A> the annotation type
duke@1 137 * @param annotationType the {@code Class} object corresponding to
duke@1 138 * the annotation type
duke@1 139 * @return this element's annotation for the specified annotation
duke@1 140 * type if present on this element, else {@code null}
duke@1 141 *
duke@1 142 * @see #getAnnotationMirrors()
duke@1 143 * @see java.lang.reflect.AnnotatedElement#getAnnotation
duke@1 144 * @see EnumConstantNotPresentException
duke@1 145 * @see AnnotationTypeMismatchException
duke@1 146 * @see IncompleteAnnotationException
duke@1 147 * @see MirroredTypeException
duke@1 148 * @see MirroredTypesException
duke@1 149 */
duke@1 150 <A extends Annotation> A getAnnotation(Class<A> annotationType);
duke@1 151
duke@1 152 /**
duke@1 153 * Returns the modifiers of this element, excluding annotations.
duke@1 154 * Implicit modifiers, such as the {@code public} and {@code static}
duke@1 155 * modifiers of interface members, are included.
duke@1 156 *
duke@1 157 * @return the modifiers of this element, or an empty set if there are none
duke@1 158 */
duke@1 159 Set<Modifier> getModifiers();
duke@1 160
duke@1 161 /**
duke@1 162 * Returns the simple (unqualified) name of this element.
duke@1 163 * The name of a generic type does not include any reference
duke@1 164 * to its formal type parameters.
duke@1 165 * For example, the simple name of the type element
duke@1 166 * {@code java.util.Set<E>} is {@code "Set"}.
duke@1 167 * If this element represents an unnamed package, an empty name is
duke@1 168 * returned. If it represents a constructor, the name "{@code
duke@1 169 * <init>}" is returned. If it represents a static initializer,
duke@1 170 * the name "{@code <clinit>}" is returned. If it represents an
duke@1 171 * anonymous class or instance initializer, an empty name is
duke@1 172 * returned.
duke@1 173 *
duke@1 174 * @return the simple name of this element
duke@1 175 */
duke@1 176 Name getSimpleName();
duke@1 177
duke@1 178 /**
duke@1 179 * Returns the innermost element
duke@1 180 * within which this element is, loosely speaking, enclosed.
duke@1 181 * <ul>
duke@1 182 * <li> If this element is one whose declaration is lexically enclosed
duke@1 183 * immediately within the declaration of another element, that other
duke@1 184 * element is returned.
duke@1 185 * <li> If this is a top-level type, its package is returned.
duke@1 186 * <li> If this is a package, {@code null} is returned.
duke@1 187 * <li> If this is a type parameter, {@code null} is returned.
duke@1 188 * </ul>
duke@1 189 *
duke@1 190 * @return the enclosing element, or {@code null} if there is none
duke@1 191 * @see Elements#getPackageOf
duke@1 192 */
duke@1 193 Element getEnclosingElement();
duke@1 194
duke@1 195 /**
duke@1 196 * Returns the elements that are, loosely speaking, directly
duke@1 197 * enclosed by this element.
duke@1 198 *
duke@1 199 * A class or interface is considered to enclose the fields,
duke@1 200 * methods, constructors, and member types that it directly
duke@1 201 * declares. This includes any (implicit) default constructor and
duke@1 202 * the implicit {@code values} and {@code valueOf} methods of an
duke@1 203 * enum type.
duke@1 204 *
duke@1 205 * A package encloses the top-level classes and interfaces within
duke@1 206 * it, but is not considered to enclose subpackages.
duke@1 207 *
duke@1 208 * Other kinds of elements are not currently considered to enclose
duke@1 209 * any elements; however, that may change as this API or the
duke@1 210 * programming language evolves.
duke@1 211 *
duke@1 212 * <p>Note that elements of certain kinds can be isolated using
duke@1 213 * methods in {@link ElementFilter}.
duke@1 214 *
duke@1 215 * @return the enclosed elements, or an empty list if none
duke@1 216 * @see Elements#getAllMembers
duke@1 217 * @jls3 8.8.9 Default Constructor
duke@1 218 * @jls3 8.9 Enums
duke@1 219 */
duke@1 220 List<? extends Element> getEnclosedElements();
duke@1 221
duke@1 222 /**
duke@1 223 * Returns {@code true} if the argument represents the same
duke@1 224 * element as {@code this}, or {@code false} otherwise.
duke@1 225 *
duke@1 226 * <p>Note that the identity of an element involves implicit state
duke@1 227 * not directly accessible from the element's methods, including
duke@1 228 * state about the presence of unrelated types. Element objects
duke@1 229 * created by different implementations of these interfaces should
duke@1 230 * <i>not</i> be expected to be equal even if &quot;the same&quot;
duke@1 231 * element is being modeled; this is analogous to the inequality
duke@1 232 * of {@code Class} objects for the same class file loaded through
duke@1 233 * different class loaders.
duke@1 234 *
duke@1 235 * @param obj the object to be compared with this element
duke@1 236 * @return {@code true} if the specified object represents the same
duke@1 237 * element as this
duke@1 238 */
duke@1 239 boolean equals(Object obj);
duke@1 240
duke@1 241 /**
duke@1 242 * Obeys the general contract of {@link Object#hashCode Object.hashCode}.
duke@1 243 *
duke@1 244 * @see #equals
duke@1 245 */
duke@1 246 int hashCode();
duke@1 247
duke@1 248 /**
duke@1 249 * Applies a visitor to this element.
duke@1 250 *
duke@1 251 * @param <R> the return type of the visitor's methods
duke@1 252 * @param <P> the type of the additional parameter to the visitor's methods
duke@1 253 * @param v the visitor operating on this element
duke@1 254 * @param p additional parameter to the visitor
duke@1 255 * @return a visitor-specified result
duke@1 256 */
duke@1 257 <R, P> R accept(ElementVisitor<R, P> v, P p);
duke@1 258 }

mercurial