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

Thu, 15 Nov 2012 09:18:36 -0800

author
jjg
date
Thu, 15 Nov 2012 09:18:36 -0800
changeset 1410
bfec2a1cc869
parent 1374
c002fdee76fd
child 1443
cfde9737131e
permissions
-rw-r--r--

8000800: javadoc uses static non-final fields
Reviewed-by: bpatel

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

mercurial