duke@1: /* jjg@1706: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as ohair@554: * published by the Free Software Foundation. Oracle designates this duke@1: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. duke@1: */ duke@1: duke@1: package com.sun.tools.javadoc; duke@1: jjg@1443: import com.sun.source.util.TreePath; jjg@197: import java.lang.reflect.Modifier; jjg@197: duke@1: import com.sun.javadoc.*; duke@1: duke@1: import com.sun.tools.javac.code.Flags; duke@1: import com.sun.tools.javac.code.Symbol.ClassSymbol; duke@1: import com.sun.tools.javac.code.Symbol.VarSymbol; duke@1: jjg@1374: import static com.sun.tools.javac.code.TypeTag.BOOLEAN; jjg@1374: duke@1: /** duke@1: * Represents a field in a java class. duke@1: * jjg@1359: *

This is NOT part of any supported API. jjg@1359: * If you write code that depends on this, you do so at your own risk. jjg@1359: * This code and its internal interfaces are subject to change or jjg@1359: * deletion without notice. jjg@1359: * duke@1: * @see MemberDocImpl duke@1: * duke@1: * @since 1.2 duke@1: * @author Robert Field duke@1: * @author Neal Gafter (rewrite) duke@1: * @author Scott Seligman (generics, enums, annotations) duke@1: */ duke@1: public class FieldDocImpl extends MemberDocImpl implements FieldDoc { duke@1: duke@1: protected final VarSymbol sym; duke@1: duke@1: /** duke@1: * Constructor. duke@1: */ jjg@1443: public FieldDocImpl(DocEnv env, VarSymbol sym, TreePath treePath) { jjg@1443: super(env, sym, treePath); duke@1: this.sym = sym; duke@1: } duke@1: duke@1: /** duke@1: * Constructor. duke@1: */ duke@1: public FieldDocImpl(DocEnv env, VarSymbol sym) { jjg@1443: this(env, sym, null); duke@1: } duke@1: duke@1: /** duke@1: * Returns the flags in terms of javac's flags duke@1: */ duke@1: protected long getFlags() { duke@1: return sym.flags(); duke@1: } duke@1: duke@1: /** duke@1: * Identify the containing class duke@1: */ duke@1: protected ClassSymbol getContainingClass() { duke@1: return sym.enclClass(); duke@1: } duke@1: duke@1: /** duke@1: * Get type of this field. duke@1: */ duke@1: public com.sun.javadoc.Type type() { duke@1: return TypeMaker.getType(env, sym.type, false); duke@1: } duke@1: duke@1: /** duke@1: * Get the value of a constant field. duke@1: * duke@1: * @return the value of a constant field. The value is duke@1: * automatically wrapped in an object if it has a primitive type. duke@1: * If the field is not constant, returns null. duke@1: */ duke@1: public Object constantValue() { duke@1: Object result = sym.getConstValue(); jjg@1374: if (result != null && sym.type.hasTag(BOOLEAN)) duke@1: // javac represents false and true as Integers 0 and 1 duke@1: result = Boolean.valueOf(((Integer)result).intValue() != 0); duke@1: return result; duke@1: } duke@1: duke@1: /** duke@1: * Get the value of a constant field. duke@1: * duke@1: * @return the text of a Java language expression whose value duke@1: * is the value of the constant. The expression uses no identifiers duke@1: * other than primitive literals. If the field is duke@1: * not constant, returns null. duke@1: */ duke@1: public String constantValueExpression() { duke@1: return constantValueExpression(constantValue()); duke@1: } duke@1: duke@1: /** duke@1: * A static version of the above. duke@1: */ duke@1: static String constantValueExpression(Object cb) { duke@1: if (cb == null) return null; duke@1: if (cb instanceof Character) return sourceForm(((Character)cb).charValue()); duke@1: if (cb instanceof Byte) return sourceForm(((Byte)cb).byteValue()); duke@1: if (cb instanceof String) return sourceForm((String)cb); duke@1: if (cb instanceof Double) return sourceForm(((Double)cb).doubleValue(), 'd'); duke@1: if (cb instanceof Float) return sourceForm(((Float)cb).doubleValue(), 'f'); duke@1: if (cb instanceof Long) return cb + "L"; duke@1: return cb.toString(); // covers int, short duke@1: } duke@1: // where duke@1: private static String sourceForm(double v, char suffix) { duke@1: if (Double.isNaN(v)) duke@1: return "0" + suffix + "/0" + suffix; duke@1: if (v == Double.POSITIVE_INFINITY) duke@1: return "1" + suffix + "/0" + suffix; duke@1: if (v == Double.NEGATIVE_INFINITY) duke@1: return "-1" + suffix + "/0" + suffix; duke@1: return v + (suffix == 'f' || suffix == 'F' ? "" + suffix : ""); duke@1: } duke@1: private static String sourceForm(char c) { jjg@910: StringBuilder buf = new StringBuilder(8); duke@1: buf.append('\''); duke@1: sourceChar(c, buf); duke@1: buf.append('\''); duke@1: return buf.toString(); duke@1: } duke@1: private static String sourceForm(byte c) { duke@1: return "0x" + Integer.toString(c & 0xff, 16); duke@1: } duke@1: private static String sourceForm(String s) { jjg@910: StringBuilder buf = new StringBuilder(s.length() + 5); duke@1: buf.append('\"'); duke@1: for (int i=0; i>12))); duke@1: buf.append(chars.charAt(15 & (c>>8))); duke@1: buf.append(chars.charAt(15 & (c>>4))); duke@1: buf.append(chars.charAt(15 & (c>>0))); duke@1: } duke@1: private static boolean isPrintableAscii(char c) { duke@1: return c >= ' ' && c <= '~'; duke@1: } duke@1: duke@1: /** duke@1: * Return true if this field is included in the active set. duke@1: */ duke@1: public boolean isIncluded() { duke@1: return containingClass().isIncluded() && env.shouldDocument(sym); duke@1: } duke@1: duke@1: /** duke@1: * Is this Doc item a field (but not an enum constant? duke@1: */ jjg@910: @Override duke@1: public boolean isField() { duke@1: return !isEnumConstant(); duke@1: } duke@1: duke@1: /** duke@1: * Is this Doc item an enum constant? duke@1: * (For legacy doclets, return false.) duke@1: */ jjg@910: @Override duke@1: public boolean isEnumConstant() { duke@1: return (getFlags() & Flags.ENUM) != 0 && duke@1: !env.legacyDoclet; duke@1: } duke@1: duke@1: /** duke@1: * Return true if this field is transient duke@1: */ duke@1: public boolean isTransient() { duke@1: return Modifier.isTransient(getModifiers()); duke@1: } duke@1: duke@1: /** duke@1: * Return true if this field is volatile duke@1: */ duke@1: public boolean isVolatile() { duke@1: return Modifier.isVolatile(getModifiers()); duke@1: } duke@1: duke@1: /** duke@1: * Returns true if this field was synthesized by the compiler. duke@1: */ duke@1: public boolean isSynthetic() { duke@1: return (getFlags() & Flags.SYNTHETIC) != 0; duke@1: } duke@1: duke@1: /** duke@1: * Return the serialField tags in this FieldDocImpl item. duke@1: * duke@1: * @return an array of SerialFieldTagImpl containing all jjg@1326: * @serialField tags. duke@1: */ duke@1: public SerialFieldTag[] serialFieldTags() { duke@1: return comment().serialFieldTags(); duke@1: } duke@1: duke@1: public String name() { jjg@1706: if (name == null) { jjg@1706: name = sym.name.toString(); jjg@1706: } jjg@1706: return name; duke@1: } duke@1: jjg@1706: private String name; jjg@1706: duke@1: public String qualifiedName() { jfranck@1707: if (qualifiedName == null) { jjg@1706: qualifiedName = sym.enclClass().getQualifiedName() + "." + name(); jjg@1706: } jjg@1706: return qualifiedName; duke@1: } duke@1: jjg@1706: private String qualifiedName; jjg@1706: duke@1: /** duke@1: * Return the source position of the entity, or null if duke@1: * no position is available. duke@1: */ jjg@910: @Override duke@1: public SourcePosition position() { duke@1: if (sym.enclClass().sourcefile == null) return null; jjg@197: return SourcePositionImpl.make(sym.enclClass().sourcefile, duke@1: (tree==null) ? 0 : tree.pos, duke@1: lineMap); duke@1: } duke@1: }