Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/src/share/classes/com/sun/tools/javadoc/ProgramElementDocImpl.java
38899 views
/*1* Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.tools.javadoc;2627import java.lang.reflect.Modifier;28import java.text.CollationKey;2930import com.sun.javadoc.*;31import com.sun.source.util.TreePath;32import com.sun.tools.javac.code.Attribute;33import com.sun.tools.javac.code.Symbol;34import com.sun.tools.javac.code.Symbol.ClassSymbol;35import com.sun.tools.javac.tree.JCTree;36import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;37import com.sun.tools.javac.util.Position;3839/**40* Represents a java program element: class, interface, field,41* constructor, or method.42* This is an abstract class dealing with information common to43* these elements.44*45* <p><b>This is NOT part of any supported API.46* If you write code that depends on this, you do so at your own risk.47* This code and its internal interfaces are subject to change or48* deletion without notice.</b>49*50* @see MemberDocImpl51* @see ClassDocImpl52*53* @author Robert Field54* @author Neal Gafter (rewrite)55* @author Scott Seligman (generics, enums, annotations)56*/57public abstract class ProgramElementDocImpl58extends DocImpl implements ProgramElementDoc {5960private final Symbol sym;6162// For source position information.63JCTree tree = null;64Position.LineMap lineMap = null;656667// Cache for getModifiers().68private int modifiers = -1;6970protected ProgramElementDocImpl(DocEnv env, Symbol sym, TreePath treePath) {71super(env, treePath);72this.sym = sym;73if (treePath != null) {74tree = (JCTree) treePath.getLeaf();75lineMap = ((JCCompilationUnit) treePath.getCompilationUnit()).lineMap;76}77}7879@Override80void setTreePath(TreePath treePath) {81super.setTreePath(treePath);82this.tree = (JCTree) treePath.getLeaf();83this.lineMap = ((JCCompilationUnit) treePath.getCompilationUnit()).lineMap;84}8586/**87* Subclasses override to identify the containing class88*/89protected abstract ClassSymbol getContainingClass();9091/**92* Returns the flags in terms of javac's flags93*/94abstract protected long getFlags();9596/**97* Returns the modifier flags in terms of java.lang.reflect.Modifier.98*/99protected int getModifiers() {100if (modifiers == -1) {101modifiers = DocEnv.translateModifiers(getFlags());102}103return modifiers;104}105106/**107* Get the containing class of this program element.108*109* @return a ClassDocImpl for this element's containing class.110* If this is a class with no outer class, return null.111*/112public ClassDoc containingClass() {113if (getContainingClass() == null) {114return null;115}116return env.getClassDoc(getContainingClass());117}118119/**120* Return the package that this member is contained in.121* Return "" if in unnamed package.122*/123public PackageDoc containingPackage() {124return env.getPackageDoc(getContainingClass().packge());125}126127/**128* Get the modifier specifier integer.129*130* @see java.lang.reflect.Modifier131*/132public int modifierSpecifier() {133int modifiers = getModifiers();134if (isMethod() && containingClass().isInterface())135// Remove the implicit abstract modifier.136return modifiers & ~Modifier.ABSTRACT;137return modifiers;138}139140/**141* Get modifiers string.142* <pre>143* Example, for:144* public abstract int foo() { ... }145* modifiers() would return:146* 'public abstract'147* </pre>148* Annotations are not included.149*/150public String modifiers() {151int modifiers = getModifiers();152if (isAnnotationTypeElement() ||153(isMethod() && containingClass().isInterface())) {154// Remove the implicit abstract modifier.155return Modifier.toString(modifiers & ~Modifier.ABSTRACT);156} else {157return Modifier.toString(modifiers);158}159}160161/**162* Get the annotations of this program element.163* Return an empty array if there are none.164*/165public AnnotationDesc[] annotations() {166AnnotationDesc res[] = new AnnotationDesc[sym.getRawAttributes().length()];167int i = 0;168for (Attribute.Compound a : sym.getRawAttributes()) {169res[i++] = new AnnotationDescImpl(env, a);170}171return res;172}173174/**175* Return true if this program element is public176*/177public boolean isPublic() {178int modifiers = getModifiers();179return Modifier.isPublic(modifiers);180}181182/**183* Return true if this program element is protected184*/185public boolean isProtected() {186int modifiers = getModifiers();187return Modifier.isProtected(modifiers);188}189190/**191* Return true if this program element is private192*/193public boolean isPrivate() {194int modifiers = getModifiers();195return Modifier.isPrivate(modifiers);196}197198/**199* Return true if this program element is package private200*/201public boolean isPackagePrivate() {202return !(isPublic() || isPrivate() || isProtected());203}204205/**206* Return true if this program element is static207*/208public boolean isStatic() {209int modifiers = getModifiers();210return Modifier.isStatic(modifiers);211}212213/**214* Return true if this program element is final215*/216public boolean isFinal() {217int modifiers = getModifiers();218return Modifier.isFinal(modifiers);219}220221/**222* Generate a key for sorting.223*/224CollationKey generateKey() {225String k = name();226// System.out.println("COLLATION KEY FOR " + this + " is \"" + k + "\"");227return env.doclocale.collator.getCollationKey(k);228}229230}231232233