author | jfranck |
Fri, 31 Aug 2012 10:37:46 +0100 | |
changeset 13689 | 4d519199a6aa |
parent 8631 | 664f84942e74 |
child 14258 | 8d2148961366 |
permissions | -rw-r--r-- |
10 | 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 | 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 |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 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 |
* |
|
5520 | 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. |
|
10 | 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.TypeTags; |
|
34 |
||
35 |
||
36 |
/** |
|
37 |
* Represents a value of an annotation type element. |
|
38 |
* |
|
39 |
* @author Scott Seligman |
|
40 |
* @since 1.5 |
|
41 |
*/ |
|
42 |
||
43 |
public class AnnotationValueImpl implements AnnotationValue { |
|
44 |
||
45 |
private final DocEnv env; |
|
46 |
private final Attribute attr; |
|
47 |
||
48 |
||
49 |
AnnotationValueImpl(DocEnv env, Attribute attr) { |
|
50 |
this.env = env; |
|
51 |
this.attr = attr; |
|
52 |
} |
|
53 |
||
54 |
/** |
|
55 |
* Returns the value. |
|
56 |
* The type of the returned object is one of the following: |
|
57 |
* <ul><li> a wrapper class for a primitive type |
|
58 |
* <li> <code>String</code> |
|
59 |
* <li> <code>Type</code> (representing a class literal) |
|
60 |
* <li> <code>FieldDoc</code> (representing an enum constant) |
|
61 |
* <li> <code>AnnotationDesc</code> |
|
62 |
* <li> <code>AnnotationValue[]</code> |
|
63 |
* </ul> |
|
64 |
*/ |
|
65 |
public Object value() { |
|
66 |
ValueVisitor vv = new ValueVisitor(); |
|
67 |
attr.accept(vv); |
|
68 |
return vv.value; |
|
69 |
} |
|
70 |
||
71 |
private class ValueVisitor implements Attribute.Visitor { |
|
72 |
public Object value; |
|
73 |
||
74 |
public void visitConstant(Attribute.Constant c) { |
|
75 |
if (c.type.tag == TypeTags.BOOLEAN) { |
|
76 |
// javac represents false and true as integers 0 and 1 |
|
77 |
value = Boolean.valueOf( |
|
78 |
((Integer)c.value).intValue() != 0); |
|
79 |
} else { |
|
80 |
value = c.value; |
|
81 |
} |
|
82 |
} |
|
83 |
||
84 |
public void visitClass(Attribute.Class c) { |
|
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 | 87 |
} |
88 |
||
89 |
public void visitEnum(Attribute.Enum e) { |
|
90 |
value = env.getFieldDoc(e.value); |
|
91 |
} |
|
92 |
||
93 |
public void visitCompound(Attribute.Compound c) { |
|
94 |
value = new AnnotationDescImpl(env, c); |
|
95 |
} |
|
96 |
||
97 |
public void visitArray(Attribute.Array a) { |
|
98 |
AnnotationValue vals[] = new AnnotationValue[a.values.length]; |
|
99 |
for (int i = 0; i < vals.length; i++) { |
|
100 |
vals[i] = new AnnotationValueImpl(env, a.values[i]); |
|
101 |
} |
|
102 |
value = vals; |
|
103 |
} |
|
104 |
||
105 |
public void visitError(Attribute.Error e) { |
|
106 |
value = "<error>"; |
|
107 |
} |
|
108 |
} |
|
109 |
||
110 |
/** |
|
111 |
* Returns a string representation of the value. |
|
112 |
* |
|
113 |
* @return the text of a Java language annotation value expression |
|
114 |
* whose value is the value of this annotation type element. |
|
115 |
*/ |
|
8631
664f84942e74
6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents:
5520
diff
changeset
|
116 |
@Override |
10 | 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 { |
|
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 | 125 |
|
8631
664f84942e74
6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents:
5520
diff
changeset
|
126 |
@Override |
10 | 127 |
public String toString() { |
128 |
return sb.toString(); |
|
129 |
} |
|
130 |
||
131 |
public void visitConstant(Attribute.Constant c) { |
|
132 |
if (c.type.tag == TypeTags.BOOLEAN) { |
|
133 |
// javac represents false and true as integers 0 and 1 |
|
134 |
sb.append(((Integer)c.value).intValue() != 0); |
|
135 |
} else { |
|
136 |
sb.append(FieldDocImpl.constantValueExpression(c.value)); |
|
137 |
} |
|
138 |
} |
|
139 |
||
140 |
public void visitClass(Attribute.Class c) { |
|
141 |
sb.append(c); |
|
142 |
} |
|
143 |
||
144 |
public void visitEnum(Attribute.Enum e) { |
|
145 |
sb.append(e); |
|
146 |
} |
|
147 |
||
148 |
public void visitCompound(Attribute.Compound c) { |
|
149 |
sb.append(new AnnotationDescImpl(env, c)); |
|
150 |
} |
|
151 |
||
152 |
public void visitArray(Attribute.Array a) { |
|
153 |
// Omit braces from singleton. |
|
154 |
if (a.values.length != 1) sb.append('{'); |
|
155 |
||
156 |
boolean first = true; |
|
157 |
for (Attribute elem : a.values) { |
|
158 |
if (first) { |
|
159 |
first = false; |
|
160 |
} else { |
|
161 |
sb.append(", "); |
|
162 |
} |
|
163 |
elem.accept(this); |
|
164 |
} |
|
165 |
// Omit braces from singleton. |
|
166 |
if (a.values.length != 1) sb.append('}'); |
|
167 |
} |
|
168 |
||
169 |
public void visitError(Attribute.Error e) { |
|
170 |
sb.append("<error>"); |
|
171 |
} |
|
172 |
} |
|
173 |
} |