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