Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/src/share/classes/com/sun/tools/javadoc/FieldDocImpl.java
38899 views
/*1* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.tools.javadoc;2627import com.sun.source.util.TreePath;28import java.lang.reflect.Modifier;2930import com.sun.javadoc.*;3132import com.sun.tools.javac.code.Flags;33import com.sun.tools.javac.code.Symbol.ClassSymbol;34import com.sun.tools.javac.code.Symbol.VarSymbol;3536import static com.sun.tools.javac.code.TypeTag.BOOLEAN;3738/**39* Represents a field in a java class.40*41* <p><b>This is NOT part of any supported API.42* If you write code that depends on this, you do so at your own risk.43* This code and its internal interfaces are subject to change or44* deletion without notice.</b>45*46* @see MemberDocImpl47*48* @since 1.249* @author Robert Field50* @author Neal Gafter (rewrite)51* @author Scott Seligman (generics, enums, annotations)52*/53public class FieldDocImpl extends MemberDocImpl implements FieldDoc {5455protected final VarSymbol sym;5657/**58* Constructor.59*/60public FieldDocImpl(DocEnv env, VarSymbol sym, TreePath treePath) {61super(env, sym, treePath);62this.sym = sym;63}6465/**66* Constructor.67*/68public FieldDocImpl(DocEnv env, VarSymbol sym) {69this(env, sym, null);70}7172/**73* Returns the flags in terms of javac's flags74*/75protected long getFlags() {76return sym.flags();77}7879/**80* Identify the containing class81*/82protected ClassSymbol getContainingClass() {83return sym.enclClass();84}8586/**87* Get type of this field.88*/89public com.sun.javadoc.Type type() {90return TypeMaker.getType(env, sym.type, false);91}9293/**94* Get the value of a constant field.95*96* @return the value of a constant field. The value is97* automatically wrapped in an object if it has a primitive type.98* If the field is not constant, returns null.99*/100public Object constantValue() {101Object result = sym.getConstValue();102if (result != null && sym.type.hasTag(BOOLEAN))103// javac represents false and true as Integers 0 and 1104result = Boolean.valueOf(((Integer)result).intValue() != 0);105return result;106}107108/**109* Get the value of a constant field.110*111* @return the text of a Java language expression whose value112* is the value of the constant. The expression uses no identifiers113* other than primitive literals. If the field is114* not constant, returns null.115*/116public String constantValueExpression() {117return constantValueExpression(constantValue());118}119120/**121* A static version of the above.122*/123static String constantValueExpression(Object cb) {124if (cb == null) return null;125if (cb instanceof Character) return sourceForm(((Character)cb).charValue());126if (cb instanceof Byte) return sourceForm(((Byte)cb).byteValue());127if (cb instanceof String) return sourceForm((String)cb);128if (cb instanceof Double) return sourceForm(((Double)cb).doubleValue(), 'd');129if (cb instanceof Float) return sourceForm(((Float)cb).doubleValue(), 'f');130if (cb instanceof Long) return cb + "L";131return cb.toString(); // covers int, short132}133// where134private static String sourceForm(double v, char suffix) {135if (Double.isNaN(v))136return "0" + suffix + "/0" + suffix;137if (v == Double.POSITIVE_INFINITY)138return "1" + suffix + "/0" + suffix;139if (v == Double.NEGATIVE_INFINITY)140return "-1" + suffix + "/0" + suffix;141return v + (suffix == 'f' || suffix == 'F' ? "" + suffix : "");142}143private static String sourceForm(char c) {144StringBuilder buf = new StringBuilder(8);145buf.append('\'');146sourceChar(c, buf);147buf.append('\'');148return buf.toString();149}150private static String sourceForm(byte c) {151return "0x" + Integer.toString(c & 0xff, 16);152}153private static String sourceForm(String s) {154StringBuilder buf = new StringBuilder(s.length() + 5);155buf.append('\"');156for (int i=0; i<s.length(); i++) {157char c = s.charAt(i);158sourceChar(c, buf);159}160buf.append('\"');161return buf.toString();162}163private static void sourceChar(char c, StringBuilder buf) {164switch (c) {165case '\b': buf.append("\\b"); return;166case '\t': buf.append("\\t"); return;167case '\n': buf.append("\\n"); return;168case '\f': buf.append("\\f"); return;169case '\r': buf.append("\\r"); return;170case '\"': buf.append("\\\""); return;171case '\'': buf.append("\\\'"); return;172case '\\': buf.append("\\\\"); return;173default:174if (isPrintableAscii(c)) {175buf.append(c); return;176}177unicodeEscape(c, buf);178return;179}180}181private static void unicodeEscape(char c, StringBuilder buf) {182final String chars = "0123456789abcdef";183buf.append("\\u");184buf.append(chars.charAt(15 & (c>>12)));185buf.append(chars.charAt(15 & (c>>8)));186buf.append(chars.charAt(15 & (c>>4)));187buf.append(chars.charAt(15 & (c>>0)));188}189private static boolean isPrintableAscii(char c) {190return c >= ' ' && c <= '~';191}192193/**194* Return true if this field is included in the active set.195*/196public boolean isIncluded() {197return containingClass().isIncluded() && env.shouldDocument(sym);198}199200/**201* Is this Doc item a field (but not an enum constant?202*/203@Override204public boolean isField() {205return !isEnumConstant();206}207208/**209* Is this Doc item an enum constant?210* (For legacy doclets, return false.)211*/212@Override213public boolean isEnumConstant() {214return (getFlags() & Flags.ENUM) != 0 &&215!env.legacyDoclet;216}217218/**219* Return true if this field is transient220*/221public boolean isTransient() {222return Modifier.isTransient(getModifiers());223}224225/**226* Return true if this field is volatile227*/228public boolean isVolatile() {229return Modifier.isVolatile(getModifiers());230}231232/**233* Returns true if this field was synthesized by the compiler.234*/235public boolean isSynthetic() {236return (getFlags() & Flags.SYNTHETIC) != 0;237}238239/**240* Return the serialField tags in this FieldDocImpl item.241*242* @return an array of <tt>SerialFieldTagImpl</tt> containing all243* <code>@serialField</code> tags.244*/245public SerialFieldTag[] serialFieldTags() {246return comment().serialFieldTags();247}248249public String name() {250if (name == null) {251name = sym.name.toString();252}253return name;254}255256private String name;257258public String qualifiedName() {259if (qualifiedName == null) {260qualifiedName = sym.enclClass().getQualifiedName() + "." + name();261}262return qualifiedName;263}264265private String qualifiedName;266267/**268* Return the source position of the entity, or null if269* no position is available.270*/271@Override272public SourcePosition position() {273if (sym.enclClass().sourcefile == null) return null;274return SourcePositionImpl.make(sym.enclClass().sourcefile,275(tree==null) ? 0 : tree.pos,276lineMap);277}278}279280281