author | jjg |
Thu, 13 Sep 2012 14:29:36 -0700 | |
changeset 13844 | 56339cf983a3 |
parent 8631 | 664f84942e74 |
child 14258 | 8d2148961366 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
13844 | 2 |
* Copyright (c) 1997, 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 |
||
1869 | 28 |
import java.lang.reflect.Modifier; |
29 |
||
10 | 30 |
import com.sun.javadoc.*; |
31 |
||
32 |
import static com.sun.javadoc.LanguageVersion.*; |
|
33 |
||
34 |
import com.sun.tools.javac.code.Flags; |
|
35 |
import com.sun.tools.javac.code.Symbol.ClassSymbol; |
|
36 |
import com.sun.tools.javac.code.Symbol.VarSymbol; |
|
37 |
import com.sun.tools.javac.code.TypeTags; |
|
38 |
||
39 |
import com.sun.tools.javac.tree.JCTree.JCVariableDecl; |
|
40 |
||
41 |
import com.sun.tools.javac.util.Position; |
|
42 |
||
43 |
/** |
|
44 |
* Represents a field in a java class. |
|
45 |
* |
|
46 |
* @see MemberDocImpl |
|
47 |
* |
|
48 |
* @since 1.2 |
|
49 |
* @author Robert Field |
|
50 |
* @author Neal Gafter (rewrite) |
|
51 |
* @author Scott Seligman (generics, enums, annotations) |
|
52 |
*/ |
|
53 |
public class FieldDocImpl extends MemberDocImpl implements FieldDoc { |
|
54 |
||
55 |
protected final VarSymbol sym; |
|
56 |
||
57 |
/** |
|
58 |
* Constructor. |
|
59 |
*/ |
|
60 |
public FieldDocImpl(DocEnv env, VarSymbol sym, |
|
61 |
String rawDocs, JCVariableDecl tree, Position.LineMap lineMap) { |
|
62 |
super(env, sym, rawDocs, tree, lineMap); |
|
63 |
this.sym = sym; |
|
64 |
} |
|
65 |
||
66 |
/** |
|
67 |
* Constructor. |
|
68 |
*/ |
|
69 |
public FieldDocImpl(DocEnv env, VarSymbol sym) { |
|
70 |
this(env, sym, null, null, null); |
|
71 |
} |
|
72 |
||
73 |
/** |
|
74 |
* Returns the flags in terms of javac's flags |
|
75 |
*/ |
|
76 |
protected long getFlags() { |
|
77 |
return sym.flags(); |
|
78 |
} |
|
79 |
||
80 |
/** |
|
81 |
* Identify the containing class |
|
82 |
*/ |
|
83 |
protected ClassSymbol getContainingClass() { |
|
84 |
return sym.enclClass(); |
|
85 |
} |
|
86 |
||
87 |
/** |
|
88 |
* Get type of this field. |
|
89 |
*/ |
|
90 |
public com.sun.javadoc.Type type() { |
|
91 |
return TypeMaker.getType(env, sym.type, false); |
|
92 |
} |
|
93 |
||
94 |
/** |
|
95 |
* Get the value of a constant field. |
|
96 |
* |
|
97 |
* @return the value of a constant field. The value is |
|
98 |
* automatically wrapped in an object if it has a primitive type. |
|
99 |
* If the field is not constant, returns null. |
|
100 |
*/ |
|
101 |
public Object constantValue() { |
|
102 |
Object result = sym.getConstValue(); |
|
103 |
if (result != null && sym.type.tag == TypeTags.BOOLEAN) |
|
104 |
// javac represents false and true as Integers 0 and 1 |
|
105 |
result = Boolean.valueOf(((Integer)result).intValue() != 0); |
|
106 |
return result; |
|
107 |
} |
|
108 |
||
109 |
/** |
|
110 |
* Get the value of a constant field. |
|
111 |
* |
|
112 |
* @return the text of a Java language expression whose value |
|
113 |
* is the value of the constant. The expression uses no identifiers |
|
114 |
* other than primitive literals. If the field is |
|
115 |
* not constant, returns null. |
|
116 |
*/ |
|
117 |
public String constantValueExpression() { |
|
118 |
return constantValueExpression(constantValue()); |
|
119 |
} |
|
120 |
||
121 |
/** |
|
122 |
* A static version of the above. |
|
123 |
*/ |
|
124 |
static String constantValueExpression(Object cb) { |
|
125 |
if (cb == null) return null; |
|
126 |
if (cb instanceof Character) return sourceForm(((Character)cb).charValue()); |
|
127 |
if (cb instanceof Byte) return sourceForm(((Byte)cb).byteValue()); |
|
128 |
if (cb instanceof String) return sourceForm((String)cb); |
|
129 |
if (cb instanceof Double) return sourceForm(((Double)cb).doubleValue(), 'd'); |
|
130 |
if (cb instanceof Float) return sourceForm(((Float)cb).doubleValue(), 'f'); |
|
131 |
if (cb instanceof Long) return cb + "L"; |
|
132 |
return cb.toString(); // covers int, short |
|
133 |
} |
|
134 |
// where |
|
135 |
private static String sourceForm(double v, char suffix) { |
|
136 |
if (Double.isNaN(v)) |
|
137 |
return "0" + suffix + "/0" + suffix; |
|
138 |
if (v == Double.POSITIVE_INFINITY) |
|
139 |
return "1" + suffix + "/0" + suffix; |
|
140 |
if (v == Double.NEGATIVE_INFINITY) |
|
141 |
return "-1" + suffix + "/0" + suffix; |
|
142 |
return v + (suffix == 'f' || suffix == 'F' ? "" + suffix : ""); |
|
143 |
} |
|
144 |
private static String sourceForm(char c) { |
|
8631
664f84942e74
6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents:
5520
diff
changeset
|
145 |
StringBuilder buf = new StringBuilder(8); |
10 | 146 |
buf.append('\''); |
147 |
sourceChar(c, buf); |
|
148 |
buf.append('\''); |
|
149 |
return buf.toString(); |
|
150 |
} |
|
151 |
private static String sourceForm(byte c) { |
|
152 |
return "0x" + Integer.toString(c & 0xff, 16); |
|
153 |
} |
|
154 |
private static String sourceForm(String s) { |
|
8631
664f84942e74
6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents:
5520
diff
changeset
|
155 |
StringBuilder buf = new StringBuilder(s.length() + 5); |
10 | 156 |
buf.append('\"'); |
157 |
for (int i=0; i<s.length(); i++) { |
|
158 |
char c = s.charAt(i); |
|
159 |
sourceChar(c, buf); |
|
160 |
} |
|
161 |
buf.append('\"'); |
|
162 |
return buf.toString(); |
|
163 |
} |
|
8631
664f84942e74
6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents:
5520
diff
changeset
|
164 |
private static void sourceChar(char c, StringBuilder buf) { |
10 | 165 |
switch (c) { |
166 |
case '\b': buf.append("\\b"); return; |
|
167 |
case '\t': buf.append("\\t"); return; |
|
168 |
case '\n': buf.append("\\n"); return; |
|
169 |
case '\f': buf.append("\\f"); return; |
|
170 |
case '\r': buf.append("\\r"); return; |
|
171 |
case '\"': buf.append("\\\""); return; |
|
172 |
case '\'': buf.append("\\\'"); return; |
|
173 |
case '\\': buf.append("\\\\"); return; |
|
174 |
default: |
|
175 |
if (isPrintableAscii(c)) { |
|
176 |
buf.append(c); return; |
|
177 |
} |
|
178 |
unicodeEscape(c, buf); |
|
179 |
return; |
|
180 |
} |
|
181 |
} |
|
8631
664f84942e74
6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents:
5520
diff
changeset
|
182 |
private static void unicodeEscape(char c, StringBuilder buf) { |
10 | 183 |
final String chars = "0123456789abcdef"; |
184 |
buf.append("\\u"); |
|
185 |
buf.append(chars.charAt(15 & (c>>12))); |
|
186 |
buf.append(chars.charAt(15 & (c>>8))); |
|
187 |
buf.append(chars.charAt(15 & (c>>4))); |
|
188 |
buf.append(chars.charAt(15 & (c>>0))); |
|
189 |
} |
|
190 |
private static boolean isPrintableAscii(char c) { |
|
191 |
return c >= ' ' && c <= '~'; |
|
192 |
} |
|
193 |
||
194 |
/** |
|
195 |
* Return true if this field is included in the active set. |
|
196 |
*/ |
|
197 |
public boolean isIncluded() { |
|
198 |
return containingClass().isIncluded() && env.shouldDocument(sym); |
|
199 |
} |
|
200 |
||
201 |
/** |
|
202 |
* Is this Doc item a field (but not an enum constant? |
|
203 |
*/ |
|
8631
664f84942e74
6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents:
5520
diff
changeset
|
204 |
@Override |
10 | 205 |
public boolean isField() { |
206 |
return !isEnumConstant(); |
|
207 |
} |
|
208 |
||
209 |
/** |
|
210 |
* Is this Doc item an enum constant? |
|
211 |
* (For legacy doclets, return false.) |
|
212 |
*/ |
|
8631
664f84942e74
6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents:
5520
diff
changeset
|
213 |
@Override |
10 | 214 |
public boolean isEnumConstant() { |
215 |
return (getFlags() & Flags.ENUM) != 0 && |
|
216 |
!env.legacyDoclet; |
|
217 |
} |
|
218 |
||
219 |
/** |
|
220 |
* Return true if this field is transient |
|
221 |
*/ |
|
222 |
public boolean isTransient() { |
|
223 |
return Modifier.isTransient(getModifiers()); |
|
224 |
} |
|
225 |
||
226 |
/** |
|
227 |
* Return true if this field is volatile |
|
228 |
*/ |
|
229 |
public boolean isVolatile() { |
|
230 |
return Modifier.isVolatile(getModifiers()); |
|
231 |
} |
|
232 |
||
233 |
/** |
|
234 |
* Returns true if this field was synthesized by the compiler. |
|
235 |
*/ |
|
236 |
public boolean isSynthetic() { |
|
237 |
return (getFlags() & Flags.SYNTHETIC) != 0; |
|
238 |
} |
|
239 |
||
240 |
/** |
|
241 |
* Return the serialField tags in this FieldDocImpl item. |
|
242 |
* |
|
243 |
* @return an array of <tt>SerialFieldTagImpl</tt> containing all |
|
13844 | 244 |
* <code>@serialField</code> tags. |
10 | 245 |
*/ |
246 |
public SerialFieldTag[] serialFieldTags() { |
|
247 |
return comment().serialFieldTags(); |
|
248 |
} |
|
249 |
||
250 |
public String name() { |
|
251 |
return sym.name.toString(); |
|
252 |
} |
|
253 |
||
254 |
public String qualifiedName() { |
|
255 |
return sym.enclClass().getQualifiedName() + "." + name(); |
|
256 |
} |
|
257 |
||
258 |
/** |
|
259 |
* Return the source position of the entity, or null if |
|
260 |
* no position is available. |
|
261 |
*/ |
|
8631
664f84942e74
6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents:
5520
diff
changeset
|
262 |
@Override |
10 | 263 |
public SourcePosition position() { |
264 |
if (sym.enclClass().sourcefile == null) return null; |
|
1869 | 265 |
return SourcePositionImpl.make(sym.enclClass().sourcefile, |
10 | 266 |
(tree==null) ? 0 : tree.pos, |
267 |
lineMap); |
|
268 |
} |
|
269 |
} |