1 /* 2 * Copyright (c) 1997, 2018, 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 com.sun.tools.javadoc.main; 27 28 import java.lang.reflect.Modifier; 29 30 import com.sun.javadoc.*; 31 import com.sun.source.util.TreePath; 32 import com.sun.tools.javac.code.*; 33 import com.sun.tools.javac.code.Symbol.*; 34 import com.sun.tools.javac.code.Type; 35 import static com.sun.tools.javac.code.TypeTag.CLASS; 36 37 /** 38 * Represents a method of a java class. 39 * 40 * <p><b>This is NOT part of any supported API. 41 * If you write code that depends on this, you do so at your own risk. 42 * This code and its internal interfaces are subject to change or 43 * deletion without notice.</b> 44 * 45 * @since 1.2 46 * @author Robert Field 47 * @author Neal Gafter (rewrite) 48 */ 49 50 @Deprecated(since="9", forRemoval=true) 51 @SuppressWarnings("removal") 52 public class MethodDocImpl 53 extends ExecutableMemberDocImpl implements MethodDoc { 54 55 /** 56 * constructor. 57 */ MethodDocImpl(DocEnv env, MethodSymbol sym)58 public MethodDocImpl(DocEnv env, MethodSymbol sym) { 59 super(env, sym); 60 } 61 62 /** 63 * constructor. 64 */ MethodDocImpl(DocEnv env, MethodSymbol sym, TreePath treePath)65 public MethodDocImpl(DocEnv env, MethodSymbol sym, TreePath treePath) { 66 super(env, sym, treePath); 67 } 68 69 /** 70 * Return true if it is a method, which it is. 71 * Note: constructors are not methods. 72 * This method is overridden by AnnotationTypeElementDocImpl. 73 * 74 * @return true 75 */ isMethod()76 public boolean isMethod() { 77 return true; 78 } 79 80 /** 81 * Return true if this method is default 82 */ isDefault()83 public boolean isDefault() { 84 return (sym.flags() & Flags.DEFAULT) != 0; 85 } 86 87 /** 88 * Return true if this method is abstract 89 */ isAbstract()90 public boolean isAbstract() { 91 return (Modifier.isAbstract(getModifiers()) && !isDefault()); 92 } 93 94 /** 95 * Get return type. 96 * 97 * @return the return type of this method, null if it 98 * is a constructor. 99 */ returnType()100 public com.sun.javadoc.Type returnType() { 101 return TypeMaker.getType(env, sym.type.getReturnType(), false); 102 } 103 104 /** 105 * Return the class that originally defined the method that 106 * is overridden by the current definition, or null if no 107 * such class exists. 108 * 109 * @return a ClassDocImpl representing the superclass that 110 * originally defined this method, null if this method does 111 * not override a definition in a superclass. 112 */ overriddenClass()113 public ClassDoc overriddenClass() { 114 com.sun.javadoc.Type t = overriddenType(); 115 return (t != null) ? t.asClassDoc() : null; 116 } 117 118 /** 119 * Return the type containing the method that this method overrides. 120 * It may be a <code>ClassDoc</code> or a <code>ParameterizedType</code>. 121 */ overriddenType()122 public com.sun.javadoc.Type overriddenType() { 123 124 if ((sym.flags() & Flags.STATIC) != 0) { 125 return null; 126 } 127 128 ClassSymbol origin = (ClassSymbol)sym.owner; 129 for (Type t = env.types.supertype(origin.type); 130 t.hasTag(CLASS); 131 t = env.types.supertype(t)) { 132 ClassSymbol c = (ClassSymbol)t.tsym; 133 for (Symbol sym2 : membersOf(c).getSymbolsByName(sym.name)) { 134 if (sym.overrides(sym2, origin, env.types, true)) { 135 return TypeMaker.getType(env, t); 136 } 137 } 138 } 139 return null; 140 } 141 142 /** 143 * Return the method that this method overrides. 144 * 145 * @return a MethodDoc representing a method definition 146 * in a superclass this method overrides, null if 147 * this method does not override. 148 */ overriddenMethod()149 public MethodDoc overriddenMethod() { 150 151 // Real overriding only. Static members are simply hidden. 152 // Likewise for constructors, but the MethodSymbol.overrides 153 // method takes this into account. 154 if ((sym.flags() & Flags.STATIC) != 0) { 155 return null; 156 } 157 158 // Derived from com.sun.tools.javac.comp.Check.checkOverride . 159 160 ClassSymbol origin = (ClassSymbol)sym.owner; 161 for (Type t = env.types.supertype(origin.type); 162 t.hasTag(CLASS); 163 t = env.types.supertype(t)) { 164 ClassSymbol c = (ClassSymbol)t.tsym; 165 for (Symbol sym2 : membersOf(c).getSymbolsByName(sym.name)) { 166 if (sym.overrides(sym2, origin, env.types, true)) { 167 return env.getMethodDoc((MethodSymbol)sym2); 168 } 169 } 170 } 171 return null; 172 } 173 174 /**Retrieve members of c, ignoring any CompletionFailures that occur. */ membersOf(ClassSymbol c)175 private Scope membersOf(ClassSymbol c) { 176 try { 177 return c.members(); 178 } catch (CompletionFailure cf) { 179 /* Quietly ignore completion failures and try again - the type 180 * for which the CompletionFailure was thrown shouldn't be completed 181 * again by the completer that threw the CompletionFailure. 182 */ 183 return membersOf(c); 184 } 185 } 186 187 /** 188 * Tests whether this method overrides another. 189 * The overridden method may be one declared in a superclass or 190 * a superinterface (unlike {@link #overriddenMethod()}). 191 * 192 * <p> When a non-abstract method overrides an abstract one, it is 193 * also said to <i>implement</i> the other. 194 * 195 * @param meth the other method to examine 196 * @return <tt>true</tt> if this method overrides the other 197 */ overrides(MethodDoc meth)198 public boolean overrides(MethodDoc meth) { 199 MethodSymbol overridee = ((MethodDocImpl) meth).sym; 200 ClassSymbol origin = (ClassSymbol) sym.owner; 201 202 return sym.name == overridee.name && 203 204 // not reflexive as per JLS 205 sym != overridee && 206 207 // we don't care if overridee is static, though that wouldn't 208 // compile 209 !sym.isStatic() && 210 211 // sym, whose declaring type is the origin, must be 212 // in a subtype of overridee's type 213 env.types.asSuper(origin.type, overridee.owner) != null && 214 215 // check access and signatures; don't check return types 216 sym.overrides(overridee, origin, env.types, false); 217 } 218 219 name()220 public String name() { 221 if (name == null) { 222 name = sym.name.toString(); 223 } 224 return name; 225 } 226 227 private String name; 228 qualifiedName()229 public String qualifiedName() { 230 if (qualifiedName == null) { 231 qualifiedName = sym.enclClass().getQualifiedName() + "." + sym.name; 232 } 233 return qualifiedName; 234 } 235 236 private String qualifiedName; 237 238 /** 239 * Returns a string representation of this method. Includes the 240 * qualified signature, the qualified method name, and any type 241 * parameters. Type parameters follow the class name, as they do 242 * in the syntax for invoking methods with explicit type parameters. 243 */ toString()244 public String toString() { 245 return sym.enclClass().getQualifiedName() + 246 "." + typeParametersString() + name() + signature(); 247 } 248 } 249