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;
29 import java.text.CollationKey;
31 import com.sun.javadoc.*;
33 import com.sun.tools.javac.code.Flags;
34 import com.sun.tools.javac.code.Symbol.*;
35 import com.sun.tools.javac.code.Type;
36 import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
37 import com.sun.tools.javac.util.List;
38 import com.sun.tools.javac.util.ListBuffer;
39 import com.sun.tools.javac.util.Position;
41 /**
42 * Represents a method or constructor of a java class.
43 *
44 * @since 1.2
45 * @author Robert Field
46 * @author Neal Gafter (rewrite)
47 * @author Scott Seligman (generics, annotations)
48 */
50 public abstract class ExecutableMemberDocImpl
51 extends MemberDocImpl implements ExecutableMemberDoc {
53 protected final MethodSymbol sym;
55 /**
56 * Constructor.
57 */
58 public ExecutableMemberDocImpl(DocEnv env, MethodSymbol sym,
59 String rawDocs, JCMethodDecl tree, Position.LineMap lineMap) {
60 super(env, sym, rawDocs, tree, lineMap);
61 this.sym = sym;
62 }
64 /**
65 * Constructor.
66 */
67 public ExecutableMemberDocImpl(DocEnv env, MethodSymbol sym) {
68 this(env, sym, null, null, null);
69 }
71 /**
72 * Returns the flags in terms of javac's flags
73 */
74 protected long getFlags() {
75 return sym.flags();
76 }
78 /**
79 * Identify the containing class
80 */
81 protected ClassSymbol getContainingClass() {
82 return sym.enclClass();
83 }
85 /**
86 * Return true if this method is native
87 */
88 public boolean isNative() {
89 return Modifier.isNative(getModifiers());
90 }
92 /**
93 * Return true if this method is synchronized
94 */
95 public boolean isSynchronized() {
96 return Modifier.isSynchronized(getModifiers());
97 }
99 /**
100 * Return true if this method was declared to take a variable number
101 * of arguments.
102 */
103 public boolean isVarArgs() {
104 return ((sym.flags() & Flags.VARARGS) != 0
105 && !env.legacyDoclet);
106 }
108 /**
109 * Returns true if this field was synthesized by the compiler.
110 */
111 public boolean isSynthetic() {
112 return ((sym.flags() & Flags.SYNTHETIC) != 0);
113 }
115 public boolean isIncluded() {
116 return containingClass().isIncluded() && env.shouldDocument(sym);
117 }
119 /**
120 * Return the throws tags in this method.
121 *
122 * @return an array of ThrowTagImpl containing all {@code @exception}
123 * and {@code @throws} tags.
124 */
125 public ThrowsTag[] throwsTags() {
126 return comment().throwsTags();
127 }
129 /**
130 * Return the param tags in this method, excluding the type
131 * parameter tags.
132 *
133 * @return an array of ParamTagImpl containing all {@code @param} tags.
134 */
135 public ParamTag[] paramTags() {
136 return comment().paramTags();
137 }
139 /**
140 * Return the type parameter tags in this method.
141 */
142 public ParamTag[] typeParamTags() {
143 return env.legacyDoclet
144 ? new ParamTag[0]
145 : comment().typeParamTags();
146 }
148 /**
149 * Return exceptions this method or constructor throws.
150 *
151 * @return an array of ClassDoc[] representing the exceptions
152 * thrown by this method.
153 */
154 public ClassDoc[] thrownExceptions() {
155 ListBuffer<ClassDocImpl> l = new ListBuffer<ClassDocImpl>();
156 for (Type ex : sym.type.getThrownTypes()) {
157 ex = env.types.erasure(ex);
158 //### Will these casts succeed in the face of static semantic
159 //### errors in the documented code?
160 ClassDocImpl cdi = env.getClassDoc((ClassSymbol)ex.tsym);
161 if (cdi != null) l.append(cdi);
162 }
163 return l.toArray(new ClassDocImpl[l.length()]);
164 }
166 /**
167 * Return exceptions this method or constructor throws.
168 * Each array element is either a <code>ClassDoc</code> or a
169 * <code>TypeVariable</code>.
170 */
171 public com.sun.javadoc.Type[] thrownExceptionTypes() {
172 return TypeMaker.getTypes(env, sym.type.getThrownTypes());
173 }
175 /**
176 * Get argument information.
177 *
178 * @see ParameterImpl
179 *
180 * @return an array of ParameterImpl, one element per argument
181 * in the order the arguments are present.
182 */
183 public Parameter[] parameters() {
184 // generate the parameters on the fly: they're not cached
185 List<VarSymbol> params = sym.params();
186 Parameter result[] = new Parameter[params.length()];
188 int i = 0;
189 for (VarSymbol param : params) {
190 result[i++] = new ParameterImpl(env, param);
191 }
192 return result;
193 }
195 /**
196 * Return the formal type parameters of this method or constructor.
197 * Return an empty array if there are none.
198 */
199 public TypeVariable[] typeParameters() {
200 if (env.legacyDoclet) {
201 return new TypeVariable[0];
202 }
203 TypeVariable res[] = new TypeVariable[sym.type.getTypeArguments().length()];
204 TypeMaker.getTypes(env, sym.type.getTypeArguments(), res);
205 return res;
206 }
208 /**
209 * Get the signature. It is the parameter list, type is qualified.
210 * For instance, for a method <code>mymethod(String x, int y)</code>,
211 * it will return <code>(java.lang.String,int)</code>.
212 */
213 public String signature() {
214 return makeSignature(true);
215 }
217 /**
218 * Get flat signature. All types are not qualified.
219 * Return a String, which is the flat signiture of this member.
220 * It is the parameter list, type is not qualified.
221 * For instance, for a method <code>mymethod(String x, int y)</code>,
222 * it will return <code>(String, int)</code>.
223 */
224 public String flatSignature() {
225 return makeSignature(false);
226 }
228 private String makeSignature(boolean full) {
229 StringBuffer result = new StringBuffer();
230 result.append("(");
231 for (List<Type> types = sym.type.getParameterTypes(); types.nonEmpty(); ) {
232 Type t = types.head;
233 result.append(TypeMaker.getTypeString(env, t, full));
234 types = types.tail;
235 if (types.nonEmpty()) {
236 result.append(", ");
237 }
238 }
239 if (isVarArgs()) {
240 int len = result.length();
241 result.replace(len - 2, len, "...");
242 }
243 result.append(")");
244 return result.toString();
245 }
247 protected String typeParametersString() {
248 return TypeMaker.typeParametersString(env, sym, true);
249 }
251 /**
252 * Generate a key for sorting.
253 */
254 CollationKey generateKey() {
255 String k = name() + flatSignature() + typeParametersString();
256 // ',' and '&' are between '$' and 'a': normalize to spaces.
257 k = k.replace(',', ' ').replace('&', ' ');
258 // System.out.println("COLLATION KEY FOR " + this + " is \"" + k + "\"");
259 return env.doclocale.collator.getCollationKey(k);
260 }
262 /**
263 * Return the source position of the entity, or null if
264 * no position is available.
265 */
266 public SourcePosition position() {
267 if (sym.enclClass().sourcefile == null) return null;
268 return SourcePositionImpl.make(sym.enclClass().sourcefile,
269 (tree==null) ? 0 : tree.pos,
270 lineMap);
271 }
272 }