duke@1: /* jjg@1326: * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as ohair@554: * published by the Free Software Foundation. Oracle designates this duke@1: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. duke@1: */ duke@1: duke@1: package javax.lang.model.util; duke@1: duke@1: duke@1: import java.util.List; duke@1: import java.util.Map; duke@1: duke@1: import javax.lang.model.element.*; duke@1: import javax.lang.model.type.*; duke@1: duke@1: duke@1: /** duke@1: * Utility methods for operating on program elements. duke@1: * duke@1: *

Compatibility Note: Methods may be added to this interface duke@1: * in future releases of the platform. duke@1: * duke@1: * @author Joseph D. Darcy duke@1: * @author Scott Seligman duke@1: * @author Peter von der Ahé duke@1: * @see javax.annotation.processing.ProcessingEnvironment#getElementUtils duke@1: * @since 1.6 duke@1: */ duke@1: public interface Elements { duke@1: duke@1: /** duke@1: * Returns a package given its fully qualified name. duke@1: * duke@1: * @param name fully qualified package name, or "" for an unnamed package duke@1: * @return the named package, or {@code null} if it cannot be found duke@1: */ duke@1: PackageElement getPackageElement(CharSequence name); duke@1: duke@1: /** duke@1: * Returns a type element given its canonical name. duke@1: * duke@1: * @param name the canonical name duke@1: * @return the named type element, or {@code null} if it cannot be found duke@1: */ duke@1: TypeElement getTypeElement(CharSequence name); duke@1: duke@1: /** duke@1: * Returns the values of an annotation's elements, including defaults. duke@1: * duke@1: * @see AnnotationMirror#getElementValues() duke@1: * @param a annotation to examine duke@1: * @return the values of the annotation's elements, including defaults duke@1: */ duke@1: Map duke@1: getElementValuesWithDefaults(AnnotationMirror a); duke@1: duke@1: /** duke@1: * Returns the text of the documentation ("Javadoc") duke@1: * comment of an element. duke@1: * darcy@425: *

A documentation comment of an element is a comment that darcy@425: * begins with "{@code /**}" , ends with a separate jjg@1326: * "*/", and immediately precedes the element, darcy@425: * ignoring white space. Therefore, a documentation comment darcy@425: * contains at least three"{@code *}" characters. The text darcy@425: * returned for the documentation comment is a processed form of darcy@425: * the comment as it appears in source code. The leading "{@code jjg@1326: * /**}" and trailing "*/" are removed. For lines darcy@425: * of the comment starting after the initial "{@code /**}", darcy@425: * leading white space characters are discarded as are any darcy@425: * consecutive "{@code *}" characters appearing after the white darcy@425: * space or starting the line. The processed lines are then darcy@425: * concatenated together (including line terminators) and darcy@425: * returned. darcy@425: * duke@1: * @param e the element being examined duke@1: * @return the documentation comment of the element, or {@code null} duke@1: * if there is none jjh@972: * @jls 3.6 White Space duke@1: */ duke@1: String getDocComment(Element e); duke@1: duke@1: /** duke@1: * Returns {@code true} if the element is deprecated, {@code false} otherwise. duke@1: * duke@1: * @param e the element being examined duke@1: * @return {@code true} if the element is deprecated, {@code false} otherwise duke@1: */ duke@1: boolean isDeprecated(Element e); duke@1: duke@1: /** duke@1: * Returns the binary name of a type element. duke@1: * duke@1: * @param type the type element being examined duke@1: * @return the binary name duke@1: * duke@1: * @see TypeElement#getQualifiedName jjh@972: * @jls 13.1 The Form of a Binary duke@1: */ duke@1: Name getBinaryName(TypeElement type); duke@1: duke@1: duke@1: /** duke@1: * Returns the package of an element. The package of a package is duke@1: * itself. duke@1: * duke@1: * @param type the element being examined duke@1: * @return the package of an element duke@1: */ duke@1: PackageElement getPackageOf(Element type); duke@1: duke@1: /** duke@1: * Returns all members of a type element, whether inherited or duke@1: * declared directly. For a class the result also includes its duke@1: * constructors, but not local or anonymous classes. duke@1: * duke@1: *

Note that elements of certain kinds can be isolated using duke@1: * methods in {@link ElementFilter}. duke@1: * duke@1: * @param type the type being examined duke@1: * @return all members of the type duke@1: * @see Element#getEnclosedElements duke@1: */ duke@1: List getAllMembers(TypeElement type); duke@1: duke@1: /** duke@1: * Returns all annotations of an element, whether duke@1: * inherited or directly present. duke@1: * duke@1: * @param e the element being examined duke@1: * @return all annotations of the element duke@1: * @see Element#getAnnotationMirrors duke@1: */ duke@1: List getAllAnnotationMirrors(Element e); duke@1: duke@1: /** duke@1: * Tests whether one type, method, or field hides another. duke@1: * duke@1: * @param hider the first element duke@1: * @param hidden the second element duke@1: * @return {@code true} if and only if the first element hides duke@1: * the second duke@1: */ duke@1: boolean hides(Element hider, Element hidden); duke@1: duke@1: /** duke@1: * Tests whether one method, as a member of a given type, duke@1: * overrides another method. duke@1: * When a non-abstract method overrides an abstract one, the duke@1: * former is also said to implement the latter. duke@1: * duke@1: *

In the simplest and most typical usage, the value of the duke@1: * {@code type} parameter will simply be the class or interface duke@1: * directly enclosing {@code overrider} (the possibly-overriding duke@1: * method). For example, suppose {@code m1} represents the method duke@1: * {@code String.hashCode} and {@code m2} represents {@code duke@1: * Object.hashCode}. We can then ask whether {@code m1} overrides duke@1: * {@code m2} within the class {@code String} (it does): duke@1: * duke@1: *

duke@1: * {@code assert elements.overrides(m1, m2, duke@1: * elements.getTypeElement("java.lang.String")); } duke@1: *
duke@1: * duke@1: * A more interesting case can be illustrated by the following example duke@1: * in which a method in type {@code A} does not override a duke@1: * like-named method in type {@code B}: duke@1: * duke@1: *
duke@1: * {@code class A { public void m() {} } }
duke@1: * {@code interface B { void m(); } }
duke@1: * ...
duke@1: * {@code m1 = ...; // A.m }
duke@1: * {@code m2 = ...; // B.m }
duke@1: * {@code assert ! elements.overrides(m1, m2, duke@1: * elements.getTypeElement("A")); } duke@1: *
duke@1: * duke@1: * When viewed as a member of a third type {@code C}, however, duke@1: * the method in {@code A} does override the one in {@code B}: duke@1: * duke@1: *
duke@1: * {@code class C extends A implements B {} }
duke@1: * ...
duke@1: * {@code assert elements.overrides(m1, m2, duke@1: * elements.getTypeElement("C")); } duke@1: *
duke@1: * duke@1: * @param overrider the first method, possible overrider duke@1: * @param overridden the second method, possibly being overridden duke@1: * @param type the type of which the first method is a member duke@1: * @return {@code true} if and only if the first method overrides duke@1: * the second jjh@972: * @jls 8.4.8 Inheritance, Overriding, and Hiding jjh@972: * @jls 9.4.1 Inheritance and Overriding duke@1: */ duke@1: boolean overrides(ExecutableElement overrider, ExecutableElement overridden, duke@1: TypeElement type); duke@1: duke@1: /** duke@1: * Returns the text of a constant expression representing a duke@1: * primitive value or a string. duke@1: * The text returned is in a form suitable for representing the value duke@1: * in source code. duke@1: * duke@1: * @param value a primitive value or string duke@1: * @return the text of a constant expression duke@1: * @throws IllegalArgumentException if the argument is not a primitive duke@1: * value or string duke@1: * duke@1: * @see VariableElement#getConstantValue() duke@1: */ duke@1: String getConstantExpression(Object value); duke@1: duke@1: /** duke@1: * Prints a representation of the elements to the given writer in duke@1: * the specified order. The main purpose of this method is for duke@1: * diagnostics. The exact format of the output is not duke@1: * specified and is subject to change. duke@1: * duke@1: * @param w the writer to print the output to duke@1: * @param elements the elements to print duke@1: */ duke@1: void printElements(java.io.Writer w, Element... elements); duke@1: duke@1: /** duke@1: * Return a name with the same sequence of characters as the duke@1: * argument. duke@1: * duke@1: * @param cs the character sequence to return as a name duke@1: */ duke@1: Name getName(CharSequence cs); duke@1: }