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