langtools/src/share/classes/com/sun/tools/javadoc/AnnotationDescImpl.java
author bpatel
Sat, 29 Mar 2014 11:06:33 -0700
changeset 23799 daa645653200
parent 22448 a85fbad9d687
permissions -rw-r--r--
8029143: javadoc standard doclet should add Functional Interface blurb when @FunctionalInterface annotation is present 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
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.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
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
public class AnnotationDescImpl implements AnnotationDesc {
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
    private final DocEnv env;
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
    private final Attribute.Compound annotation;
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
    AnnotationDescImpl(DocEnv env, Attribute.Compound annotation) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
        this.env = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
        this.annotation = annotation;
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
     * Returns the annotation type of this annotation.
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    public AnnotationTypeDoc annotationType() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
        ClassSymbol atsym = (ClassSymbol)annotation.type.tsym;
3892
7f888c00fad7 6709246: ClassCastException in javadoc
jjg
parents: 10
diff changeset
    67
        if (annotation.type.isErroneous()) {
7f888c00fad7 6709246: ClassCastException in javadoc
jjg
parents: 10
diff changeset
    68
            env.warning(null, "javadoc.class_not_found", annotation.type.toString());
7f888c00fad7 6709246: ClassCastException in javadoc
jjg
parents: 10
diff changeset
    69
            return new AnnotationTypeDocImpl(env, atsym);
7f888c00fad7 6709246: ClassCastException in javadoc
jjg
parents: 10
diff changeset
    70
        } else {
7f888c00fad7 6709246: ClassCastException in javadoc
jjg
parents: 10
diff changeset
    71
            return (AnnotationTypeDoc)env.getClassDoc(atsym);
7f888c00fad7 6709246: ClassCastException in javadoc
jjg
parents: 10
diff changeset
    72
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
     * Returns this annotation's elements and their values.
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
     * Only those explicitly present in the annotation are
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
     * included, not those assuming their default values.
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
     * Returns an empty array if there are none.
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
    public ElementValuePair[] elementValues() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
        List<Pair<MethodSymbol,Attribute>> vals = annotation.values;
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
        ElementValuePair res[] = new ElementValuePair[vals.length()];
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
        int i = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
        for (Pair<MethodSymbol,Attribute> val : vals) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
            res[i++] = new ElementValuePairImpl(env, val.fst, val.snd);
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
        return res;
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
    /**
15035
d03c311f44e6 8005092: javadoc should check for synthesized bit on an annotation
bpatel
parents: 14260
diff changeset
    92
     * Check for the synthesized bit on the annotation.
d03c311f44e6 8005092: javadoc should check for synthesized bit on an annotation
bpatel
parents: 14260
diff changeset
    93
     *
d03c311f44e6 8005092: javadoc should check for synthesized bit on an annotation
bpatel
parents: 14260
diff changeset
    94
     * @return true if the annotation is synthesized.
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
    public boolean isSynthesized() {
d03c311f44e6 8005092: javadoc should check for synthesized bit on an annotation
bpatel
parents: 14260
diff changeset
    97
        return annotation.isSynthesized();
d03c311f44e6 8005092: javadoc should check for synthesized bit on an annotation
bpatel
parents: 14260
diff changeset
    98
    }
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
    /**
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
     * Returns a string representation of this annotation.
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
     * String is of one of the forms:
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
     *     @com.example.foo(name1=val1, name2=val2)
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
     *     @com.example.foo(val)
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
     *     @com.example.foo
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
     * Omit parens for marker annotations, and omit "value=" when allowed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
     */
8631
664f84942e74 6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents: 5520
diff changeset
   108
    @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
    public String toString() {
8631
664f84942e74 6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents: 5520
diff changeset
   110
        StringBuilder sb = new StringBuilder("@");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
        sb.append(annotation.type.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
        ElementValuePair vals[] = elementValues();
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
        if (vals.length > 0) {          // omit parens for marker annotation
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
            sb.append('(');
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
            boolean first = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
            for (ElementValuePair val : vals) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
                if (!first) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
                    sb.append(", ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
                first = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
                String name = val.element().name();
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
                if (vals.length == 1 && name.equals("value")) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
                    sb.append(val.value());
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
                    sb.append(val);
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
            sb.append(')');
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
        return sb.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
     * Represents an association between an annotation type element
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
     * and one of its values.
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
    public static class ElementValuePairImpl implements ElementValuePair {
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
        private final DocEnv env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
        private final MethodSymbol meth;
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
        private final Attribute value;
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
        ElementValuePairImpl(DocEnv env, MethodSymbol meth, Attribute value) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
            this.env = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
            this.meth = meth;
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
            this.value = value;
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
         * Returns the annotation type element.
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
        public AnnotationTypeElementDoc element() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
            return env.getAnnotationTypeElementDoc(meth);
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
         * Returns the value associated with the annotation type element.
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
        public AnnotationValue value() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
            return new AnnotationValueImpl(env, value);
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
         * Returns a string representation of this pair
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
         * of the form "name=value".
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
         */
8631
664f84942e74 6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents: 5520
diff changeset
   170
        @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
        public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
            return meth.name + "=" + value();
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
}