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

Tue, 09 Apr 2013 14:18:22 -0700

author
bpatel
date
Tue, 09 Apr 2013 14:18:22 -0700
changeset 1686
eb134c8e931d
parent 1644
40adaf938847
child 1691
f10cffab99b4
permissions
-rw-r--r--

8005091: javadoc should be able to return the receiver type
Reviewed-by: jjg

     1 /*
     2  * Copyright (c) 1997, 2013, 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;
    29 import java.text.CollationKey;
    31 import javax.lang.model.type.TypeKind;
    33 import com.sun.javadoc.*;
    35 import com.sun.source.util.TreePath;
    36 import com.sun.tools.javac.code.Attribute;
    37 import com.sun.tools.javac.code.Flags;
    38 import com.sun.tools.javac.code.Attribute.Compound;
    39 import com.sun.tools.javac.code.Symbol.*;
    40 import com.sun.tools.javac.code.Type;
    41 import com.sun.tools.javac.util.List;
    42 import com.sun.tools.javac.util.ListBuffer;
    44 /**
    45  * Represents a method or constructor of a java class.
    46  *
    47  *  <p><b>This is NOT part of any supported API.
    48  *  If you write code that depends on this, you do so at your own risk.
    49  *  This code and its internal interfaces are subject to change or
    50  *  deletion without notice.</b>
    51  *
    52  * @since 1.2
    53  * @author Robert Field
    54  * @author Neal Gafter (rewrite)
    55  * @author Scott Seligman (generics, annotations)
    56  */
    58 public abstract class ExecutableMemberDocImpl
    59         extends MemberDocImpl implements ExecutableMemberDoc {
    61     protected final MethodSymbol sym;
    63     /**
    64      * Constructor.
    65      */
    66     public ExecutableMemberDocImpl(DocEnv env, MethodSymbol sym, TreePath treePath) {
    67         super(env, sym, treePath);
    68         this.sym = sym;
    69     }
    71     /**
    72      * Constructor.
    73      */
    74     public ExecutableMemberDocImpl(DocEnv env, MethodSymbol sym) {
    75         this(env, sym, null);
    76     }
    78     /**
    79      * Returns the flags in terms of javac's flags
    80      */
    81     protected long getFlags() {
    82         return sym.flags();
    83     }
    85     /**
    86      * Identify the containing class
    87      */
    88     protected ClassSymbol getContainingClass() {
    89         return sym.enclClass();
    90     }
    92     /**
    93      * Return true if this method is native
    94      */
    95     public boolean isNative() {
    96         return Modifier.isNative(getModifiers());
    97     }
    99     /**
   100      * Return true if this method is synchronized
   101      */
   102     public boolean isSynchronized() {
   103         return Modifier.isSynchronized(getModifiers());
   104     }
   106     /**
   107      * Return true if this method was declared to take a variable number
   108      * of arguments.
   109      */
   110     public boolean isVarArgs() {
   111         return ((sym.flags() & Flags.VARARGS) != 0
   112                 && !env.legacyDoclet);
   113     }
   115     /**
   116      * Returns true if this field was synthesized by the compiler.
   117      */
   118     public boolean isSynthetic() {
   119         return ((sym.flags() & Flags.SYNTHETIC) != 0);
   120     }
   122     public boolean isIncluded() {
   123         return containingClass().isIncluded() && env.shouldDocument(sym);
   124     }
   126     /**
   127      * Return the throws tags in this method.
   128      *
   129      * @return an array of ThrowTagImpl containing all {@code @exception}
   130      * and {@code @throws} tags.
   131      */
   132     public ThrowsTag[] throwsTags() {
   133         return comment().throwsTags();
   134     }
   136     /**
   137      * Return the param tags in this method, excluding the type
   138      * parameter tags.
   139      *
   140      * @return an array of ParamTagImpl containing all {@code @param} tags.
   141      */
   142     public ParamTag[] paramTags() {
   143         return comment().paramTags();
   144     }
   146     /**
   147      * Return the type parameter tags in this method.
   148      */
   149     public ParamTag[] typeParamTags() {
   150         return env.legacyDoclet
   151             ? new ParamTag[0]
   152             : comment().typeParamTags();
   153     }
   155     /**
   156      * Return exceptions this method or constructor throws.
   157      *
   158      * @return an array of ClassDoc[] representing the exceptions
   159      * thrown by this method.
   160      */
   161     public ClassDoc[] thrownExceptions() {
   162         ListBuffer<ClassDocImpl> l = new ListBuffer<ClassDocImpl>();
   163         for (Type ex : sym.type.getThrownTypes()) {
   164             ex = env.types.erasure(ex);
   165             //### Will these casts succeed in the face of static semantic
   166             //### errors in the documented code?
   167             ClassDocImpl cdi = env.getClassDoc((ClassSymbol)ex.tsym);
   168             if (cdi != null) l.append(cdi);
   169         }
   170         return l.toArray(new ClassDocImpl[l.length()]);
   171     }
   173     /**
   174      * Return exceptions this method or constructor throws.
   175      * Each array element is either a <code>ClassDoc</code> or a
   176      * <code>TypeVariable</code>.
   177      */
   178     public com.sun.javadoc.Type[] thrownExceptionTypes() {
   179         return TypeMaker.getTypes(env, sym.type.getThrownTypes());
   180     }
   182     /**
   183      * Get argument information.
   184      *
   185      * @see ParameterImpl
   186      *
   187      * @return an array of ParameterImpl, one element per argument
   188      * in the order the arguments are present.
   189      */
   190     public Parameter[] parameters() {
   191         // generate the parameters on the fly:  they're not cached
   192         List<VarSymbol> params = sym.params();
   193         Parameter result[] = new Parameter[params.length()];
   195         int i = 0;
   196         for (VarSymbol param : params) {
   197             result[i++] = new ParameterImpl(env, param);
   198         }
   199         return result;
   200     }
   202     /**
   203      * Get the receiver type of this executable element.
   204      *
   205      * @return the receiver type of this executable element.
   206      * @since 1.8
   207      */
   208     public com.sun.javadoc.Type receiverType() {
   209         Type recvtype = sym.type.asMethodType().recvtype;
   210         return (recvtype != null) ? TypeMaker.getType(env, recvtype, false, true) : null;
   211     }
   213     public AnnotationDesc[] receiverAnnotations() {
   214         // TODO: change how receiver annotations are output!
   215         Type recvtype = sym.type.asMethodType().recvtype;
   216         if (recvtype == null) {
   217             return new AnnotationDesc[0];
   218         }
   219         if (!recvtype.isAnnotated()) {
   220             return new AnnotationDesc[0];
   221         }
   222         List<? extends Compound> typeAnnos = ((com.sun.tools.javac.code.Type.AnnotatedType)recvtype).typeAnnotations;
   223         AnnotationDesc result[] = new AnnotationDesc[typeAnnos.length()];
   224         int i = 0;
   225         for (Attribute.Compound a : typeAnnos) {
   226             result[i++] = new AnnotationDescImpl(env, a);
   227         }
   228         return result;
   229     }
   231     /**
   232      * Return the formal type parameters of this method or constructor.
   233      * Return an empty array if there are none.
   234      */
   235     public TypeVariable[] typeParameters() {
   236         if (env.legacyDoclet) {
   237             return new TypeVariable[0];
   238         }
   239         TypeVariable res[] = new TypeVariable[sym.type.getTypeArguments().length()];
   240         TypeMaker.getTypes(env, sym.type.getTypeArguments(), res);
   241         return res;
   242     }
   244     /**
   245      * Get the signature. It is the parameter list, type is qualified.
   246      * For instance, for a method <code>mymethod(String x, int y)</code>,
   247      * it will return <code>(java.lang.String,int)</code>.
   248      */
   249     public String signature() {
   250         return makeSignature(true);
   251     }
   253     /**
   254      * Get flat signature.  All types are not qualified.
   255      * Return a String, which is the flat signiture of this member.
   256      * It is the parameter list, type is not qualified.
   257      * For instance, for a method <code>mymethod(String x, int y)</code>,
   258      * it will return <code>(String, int)</code>.
   259      */
   260     public String flatSignature() {
   261         return makeSignature(false);
   262     }
   264     private String makeSignature(boolean full) {
   265         StringBuilder result = new StringBuilder();
   266         result.append("(");
   267         for (List<Type> types = sym.type.getParameterTypes(); types.nonEmpty(); ) {
   268             Type t = types.head;
   269             result.append(TypeMaker.getTypeString(env, t, full));
   270             types = types.tail;
   271             if (types.nonEmpty()) {
   272                 result.append(", ");
   273             }
   274         }
   275         if (isVarArgs()) {
   276             int len = result.length();
   277             result.replace(len - 2, len, "...");
   278         }
   279         result.append(")");
   280         return result.toString();
   281     }
   283     protected String typeParametersString() {
   284         return TypeMaker.typeParametersString(env, sym, true);
   285     }
   287     /**
   288      * Generate a key for sorting.
   289      */
   290     @Override
   291     CollationKey generateKey() {
   292         String k = name() + flatSignature() + typeParametersString();
   293         // ',' and '&' are between '$' and 'a':  normalize to spaces.
   294         k = k.replace(',', ' ').replace('&', ' ');
   295         // System.out.println("COLLATION KEY FOR " + this + " is \"" + k + "\"");
   296         return env.doclocale.collator.getCollationKey(k);
   297     }
   299     /**
   300      * Return the source position of the entity, or null if
   301      * no position is available.
   302      */
   303     @Override
   304     public SourcePosition position() {
   305         if (sym.enclClass().sourcefile == null) return null;
   306         return SourcePositionImpl.make(sym.enclClass().sourcefile,
   307                                        (tree==null) ? 0 : tree.pos,
   308                                        lineMap);
   309     }
   310 }

mercurial