1 /* 2 * Copyright (c) 2005, 2020, 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 */ 25 26 package javax.lang.model.element; 27 28 import java.util.List; 29 import javax.lang.model.type.*; 30 import javax.lang.model.util.*; 31 32 /** 33 * Represents a class or interface program element. Provides access 34 * to information about the type and its members. Note that an enum 35 * type and a record type are kinds of classes and an annotation type is a kind of 36 * interface. 37 * 38 * <p> While a {@code TypeElement} represents a class or interface 39 * <i>element</i>, a {@link DeclaredType} represents a class 40 * or interface <i>type</i>, the latter being a use 41 * (or <i>invocation</i>) of the former. 42 * The distinction is most apparent with generic types, 43 * for which a single element can define a whole 44 * family of types. For example, the element 45 * {@code java.util.Set} corresponds to the parameterized types 46 * {@code java.util.Set<String>} and {@code java.util.Set<Number>} 47 * (and many others), and to the raw type {@code java.util.Set}. 48 * 49 * <p> Each method of this interface that returns a list of elements 50 * will return them in the order that is natural for the underlying 51 * source of program information. For example, if the underlying 52 * source of information is Java source code, then the elements will be 53 * returned in source code order. 54 * 55 * @author Joseph D. Darcy 56 * @author Scott Seligman 57 * @author Peter von der Ahé 58 * @see DeclaredType 59 * @since 1.6 60 */ 61 public interface TypeElement extends Element, Parameterizable, QualifiedNameable { 62 /** 63 * Returns the type defined by this type element, returning the 64 * <i>prototypical</i> type for an element representing a generic type. 65 * 66 * <p>A generic element defines a family of types, not just one. 67 * If this is a generic element, a prototypical type is 68 * returned which has the element's invocation on the 69 * type variables corresponding to its own formal type parameters. 70 * For example, 71 * for the generic class element {@code C<N extends Number>}, 72 * the parameterized type {@code C<N>} is returned. 73 * The {@link Types} utility interface has more general methods 74 * for obtaining the full range of types defined by an element. 75 * 76 * @return the type defined by this type element 77 * 78 * @see Types#asMemberOf(DeclaredType, Element) 79 * @see Types#getDeclaredType(TypeElement, TypeMirror...) 80 */ 81 @Override 82 TypeMirror asType(); 83 84 /** 85 * Returns the fields, methods, constructors, record components, 86 * and member types that are directly declared in this class or 87 * interface. 88 * 89 * This includes any {@linkplain Elements.Origin#MANDATED 90 * mandated} elements such as the (implicit) default constructor 91 * and the implicit {@code values} and {@code valueOf} methods of 92 * an enum type. 93 * 94 * @apiNote As a particular instance of the {@linkplain 95 * javax.lang.model.element general accuracy requirements} and the 96 * ordering behavior required of this interface, the list of 97 * enclosed elements will be returned in the natural order for the 98 * originating source of information about the type. For example, 99 * if the information about the type is originating from a source 100 * file, the elements will be returned in source code order. 101 * (However, in that case the the ordering of {@linkplain 102 * Elements.Origin#MANDATED implicitly declared} elements, such as 103 * default constructors, is not specified.) 104 * 105 * @return the enclosed elements in proper order, or an empty list if none 106 * 107 * @jls 8.8.9 Default Constructor 108 * @jls 8.9.3 Enum Members 109 */ 110 @Override 111 List<? extends Element> getEnclosedElements(); 112 113 /** 114 * Returns the <i>nesting kind</i> of this type element. 115 * 116 * @return the nesting kind of this type element 117 */ 118 NestingKind getNestingKind(); 119 120 /** 121 * Returns the fully qualified name of this type element. 122 * More precisely, it returns the <i>canonical</i> name. 123 * For local and anonymous classes, which do not have canonical names, 124 * an empty name is returned. 125 * 126 * <p>The name of a generic type does not include any reference 127 * to its formal type parameters. 128 * For example, the fully qualified name of the interface 129 * {@code java.util.Set<E>} is "{@code java.util.Set}". 130 * Nested types use "{@code .}" as a separator, as in 131 * "{@code java.util.Map.Entry}". 132 * 133 * @return the fully qualified name of this class or interface, or 134 * an empty name if none 135 * 136 * @see Elements#getBinaryName 137 * @jls 6.7 Fully Qualified Names and Canonical Names 138 */ 139 Name getQualifiedName(); 140 141 /** 142 * Returns the simple name of this type element. 143 * 144 * For an anonymous class, an empty name is returned. 145 * 146 * @return the simple name of this class or interface, 147 * an empty name for an anonymous class 148 * 149 */ 150 @Override 151 Name getSimpleName(); 152 153 /** 154 * Returns the direct superclass of this type element. 155 * If this type element represents an interface or the class 156 * {@code java.lang.Object}, then a {@link NoType} 157 * with kind {@link TypeKind#NONE NONE} is returned. 158 * 159 * @return the direct superclass, or a {@code NoType} if there is none 160 */ 161 TypeMirror getSuperclass(); 162 163 /** 164 * Returns the interface types directly implemented by this class 165 * or extended by this interface. 166 * 167 * @return the interface types directly implemented by this class 168 * or extended by this interface, or an empty list if there are none 169 */ 170 List<? extends TypeMirror> getInterfaces(); 171 172 /** 173 * Returns the formal type parameters of this type element 174 * in declaration order. 175 * 176 * @return the formal type parameters, or an empty list 177 * if there are none 178 */ 179 List<? extends TypeParameterElement> getTypeParameters(); 180 181 /** 182 * {@preview Associated with records, a preview feature of the Java language. 183 * 184 * This method is associated with <i>records</i>, a preview 185 * feature of the Java language. Preview features 186 * may be removed in a future release, or upgraded to permanent 187 * features of the Java language.} 188 * 189 * Returns the record components of this type element in 190 * declaration order. 191 * 192 * @implSpec The default implementations of this method returns an 193 * empty and unmodifiable list. 194 * 195 * @return the record components, or an empty list if there are 196 * none 197 * 198 * @since 14 199 */ 200 @jdk.internal.PreviewFeature(feature=jdk.internal.PreviewFeature.Feature.RECORDS, 201 essentialAPI=false) 202 @SuppressWarnings("preview") 203 default List<? extends RecordComponentElement> getRecordComponents() { 204 return List.of(); 205 } 206 207 /** 208 * {@preview Associated with sealed classes, a preview feature of the Java language. 209 * 210 * This method is associated with <i>sealed classes</i>, a preview 211 * feature of the Java language. Preview features 212 * may be removed in a future release, or upgraded to permanent 213 * features of the Java language.} 214 * Returns the permitted classes of this type element in 215 * declaration order. 216 * 217 * @implSpec The default implementations of this method returns an 218 * empty and unmodifiable list. 219 * 220 * @return the permitted classes, or an empty list if there are none 221 * 222 * @since 15 223 */ 224 @jdk.internal.PreviewFeature(feature=jdk.internal.PreviewFeature.Feature.SEALED_CLASSES, 225 essentialAPI=false) 226 default List<? extends TypeMirror> getPermittedSubclasses() { 227 return List.of(); 228 } 229 230 /** 231 * Returns the package of a top-level type and returns the 232 * immediately lexically enclosing element for a {@linkplain 233 * NestingKind#isNested nested} type. 234 * 235 * @return the package of a top-level type, the immediately 236 * lexically enclosing element for a nested type 237 */ 238 @Override 239 Element getEnclosingElement(); 240 }