src/share/classes/com/sun/tools/javadoc/FieldDocImpl.java

Thu, 18 Sep 2008 18:39:44 -0700

author
jjg
date
Thu, 18 Sep 2008 18:39:44 -0700
changeset 115
829dea15ff99
parent 1
9a66ca7c79fa
child 197
1bf037016426
permissions
-rw-r--r--

6744408: Extra ouput is appearing in stderr
Reviewed-by: bpatel

duke@1 1 /*
duke@1 2 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
duke@1 7 * published by the Free Software Foundation. Sun designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
duke@1 9 * by Sun in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
duke@1 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@1 22 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@1 23 * have any questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.javadoc;
duke@1 27
duke@1 28 import com.sun.javadoc.*;
duke@1 29
duke@1 30 import static com.sun.javadoc.LanguageVersion.*;
duke@1 31
duke@1 32 import com.sun.tools.javac.code.Flags;
duke@1 33 import com.sun.tools.javac.code.Symbol.ClassSymbol;
duke@1 34 import com.sun.tools.javac.code.Symbol.VarSymbol;
duke@1 35 import com.sun.tools.javac.code.TypeTags;
duke@1 36
duke@1 37 import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
duke@1 38
duke@1 39 import com.sun.tools.javac.util.Position;
duke@1 40
duke@1 41 import java.lang.reflect.Modifier;
duke@1 42
duke@1 43
duke@1 44 /**
duke@1 45 * Represents a field in a java class.
duke@1 46 *
duke@1 47 * @see MemberDocImpl
duke@1 48 *
duke@1 49 * @since 1.2
duke@1 50 * @author Robert Field
duke@1 51 * @author Neal Gafter (rewrite)
duke@1 52 * @author Scott Seligman (generics, enums, annotations)
duke@1 53 */
duke@1 54 public class FieldDocImpl extends MemberDocImpl implements FieldDoc {
duke@1 55
duke@1 56 protected final VarSymbol sym;
duke@1 57
duke@1 58 /**
duke@1 59 * Constructor.
duke@1 60 */
duke@1 61 public FieldDocImpl(DocEnv env, VarSymbol sym,
duke@1 62 String rawDocs, JCVariableDecl tree, Position.LineMap lineMap) {
duke@1 63 super(env, sym, rawDocs, tree, lineMap);
duke@1 64 this.sym = sym;
duke@1 65 }
duke@1 66
duke@1 67 /**
duke@1 68 * Constructor.
duke@1 69 */
duke@1 70 public FieldDocImpl(DocEnv env, VarSymbol sym) {
duke@1 71 this(env, sym, null, null, null);
duke@1 72 }
duke@1 73
duke@1 74 /**
duke@1 75 * Returns the flags in terms of javac's flags
duke@1 76 */
duke@1 77 protected long getFlags() {
duke@1 78 return sym.flags();
duke@1 79 }
duke@1 80
duke@1 81 /**
duke@1 82 * Identify the containing class
duke@1 83 */
duke@1 84 protected ClassSymbol getContainingClass() {
duke@1 85 return sym.enclClass();
duke@1 86 }
duke@1 87
duke@1 88 /**
duke@1 89 * Get type of this field.
duke@1 90 */
duke@1 91 public com.sun.javadoc.Type type() {
duke@1 92 return TypeMaker.getType(env, sym.type, false);
duke@1 93 }
duke@1 94
duke@1 95 /**
duke@1 96 * Get the value of a constant field.
duke@1 97 *
duke@1 98 * @return the value of a constant field. The value is
duke@1 99 * automatically wrapped in an object if it has a primitive type.
duke@1 100 * If the field is not constant, returns null.
duke@1 101 */
duke@1 102 public Object constantValue() {
duke@1 103 Object result = sym.getConstValue();
duke@1 104 if (result != null && sym.type.tag == TypeTags.BOOLEAN)
duke@1 105 // javac represents false and true as Integers 0 and 1
duke@1 106 result = Boolean.valueOf(((Integer)result).intValue() != 0);
duke@1 107 return result;
duke@1 108 }
duke@1 109
duke@1 110 /**
duke@1 111 * Get the value of a constant field.
duke@1 112 *
duke@1 113 * @return the text of a Java language expression whose value
duke@1 114 * is the value of the constant. The expression uses no identifiers
duke@1 115 * other than primitive literals. If the field is
duke@1 116 * not constant, returns null.
duke@1 117 */
duke@1 118 public String constantValueExpression() {
duke@1 119 return constantValueExpression(constantValue());
duke@1 120 }
duke@1 121
duke@1 122 /**
duke@1 123 * A static version of the above.
duke@1 124 */
duke@1 125 static String constantValueExpression(Object cb) {
duke@1 126 if (cb == null) return null;
duke@1 127 if (cb instanceof Character) return sourceForm(((Character)cb).charValue());
duke@1 128 if (cb instanceof Byte) return sourceForm(((Byte)cb).byteValue());
duke@1 129 if (cb instanceof String) return sourceForm((String)cb);
duke@1 130 if (cb instanceof Double) return sourceForm(((Double)cb).doubleValue(), 'd');
duke@1 131 if (cb instanceof Float) return sourceForm(((Float)cb).doubleValue(), 'f');
duke@1 132 if (cb instanceof Long) return cb + "L";
duke@1 133 return cb.toString(); // covers int, short
duke@1 134 }
duke@1 135 // where
duke@1 136 private static String sourceForm(double v, char suffix) {
duke@1 137 if (Double.isNaN(v))
duke@1 138 return "0" + suffix + "/0" + suffix;
duke@1 139 if (v == Double.POSITIVE_INFINITY)
duke@1 140 return "1" + suffix + "/0" + suffix;
duke@1 141 if (v == Double.NEGATIVE_INFINITY)
duke@1 142 return "-1" + suffix + "/0" + suffix;
duke@1 143 return v + (suffix == 'f' || suffix == 'F' ? "" + suffix : "");
duke@1 144 }
duke@1 145 private static String sourceForm(char c) {
duke@1 146 StringBuffer buf = new StringBuffer(8);
duke@1 147 buf.append('\'');
duke@1 148 sourceChar(c, buf);
duke@1 149 buf.append('\'');
duke@1 150 return buf.toString();
duke@1 151 }
duke@1 152 private static String sourceForm(byte c) {
duke@1 153 return "0x" + Integer.toString(c & 0xff, 16);
duke@1 154 }
duke@1 155 private static String sourceForm(String s) {
duke@1 156 StringBuffer buf = new StringBuffer(s.length() + 5);
duke@1 157 buf.append('\"');
duke@1 158 for (int i=0; i<s.length(); i++) {
duke@1 159 char c = s.charAt(i);
duke@1 160 sourceChar(c, buf);
duke@1 161 }
duke@1 162 buf.append('\"');
duke@1 163 return buf.toString();
duke@1 164 }
duke@1 165 private static void sourceChar(char c, StringBuffer buf) {
duke@1 166 switch (c) {
duke@1 167 case '\b': buf.append("\\b"); return;
duke@1 168 case '\t': buf.append("\\t"); return;
duke@1 169 case '\n': buf.append("\\n"); return;
duke@1 170 case '\f': buf.append("\\f"); return;
duke@1 171 case '\r': buf.append("\\r"); return;
duke@1 172 case '\"': buf.append("\\\""); return;
duke@1 173 case '\'': buf.append("\\\'"); return;
duke@1 174 case '\\': buf.append("\\\\"); return;
duke@1 175 default:
duke@1 176 if (isPrintableAscii(c)) {
duke@1 177 buf.append(c); return;
duke@1 178 }
duke@1 179 unicodeEscape(c, buf);
duke@1 180 return;
duke@1 181 }
duke@1 182 }
duke@1 183 private static void unicodeEscape(char c, StringBuffer buf) {
duke@1 184 final String chars = "0123456789abcdef";
duke@1 185 buf.append("\\u");
duke@1 186 buf.append(chars.charAt(15 & (c>>12)));
duke@1 187 buf.append(chars.charAt(15 & (c>>8)));
duke@1 188 buf.append(chars.charAt(15 & (c>>4)));
duke@1 189 buf.append(chars.charAt(15 & (c>>0)));
duke@1 190 }
duke@1 191 private static boolean isPrintableAscii(char c) {
duke@1 192 return c >= ' ' && c <= '~';
duke@1 193 }
duke@1 194
duke@1 195 /**
duke@1 196 * Return true if this field is included in the active set.
duke@1 197 */
duke@1 198 public boolean isIncluded() {
duke@1 199 return containingClass().isIncluded() && env.shouldDocument(sym);
duke@1 200 }
duke@1 201
duke@1 202 /**
duke@1 203 * Is this Doc item a field (but not an enum constant?
duke@1 204 */
duke@1 205 public boolean isField() {
duke@1 206 return !isEnumConstant();
duke@1 207 }
duke@1 208
duke@1 209 /**
duke@1 210 * Is this Doc item an enum constant?
duke@1 211 * (For legacy doclets, return false.)
duke@1 212 */
duke@1 213 public boolean isEnumConstant() {
duke@1 214 return (getFlags() & Flags.ENUM) != 0 &&
duke@1 215 !env.legacyDoclet;
duke@1 216 }
duke@1 217
duke@1 218 /**
duke@1 219 * Return true if this field is transient
duke@1 220 */
duke@1 221 public boolean isTransient() {
duke@1 222 return Modifier.isTransient(getModifiers());
duke@1 223 }
duke@1 224
duke@1 225 /**
duke@1 226 * Return true if this field is volatile
duke@1 227 */
duke@1 228 public boolean isVolatile() {
duke@1 229 return Modifier.isVolatile(getModifiers());
duke@1 230 }
duke@1 231
duke@1 232 /**
duke@1 233 * Returns true if this field was synthesized by the compiler.
duke@1 234 */
duke@1 235 public boolean isSynthetic() {
duke@1 236 return (getFlags() & Flags.SYNTHETIC) != 0;
duke@1 237 }
duke@1 238
duke@1 239 /**
duke@1 240 * Return the serialField tags in this FieldDocImpl item.
duke@1 241 *
duke@1 242 * @return an array of <tt>SerialFieldTagImpl</tt> containing all
duke@1 243 * <code>&#64serialField</code> tags.
duke@1 244 */
duke@1 245 public SerialFieldTag[] serialFieldTags() {
duke@1 246 return comment().serialFieldTags();
duke@1 247 }
duke@1 248
duke@1 249 public String name() {
duke@1 250 return sym.name.toString();
duke@1 251 }
duke@1 252
duke@1 253 public String qualifiedName() {
duke@1 254 return sym.enclClass().getQualifiedName() + "." + name();
duke@1 255 }
duke@1 256
duke@1 257 /**
duke@1 258 * Return the source position of the entity, or null if
duke@1 259 * no position is available.
duke@1 260 */
duke@1 261 public SourcePosition position() {
duke@1 262 if (sym.enclClass().sourcefile == null) return null;
duke@1 263 return SourcePositionImpl.make(sym.enclClass().sourcefile.toString(),
duke@1 264 (tree==null) ? 0 : tree.pos,
duke@1 265 lineMap);
duke@1 266 }
duke@1 267 }

mercurial