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

Fri, 04 Mar 2011 19:53:03 -0800

author
jjg
date
Fri, 04 Mar 2011 19:53:03 -0800
changeset 910
ebf7c13df6c0
parent 554
9d9f26857129
child 1326
30c36e23f154
permissions
-rw-r--r--

6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
Reviewed-by: bpatel

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

mercurial