Mon, 17 Dec 2012 14:54:42 +0000
8004814: javadoc should be able to detect default methods
Reviewed-by: jjg
Contributed-by: maurizio.cimadamore@oracle.com
1 /*
2 * Copyright (c) 1997, 2012, 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 */
26 package com.sun.tools.javadoc;
28 import java.lang.reflect.Modifier;
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;
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 */
50 public class MethodDocImpl
51 extends ExecutableMemberDocImpl implements MethodDoc {
53 /**
54 * constructor.
55 */
56 public MethodDocImpl(DocEnv env, MethodSymbol sym) {
57 super(env, sym);
58 }
60 /**
61 * constructor.
62 */
63 public MethodDocImpl(DocEnv env, MethodSymbol sym, TreePath treePath) {
64 super(env, sym, treePath);
65 }
67 /**
68 * Return true if it is a method, which it is.
69 * Note: constructors are not methods.
70 * This method is overridden by AnnotationTypeElementDocImpl.
71 *
72 * @return true
73 */
74 public boolean isMethod() {
75 return true;
76 }
78 /**
79 * Return true if this method is default
80 */
81 public boolean isDefault() {
82 return (sym.flags() & Flags.DEFAULT) != 0;
83 }
85 /**
86 * Return true if this method is abstract
87 */
88 public boolean isAbstract() {
89 //### This is dubious, but old 'javadoc' apparently does it.
90 //### I regard this as a bug and an obstacle to treating the
91 //### doclet API as a proper compile-time reflection facility.
92 //### (maddox 09/26/2000)
93 if (containingClass().isInterface()) {
94 //### Don't force creation of ClassDocImpl for super here.
95 // Abstract modifier is implicit. Strip/canonicalize it.
96 return false;
97 }
98 return Modifier.isAbstract(getModifiers());
99 }
101 /**
102 * Get return type.
103 *
104 * @return the return type of this method, null if it
105 * is a constructor.
106 */
107 public com.sun.javadoc.Type returnType() {
108 return TypeMaker.getType(env, sym.type.getReturnType(), false);
109 }
111 /**
112 * Return the class that originally defined the method that
113 * is overridden by the current definition, or null if no
114 * such class exists.
115 *
116 * @return a ClassDocImpl representing the superclass that
117 * originally defined this method, null if this method does
118 * not override a definition in a superclass.
119 */
120 public ClassDoc overriddenClass() {
121 com.sun.javadoc.Type t = overriddenType();
122 return (t != null) ? t.asClassDoc() : null;
123 }
125 /**
126 * Return the type containing the method that this method overrides.
127 * It may be a <code>ClassDoc</code> or a <code>ParameterizedType</code>.
128 */
129 public com.sun.javadoc.Type overriddenType() {
131 if ((sym.flags() & Flags.STATIC) != 0) {
132 return null;
133 }
135 ClassSymbol origin = (ClassSymbol)sym.owner;
136 for (Type t = env.types.supertype(origin.type);
137 t.hasTag(CLASS);
138 t = env.types.supertype(t)) {
139 ClassSymbol c = (ClassSymbol)t.tsym;
140 for (Scope.Entry e = c.members().lookup(sym.name); e.scope != null; e = e.next()) {
141 if (sym.overrides(e.sym, origin, env.types, true)) {
142 return TypeMaker.getType(env, t);
143 }
144 }
145 }
146 return null;
147 }
149 /**
150 * Return the method that this method overrides.
151 *
152 * @return a MethodDoc representing a method definition
153 * in a superclass this method overrides, null if
154 * this method does not override.
155 */
156 public MethodDoc overriddenMethod() {
158 // Real overriding only. Static members are simply hidden.
159 // Likewise for constructors, but the MethodSymbol.overrides
160 // method takes this into account.
161 if ((sym.flags() & Flags.STATIC) != 0) {
162 return null;
163 }
165 // Derived from com.sun.tools.javac.comp.Check.checkOverride .
167 ClassSymbol origin = (ClassSymbol)sym.owner;
168 for (Type t = env.types.supertype(origin.type);
169 t.hasTag(CLASS);
170 t = env.types.supertype(t)) {
171 ClassSymbol c = (ClassSymbol)t.tsym;
172 for (Scope.Entry e = c.members().lookup(sym.name); e.scope != null; e = e.next()) {
173 if (sym.overrides(e.sym, origin, env.types, true)) {
174 return env.getMethodDoc((MethodSymbol)e.sym);
175 }
176 }
177 }
178 return null;
179 }
181 /**
182 * Tests whether this method overrides another.
183 * The overridden method may be one declared in a superclass or
184 * a superinterface (unlike {@link #overriddenMethod()}).
185 *
186 * <p> When a non-abstract method overrides an abstract one, it is
187 * also said to <i>implement</i> the other.
188 *
189 * @param meth the other method to examine
190 * @return <tt>true</tt> if this method overrides the other
191 */
192 public boolean overrides(MethodDoc meth) {
193 MethodSymbol overridee = ((MethodDocImpl) meth).sym;
194 ClassSymbol origin = (ClassSymbol) sym.owner;
196 return sym.name == overridee.name &&
198 // not reflexive as per JLS
199 sym != overridee &&
201 // we don't care if overridee is static, though that wouldn't
202 // compile
203 !sym.isStatic() &&
205 // sym, whose declaring type is the origin, must be
206 // in a subtype of overridee's type
207 env.types.asSuper(origin.type, overridee.owner) != null &&
209 // check access and signatures; don't check return types
210 sym.overrides(overridee, origin, env.types, false);
211 }
214 public String name() {
215 return sym.name.toString();
216 }
218 public String qualifiedName() {
219 return sym.enclClass().getQualifiedName() + "." + sym.name;
220 }
222 /**
223 * Returns a string representation of this method. Includes the
224 * qualified signature, the qualified method name, and any type
225 * parameters. Type parameters follow the class name, as they do
226 * in the syntax for invoking methods with explicit type parameters.
227 */
228 public String toString() {
229 return sym.enclClass().getQualifiedName() +
230 "." + typeParametersString() + name() + signature();
231 }
232 }