Thu, 18 Apr 2013 20:00:14 -0700
8012656: cache frequently used name strings for DocImpl classes
Reviewed-by: darcy
duke@1 | 1 | /* |
jjg@1357 | 2 | * Copyright (c) 1997, 2012, 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@1357 | 28 | import java.lang.reflect.Modifier; |
jjg@1357 | 29 | import java.text.CollationKey; |
jjg@1357 | 30 | |
duke@1 | 31 | import com.sun.javadoc.*; |
jjg@1443 | 32 | import com.sun.source.util.TreePath; |
duke@1 | 33 | import com.sun.tools.javac.code.Attribute; |
duke@1 | 34 | import com.sun.tools.javac.code.Symbol; |
duke@1 | 35 | import com.sun.tools.javac.code.Symbol.ClassSymbol; |
duke@1 | 36 | import com.sun.tools.javac.tree.JCTree; |
jjg@1443 | 37 | import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; |
duke@1 | 38 | import com.sun.tools.javac.util.Position; |
duke@1 | 39 | |
duke@1 | 40 | /** |
duke@1 | 41 | * Represents a java program element: class, interface, field, |
duke@1 | 42 | * constructor, or method. |
duke@1 | 43 | * This is an abstract class dealing with information common to |
duke@1 | 44 | * these elements. |
duke@1 | 45 | * |
jjg@1359 | 46 | * <p><b>This is NOT part of any supported API. |
jjg@1359 | 47 | * If you write code that depends on this, you do so at your own risk. |
jjg@1359 | 48 | * This code and its internal interfaces are subject to change or |
jjg@1359 | 49 | * deletion without notice.</b> |
jjg@1359 | 50 | * |
duke@1 | 51 | * @see MemberDocImpl |
duke@1 | 52 | * @see ClassDocImpl |
duke@1 | 53 | * |
duke@1 | 54 | * @author Robert Field |
duke@1 | 55 | * @author Neal Gafter (rewrite) |
duke@1 | 56 | * @author Scott Seligman (generics, enums, annotations) |
duke@1 | 57 | */ |
duke@1 | 58 | public abstract class ProgramElementDocImpl |
duke@1 | 59 | extends DocImpl implements ProgramElementDoc { |
duke@1 | 60 | |
duke@1 | 61 | private final Symbol sym; |
duke@1 | 62 | |
duke@1 | 63 | // For source position information. |
duke@1 | 64 | JCTree tree = null; |
duke@1 | 65 | Position.LineMap lineMap = null; |
duke@1 | 66 | |
duke@1 | 67 | |
duke@1 | 68 | // Cache for getModifiers(). |
duke@1 | 69 | private int modifiers = -1; |
duke@1 | 70 | |
jjg@1443 | 71 | protected ProgramElementDocImpl(DocEnv env, Symbol sym, TreePath treePath) { |
jjg@1443 | 72 | super(env, treePath); |
duke@1 | 73 | this.sym = sym; |
jjg@1443 | 74 | if (treePath != null) { |
jjg@1443 | 75 | tree = (JCTree) treePath.getLeaf(); |
jjg@1443 | 76 | lineMap = ((JCCompilationUnit) treePath.getCompilationUnit()).lineMap; |
jjg@1443 | 77 | } |
duke@1 | 78 | } |
duke@1 | 79 | |
jjg@1443 | 80 | @Override |
jjg@1443 | 81 | void setTreePath(TreePath treePath) { |
jjg@1443 | 82 | super.setTreePath(treePath); |
jjg@1443 | 83 | this.tree = (JCTree) treePath.getLeaf(); |
jjg@1443 | 84 | this.lineMap = ((JCCompilationUnit) treePath.getCompilationUnit()).lineMap; |
duke@1 | 85 | } |
duke@1 | 86 | |
duke@1 | 87 | /** |
duke@1 | 88 | * Subclasses override to identify the containing class |
duke@1 | 89 | */ |
duke@1 | 90 | protected abstract ClassSymbol getContainingClass(); |
duke@1 | 91 | |
duke@1 | 92 | /** |
duke@1 | 93 | * Returns the flags in terms of javac's flags |
duke@1 | 94 | */ |
duke@1 | 95 | abstract protected long getFlags(); |
duke@1 | 96 | |
duke@1 | 97 | /** |
duke@1 | 98 | * Returns the modifier flags in terms of java.lang.reflect.Modifier. |
duke@1 | 99 | */ |
duke@1 | 100 | protected int getModifiers() { |
duke@1 | 101 | if (modifiers == -1) { |
duke@1 | 102 | modifiers = DocEnv.translateModifiers(getFlags()); |
duke@1 | 103 | } |
duke@1 | 104 | return modifiers; |
duke@1 | 105 | } |
duke@1 | 106 | |
duke@1 | 107 | /** |
duke@1 | 108 | * Get the containing class of this program element. |
duke@1 | 109 | * |
duke@1 | 110 | * @return a ClassDocImpl for this element's containing class. |
duke@1 | 111 | * If this is a class with no outer class, return null. |
duke@1 | 112 | */ |
duke@1 | 113 | public ClassDoc containingClass() { |
duke@1 | 114 | if (getContainingClass() == null) { |
duke@1 | 115 | return null; |
duke@1 | 116 | } |
duke@1 | 117 | return env.getClassDoc(getContainingClass()); |
duke@1 | 118 | } |
duke@1 | 119 | |
duke@1 | 120 | /** |
duke@1 | 121 | * Return the package that this member is contained in. |
duke@1 | 122 | * Return "" if in unnamed package. |
duke@1 | 123 | */ |
duke@1 | 124 | public PackageDoc containingPackage() { |
duke@1 | 125 | return env.getPackageDoc(getContainingClass().packge()); |
duke@1 | 126 | } |
duke@1 | 127 | |
duke@1 | 128 | /** |
duke@1 | 129 | * Get the modifier specifier integer. |
duke@1 | 130 | * |
duke@1 | 131 | * @see java.lang.reflect.Modifier |
duke@1 | 132 | */ |
duke@1 | 133 | public int modifierSpecifier() { |
duke@1 | 134 | int modifiers = getModifiers(); |
duke@1 | 135 | if (isMethod() && containingClass().isInterface()) |
duke@1 | 136 | // Remove the implicit abstract modifier. |
duke@1 | 137 | return modifiers & ~Modifier.ABSTRACT; |
duke@1 | 138 | return modifiers; |
duke@1 | 139 | } |
duke@1 | 140 | |
duke@1 | 141 | /** |
duke@1 | 142 | * Get modifiers string. |
duke@1 | 143 | * <pre> |
duke@1 | 144 | * Example, for: |
duke@1 | 145 | * public abstract int foo() { ... } |
duke@1 | 146 | * modifiers() would return: |
duke@1 | 147 | * 'public abstract' |
duke@1 | 148 | * </pre> |
duke@1 | 149 | * Annotations are not included. |
duke@1 | 150 | */ |
duke@1 | 151 | public String modifiers() { |
duke@1 | 152 | int modifiers = getModifiers(); |
duke@1 | 153 | if (isAnnotationTypeElement() || |
duke@1 | 154 | (isMethod() && containingClass().isInterface())) { |
duke@1 | 155 | // Remove the implicit abstract modifier. |
duke@1 | 156 | return Modifier.toString(modifiers & ~Modifier.ABSTRACT); |
duke@1 | 157 | } else { |
duke@1 | 158 | return Modifier.toString(modifiers); |
duke@1 | 159 | } |
duke@1 | 160 | } |
duke@1 | 161 | |
duke@1 | 162 | /** |
duke@1 | 163 | * Get the annotations of this program element. |
duke@1 | 164 | * Return an empty array if there are none. |
duke@1 | 165 | */ |
duke@1 | 166 | public AnnotationDesc[] annotations() { |
jfranck@1464 | 167 | AnnotationDesc res[] = new AnnotationDesc[sym.getRawAttributes().length()]; |
duke@1 | 168 | int i = 0; |
jfranck@1464 | 169 | for (Attribute.Compound a : sym.getRawAttributes()) { |
duke@1 | 170 | res[i++] = new AnnotationDescImpl(env, a); |
duke@1 | 171 | } |
duke@1 | 172 | return res; |
duke@1 | 173 | } |
duke@1 | 174 | |
duke@1 | 175 | /** |
duke@1 | 176 | * Return true if this program element is public |
duke@1 | 177 | */ |
duke@1 | 178 | public boolean isPublic() { |
duke@1 | 179 | int modifiers = getModifiers(); |
duke@1 | 180 | return Modifier.isPublic(modifiers); |
duke@1 | 181 | } |
duke@1 | 182 | |
duke@1 | 183 | /** |
duke@1 | 184 | * Return true if this program element is protected |
duke@1 | 185 | */ |
duke@1 | 186 | public boolean isProtected() { |
duke@1 | 187 | int modifiers = getModifiers(); |
duke@1 | 188 | return Modifier.isProtected(modifiers); |
duke@1 | 189 | } |
duke@1 | 190 | |
duke@1 | 191 | /** |
duke@1 | 192 | * Return true if this program element is private |
duke@1 | 193 | */ |
duke@1 | 194 | public boolean isPrivate() { |
duke@1 | 195 | int modifiers = getModifiers(); |
duke@1 | 196 | return Modifier.isPrivate(modifiers); |
duke@1 | 197 | } |
duke@1 | 198 | |
duke@1 | 199 | /** |
duke@1 | 200 | * Return true if this program element is package private |
duke@1 | 201 | */ |
duke@1 | 202 | public boolean isPackagePrivate() { |
duke@1 | 203 | return !(isPublic() || isPrivate() || isProtected()); |
duke@1 | 204 | } |
duke@1 | 205 | |
duke@1 | 206 | /** |
duke@1 | 207 | * Return true if this program element is static |
duke@1 | 208 | */ |
duke@1 | 209 | public boolean isStatic() { |
duke@1 | 210 | int modifiers = getModifiers(); |
duke@1 | 211 | return Modifier.isStatic(modifiers); |
duke@1 | 212 | } |
duke@1 | 213 | |
duke@1 | 214 | /** |
duke@1 | 215 | * Return true if this program element is final |
duke@1 | 216 | */ |
duke@1 | 217 | public boolean isFinal() { |
duke@1 | 218 | int modifiers = getModifiers(); |
duke@1 | 219 | return Modifier.isFinal(modifiers); |
duke@1 | 220 | } |
duke@1 | 221 | |
duke@1 | 222 | /** |
duke@1 | 223 | * Generate a key for sorting. |
duke@1 | 224 | */ |
duke@1 | 225 | CollationKey generateKey() { |
duke@1 | 226 | String k = name(); |
duke@1 | 227 | // System.out.println("COLLATION KEY FOR " + this + " is \"" + k + "\""); |
duke@1 | 228 | return env.doclocale.collator.getCollationKey(k); |
duke@1 | 229 | } |
duke@1 | 230 | |
duke@1 | 231 | } |