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