Thu, 18 Apr 2013 20:00:14 -0700
8012656: cache frequently used name strings for DocImpl classes
Reviewed-by: darcy
duke@1 | 1 | /* |
jjg@1521 | 2 | * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@554 | 7 | * published by the Free Software Foundation. Oracle designates this |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@554 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
duke@1 | 10 | * |
duke@1 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 15 | * accompanied this code). |
duke@1 | 16 | * |
duke@1 | 17 | * You should have received a copy of the GNU General Public License version |
duke@1 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 20 | * |
ohair@554 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@554 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@554 | 23 | * questions. |
duke@1 | 24 | */ |
duke@1 | 25 | |
duke@1 | 26 | package com.sun.tools.javadoc; |
duke@1 | 27 | |
jjg@197 | 28 | import java.io.File; |
jjg@197 | 29 | import java.io.IOException; |
jjg@197 | 30 | import java.lang.reflect.Modifier; |
jjg@197 | 31 | import java.net.URI; |
jjg@197 | 32 | import java.util.HashSet; |
jjg@197 | 33 | import java.util.Set; |
jjg@1443 | 34 | |
jjg@197 | 35 | import javax.tools.FileObject; |
jjg@197 | 36 | import javax.tools.JavaFileManager.Location; |
jjg@197 | 37 | import javax.tools.StandardJavaFileManager; |
jjg@197 | 38 | import javax.tools.StandardLocation; |
jjg@197 | 39 | |
duke@1 | 40 | import com.sun.javadoc.*; |
jjg@1443 | 41 | import com.sun.source.util.TreePath; |
duke@1 | 42 | import com.sun.tools.javac.code.Flags; |
duke@1 | 43 | import com.sun.tools.javac.code.Kinds; |
duke@1 | 44 | import com.sun.tools.javac.code.Scope; |
duke@1 | 45 | import com.sun.tools.javac.code.Symbol; |
duke@1 | 46 | import com.sun.tools.javac.code.Symbol.*; |
jjg@197 | 47 | import com.sun.tools.javac.code.Type; |
jjg@197 | 48 | import com.sun.tools.javac.code.Type.ClassType; |
duke@1 | 49 | import com.sun.tools.javac.comp.AttrContext; |
duke@1 | 50 | import com.sun.tools.javac.comp.Env; |
duke@1 | 51 | import com.sun.tools.javac.tree.JCTree; |
duke@1 | 52 | import com.sun.tools.javac.tree.JCTree.JCFieldAccess; |
duke@1 | 53 | import com.sun.tools.javac.tree.JCTree.JCImport; |
duke@1 | 54 | import com.sun.tools.javac.tree.TreeInfo; |
jjg@197 | 55 | import com.sun.tools.javac.util.List; |
jjg@197 | 56 | import com.sun.tools.javac.util.ListBuffer; |
jjg@197 | 57 | import com.sun.tools.javac.util.Name; |
jjg@113 | 58 | import com.sun.tools.javac.util.Names; |
jjg@197 | 59 | import com.sun.tools.javac.util.Position; |
duke@1 | 60 | import static com.sun.tools.javac.code.Kinds.*; |
jjg@1374 | 61 | import static com.sun.tools.javac.code.TypeTag.CLASS; |
jjg@1127 | 62 | import static com.sun.tools.javac.tree.JCTree.Tag.*; |
duke@1 | 63 | |
duke@1 | 64 | /** |
duke@1 | 65 | * Represents a java class and provides access to information |
duke@1 | 66 | * about the class, the class' comment and tags, and the |
duke@1 | 67 | * members of the class. A ClassDocImpl only exists if it was |
duke@1 | 68 | * processed in this run of javadoc. References to classes |
duke@1 | 69 | * which may or may not have been processed in this run are |
duke@1 | 70 | * referred to using Type (which can be converted to ClassDocImpl, |
duke@1 | 71 | * if possible). |
duke@1 | 72 | * |
jjg@1359 | 73 | * <p><b>This is NOT part of any supported API. |
jjg@1359 | 74 | * If you write code that depends on this, you do so at your own risk. |
jjg@1359 | 75 | * This code and its internal interfaces are subject to change or |
jjg@1359 | 76 | * deletion without notice.</b> |
jjg@1359 | 77 | * |
duke@1 | 78 | * @see Type |
duke@1 | 79 | * |
duke@1 | 80 | * @since 1.2 |
duke@1 | 81 | * @author Robert Field |
duke@1 | 82 | * @author Neal Gafter (rewrite) |
duke@1 | 83 | * @author Scott Seligman (generics, enums, annotations) |
duke@1 | 84 | */ |
duke@1 | 85 | |
duke@1 | 86 | public class ClassDocImpl extends ProgramElementDocImpl implements ClassDoc { |
duke@1 | 87 | |
duke@1 | 88 | public final ClassType type; // protected->public for debugging |
duke@1 | 89 | protected final ClassSymbol tsym; |
duke@1 | 90 | |
duke@1 | 91 | boolean isIncluded = false; // Set in RootDocImpl |
duke@1 | 92 | |
duke@1 | 93 | private SerializedForm serializedForm; |
duke@1 | 94 | |
duke@1 | 95 | /** |
duke@1 | 96 | * Constructor |
duke@1 | 97 | */ |
duke@1 | 98 | public ClassDocImpl(DocEnv env, ClassSymbol sym) { |
jjg@1443 | 99 | this(env, sym, null); |
duke@1 | 100 | } |
duke@1 | 101 | |
duke@1 | 102 | /** |
duke@1 | 103 | * Constructor |
duke@1 | 104 | */ |
jjg@1443 | 105 | public ClassDocImpl(DocEnv env, ClassSymbol sym, TreePath treePath) { |
jjg@1443 | 106 | super(env, sym, treePath); |
duke@1 | 107 | this.type = (ClassType)sym.type; |
duke@1 | 108 | this.tsym = sym; |
duke@1 | 109 | } |
duke@1 | 110 | |
bpatel@1691 | 111 | public com.sun.javadoc.Type getElementType() { |
bpatel@1691 | 112 | return null; |
bpatel@1691 | 113 | } |
bpatel@1691 | 114 | |
duke@1 | 115 | /** |
duke@1 | 116 | * Returns the flags in terms of javac's flags |
duke@1 | 117 | */ |
duke@1 | 118 | protected long getFlags() { |
duke@1 | 119 | return getFlags(tsym); |
duke@1 | 120 | } |
duke@1 | 121 | |
duke@1 | 122 | /** |
duke@1 | 123 | * Returns the flags of a ClassSymbol in terms of javac's flags |
duke@1 | 124 | */ |
duke@1 | 125 | static long getFlags(ClassSymbol clazz) { |
duke@1 | 126 | while (true) { |
duke@1 | 127 | try { |
duke@1 | 128 | return clazz.flags(); |
duke@1 | 129 | } catch (CompletionFailure ex) { |
duke@1 | 130 | // quietly ignore completion failures |
duke@1 | 131 | } |
duke@1 | 132 | } |
duke@1 | 133 | } |
duke@1 | 134 | |
duke@1 | 135 | /** |
duke@1 | 136 | * Is a ClassSymbol an annotation type? |
duke@1 | 137 | */ |
duke@1 | 138 | static boolean isAnnotationType(ClassSymbol clazz) { |
duke@1 | 139 | return (getFlags(clazz) & Flags.ANNOTATION) != 0; |
duke@1 | 140 | } |
duke@1 | 141 | |
duke@1 | 142 | /** |
duke@1 | 143 | * Identify the containing class |
duke@1 | 144 | */ |
duke@1 | 145 | protected ClassSymbol getContainingClass() { |
duke@1 | 146 | return tsym.owner.enclClass(); |
duke@1 | 147 | } |
duke@1 | 148 | |
duke@1 | 149 | /** |
duke@1 | 150 | * Return true if this is a class, not an interface. |
duke@1 | 151 | */ |
jjg@910 | 152 | @Override |
duke@1 | 153 | public boolean isClass() { |
duke@1 | 154 | return !Modifier.isInterface(getModifiers()); |
duke@1 | 155 | } |
duke@1 | 156 | |
duke@1 | 157 | /** |
duke@1 | 158 | * Return true if this is a ordinary class, |
duke@1 | 159 | * not an enumeration, exception, an error, or an interface. |
duke@1 | 160 | */ |
jjg@910 | 161 | @Override |
duke@1 | 162 | public boolean isOrdinaryClass() { |
duke@1 | 163 | if (isEnum() || isInterface() || isAnnotationType()) { |
duke@1 | 164 | return false; |
duke@1 | 165 | } |
jjg@1374 | 166 | for (Type t = type; t.hasTag(CLASS); t = env.types.supertype(t)) { |
duke@1 | 167 | if (t.tsym == env.syms.errorType.tsym || |
duke@1 | 168 | t.tsym == env.syms.exceptionType.tsym) { |
duke@1 | 169 | return false; |
duke@1 | 170 | } |
duke@1 | 171 | } |
duke@1 | 172 | return true; |
duke@1 | 173 | } |
duke@1 | 174 | |
duke@1 | 175 | /** |
duke@1 | 176 | * Return true if this is an enumeration. |
duke@1 | 177 | * (For legacy doclets, return false.) |
duke@1 | 178 | */ |
jjg@910 | 179 | @Override |
duke@1 | 180 | public boolean isEnum() { |
duke@1 | 181 | return (getFlags() & Flags.ENUM) != 0 |
duke@1 | 182 | && |
duke@1 | 183 | !env.legacyDoclet; |
duke@1 | 184 | } |
duke@1 | 185 | |
duke@1 | 186 | /** |
duke@1 | 187 | * Return true if this is an interface, but not an annotation type. |
duke@1 | 188 | * Overridden by AnnotationTypeDocImpl. |
duke@1 | 189 | */ |
jjg@910 | 190 | @Override |
duke@1 | 191 | public boolean isInterface() { |
duke@1 | 192 | return Modifier.isInterface(getModifiers()); |
duke@1 | 193 | } |
duke@1 | 194 | |
duke@1 | 195 | /** |
duke@1 | 196 | * Return true if this is an exception class |
duke@1 | 197 | */ |
jjg@910 | 198 | @Override |
duke@1 | 199 | public boolean isException() { |
duke@1 | 200 | if (isEnum() || isInterface() || isAnnotationType()) { |
duke@1 | 201 | return false; |
duke@1 | 202 | } |
jjg@1374 | 203 | for (Type t = type; t.hasTag(CLASS); t = env.types.supertype(t)) { |
duke@1 | 204 | if (t.tsym == env.syms.exceptionType.tsym) { |
duke@1 | 205 | return true; |
duke@1 | 206 | } |
duke@1 | 207 | } |
duke@1 | 208 | return false; |
duke@1 | 209 | } |
duke@1 | 210 | |
duke@1 | 211 | /** |
duke@1 | 212 | * Return true if this is an error class |
duke@1 | 213 | */ |
jjg@910 | 214 | @Override |
duke@1 | 215 | public boolean isError() { |
duke@1 | 216 | if (isEnum() || isInterface() || isAnnotationType()) { |
duke@1 | 217 | return false; |
duke@1 | 218 | } |
jjg@1374 | 219 | for (Type t = type; t.hasTag(CLASS); t = env.types.supertype(t)) { |
duke@1 | 220 | if (t.tsym == env.syms.errorType.tsym) { |
duke@1 | 221 | return true; |
duke@1 | 222 | } |
duke@1 | 223 | } |
duke@1 | 224 | return false; |
duke@1 | 225 | } |
duke@1 | 226 | |
duke@1 | 227 | /** |
duke@1 | 228 | * Return true if this is a throwable class |
duke@1 | 229 | */ |
duke@1 | 230 | public boolean isThrowable() { |
duke@1 | 231 | if (isEnum() || isInterface() || isAnnotationType()) { |
duke@1 | 232 | return false; |
duke@1 | 233 | } |
jjg@1374 | 234 | for (Type t = type; t.hasTag(CLASS); t = env.types.supertype(t)) { |
duke@1 | 235 | if (t.tsym == env.syms.throwableType.tsym) { |
duke@1 | 236 | return true; |
duke@1 | 237 | } |
duke@1 | 238 | } |
duke@1 | 239 | return false; |
duke@1 | 240 | } |
duke@1 | 241 | |
duke@1 | 242 | /** |
duke@1 | 243 | * Return true if this class is abstract |
duke@1 | 244 | */ |
duke@1 | 245 | public boolean isAbstract() { |
duke@1 | 246 | return Modifier.isAbstract(getModifiers()); |
duke@1 | 247 | } |
duke@1 | 248 | |
duke@1 | 249 | /** |
duke@1 | 250 | * Returns true if this class was synthesized by the compiler. |
duke@1 | 251 | */ |
duke@1 | 252 | public boolean isSynthetic() { |
duke@1 | 253 | return (getFlags() & Flags.SYNTHETIC) != 0; |
duke@1 | 254 | } |
duke@1 | 255 | |
duke@1 | 256 | /** |
duke@1 | 257 | * Return true if this class is included in the active set. |
duke@1 | 258 | * A ClassDoc is included iff either it is specified on the |
duke@1 | 259 | * commandline, or if it's containing package is specified |
duke@1 | 260 | * on the command line, or if it is a member class of an |
duke@1 | 261 | * included class. |
duke@1 | 262 | */ |
duke@1 | 263 | |
duke@1 | 264 | public boolean isIncluded() { |
duke@1 | 265 | if (isIncluded) { |
duke@1 | 266 | return true; |
duke@1 | 267 | } |
duke@1 | 268 | if (env.shouldDocument(tsym)) { |
duke@1 | 269 | // Class is nameable from top-level and |
duke@1 | 270 | // the class and all enclosing classes |
duke@1 | 271 | // pass the modifier filter. |
duke@1 | 272 | if (containingPackage().isIncluded()) { |
duke@1 | 273 | return isIncluded=true; |
duke@1 | 274 | } |
duke@1 | 275 | ClassDoc outer = containingClass(); |
duke@1 | 276 | if (outer != null && outer.isIncluded()) { |
duke@1 | 277 | return isIncluded=true; |
duke@1 | 278 | } |
duke@1 | 279 | } |
duke@1 | 280 | return false; |
duke@1 | 281 | } |
duke@1 | 282 | |
vromero@1454 | 283 | public boolean isFunctionalInterface() { |
vromero@1454 | 284 | return env.types.isFunctionalInterface(tsym); |
vromero@1454 | 285 | } |
vromero@1454 | 286 | |
duke@1 | 287 | /** |
duke@1 | 288 | * Return the package that this class is contained in. |
duke@1 | 289 | */ |
jjg@910 | 290 | @Override |
duke@1 | 291 | public PackageDoc containingPackage() { |
duke@1 | 292 | PackageDocImpl p = env.getPackageDoc(tsym.packge()); |
jjg@197 | 293 | if (p.setDocPath == false) { |
jjg@197 | 294 | FileObject docPath; |
jjg@197 | 295 | try { |
jjg@197 | 296 | Location location = env.fileManager.hasLocation(StandardLocation.SOURCE_PATH) |
jjg@197 | 297 | ? StandardLocation.SOURCE_PATH : StandardLocation.CLASS_PATH; |
jjg@197 | 298 | |
jjg@197 | 299 | docPath = env.fileManager.getFileForInput( |
jjg@197 | 300 | location, p.qualifiedName(), "package.html"); |
jjg@197 | 301 | } catch (IOException e) { |
jjg@197 | 302 | docPath = null; |
duke@1 | 303 | } |
jjg@197 | 304 | |
jjg@197 | 305 | if (docPath == null) { |
jjg@197 | 306 | // fall back on older semantics of looking in same directory as |
jjg@197 | 307 | // source file for this class |
jjg@197 | 308 | SourcePosition po = position(); |
jjg@197 | 309 | if (env.fileManager instanceof StandardJavaFileManager && |
jjg@197 | 310 | po instanceof SourcePositionImpl) { |
jjg@197 | 311 | URI uri = ((SourcePositionImpl) po).filename.toUri(); |
jjg@197 | 312 | if ("file".equals(uri.getScheme())) { |
jjg@400 | 313 | File f = new File(uri); |
jjg@197 | 314 | File dir = f.getParentFile(); |
jjg@197 | 315 | if (dir != null) { |
jjg@197 | 316 | File pf = new File(dir, "package.html"); |
jjg@197 | 317 | if (pf.exists()) { |
jjg@197 | 318 | StandardJavaFileManager sfm = (StandardJavaFileManager) env.fileManager; |
jjg@197 | 319 | docPath = sfm.getJavaFileObjects(pf).iterator().next(); |
jjg@197 | 320 | } |
jjg@197 | 321 | } |
jjg@197 | 322 | |
jjg@197 | 323 | } |
jjg@197 | 324 | } |
jjg@197 | 325 | } |
jjg@197 | 326 | |
jjg@197 | 327 | p.setDocPath(docPath); |
duke@1 | 328 | } |
duke@1 | 329 | return p; |
duke@1 | 330 | } |
duke@1 | 331 | |
duke@1 | 332 | /** |
duke@1 | 333 | * Return the class name without package qualifier - but with |
duke@1 | 334 | * enclosing class qualifier - as a String. |
duke@1 | 335 | * <pre> |
duke@1 | 336 | * Examples: |
duke@1 | 337 | * for java.util.Hashtable |
duke@1 | 338 | * return Hashtable |
duke@1 | 339 | * for java.util.Map.Entry |
duke@1 | 340 | * return Map.Entry |
duke@1 | 341 | * </pre> |
duke@1 | 342 | */ |
duke@1 | 343 | public String name() { |
jjg@1706 | 344 | if (name == null) { |
jjg@1706 | 345 | name = getClassName(tsym, false); |
jjg@1706 | 346 | } |
jjg@1706 | 347 | return name; |
duke@1 | 348 | } |
duke@1 | 349 | |
jjg@1706 | 350 | private String name; |
jjg@1706 | 351 | |
duke@1 | 352 | /** |
duke@1 | 353 | * Return the qualified class name as a String. |
duke@1 | 354 | * <pre> |
duke@1 | 355 | * Example: |
duke@1 | 356 | * for java.util.Hashtable |
duke@1 | 357 | * return java.util.Hashtable |
duke@1 | 358 | * if no qualifier, just return flat name |
duke@1 | 359 | * </pre> |
duke@1 | 360 | */ |
duke@1 | 361 | public String qualifiedName() { |
jjg@1706 | 362 | if (qualifiedName == null) { |
jjg@1706 | 363 | qualifiedName = getClassName(tsym, true); |
jjg@1706 | 364 | } |
jjg@1706 | 365 | return qualifiedName; |
duke@1 | 366 | } |
duke@1 | 367 | |
jjg@1706 | 368 | private String qualifiedName; |
jjg@1706 | 369 | |
duke@1 | 370 | /** |
duke@1 | 371 | * Return unqualified name of type excluding any dimension information. |
duke@1 | 372 | * <p> |
duke@1 | 373 | * For example, a two dimensional array of String returns 'String'. |
duke@1 | 374 | */ |
duke@1 | 375 | public String typeName() { |
duke@1 | 376 | return name(); |
duke@1 | 377 | } |
duke@1 | 378 | |
duke@1 | 379 | /** |
duke@1 | 380 | * Return qualified name of type excluding any dimension information. |
duke@1 | 381 | *<p> |
duke@1 | 382 | * For example, a two dimensional array of String |
duke@1 | 383 | * returns 'java.lang.String'. |
duke@1 | 384 | */ |
duke@1 | 385 | public String qualifiedTypeName() { |
duke@1 | 386 | return qualifiedName(); |
duke@1 | 387 | } |
duke@1 | 388 | |
duke@1 | 389 | /** |
duke@1 | 390 | * Return the simple name of this type. |
duke@1 | 391 | */ |
duke@1 | 392 | public String simpleTypeName() { |
jjg@1706 | 393 | if (simpleTypeName == null) { |
jjg@1706 | 394 | simpleTypeName = tsym.name.toString(); |
jjg@1706 | 395 | } |
jjg@1706 | 396 | return simpleTypeName; |
duke@1 | 397 | } |
duke@1 | 398 | |
jjg@1706 | 399 | private String simpleTypeName; |
jjg@1706 | 400 | |
duke@1 | 401 | /** |
duke@1 | 402 | * Return the qualified name and any type parameters. |
duke@1 | 403 | * Each parameter is a type variable with optional bounds. |
duke@1 | 404 | */ |
jjg@910 | 405 | @Override |
duke@1 | 406 | public String toString() { |
duke@1 | 407 | return classToString(env, tsym, true); |
duke@1 | 408 | } |
duke@1 | 409 | |
duke@1 | 410 | /** |
duke@1 | 411 | * Return the class name as a string. If "full" is true the name is |
duke@1 | 412 | * qualified, otherwise it is qualified by its enclosing class(es) only. |
duke@1 | 413 | */ |
duke@1 | 414 | static String getClassName(ClassSymbol c, boolean full) { |
duke@1 | 415 | if (full) { |
duke@1 | 416 | return c.getQualifiedName().toString(); |
duke@1 | 417 | } else { |
duke@1 | 418 | String n = ""; |
duke@1 | 419 | for ( ; c != null; c = c.owner.enclClass()) { |
duke@1 | 420 | n = c.name + (n.equals("") ? "" : ".") + n; |
duke@1 | 421 | } |
duke@1 | 422 | return n; |
duke@1 | 423 | } |
duke@1 | 424 | } |
duke@1 | 425 | |
duke@1 | 426 | /** |
duke@1 | 427 | * Return the class name with any type parameters as a string. |
duke@1 | 428 | * Each parameter is a type variable with optional bounds. |
duke@1 | 429 | * If "full" is true all names are qualified, otherwise they are |
duke@1 | 430 | * qualified by their enclosing class(es) only. |
duke@1 | 431 | */ |
duke@1 | 432 | static String classToString(DocEnv env, ClassSymbol c, boolean full) { |
jjg@910 | 433 | StringBuilder s = new StringBuilder(); |
duke@1 | 434 | if (!c.isInner()) { // if c is not an inner class |
duke@1 | 435 | s.append(getClassName(c, full)); |
duke@1 | 436 | } else { |
duke@1 | 437 | // c is an inner class, so include type params of outer. |
duke@1 | 438 | ClassSymbol encl = c.owner.enclClass(); |
duke@1 | 439 | s.append(classToString(env, encl, full)) |
duke@1 | 440 | .append('.') |
duke@1 | 441 | .append(c.name); |
duke@1 | 442 | } |
duke@1 | 443 | s.append(TypeMaker.typeParametersString(env, c, full)); |
duke@1 | 444 | return s.toString(); |
duke@1 | 445 | } |
duke@1 | 446 | |
duke@1 | 447 | /** |
duke@1 | 448 | * Is this class (or any enclosing class) generic? That is, does |
duke@1 | 449 | * it have type parameters? |
duke@1 | 450 | */ |
duke@1 | 451 | static boolean isGeneric(ClassSymbol c) { |
duke@1 | 452 | return c.type.allparams().nonEmpty(); |
duke@1 | 453 | } |
duke@1 | 454 | |
duke@1 | 455 | /** |
duke@1 | 456 | * Return the formal type parameters of this class or interface. |
duke@1 | 457 | * Return an empty array if there are none. |
duke@1 | 458 | */ |
duke@1 | 459 | public TypeVariable[] typeParameters() { |
duke@1 | 460 | if (env.legacyDoclet) { |
duke@1 | 461 | return new TypeVariable[0]; |
duke@1 | 462 | } |
duke@1 | 463 | TypeVariable res[] = new TypeVariable[type.getTypeArguments().length()]; |
duke@1 | 464 | TypeMaker.getTypes(env, type.getTypeArguments(), res); |
duke@1 | 465 | return res; |
duke@1 | 466 | } |
duke@1 | 467 | |
duke@1 | 468 | /** |
duke@1 | 469 | * Return the type parameter tags of this class or interface. |
duke@1 | 470 | */ |
duke@1 | 471 | public ParamTag[] typeParamTags() { |
duke@1 | 472 | return (env.legacyDoclet) |
duke@1 | 473 | ? new ParamTag[0] |
duke@1 | 474 | : comment().typeParamTags(); |
duke@1 | 475 | } |
duke@1 | 476 | |
duke@1 | 477 | /** |
duke@1 | 478 | * Return the modifier string for this class. If it's an interface |
duke@1 | 479 | * exclude 'abstract' keyword from the modifier string |
duke@1 | 480 | */ |
jjg@910 | 481 | @Override |
duke@1 | 482 | public String modifiers() { |
duke@1 | 483 | return Modifier.toString(modifierSpecifier()); |
duke@1 | 484 | } |
duke@1 | 485 | |
jjg@910 | 486 | @Override |
duke@1 | 487 | public int modifierSpecifier() { |
duke@1 | 488 | int modifiers = getModifiers(); |
duke@1 | 489 | return (isInterface() || isAnnotationType()) |
duke@1 | 490 | ? modifiers & ~Modifier.ABSTRACT |
duke@1 | 491 | : modifiers; |
duke@1 | 492 | } |
duke@1 | 493 | |
duke@1 | 494 | /** |
duke@1 | 495 | * Return the superclass of this class |
duke@1 | 496 | * |
duke@1 | 497 | * @return the ClassDocImpl for the superclass of this class, null |
duke@1 | 498 | * if there is no superclass. |
duke@1 | 499 | */ |
duke@1 | 500 | public ClassDoc superclass() { |
duke@1 | 501 | if (isInterface() || isAnnotationType()) return null; |
duke@1 | 502 | if (tsym == env.syms.objectType.tsym) return null; |
duke@1 | 503 | ClassSymbol c = (ClassSymbol)env.types.supertype(type).tsym; |
duke@1 | 504 | if (c == null || c == tsym) c = (ClassSymbol)env.syms.objectType.tsym; |
duke@1 | 505 | return env.getClassDoc(c); |
duke@1 | 506 | } |
duke@1 | 507 | |
duke@1 | 508 | /** |
duke@1 | 509 | * Return the superclass of this class. Return null if this is an |
duke@1 | 510 | * interface. A superclass is represented by either a |
duke@1 | 511 | * <code>ClassDoc</code> or a <code>ParameterizedType</code>. |
duke@1 | 512 | */ |
duke@1 | 513 | public com.sun.javadoc.Type superclassType() { |
duke@1 | 514 | if (isInterface() || isAnnotationType() || |
duke@1 | 515 | (tsym == env.syms.objectType.tsym)) |
duke@1 | 516 | return null; |
duke@1 | 517 | Type sup = env.types.supertype(type); |
duke@1 | 518 | return TypeMaker.getType(env, |
duke@1 | 519 | (sup != type) ? sup : env.syms.objectType); |
duke@1 | 520 | } |
duke@1 | 521 | |
duke@1 | 522 | /** |
duke@1 | 523 | * Test whether this class is a subclass of the specified class. |
duke@1 | 524 | * |
duke@1 | 525 | * @param cd the candidate superclass. |
duke@1 | 526 | * @return true if cd is a superclass of this class. |
duke@1 | 527 | */ |
duke@1 | 528 | public boolean subclassOf(ClassDoc cd) { |
duke@1 | 529 | return tsym.isSubClass(((ClassDocImpl)cd).tsym, env.types); |
duke@1 | 530 | } |
duke@1 | 531 | |
duke@1 | 532 | /** |
duke@1 | 533 | * Return interfaces implemented by this class or interfaces |
duke@1 | 534 | * extended by this interface. |
duke@1 | 535 | * |
duke@1 | 536 | * @return An array of ClassDocImpl representing the interfaces. |
duke@1 | 537 | * Return an empty array if there are no interfaces. |
duke@1 | 538 | */ |
duke@1 | 539 | public ClassDoc[] interfaces() { |
duke@1 | 540 | ListBuffer<ClassDocImpl> ta = new ListBuffer<ClassDocImpl>(); |
duke@1 | 541 | for (Type t : env.types.interfaces(type)) { |
duke@1 | 542 | ta.append(env.getClassDoc((ClassSymbol)t.tsym)); |
duke@1 | 543 | } |
duke@1 | 544 | //### Cache ta here? |
duke@1 | 545 | return ta.toArray(new ClassDocImpl[ta.length()]); |
duke@1 | 546 | } |
duke@1 | 547 | |
duke@1 | 548 | /** |
duke@1 | 549 | * Return interfaces implemented by this class or interfaces extended |
duke@1 | 550 | * by this interface. Includes only directly-declared interfaces, not |
duke@1 | 551 | * inherited interfaces. |
duke@1 | 552 | * Return an empty array if there are no interfaces. |
duke@1 | 553 | */ |
duke@1 | 554 | public com.sun.javadoc.Type[] interfaceTypes() { |
duke@1 | 555 | //### Cache result here? |
duke@1 | 556 | return TypeMaker.getTypes(env, env.types.interfaces(type)); |
duke@1 | 557 | } |
duke@1 | 558 | |
duke@1 | 559 | /** |
duke@1 | 560 | * Return fields in class. |
duke@1 | 561 | * @param filter include only the included fields if filter==true |
duke@1 | 562 | */ |
duke@1 | 563 | public FieldDoc[] fields(boolean filter) { |
duke@1 | 564 | return fields(filter, false); |
duke@1 | 565 | } |
duke@1 | 566 | |
duke@1 | 567 | /** |
duke@1 | 568 | * Return included fields in class. |
duke@1 | 569 | */ |
duke@1 | 570 | public FieldDoc[] fields() { |
duke@1 | 571 | return fields(true, false); |
duke@1 | 572 | } |
duke@1 | 573 | |
duke@1 | 574 | /** |
duke@1 | 575 | * Return the enum constants if this is an enum type. |
duke@1 | 576 | */ |
duke@1 | 577 | public FieldDoc[] enumConstants() { |
duke@1 | 578 | return fields(false, true); |
duke@1 | 579 | } |
duke@1 | 580 | |
duke@1 | 581 | /** |
duke@1 | 582 | * Return fields in class. |
duke@1 | 583 | * @param filter if true, return only the included fields |
duke@1 | 584 | * @param enumConstants if true, return the enum constants instead |
duke@1 | 585 | */ |
duke@1 | 586 | private FieldDoc[] fields(boolean filter, boolean enumConstants) { |
duke@1 | 587 | List<FieldDocImpl> fields = List.nil(); |
duke@1 | 588 | for (Scope.Entry e = tsym.members().elems; e != null; e = e.sibling) { |
duke@1 | 589 | if (e.sym != null && e.sym.kind == VAR) { |
duke@1 | 590 | VarSymbol s = (VarSymbol)e.sym; |
duke@1 | 591 | boolean isEnum = ((s.flags() & Flags.ENUM) != 0) && |
duke@1 | 592 | !env.legacyDoclet; |
duke@1 | 593 | if (isEnum == enumConstants && |
duke@1 | 594 | (!filter || env.shouldDocument(s))) { |
duke@1 | 595 | fields = fields.prepend(env.getFieldDoc(s)); |
duke@1 | 596 | } |
duke@1 | 597 | } |
duke@1 | 598 | } |
duke@1 | 599 | return fields.toArray(new FieldDocImpl[fields.length()]); |
duke@1 | 600 | } |
duke@1 | 601 | |
duke@1 | 602 | /** |
duke@1 | 603 | * Return methods in class. |
duke@1 | 604 | * This method is overridden by AnnotationTypeDocImpl. |
duke@1 | 605 | * |
duke@1 | 606 | * @param filter include only the included methods if filter==true |
duke@1 | 607 | * @return an array of MethodDocImpl for representing the visible |
duke@1 | 608 | * methods in this class. Does not include constructors. |
duke@1 | 609 | */ |
duke@1 | 610 | public MethodDoc[] methods(boolean filter) { |
jjg@113 | 611 | Names names = tsym.name.table.names; |
duke@1 | 612 | List<MethodDocImpl> methods = List.nil(); |
duke@1 | 613 | for (Scope.Entry e = tsym.members().elems; e != null; e = e.sibling) { |
duke@1 | 614 | if (e.sym != null && |
duke@1 | 615 | e.sym.kind == Kinds.MTH && e.sym.name != names.init) { |
duke@1 | 616 | MethodSymbol s = (MethodSymbol)e.sym; |
duke@1 | 617 | if (!filter || env.shouldDocument(s)) { |
duke@1 | 618 | methods = methods.prepend(env.getMethodDoc(s)); |
duke@1 | 619 | } |
duke@1 | 620 | } |
duke@1 | 621 | } |
duke@1 | 622 | //### Cache methods here? |
duke@1 | 623 | return methods.toArray(new MethodDocImpl[methods.length()]); |
duke@1 | 624 | } |
duke@1 | 625 | |
duke@1 | 626 | /** |
duke@1 | 627 | * Return included methods in class. |
duke@1 | 628 | * |
duke@1 | 629 | * @return an array of MethodDocImpl for representing the visible |
duke@1 | 630 | * methods in this class. Does not include constructors. |
duke@1 | 631 | */ |
duke@1 | 632 | public MethodDoc[] methods() { |
duke@1 | 633 | return methods(true); |
duke@1 | 634 | } |
duke@1 | 635 | |
duke@1 | 636 | /** |
duke@1 | 637 | * Return constructors in class. |
duke@1 | 638 | * |
duke@1 | 639 | * @param filter include only the included constructors if filter==true |
duke@1 | 640 | * @return an array of ConstructorDocImpl for representing the visible |
duke@1 | 641 | * constructors in this class. |
duke@1 | 642 | */ |
duke@1 | 643 | public ConstructorDoc[] constructors(boolean filter) { |
jjg@113 | 644 | Names names = tsym.name.table.names; |
duke@1 | 645 | List<ConstructorDocImpl> constructors = List.nil(); |
duke@1 | 646 | for (Scope.Entry e = tsym.members().elems; e != null; e = e.sibling) { |
duke@1 | 647 | if (e.sym != null && |
duke@1 | 648 | e.sym.kind == Kinds.MTH && e.sym.name == names.init) { |
duke@1 | 649 | MethodSymbol s = (MethodSymbol)e.sym; |
duke@1 | 650 | if (!filter || env.shouldDocument(s)) { |
duke@1 | 651 | constructors = constructors.prepend(env.getConstructorDoc(s)); |
duke@1 | 652 | } |
duke@1 | 653 | } |
duke@1 | 654 | } |
duke@1 | 655 | //### Cache constructors here? |
duke@1 | 656 | return constructors.toArray(new ConstructorDocImpl[constructors.length()]); |
duke@1 | 657 | } |
duke@1 | 658 | |
duke@1 | 659 | /** |
duke@1 | 660 | * Return included constructors in class. |
duke@1 | 661 | * |
duke@1 | 662 | * @return an array of ConstructorDocImpl for representing the visible |
duke@1 | 663 | * constructors in this class. |
duke@1 | 664 | */ |
duke@1 | 665 | public ConstructorDoc[] constructors() { |
duke@1 | 666 | return constructors(true); |
duke@1 | 667 | } |
duke@1 | 668 | |
duke@1 | 669 | /** |
duke@1 | 670 | * Adds all inner classes of this class, and their |
duke@1 | 671 | * inner classes recursively, to the list l. |
duke@1 | 672 | */ |
duke@1 | 673 | void addAllClasses(ListBuffer<ClassDocImpl> l, boolean filtered) { |
duke@1 | 674 | try { |
duke@1 | 675 | if (isSynthetic()) return; |
duke@1 | 676 | // sometimes synthetic classes are not marked synthetic |
duke@1 | 677 | if (!JavadocTool.isValidClassName(tsym.name.toString())) return; |
duke@1 | 678 | if (filtered && !env.shouldDocument(tsym)) return; |
duke@1 | 679 | if (l.contains(this)) return; |
duke@1 | 680 | l.append(this); |
duke@1 | 681 | List<ClassDocImpl> more = List.nil(); |
duke@1 | 682 | for (Scope.Entry e = tsym.members().elems; e != null; |
duke@1 | 683 | e = e.sibling) { |
duke@1 | 684 | if (e.sym != null && e.sym.kind == Kinds.TYP) { |
duke@1 | 685 | ClassSymbol s = (ClassSymbol)e.sym; |
duke@1 | 686 | ClassDocImpl c = env.getClassDoc(s); |
duke@1 | 687 | if (c.isSynthetic()) continue; |
duke@1 | 688 | if (c != null) more = more.prepend(c); |
duke@1 | 689 | } |
duke@1 | 690 | } |
duke@1 | 691 | // this extra step preserves the ordering from oldjavadoc |
duke@1 | 692 | for (; more.nonEmpty(); more=more.tail) { |
duke@1 | 693 | more.head.addAllClasses(l, filtered); |
duke@1 | 694 | } |
duke@1 | 695 | } catch (CompletionFailure e) { |
duke@1 | 696 | // quietly ignore completion failures |
duke@1 | 697 | } |
duke@1 | 698 | } |
duke@1 | 699 | |
duke@1 | 700 | /** |
duke@1 | 701 | * Return inner classes within this class. |
duke@1 | 702 | * |
duke@1 | 703 | * @param filter include only the included inner classes if filter==true. |
duke@1 | 704 | * @return an array of ClassDocImpl for representing the visible |
duke@1 | 705 | * classes defined in this class. Anonymous and local classes |
duke@1 | 706 | * are not included. |
duke@1 | 707 | */ |
duke@1 | 708 | public ClassDoc[] innerClasses(boolean filter) { |
duke@1 | 709 | ListBuffer<ClassDocImpl> innerClasses = new ListBuffer<ClassDocImpl>(); |
duke@1 | 710 | for (Scope.Entry e = tsym.members().elems; e != null; e = e.sibling) { |
duke@1 | 711 | if (e.sym != null && e.sym.kind == Kinds.TYP) { |
duke@1 | 712 | ClassSymbol s = (ClassSymbol)e.sym; |
duke@1 | 713 | if ((s.flags_field & Flags.SYNTHETIC) != 0) continue; |
duke@1 | 714 | if (!filter || env.isVisible(s)) { |
duke@1 | 715 | innerClasses.prepend(env.getClassDoc(s)); |
duke@1 | 716 | } |
duke@1 | 717 | } |
duke@1 | 718 | } |
duke@1 | 719 | //### Cache classes here? |
duke@1 | 720 | return innerClasses.toArray(new ClassDocImpl[innerClasses.length()]); |
duke@1 | 721 | } |
duke@1 | 722 | |
duke@1 | 723 | /** |
duke@1 | 724 | * Return included inner classes within this class. |
duke@1 | 725 | * |
duke@1 | 726 | * @return an array of ClassDocImpl for representing the visible |
duke@1 | 727 | * classes defined in this class. Anonymous and local classes |
duke@1 | 728 | * are not included. |
duke@1 | 729 | */ |
duke@1 | 730 | public ClassDoc[] innerClasses() { |
duke@1 | 731 | return innerClasses(true); |
duke@1 | 732 | } |
duke@1 | 733 | |
duke@1 | 734 | /** |
duke@1 | 735 | * Find a class within the context of this class. |
duke@1 | 736 | * Search order: qualified name, in this class (inner), |
duke@1 | 737 | * in this package, in the class imports, in the package |
duke@1 | 738 | * imports. |
duke@1 | 739 | * Return the ClassDocImpl if found, null if not found. |
duke@1 | 740 | */ |
duke@1 | 741 | //### The specified search order is not the normal rule the |
duke@1 | 742 | //### compiler would use. Leave as specified or change it? |
duke@1 | 743 | public ClassDoc findClass(String className) { |
duke@1 | 744 | ClassDoc searchResult = searchClass(className); |
duke@1 | 745 | if (searchResult == null) { |
duke@1 | 746 | ClassDocImpl enclosingClass = (ClassDocImpl)containingClass(); |
duke@1 | 747 | //Expand search space to include enclosing class. |
duke@1 | 748 | while (enclosingClass != null && enclosingClass.containingClass() != null) { |
duke@1 | 749 | enclosingClass = (ClassDocImpl)enclosingClass.containingClass(); |
duke@1 | 750 | } |
duke@1 | 751 | searchResult = enclosingClass == null ? |
duke@1 | 752 | null : enclosingClass.searchClass(className); |
duke@1 | 753 | } |
duke@1 | 754 | return searchResult; |
duke@1 | 755 | } |
duke@1 | 756 | |
duke@1 | 757 | private ClassDoc searchClass(String className) { |
jjg@113 | 758 | Names names = tsym.name.table.names; |
duke@1 | 759 | |
duke@1 | 760 | // search by qualified name first |
duke@1 | 761 | ClassDoc cd = env.lookupClass(className); |
duke@1 | 762 | if (cd != null) { |
duke@1 | 763 | return cd; |
duke@1 | 764 | } |
duke@1 | 765 | |
duke@1 | 766 | // search inner classes |
duke@1 | 767 | //### Add private entry point to avoid creating array? |
duke@1 | 768 | //### Replicate code in innerClasses here to avoid consing? |
ksrini@1065 | 769 | for (ClassDoc icd : innerClasses()) { |
ksrini@1065 | 770 | if (icd.name().equals(className) || |
ksrini@1065 | 771 | //### This is from original javadoc but it looks suspicious to me... |
ksrini@1065 | 772 | //### I believe it is attempting to compensate for the confused |
ksrini@1065 | 773 | //### convention of including the nested class qualifiers in the |
ksrini@1065 | 774 | //### 'name' of the inner class, rather than the true simple name. |
ksrini@1065 | 775 | icd.name().endsWith("." + className)) { |
ksrini@1065 | 776 | return icd; |
duke@1 | 777 | } else { |
ksrini@1065 | 778 | ClassDoc innercd = ((ClassDocImpl) icd).searchClass(className); |
duke@1 | 779 | if (innercd != null) { |
duke@1 | 780 | return innercd; |
duke@1 | 781 | } |
duke@1 | 782 | } |
duke@1 | 783 | } |
duke@1 | 784 | |
duke@1 | 785 | // check in this package |
duke@1 | 786 | cd = containingPackage().findClass(className); |
duke@1 | 787 | if (cd != null) { |
duke@1 | 788 | return cd; |
duke@1 | 789 | } |
duke@1 | 790 | |
duke@1 | 791 | // make sure that this symbol has been completed |
duke@1 | 792 | if (tsym.completer != null) { |
duke@1 | 793 | tsym.complete(); |
duke@1 | 794 | } |
duke@1 | 795 | |
duke@1 | 796 | // search imports |
duke@1 | 797 | |
duke@1 | 798 | if (tsym.sourcefile != null) { |
duke@1 | 799 | |
duke@1 | 800 | //### This information is available only for source classes. |
duke@1 | 801 | |
duke@1 | 802 | Env<AttrContext> compenv = env.enter.getEnv(tsym); |
duke@1 | 803 | if (compenv == null) return null; |
duke@1 | 804 | |
duke@1 | 805 | Scope s = compenv.toplevel.namedImportScope; |
duke@1 | 806 | for (Scope.Entry e = s.lookup(names.fromString(className)); e.scope != null; e = e.next()) { |
duke@1 | 807 | if (e.sym.kind == Kinds.TYP) { |
duke@1 | 808 | ClassDoc c = env.getClassDoc((ClassSymbol)e.sym); |
duke@1 | 809 | return c; |
duke@1 | 810 | } |
duke@1 | 811 | } |
duke@1 | 812 | |
duke@1 | 813 | s = compenv.toplevel.starImportScope; |
duke@1 | 814 | for (Scope.Entry e = s.lookup(names.fromString(className)); e.scope != null; e = e.next()) { |
duke@1 | 815 | if (e.sym.kind == Kinds.TYP) { |
duke@1 | 816 | ClassDoc c = env.getClassDoc((ClassSymbol)e.sym); |
duke@1 | 817 | return c; |
duke@1 | 818 | } |
duke@1 | 819 | } |
duke@1 | 820 | } |
duke@1 | 821 | |
duke@1 | 822 | return null; // not found |
duke@1 | 823 | } |
duke@1 | 824 | |
duke@1 | 825 | |
duke@1 | 826 | private boolean hasParameterTypes(MethodSymbol method, String[] argTypes) { |
duke@1 | 827 | |
duke@1 | 828 | if (argTypes == null) { |
duke@1 | 829 | // wildcard |
duke@1 | 830 | return true; |
duke@1 | 831 | } |
duke@1 | 832 | |
duke@1 | 833 | int i = 0; |
duke@1 | 834 | List<Type> types = method.type.getParameterTypes(); |
duke@1 | 835 | |
duke@1 | 836 | if (argTypes.length != types.length()) { |
duke@1 | 837 | return false; |
duke@1 | 838 | } |
duke@1 | 839 | |
duke@1 | 840 | for (Type t : types) { |
duke@1 | 841 | String argType = argTypes[i++]; |
duke@1 | 842 | // For vararg method, "T..." matches type T[]. |
duke@1 | 843 | if (i == argTypes.length) { |
duke@1 | 844 | argType = argType.replace("...", "[]"); |
duke@1 | 845 | } |
duke@1 | 846 | if (!hasTypeName(env.types.erasure(t), argType)) { //###(gj) |
duke@1 | 847 | return false; |
duke@1 | 848 | } |
duke@1 | 849 | } |
duke@1 | 850 | return true; |
duke@1 | 851 | } |
duke@1 | 852 | // where |
duke@1 | 853 | private boolean hasTypeName(Type t, String name) { |
duke@1 | 854 | return |
duke@1 | 855 | name.equals(TypeMaker.getTypeName(t, true)) |
duke@1 | 856 | || |
duke@1 | 857 | name.equals(TypeMaker.getTypeName(t, false)) |
duke@1 | 858 | || |
duke@1 | 859 | (qualifiedName() + "." + name).equals(TypeMaker.getTypeName(t, true)); |
duke@1 | 860 | } |
duke@1 | 861 | |
duke@1 | 862 | |
duke@1 | 863 | |
duke@1 | 864 | /** |
duke@1 | 865 | * Find a method in this class scope. |
duke@1 | 866 | * Search order: this class, interfaces, superclasses, outerclasses. |
duke@1 | 867 | * Note that this is not necessarily what the compiler would do! |
duke@1 | 868 | * |
duke@1 | 869 | * @param methodName the unqualified name to search for. |
jjg@1358 | 870 | * @param paramTypes the array of Strings for method parameter types. |
duke@1 | 871 | * @return the first MethodDocImpl which matches, null if not found. |
duke@1 | 872 | */ |
duke@1 | 873 | public MethodDocImpl findMethod(String methodName, String[] paramTypes) { |
duke@1 | 874 | // Use hash table 'searched' to avoid searching same class twice. |
duke@1 | 875 | //### It is not clear how this could happen. |
duke@1 | 876 | return searchMethod(methodName, paramTypes, new HashSet<ClassDocImpl>()); |
duke@1 | 877 | } |
duke@1 | 878 | |
duke@1 | 879 | private MethodDocImpl searchMethod(String methodName, |
duke@1 | 880 | String[] paramTypes, Set<ClassDocImpl> searched) { |
duke@1 | 881 | //### Note that this search is not necessarily what the compiler would do! |
duke@1 | 882 | |
sundar@721 | 883 | Names names = tsym.name.table.names; |
sundar@721 | 884 | // do not match constructors |
sundar@721 | 885 | if (names.init.contentEquals(methodName)) { |
sundar@721 | 886 | return null; |
sundar@721 | 887 | } |
sundar@721 | 888 | |
duke@1 | 889 | ClassDocImpl cdi; |
duke@1 | 890 | MethodDocImpl mdi; |
duke@1 | 891 | |
duke@1 | 892 | if (searched.contains(this)) { |
duke@1 | 893 | return null; |
duke@1 | 894 | } |
duke@1 | 895 | searched.add(this); |
duke@1 | 896 | |
duke@1 | 897 | //DEBUG |
duke@1 | 898 | /*---------------------------------* |
duke@1 | 899 | System.out.print("searching " + this + " for " + methodName); |
duke@1 | 900 | if (paramTypes == null) { |
duke@1 | 901 | System.out.println("()"); |
duke@1 | 902 | } else { |
duke@1 | 903 | System.out.print("("); |
duke@1 | 904 | for (int k=0; k < paramTypes.length; k++) { |
duke@1 | 905 | System.out.print(paramTypes[k]); |
duke@1 | 906 | if ((k + 1) < paramTypes.length) { |
duke@1 | 907 | System.out.print(", "); |
duke@1 | 908 | } |
duke@1 | 909 | } |
duke@1 | 910 | System.out.println(")"); |
duke@1 | 911 | } |
duke@1 | 912 | *---------------------------------*/ |
duke@1 | 913 | |
duke@1 | 914 | // search current class |
duke@1 | 915 | Scope.Entry e = tsym.members().lookup(names.fromString(methodName)); |
duke@1 | 916 | |
duke@1 | 917 | //### Using modifier filter here isn't really correct, |
duke@1 | 918 | //### but emulates the old behavior. Instead, we should |
duke@1 | 919 | //### apply the normal rules of visibility and inheritance. |
duke@1 | 920 | |
duke@1 | 921 | if (paramTypes == null) { |
duke@1 | 922 | // If no parameters specified, we are allowed to return |
duke@1 | 923 | // any method with a matching name. In practice, the old |
duke@1 | 924 | // code returned the first method, which is now the last! |
duke@1 | 925 | // In order to provide textually identical results, we |
duke@1 | 926 | // attempt to emulate the old behavior. |
duke@1 | 927 | MethodSymbol lastFound = null; |
duke@1 | 928 | for (; e.scope != null; e = e.next()) { |
duke@1 | 929 | if (e.sym.kind == Kinds.MTH) { |
duke@1 | 930 | //### Should intern methodName as Name. |
duke@1 | 931 | if (e.sym.name.toString().equals(methodName)) { |
duke@1 | 932 | lastFound = (MethodSymbol)e.sym; |
duke@1 | 933 | } |
duke@1 | 934 | } |
duke@1 | 935 | } |
duke@1 | 936 | if (lastFound != null) { |
duke@1 | 937 | return env.getMethodDoc(lastFound); |
duke@1 | 938 | } |
duke@1 | 939 | } else { |
duke@1 | 940 | for (; e.scope != null; e = e.next()) { |
duke@1 | 941 | if (e.sym != null && |
duke@1 | 942 | e.sym.kind == Kinds.MTH) { |
duke@1 | 943 | //### Should intern methodName as Name. |
duke@1 | 944 | if (hasParameterTypes((MethodSymbol)e.sym, paramTypes)) { |
duke@1 | 945 | return env.getMethodDoc((MethodSymbol)e.sym); |
duke@1 | 946 | } |
duke@1 | 947 | } |
duke@1 | 948 | } |
duke@1 | 949 | } |
duke@1 | 950 | |
duke@1 | 951 | //### If we found a MethodDoc above, but which did not pass |
duke@1 | 952 | //### the modifier filter, we should return failure here! |
duke@1 | 953 | |
duke@1 | 954 | // search superclass |
duke@1 | 955 | cdi = (ClassDocImpl)superclass(); |
duke@1 | 956 | if (cdi != null) { |
duke@1 | 957 | mdi = cdi.searchMethod(methodName, paramTypes, searched); |
duke@1 | 958 | if (mdi != null) { |
duke@1 | 959 | return mdi; |
duke@1 | 960 | } |
duke@1 | 961 | } |
duke@1 | 962 | |
duke@1 | 963 | // search interfaces |
duke@1 | 964 | ClassDoc intf[] = interfaces(); |
duke@1 | 965 | for (int i = 0; i < intf.length; i++) { |
duke@1 | 966 | cdi = (ClassDocImpl)intf[i]; |
duke@1 | 967 | mdi = cdi.searchMethod(methodName, paramTypes, searched); |
duke@1 | 968 | if (mdi != null) { |
duke@1 | 969 | return mdi; |
duke@1 | 970 | } |
duke@1 | 971 | } |
duke@1 | 972 | |
duke@1 | 973 | // search enclosing class |
duke@1 | 974 | cdi = (ClassDocImpl)containingClass(); |
duke@1 | 975 | if (cdi != null) { |
duke@1 | 976 | mdi = cdi.searchMethod(methodName, paramTypes, searched); |
duke@1 | 977 | if (mdi != null) { |
duke@1 | 978 | return mdi; |
duke@1 | 979 | } |
duke@1 | 980 | } |
duke@1 | 981 | |
duke@1 | 982 | //###(gj) As a temporary measure until type variables are better |
duke@1 | 983 | //### handled, try again without the parameter types. |
duke@1 | 984 | //### This should most often find the right method, and occassionally |
duke@1 | 985 | //### find the wrong one. |
duke@1 | 986 | //if (paramTypes != null) { |
duke@1 | 987 | // return findMethod(methodName, null); |
duke@1 | 988 | //} |
duke@1 | 989 | |
duke@1 | 990 | return null; |
duke@1 | 991 | } |
duke@1 | 992 | |
duke@1 | 993 | /** |
duke@1 | 994 | * Find constructor in this class. |
duke@1 | 995 | * |
duke@1 | 996 | * @param constrName the unqualified name to search for. |
jjg@1358 | 997 | * @param paramTypes the array of Strings for constructor parameters. |
duke@1 | 998 | * @return the first ConstructorDocImpl which matches, null if not found. |
duke@1 | 999 | */ |
duke@1 | 1000 | public ConstructorDoc findConstructor(String constrName, |
duke@1 | 1001 | String[] paramTypes) { |
jjg@113 | 1002 | Names names = tsym.name.table.names; |
duke@1 | 1003 | for (Scope.Entry e = tsym.members().lookup(names.fromString("<init>")); e.scope != null; e = e.next()) { |
duke@1 | 1004 | if (e.sym.kind == Kinds.MTH) { |
duke@1 | 1005 | if (hasParameterTypes((MethodSymbol)e.sym, paramTypes)) { |
duke@1 | 1006 | return env.getConstructorDoc((MethodSymbol)e.sym); |
duke@1 | 1007 | } |
duke@1 | 1008 | } |
duke@1 | 1009 | } |
duke@1 | 1010 | |
duke@1 | 1011 | //###(gj) As a temporary measure until type variables are better |
duke@1 | 1012 | //### handled, try again without the parameter types. |
duke@1 | 1013 | //### This will often find the right constructor, and occassionally |
duke@1 | 1014 | //### find the wrong one. |
duke@1 | 1015 | //if (paramTypes != null) { |
duke@1 | 1016 | // return findConstructor(constrName, null); |
duke@1 | 1017 | //} |
duke@1 | 1018 | |
duke@1 | 1019 | return null; |
duke@1 | 1020 | } |
duke@1 | 1021 | |
duke@1 | 1022 | /** |
duke@1 | 1023 | * Find a field in this class scope. |
duke@1 | 1024 | * Search order: this class, outerclasses, interfaces, |
duke@1 | 1025 | * superclasses. IMP: If see tag is defined in an inner class, |
duke@1 | 1026 | * which extends a super class and if outerclass and the super |
duke@1 | 1027 | * class have a visible field in common then Java compiler cribs |
duke@1 | 1028 | * about the ambiguity, but the following code will search in the |
duke@1 | 1029 | * above given search order. |
duke@1 | 1030 | * |
duke@1 | 1031 | * @param fieldName the unqualified name to search for. |
duke@1 | 1032 | * @return the first FieldDocImpl which matches, null if not found. |
duke@1 | 1033 | */ |
duke@1 | 1034 | public FieldDoc findField(String fieldName) { |
duke@1 | 1035 | return searchField(fieldName, new HashSet<ClassDocImpl>()); |
duke@1 | 1036 | } |
duke@1 | 1037 | |
duke@1 | 1038 | private FieldDocImpl searchField(String fieldName, Set<ClassDocImpl> searched) { |
jjg@113 | 1039 | Names names = tsym.name.table.names; |
duke@1 | 1040 | if (searched.contains(this)) { |
duke@1 | 1041 | return null; |
duke@1 | 1042 | } |
duke@1 | 1043 | searched.add(this); |
duke@1 | 1044 | |
duke@1 | 1045 | for (Scope.Entry e = tsym.members().lookup(names.fromString(fieldName)); e.scope != null; e = e.next()) { |
duke@1 | 1046 | if (e.sym.kind == Kinds.VAR) { |
duke@1 | 1047 | //### Should intern fieldName as Name. |
duke@1 | 1048 | return env.getFieldDoc((VarSymbol)e.sym); |
duke@1 | 1049 | } |
duke@1 | 1050 | } |
duke@1 | 1051 | |
duke@1 | 1052 | //### If we found a FieldDoc above, but which did not pass |
duke@1 | 1053 | //### the modifier filter, we should return failure here! |
duke@1 | 1054 | |
duke@1 | 1055 | ClassDocImpl cdi = (ClassDocImpl)containingClass(); |
duke@1 | 1056 | if (cdi != null) { |
duke@1 | 1057 | FieldDocImpl fdi = cdi.searchField(fieldName, searched); |
duke@1 | 1058 | if (fdi != null) { |
duke@1 | 1059 | return fdi; |
duke@1 | 1060 | } |
duke@1 | 1061 | } |
duke@1 | 1062 | |
duke@1 | 1063 | // search superclass |
duke@1 | 1064 | cdi = (ClassDocImpl)superclass(); |
duke@1 | 1065 | if (cdi != null) { |
duke@1 | 1066 | FieldDocImpl fdi = cdi.searchField(fieldName, searched); |
duke@1 | 1067 | if (fdi != null) { |
duke@1 | 1068 | return fdi; |
duke@1 | 1069 | } |
duke@1 | 1070 | } |
duke@1 | 1071 | |
duke@1 | 1072 | // search interfaces |
duke@1 | 1073 | ClassDoc intf[] = interfaces(); |
duke@1 | 1074 | for (int i = 0; i < intf.length; i++) { |
duke@1 | 1075 | cdi = (ClassDocImpl)intf[i]; |
duke@1 | 1076 | FieldDocImpl fdi = cdi.searchField(fieldName, searched); |
duke@1 | 1077 | if (fdi != null) { |
duke@1 | 1078 | return fdi; |
duke@1 | 1079 | } |
duke@1 | 1080 | } |
duke@1 | 1081 | |
duke@1 | 1082 | return null; |
duke@1 | 1083 | } |
duke@1 | 1084 | |
duke@1 | 1085 | /** |
duke@1 | 1086 | * Get the list of classes declared as imported. |
duke@1 | 1087 | * These are called "single-type-import declarations" in the JLS. |
duke@1 | 1088 | * This method is deprecated in the ClassDoc interface. |
duke@1 | 1089 | * |
duke@1 | 1090 | * @return an array of ClassDocImpl representing the imported classes. |
duke@1 | 1091 | * |
duke@1 | 1092 | * @deprecated Import declarations are implementation details that |
duke@1 | 1093 | * should not be exposed here. In addition, not all imported |
duke@1 | 1094 | * classes are imported through single-type-import declarations. |
duke@1 | 1095 | */ |
duke@1 | 1096 | @Deprecated |
duke@1 | 1097 | public ClassDoc[] importedClasses() { |
duke@1 | 1098 | // information is not available for binary classfiles |
duke@1 | 1099 | if (tsym.sourcefile == null) return new ClassDoc[0]; |
duke@1 | 1100 | |
duke@1 | 1101 | ListBuffer<ClassDocImpl> importedClasses = new ListBuffer<ClassDocImpl>(); |
duke@1 | 1102 | |
duke@1 | 1103 | Env<AttrContext> compenv = env.enter.getEnv(tsym); |
duke@1 | 1104 | if (compenv == null) return new ClassDocImpl[0]; |
duke@1 | 1105 | |
jjg@113 | 1106 | Name asterisk = tsym.name.table.names.asterisk; |
duke@1 | 1107 | for (JCTree t : compenv.toplevel.defs) { |
jjg@1127 | 1108 | if (t.hasTag(IMPORT)) { |
duke@1 | 1109 | JCTree imp = ((JCImport) t).qualid; |
duke@1 | 1110 | if ((TreeInfo.name(imp) != asterisk) && |
duke@1 | 1111 | (imp.type.tsym.kind & Kinds.TYP) != 0) { |
duke@1 | 1112 | importedClasses.append( |
duke@1 | 1113 | env.getClassDoc((ClassSymbol)imp.type.tsym)); |
duke@1 | 1114 | } |
duke@1 | 1115 | } |
duke@1 | 1116 | } |
duke@1 | 1117 | |
duke@1 | 1118 | return importedClasses.toArray(new ClassDocImpl[importedClasses.length()]); |
duke@1 | 1119 | } |
duke@1 | 1120 | |
duke@1 | 1121 | /** |
duke@1 | 1122 | * Get the list of packages declared as imported. |
duke@1 | 1123 | * These are called "type-import-on-demand declarations" in the JLS. |
duke@1 | 1124 | * This method is deprecated in the ClassDoc interface. |
duke@1 | 1125 | * |
duke@1 | 1126 | * @return an array of PackageDocImpl representing the imported packages. |
duke@1 | 1127 | * |
duke@1 | 1128 | * ###NOTE: the syntax supports importing all inner classes from a class as well. |
duke@1 | 1129 | * @deprecated Import declarations are implementation details that |
duke@1 | 1130 | * should not be exposed here. In addition, this method's |
duke@1 | 1131 | * return type does not allow for all type-import-on-demand |
duke@1 | 1132 | * declarations to be returned. |
duke@1 | 1133 | */ |
duke@1 | 1134 | @Deprecated |
duke@1 | 1135 | public PackageDoc[] importedPackages() { |
duke@1 | 1136 | // information is not available for binary classfiles |
duke@1 | 1137 | if (tsym.sourcefile == null) return new PackageDoc[0]; |
duke@1 | 1138 | |
duke@1 | 1139 | ListBuffer<PackageDocImpl> importedPackages = new ListBuffer<PackageDocImpl>(); |
duke@1 | 1140 | |
duke@1 | 1141 | //### Add the implicit "import java.lang.*" to the result |
jjg@113 | 1142 | Names names = tsym.name.table.names; |
duke@1 | 1143 | importedPackages.append(env.getPackageDoc(env.reader.enterPackage(names.java_lang))); |
duke@1 | 1144 | |
duke@1 | 1145 | Env<AttrContext> compenv = env.enter.getEnv(tsym); |
duke@1 | 1146 | if (compenv == null) return new PackageDocImpl[0]; |
duke@1 | 1147 | |
duke@1 | 1148 | for (JCTree t : compenv.toplevel.defs) { |
jjg@1127 | 1149 | if (t.hasTag(IMPORT)) { |
duke@1 | 1150 | JCTree imp = ((JCImport) t).qualid; |
duke@1 | 1151 | if (TreeInfo.name(imp) == names.asterisk) { |
duke@1 | 1152 | JCFieldAccess sel = (JCFieldAccess)imp; |
duke@1 | 1153 | Symbol s = sel.selected.type.tsym; |
duke@1 | 1154 | PackageDocImpl pdoc = env.getPackageDoc(s.packge()); |
duke@1 | 1155 | if (!importedPackages.contains(pdoc)) |
duke@1 | 1156 | importedPackages.append(pdoc); |
duke@1 | 1157 | } |
duke@1 | 1158 | } |
duke@1 | 1159 | } |
duke@1 | 1160 | |
duke@1 | 1161 | return importedPackages.toArray(new PackageDocImpl[importedPackages.length()]); |
duke@1 | 1162 | } |
duke@1 | 1163 | |
duke@1 | 1164 | /** |
duke@1 | 1165 | * Return the type's dimension information. |
duke@1 | 1166 | * Always return "", as this is not an array type. |
duke@1 | 1167 | */ |
duke@1 | 1168 | public String dimension() { |
duke@1 | 1169 | return ""; |
duke@1 | 1170 | } |
duke@1 | 1171 | |
duke@1 | 1172 | /** |
duke@1 | 1173 | * Return this type as a class, which it already is. |
duke@1 | 1174 | */ |
duke@1 | 1175 | public ClassDoc asClassDoc() { |
duke@1 | 1176 | return this; |
duke@1 | 1177 | } |
duke@1 | 1178 | |
duke@1 | 1179 | /** |
duke@1 | 1180 | * Return null (unless overridden), as this is not an annotation type. |
duke@1 | 1181 | */ |
duke@1 | 1182 | public AnnotationTypeDoc asAnnotationTypeDoc() { |
duke@1 | 1183 | return null; |
duke@1 | 1184 | } |
duke@1 | 1185 | |
duke@1 | 1186 | /** |
duke@1 | 1187 | * Return null, as this is not a class instantiation. |
duke@1 | 1188 | */ |
duke@1 | 1189 | public ParameterizedType asParameterizedType() { |
duke@1 | 1190 | return null; |
duke@1 | 1191 | } |
duke@1 | 1192 | |
duke@1 | 1193 | /** |
duke@1 | 1194 | * Return null, as this is not a type variable. |
duke@1 | 1195 | */ |
duke@1 | 1196 | public TypeVariable asTypeVariable() { |
duke@1 | 1197 | return null; |
duke@1 | 1198 | } |
duke@1 | 1199 | |
duke@1 | 1200 | /** |
duke@1 | 1201 | * Return null, as this is not a wildcard type. |
duke@1 | 1202 | */ |
duke@1 | 1203 | public WildcardType asWildcardType() { |
duke@1 | 1204 | return null; |
duke@1 | 1205 | } |
duke@1 | 1206 | |
duke@1 | 1207 | /** |
jjg@1521 | 1208 | * Returns null, as this is not an annotated type. |
jjg@1521 | 1209 | */ |
jjg@1521 | 1210 | public AnnotatedType asAnnotatedType() { |
jjg@1521 | 1211 | return null; |
jjg@1521 | 1212 | } |
jjg@1521 | 1213 | |
jjg@1521 | 1214 | /** |
duke@1 | 1215 | * Return false, as this is not a primitive type. |
duke@1 | 1216 | */ |
duke@1 | 1217 | public boolean isPrimitive() { |
duke@1 | 1218 | return false; |
duke@1 | 1219 | } |
duke@1 | 1220 | |
duke@1 | 1221 | //--- Serialization --- |
duke@1 | 1222 | |
duke@1 | 1223 | //### These methods ignore modifier filter. |
duke@1 | 1224 | |
duke@1 | 1225 | /** |
duke@1 | 1226 | * Return true if this class implements <code>java.io.Serializable</code>. |
duke@1 | 1227 | * |
duke@1 | 1228 | * Since <code>java.io.Externalizable</code> extends |
duke@1 | 1229 | * <code>java.io.Serializable</code>, |
duke@1 | 1230 | * Externalizable objects are also Serializable. |
duke@1 | 1231 | */ |
duke@1 | 1232 | public boolean isSerializable() { |
duke@1 | 1233 | try { |
duke@1 | 1234 | return env.types.isSubtype(type, env.syms.serializableType); |
duke@1 | 1235 | } catch (CompletionFailure ex) { |
duke@1 | 1236 | // quietly ignore completion failures |
duke@1 | 1237 | return false; |
duke@1 | 1238 | } |
duke@1 | 1239 | } |
duke@1 | 1240 | |
duke@1 | 1241 | /** |
duke@1 | 1242 | * Return true if this class implements |
duke@1 | 1243 | * <code>java.io.Externalizable</code>. |
duke@1 | 1244 | */ |
duke@1 | 1245 | public boolean isExternalizable() { |
duke@1 | 1246 | try { |
duke@1 | 1247 | return env.types.isSubtype(type, env.externalizableSym.type); |
duke@1 | 1248 | } catch (CompletionFailure ex) { |
duke@1 | 1249 | // quietly ignore completion failures |
duke@1 | 1250 | return false; |
duke@1 | 1251 | } |
duke@1 | 1252 | } |
duke@1 | 1253 | |
duke@1 | 1254 | /** |
duke@1 | 1255 | * Return the serialization methods for this class. |
duke@1 | 1256 | * |
duke@1 | 1257 | * @return an array of <code>MethodDocImpl</code> that represents |
duke@1 | 1258 | * the serialization methods for this class. |
duke@1 | 1259 | */ |
duke@1 | 1260 | public MethodDoc[] serializationMethods() { |
duke@1 | 1261 | if (serializedForm == null) { |
duke@1 | 1262 | serializedForm = new SerializedForm(env, tsym, this); |
duke@1 | 1263 | } |
duke@1 | 1264 | //### Clone this? |
duke@1 | 1265 | return serializedForm.methods(); |
duke@1 | 1266 | } |
duke@1 | 1267 | |
duke@1 | 1268 | /** |
duke@1 | 1269 | * Return the Serializable fields of class.<p> |
duke@1 | 1270 | * |
duke@1 | 1271 | * Return either a list of default fields documented by |
duke@1 | 1272 | * <code>serial</code> tag<br> |
duke@1 | 1273 | * or return a single <code>FieldDoc</code> for |
duke@1 | 1274 | * <code>serialPersistentField</code> member. |
duke@1 | 1275 | * There should be a <code>serialField</code> tag for |
duke@1 | 1276 | * each Serializable field defined by an <code>ObjectStreamField</code> |
duke@1 | 1277 | * array component of <code>serialPersistentField</code>. |
duke@1 | 1278 | * |
duke@1 | 1279 | * @returns an array of <code>FieldDoc</code> for the Serializable fields |
duke@1 | 1280 | * of this class. |
duke@1 | 1281 | * |
duke@1 | 1282 | * @see #definesSerializableFields() |
duke@1 | 1283 | * @see SerialFieldTagImpl |
duke@1 | 1284 | */ |
duke@1 | 1285 | public FieldDoc[] serializableFields() { |
duke@1 | 1286 | if (serializedForm == null) { |
duke@1 | 1287 | serializedForm = new SerializedForm(env, tsym, this); |
duke@1 | 1288 | } |
duke@1 | 1289 | //### Clone this? |
duke@1 | 1290 | return serializedForm.fields(); |
duke@1 | 1291 | } |
duke@1 | 1292 | |
duke@1 | 1293 | /** |
duke@1 | 1294 | * Return true if Serializable fields are explicitly defined with |
duke@1 | 1295 | * the special class member <code>serialPersistentFields</code>. |
duke@1 | 1296 | * |
duke@1 | 1297 | * @see #serializableFields() |
duke@1 | 1298 | * @see SerialFieldTagImpl |
duke@1 | 1299 | */ |
duke@1 | 1300 | public boolean definesSerializableFields() { |
duke@1 | 1301 | if (!isSerializable() || isExternalizable()) { |
duke@1 | 1302 | return false; |
duke@1 | 1303 | } else { |
duke@1 | 1304 | if (serializedForm == null) { |
duke@1 | 1305 | serializedForm = new SerializedForm(env, tsym, this); |
duke@1 | 1306 | } |
duke@1 | 1307 | //### Clone this? |
duke@1 | 1308 | return serializedForm.definesSerializableFields(); |
duke@1 | 1309 | } |
duke@1 | 1310 | } |
duke@1 | 1311 | |
duke@1 | 1312 | /** |
duke@1 | 1313 | * Determine if a class is a RuntimeException. |
duke@1 | 1314 | * <p> |
duke@1 | 1315 | * Used only by ThrowsTagImpl. |
duke@1 | 1316 | */ |
duke@1 | 1317 | boolean isRuntimeException() { |
duke@1 | 1318 | return tsym.isSubClass(env.syms.runtimeExceptionType.tsym, env.types); |
duke@1 | 1319 | } |
duke@1 | 1320 | |
duke@1 | 1321 | /** |
duke@1 | 1322 | * Return the source position of the entity, or null if |
duke@1 | 1323 | * no position is available. |
duke@1 | 1324 | */ |
jjg@910 | 1325 | @Override |
duke@1 | 1326 | public SourcePosition position() { |
duke@1 | 1327 | if (tsym.sourcefile == null) return null; |
jjg@197 | 1328 | return SourcePositionImpl.make(tsym.sourcefile, |
duke@1 | 1329 | (tree==null) ? Position.NOPOS : tree.pos, |
duke@1 | 1330 | lineMap); |
duke@1 | 1331 | } |
duke@1 | 1332 | } |