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