0N/A/*
909N/A * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
553N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
553N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
553N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
553N/A * or visit www.oracle.com if you need additional information or have any
553N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.tools.javadoc;
0N/A
196N/Aimport java.lang.reflect.Modifier;
196N/A
0N/Aimport com.sun.javadoc.*;
0N/A
0N/Aimport static com.sun.javadoc.LanguageVersion.*;
0N/A
0N/Aimport com.sun.tools.javac.code.Flags;
0N/Aimport com.sun.tools.javac.code.Symbol.ClassSymbol;
0N/Aimport com.sun.tools.javac.code.Symbol.VarSymbol;
0N/Aimport com.sun.tools.javac.code.TypeTags;
0N/A
0N/Aimport com.sun.tools.javac.tree.JCTree.JCVariableDecl;
0N/A
0N/Aimport com.sun.tools.javac.util.Position;
0N/A
0N/A/**
0N/A * Represents a field in a java class.
0N/A *
0N/A * @see MemberDocImpl
0N/A *
0N/A * @since 1.2
0N/A * @author Robert Field
0N/A * @author Neal Gafter (rewrite)
0N/A * @author Scott Seligman (generics, enums, annotations)
0N/A */
0N/Apublic class FieldDocImpl extends MemberDocImpl implements FieldDoc {
0N/A
0N/A protected final VarSymbol sym;
0N/A
0N/A /**
0N/A * Constructor.
0N/A */
0N/A public FieldDocImpl(DocEnv env, VarSymbol sym,
0N/A String rawDocs, JCVariableDecl tree, Position.LineMap lineMap) {
0N/A super(env, sym, rawDocs, tree, lineMap);
0N/A this.sym = sym;
0N/A }
0N/A
0N/A /**
0N/A * Constructor.
0N/A */
0N/A public FieldDocImpl(DocEnv env, VarSymbol sym) {
0N/A this(env, sym, null, null, null);
0N/A }
0N/A
0N/A /**
0N/A * Returns the flags in terms of javac's flags
0N/A */
0N/A protected long getFlags() {
0N/A return sym.flags();
0N/A }
0N/A
0N/A /**
0N/A * Identify the containing class
0N/A */
0N/A protected ClassSymbol getContainingClass() {
0N/A return sym.enclClass();
0N/A }
0N/A
0N/A /**
0N/A * Get type of this field.
0N/A */
0N/A public com.sun.javadoc.Type type() {
0N/A return TypeMaker.getType(env, sym.type, false);
0N/A }
0N/A
0N/A /**
0N/A * Get the value of a constant field.
0N/A *
0N/A * @return the value of a constant field. The value is
0N/A * automatically wrapped in an object if it has a primitive type.
0N/A * If the field is not constant, returns null.
0N/A */
0N/A public Object constantValue() {
0N/A Object result = sym.getConstValue();
0N/A if (result != null && sym.type.tag == TypeTags.BOOLEAN)
0N/A // javac represents false and true as Integers 0 and 1
0N/A result = Boolean.valueOf(((Integer)result).intValue() != 0);
0N/A return result;
0N/A }
0N/A
0N/A /**
0N/A * Get the value of a constant field.
0N/A *
0N/A * @return the text of a Java language expression whose value
0N/A * is the value of the constant. The expression uses no identifiers
0N/A * other than primitive literals. If the field is
0N/A * not constant, returns null.
0N/A */
0N/A public String constantValueExpression() {
0N/A return constantValueExpression(constantValue());
0N/A }
0N/A
0N/A /**
0N/A * A static version of the above.
0N/A */
0N/A static String constantValueExpression(Object cb) {
0N/A if (cb == null) return null;
0N/A if (cb instanceof Character) return sourceForm(((Character)cb).charValue());
0N/A if (cb instanceof Byte) return sourceForm(((Byte)cb).byteValue());
0N/A if (cb instanceof String) return sourceForm((String)cb);
0N/A if (cb instanceof Double) return sourceForm(((Double)cb).doubleValue(), 'd');
0N/A if (cb instanceof Float) return sourceForm(((Float)cb).doubleValue(), 'f');
0N/A if (cb instanceof Long) return cb + "L";
0N/A return cb.toString(); // covers int, short
0N/A }
0N/A // where
0N/A private static String sourceForm(double v, char suffix) {
0N/A if (Double.isNaN(v))
0N/A return "0" + suffix + "/0" + suffix;
0N/A if (v == Double.POSITIVE_INFINITY)
0N/A return "1" + suffix + "/0" + suffix;
0N/A if (v == Double.NEGATIVE_INFINITY)
0N/A return "-1" + suffix + "/0" + suffix;
0N/A return v + (suffix == 'f' || suffix == 'F' ? "" + suffix : "");
0N/A }
0N/A private static String sourceForm(char c) {
909N/A StringBuilder buf = new StringBuilder(8);
0N/A buf.append('\'');
0N/A sourceChar(c, buf);
0N/A buf.append('\'');
0N/A return buf.toString();
0N/A }
0N/A private static String sourceForm(byte c) {
0N/A return "0x" + Integer.toString(c & 0xff, 16);
0N/A }
0N/A private static String sourceForm(String s) {
909N/A StringBuilder buf = new StringBuilder(s.length() + 5);
0N/A buf.append('\"');
0N/A for (int i=0; i<s.length(); i++) {
0N/A char c = s.charAt(i);
0N/A sourceChar(c, buf);
0N/A }
0N/A buf.append('\"');
0N/A return buf.toString();
0N/A }
909N/A private static void sourceChar(char c, StringBuilder buf) {
0N/A switch (c) {
0N/A case '\b': buf.append("\\b"); return;
0N/A case '\t': buf.append("\\t"); return;
0N/A case '\n': buf.append("\\n"); return;
0N/A case '\f': buf.append("\\f"); return;
0N/A case '\r': buf.append("\\r"); return;
0N/A case '\"': buf.append("\\\""); return;
0N/A case '\'': buf.append("\\\'"); return;
0N/A case '\\': buf.append("\\\\"); return;
0N/A default:
0N/A if (isPrintableAscii(c)) {
0N/A buf.append(c); return;
0N/A }
0N/A unicodeEscape(c, buf);
0N/A return;
0N/A }
0N/A }
909N/A private static void unicodeEscape(char c, StringBuilder buf) {
0N/A final String chars = "0123456789abcdef";
0N/A buf.append("\\u");
0N/A buf.append(chars.charAt(15 & (c>>12)));
0N/A buf.append(chars.charAt(15 & (c>>8)));
0N/A buf.append(chars.charAt(15 & (c>>4)));
0N/A buf.append(chars.charAt(15 & (c>>0)));
0N/A }
0N/A private static boolean isPrintableAscii(char c) {
0N/A return c >= ' ' && c <= '~';
0N/A }
0N/A
0N/A /**
0N/A * Return true if this field is included in the active set.
0N/A */
0N/A public boolean isIncluded() {
0N/A return containingClass().isIncluded() && env.shouldDocument(sym);
0N/A }
0N/A
0N/A /**
0N/A * Is this Doc item a field (but not an enum constant?
0N/A */
909N/A @Override
0N/A public boolean isField() {
0N/A return !isEnumConstant();
0N/A }
0N/A
0N/A /**
0N/A * Is this Doc item an enum constant?
0N/A * (For legacy doclets, return false.)
0N/A */
909N/A @Override
0N/A public boolean isEnumConstant() {
0N/A return (getFlags() & Flags.ENUM) != 0 &&
0N/A !env.legacyDoclet;
0N/A }
0N/A
0N/A /**
0N/A * Return true if this field is transient
0N/A */
0N/A public boolean isTransient() {
0N/A return Modifier.isTransient(getModifiers());
0N/A }
0N/A
0N/A /**
0N/A * Return true if this field is volatile
0N/A */
0N/A public boolean isVolatile() {
0N/A return Modifier.isVolatile(getModifiers());
0N/A }
0N/A
0N/A /**
0N/A * Returns true if this field was synthesized by the compiler.
0N/A */
0N/A public boolean isSynthetic() {
0N/A return (getFlags() & Flags.SYNTHETIC) != 0;
0N/A }
0N/A
0N/A /**
0N/A * Return the serialField tags in this FieldDocImpl item.
0N/A *
0N/A * @return an array of <tt>SerialFieldTagImpl</tt> containing all
0N/A * <code>&#64serialField</code> tags.
0N/A */
0N/A public SerialFieldTag[] serialFieldTags() {
0N/A return comment().serialFieldTags();
0N/A }
0N/A
0N/A public String name() {
0N/A return sym.name.toString();
0N/A }
0N/A
0N/A public String qualifiedName() {
0N/A return sym.enclClass().getQualifiedName() + "." + name();
0N/A }
0N/A
0N/A /**
0N/A * Return the source position of the entity, or null if
0N/A * no position is available.
0N/A */
909N/A @Override
0N/A public SourcePosition position() {
0N/A if (sym.enclClass().sourcefile == null) return null;
196N/A return SourcePositionImpl.make(sym.enclClass().sourcefile,
0N/A (tree==null) ? 0 : tree.pos,
0N/A lineMap);
0N/A }
0N/A}