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

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/javadoc/MethodDocImpl.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,233 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.javadoc;
    1.30 +
    1.31 +import java.lang.reflect.Modifier;
    1.32 +
    1.33 +import com.sun.javadoc.*;
    1.34 +import com.sun.source.util.TreePath;
    1.35 +import com.sun.tools.javac.code.*;
    1.36 +import com.sun.tools.javac.code.Symbol.*;
    1.37 +import com.sun.tools.javac.code.Type;
    1.38 +import static com.sun.tools.javac.code.TypeTag.CLASS;
    1.39 +
    1.40 +/**
    1.41 + * Represents a method of a java class.
    1.42 + *
    1.43 + *  <p><b>This is NOT part of any supported API.
    1.44 + *  If you write code that depends on this, you do so at your own risk.
    1.45 + *  This code and its internal interfaces are subject to change or
    1.46 + *  deletion without notice.</b>
    1.47 + *
    1.48 + * @since 1.2
    1.49 + * @author Robert Field
    1.50 + * @author Neal Gafter (rewrite)
    1.51 + */
    1.52 +
    1.53 +public class MethodDocImpl
    1.54 +        extends ExecutableMemberDocImpl implements MethodDoc {
    1.55 +
    1.56 +    /**
    1.57 +     * constructor.
    1.58 +     */
    1.59 +    public MethodDocImpl(DocEnv env, MethodSymbol sym) {
    1.60 +        super(env, sym);
    1.61 +    }
    1.62 +
    1.63 +    /**
    1.64 +     * constructor.
    1.65 +     */
    1.66 +    public MethodDocImpl(DocEnv env, MethodSymbol sym, TreePath treePath) {
    1.67 +        super(env, sym, treePath);
    1.68 +    }
    1.69 +
    1.70 +    /**
    1.71 +     * Return true if it is a method, which it is.
    1.72 +     * Note: constructors are not methods.
    1.73 +     * This method is overridden by AnnotationTypeElementDocImpl.
    1.74 +     *
    1.75 +     * @return true
    1.76 +     */
    1.77 +    public boolean isMethod() {
    1.78 +        return true;
    1.79 +    }
    1.80 +
    1.81 +    /**
    1.82 +     * Return true if this method is default
    1.83 +     */
    1.84 +    public boolean isDefault() {
    1.85 +        return (sym.flags() & Flags.DEFAULT) != 0;
    1.86 +    }
    1.87 +
    1.88 +    /**
    1.89 +     * Return true if this method is abstract
    1.90 +     */
    1.91 +    public boolean isAbstract() {
    1.92 +        return (Modifier.isAbstract(getModifiers()) && !isDefault());
    1.93 +    }
    1.94 +
    1.95 +    /**
    1.96 +     * Get return type.
    1.97 +     *
    1.98 +     * @return the return type of this method, null if it
    1.99 +     * is a constructor.
   1.100 +     */
   1.101 +    public com.sun.javadoc.Type returnType() {
   1.102 +        return TypeMaker.getType(env, sym.type.getReturnType(), false);
   1.103 +    }
   1.104 +
   1.105 +    /**
   1.106 +     * Return the class that originally defined the method that
   1.107 +     * is overridden by the current definition, or null if no
   1.108 +     * such class exists.
   1.109 +     *
   1.110 +     * @return a ClassDocImpl representing the superclass that
   1.111 +     * originally defined this method, null if this method does
   1.112 +     * not override a definition in a superclass.
   1.113 +     */
   1.114 +    public ClassDoc overriddenClass() {
   1.115 +        com.sun.javadoc.Type t = overriddenType();
   1.116 +        return (t != null) ? t.asClassDoc() : null;
   1.117 +    }
   1.118 +
   1.119 +    /**
   1.120 +     * Return the type containing the method that this method overrides.
   1.121 +     * It may be a <code>ClassDoc</code> or a <code>ParameterizedType</code>.
   1.122 +     */
   1.123 +    public com.sun.javadoc.Type overriddenType() {
   1.124 +
   1.125 +        if ((sym.flags() & Flags.STATIC) != 0) {
   1.126 +            return null;
   1.127 +        }
   1.128 +
   1.129 +        ClassSymbol origin = (ClassSymbol)sym.owner;
   1.130 +        for (Type t = env.types.supertype(origin.type);
   1.131 +             t.hasTag(CLASS);
   1.132 +             t = env.types.supertype(t)) {
   1.133 +            ClassSymbol c = (ClassSymbol)t.tsym;
   1.134 +            for (Scope.Entry e = c.members().lookup(sym.name); e.scope != null; e = e.next()) {
   1.135 +                if (sym.overrides(e.sym, origin, env.types, true)) {
   1.136 +                    return TypeMaker.getType(env, t);
   1.137 +                }
   1.138 +            }
   1.139 +        }
   1.140 +        return null;
   1.141 +    }
   1.142 +
   1.143 +    /**
   1.144 +     * Return the method that this method overrides.
   1.145 +     *
   1.146 +     * @return a MethodDoc representing a method definition
   1.147 +     * in a superclass this method overrides, null if
   1.148 +     * this method does not override.
   1.149 +     */
   1.150 +    public MethodDoc overriddenMethod() {
   1.151 +
   1.152 +        // Real overriding only.  Static members are simply hidden.
   1.153 +        // Likewise for constructors, but the MethodSymbol.overrides
   1.154 +        // method takes this into account.
   1.155 +        if ((sym.flags() & Flags.STATIC) != 0) {
   1.156 +            return null;
   1.157 +        }
   1.158 +
   1.159 +        // Derived from  com.sun.tools.javac.comp.Check.checkOverride .
   1.160 +
   1.161 +        ClassSymbol origin = (ClassSymbol)sym.owner;
   1.162 +        for (Type t = env.types.supertype(origin.type);
   1.163 +             t.hasTag(CLASS);
   1.164 +             t = env.types.supertype(t)) {
   1.165 +            ClassSymbol c = (ClassSymbol)t.tsym;
   1.166 +            for (Scope.Entry e = c.members().lookup(sym.name); e.scope != null; e = e.next()) {
   1.167 +                if (sym.overrides(e.sym, origin, env.types, true)) {
   1.168 +                    return env.getMethodDoc((MethodSymbol)e.sym);
   1.169 +                }
   1.170 +            }
   1.171 +        }
   1.172 +        return null;
   1.173 +    }
   1.174 +
   1.175 +    /**
   1.176 +     * Tests whether this method overrides another.
   1.177 +     * The overridden method may be one declared in a superclass or
   1.178 +     * a superinterface (unlike {@link #overriddenMethod()}).
   1.179 +     *
   1.180 +     * <p> When a non-abstract method overrides an abstract one, it is
   1.181 +     * also said to <i>implement</i> the other.
   1.182 +     *
   1.183 +     * @param meth  the other method to examine
   1.184 +     * @return <tt>true</tt> if this method overrides the other
   1.185 +     */
   1.186 +    public boolean overrides(MethodDoc meth) {
   1.187 +        MethodSymbol overridee = ((MethodDocImpl) meth).sym;
   1.188 +        ClassSymbol origin = (ClassSymbol) sym.owner;
   1.189 +
   1.190 +        return sym.name == overridee.name &&
   1.191 +
   1.192 +               // not reflexive as per JLS
   1.193 +               sym != overridee &&
   1.194 +
   1.195 +               // we don't care if overridee is static, though that wouldn't
   1.196 +               // compile
   1.197 +               !sym.isStatic() &&
   1.198 +
   1.199 +               // sym, whose declaring type is the origin, must be
   1.200 +               // in a subtype of overridee's type
   1.201 +               env.types.asSuper(origin.type, overridee.owner) != null &&
   1.202 +
   1.203 +               // check access and signatures; don't check return types
   1.204 +               sym.overrides(overridee, origin, env.types, false);
   1.205 +    }
   1.206 +
   1.207 +
   1.208 +    public String name() {
   1.209 +        if (name == null) {
   1.210 +            name = sym.name.toString();
   1.211 +        }
   1.212 +        return name;
   1.213 +    }
   1.214 +
   1.215 +    private String name;
   1.216 +
   1.217 +    public String qualifiedName() {
   1.218 +        if (qualifiedName == null) {
   1.219 +            qualifiedName =  sym.enclClass().getQualifiedName() + "." + sym.name;
   1.220 +        }
   1.221 +        return qualifiedName;
   1.222 +    }
   1.223 +
   1.224 +    private String qualifiedName;
   1.225 +
   1.226 +    /**
   1.227 +     * Returns a string representation of this method.  Includes the
   1.228 +     * qualified signature, the qualified method name, and any type
   1.229 +     * parameters.  Type parameters follow the class name, as they do
   1.230 +     * in the syntax for invoking methods with explicit type parameters.
   1.231 +     */
   1.232 +    public String toString() {
   1.233 +        return sym.enclClass().getQualifiedName() +
   1.234 +                "." + typeParametersString() + name() + signature();
   1.235 +    }
   1.236 +}

mercurial