test/tools/javac/MethodParameters/ClassFileVisitor.java

Wed, 08 Oct 2014 14:16:40 -0700

author
asaha
date
Wed, 08 Oct 2014 14:16:40 -0700
changeset 2586
f5e5ca7505e2
parent 2137
a48d3b981083
child 2525
2eb010b6cb22
child 2733
7974f6da2d76
permissions
-rw-r--r--

Merge

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

mercurial