author | jjg |
Mon, 09 May 2016 16:52:15 -0700 | |
changeset 37938 | 42baa89d2156 |
parent 25874 | langtools/src/jdk.javadoc/share/classes/com/sun/tools/javadoc/FieldDocImpl.java@83c19f00452c |
child 38617 | d93a7f64e231 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
17275
bcaa1940863a
8012656: cache frequently used name strings for DocImpl classes
jjg
parents:
14802
diff
changeset
|
2 |
* Copyright (c) 1997, 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 |
||
37938
42baa89d2156
8075703: jdk.javadoc module exports com.sun.tools.javadoc package which contains a lot of internal API.
jjg
parents:
25874
diff
changeset
|
26 |
package com.sun.tools.javadoc.main; |
10 | 27 |
|
14802 | 28 |
import com.sun.source.util.TreePath; |
1869 | 29 |
import java.lang.reflect.Modifier; |
30 |
||
10 | 31 |
import com.sun.javadoc.*; |
32 |
||
33 |
import com.sun.tools.javac.code.Flags; |
|
34 |
import com.sun.tools.javac.code.Symbol.ClassSymbol; |
|
35 |
import com.sun.tools.javac.code.Symbol.VarSymbol; |
|
36 |
||
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14260
diff
changeset
|
37 |
import static com.sun.tools.javac.code.TypeTag.BOOLEAN; |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14260
diff
changeset
|
38 |
|
10 | 39 |
/** |
40 |
* Represents a field in a java class. |
|
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 |
* @see MemberDocImpl |
48 |
* |
|
49 |
* @since 1.2 |
|
50 |
* @author Robert Field |
|
51 |
* @author Neal Gafter (rewrite) |
|
52 |
* @author Scott Seligman (generics, enums, annotations) |
|
53 |
*/ |
|
54 |
public class FieldDocImpl extends MemberDocImpl implements FieldDoc { |
|
55 |
||
56 |
protected final VarSymbol sym; |
|
57 |
||
58 |
/** |
|
59 |
* Constructor. |
|
60 |
*/ |
|
14802 | 61 |
public FieldDocImpl(DocEnv env, VarSymbol sym, TreePath treePath) { |
62 |
super(env, sym, treePath); |
|
10 | 63 |
this.sym = sym; |
64 |
} |
|
65 |
||
66 |
/** |
|
67 |
* Constructor. |
|
68 |
*/ |
|
69 |
public FieldDocImpl(DocEnv env, VarSymbol sym) { |
|
14802 | 70 |
this(env, sym, null); |
10 | 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(); |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14260
diff
changeset
|
103 |
if (result != null && sym.type.hasTag(BOOLEAN)) |
10 | 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() { |
|
17275
bcaa1940863a
8012656: cache frequently used name strings for DocImpl classes
jjg
parents:
14802
diff
changeset
|
251 |
if (name == null) { |
bcaa1940863a
8012656: cache frequently used name strings for DocImpl classes
jjg
parents:
14802
diff
changeset
|
252 |
name = sym.name.toString(); |
bcaa1940863a
8012656: cache frequently used name strings for DocImpl classes
jjg
parents:
14802
diff
changeset
|
253 |
} |
bcaa1940863a
8012656: cache frequently used name strings for DocImpl classes
jjg
parents:
14802
diff
changeset
|
254 |
return name; |
10 | 255 |
} |
256 |
||
17275
bcaa1940863a
8012656: cache frequently used name strings for DocImpl classes
jjg
parents:
14802
diff
changeset
|
257 |
private String name; |
bcaa1940863a
8012656: cache frequently used name strings for DocImpl classes
jjg
parents:
14802
diff
changeset
|
258 |
|
10 | 259 |
public String qualifiedName() { |
17276 | 260 |
if (qualifiedName == null) { |
17275
bcaa1940863a
8012656: cache frequently used name strings for DocImpl classes
jjg
parents:
14802
diff
changeset
|
261 |
qualifiedName = sym.enclClass().getQualifiedName() + "." + name(); |
bcaa1940863a
8012656: cache frequently used name strings for DocImpl classes
jjg
parents:
14802
diff
changeset
|
262 |
} |
bcaa1940863a
8012656: cache frequently used name strings for DocImpl classes
jjg
parents:
14802
diff
changeset
|
263 |
return qualifiedName; |
10 | 264 |
} |
265 |
||
17275
bcaa1940863a
8012656: cache frequently used name strings for DocImpl classes
jjg
parents:
14802
diff
changeset
|
266 |
private String qualifiedName; |
bcaa1940863a
8012656: cache frequently used name strings for DocImpl classes
jjg
parents:
14802
diff
changeset
|
267 |
|
10 | 268 |
/** |
269 |
* Return the source position of the entity, or null if |
|
270 |
* no position is available. |
|
271 |
*/ |
|
8631
664f84942e74
6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents:
5520
diff
changeset
|
272 |
@Override |
10 | 273 |
public SourcePosition position() { |
274 |
if (sym.enclClass().sourcefile == null) return null; |
|
1869 | 275 |
return SourcePositionImpl.make(sym.enclClass().sourcefile, |
10 | 276 |
(tree==null) ? 0 : tree.pos, |
277 |
lineMap); |
|
278 |
} |
|
279 |
} |