langtools/src/share/classes/com/sun/tools/javadoc/ProgramElementDocImpl.java
author jjg
Tue, 11 Dec 2012 15:05:55 -0800
changeset 14802 969e90f68ac5
parent 14260 727a84636f12
child 14961 e731935052af
permissions -rw-r--r--
8004828: refactor init of *DocImpl classes Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 5520
diff changeset
     2
 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
10
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
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
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
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    23
 * questions.
10
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
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 5520
diff changeset
    28
import java.lang.reflect.Modifier;
8d2148961366 8000663: clean up langtools imports
jjg
parents: 5520
diff changeset
    29
import java.text.CollationKey;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 5520
diff changeset
    31
import com.sun.javadoc.*;
14802
969e90f68ac5 8004828: refactor init of *DocImpl classes
jjg
parents: 14260
diff changeset
    32
import com.sun.source.util.TreePath;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import com.sun.tools.javac.code.Attribute;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import com.sun.tools.javac.code.Symbol;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import com.sun.tools.javac.code.Symbol.ClassSymbol;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import com.sun.tools.javac.tree.JCTree;
14802
969e90f68ac5 8004828: refactor init of *DocImpl classes
jjg
parents: 14260
diff changeset
    37
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import com.sun.tools.javac.util.Position;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 * Represents a java program element: class, interface, field,
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 * constructor, or method.
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 * This is an abstract class dealing with information common to
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 * these elements.
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
 *
14260
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    46
 *  <p><b>This is NOT part of any supported API.
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    47
 *  If you write code that depends on this, you do so at your own risk.
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    48
 *  This code and its internal interfaces are subject to change or
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    49
 *  deletion without notice.</b>
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    50
 *
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
 * @see MemberDocImpl
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
 * @see ClassDocImpl
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
 * @author Robert Field
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
 * @author Neal Gafter (rewrite)
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
 * @author Scott Seligman (generics, enums, annotations)
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
public abstract class ProgramElementDocImpl
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
        extends DocImpl implements ProgramElementDoc {
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
    private final Symbol sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
    // For source position information.
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
    JCTree tree = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    Position.LineMap lineMap = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
    // Cache for getModifiers().
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    private int modifiers = -1;
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
14802
969e90f68ac5 8004828: refactor init of *DocImpl classes
jjg
parents: 14260
diff changeset
    71
    protected ProgramElementDocImpl(DocEnv env, Symbol sym, TreePath treePath) {
969e90f68ac5 8004828: refactor init of *DocImpl classes
jjg
parents: 14260
diff changeset
    72
        super(env, treePath);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
        this.sym = sym;
14802
969e90f68ac5 8004828: refactor init of *DocImpl classes
jjg
parents: 14260
diff changeset
    74
        if (treePath != null) {
969e90f68ac5 8004828: refactor init of *DocImpl classes
jjg
parents: 14260
diff changeset
    75
            tree = (JCTree) treePath.getLeaf();
969e90f68ac5 8004828: refactor init of *DocImpl classes
jjg
parents: 14260
diff changeset
    76
            lineMap = ((JCCompilationUnit) treePath.getCompilationUnit()).lineMap;
969e90f68ac5 8004828: refactor init of *DocImpl classes
jjg
parents: 14260
diff changeset
    77
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
14802
969e90f68ac5 8004828: refactor init of *DocImpl classes
jjg
parents: 14260
diff changeset
    80
    @Override
969e90f68ac5 8004828: refactor init of *DocImpl classes
jjg
parents: 14260
diff changeset
    81
    void setTreePath(TreePath treePath) {
969e90f68ac5 8004828: refactor init of *DocImpl classes
jjg
parents: 14260
diff changeset
    82
        super.setTreePath(treePath);
969e90f68ac5 8004828: refactor init of *DocImpl classes
jjg
parents: 14260
diff changeset
    83
        this.tree = (JCTree) treePath.getLeaf();
969e90f68ac5 8004828: refactor init of *DocImpl classes
jjg
parents: 14260
diff changeset
    84
        this.lineMap = ((JCCompilationUnit) treePath.getCompilationUnit()).lineMap;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
     * Subclasses override to identify the containing class
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
    protected abstract ClassSymbol getContainingClass();
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
     * Returns the flags in terms of javac's flags
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
    abstract protected long getFlags();
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
     * Returns the modifier flags in terms of java.lang.reflect.Modifier.
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
    protected int getModifiers() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
        if (modifiers == -1) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
            modifiers = DocEnv.translateModifiers(getFlags());
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
        return modifiers;
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
     * Get the containing class of this program element.
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
     * @return a ClassDocImpl for this element's containing class.
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
     * If this is a class with no outer class, return null.
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
    public ClassDoc containingClass() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
        if (getContainingClass() == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
        return env.getClassDoc(getContainingClass());
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
     * Return the package that this member is contained in.
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
     * Return "" if in unnamed package.
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
    public PackageDoc containingPackage() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
        return env.getPackageDoc(getContainingClass().packge());
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
     * Get the modifier specifier integer.
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
     * @see java.lang.reflect.Modifier
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
    public int modifierSpecifier() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
        int modifiers = getModifiers();
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
        if (isMethod() && containingClass().isInterface())
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
            // Remove the implicit abstract modifier.
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
            return modifiers & ~Modifier.ABSTRACT;
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
        return modifiers;
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
     * Get modifiers string.
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
     * <pre>
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
     * Example, for:
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
     *   public abstract int foo() { ... }
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
     * modifiers() would return:
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
     *   'public abstract'
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
     * </pre>
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
     * Annotations are not included.
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
    public String modifiers() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
        int modifiers = getModifiers();
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
        if (isAnnotationTypeElement() ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
                (isMethod() && containingClass().isInterface())) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
            // Remove the implicit abstract modifier.
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
            return Modifier.toString(modifiers & ~Modifier.ABSTRACT);
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
            return Modifier.toString(modifiers);
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
     * Get the annotations of this program element.
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
     * Return an empty array if there are none.
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
    public AnnotationDesc[] annotations() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
        AnnotationDesc res[] = new AnnotationDesc[sym.getAnnotationMirrors().length()];
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
        int i = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
        for (Attribute.Compound a : sym.getAnnotationMirrors()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
            res[i++] = new AnnotationDescImpl(env, a);
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
        return res;
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
     * Return true if this program element is public
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
    public boolean isPublic() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
        int modifiers = getModifiers();
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
        return Modifier.isPublic(modifiers);
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
     * Return true if this program element is protected
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
    public boolean isProtected() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
        int modifiers = getModifiers();
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
        return Modifier.isProtected(modifiers);
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
     * Return true if this program element is private
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
    public boolean isPrivate() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
        int modifiers = getModifiers();
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
        return Modifier.isPrivate(modifiers);
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 package private
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
    public boolean isPackagePrivate() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
        return !(isPublic() || isPrivate() || isProtected());
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
     * Return true if this program element is static
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
    public boolean isStatic() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
        int modifiers = getModifiers();
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
        return Modifier.isStatic(modifiers);
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
     * Return true if this program element is final
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
    public boolean isFinal() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
        int modifiers = getModifiers();
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
        return Modifier.isFinal(modifiers);
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
     * Generate a key for sorting.
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
    CollationKey generateKey() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
        String k = name();
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
        // System.out.println("COLLATION KEY FOR " + this + " is \"" + k + "\"");
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
        return env.doclocale.collator.getCollationKey(k);
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
}