Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/src/share/classes/com/sun/tools/javadoc/MethodDocImpl.java
38899 views
/*1* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.tools.javadoc;2627import java.lang.reflect.Modifier;2829import com.sun.javadoc.*;30import com.sun.source.util.TreePath;31import com.sun.tools.javac.code.*;32import com.sun.tools.javac.code.Symbol.*;33import com.sun.tools.javac.code.Type;34import static com.sun.tools.javac.code.TypeTag.CLASS;3536/**37* Represents a method of a java class.38*39* <p><b>This is NOT part of any supported API.40* If you write code that depends on this, you do so at your own risk.41* This code and its internal interfaces are subject to change or42* deletion without notice.</b>43*44* @since 1.245* @author Robert Field46* @author Neal Gafter (rewrite)47*/4849public class MethodDocImpl50extends ExecutableMemberDocImpl implements MethodDoc {5152/**53* constructor.54*/55public MethodDocImpl(DocEnv env, MethodSymbol sym) {56super(env, sym);57}5859/**60* constructor.61*/62public MethodDocImpl(DocEnv env, MethodSymbol sym, TreePath treePath) {63super(env, sym, treePath);64}6566/**67* Return true if it is a method, which it is.68* Note: constructors are not methods.69* This method is overridden by AnnotationTypeElementDocImpl.70*71* @return true72*/73public boolean isMethod() {74return true;75}7677/**78* Return true if this method is default79*/80public boolean isDefault() {81return (sym.flags() & Flags.DEFAULT) != 0;82}8384/**85* Return true if this method is abstract86*/87public boolean isAbstract() {88return (Modifier.isAbstract(getModifiers()) && !isDefault());89}9091/**92* Get return type.93*94* @return the return type of this method, null if it95* is a constructor.96*/97public com.sun.javadoc.Type returnType() {98return TypeMaker.getType(env, sym.type.getReturnType(), false);99}100101/**102* Return the class that originally defined the method that103* is overridden by the current definition, or null if no104* such class exists.105*106* @return a ClassDocImpl representing the superclass that107* originally defined this method, null if this method does108* not override a definition in a superclass.109*/110public ClassDoc overriddenClass() {111com.sun.javadoc.Type t = overriddenType();112return (t != null) ? t.asClassDoc() : null;113}114115/**116* Return the type containing the method that this method overrides.117* It may be a <code>ClassDoc</code> or a <code>ParameterizedType</code>.118*/119public com.sun.javadoc.Type overriddenType() {120121if ((sym.flags() & Flags.STATIC) != 0) {122return null;123}124125ClassSymbol origin = (ClassSymbol)sym.owner;126for (Type t = env.types.supertype(origin.type);127t.hasTag(CLASS);128t = env.types.supertype(t)) {129ClassSymbol c = (ClassSymbol)t.tsym;130for (Scope.Entry e = membersOf(c).lookup(sym.name); e.scope != null; e = e.next()) {131if (sym.overrides(e.sym, origin, env.types, true)) {132return TypeMaker.getType(env, t);133}134}135}136return null;137}138139/**140* Return the method that this method overrides.141*142* @return a MethodDoc representing a method definition143* in a superclass this method overrides, null if144* this method does not override.145*/146public MethodDoc overriddenMethod() {147148// Real overriding only. Static members are simply hidden.149// Likewise for constructors, but the MethodSymbol.overrides150// method takes this into account.151if ((sym.flags() & Flags.STATIC) != 0) {152return null;153}154155// Derived from com.sun.tools.javac.comp.Check.checkOverride .156157ClassSymbol origin = (ClassSymbol)sym.owner;158for (Type t = env.types.supertype(origin.type);159t.hasTag(CLASS);160t = env.types.supertype(t)) {161ClassSymbol c = (ClassSymbol)t.tsym;162for (Scope.Entry e = membersOf(c).lookup(sym.name); e.scope != null; e = e.next()) {163if (sym.overrides(e.sym, origin, env.types, true)) {164return env.getMethodDoc((MethodSymbol)e.sym);165}166}167}168return null;169}170171/**Retrieve members of c, ignoring any CompletionFailures that occur. */172private Scope membersOf(ClassSymbol c) {173try {174return c.members();175} catch (CompletionFailure cf) {176/* Quietly ignore completion failures and try again - the type177* for which the CompletionFailure was thrown shouldn't be completed178* again by the completer that threw the CompletionFailure.179*/180return membersOf(c);181}182}183184/**185* Tests whether this method overrides another.186* The overridden method may be one declared in a superclass or187* a superinterface (unlike {@link #overriddenMethod()}).188*189* <p> When a non-abstract method overrides an abstract one, it is190* also said to <i>implement</i> the other.191*192* @param meth the other method to examine193* @return <tt>true</tt> if this method overrides the other194*/195public boolean overrides(MethodDoc meth) {196MethodSymbol overridee = ((MethodDocImpl) meth).sym;197ClassSymbol origin = (ClassSymbol) sym.owner;198199return sym.name == overridee.name &&200201// not reflexive as per JLS202sym != overridee &&203204// we don't care if overridee is static, though that wouldn't205// compile206!sym.isStatic() &&207208// sym, whose declaring type is the origin, must be209// in a subtype of overridee's type210env.types.asSuper(origin.type, overridee.owner) != null &&211212// check access and signatures; don't check return types213sym.overrides(overridee, origin, env.types, false);214}215216217public String name() {218if (name == null) {219name = sym.name.toString();220}221return name;222}223224private String name;225226public String qualifiedName() {227if (qualifiedName == null) {228qualifiedName = sym.enclClass().getQualifiedName() + "." + sym.name;229}230return qualifiedName;231}232233private String qualifiedName;234235/**236* Returns a string representation of this method. Includes the237* qualified signature, the qualified method name, and any type238* parameters. Type parameters follow the class name, as they do239* in the syntax for invoking methods with explicit type parameters.240*/241public String toString() {242return sym.enclClass().getQualifiedName() +243"." + typeParametersString() + name() + signature();244}245}246247248