src/share/classes/com/sun/tools/javadoc/MethodDocImpl.java

Tue, 09 Oct 2012 19:10:00 -0700

author
jjg
date
Tue, 09 Oct 2012 19:10:00 -0700
changeset 1357
c75be5bc5283
parent 554
9d9f26857129
child 1359
25e14ad23cef
permissions
-rw-r--r--

8000663: clean up langtools imports
Reviewed-by: darcy

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

mercurial