src/jdk.javadoc/share/classes/com/sun/tools/javadoc/main/AnnotationValueImpl.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: 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
37938
42baa89d2156 8075703: jdk.javadoc module exports com.sun.tools.javadoc package which contains a lot of internal API.
jjg
parents: 25874
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
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14260
diff changeset
    32
import static com.sun.tools.javac.code.TypeTag.BOOLEAN;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
 * Represents a value of an annotation type element.
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
 *
14260
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    37
 *  <p><b>This is NOT part of any supported API.
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    38
 *  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
    39
 *  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
    40
 *  deletion without notice.</b>
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    41
 *
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 * @author Scott Seligman
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 * @since 1.5
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
48840
5e2d2067da48 8194651: javadoc: mark the com.sun.javadoc API for removal
ksrini
parents: 47216
diff changeset
    46
@Deprecated(since="9", forRemoval=true)
5e2d2067da48 8194651: javadoc: mark the com.sun.javadoc API for removal
ksrini
parents: 47216
diff changeset
    47
@SuppressWarnings("removal")
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
public class AnnotationValueImpl implements AnnotationValue {
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
    private final DocEnv env;
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
    private final Attribute 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
    AnnotationValueImpl(DocEnv env, Attribute attr) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
        this.env = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
        this.attr = attr;
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
     * Returns the value.
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
     * The type of the returned object is one of the following:
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
     * <ul><li> a wrapper class for a primitive type
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
     *     <li> <code>String</code>
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
     *     <li> <code>Type</code> (representing a class literal)
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
     *     <li> <code>FieldDoc</code> (representing an enum constant)
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
     *     <li> <code>AnnotationDesc</code>
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
     *     <li> <code>AnnotationValue[]</code>
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
     * </ul>
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    public Object value() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
        ValueVisitor vv = new ValueVisitor();
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
        attr.accept(vv);
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
        return vv.value;
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
    private class ValueVisitor implements Attribute.Visitor {
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
        public Object value;
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
        public void visitConstant(Attribute.Constant c) {
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14260
diff changeset
    80
            if (c.type.hasTag(BOOLEAN)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
                // javac represents false and true as integers 0 and 1
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
                value = Boolean.valueOf(
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
                                ((Integer)c.value).intValue() != 0);
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
                value = c.value;
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
            }
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 visitClass(Attribute.Class c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
            value = TypeMaker.getType(env,
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 8631
diff changeset
    91
                                      env.types.erasure(c.classType));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
        public void visitEnum(Attribute.Enum e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
            value = env.getFieldDoc(e.value);
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
        public void visitCompound(Attribute.Compound c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
            value = new AnnotationDescImpl(env, c);
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
        public void visitArray(Attribute.Array a) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
            AnnotationValue vals[] = new AnnotationValue[a.values.length];
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
            for (int i = 0; i < vals.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
                vals[i] = new AnnotationValueImpl(env, a.values[i]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
            value = vals;
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
        public void visitError(Attribute.Error e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
            value = "<error>";
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
     * Returns a string representation of the value.
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
     * @return the text of a Java language annotation value expression
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
     *          whose value is the value of this annotation type element.
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
     */
8631
664f84942e74 6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents: 5520
diff changeset
   121
    @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
    public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
        ToStringVisitor tv = new ToStringVisitor();
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
        attr.accept(tv);
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
        return tv.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
    private class ToStringVisitor implements Attribute.Visitor {
8631
664f84942e74 6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents: 5520
diff changeset
   129
        private final StringBuilder sb = new StringBuilder();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
8631
664f84942e74 6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents: 5520
diff changeset
   131
        @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
        public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
            return sb.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
        public void visitConstant(Attribute.Constant c) {
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14260
diff changeset
   137
            if (c.type.hasTag(BOOLEAN)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
                // javac represents false and true as integers 0 and 1
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
                sb.append(((Integer)c.value).intValue() != 0);
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
                sb.append(FieldDocImpl.constantValueExpression(c.value));
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
        public void visitClass(Attribute.Class c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
            sb.append(c);
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
        public void visitEnum(Attribute.Enum e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
            sb.append(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
        public void visitCompound(Attribute.Compound c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
            sb.append(new AnnotationDescImpl(env, c));
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
        public void visitArray(Attribute.Array a) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
            // Omit braces from singleton.
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
            if (a.values.length != 1) sb.append('{');
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
            boolean first = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
            for (Attribute elem : a.values) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
                if (first) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
                    first = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
                    sb.append(", ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
                elem.accept(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
            // Omit braces from singleton.
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
            if (a.values.length != 1) sb.append('}');
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
        public void visitError(Attribute.Error e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
            sb.append("<error>");
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
}