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

Tue, 11 Dec 2012 15:05:55 -0800

author
jjg
date
Tue, 11 Dec 2012 15:05:55 -0800
changeset 1443
cfde9737131e
parent 1374
c002fdee76fd
child 1454
02a18f209ab3
permissions
-rw-r--r--

8004828: refactor init of *DocImpl classes
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.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 abstract
    80      */
    81     public boolean isAbstract() {
    82         //### This is dubious, but old 'javadoc' apparently does it.
    83         //### I regard this as a bug and an obstacle to treating the
    84         //### doclet API as a proper compile-time reflection facility.
    85         //### (maddox 09/26/2000)
    86         if (containingClass().isInterface()) {
    87             //### Don't force creation of ClassDocImpl for super here.
    88             // Abstract modifier is implicit.  Strip/canonicalize it.
    89             return false;
    90         }
    91         return Modifier.isAbstract(getModifiers());
    92     }
    94     /**
    95      * Get return type.
    96      *
    97      * @return the return type of this method, null if it
    98      * is a constructor.
    99      */
   100     public com.sun.javadoc.Type returnType() {
   101         return TypeMaker.getType(env, sym.type.getReturnType(), false);
   102     }
   104     /**
   105      * Return the class that originally defined the method that
   106      * is overridden by the current definition, or null if no
   107      * such class exists.
   108      *
   109      * @return a ClassDocImpl representing the superclass that
   110      * originally defined this method, null if this method does
   111      * not override a definition in a superclass.
   112      */
   113     public ClassDoc overriddenClass() {
   114         com.sun.javadoc.Type t = overriddenType();
   115         return (t != null) ? t.asClassDoc() : null;
   116     }
   118     /**
   119      * Return the type containing the method that this method overrides.
   120      * It may be a <code>ClassDoc</code> or a <code>ParameterizedType</code>.
   121      */
   122     public com.sun.javadoc.Type overriddenType() {
   124         if ((sym.flags() & Flags.STATIC) != 0) {
   125             return null;
   126         }
   128         ClassSymbol origin = (ClassSymbol)sym.owner;
   129         for (Type t = env.types.supertype(origin.type);
   130              t.hasTag(CLASS);
   131              t = env.types.supertype(t)) {
   132             ClassSymbol c = (ClassSymbol)t.tsym;
   133             for (Scope.Entry e = c.members().lookup(sym.name); e.scope != null; e = e.next()) {
   134                 if (sym.overrides(e.sym, origin, env.types, true)) {
   135                     return TypeMaker.getType(env, t);
   136                 }
   137             }
   138         }
   139         return null;
   140     }
   142     /**
   143      * Return the method that this method overrides.
   144      *
   145      * @return a MethodDoc representing a method definition
   146      * in a superclass this method overrides, null if
   147      * this method does not override.
   148      */
   149     public MethodDoc overriddenMethod() {
   151         // Real overriding only.  Static members are simply hidden.
   152         // Likewise for constructors, but the MethodSymbol.overrides
   153         // method takes this into account.
   154         if ((sym.flags() & Flags.STATIC) != 0) {
   155             return null;
   156         }
   158         // Derived from  com.sun.tools.javac.comp.Check.checkOverride .
   160         ClassSymbol origin = (ClassSymbol)sym.owner;
   161         for (Type t = env.types.supertype(origin.type);
   162              t.hasTag(CLASS);
   163              t = env.types.supertype(t)) {
   164             ClassSymbol c = (ClassSymbol)t.tsym;
   165             for (Scope.Entry e = c.members().lookup(sym.name); e.scope != null; e = e.next()) {
   166                 if (sym.overrides(e.sym, origin, env.types, true)) {
   167                     return env.getMethodDoc((MethodSymbol)e.sym);
   168                 }
   169             }
   170         }
   171         return null;
   172     }
   174     /**
   175      * Tests whether this method overrides another.
   176      * The overridden method may be one declared in a superclass or
   177      * a superinterface (unlike {@link #overriddenMethod()}).
   178      *
   179      * <p> When a non-abstract method overrides an abstract one, it is
   180      * also said to <i>implement</i> the other.
   181      *
   182      * @param meth  the other method to examine
   183      * @return <tt>true</tt> if this method overrides the other
   184      */
   185     public boolean overrides(MethodDoc meth) {
   186         MethodSymbol overridee = ((MethodDocImpl) meth).sym;
   187         ClassSymbol origin = (ClassSymbol) sym.owner;
   189         return sym.name == overridee.name &&
   191                // not reflexive as per JLS
   192                sym != overridee &&
   194                // we don't care if overridee is static, though that wouldn't
   195                // compile
   196                !sym.isStatic() &&
   198                // sym, whose declaring type is the origin, must be
   199                // in a subtype of overridee's type
   200                env.types.asSuper(origin.type, overridee.owner) != null &&
   202                // check access and signatures; don't check return types
   203                sym.overrides(overridee, origin, env.types, false);
   204     }
   207     public String name() {
   208         return sym.name.toString();
   209     }
   211     public String qualifiedName() {
   212         return sym.enclClass().getQualifiedName() + "." + sym.name;
   213     }
   215     /**
   216      * Returns a string representation of this method.  Includes the
   217      * qualified signature, the qualified method name, and any type
   218      * parameters.  Type parameters follow the class name, as they do
   219      * in the syntax for invoking methods with explicit type parameters.
   220      */
   221     public String toString() {
   222         return sym.enclClass().getQualifiedName() +
   223                 "." + typeParametersString() + name() + signature();
   224     }
   225 }

mercurial