Thu, 18 Sep 2008 18:39:44 -0700
6744408: Extra ouput is appearing in stderr
Reviewed-by: bpatel
1 /*
2 * Copyright 1997-2006 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
26 package com.sun.tools.javadoc;
28 import com.sun.javadoc.*;
30 import static com.sun.javadoc.LanguageVersion.*;
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 import java.lang.reflect.Modifier;
44 /**
45 * Represents a field in a java class.
46 *
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 {
56 protected final VarSymbol sym;
58 /**
59 * Constructor.
60 */
61 public FieldDocImpl(DocEnv env, VarSymbol sym,
62 String rawDocs, JCVariableDecl tree, Position.LineMap lineMap) {
63 super(env, sym, rawDocs, tree, lineMap);
64 this.sym = sym;
65 }
67 /**
68 * Constructor.
69 */
70 public FieldDocImpl(DocEnv env, VarSymbol sym) {
71 this(env, sym, null, null, null);
72 }
74 /**
75 * Returns the flags in terms of javac's flags
76 */
77 protected long getFlags() {
78 return sym.flags();
79 }
81 /**
82 * Identify the containing class
83 */
84 protected ClassSymbol getContainingClass() {
85 return sym.enclClass();
86 }
88 /**
89 * Get type of this field.
90 */
91 public com.sun.javadoc.Type type() {
92 return TypeMaker.getType(env, sym.type, false);
93 }
95 /**
96 * Get the value of a constant field.
97 *
98 * @return the value of a constant field. The value is
99 * automatically wrapped in an object if it has a primitive type.
100 * If the field is not constant, returns null.
101 */
102 public Object constantValue() {
103 Object result = sym.getConstValue();
104 if (result != null && sym.type.tag == TypeTags.BOOLEAN)
105 // javac represents false and true as Integers 0 and 1
106 result = Boolean.valueOf(((Integer)result).intValue() != 0);
107 return result;
108 }
110 /**
111 * Get the value of a constant field.
112 *
113 * @return the text of a Java language expression whose value
114 * is the value of the constant. The expression uses no identifiers
115 * other than primitive literals. If the field is
116 * not constant, returns null.
117 */
118 public String constantValueExpression() {
119 return constantValueExpression(constantValue());
120 }
122 /**
123 * A static version of the above.
124 */
125 static String constantValueExpression(Object cb) {
126 if (cb == null) return null;
127 if (cb instanceof Character) return sourceForm(((Character)cb).charValue());
128 if (cb instanceof Byte) return sourceForm(((Byte)cb).byteValue());
129 if (cb instanceof String) return sourceForm((String)cb);
130 if (cb instanceof Double) return sourceForm(((Double)cb).doubleValue(), 'd');
131 if (cb instanceof Float) return sourceForm(((Float)cb).doubleValue(), 'f');
132 if (cb instanceof Long) return cb + "L";
133 return cb.toString(); // covers int, short
134 }
135 // where
136 private static String sourceForm(double v, char suffix) {
137 if (Double.isNaN(v))
138 return "0" + suffix + "/0" + suffix;
139 if (v == Double.POSITIVE_INFINITY)
140 return "1" + suffix + "/0" + suffix;
141 if (v == Double.NEGATIVE_INFINITY)
142 return "-1" + suffix + "/0" + suffix;
143 return v + (suffix == 'f' || suffix == 'F' ? "" + suffix : "");
144 }
145 private static String sourceForm(char c) {
146 StringBuffer buf = new StringBuffer(8);
147 buf.append('\'');
148 sourceChar(c, buf);
149 buf.append('\'');
150 return buf.toString();
151 }
152 private static String sourceForm(byte c) {
153 return "0x" + Integer.toString(c & 0xff, 16);
154 }
155 private static String sourceForm(String s) {
156 StringBuffer buf = new StringBuffer(s.length() + 5);
157 buf.append('\"');
158 for (int i=0; i<s.length(); i++) {
159 char c = s.charAt(i);
160 sourceChar(c, buf);
161 }
162 buf.append('\"');
163 return buf.toString();
164 }
165 private static void sourceChar(char c, StringBuffer buf) {
166 switch (c) {
167 case '\b': buf.append("\\b"); return;
168 case '\t': buf.append("\\t"); return;
169 case '\n': buf.append("\\n"); return;
170 case '\f': buf.append("\\f"); return;
171 case '\r': buf.append("\\r"); return;
172 case '\"': buf.append("\\\""); return;
173 case '\'': buf.append("\\\'"); return;
174 case '\\': buf.append("\\\\"); return;
175 default:
176 if (isPrintableAscii(c)) {
177 buf.append(c); return;
178 }
179 unicodeEscape(c, buf);
180 return;
181 }
182 }
183 private static void unicodeEscape(char c, StringBuffer buf) {
184 final String chars = "0123456789abcdef";
185 buf.append("\\u");
186 buf.append(chars.charAt(15 & (c>>12)));
187 buf.append(chars.charAt(15 & (c>>8)));
188 buf.append(chars.charAt(15 & (c>>4)));
189 buf.append(chars.charAt(15 & (c>>0)));
190 }
191 private static boolean isPrintableAscii(char c) {
192 return c >= ' ' && c <= '~';
193 }
195 /**
196 * Return true if this field is included in the active set.
197 */
198 public boolean isIncluded() {
199 return containingClass().isIncluded() && env.shouldDocument(sym);
200 }
202 /**
203 * Is this Doc item a field (but not an enum constant?
204 */
205 public boolean isField() {
206 return !isEnumConstant();
207 }
209 /**
210 * Is this Doc item an enum constant?
211 * (For legacy doclets, return false.)
212 */
213 public boolean isEnumConstant() {
214 return (getFlags() & Flags.ENUM) != 0 &&
215 !env.legacyDoclet;
216 }
218 /**
219 * Return true if this field is transient
220 */
221 public boolean isTransient() {
222 return Modifier.isTransient(getModifiers());
223 }
225 /**
226 * Return true if this field is volatile
227 */
228 public boolean isVolatile() {
229 return Modifier.isVolatile(getModifiers());
230 }
232 /**
233 * Returns true if this field was synthesized by the compiler.
234 */
235 public boolean isSynthetic() {
236 return (getFlags() & Flags.SYNTHETIC) != 0;
237 }
239 /**
240 * Return the serialField tags in this FieldDocImpl item.
241 *
242 * @return an array of <tt>SerialFieldTagImpl</tt> containing all
243 * <code>@serialField</code> tags.
244 */
245 public SerialFieldTag[] serialFieldTags() {
246 return comment().serialFieldTags();
247 }
249 public String name() {
250 return sym.name.toString();
251 }
253 public String qualifiedName() {
254 return sym.enclClass().getQualifiedName() + "." + name();
255 }
257 /**
258 * Return the source position of the entity, or null if
259 * no position is available.
260 */
261 public SourcePosition position() {
262 if (sym.enclClass().sourcefile == null) return null;
263 return SourcePositionImpl.make(sym.enclClass().sourcefile.toString(),
264 (tree==null) ? 0 : tree.pos,
265 lineMap);
266 }
267 }