src/share/classes/com/sun/tools/javadoc/ProgramElementDocImpl.java

Tue, 09 Oct 2012 19:10:00 -0700

author
jjg
date
Tue, 09 Oct 2012 19:10:00 -0700
changeset 1357
c75be5bc5283
parent 554
9d9f26857129
child 1359
25e14ad23cef
permissions
-rw-r--r--

8000663: clean up langtools imports
Reviewed-by: darcy

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

mercurial