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

Fri, 19 Apr 2013 11:57:46 +0200

author
jfranck
date
Fri, 19 Apr 2013 11:57:46 +0200
changeset 1707
a3655c24e232
parent 1706
95d29b99e5b3
child 1755
ddb4a2bfcd82
permissions
-rw-r--r--

8012681: Commit for JDK-8012656 breaks tl build
Reviewed-by: vromero, chegar, alanb

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

mercurial