langtools/src/jdk.javadoc/share/classes/com/sun/tools/javadoc/main/AnnotationDescImpl.java
author jjg
Fri, 27 May 2016 13:06:58 -0700
changeset 38617 d93a7f64e231
parent 37938 42baa89d2156
permissions -rw-r--r--
8157606: deprecate com.sun.javadoc API Reviewed-by: ksrini
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
22448
a85fbad9d687 8029230: Update copyright year to match last edit in jdk8 langtools repository for 2013
ksrini
parents: 15035
diff changeset
     2
 * Copyright (c) 2003, 2013, 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
38617
d93a7f64e231 8157606: deprecate com.sun.javadoc API
jjg
parents: 37938
diff changeset
    51
@Deprecated
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
public class AnnotationDescImpl implements AnnotationDesc {
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
    private final DocEnv env;
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
    private final Attribute.Compound annotation;
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    AnnotationDescImpl(DocEnv env, Attribute.Compound annotation) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
        this.env = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
        this.annotation = annotation;
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
     * Returns the annotation type of this annotation.
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
    public AnnotationTypeDoc annotationType() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
        ClassSymbol atsym = (ClassSymbol)annotation.type.tsym;
3892
7f888c00fad7 6709246: ClassCastException in javadoc
jjg
parents: 10
diff changeset
    68
        if (annotation.type.isErroneous()) {
7f888c00fad7 6709246: ClassCastException in javadoc
jjg
parents: 10
diff changeset
    69
            env.warning(null, "javadoc.class_not_found", annotation.type.toString());
7f888c00fad7 6709246: ClassCastException in javadoc
jjg
parents: 10
diff changeset
    70
            return new AnnotationTypeDocImpl(env, atsym);
7f888c00fad7 6709246: ClassCastException in javadoc
jjg
parents: 10
diff changeset
    71
        } else {
7f888c00fad7 6709246: ClassCastException in javadoc
jjg
parents: 10
diff changeset
    72
            return (AnnotationTypeDoc)env.getClassDoc(atsym);
7f888c00fad7 6709246: ClassCastException in javadoc
jjg
parents: 10
diff changeset
    73
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
     * Returns this annotation's elements and their values.
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
     * Only those explicitly present in the annotation are
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
     * included, not those assuming their default values.
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
     * Returns an empty array if there are none.
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
    public ElementValuePair[] elementValues() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
        List<Pair<MethodSymbol,Attribute>> vals = annotation.values;
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
        ElementValuePair res[] = new ElementValuePair[vals.length()];
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
        int i = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
        for (Pair<MethodSymbol,Attribute> val : vals) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
            res[i++] = new ElementValuePairImpl(env, val.fst, val.snd);
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
        return res;
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
    /**
15035
d03c311f44e6 8005092: javadoc should check for synthesized bit on an annotation
bpatel
parents: 14260
diff changeset
    93
     * Check for the synthesized bit on the annotation.
d03c311f44e6 8005092: javadoc should check for synthesized bit on an annotation
bpatel
parents: 14260
diff changeset
    94
     *
d03c311f44e6 8005092: javadoc should check for synthesized bit on an annotation
bpatel
parents: 14260
diff changeset
    95
     * @return true if the annotation is synthesized.
d03c311f44e6 8005092: javadoc should check for synthesized bit on an annotation
bpatel
parents: 14260
diff changeset
    96
     */
d03c311f44e6 8005092: javadoc should check for synthesized bit on an annotation
bpatel
parents: 14260
diff changeset
    97
    public boolean isSynthesized() {
d03c311f44e6 8005092: javadoc should check for synthesized bit on an annotation
bpatel
parents: 14260
diff changeset
    98
        return annotation.isSynthesized();
d03c311f44e6 8005092: javadoc should check for synthesized bit on an annotation
bpatel
parents: 14260
diff changeset
    99
    }
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
    /**
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
     * Returns a string representation of this annotation.
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
     * String is of one of the forms:
31751
ec251536a004 8080880: some docs cleanup for langtools
avstepan
parents: 25874
diff changeset
   104
     * <pre>
ec251536a004 8080880: some docs cleanup for langtools
avstepan
parents: 25874
diff changeset
   105
     *     {@code @com.example.foo(name1=val1, name2=val2)}
ec251536a004 8080880: some docs cleanup for langtools
avstepan
parents: 25874
diff changeset
   106
     *     {@code @com.example.foo(val)}
ec251536a004 8080880: some docs cleanup for langtools
avstepan
parents: 25874
diff changeset
   107
     *     {@code @com.example.foo}
ec251536a004 8080880: some docs cleanup for langtools
avstepan
parents: 25874
diff changeset
   108
     * </pre>
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
     * Omit parens for marker annotations, and omit "value=" when allowed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
     */
8631
664f84942e74 6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents: 5520
diff changeset
   111
    @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
    public String toString() {
8631
664f84942e74 6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents: 5520
diff changeset
   113
        StringBuilder sb = new StringBuilder("@");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
        sb.append(annotation.type.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
        ElementValuePair vals[] = elementValues();
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
        if (vals.length > 0) {          // omit parens for marker annotation
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
            sb.append('(');
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
            boolean first = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
            for (ElementValuePair val : vals) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
                if (!first) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
                    sb.append(", ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
                first = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
                String name = val.element().name();
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
                if (vals.length == 1 && name.equals("value")) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
                    sb.append(val.value());
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
                    sb.append(val);
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
            sb.append(')');
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
        return sb.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
    }
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
     * Represents an association between an annotation type element
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
     * and one of its values.
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
    public static class ElementValuePairImpl implements ElementValuePair {
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
        private final DocEnv env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
        private final MethodSymbol meth;
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
        private final Attribute value;
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
        ElementValuePairImpl(DocEnv env, MethodSymbol meth, Attribute value) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
            this.env = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
            this.meth = meth;
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
            this.value = value;
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
         * Returns the annotation type element.
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
        public AnnotationTypeElementDoc element() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
            return env.getAnnotationTypeElementDoc(meth);
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
         * Returns the value associated with the annotation type element.
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
        public AnnotationValue value() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
            return new AnnotationValueImpl(env, value);
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
         * Returns a string representation of this pair
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
         * of the form "name=value".
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
         */
8631
664f84942e74 6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents: 5520
diff changeset
   173
        @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
        public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
            return meth.name + "=" + value();
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
}