author | bpatel |
Sat, 29 Mar 2014 11:06:33 -0700 | |
changeset 23799 | daa645653200 |
parent 22448 | a85fbad9d687 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
22448
a85fbad9d687
8029230: Update copyright year to match last edit in jdk8 langtools repository for 2013
ksrini
parents:
15035
diff
changeset
|
2 |
* Copyright (c) 2003, 2013, 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 |
import com.sun.tools.javac.util.List; |
|
33 |
import com.sun.tools.javac.util.Pair; |
|
34 |
||
35 |
||
36 |
/** |
|
37 |
* Represents an annotation. |
|
38 |
* An annotation associates a value with each element of an annotation type. |
|
39 |
* Sure it ought to be called "Annotation", but that clashes with |
|
40 |
* java.lang.annotation.Annotation. |
|
41 |
* |
|
14260
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14258
diff
changeset
|
42 |
* <p><b>This is NOT part of any supported API. |
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14258
diff
changeset
|
43 |
* 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
|
44 |
* 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
|
45 |
* deletion without notice.</b> |
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14258
diff
changeset
|
46 |
* |
10 | 47 |
* @author Scott Seligman |
48 |
* @since 1.5 |
|
49 |
*/ |
|
50 |
||
51 |
public class AnnotationDescImpl implements AnnotationDesc { |
|
52 |
||
53 |
private final DocEnv env; |
|
54 |
private final Attribute.Compound annotation; |
|
55 |
||
56 |
||
57 |
AnnotationDescImpl(DocEnv env, Attribute.Compound annotation) { |
|
58 |
this.env = env; |
|
59 |
this.annotation = annotation; |
|
60 |
} |
|
61 |
||
62 |
/** |
|
63 |
* Returns the annotation type of this annotation. |
|
64 |
*/ |
|
65 |
public AnnotationTypeDoc annotationType() { |
|
66 |
ClassSymbol atsym = (ClassSymbol)annotation.type.tsym; |
|
3892 | 67 |
if (annotation.type.isErroneous()) { |
68 |
env.warning(null, "javadoc.class_not_found", annotation.type.toString()); |
|
69 |
return new AnnotationTypeDocImpl(env, atsym); |
|
70 |
} else { |
|
71 |
return (AnnotationTypeDoc)env.getClassDoc(atsym); |
|
72 |
} |
|
10 | 73 |
} |
74 |
||
75 |
/** |
|
76 |
* Returns this annotation's elements and their values. |
|
77 |
* Only those explicitly present in the annotation are |
|
78 |
* included, not those assuming their default values. |
|
79 |
* Returns an empty array if there are none. |
|
80 |
*/ |
|
81 |
public ElementValuePair[] elementValues() { |
|
82 |
List<Pair<MethodSymbol,Attribute>> vals = annotation.values; |
|
83 |
ElementValuePair res[] = new ElementValuePair[vals.length()]; |
|
84 |
int i = 0; |
|
85 |
for (Pair<MethodSymbol,Attribute> val : vals) { |
|
86 |
res[i++] = new ElementValuePairImpl(env, val.fst, val.snd); |
|
87 |
} |
|
88 |
return res; |
|
89 |
} |
|
90 |
||
91 |
/** |
|
15035
d03c311f44e6
8005092: javadoc should check for synthesized bit on an annotation
bpatel
parents:
14260
diff
changeset
|
92 |
* Check for the synthesized bit on the annotation. |
d03c311f44e6
8005092: javadoc should check for synthesized bit on an annotation
bpatel
parents:
14260
diff
changeset
|
93 |
* |
d03c311f44e6
8005092: javadoc should check for synthesized bit on an annotation
bpatel
parents:
14260
diff
changeset
|
94 |
* @return true if the annotation is synthesized. |
d03c311f44e6
8005092: javadoc should check for synthesized bit on an annotation
bpatel
parents:
14260
diff
changeset
|
95 |
*/ |
d03c311f44e6
8005092: javadoc should check for synthesized bit on an annotation
bpatel
parents:
14260
diff
changeset
|
96 |
public boolean isSynthesized() { |
d03c311f44e6
8005092: javadoc should check for synthesized bit on an annotation
bpatel
parents:
14260
diff
changeset
|
97 |
return annotation.isSynthesized(); |
d03c311f44e6
8005092: javadoc should check for synthesized bit on an annotation
bpatel
parents:
14260
diff
changeset
|
98 |
} |
d03c311f44e6
8005092: javadoc should check for synthesized bit on an annotation
bpatel
parents:
14260
diff
changeset
|
99 |
|
d03c311f44e6
8005092: javadoc should check for synthesized bit on an annotation
bpatel
parents:
14260
diff
changeset
|
100 |
/** |
10 | 101 |
* Returns a string representation of this annotation. |
102 |
* String is of one of the forms: |
|
103 |
* @com.example.foo(name1=val1, name2=val2) |
|
104 |
* @com.example.foo(val) |
|
105 |
* @com.example.foo |
|
106 |
* Omit parens for marker annotations, and omit "value=" when allowed. |
|
107 |
*/ |
|
8631
664f84942e74
6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents:
5520
diff
changeset
|
108 |
@Override |
10 | 109 |
public String toString() { |
8631
664f84942e74
6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents:
5520
diff
changeset
|
110 |
StringBuilder sb = new StringBuilder("@"); |
10 | 111 |
sb.append(annotation.type.tsym); |
112 |
||
113 |
ElementValuePair vals[] = elementValues(); |
|
114 |
if (vals.length > 0) { // omit parens for marker annotation |
|
115 |
sb.append('('); |
|
116 |
boolean first = true; |
|
117 |
for (ElementValuePair val : vals) { |
|
118 |
if (!first) { |
|
119 |
sb.append(", "); |
|
120 |
} |
|
121 |
first = false; |
|
122 |
||
123 |
String name = val.element().name(); |
|
124 |
if (vals.length == 1 && name.equals("value")) { |
|
125 |
sb.append(val.value()); |
|
126 |
} else { |
|
127 |
sb.append(val); |
|
128 |
} |
|
129 |
} |
|
130 |
sb.append(')'); |
|
131 |
} |
|
132 |
return sb.toString(); |
|
133 |
} |
|
134 |
||
135 |
||
136 |
/** |
|
137 |
* Represents an association between an annotation type element |
|
138 |
* and one of its values. |
|
139 |
*/ |
|
140 |
public static class ElementValuePairImpl implements ElementValuePair { |
|
141 |
||
142 |
private final DocEnv env; |
|
143 |
private final MethodSymbol meth; |
|
144 |
private final Attribute value; |
|
145 |
||
146 |
ElementValuePairImpl(DocEnv env, MethodSymbol meth, Attribute value) { |
|
147 |
this.env = env; |
|
148 |
this.meth = meth; |
|
149 |
this.value = value; |
|
150 |
} |
|
151 |
||
152 |
/** |
|
153 |
* Returns the annotation type element. |
|
154 |
*/ |
|
155 |
public AnnotationTypeElementDoc element() { |
|
156 |
return env.getAnnotationTypeElementDoc(meth); |
|
157 |
} |
|
158 |
||
159 |
/** |
|
160 |
* Returns the value associated with the annotation type element. |
|
161 |
*/ |
|
162 |
public AnnotationValue value() { |
|
163 |
return new AnnotationValueImpl(env, value); |
|
164 |
} |
|
165 |
||
166 |
/** |
|
167 |
* Returns a string representation of this pair |
|
168 |
* of the form "name=value". |
|
169 |
*/ |
|
8631
664f84942e74
6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents:
5520
diff
changeset
|
170 |
@Override |
10 | 171 |
public String toString() { |
172 |
return meth.name + "=" + value(); |
|
173 |
} |
|
174 |
} |
|
175 |
} |