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

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

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