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