Tue, 23 Oct 2012 13:20:37 -0700
8000741: refactor javadoc to use abstraction to handle relative paths
Reviewed-by: darcy
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.tools.javac.code.*;
32 import com.sun.tools.javac.code.Symbol.*;
33 import com.sun.tools.javac.code.Type;
34 import com.sun.tools.javac.code.TypeTags;
35 import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
36 import com.sun.tools.javac.util.Position;
38 /**
39 * Represents a method of a java class.
40 *
41 * <p><b>This is NOT part of any supported API.
42 * If you write code that depends on this, you do so at your own risk.
43 * This code and its internal interfaces are subject to change or
44 * deletion without notice.</b>
45 *
46 * @since 1.2
47 * @author Robert Field
48 * @author Neal Gafter (rewrite)
49 */
51 public class MethodDocImpl
52 extends ExecutableMemberDocImpl implements MethodDoc {
54 /**
55 * constructor.
56 */
57 public MethodDocImpl(DocEnv env, MethodSymbol sym) {
58 super(env, sym);
59 }
61 /**
62 * constructor.
63 */
64 public MethodDocImpl(DocEnv env, MethodSymbol sym,
65 String docComment, JCMethodDecl tree, Position.LineMap lineMap) {
66 super(env, sym, docComment, tree, lineMap);
67 }
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 */
76 public boolean isMethod() {
77 return true;
78 }
80 /**
81 * Return true if this method is abstract
82 */
83 public boolean isAbstract() {
84 //### This is dubious, but old 'javadoc' apparently does it.
85 //### I regard this as a bug and an obstacle to treating the
86 //### doclet API as a proper compile-time reflection facility.
87 //### (maddox 09/26/2000)
88 if (containingClass().isInterface()) {
89 //### Don't force creation of ClassDocImpl for super here.
90 // Abstract modifier is implicit. Strip/canonicalize it.
91 return false;
92 }
93 return Modifier.isAbstract(getModifiers());
94 }
96 /**
97 * Get return type.
98 *
99 * @return the return type of this method, null if it
100 * is a constructor.
101 */
102 public com.sun.javadoc.Type returnType() {
103 return TypeMaker.getType(env, sym.type.getReturnType(), false);
104 }
106 /**
107 * Return the class that originally defined the method that
108 * is overridden by the current definition, or null if no
109 * such class exists.
110 *
111 * @return a ClassDocImpl representing the superclass that
112 * originally defined this method, null if this method does
113 * not override a definition in a superclass.
114 */
115 public ClassDoc overriddenClass() {
116 com.sun.javadoc.Type t = overriddenType();
117 return (t != null) ? t.asClassDoc() : null;
118 }
120 /**
121 * Return the type containing the method that this method overrides.
122 * It may be a <code>ClassDoc</code> or a <code>ParameterizedType</code>.
123 */
124 public com.sun.javadoc.Type overriddenType() {
126 if ((sym.flags() & Flags.STATIC) != 0) {
127 return null;
128 }
130 ClassSymbol origin = (ClassSymbol)sym.owner;
131 for (Type t = env.types.supertype(origin.type);
132 t.tag == TypeTags.CLASS;
133 t = env.types.supertype(t)) {
134 ClassSymbol c = (ClassSymbol)t.tsym;
135 for (Scope.Entry e = c.members().lookup(sym.name); e.scope != null; e = e.next()) {
136 if (sym.overrides(e.sym, origin, env.types, true)) {
137 return TypeMaker.getType(env, t);
138 }
139 }
140 }
141 return null;
142 }
144 /**
145 * Return the method that this method overrides.
146 *
147 * @return a MethodDoc representing a method definition
148 * in a superclass this method overrides, null if
149 * this method does not override.
150 */
151 public MethodDoc overriddenMethod() {
153 // Real overriding only. Static members are simply hidden.
154 // Likewise for constructors, but the MethodSymbol.overrides
155 // method takes this into account.
156 if ((sym.flags() & Flags.STATIC) != 0) {
157 return null;
158 }
160 // Derived from com.sun.tools.javac.comp.Check.checkOverride .
162 ClassSymbol origin = (ClassSymbol)sym.owner;
163 for (Type t = env.types.supertype(origin.type);
164 t.tag == TypeTags.CLASS;
165 t = env.types.supertype(t)) {
166 ClassSymbol c = (ClassSymbol)t.tsym;
167 for (Scope.Entry e = c.members().lookup(sym.name); e.scope != null; e = e.next()) {
168 if (sym.overrides(e.sym, origin, env.types, true)) {
169 return env.getMethodDoc((MethodSymbol)e.sym);
170 }
171 }
172 }
173 return null;
174 }
176 /**
177 * Tests whether this method overrides another.
178 * The overridden method may be one declared in a superclass or
179 * a superinterface (unlike {@link #overriddenMethod()}).
180 *
181 * <p> When a non-abstract method overrides an abstract one, it is
182 * also said to <i>implement</i> the other.
183 *
184 * @param meth the other method to examine
185 * @return <tt>true</tt> if this method overrides the other
186 */
187 public boolean overrides(MethodDoc meth) {
188 MethodSymbol overridee = ((MethodDocImpl) meth).sym;
189 ClassSymbol origin = (ClassSymbol) sym.owner;
191 return sym.name == overridee.name &&
193 // not reflexive as per JLS
194 sym != overridee &&
196 // we don't care if overridee is static, though that wouldn't
197 // compile
198 !sym.isStatic() &&
200 // sym, whose declaring type is the origin, must be
201 // in a subtype of overridee's type
202 env.types.asSuper(origin.type, overridee.owner) != null &&
204 // check access and signatures; don't check return types
205 sym.overrides(overridee, origin, env.types, false);
206 }
209 public String name() {
210 return sym.name.toString();
211 }
213 public String qualifiedName() {
214 return sym.enclClass().getQualifiedName() + "." + sym.name;
215 }
217 /**
218 * Returns a string representation of this method. Includes the
219 * qualified signature, the qualified method name, and any type
220 * parameters. Type parameters follow the class name, as they do
221 * in the syntax for invoking methods with explicit type parameters.
222 */
223 public String toString() {
224 return sym.enclClass().getQualifiedName() +
225 "." + typeParametersString() + name() + signature();
226 }
227 }