src/jdk.javadoc/share/classes/com/sun/tools/javadoc/main/AnnotationDescImpl.java
author ksrini
Fri, 09 Feb 2018 13:58:17 -0800
changeset 48840 5e2d2067da48
parent 47216 71c04702a3d5
permissions -rw-r--r--
8194651: javadoc: mark the com.sun.javadoc API for removal Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
48840
5e2d2067da48 8194651: javadoc: mark the com.sun.javadoc API for removal
ksrini
parents: 47216
diff changeset
     2
 * Copyright (c) 2003, 2018, 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: 3892
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: 3892
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: 3892
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3892
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3892
diff changeset
    23
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
37938
42baa89d2156 8075703: jdk.javadoc module exports com.sun.tools.javadoc package which contains a lot of internal API.
jjg
parents: 31751
diff changeset
    26
package com.sun.tools.javadoc.main;
10
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.Symbol.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import com.sun.tools.javac.util.List;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import com.sun.tools.javac.util.Pair;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
 * Represents an annotation.
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
 * An annotation associates a value with each element of an annotation type.
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
 * Sure it ought to be called "Annotation", but that clashes with
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
 * java.lang.annotation.Annotation.
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 *
14260
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    42
 *  <p><b>This is NOT part of any supported API.
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    43
 *  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
    44
 *  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
    45
 *  deletion without notice.</b>
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    46
 *
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
 * @author Scott Seligman
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
 * @since 1.5
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
48840
5e2d2067da48 8194651: javadoc: mark the com.sun.javadoc API for removal
ksrini
parents: 47216
diff changeset
    51
@Deprecated(since="9", forRemoval=true)
5e2d2067da48 8194651: javadoc: mark the com.sun.javadoc API for removal
ksrini
parents: 47216
diff changeset
    52
@SuppressWarnings("removal")
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
public class AnnotationDescImpl implements AnnotationDesc {
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
    private final DocEnv env;
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
    private final Attribute.Compound annotation;
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
    AnnotationDescImpl(DocEnv env, Attribute.Compound annotation) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
        this.env = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
        this.annotation = annotation;
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
     * Returns the annotation type of this annotation.
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
    public AnnotationTypeDoc annotationType() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
        ClassSymbol atsym = (ClassSymbol)annotation.type.tsym;
3892
7f888c00fad7 6709246: ClassCastException in javadoc
jjg
parents: 10
diff changeset
    69
        if (annotation.type.isErroneous()) {
7f888c00fad7 6709246: ClassCastException in javadoc
jjg
parents: 10
diff changeset
    70
            env.warning(null, "javadoc.class_not_found", annotation.type.toString());
7f888c00fad7 6709246: ClassCastException in javadoc
jjg
parents: 10
diff changeset
    71
            return new AnnotationTypeDocImpl(env, atsym);
7f888c00fad7 6709246: ClassCastException in javadoc
jjg
parents: 10
diff changeset
    72
        } else {
7f888c00fad7 6709246: ClassCastException in javadoc
jjg
parents: 10
diff changeset
    73
            return (AnnotationTypeDoc)env.getClassDoc(atsym);
7f888c00fad7 6709246: ClassCastException in javadoc
jjg
parents: 10
diff changeset
    74
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
     * Returns this annotation's elements and their values.
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
     * Only those explicitly present in the annotation are
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
     * included, not those assuming their default values.
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
     * Returns an empty array if there are none.
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
    public ElementValuePair[] elementValues() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
        List<Pair<MethodSymbol,Attribute>> vals = annotation.values;
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
        ElementValuePair res[] = new ElementValuePair[vals.length()];
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
        int i = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
        for (Pair<MethodSymbol,Attribute> val : vals) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
            res[i++] = new ElementValuePairImpl(env, val.fst, val.snd);
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
        return res;
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
    /**
15035
d03c311f44e6 8005092: javadoc should check for synthesized bit on an annotation
bpatel
parents: 14260
diff changeset
    94
     * Check for the synthesized bit on the annotation.
d03c311f44e6 8005092: javadoc should check for synthesized bit on an annotation
bpatel
parents: 14260
diff changeset
    95
     *
d03c311f44e6 8005092: javadoc should check for synthesized bit on an annotation
bpatel
parents: 14260
diff changeset
    96
     * @return true if the annotation is synthesized.
d03c311f44e6 8005092: javadoc should check for synthesized bit on an annotation
bpatel
parents: 14260
diff changeset
    97
     */
d03c311f44e6 8005092: javadoc should check for synthesized bit on an annotation
bpatel
parents: 14260
diff changeset
    98
    public boolean isSynthesized() {
d03c311f44e6 8005092: javadoc should check for synthesized bit on an annotation
bpatel
parents: 14260
diff changeset
    99
        return annotation.isSynthesized();
d03c311f44e6 8005092: javadoc should check for synthesized bit on an annotation
bpatel
parents: 14260
diff changeset
   100
    }
d03c311f44e6 8005092: javadoc should check for synthesized bit on an annotation
bpatel
parents: 14260
diff changeset
   101
d03c311f44e6 8005092: javadoc should check for synthesized bit on an annotation
bpatel
parents: 14260
diff changeset
   102
    /**
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
     * Returns a string representation of this annotation.
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
     * String is of one of the forms:
31751
ec251536a004 8080880: some docs cleanup for langtools
avstepan
parents: 25874
diff changeset
   105
     * <pre>
ec251536a004 8080880: some docs cleanup for langtools
avstepan
parents: 25874
diff changeset
   106
     *     {@code @com.example.foo(name1=val1, name2=val2)}
ec251536a004 8080880: some docs cleanup for langtools
avstepan
parents: 25874
diff changeset
   107
     *     {@code @com.example.foo(val)}
ec251536a004 8080880: some docs cleanup for langtools
avstepan
parents: 25874
diff changeset
   108
     *     {@code @com.example.foo}
ec251536a004 8080880: some docs cleanup for langtools
avstepan
parents: 25874
diff changeset
   109
     * </pre>
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
     * Omit parens for marker annotations, and omit "value=" when allowed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
     */
8631
664f84942e74 6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents: 5520
diff changeset
   112
    @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
    public String toString() {
8631
664f84942e74 6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents: 5520
diff changeset
   114
        StringBuilder sb = new StringBuilder("@");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
        sb.append(annotation.type.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
        ElementValuePair vals[] = elementValues();
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
        if (vals.length > 0) {          // omit parens for marker annotation
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
            sb.append('(');
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
            boolean first = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
            for (ElementValuePair val : vals) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
                if (!first) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
                    sb.append(", ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
                first = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
                String name = val.element().name();
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
                if (vals.length == 1 && name.equals("value")) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
                    sb.append(val.value());
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
                    sb.append(val);
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
            sb.append(')');
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
        return sb.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
     * Represents an association between an annotation type element
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
     * and one of its values.
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
    public static class ElementValuePairImpl implements ElementValuePair {
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
        private final DocEnv env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
        private final MethodSymbol meth;
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
        private final Attribute value;
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
        ElementValuePairImpl(DocEnv env, MethodSymbol meth, Attribute value) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
            this.env = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
            this.meth = meth;
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
            this.value = value;
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
         * Returns the annotation type element.
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
        public AnnotationTypeElementDoc element() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
            return env.getAnnotationTypeElementDoc(meth);
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
         * Returns the value associated with the annotation type element.
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
        public AnnotationValue value() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
            return new AnnotationValueImpl(env, value);
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
         * Returns a string representation of this pair
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
         * of the form "name=value".
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
         */
8631
664f84942e74 6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents: 5520
diff changeset
   174
        @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
        public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
            return meth.name + "=" + value();
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
}