1 /* 2 * Copyright (c) 2005, 2016, 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.util; 27 28 29 import java.util.List; 30 import java.util.Map; 31 32 import javax.lang.model.AnnotatedConstruct; 33 import javax.lang.model.element.*; 34 35 36 /** 37 * Utility methods for operating on program elements. 38 * 39 * <p><b>Compatibility Note:</b> Methods may be added to this interface 40 * in future releases of the platform. 41 * 42 * @author Joseph D. Darcy 43 * @author Scott Seligman 44 * @author Peter von der Ahé 45 * @see javax.annotation.processing.ProcessingEnvironment#getElementUtils 46 * @since 1.6 47 */ 48 public interface Elements { 49 50 /** 51 * Returns a package given its fully qualified name. 52 * 53 * @param name fully qualified package name, or an empty string for an unnamed package 54 * @return the named package, or {@code null} if it cannot be found 55 */ 56 PackageElement getPackageElement(CharSequence name); 57 58 /** 59 * Returns a package given its fully qualified name, as seen from the given module. 60 * 61 * @param name fully qualified package name, or an empty string for an unnamed package 62 * @param module module relative to which the lookup should happen 63 * @return the named package, or {@code null} if it cannot be found 64 * @since 9 65 */ 66 PackageElement getPackageElement(ModuleElement module, CharSequence name); 67 68 /** 69 * Returns a type element given its canonical name. 70 * 71 * @param name the canonical name 72 * @return the named type element, or {@code null} if it cannot be found 73 */ 74 TypeElement getTypeElement(CharSequence name); 75 76 /** 77 * Returns a type element given its canonical name, as seen from the given module. 78 * 79 * @param name the canonical name 80 * @param module module relative to which the lookup should happen 81 * @return the named type element, or {@code null} if it cannot be found 82 * @since 9 83 */ 84 TypeElement getTypeElement(ModuleElement module, CharSequence name); 85 86 /** 87 * Returns a module element given its fully qualified name. 88 * 89 * @param name the name 90 * @return the named module element, or {@code null} if it cannot be found 91 * @since 9 92 */ 93 ModuleElement getModuleElement(CharSequence name); 94 95 /** 96 * Returns the values of an annotation's elements, including defaults. 97 * 98 * @see AnnotationMirror#getElementValues() 99 * @param a annotation to examine 100 * @return the values of the annotation's elements, including defaults 101 */ 102 Map<? extends ExecutableElement, ? extends AnnotationValue> 103 getElementValuesWithDefaults(AnnotationMirror a); 104 105 /** 106 * Returns the text of the documentation ("Javadoc") 107 * comment of an element. 108 * 109 * <p> A documentation comment of an element is a comment that 110 * begins with "{@code /**}" , ends with a separate 111 * "<code>*/</code>", and immediately precedes the element, 112 * ignoring white space. Therefore, a documentation comment 113 * contains at least three"{@code *}" characters. The text 114 * returned for the documentation comment is a processed form of 115 * the comment as it appears in source code. The leading "{@code 116 * /**}" and trailing "<code>*/</code>" are removed. For lines 117 * of the comment starting after the initial "{@code /**}", 118 * leading white space characters are discarded as are any 119 * consecutive "{@code *}" characters appearing after the white 120 * space or starting the line. The processed lines are then 121 * concatenated together (including line terminators) and 122 * returned. 123 * 124 * @param e the element being examined 125 * @return the documentation comment of the element, or {@code null} 126 * if there is none 127 * @jls 3.6 White Space 128 */ 129 String getDocComment(Element e); 130 131 /** 132 * Returns {@code true} if the element is deprecated, {@code false} otherwise. 133 * 134 * @param e the element being examined 135 * @return {@code true} if the element is deprecated, {@code false} otherwise 136 */ 137 boolean isDeprecated(Element e); 138 139 /** 140 * Returns the <em>origin</em> of the given element. 141 * 142 * <p>Note that if an element logically has a origin other 143 * than {@link Origin#EXPLICIT EXPLICIT}, an implementation may 144 * not be able to reliably determine this status if the element is 145 * created from a class file due to limitations of the fidelity of 146 * the class file format in preserving information from source 147 * code. 148 * 149 * @implSpec The default implementation of this method returns 150 * {@link Origin#EXPLICIT EXPLICIT}. 151 * 152 * @param e the element being examined 153 * @return the origin of the given element 154 * @since 9 155 */ 156 default Origin getOrigin(Element e) { 157 return Origin.EXPLICIT; 158 } 159 160 /** 161 * Returns the <em>origin</em> of the given annotation mirror. 162 * 163 * An annotation mirror is {@linkplain Origin#MANDATED mandated} 164 * if it is an implicitly declared <em>container annotation</em> 165 * used to hold repeated base annotations of a repeatable 166 * annotation type. 167 * 168 * <p>Note that if an annotation mirror logically has a origin 169 * other than {@link Origin#EXPLICIT EXPLICIT}, an implementation 170 * may not be able to reliably determine this status if the 171 * annotation mirror is created from a class file due to 172 * limitations of the fidelity of the class file format in 173 * preserving information from source code. 174 * 175 * @implSpec The default implementation of this method returns 176 * {@link Origin#EXPLICIT EXPLICIT}. 177 * 178 * @param c the construct the annotation mirror modifies 179 * @param a the annotation mirror being examined 180 * @return the origin of the given annotation mirror 181 * @jls 9.6.3 Repeatable Annotation Types 182 * @jls 9.7.5 Multiple Annotations of the Same Type 183 * @since 9 184 */ 185 default Origin getOrigin(AnnotatedConstruct c, 186 AnnotationMirror a) { 187 return Origin.EXPLICIT; 188 } 189 190 /** 191 * Returns the <em>origin</em> of the given module directive. 192 * 193 * <p>Note that if a directive logically has a origin other than 194 * {@link Origin#EXPLICIT EXPLICIT}, an implementation may not be 195 * able to reliably determine this status if the directive is 196 * created from a class file due to limitations of the fidelity of 197 * the class file format in preserving information from source 198 * code. 199 * 200 * @implSpec The default implementation of this method returns 201 * {@link Origin#EXPLICIT EXPLICIT}. 202 * 203 * @param m the module of the directive 204 * @param directive the module directive being examined 205 * @return the origin of the given directive 206 * @since 9 207 */ 208 default Origin getOrigin(ModuleElement m, 209 ModuleElement.Directive directive) { 210 return Origin.EXPLICIT; 211 } 212 213 /** 214 * The <em>origin</em> of a construct. The origin of a construct 215 * concerns the consistency between how a construct is declared in 216 * source code (explicitly, implicitly, or not at all) compared to 217 * how the construct is represented in this model. 218 * 219 * <p>Note that it is possible additional kinds of origin values 220 * will be added in future versions of the platform. 221 * 222 * @jls 13.1 The Form of a Binary 223 * @since 9 224 */ 225 enum Origin { 226 /** 227 * Describes a construct explicitly declared in source code. 228 */ 229 EXPLICIT, 230 231 /** 232 * A mandated construct is one that is not explicitly declared 233 * in the source code, but whose presence is mandated by the 234 * specification. Such a construct is said to be implicitly 235 * declared. 236 * 237 * One example of a mandated element is a <em>default 238 * constructor</em> in a class that contains no explicit 239 * constructor declarations. 240 * 241 * Another example of a mandated construct is an implicitly 242 * declared <em>container annotation</em> used to hold 243 * multiple annotations of a repeatable annotation type. 244 * 245 * @jls 8.8.9 Default Constructor 246 * @jls 9.6.3 Repeatable Annotation Types 247 * @jls 9.7.5 Multiple Annotations of the Same Type 248 */ 249 MANDATED, 250 251 /** 252 * A synthetic construct is one that is neither implicitly nor 253 * explicitly declared in the source code. Such a construct is 254 * typically a translation artifact created by a compiler. 255 */ 256 SYNTHETIC; 257 258 /** 259 * Returns {@code true} for values corresponding to constructs 260 * that are implicitly or explicitly declared, {@code false} 261 * otherwise. 262 * @return {@code true} for {@link EXPLICIT} and {@link 263 * MANDATED}, {@code false} otherwise. 264 */ 265 public boolean isDeclared() { 266 return this != SYNTHETIC; 267 } 268 } 269 270 /** 271 * Returns {@code true} if the executable element is a bridge 272 * method, {@code false} otherwise. 273 * 274 * @implSpec The default implementation of this method returns {@code false}. 275 * 276 * @param e the executable being examined 277 * @return {@code true} if the executable element is a bridge 278 * method, {@code false} otherwise 279 * @since 9 280 */ 281 default boolean isBridge(ExecutableElement e) { 282 return false; 283 } 284 285 /** 286 * Returns the <i>binary name</i> of a type element. 287 * 288 * @param type the type element being examined 289 * @return the binary name 290 * 291 * @see TypeElement#getQualifiedName 292 * @jls 13.1 The Form of a Binary 293 */ 294 Name getBinaryName(TypeElement type); 295 296 297 /** 298 * Returns the package of an element. The package of a package is 299 * itself. 300 * 301 * @param type the element being examined 302 * @return the package of an element 303 */ 304 PackageElement getPackageOf(Element type); 305 306 /** 307 * Returns the module of an element. The module of a module is 308 * itself. 309 * 310 * @param type the element being examined 311 * @return the module of an element 312 * @since 9 313 */ 314 ModuleElement getModuleOf(Element type); 315 316 /** 317 * Returns all members of a type element, whether inherited or 318 * declared directly. For a class the result also includes its 319 * constructors, but not local or anonymous classes. 320 * 321 * <p>Note that elements of certain kinds can be isolated using 322 * methods in {@link ElementFilter}. 323 * 324 * @param type the type being examined 325 * @return all members of the type 326 * @see Element#getEnclosedElements 327 */ 328 List<? extends Element> getAllMembers(TypeElement type); 329 330 /** 331 * Returns all annotations <i>present</i> on an element, whether 332 * directly present or present via inheritance. 333 * 334 * @param e the element being examined 335 * @return all annotations of the element 336 * @see Element#getAnnotationMirrors 337 * @see javax.lang.model.AnnotatedConstruct 338 */ 339 List<? extends AnnotationMirror> getAllAnnotationMirrors(Element e); 340 341 /** 342 * Tests whether one type, method, or field hides another. 343 * 344 * @param hider the first element 345 * @param hidden the second element 346 * @return {@code true} if and only if the first element hides 347 * the second 348 */ 349 boolean hides(Element hider, Element hidden); 350 351 /** 352 * Tests whether one method, as a member of a given type, 353 * overrides another method. 354 * When a non-abstract method overrides an abstract one, the 355 * former is also said to <i>implement</i> the latter. 356 * 357 * <p> In the simplest and most typical usage, the value of the 358 * {@code type} parameter will simply be the class or interface 359 * directly enclosing {@code overrider} (the possibly-overriding 360 * method). For example, suppose {@code m1} represents the method 361 * {@code String.hashCode} and {@code m2} represents {@code 362 * Object.hashCode}. We can then ask whether {@code m1} overrides 363 * {@code m2} within the class {@code String} (it does): 364 * 365 * <blockquote> 366 * {@code assert elements.overrides(m1, m2, 367 * elements.getTypeElement("java.lang.String")); } 368 * </blockquote> 369 * 370 * A more interesting case can be illustrated by the following example 371 * in which a method in type {@code A} does not override a 372 * like-named method in type {@code B}: 373 * 374 * <blockquote> 375 * {@code class A { public void m() {} } }<br> 376 * {@code interface B { void m(); } }<br> 377 * ...<br> 378 * {@code m1 = ...; // A.m }<br> 379 * {@code m2 = ...; // B.m }<br> 380 * {@code assert ! elements.overrides(m1, m2, 381 * elements.getTypeElement("A")); } 382 * </blockquote> 383 * 384 * When viewed as a member of a third type {@code C}, however, 385 * the method in {@code A} does override the one in {@code B}: 386 * 387 * <blockquote> 388 * {@code class C extends A implements B {} }<br> 389 * ...<br> 390 * {@code assert elements.overrides(m1, m2, 391 * elements.getTypeElement("C")); } 392 * </blockquote> 393 * 394 * @param overrider the first method, possible overrider 395 * @param overridden the second method, possibly being overridden 396 * @param type the type of which the first method is a member 397 * @return {@code true} if and only if the first method overrides 398 * the second 399 * @jls 8.4.8 Inheritance, Overriding, and Hiding 400 * @jls 9.4.1 Inheritance and Overriding 401 */ 402 boolean overrides(ExecutableElement overrider, ExecutableElement overridden, 403 TypeElement type); 404 405 /** 406 * Returns the text of a <i>constant expression</i> representing a 407 * primitive value or a string. 408 * The text returned is in a form suitable for representing the value 409 * in source code. 410 * 411 * @param value a primitive value or string 412 * @return the text of a constant expression 413 * @throws IllegalArgumentException if the argument is not a primitive 414 * value or string 415 * 416 * @see VariableElement#getConstantValue() 417 */ 418 String getConstantExpression(Object value); 419 420 /** 421 * Prints a representation of the elements to the given writer in 422 * the specified order. The main purpose of this method is for 423 * diagnostics. The exact format of the output is <em>not</em> 424 * specified and is subject to change. 425 * 426 * @param w the writer to print the output to 427 * @param elements the elements to print 428 */ 429 void printElements(java.io.Writer w, Element... elements); 430 431 /** 432 * Return a name with the same sequence of characters as the 433 * argument. 434 * 435 * @param cs the character sequence to return as a name 436 * @return a name with the same sequence of characters as the argument 437 */ 438 Name getName(CharSequence cs); 439 440 /** 441 * Returns {@code true} if the type element is a functional interface, {@code false} otherwise. 442 * 443 * @param type the type element being examined 444 * @return {@code true} if the element is a functional interface, {@code false} otherwise 445 * @jls 9.8 Functional Interfaces 446 * @since 1.8 447 */ 448 boolean isFunctionalInterface(TypeElement type); 449 }