langtools/src/share/classes/com/sun/tools/javadoc/AnnotationValueImpl.java
author jfranck
Fri, 31 Aug 2012 10:37:46 +0100
changeset 13689 4d519199a6aa
parent 8631 664f84942e74
child 14258 8d2148961366
permissions -rw-r--r--
7151010: Add compiler support for repeating annotations Reviewed-by: jjg, mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 8631
diff changeset
     2
 * Copyright (c) 2003, 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
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.code.TypeTags;
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 a value of an annotation type element.
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
 * @author Scott Seligman
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
 * @since 1.5
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
public class AnnotationValueImpl implements AnnotationValue {
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
    private final DocEnv env;
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
    private final Attribute attr;
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
    AnnotationValueImpl(DocEnv env, Attribute attr) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
        this.env = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
        this.attr = attr;
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
     * Returns the value.
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
     * The type of the returned object is one of the following:
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
     * <ul><li> a wrapper class for a primitive type
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
     *     <li> <code>String</code>
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
     *     <li> <code>Type</code> (representing a class literal)
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
     *     <li> <code>FieldDoc</code> (representing an enum constant)
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
     *     <li> <code>AnnotationDesc</code>
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
     *     <li> <code>AnnotationValue[]</code>
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
     * </ul>
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    public Object value() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
        ValueVisitor vv = new ValueVisitor();
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
        attr.accept(vv);
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
        return vv.value;
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
    private class ValueVisitor implements Attribute.Visitor {
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
        public Object value;
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
        public void visitConstant(Attribute.Constant c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
            if (c.type.tag == TypeTags.BOOLEAN) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
                // javac represents false and true as integers 0 and 1
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
                value = Boolean.valueOf(
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
                                ((Integer)c.value).intValue() != 0);
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
                value = c.value;
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
        public void visitClass(Attribute.Class c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
            value = TypeMaker.getType(env,
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 8631
diff changeset
    86
                                      env.types.erasure(c.classType));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
        public void visitEnum(Attribute.Enum e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
            value = env.getFieldDoc(e.value);
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
        public void visitCompound(Attribute.Compound c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
            value = new AnnotationDescImpl(env, c);
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
        public void visitArray(Attribute.Array a) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
            AnnotationValue vals[] = new AnnotationValue[a.values.length];
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
            for (int i = 0; i < vals.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
                vals[i] = new AnnotationValueImpl(env, a.values[i]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
            value = vals;
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
        public void visitError(Attribute.Error e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
            value = "<error>";
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
     * Returns a string representation of the value.
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
     * @return the text of a Java language annotation value expression
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
     *          whose value is the value of this annotation type element.
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
     */
8631
664f84942e74 6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents: 5520
diff changeset
   116
    @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
    public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
        ToStringVisitor tv = new ToStringVisitor();
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
        attr.accept(tv);
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
        return tv.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
    private class ToStringVisitor implements Attribute.Visitor {
8631
664f84942e74 6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents: 5520
diff changeset
   124
        private final StringBuilder sb = new StringBuilder();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
8631
664f84942e74 6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents: 5520
diff changeset
   126
        @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
        public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
            return sb.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
        public void visitConstant(Attribute.Constant c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
            if (c.type.tag == TypeTags.BOOLEAN) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
                // javac represents false and true as integers 0 and 1
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
                sb.append(((Integer)c.value).intValue() != 0);
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
                sb.append(FieldDocImpl.constantValueExpression(c.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
        public void visitClass(Attribute.Class c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
            sb.append(c);
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
        public void visitEnum(Attribute.Enum e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
            sb.append(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
        public void visitCompound(Attribute.Compound c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
            sb.append(new AnnotationDescImpl(env, c));
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
        public void visitArray(Attribute.Array a) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
            // Omit braces from singleton.
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
            if (a.values.length != 1) sb.append('{');
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
            boolean first = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
            for (Attribute elem : a.values) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
                if (first) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
                    first = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
                    sb.append(", ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
                elem.accept(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
            // Omit braces from singleton.
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
            if (a.values.length != 1) sb.append('}');
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
        public void visitError(Attribute.Error e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
            sb.append("<error>");
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
}