Tue, 09 Oct 2012 19:10:00 -0700
8000663: clean up langtools imports
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 * @since 1.2
42 * @author Robert Field
43 * @author Neal Gafter (rewrite)
44 */
46 public class MethodDocImpl
47 extends ExecutableMemberDocImpl implements MethodDoc {
49 /**
50 * constructor.
51 */
52 public MethodDocImpl(DocEnv env, MethodSymbol sym) {
53 super(env, sym);
54 }
56 /**
57 * constructor.
58 */
59 public MethodDocImpl(DocEnv env, MethodSymbol sym,
60 String docComment, JCMethodDecl tree, Position.LineMap lineMap) {
61 super(env, sym, docComment, tree, lineMap);
62 }
64 /**
65 * Return true if it is a method, which it is.
66 * Note: constructors are not methods.
67 * This method is overridden by AnnotationTypeElementDocImpl.
68 *
69 * @return true
70 */
71 public boolean isMethod() {
72 return true;
73 }
75 /**
76 * Return true if this method is abstract
77 */
78 public boolean isAbstract() {
79 //### This is dubious, but old 'javadoc' apparently does it.
80 //### I regard this as a bug and an obstacle to treating the
81 //### doclet API as a proper compile-time reflection facility.
82 //### (maddox 09/26/2000)
83 if (containingClass().isInterface()) {
84 //### Don't force creation of ClassDocImpl for super here.
85 // Abstract modifier is implicit. Strip/canonicalize it.
86 return false;
87 }
88 return Modifier.isAbstract(getModifiers());
89 }
91 /**
92 * Get return type.
93 *
94 * @return the return type of this method, null if it
95 * is a constructor.
96 */
97 public com.sun.javadoc.Type returnType() {
98 return TypeMaker.getType(env, sym.type.getReturnType(), false);
99 }
101 /**
102 * Return the class that originally defined the method that
103 * is overridden by the current definition, or null if no
104 * such class exists.
105 *
106 * @return a ClassDocImpl representing the superclass that
107 * originally defined this method, null if this method does
108 * not override a definition in a superclass.
109 */
110 public ClassDoc overriddenClass() {
111 com.sun.javadoc.Type t = overriddenType();
112 return (t != null) ? t.asClassDoc() : null;
113 }
115 /**
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 */
119 public com.sun.javadoc.Type overriddenType() {
121 if ((sym.flags() & Flags.STATIC) != 0) {
122 return null;
123 }
125 ClassSymbol origin = (ClassSymbol)sym.owner;
126 for (Type t = env.types.supertype(origin.type);
127 t.tag == TypeTags.CLASS;
128 t = env.types.supertype(t)) {
129 ClassSymbol c = (ClassSymbol)t.tsym;
130 for (Scope.Entry e = c.members().lookup(sym.name); e.scope != null; e = e.next()) {
131 if (sym.overrides(e.sym, origin, env.types, true)) {
132 return TypeMaker.getType(env, t);
133 }
134 }
135 }
136 return null;
137 }
139 /**
140 * Return the method that this method overrides.
141 *
142 * @return a MethodDoc representing a method definition
143 * in a superclass this method overrides, null if
144 * this method does not override.
145 */
146 public MethodDoc overriddenMethod() {
148 // Real overriding only. Static members are simply hidden.
149 // Likewise for constructors, but the MethodSymbol.overrides
150 // method takes this into account.
151 if ((sym.flags() & Flags.STATIC) != 0) {
152 return null;
153 }
155 // Derived from com.sun.tools.javac.comp.Check.checkOverride .
157 ClassSymbol origin = (ClassSymbol)sym.owner;
158 for (Type t = env.types.supertype(origin.type);
159 t.tag == TypeTags.CLASS;
160 t = env.types.supertype(t)) {
161 ClassSymbol c = (ClassSymbol)t.tsym;
162 for (Scope.Entry e = c.members().lookup(sym.name); e.scope != null; e = e.next()) {
163 if (sym.overrides(e.sym, origin, env.types, true)) {
164 return env.getMethodDoc((MethodSymbol)e.sym);
165 }
166 }
167 }
168 return null;
169 }
171 /**
172 * Tests whether this method overrides another.
173 * The overridden method may be one declared in a superclass or
174 * a superinterface (unlike {@link #overriddenMethod()}).
175 *
176 * <p> When a non-abstract method overrides an abstract one, it is
177 * also said to <i>implement</i> the other.
178 *
179 * @param meth the other method to examine
180 * @return <tt>true</tt> if this method overrides the other
181 */
182 public boolean overrides(MethodDoc meth) {
183 MethodSymbol overridee = ((MethodDocImpl) meth).sym;
184 ClassSymbol origin = (ClassSymbol) sym.owner;
186 return sym.name == overridee.name &&
188 // not reflexive as per JLS
189 sym != overridee &&
191 // we don't care if overridee is static, though that wouldn't
192 // compile
193 !sym.isStatic() &&
195 // sym, whose declaring type is the origin, must be
196 // in a subtype of overridee's type
197 env.types.asSuper(origin.type, overridee.owner) != null &&
199 // check access and signatures; don't check return types
200 sym.overrides(overridee, origin, env.types, false);
201 }
204 public String name() {
205 return sym.name.toString();
206 }
208 public String qualifiedName() {
209 return sym.enclClass().getQualifiedName() + "." + sym.name;
210 }
212 /**
213 * Returns a string representation of this method. Includes the
214 * qualified signature, the qualified method name, and any type
215 * parameters. Type parameters follow the class name, as they do
216 * in the syntax for invoking methods with explicit type parameters.
217 */
218 public String toString() {
219 return sym.enclClass().getQualifiedName() +
220 "." + typeParametersString() + name() + signature();
221 }
222 }