langtools/src/jdk.javadoc/share/classes/com/sun/tools/javadoc/main/ProgramElementDocImpl.java
changeset 37938 42baa89d2156
parent 25874 83c19f00452c
child 38617 d93a7f64e231
equal deleted inserted replaced
37858:7c04fcb12bd4 37938:42baa89d2156
       
     1 /*
       
     2  * Copyright (c) 1997, 2012, 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 public abstract class ProgramElementDocImpl
       
    59         extends DocImpl implements ProgramElementDoc {
       
    60 
       
    61     private final Symbol sym;
       
    62 
       
    63     // For source position information.
       
    64     JCTree tree = null;
       
    65     Position.LineMap lineMap = null;
       
    66 
       
    67 
       
    68     // Cache for getModifiers().
       
    69     private int modifiers = -1;
       
    70 
       
    71     protected ProgramElementDocImpl(DocEnv env, Symbol sym, TreePath treePath) {
       
    72         super(env, treePath);
       
    73         this.sym = sym;
       
    74         if (treePath != null) {
       
    75             tree = (JCTree) treePath.getLeaf();
       
    76             lineMap = ((JCCompilationUnit) treePath.getCompilationUnit()).lineMap;
       
    77         }
       
    78     }
       
    79 
       
    80     @Override
       
    81     void setTreePath(TreePath treePath) {
       
    82         super.setTreePath(treePath);
       
    83         this.tree = (JCTree) treePath.getLeaf();
       
    84         this.lineMap = ((JCCompilationUnit) treePath.getCompilationUnit()).lineMap;
       
    85     }
       
    86 
       
    87     /**
       
    88      * Subclasses override to identify the containing class
       
    89      */
       
    90     protected abstract ClassSymbol getContainingClass();
       
    91 
       
    92     /**
       
    93      * Returns the flags in terms of javac's flags
       
    94      */
       
    95     abstract protected long getFlags();
       
    96 
       
    97     /**
       
    98      * Returns the modifier flags in terms of java.lang.reflect.Modifier.
       
    99      */
       
   100     protected int getModifiers() {
       
   101         if (modifiers == -1) {
       
   102             modifiers = DocEnv.translateModifiers(getFlags());
       
   103         }
       
   104         return modifiers;
       
   105     }
       
   106 
       
   107     /**
       
   108      * Get the containing class of this program element.
       
   109      *
       
   110      * @return a ClassDocImpl for this element's containing class.
       
   111      * If this is a class with no outer class, return null.
       
   112      */
       
   113     public ClassDoc containingClass() {
       
   114         if (getContainingClass() == null) {
       
   115             return null;
       
   116         }
       
   117         return env.getClassDoc(getContainingClass());
       
   118     }
       
   119 
       
   120     /**
       
   121      * Return the package that this member is contained in.
       
   122      * Return "" if in unnamed package.
       
   123      */
       
   124     public PackageDoc containingPackage() {
       
   125         return env.getPackageDoc(getContainingClass().packge());
       
   126     }
       
   127 
       
   128     /**
       
   129      * Get the modifier specifier integer.
       
   130      *
       
   131      * @see java.lang.reflect.Modifier
       
   132      */
       
   133     public int modifierSpecifier() {
       
   134         int modifiers = getModifiers();
       
   135         if (isMethod() && containingClass().isInterface())
       
   136             // Remove the implicit abstract modifier.
       
   137             return modifiers & ~Modifier.ABSTRACT;
       
   138         return modifiers;
       
   139     }
       
   140 
       
   141     /**
       
   142      * Get modifiers string.
       
   143      * <pre>
       
   144      * Example, for:
       
   145      *   public abstract int foo() { ... }
       
   146      * modifiers() would return:
       
   147      *   'public abstract'
       
   148      * </pre>
       
   149      * Annotations are not included.
       
   150      */
       
   151     public String modifiers() {
       
   152         int modifiers = getModifiers();
       
   153         if (isAnnotationTypeElement() ||
       
   154                 (isMethod() && containingClass().isInterface())) {
       
   155             // Remove the implicit abstract modifier.
       
   156             return Modifier.toString(modifiers & ~Modifier.ABSTRACT);
       
   157         } else {
       
   158             return Modifier.toString(modifiers);
       
   159         }
       
   160     }
       
   161 
       
   162     /**
       
   163      * Get the annotations of this program element.
       
   164      * Return an empty array if there are none.
       
   165      */
       
   166     public AnnotationDesc[] annotations() {
       
   167         AnnotationDesc res[] = new AnnotationDesc[sym.getRawAttributes().length()];
       
   168         int i = 0;
       
   169         for (Attribute.Compound a : sym.getRawAttributes()) {
       
   170             res[i++] = new AnnotationDescImpl(env, a);
       
   171         }
       
   172         return res;
       
   173     }
       
   174 
       
   175     /**
       
   176      * Return true if this program element is public
       
   177      */
       
   178     public boolean isPublic() {
       
   179         int modifiers = getModifiers();
       
   180         return Modifier.isPublic(modifiers);
       
   181     }
       
   182 
       
   183     /**
       
   184      * Return true if this program element is protected
       
   185      */
       
   186     public boolean isProtected() {
       
   187         int modifiers = getModifiers();
       
   188         return Modifier.isProtected(modifiers);
       
   189     }
       
   190 
       
   191     /**
       
   192      * Return true if this program element is private
       
   193      */
       
   194     public boolean isPrivate() {
       
   195         int modifiers = getModifiers();
       
   196         return Modifier.isPrivate(modifiers);
       
   197     }
       
   198 
       
   199     /**
       
   200      * Return true if this program element is package private
       
   201      */
       
   202     public boolean isPackagePrivate() {
       
   203         return !(isPublic() || isPrivate() || isProtected());
       
   204     }
       
   205 
       
   206     /**
       
   207      * Return true if this program element is static
       
   208      */
       
   209     public boolean isStatic() {
       
   210         int modifiers = getModifiers();
       
   211         return Modifier.isStatic(modifiers);
       
   212     }
       
   213 
       
   214     /**
       
   215      * Return true if this program element is final
       
   216      */
       
   217     public boolean isFinal() {
       
   218         int modifiers = getModifiers();
       
   219         return Modifier.isFinal(modifiers);
       
   220     }
       
   221 
       
   222     /**
       
   223      * Generate a key for sorting.
       
   224      */
       
   225     CollationKey generateKey() {
       
   226         String k = name();
       
   227         // System.out.println("COLLATION KEY FOR " + this + " is \"" + k + "\"");
       
   228         return env.doclocale.collator.getCollationKey(k);
       
   229     }
       
   230 
       
   231 }