Thu, 24 May 2018 16:48:51 +0800
Merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.tools.javadoc; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import java.lang.reflect.Modifier; |
aoqi@0 | 29 | |
aoqi@0 | 30 | import com.sun.javadoc.*; |
aoqi@0 | 31 | import com.sun.source.util.TreePath; |
aoqi@0 | 32 | import com.sun.tools.javac.code.*; |
aoqi@0 | 33 | import com.sun.tools.javac.code.Symbol.*; |
aoqi@0 | 34 | import com.sun.tools.javac.code.Type; |
aoqi@0 | 35 | import static com.sun.tools.javac.code.TypeTag.CLASS; |
aoqi@0 | 36 | |
aoqi@0 | 37 | /** |
aoqi@0 | 38 | * Represents a method of a java class. |
aoqi@0 | 39 | * |
aoqi@0 | 40 | * <p><b>This is NOT part of any supported API. |
aoqi@0 | 41 | * If you write code that depends on this, you do so at your own risk. |
aoqi@0 | 42 | * This code and its internal interfaces are subject to change or |
aoqi@0 | 43 | * deletion without notice.</b> |
aoqi@0 | 44 | * |
aoqi@0 | 45 | * @since 1.2 |
aoqi@0 | 46 | * @author Robert Field |
aoqi@0 | 47 | * @author Neal Gafter (rewrite) |
aoqi@0 | 48 | */ |
aoqi@0 | 49 | |
aoqi@0 | 50 | public class MethodDocImpl |
aoqi@0 | 51 | extends ExecutableMemberDocImpl implements MethodDoc { |
aoqi@0 | 52 | |
aoqi@0 | 53 | /** |
aoqi@0 | 54 | * constructor. |
aoqi@0 | 55 | */ |
aoqi@0 | 56 | public MethodDocImpl(DocEnv env, MethodSymbol sym) { |
aoqi@0 | 57 | super(env, sym); |
aoqi@0 | 58 | } |
aoqi@0 | 59 | |
aoqi@0 | 60 | /** |
aoqi@0 | 61 | * constructor. |
aoqi@0 | 62 | */ |
aoqi@0 | 63 | public MethodDocImpl(DocEnv env, MethodSymbol sym, TreePath treePath) { |
aoqi@0 | 64 | super(env, sym, treePath); |
aoqi@0 | 65 | } |
aoqi@0 | 66 | |
aoqi@0 | 67 | /** |
aoqi@0 | 68 | * Return true if it is a method, which it is. |
aoqi@0 | 69 | * Note: constructors are not methods. |
aoqi@0 | 70 | * This method is overridden by AnnotationTypeElementDocImpl. |
aoqi@0 | 71 | * |
aoqi@0 | 72 | * @return true |
aoqi@0 | 73 | */ |
aoqi@0 | 74 | public boolean isMethod() { |
aoqi@0 | 75 | return true; |
aoqi@0 | 76 | } |
aoqi@0 | 77 | |
aoqi@0 | 78 | /** |
aoqi@0 | 79 | * Return true if this method is default |
aoqi@0 | 80 | */ |
aoqi@0 | 81 | public boolean isDefault() { |
aoqi@0 | 82 | return (sym.flags() & Flags.DEFAULT) != 0; |
aoqi@0 | 83 | } |
aoqi@0 | 84 | |
aoqi@0 | 85 | /** |
aoqi@0 | 86 | * Return true if this method is abstract |
aoqi@0 | 87 | */ |
aoqi@0 | 88 | public boolean isAbstract() { |
aoqi@0 | 89 | return (Modifier.isAbstract(getModifiers()) && !isDefault()); |
aoqi@0 | 90 | } |
aoqi@0 | 91 | |
aoqi@0 | 92 | /** |
aoqi@0 | 93 | * Get return type. |
aoqi@0 | 94 | * |
aoqi@0 | 95 | * @return the return type of this method, null if it |
aoqi@0 | 96 | * is a constructor. |
aoqi@0 | 97 | */ |
aoqi@0 | 98 | public com.sun.javadoc.Type returnType() { |
aoqi@0 | 99 | return TypeMaker.getType(env, sym.type.getReturnType(), false); |
aoqi@0 | 100 | } |
aoqi@0 | 101 | |
aoqi@0 | 102 | /** |
aoqi@0 | 103 | * Return the class that originally defined the method that |
aoqi@0 | 104 | * is overridden by the current definition, or null if no |
aoqi@0 | 105 | * such class exists. |
aoqi@0 | 106 | * |
aoqi@0 | 107 | * @return a ClassDocImpl representing the superclass that |
aoqi@0 | 108 | * originally defined this method, null if this method does |
aoqi@0 | 109 | * not override a definition in a superclass. |
aoqi@0 | 110 | */ |
aoqi@0 | 111 | public ClassDoc overriddenClass() { |
aoqi@0 | 112 | com.sun.javadoc.Type t = overriddenType(); |
aoqi@0 | 113 | return (t != null) ? t.asClassDoc() : null; |
aoqi@0 | 114 | } |
aoqi@0 | 115 | |
aoqi@0 | 116 | /** |
aoqi@0 | 117 | * Return the type containing the method that this method overrides. |
aoqi@0 | 118 | * It may be a <code>ClassDoc</code> or a <code>ParameterizedType</code>. |
aoqi@0 | 119 | */ |
aoqi@0 | 120 | public com.sun.javadoc.Type overriddenType() { |
aoqi@0 | 121 | |
aoqi@0 | 122 | if ((sym.flags() & Flags.STATIC) != 0) { |
aoqi@0 | 123 | return null; |
aoqi@0 | 124 | } |
aoqi@0 | 125 | |
aoqi@0 | 126 | ClassSymbol origin = (ClassSymbol)sym.owner; |
aoqi@0 | 127 | for (Type t = env.types.supertype(origin.type); |
aoqi@0 | 128 | t.hasTag(CLASS); |
aoqi@0 | 129 | t = env.types.supertype(t)) { |
aoqi@0 | 130 | ClassSymbol c = (ClassSymbol)t.tsym; |
jlahoda@2906 | 131 | for (Scope.Entry e = membersOf(c).lookup(sym.name); e.scope != null; e = e.next()) { |
aoqi@0 | 132 | if (sym.overrides(e.sym, origin, env.types, true)) { |
aoqi@0 | 133 | return TypeMaker.getType(env, t); |
aoqi@0 | 134 | } |
aoqi@0 | 135 | } |
aoqi@0 | 136 | } |
aoqi@0 | 137 | return null; |
aoqi@0 | 138 | } |
aoqi@0 | 139 | |
aoqi@0 | 140 | /** |
aoqi@0 | 141 | * Return the method that this method overrides. |
aoqi@0 | 142 | * |
aoqi@0 | 143 | * @return a MethodDoc representing a method definition |
aoqi@0 | 144 | * in a superclass this method overrides, null if |
aoqi@0 | 145 | * this method does not override. |
aoqi@0 | 146 | */ |
aoqi@0 | 147 | public MethodDoc overriddenMethod() { |
aoqi@0 | 148 | |
aoqi@0 | 149 | // Real overriding only. Static members are simply hidden. |
aoqi@0 | 150 | // Likewise for constructors, but the MethodSymbol.overrides |
aoqi@0 | 151 | // method takes this into account. |
aoqi@0 | 152 | if ((sym.flags() & Flags.STATIC) != 0) { |
aoqi@0 | 153 | return null; |
aoqi@0 | 154 | } |
aoqi@0 | 155 | |
aoqi@0 | 156 | // Derived from com.sun.tools.javac.comp.Check.checkOverride . |
aoqi@0 | 157 | |
aoqi@0 | 158 | ClassSymbol origin = (ClassSymbol)sym.owner; |
aoqi@0 | 159 | for (Type t = env.types.supertype(origin.type); |
aoqi@0 | 160 | t.hasTag(CLASS); |
aoqi@0 | 161 | t = env.types.supertype(t)) { |
aoqi@0 | 162 | ClassSymbol c = (ClassSymbol)t.tsym; |
jlahoda@2906 | 163 | for (Scope.Entry e = membersOf(c).lookup(sym.name); e.scope != null; e = e.next()) { |
aoqi@0 | 164 | if (sym.overrides(e.sym, origin, env.types, true)) { |
aoqi@0 | 165 | return env.getMethodDoc((MethodSymbol)e.sym); |
aoqi@0 | 166 | } |
aoqi@0 | 167 | } |
aoqi@0 | 168 | } |
aoqi@0 | 169 | return null; |
aoqi@0 | 170 | } |
aoqi@0 | 171 | |
jlahoda@2906 | 172 | /**Retrieve members of c, ignoring any CompletionFailures that occur. */ |
jlahoda@2906 | 173 | private Scope membersOf(ClassSymbol c) { |
jlahoda@2906 | 174 | try { |
jlahoda@2906 | 175 | return c.members(); |
jlahoda@2906 | 176 | } catch (CompletionFailure cf) { |
jlahoda@2906 | 177 | /* Quietly ignore completion failures and try again - the type |
jlahoda@2906 | 178 | * for which the CompletionFailure was thrown shouldn't be completed |
jlahoda@2906 | 179 | * again by the completer that threw the CompletionFailure. |
jlahoda@2906 | 180 | */ |
jlahoda@2906 | 181 | return membersOf(c); |
jlahoda@2906 | 182 | } |
jlahoda@2906 | 183 | } |
jlahoda@2906 | 184 | |
aoqi@0 | 185 | /** |
aoqi@0 | 186 | * Tests whether this method overrides another. |
aoqi@0 | 187 | * The overridden method may be one declared in a superclass or |
aoqi@0 | 188 | * a superinterface (unlike {@link #overriddenMethod()}). |
aoqi@0 | 189 | * |
aoqi@0 | 190 | * <p> When a non-abstract method overrides an abstract one, it is |
aoqi@0 | 191 | * also said to <i>implement</i> the other. |
aoqi@0 | 192 | * |
aoqi@0 | 193 | * @param meth the other method to examine |
aoqi@0 | 194 | * @return <tt>true</tt> if this method overrides the other |
aoqi@0 | 195 | */ |
aoqi@0 | 196 | public boolean overrides(MethodDoc meth) { |
aoqi@0 | 197 | MethodSymbol overridee = ((MethodDocImpl) meth).sym; |
aoqi@0 | 198 | ClassSymbol origin = (ClassSymbol) sym.owner; |
aoqi@0 | 199 | |
aoqi@0 | 200 | return sym.name == overridee.name && |
aoqi@0 | 201 | |
aoqi@0 | 202 | // not reflexive as per JLS |
aoqi@0 | 203 | sym != overridee && |
aoqi@0 | 204 | |
aoqi@0 | 205 | // we don't care if overridee is static, though that wouldn't |
aoqi@0 | 206 | // compile |
aoqi@0 | 207 | !sym.isStatic() && |
aoqi@0 | 208 | |
aoqi@0 | 209 | // sym, whose declaring type is the origin, must be |
aoqi@0 | 210 | // in a subtype of overridee's type |
aoqi@0 | 211 | env.types.asSuper(origin.type, overridee.owner) != null && |
aoqi@0 | 212 | |
aoqi@0 | 213 | // check access and signatures; don't check return types |
aoqi@0 | 214 | sym.overrides(overridee, origin, env.types, false); |
aoqi@0 | 215 | } |
aoqi@0 | 216 | |
aoqi@0 | 217 | |
aoqi@0 | 218 | public String name() { |
aoqi@0 | 219 | if (name == null) { |
aoqi@0 | 220 | name = sym.name.toString(); |
aoqi@0 | 221 | } |
aoqi@0 | 222 | return name; |
aoqi@0 | 223 | } |
aoqi@0 | 224 | |
aoqi@0 | 225 | private String name; |
aoqi@0 | 226 | |
aoqi@0 | 227 | public String qualifiedName() { |
aoqi@0 | 228 | if (qualifiedName == null) { |
aoqi@0 | 229 | qualifiedName = sym.enclClass().getQualifiedName() + "." + sym.name; |
aoqi@0 | 230 | } |
aoqi@0 | 231 | return qualifiedName; |
aoqi@0 | 232 | } |
aoqi@0 | 233 | |
aoqi@0 | 234 | private String qualifiedName; |
aoqi@0 | 235 | |
aoqi@0 | 236 | /** |
aoqi@0 | 237 | * Returns a string representation of this method. Includes the |
aoqi@0 | 238 | * qualified signature, the qualified method name, and any type |
aoqi@0 | 239 | * parameters. Type parameters follow the class name, as they do |
aoqi@0 | 240 | * in the syntax for invoking methods with explicit type parameters. |
aoqi@0 | 241 | */ |
aoqi@0 | 242 | public String toString() { |
aoqi@0 | 243 | return sym.enclClass().getQualifiedName() + |
aoqi@0 | 244 | "." + typeParametersString() + name() + signature(); |
aoqi@0 | 245 | } |
aoqi@0 | 246 | } |