Sat, 01 Jun 2013 22:09:18 +0100
6695379: Copy method annotations and parameter annotations to synthetic bridge methods
Reviewed-by: mcimadamore
1 /*
2 * Copyright (c) 2013, 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.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
24 import java.io.*;
25 import com.sun.tools.classfile.*;
27 /**
28 * The {@code ClassFileVisitor} reads a class file using the
29 * {@code com.sun.tools.classfile} library. It iterates over the methods
30 * in a class, and checks MethodParameters attributes against JLS
31 * requirements, as well as assumptions about the javac implementations.
32 * <p>
33 * It enforces the following rules:
34 * <ul>
35 * <li>All non-synthetic methods with arguments must have the
36 * MethodParameters attribute. </li>
37 * <li>At most one MethodParameters attribute per method.</li>
38 * <li>An empty MethodParameters attribute is not allowed (i.e. no
39 * attribute for methods taking no parameters).</li>
40 * <li>The number of recorded parameter names much equal the number
41 * of parameters, including any implicit or synthetic parameters generated
42 * by the compiler.</li>
43 * <li>Although the spec allow recording parameters with no name, the javac
44 * implementation is assumed to record a name for all parameters. That is,
45 * the Methodparameters attribute must record a non-zero, valid constant
46 * pool index for each parameter.</li>
47 * <li>Check presence, expected names (e.g. this$N, $enum$name, ...) and flags
48 * (e.g. ACC_SYNTHETIC, ACC_MANDATED) for compiler generated parameters.</li>
49 * <li>Names of explicit parameters must reflect the names in the Java source.
50 * This is checked by assuming a design pattern where any name is permitted
51 * for the first explicit parameter. For subsequent parameters the following
52 * rule is checked: <i>param[n] == ++param[n-1].charAt(0) + param[n-1]</i>
53 * </ul>
54 */
55 class ClassFileVisitor extends Tester.Visitor {
57 Tester tester;
59 public String cname;
60 public boolean isEnum;
61 public boolean isInterface;
62 public boolean isInner;
63 public boolean isPublic;
64 public boolean isStatic;
65 public boolean isAnon;
66 public ClassFile classFile;
69 public ClassFileVisitor(Tester tester) {
70 super(tester);
71 }
73 public void error(String msg) {
74 super.error("classfile: " + msg);
75 }
77 public void warn(String msg) {
78 super.warn("classfile: " + msg);
79 }
81 /**
82 * Read the class and determine some key characteristics, like if it's
83 * an enum, or inner class, etc.
84 */
85 void visitClass(final String cname, final File cfile, final StringBuilder sb)
86 throws Exception {
87 this.cname = cname;
88 classFile = ClassFile.read(cfile);
89 isEnum = classFile.access_flags.is(AccessFlags.ACC_ENUM);
90 isInterface = classFile.access_flags.is(AccessFlags.ACC_INTERFACE);
91 isPublic = classFile.access_flags.is(AccessFlags.ACC_PUBLIC);
92 isInner = false;
93 isStatic = true;
94 isAnon = false;
96 Attribute attr = classFile.getAttribute("InnerClasses");
97 if (attr != null) attr.accept(new InnerClassVisitor(), null);
98 isAnon = isInner & isAnon;
100 sb.append(isStatic ? "static " : "")
101 .append(isPublic ? "public " : "")
102 .append(isEnum ? "enum " : isInterface ? "interface " : "class ")
103 .append(cname).append(" -- ")
104 .append(isInner? "inner " : "" )
105 .append(isAnon ? "anon" : "")
106 .append("\n");;
108 for (Method method : classFile.methods) {
109 new MethodVisitor().visitMethod(method, sb);
110 }
111 }
113 /**
114 * Used to visit InnerClasses_attribute of a class,
115 * to determne if this class is an local class, and anonymous
116 * inner class or a none-static member class. These types of
117 * classes all have an containing class instances field that
118 * requires an implicit or synthetic constructor argument.
119 */
120 class InnerClassVisitor extends AttributeVisitor<Void, Void> {
121 public Void visitInnerClasses(InnerClasses_attribute iattr, Void v) {
122 try{
123 for (InnerClasses_attribute.Info info : iattr.classes) {
124 if (info.getInnerClassInfo(classFile.constant_pool) == null) continue;
125 String in = info.getInnerClassInfo(classFile.constant_pool).getName();
126 if (in == null || !cname.equals(in)) continue;
127 isInner = true;
128 isAnon = null == info.getInnerName(classFile.constant_pool);
129 isStatic = info.inner_class_access_flags.is(AccessFlags.ACC_STATIC);
130 break;
131 }
132 } catch(Exception e) {
133 throw new IllegalStateException(e);
134 }
135 return null;
136 }
137 }
139 /**
140 * Check the MethodParameters attribute of a method.
141 */
142 class MethodVisitor extends AttributeVisitor<Void, StringBuilder> {
144 public String mName;
145 public Descriptor mDesc;
146 public int mParams;
147 public int mAttrs;
148 public int mNumParams;
149 public boolean mSynthetic;
150 public boolean mIsConstructor;
151 public boolean mIsBridge;
152 public String prefix;
154 void visitMethod(Method method, StringBuilder sb) throws Exception {
156 mName = method.getName(classFile.constant_pool);
157 mDesc = method.descriptor;
158 mParams = mDesc.getParameterCount(classFile.constant_pool);
159 mAttrs = method.attributes.attrs.length;
160 mNumParams = -1; // no MethodParameters attribute found
161 mSynthetic = method.access_flags.is(AccessFlags.ACC_SYNTHETIC);
162 mIsConstructor = mName.equals("<init>");
163 prefix = cname + "." + mName + "() - ";
164 mIsBridge = method.access_flags.is(AccessFlags.ACC_BRIDGE);
166 sb.append(cname).append(".").append(mName).append("(");
168 for (Attribute a : method.attributes) {
169 a.accept(this, sb);
170 }
171 if (mNumParams == -1) {
172 if (mSynthetic) {
173 sb.append("<none>)!!");
174 } else {
175 sb.append("<none>)");
176 }
177 }
178 sb.append("\n");
180 // IMPL: methods with arguments must have a MethodParameters
181 // attribute, except possibly some synthetic methods.
182 if (mNumParams == -1 && mParams > 0 && ! mSynthetic) {
183 error(prefix + "missing MethodParameters attribute");
184 }
185 }
187 public Void visitMethodParameters(MethodParameters_attribute mp,
188 StringBuilder sb) {
190 // SPEC: At most one MethodParameters attribute allowed
191 if (mNumParams != -1) {
192 error(prefix + "Multiple MethodParameters attributes");
193 return null;
194 }
196 mNumParams = mp.method_parameter_table_length;
198 // SPEC: An empty attribute is not allowed!
199 if (mNumParams == 0) {
200 error(prefix + "0 length MethodParameters attribute");
201 return null;
202 }
204 // SPEC: one name per parameter.
205 if (mNumParams != mParams) {
206 error(prefix + "found " + mNumParams +
207 " parameters, expected " + mParams);
208 return null;
209 }
211 // IMPL: Whether MethodParameters attributes will be generated
212 // for some synthetics is unresolved. For now, assume no.
213 if (mSynthetic) {
214 warn(prefix + "synthetic has MethodParameter attribute");
215 }
217 String sep = "";
218 String userParam = null;
219 for (int x = 0; x < mNumParams; x++) {
221 // IMPL: Assume all parameters are named, something.
222 int cpi = mp.method_parameter_table[x].name_index;
223 if (cpi == 0) {
224 error(prefix + "name expected, param[" + x + "]");
225 return null;
226 }
228 // SPEC: a non 0 index, must be valid!
229 String param = null;
230 try {
231 param = classFile.constant_pool.getUTF8Value(cpi);
232 sb.append(sep).append(param);
233 sep = ", ";
234 } catch(ConstantPoolException e) {
235 error(prefix + "invalid index " + cpi + " for param["
236 + x + "]");
237 return null;
238 }
241 // Check availability, flags and special names
242 int check = checkParam(mp, param, x, sb);
243 if (check < 0) {
244 return null;
245 }
247 // TEST: check test assumptions about parameter name.
248 // Expected names are calculated starting with the
249 // 2nd explicit (user given) parameter.
250 // param[n] == ++param[n-1].charAt(0) + param[n-1]
251 String expect = null;
252 if (userParam != null) {
253 char c = userParam.charAt(0);
254 expect = (++c) + userParam;
255 }
256 if (check > 0) {
257 userParam = param;
258 }
259 if (expect != null && !param.equals(expect)) {
260 error(prefix + "param[" + x + "]='"
261 + param + "' expected '" + expect + "'");
262 return null;
263 }
264 }
265 if (mSynthetic) {
266 sb.append(")!!");
267 } else {
268 sb.append(")");
269 }
270 return null;
271 }
273 /*
274 * Check a parameter for conformity to JLS and javac specific
275 * assumptions.
276 * Return -1, if an error is detected. Otherwise, return 0, if
277 * the parameter is compiler generated, or 1 for an (presumably)
278 * explicitly declared parameter.
279 */
280 int checkParam(MethodParameters_attribute mp, String param, int index,
281 StringBuilder sb) {
283 boolean synthetic = (mp.method_parameter_table[index].flags
284 & AccessFlags.ACC_SYNTHETIC) != 0;
285 boolean mandated = (mp.method_parameter_table[index].flags
286 & AccessFlags.ACC_MANDATED) != 0;
288 // Setup expectations for flags and special names
289 String expect = null;
290 boolean allowMandated = false;
291 boolean allowSynthetic = false;
292 if (mSynthetic || synthetic) {
293 // not an implementation gurantee, but okay for now
294 expect = "arg" + index; // default
295 }
296 if (mIsConstructor) {
297 if (isEnum) {
298 if (index == 0) {
299 expect = "\\$enum\\$name";
300 allowSynthetic = true;
301 } else if(index == 1) {
302 expect = "\\$enum\\$ordinal";
303 allowSynthetic = true;
304 }
305 } else if (index == 0) {
306 if (isAnon) {
307 allowMandated = true;
308 expect = "this\\$[0-n]*";
309 } else if (isInner && !isStatic) {
310 allowMandated = true;
311 if (!isPublic) {
312 // some but not all non-public inner classes
313 // have synthetic argument. For now we give
314 // the test a bit of slack and allow either.
315 allowSynthetic = true;
316 }
317 expect = "this\\$[0-n]*";
318 }
319 }
320 } else if (isEnum && mNumParams == 1 && index == 0 && mName.equals("valueOf")) {
321 expect = "name";
322 allowMandated = true;
323 } else if (mIsBridge) {
324 allowSynthetic = true;
325 /* you can't expect an special name for bridges' parameters.
326 * The name of the original parameters are now copied.
327 */
328 expect = null;
329 }
330 if (mandated) sb.append("!");
331 if (synthetic) sb.append("!!");
333 // IMPL: our rules a somewhat fuzzy, sometimes allowing both mandated
334 // and synthetic. However, a parameters cannot be both.
335 if (mandated && synthetic) {
336 error(prefix + "param[" + index + "] == \"" + param
337 + "\" ACC_SYNTHETIC and ACC_MANDATED");
338 return -1;
339 }
340 // ... but must be either, if both "allowed".
341 if (!(mandated || synthetic) && allowMandated && allowSynthetic) {
342 error(prefix + "param[" + index + "] == \"" + param
343 + "\" expected ACC_MANDATED or ACC_SYNTHETIC");
344 return -1;
345 }
347 // ... if only one is "allowed", we meant "required".
348 if (!mandated && allowMandated && !allowSynthetic) {
349 error(prefix + "param[" + index + "] == \"" + param
350 + "\" expected ACC_MANDATED");
351 return -1;
352 }
353 if (!synthetic && !allowMandated && allowSynthetic) {
354 error(prefix + "param[" + index + "] == \"" + param
355 + "\" expected ACC_SYNTHETIC");
356 return -1;
357 }
359 // ... and not "allowed", means prohibited.
360 if (mandated && !allowMandated) {
361 error(prefix + "param[" + index + "] == \"" + param
362 + "\" unexpected, is ACC_MANDATED");
363 return -1;
364 }
365 if (synthetic && !allowSynthetic) {
366 error(prefix + "param[" + index + "] == \"" + param
367 + "\" unexpected, is ACC_SYNTHETIC");
368 return -1;
369 }
371 // Test special name expectations
372 if (expect != null) {
373 if (param.matches(expect)) {
374 return 0;
375 }
376 error(prefix + "param[" + index + "]='" + param +
377 "' expected '" + expect + "'");
378 return -1;
379 }
381 // No further checking for synthetic methods.
382 if (mSynthetic) {
383 return 0;
384 }
386 // Otherwise, do check test parameter naming convention.
387 return 1;
388 }
389 }
390 }