1 /*
2  * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package com.sun.tools.javadoc.main;
27 
28 import com.sun.javadoc.*;
29 
30 import com.sun.tools.javac.code.Attribute;
31 
32 import static com.sun.tools.javac.code.TypeTag.BOOLEAN;
33 
34 /**
35  * Represents a value of an annotation type element.
36  *
37  *  <p><b>This is NOT part of any supported API.
38  *  If you write code that depends on this, you do so at your own risk.
39  *  This code and its internal interfaces are subject to change or
40  *  deletion without notice.</b>
41  *
42  * @author Scott Seligman
43  * @since 1.5
44  */
45 
46 @Deprecated(since="9", forRemoval=true)
47 @SuppressWarnings("removal")
48 public class AnnotationValueImpl implements AnnotationValue {
49 
50     private final DocEnv env;
51     private final Attribute attr;
52 
53 
AnnotationValueImpl(DocEnv env, Attribute attr)54     AnnotationValueImpl(DocEnv env, Attribute attr) {
55         this.env = env;
56         this.attr = attr;
57     }
58 
59     /**
60      * Returns the value.
61      * The type of the returned object is one of the following:
62      * <ul><li> a wrapper class for a primitive type
63      *     <li> <code>String</code>
64      *     <li> <code>Type</code> (representing a class literal)
65      *     <li> <code>FieldDoc</code> (representing an enum constant)
66      *     <li> <code>AnnotationDesc</code>
67      *     <li> <code>AnnotationValue[]</code>
68      * </ul>
69      */
value()70     public Object value() {
71         ValueVisitor vv = new ValueVisitor();
72         attr.accept(vv);
73         return vv.value;
74     }
75 
76     private class ValueVisitor implements Attribute.Visitor {
77         public Object value;
78 
visitConstant(Attribute.Constant c)79         public void visitConstant(Attribute.Constant c) {
80             if (c.type.hasTag(BOOLEAN)) {
81                 // javac represents false and true as integers 0 and 1
82                 value = Boolean.valueOf(
83                                 ((Integer)c.value).intValue() != 0);
84             } else {
85                 value = c.value;
86             }
87         }
88 
visitClass(Attribute.Class c)89         public void visitClass(Attribute.Class c) {
90             value = TypeMaker.getType(env,
91                                       env.types.erasure(c.classType));
92         }
93 
visitEnum(Attribute.Enum e)94         public void visitEnum(Attribute.Enum e) {
95             value = env.getFieldDoc(e.value);
96         }
97 
visitCompound(Attribute.Compound c)98         public void visitCompound(Attribute.Compound c) {
99             value = new AnnotationDescImpl(env, c);
100         }
101 
visitArray(Attribute.Array a)102         public void visitArray(Attribute.Array a) {
103             AnnotationValue vals[] = new AnnotationValue[a.values.length];
104             for (int i = 0; i < vals.length; i++) {
105                 vals[i] = new AnnotationValueImpl(env, a.values[i]);
106             }
107             value = vals;
108         }
109 
visitError(Attribute.Error e)110         public void visitError(Attribute.Error e) {
111             value = "<error>";
112         }
113     }
114 
115     /**
116      * Returns a string representation of the value.
117      *
118      * @return the text of a Java language annotation value expression
119      *          whose value is the value of this annotation type element.
120      */
121     @Override
toString()122     public String toString() {
123         ToStringVisitor tv = new ToStringVisitor();
124         attr.accept(tv);
125         return tv.toString();
126     }
127 
128     private class ToStringVisitor implements Attribute.Visitor {
129         private final StringBuilder sb = new StringBuilder();
130 
131         @Override
toString()132         public String toString() {
133             return sb.toString();
134         }
135 
visitConstant(Attribute.Constant c)136         public void visitConstant(Attribute.Constant c) {
137             if (c.type.hasTag(BOOLEAN)) {
138                 // javac represents false and true as integers 0 and 1
139                 sb.append(((Integer)c.value).intValue() != 0);
140             } else {
141                 sb.append(FieldDocImpl.constantValueExpression(c.value));
142             }
143         }
144 
visitClass(Attribute.Class c)145         public void visitClass(Attribute.Class c) {
146             sb.append(c);
147         }
148 
visitEnum(Attribute.Enum e)149         public void visitEnum(Attribute.Enum e) {
150             sb.append(e);
151         }
152 
visitCompound(Attribute.Compound c)153         public void visitCompound(Attribute.Compound c) {
154             sb.append(new AnnotationDescImpl(env, c));
155         }
156 
visitArray(Attribute.Array a)157         public void visitArray(Attribute.Array a) {
158             // Omit braces from singleton.
159             if (a.values.length != 1) sb.append('{');
160 
161             boolean first = true;
162             for (Attribute elem : a.values) {
163                 if (first) {
164                     first = false;
165                 } else {
166                     sb.append(", ");
167                 }
168                 elem.accept(this);
169             }
170             // Omit braces from singleton.
171             if (a.values.length != 1) sb.append('}');
172         }
173 
visitError(Attribute.Error e)174         public void visitError(Attribute.Error e) {
175             sb.append("<error>");
176         }
177     }
178 }
179