Thu, 12 Jan 2012 15:28:34 +0000
7123100: javac fails with java.lang.StackOverflowError
Summary: Inference of under-constrained type-variables creates erroneous recursive wildcard types
Reviewed-by: jjg
1 /*
2 * Copyright (c) 1997, 2011, 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 static com.sun.javadoc.LanguageVersion.*;
34 import com.sun.tools.javac.code.Flags;
35 import com.sun.tools.javac.code.Symbol.ClassSymbol;
36 import com.sun.tools.javac.code.Symbol.VarSymbol;
37 import com.sun.tools.javac.code.TypeTags;
39 import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
41 import com.sun.tools.javac.util.Position;
43 /**
44 * Represents a field in a java class.
45 *
46 * @see MemberDocImpl
47 *
48 * @since 1.2
49 * @author Robert Field
50 * @author Neal Gafter (rewrite)
51 * @author Scott Seligman (generics, enums, annotations)
52 */
53 public class FieldDocImpl extends MemberDocImpl implements FieldDoc {
55 protected final VarSymbol sym;
57 /**
58 * Constructor.
59 */
60 public FieldDocImpl(DocEnv env, VarSymbol sym,
61 String rawDocs, JCVariableDecl tree, Position.LineMap lineMap) {
62 super(env, sym, rawDocs, tree, lineMap);
63 this.sym = sym;
64 }
66 /**
67 * Constructor.
68 */
69 public FieldDocImpl(DocEnv env, VarSymbol sym) {
70 this(env, sym, null, null, null);
71 }
73 /**
74 * Returns the flags in terms of javac's flags
75 */
76 protected long getFlags() {
77 return sym.flags();
78 }
80 /**
81 * Identify the containing class
82 */
83 protected ClassSymbol getContainingClass() {
84 return sym.enclClass();
85 }
87 /**
88 * Get type of this field.
89 */
90 public com.sun.javadoc.Type type() {
91 return TypeMaker.getType(env, sym.type, false);
92 }
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.tag == TypeTags.BOOLEAN)
104 // javac represents false and true as Integers 0 and 1
105 result = Boolean.valueOf(((Integer)result).intValue() != 0);
106 return result;
107 }
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 }
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 }
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 }
201 /**
202 * Is this Doc item a field (but not an enum constant?
203 */
204 @Override
205 public boolean isField() {
206 return !isEnumConstant();
207 }
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 }
219 /**
220 * Return true if this field is transient
221 */
222 public boolean isTransient() {
223 return Modifier.isTransient(getModifiers());
224 }
226 /**
227 * Return true if this field is volatile
228 */
229 public boolean isVolatile() {
230 return Modifier.isVolatile(getModifiers());
231 }
233 /**
234 * Returns true if this field was synthesized by the compiler.
235 */
236 public boolean isSynthetic() {
237 return (getFlags() & Flags.SYNTHETIC) != 0;
238 }
240 /**
241 * Return the serialField tags in this FieldDocImpl item.
242 *
243 * @return an array of <tt>SerialFieldTagImpl</tt> containing all
244 * <code>@serialField</code> tags.
245 */
246 public SerialFieldTag[] serialFieldTags() {
247 return comment().serialFieldTags();
248 }
250 public String name() {
251 return sym.name.toString();
252 }
254 public String qualifiedName() {
255 return sym.enclClass().getQualifiedName() + "." + name();
256 }
258 /**
259 * Return the source position of the entity, or null if
260 * no position is available.
261 */
262 @Override
263 public SourcePosition position() {
264 if (sym.enclClass().sourcefile == null) return null;
265 return SourcePositionImpl.make(sym.enclClass().sourcefile,
266 (tree==null) ? 0 : tree.pos,
267 lineMap);
268 }
269 }