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