duke@1: /* jjg@1357: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as ohair@554: * published by the Free Software Foundation. Oracle designates this duke@1: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. duke@1: */ duke@1: duke@1: package com.sun.tools.javadoc; duke@1: jjg@1357: import java.lang.reflect.Modifier; jjg@1357: import java.text.CollationKey; jjg@1357: duke@1: import com.sun.javadoc.*; jjg@1443: import com.sun.source.util.TreePath; duke@1: import com.sun.tools.javac.code.Attribute; duke@1: import com.sun.tools.javac.code.Symbol; duke@1: import com.sun.tools.javac.code.Symbol.ClassSymbol; duke@1: import com.sun.tools.javac.tree.JCTree; jjg@1443: import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; duke@1: import com.sun.tools.javac.util.Position; duke@1: duke@1: /** duke@1: * Represents a java program element: class, interface, field, duke@1: * constructor, or method. duke@1: * This is an abstract class dealing with information common to duke@1: * these elements. duke@1: * jjg@1359: *

This is NOT part of any supported API. jjg@1359: * If you write code that depends on this, you do so at your own risk. jjg@1359: * This code and its internal interfaces are subject to change or jjg@1359: * deletion without notice. jjg@1359: * duke@1: * @see MemberDocImpl duke@1: * @see ClassDocImpl duke@1: * duke@1: * @author Robert Field duke@1: * @author Neal Gafter (rewrite) duke@1: * @author Scott Seligman (generics, enums, annotations) duke@1: */ duke@1: public abstract class ProgramElementDocImpl duke@1: extends DocImpl implements ProgramElementDoc { duke@1: duke@1: private final Symbol sym; duke@1: duke@1: // For source position information. duke@1: JCTree tree = null; duke@1: Position.LineMap lineMap = null; duke@1: duke@1: duke@1: // Cache for getModifiers(). duke@1: private int modifiers = -1; duke@1: jjg@1443: protected ProgramElementDocImpl(DocEnv env, Symbol sym, TreePath treePath) { jjg@1443: super(env, treePath); duke@1: this.sym = sym; jjg@1443: if (treePath != null) { jjg@1443: tree = (JCTree) treePath.getLeaf(); jjg@1443: lineMap = ((JCCompilationUnit) treePath.getCompilationUnit()).lineMap; jjg@1443: } duke@1: } duke@1: jjg@1443: @Override jjg@1443: void setTreePath(TreePath treePath) { jjg@1443: super.setTreePath(treePath); jjg@1443: this.tree = (JCTree) treePath.getLeaf(); jjg@1443: this.lineMap = ((JCCompilationUnit) treePath.getCompilationUnit()).lineMap; duke@1: } duke@1: duke@1: /** duke@1: * Subclasses override to identify the containing class duke@1: */ duke@1: protected abstract ClassSymbol getContainingClass(); duke@1: duke@1: /** duke@1: * Returns the flags in terms of javac's flags duke@1: */ duke@1: abstract protected long getFlags(); duke@1: duke@1: /** duke@1: * Returns the modifier flags in terms of java.lang.reflect.Modifier. duke@1: */ duke@1: protected int getModifiers() { duke@1: if (modifiers == -1) { duke@1: modifiers = DocEnv.translateModifiers(getFlags()); duke@1: } duke@1: return modifiers; duke@1: } duke@1: duke@1: /** duke@1: * Get the containing class of this program element. duke@1: * duke@1: * @return a ClassDocImpl for this element's containing class. duke@1: * If this is a class with no outer class, return null. duke@1: */ duke@1: public ClassDoc containingClass() { duke@1: if (getContainingClass() == null) { duke@1: return null; duke@1: } duke@1: return env.getClassDoc(getContainingClass()); duke@1: } duke@1: duke@1: /** duke@1: * Return the package that this member is contained in. duke@1: * Return "" if in unnamed package. duke@1: */ duke@1: public PackageDoc containingPackage() { duke@1: return env.getPackageDoc(getContainingClass().packge()); duke@1: } duke@1: duke@1: /** duke@1: * Get the modifier specifier integer. duke@1: * duke@1: * @see java.lang.reflect.Modifier duke@1: */ duke@1: public int modifierSpecifier() { duke@1: int modifiers = getModifiers(); duke@1: if (isMethod() && containingClass().isInterface()) duke@1: // Remove the implicit abstract modifier. duke@1: return modifiers & ~Modifier.ABSTRACT; duke@1: return modifiers; duke@1: } duke@1: duke@1: /** duke@1: * Get modifiers string. duke@1: *

duke@1:      * Example, for:
duke@1:      *   public abstract int foo() { ... }
duke@1:      * modifiers() would return:
duke@1:      *   'public abstract'
duke@1:      * 
duke@1: * Annotations are not included. duke@1: */ duke@1: public String modifiers() { duke@1: int modifiers = getModifiers(); duke@1: if (isAnnotationTypeElement() || duke@1: (isMethod() && containingClass().isInterface())) { duke@1: // Remove the implicit abstract modifier. duke@1: return Modifier.toString(modifiers & ~Modifier.ABSTRACT); duke@1: } else { duke@1: return Modifier.toString(modifiers); duke@1: } duke@1: } duke@1: duke@1: /** duke@1: * Get the annotations of this program element. duke@1: * Return an empty array if there are none. duke@1: */ duke@1: public AnnotationDesc[] annotations() { jfranck@1464: AnnotationDesc res[] = new AnnotationDesc[sym.getRawAttributes().length()]; duke@1: int i = 0; jfranck@1464: for (Attribute.Compound a : sym.getRawAttributes()) { duke@1: res[i++] = new AnnotationDescImpl(env, a); duke@1: } duke@1: return res; duke@1: } duke@1: duke@1: /** duke@1: * Return true if this program element is public duke@1: */ duke@1: public boolean isPublic() { duke@1: int modifiers = getModifiers(); duke@1: return Modifier.isPublic(modifiers); duke@1: } duke@1: duke@1: /** duke@1: * Return true if this program element is protected duke@1: */ duke@1: public boolean isProtected() { duke@1: int modifiers = getModifiers(); duke@1: return Modifier.isProtected(modifiers); duke@1: } duke@1: duke@1: /** duke@1: * Return true if this program element is private duke@1: */ duke@1: public boolean isPrivate() { duke@1: int modifiers = getModifiers(); duke@1: return Modifier.isPrivate(modifiers); duke@1: } duke@1: duke@1: /** duke@1: * Return true if this program element is package private duke@1: */ duke@1: public boolean isPackagePrivate() { duke@1: return !(isPublic() || isPrivate() || isProtected()); duke@1: } duke@1: duke@1: /** duke@1: * Return true if this program element is static duke@1: */ duke@1: public boolean isStatic() { duke@1: int modifiers = getModifiers(); duke@1: return Modifier.isStatic(modifiers); duke@1: } duke@1: duke@1: /** duke@1: * Return true if this program element is final duke@1: */ duke@1: public boolean isFinal() { duke@1: int modifiers = getModifiers(); duke@1: return Modifier.isFinal(modifiers); duke@1: } duke@1: duke@1: /** duke@1: * Generate a key for sorting. duke@1: */ duke@1: CollationKey generateKey() { duke@1: String k = name(); duke@1: // System.out.println("COLLATION KEY FOR " + this + " is \"" + k + "\""); duke@1: return env.doclocale.collator.getCollationKey(k); duke@1: } duke@1: duke@1: }