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