langtools/src/share/classes/com/sun/tools/javadoc/ProgramElementDocImpl.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 10 06bc494ca11e
child 5520 86e4b9a9da40
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2006 Sun Microsystems, Inc.  All Rights Reserved.
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
 * have any questions.
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.javadoc;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import com.sun.javadoc.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import com.sun.tools.javac.code.Attribute;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import com.sun.tools.javac.code.Flags;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import com.sun.tools.javac.code.Symbol;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import com.sun.tools.javac.code.Symbol.ClassSymbol;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import com.sun.tools.javac.tree.JCTree;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import com.sun.tools.javac.util.Position;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import java.lang.reflect.Modifier;
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import java.text.CollationKey;
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 * Represents a java program element: class, interface, field,
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 * constructor, or method.
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
 * This is an abstract class dealing with information common to
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
 * these elements.
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
 * @see MemberDocImpl
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
 * @see ClassDocImpl
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
 * @author Robert Field
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
 * @author Neal Gafter (rewrite)
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
 * @author Scott Seligman (generics, enums, annotations)
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
public abstract class ProgramElementDocImpl
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
        extends DocImpl implements ProgramElementDoc {
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    private final Symbol sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    // For source position information.
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
    JCTree tree = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    Position.LineMap lineMap = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    // Cache for getModifiers().
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
    private int modifiers = -1;
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
    protected ProgramElementDocImpl(DocEnv env, Symbol sym,
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
                                    String doc, JCTree tree, Position.LineMap lineMap) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
        super(env, doc);
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
        this.sym = sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
        this.tree = tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
        this.lineMap = lineMap;
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
    void setTree(JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
        this.tree = tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
     * Subclasses override to identify the containing class
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
    protected abstract ClassSymbol getContainingClass();
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
     * Returns the flags in terms of javac's flags
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
    abstract protected long getFlags();
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
     * Returns the modifier flags in terms of java.lang.reflect.Modifier.
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
    protected int getModifiers() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
        if (modifiers == -1) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
            modifiers = DocEnv.translateModifiers(getFlags());
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
        return modifiers;
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
     * Get the containing class of this program element.
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
     * @return a ClassDocImpl for this element's containing class.
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
     * If this is a class with no outer class, return null.
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
    public ClassDoc containingClass() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
        if (getContainingClass() == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
        return env.getClassDoc(getContainingClass());
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
     * Return the package that this member is contained in.
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
     * Return "" if in unnamed package.
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
    public PackageDoc containingPackage() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
        return env.getPackageDoc(getContainingClass().packge());
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
     * Get the modifier specifier integer.
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
     * @see java.lang.reflect.Modifier
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
    public int modifierSpecifier() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
        int modifiers = getModifiers();
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
        if (isMethod() && containingClass().isInterface())
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
            // Remove the implicit abstract modifier.
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
            return modifiers & ~Modifier.ABSTRACT;
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
        return modifiers;
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
     * Get modifiers string.
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
     * <pre>
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
     * Example, for:
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
     *   public abstract int foo() { ... }
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
     * modifiers() would return:
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
     *   'public abstract'
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
     * </pre>
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
     * Annotations are not included.
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
    public String modifiers() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
        int modifiers = getModifiers();
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
        if (isAnnotationTypeElement() ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
                (isMethod() && containingClass().isInterface())) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
            // Remove the implicit abstract modifier.
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
            return Modifier.toString(modifiers & ~Modifier.ABSTRACT);
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
            return Modifier.toString(modifiers);
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
     * Get the annotations of this program element.
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
     * Return an empty array if there are none.
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
    public AnnotationDesc[] annotations() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
        AnnotationDesc res[] = new AnnotationDesc[sym.getAnnotationMirrors().length()];
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
        int i = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
        for (Attribute.Compound a : sym.getAnnotationMirrors()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
            res[i++] = new AnnotationDescImpl(env, a);
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
        return res;
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
     * Return true if this program element is public
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
    public boolean isPublic() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
        int modifiers = getModifiers();
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
        return Modifier.isPublic(modifiers);
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
     * Return true if this program element is protected
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
    public boolean isProtected() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
        int modifiers = getModifiers();
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
        return Modifier.isProtected(modifiers);
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
     * Return true if this program element is private
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
    public boolean isPrivate() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
        int modifiers = getModifiers();
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
        return Modifier.isPrivate(modifiers);
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
     * Return true if this program element is package private
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
    public boolean isPackagePrivate() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
        return !(isPublic() || isPrivate() || isProtected());
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
     * Return true if this program element is static
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
    public boolean isStatic() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
        int modifiers = getModifiers();
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
        return Modifier.isStatic(modifiers);
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
     * Return true if this program element is final
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
    public boolean isFinal() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
        int modifiers = getModifiers();
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
        return Modifier.isFinal(modifiers);
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
     * Generate a key for sorting.
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
    CollationKey generateKey() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
        String k = name();
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
        // System.out.println("COLLATION KEY FOR " + this + " is \"" + k + "\"");
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
        return env.doclocale.collator.getCollationKey(k);
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
}