|
1 /* |
|
2 * Copyright 2003-2004 Sun Microsystems, Inc. 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. Sun designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 * have any questions. |
|
24 */ |
|
25 |
|
26 package com.sun.tools.javadoc; |
|
27 |
|
28 |
|
29 import com.sun.javadoc.*; |
|
30 |
|
31 import com.sun.tools.javac.code.Attribute; |
|
32 import com.sun.tools.javac.code.Symbol.*; |
|
33 import com.sun.tools.javac.code.Type; |
|
34 import com.sun.tools.javac.code.TypeTags; |
|
35 |
|
36 |
|
37 /** |
|
38 * Represents a value of an annotation type element. |
|
39 * |
|
40 * @author Scott Seligman |
|
41 * @since 1.5 |
|
42 */ |
|
43 |
|
44 public class AnnotationValueImpl implements AnnotationValue { |
|
45 |
|
46 private final DocEnv env; |
|
47 private final Attribute attr; |
|
48 |
|
49 |
|
50 AnnotationValueImpl(DocEnv env, Attribute attr) { |
|
51 this.env = env; |
|
52 this.attr = attr; |
|
53 } |
|
54 |
|
55 /** |
|
56 * Returns the value. |
|
57 * The type of the returned object is one of the following: |
|
58 * <ul><li> a wrapper class for a primitive type |
|
59 * <li> <code>String</code> |
|
60 * <li> <code>Type</code> (representing a class literal) |
|
61 * <li> <code>FieldDoc</code> (representing an enum constant) |
|
62 * <li> <code>AnnotationDesc</code> |
|
63 * <li> <code>AnnotationValue[]</code> |
|
64 * </ul> |
|
65 */ |
|
66 public Object value() { |
|
67 ValueVisitor vv = new ValueVisitor(); |
|
68 attr.accept(vv); |
|
69 return vv.value; |
|
70 } |
|
71 |
|
72 private class ValueVisitor implements Attribute.Visitor { |
|
73 public Object value; |
|
74 |
|
75 public void visitConstant(Attribute.Constant c) { |
|
76 if (c.type.tag == TypeTags.BOOLEAN) { |
|
77 // javac represents false and true as integers 0 and 1 |
|
78 value = Boolean.valueOf( |
|
79 ((Integer)c.value).intValue() != 0); |
|
80 } else { |
|
81 value = c.value; |
|
82 } |
|
83 } |
|
84 |
|
85 public void visitClass(Attribute.Class c) { |
|
86 value = TypeMaker.getType(env, |
|
87 env.types.erasure(c.type)); |
|
88 } |
|
89 |
|
90 public void visitEnum(Attribute.Enum e) { |
|
91 value = env.getFieldDoc(e.value); |
|
92 } |
|
93 |
|
94 public void visitCompound(Attribute.Compound c) { |
|
95 value = new AnnotationDescImpl(env, c); |
|
96 } |
|
97 |
|
98 public void visitArray(Attribute.Array a) { |
|
99 AnnotationValue vals[] = new AnnotationValue[a.values.length]; |
|
100 for (int i = 0; i < vals.length; i++) { |
|
101 vals[i] = new AnnotationValueImpl(env, a.values[i]); |
|
102 } |
|
103 value = vals; |
|
104 } |
|
105 |
|
106 public void visitError(Attribute.Error e) { |
|
107 value = "<error>"; |
|
108 } |
|
109 } |
|
110 |
|
111 /** |
|
112 * Returns a string representation of the value. |
|
113 * |
|
114 * @return the text of a Java language annotation value expression |
|
115 * whose value is the value of this annotation type element. |
|
116 */ |
|
117 public String toString() { |
|
118 ToStringVisitor tv = new ToStringVisitor(); |
|
119 attr.accept(tv); |
|
120 return tv.toString(); |
|
121 } |
|
122 |
|
123 private class ToStringVisitor implements Attribute.Visitor { |
|
124 private final StringBuffer sb = new StringBuffer(); |
|
125 |
|
126 public String toString() { |
|
127 return sb.toString(); |
|
128 } |
|
129 |
|
130 public void visitConstant(Attribute.Constant c) { |
|
131 if (c.type.tag == TypeTags.BOOLEAN) { |
|
132 // javac represents false and true as integers 0 and 1 |
|
133 sb.append(((Integer)c.value).intValue() != 0); |
|
134 } else { |
|
135 sb.append(FieldDocImpl.constantValueExpression(c.value)); |
|
136 } |
|
137 } |
|
138 |
|
139 public void visitClass(Attribute.Class c) { |
|
140 sb.append(c); |
|
141 } |
|
142 |
|
143 public void visitEnum(Attribute.Enum e) { |
|
144 sb.append(e); |
|
145 } |
|
146 |
|
147 public void visitCompound(Attribute.Compound c) { |
|
148 sb.append(new AnnotationDescImpl(env, c)); |
|
149 } |
|
150 |
|
151 public void visitArray(Attribute.Array a) { |
|
152 // Omit braces from singleton. |
|
153 if (a.values.length != 1) sb.append('{'); |
|
154 |
|
155 boolean first = true; |
|
156 for (Attribute elem : a.values) { |
|
157 if (first) { |
|
158 first = false; |
|
159 } else { |
|
160 sb.append(", "); |
|
161 } |
|
162 elem.accept(this); |
|
163 } |
|
164 // Omit braces from singleton. |
|
165 if (a.values.length != 1) sb.append('}'); |
|
166 } |
|
167 |
|
168 public void visitError(Attribute.Error e) { |
|
169 sb.append("<error>"); |
|
170 } |
|
171 } |
|
172 } |