Tue, 11 Aug 2009 01:13:14 +0100
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
Summary: javac should warn/complain about identifiers with the same name as synthetic symbol
Reviewed-by: jjg
1 /*
2 * Copyright 1997-2009 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any 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 StringBuffer buf = new StringBuffer(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 StringBuffer buf = new StringBuffer(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, StringBuffer 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, StringBuffer 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 public boolean isField() {
205 return !isEnumConstant();
206 }
208 /**
209 * Is this Doc item an enum constant?
210 * (For legacy doclets, return false.)
211 */
212 public boolean isEnumConstant() {
213 return (getFlags() & Flags.ENUM) != 0 &&
214 !env.legacyDoclet;
215 }
217 /**
218 * Return true if this field is transient
219 */
220 public boolean isTransient() {
221 return Modifier.isTransient(getModifiers());
222 }
224 /**
225 * Return true if this field is volatile
226 */
227 public boolean isVolatile() {
228 return Modifier.isVolatile(getModifiers());
229 }
231 /**
232 * Returns true if this field was synthesized by the compiler.
233 */
234 public boolean isSynthetic() {
235 return (getFlags() & Flags.SYNTHETIC) != 0;
236 }
238 /**
239 * Return the serialField tags in this FieldDocImpl item.
240 *
241 * @return an array of <tt>SerialFieldTagImpl</tt> containing all
242 * <code>@serialField</code> tags.
243 */
244 public SerialFieldTag[] serialFieldTags() {
245 return comment().serialFieldTags();
246 }
248 public String name() {
249 return sym.name.toString();
250 }
252 public String qualifiedName() {
253 return sym.enclClass().getQualifiedName() + "." + name();
254 }
256 /**
257 * Return the source position of the entity, or null if
258 * no position is available.
259 */
260 public SourcePosition position() {
261 if (sym.enclClass().sourcefile == null) return null;
262 return SourcePositionImpl.make(sym.enclClass().sourcefile,
263 (tree==null) ? 0 : tree.pos,
264 lineMap);
265 }
266 }