Fri, 30 Nov 2012 15:14:48 +0000
8002099: Add support for intersection types in cast expression
Summary: Add parser and type-checking support for intersection types in cast expressions
Reviewed-by: jjg
1 /*
2 * Copyright (c) 1997, 2012, 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 java.lang.reflect.Modifier;
30 import com.sun.javadoc.*;
32 import com.sun.tools.javac.code.Flags;
33 import com.sun.tools.javac.code.Symbol.ClassSymbol;
34 import com.sun.tools.javac.code.Symbol.VarSymbol;
36 import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
38 import com.sun.tools.javac.util.Position;
40 import static com.sun.tools.javac.code.TypeTag.BOOLEAN;
42 /**
43 * Represents a field in a java class.
44 *
45 * <p><b>This is NOT part of any supported API.
46 * If you write code that depends on this, you do so at your own risk.
47 * This code and its internal interfaces are subject to change or
48 * deletion without notice.</b>
49 *
50 * @see MemberDocImpl
51 *
52 * @since 1.2
53 * @author Robert Field
54 * @author Neal Gafter (rewrite)
55 * @author Scott Seligman (generics, enums, annotations)
56 */
57 public class FieldDocImpl extends MemberDocImpl implements FieldDoc {
59 protected final VarSymbol sym;
61 /**
62 * Constructor.
63 */
64 public FieldDocImpl(DocEnv env, VarSymbol sym,
65 String rawDocs, JCVariableDecl tree, Position.LineMap lineMap) {
66 super(env, sym, rawDocs, tree, lineMap);
67 this.sym = sym;
68 }
70 /**
71 * Constructor.
72 */
73 public FieldDocImpl(DocEnv env, VarSymbol sym) {
74 this(env, sym, null, null, 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>@serialField</code> tags.
249 */
250 public SerialFieldTag[] serialFieldTags() {
251 return comment().serialFieldTags();
252 }
254 public String name() {
255 return sym.name.toString();
256 }
258 public String qualifiedName() {
259 return sym.enclClass().getQualifiedName() + "." + name();
260 }
262 /**
263 * Return the source position of the entity, or null if
264 * no position is available.
265 */
266 @Override
267 public SourcePosition position() {
268 if (sym.enclClass().sourcefile == null) return null;
269 return SourcePositionImpl.make(sym.enclClass().sourcefile,
270 (tree==null) ? 0 : tree.pos,
271 lineMap);
272 }
273 }