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

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 1755
ddb4a2bfcd82
parent 0
959103a6100f
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.tools.javadoc;
aoqi@0 27
aoqi@0 28 import java.lang.reflect.Modifier;
aoqi@0 29 import java.text.CollationKey;
aoqi@0 30
aoqi@0 31 import com.sun.javadoc.*;
aoqi@0 32
aoqi@0 33 import com.sun.source.util.TreePath;
aoqi@0 34 import com.sun.tools.javac.code.Flags;
aoqi@0 35 import com.sun.tools.javac.code.Symbol.*;
aoqi@0 36 import com.sun.tools.javac.code.Type;
aoqi@0 37 import com.sun.tools.javac.util.List;
aoqi@0 38 import com.sun.tools.javac.util.ListBuffer;
aoqi@0 39
aoqi@0 40 /**
aoqi@0 41 * Represents a method or constructor of a java class.
aoqi@0 42 *
aoqi@0 43 * <p><b>This is NOT part of any supported API.
aoqi@0 44 * If you write code that depends on this, you do so at your own risk.
aoqi@0 45 * This code and its internal interfaces are subject to change or
aoqi@0 46 * deletion without notice.</b>
aoqi@0 47 *
aoqi@0 48 * @since 1.2
aoqi@0 49 * @author Robert Field
aoqi@0 50 * @author Neal Gafter (rewrite)
aoqi@0 51 * @author Scott Seligman (generics, annotations)
aoqi@0 52 */
aoqi@0 53
aoqi@0 54 public abstract class ExecutableMemberDocImpl
aoqi@0 55 extends MemberDocImpl implements ExecutableMemberDoc {
aoqi@0 56
aoqi@0 57 protected final MethodSymbol sym;
aoqi@0 58
aoqi@0 59 /**
aoqi@0 60 * Constructor.
aoqi@0 61 */
aoqi@0 62 public ExecutableMemberDocImpl(DocEnv env, MethodSymbol sym, TreePath treePath) {
aoqi@0 63 super(env, sym, treePath);
aoqi@0 64 this.sym = sym;
aoqi@0 65 }
aoqi@0 66
aoqi@0 67 /**
aoqi@0 68 * Constructor.
aoqi@0 69 */
aoqi@0 70 public ExecutableMemberDocImpl(DocEnv env, MethodSymbol sym) {
aoqi@0 71 this(env, sym, null);
aoqi@0 72 }
aoqi@0 73
aoqi@0 74 /**
aoqi@0 75 * Returns the flags in terms of javac's flags
aoqi@0 76 */
aoqi@0 77 protected long getFlags() {
aoqi@0 78 return sym.flags();
aoqi@0 79 }
aoqi@0 80
aoqi@0 81 /**
aoqi@0 82 * Identify the containing class
aoqi@0 83 */
aoqi@0 84 protected ClassSymbol getContainingClass() {
aoqi@0 85 return sym.enclClass();
aoqi@0 86 }
aoqi@0 87
aoqi@0 88 /**
aoqi@0 89 * Return true if this method is native
aoqi@0 90 */
aoqi@0 91 public boolean isNative() {
aoqi@0 92 return Modifier.isNative(getModifiers());
aoqi@0 93 }
aoqi@0 94
aoqi@0 95 /**
aoqi@0 96 * Return true if this method is synchronized
aoqi@0 97 */
aoqi@0 98 public boolean isSynchronized() {
aoqi@0 99 return Modifier.isSynchronized(getModifiers());
aoqi@0 100 }
aoqi@0 101
aoqi@0 102 /**
aoqi@0 103 * Return true if this method was declared to take a variable number
aoqi@0 104 * of arguments.
aoqi@0 105 */
aoqi@0 106 public boolean isVarArgs() {
aoqi@0 107 return ((sym.flags() & Flags.VARARGS) != 0
aoqi@0 108 && !env.legacyDoclet);
aoqi@0 109 }
aoqi@0 110
aoqi@0 111 /**
aoqi@0 112 * Returns true if this field was synthesized by the compiler.
aoqi@0 113 */
aoqi@0 114 public boolean isSynthetic() {
aoqi@0 115 return ((sym.flags() & Flags.SYNTHETIC) != 0);
aoqi@0 116 }
aoqi@0 117
aoqi@0 118 public boolean isIncluded() {
aoqi@0 119 return containingClass().isIncluded() && env.shouldDocument(sym);
aoqi@0 120 }
aoqi@0 121
aoqi@0 122 /**
aoqi@0 123 * Return the throws tags in this method.
aoqi@0 124 *
aoqi@0 125 * @return an array of ThrowTagImpl containing all {@code @exception}
aoqi@0 126 * and {@code @throws} tags.
aoqi@0 127 */
aoqi@0 128 public ThrowsTag[] throwsTags() {
aoqi@0 129 return comment().throwsTags();
aoqi@0 130 }
aoqi@0 131
aoqi@0 132 /**
aoqi@0 133 * Return the param tags in this method, excluding the type
aoqi@0 134 * parameter tags.
aoqi@0 135 *
aoqi@0 136 * @return an array of ParamTagImpl containing all {@code @param} tags.
aoqi@0 137 */
aoqi@0 138 public ParamTag[] paramTags() {
aoqi@0 139 return comment().paramTags();
aoqi@0 140 }
aoqi@0 141
aoqi@0 142 /**
aoqi@0 143 * Return the type parameter tags in this method.
aoqi@0 144 */
aoqi@0 145 public ParamTag[] typeParamTags() {
aoqi@0 146 return env.legacyDoclet
aoqi@0 147 ? new ParamTag[0]
aoqi@0 148 : comment().typeParamTags();
aoqi@0 149 }
aoqi@0 150
aoqi@0 151 /**
aoqi@0 152 * Return exceptions this method or constructor throws.
aoqi@0 153 *
aoqi@0 154 * @return an array of ClassDoc[] representing the exceptions
aoqi@0 155 * thrown by this method.
aoqi@0 156 */
aoqi@0 157 public ClassDoc[] thrownExceptions() {
aoqi@0 158 ListBuffer<ClassDocImpl> l = new ListBuffer<ClassDocImpl>();
aoqi@0 159 for (Type ex : sym.type.getThrownTypes()) {
aoqi@0 160 ex = env.types.erasure(ex);
aoqi@0 161 //### Will these casts succeed in the face of static semantic
aoqi@0 162 //### errors in the documented code?
aoqi@0 163 ClassDocImpl cdi = env.getClassDoc((ClassSymbol)ex.tsym);
aoqi@0 164 if (cdi != null) l.append(cdi);
aoqi@0 165 }
aoqi@0 166 return l.toArray(new ClassDocImpl[l.length()]);
aoqi@0 167 }
aoqi@0 168
aoqi@0 169 /**
aoqi@0 170 * Return exceptions this method or constructor throws.
aoqi@0 171 * Each array element is either a <code>ClassDoc</code> or a
aoqi@0 172 * <code>TypeVariable</code>.
aoqi@0 173 */
aoqi@0 174 public com.sun.javadoc.Type[] thrownExceptionTypes() {
aoqi@0 175 return TypeMaker.getTypes(env, sym.type.getThrownTypes());
aoqi@0 176 }
aoqi@0 177
aoqi@0 178 /**
aoqi@0 179 * Get argument information.
aoqi@0 180 *
aoqi@0 181 * @see ParameterImpl
aoqi@0 182 *
aoqi@0 183 * @return an array of ParameterImpl, one element per argument
aoqi@0 184 * in the order the arguments are present.
aoqi@0 185 */
aoqi@0 186 public Parameter[] parameters() {
aoqi@0 187 // generate the parameters on the fly: they're not cached
aoqi@0 188 List<VarSymbol> params = sym.params();
aoqi@0 189 Parameter result[] = new Parameter[params.length()];
aoqi@0 190
aoqi@0 191 int i = 0;
aoqi@0 192 for (VarSymbol param : params) {
aoqi@0 193 result[i++] = new ParameterImpl(env, param);
aoqi@0 194 }
aoqi@0 195 return result;
aoqi@0 196 }
aoqi@0 197
aoqi@0 198 /**
aoqi@0 199 * Get the receiver type of this executable element.
aoqi@0 200 *
aoqi@0 201 * @return the receiver type of this executable element.
aoqi@0 202 * @since 1.8
aoqi@0 203 */
aoqi@0 204 public com.sun.javadoc.Type receiverType() {
aoqi@0 205 Type recvtype = sym.type.asMethodType().recvtype;
aoqi@0 206 return (recvtype != null) ? TypeMaker.getType(env, recvtype, false, true) : null;
aoqi@0 207 }
aoqi@0 208
aoqi@0 209 /**
aoqi@0 210 * Return the formal type parameters of this method or constructor.
aoqi@0 211 * Return an empty array if there are none.
aoqi@0 212 */
aoqi@0 213 public TypeVariable[] typeParameters() {
aoqi@0 214 if (env.legacyDoclet) {
aoqi@0 215 return new TypeVariable[0];
aoqi@0 216 }
aoqi@0 217 TypeVariable res[] = new TypeVariable[sym.type.getTypeArguments().length()];
aoqi@0 218 TypeMaker.getTypes(env, sym.type.getTypeArguments(), res);
aoqi@0 219 return res;
aoqi@0 220 }
aoqi@0 221
aoqi@0 222 /**
aoqi@0 223 * Get the signature. It is the parameter list, type is qualified.
aoqi@0 224 * For instance, for a method <code>mymethod(String x, int y)</code>,
aoqi@0 225 * it will return <code>(java.lang.String,int)</code>.
aoqi@0 226 */
aoqi@0 227 public String signature() {
aoqi@0 228 return makeSignature(true);
aoqi@0 229 }
aoqi@0 230
aoqi@0 231 /**
aoqi@0 232 * Get flat signature. All types are not qualified.
aoqi@0 233 * Return a String, which is the flat signiture of this member.
aoqi@0 234 * It is the parameter list, type is not qualified.
aoqi@0 235 * For instance, for a method <code>mymethod(String x, int y)</code>,
aoqi@0 236 * it will return <code>(String, int)</code>.
aoqi@0 237 */
aoqi@0 238 public String flatSignature() {
aoqi@0 239 return makeSignature(false);
aoqi@0 240 }
aoqi@0 241
aoqi@0 242 private String makeSignature(boolean full) {
aoqi@0 243 StringBuilder result = new StringBuilder();
aoqi@0 244 result.append("(");
aoqi@0 245 for (List<Type> types = sym.type.getParameterTypes(); types.nonEmpty(); ) {
aoqi@0 246 Type t = types.head;
aoqi@0 247 result.append(TypeMaker.getTypeString(env, t, full));
aoqi@0 248 types = types.tail;
aoqi@0 249 if (types.nonEmpty()) {
aoqi@0 250 result.append(", ");
aoqi@0 251 }
aoqi@0 252 }
aoqi@0 253 if (isVarArgs()) {
aoqi@0 254 int len = result.length();
aoqi@0 255 result.replace(len - 2, len, "...");
aoqi@0 256 }
aoqi@0 257 result.append(")");
aoqi@0 258 return result.toString();
aoqi@0 259 }
aoqi@0 260
aoqi@0 261 protected String typeParametersString() {
aoqi@0 262 return TypeMaker.typeParametersString(env, sym, true);
aoqi@0 263 }
aoqi@0 264
aoqi@0 265 /**
aoqi@0 266 * Generate a key for sorting.
aoqi@0 267 */
aoqi@0 268 @Override
aoqi@0 269 CollationKey generateKey() {
aoqi@0 270 String k = name() + flatSignature() + typeParametersString();
aoqi@0 271 // ',' and '&' are between '$' and 'a': normalize to spaces.
aoqi@0 272 k = k.replace(',', ' ').replace('&', ' ');
aoqi@0 273 // System.out.println("COLLATION KEY FOR " + this + " is \"" + k + "\"");
aoqi@0 274 return env.doclocale.collator.getCollationKey(k);
aoqi@0 275 }
aoqi@0 276
aoqi@0 277 /**
aoqi@0 278 * Return the source position of the entity, or null if
aoqi@0 279 * no position is available.
aoqi@0 280 */
aoqi@0 281 @Override
aoqi@0 282 public SourcePosition position() {
aoqi@0 283 if (sym.enclClass().sourcefile == null) return null;
aoqi@0 284 return SourcePositionImpl.make(sym.enclClass().sourcefile,
aoqi@0 285 (tree==null) ? 0 : tree.pos,
aoqi@0 286 lineMap);
aoqi@0 287 }
aoqi@0 288 }

mercurial