langtools/src/share/classes/com/sun/tools/javadoc/AnnotationDescImpl.java
author jjg
Fri, 04 Mar 2011 19:53:03 -0800
changeset 8631 664f84942e74
parent 5520 86e4b9a9da40
child 14258 8d2148961366
permissions -rw-r--r--
6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup Reviewed-by: bpatel
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
8631
664f84942e74 6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents: 5520
diff changeset
     2
 * Copyright (c) 2003, 2011, 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
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import com.sun.javadoc.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import com.sun.tools.javac.code.Attribute;
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.util.List;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import com.sun.tools.javac.util.Pair;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
 * Represents an annotation.
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
 * An annotation associates a value with each element of an annotation type.
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
 * Sure it ought to be called "Annotation", but that clashes with
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 * java.lang.annotation.Annotation.
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 * @author Scott Seligman
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 * @since 1.5
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
public class AnnotationDescImpl implements AnnotationDesc {
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
    private final DocEnv env;
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
    private final Attribute.Compound annotation;
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
    AnnotationDescImpl(DocEnv env, Attribute.Compound annotation) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
        this.env = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
        this.annotation = 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
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
     * Returns the annotation type of this annotation.
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
    public AnnotationTypeDoc annotationType() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
        ClassSymbol atsym = (ClassSymbol)annotation.type.tsym;
3892
7f888c00fad7 6709246: ClassCastException in javadoc
jjg
parents: 10
diff changeset
    63
        if (annotation.type.isErroneous()) {
7f888c00fad7 6709246: ClassCastException in javadoc
jjg
parents: 10
diff changeset
    64
            env.warning(null, "javadoc.class_not_found", annotation.type.toString());
7f888c00fad7 6709246: ClassCastException in javadoc
jjg
parents: 10
diff changeset
    65
            return new AnnotationTypeDocImpl(env, atsym);
7f888c00fad7 6709246: ClassCastException in javadoc
jjg
parents: 10
diff changeset
    66
        } else {
7f888c00fad7 6709246: ClassCastException in javadoc
jjg
parents: 10
diff changeset
    67
            return (AnnotationTypeDoc)env.getClassDoc(atsym);
7f888c00fad7 6709246: ClassCastException in javadoc
jjg
parents: 10
diff changeset
    68
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
     * Returns this annotation's elements and their values.
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
     * Only those explicitly present in the annotation are
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
     * included, not those assuming their default values.
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
     * Returns an empty array if there are none.
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    public ElementValuePair[] elementValues() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
        List<Pair<MethodSymbol,Attribute>> vals = annotation.values;
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
        ElementValuePair res[] = new ElementValuePair[vals.length()];
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
        int i = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
        for (Pair<MethodSymbol,Attribute> val : vals) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
            res[i++] = new ElementValuePairImpl(env, val.fst, val.snd);
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
        return res;
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
     * Returns a string representation of this annotation.
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
     * String is of one of the forms:
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
     *     @com.example.foo(name1=val1, name2=val2)
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
     *     @com.example.foo(val)
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
     *     @com.example.foo
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
     * Omit parens for marker annotations, and omit "value=" when allowed.
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
     */
8631
664f84942e74 6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents: 5520
diff changeset
    95
    @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
    public String toString() {
8631
664f84942e74 6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents: 5520
diff changeset
    97
        StringBuilder sb = new StringBuilder("@");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
        sb.append(annotation.type.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
        ElementValuePair vals[] = elementValues();
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
        if (vals.length > 0) {          // omit parens for marker annotation
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
            sb.append('(');
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
            boolean first = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
            for (ElementValuePair val : vals) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
                if (!first) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
                    sb.append(", ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
                first = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
                String name = val.element().name();
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
                if (vals.length == 1 && name.equals("value")) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
                    sb.append(val.value());
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
                    sb.append(val);
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
            sb.append(')');
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
        return sb.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
     * Represents an association between an annotation type element
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
     * and one of its values.
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
    public static class ElementValuePairImpl implements ElementValuePair {
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
        private final DocEnv env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
        private final MethodSymbol meth;
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
        private final Attribute value;
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
        ElementValuePairImpl(DocEnv env, MethodSymbol meth, Attribute value) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
            this.env = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
            this.meth = meth;
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
            this.value = value;
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
         * Returns the annotation type element.
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
        public AnnotationTypeElementDoc element() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
            return env.getAnnotationTypeElementDoc(meth);
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
         * Returns the value associated with the annotation type element.
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
        public AnnotationValue value() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
            return new AnnotationValueImpl(env, value);
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
         * Returns a string representation of this pair
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
         * of the form "name=value".
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
         */
8631
664f84942e74 6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents: 5520
diff changeset
   157
        @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
        public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
            return meth.name + "=" + value();
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
}