test/compiler/jsr292/methodHandleExceptions/TestAMEnotNPE.java

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24 import java.lang.reflect.InvocationTargetException;
aoqi@0 25 import java.lang.reflect.Method;
aoqi@0 26 import java.util.ArrayList;
aoqi@0 27 import java.util.List;
aoqi@0 28 import jdk.internal.org.objectweb.asm.ClassWriter;
aoqi@0 29 import jdk.internal.org.objectweb.asm.Handle;
aoqi@0 30 import jdk.internal.org.objectweb.asm.MethodVisitor;
aoqi@0 31 import jdk.internal.org.objectweb.asm.Opcodes;
aoqi@0 32 import p.Dok;
aoqi@0 33
aoqi@0 34 /**
aoqi@0 35 * @test @bug 8025260 8016839
aoqi@0 36 * @summary Ensure that AbstractMethodError and IllegalAccessError are thrown appropriately, not NullPointerException
aoqi@0 37 *
aoqi@0 38 * @compile -XDignore.symbol.file TestAMEnotNPE.java ByteClassLoader.java p/C.java p/Dok.java p/E.java p/F.java p/I.java p/Tdirect.java p/Treflect.java
aoqi@0 39 *
aoqi@0 40 * @run main/othervm TestAMEnotNPE
aoqi@0 41 * @run main/othervm -Xint TestAMEnotNPE
aoqi@0 42 * @run main/othervm -Xcomp TestAMEnotNPE
aoqi@0 43 */
aoqi@0 44 public class TestAMEnotNPE implements Opcodes {
aoqi@0 45
aoqi@0 46 static boolean writeJarFiles = false;
aoqi@0 47 static boolean readJarFiles = false;
aoqi@0 48
aoqi@0 49 /**
aoqi@0 50 * Optional command line parameter (any case-insensitive prefix of)
aoqi@0 51 * "writejarfiles" or "readjarfiles".
aoqi@0 52 *
aoqi@0 53 * "Writejarfiles" creates a jar file for each different set of tested classes.
aoqi@0 54 * "Readjarfiles" causes the classloader to use the copies of the classes
aoqi@0 55 * found in the corresponding jar files.
aoqi@0 56 *
aoqi@0 57 * Jarfilenames look something like pD_ext_pF (p.D extends p.F)
aoqi@0 58 * and qD_m_pp_imp_pI (q.D with package-private m implements p.I)
aoqi@0 59 *
aoqi@0 60 */
aoqi@0 61 public static void main(String args[]) throws Throwable {
aoqi@0 62 ArrayList<Throwable> lt = new ArrayList<Throwable>();
aoqi@0 63
aoqi@0 64 if (args.length > 0) {
aoqi@0 65 String a0 = args[0].toLowerCase();
aoqi@0 66 if (a0.length() > 0) {
aoqi@0 67 writeJarFiles = ("writejarfiles").startsWith(a0);
aoqi@0 68 readJarFiles = ("readjarfiles").startsWith(a0);
aoqi@0 69 }
aoqi@0 70 if (!(writeJarFiles || readJarFiles)) {
aoqi@0 71 throw new Error("Command line parameter (if any) should be prefix of writeJarFiles or readJarFiles");
aoqi@0 72 }
aoqi@0 73 }
aoqi@0 74
aoqi@0 75 try {
aoqi@0 76 System.out.println("TRYING p.D.m PRIVATE interface-invoked as p.I.m, p.D extends p.F, p.F.m FINAL");
aoqi@0 77 tryAndCheckThrown(lt, bytesForDprivateSubWhat("p/F"),
aoqi@0 78 "p.D extends p.F (p.F implements p.I, FINAL public m), private m",
aoqi@0 79 IllegalAccessError.class, "pD_ext_pF");
aoqi@0 80 // We'll take either a VerifyError (pre 2013-11-30)
aoqi@0 81 // or an IllegalAccessError (post 2013-11-22)
aoqi@0 82 } catch (VerifyError ve) {
aoqi@0 83 System.out.println("Saw expected VerifyError " + ve);
aoqi@0 84 }
aoqi@0 85 System.out.println();
aoqi@0 86
aoqi@0 87 System.out.println("TRYING p.D.m PRIVATE interface-invoked as p.I.m, p.D extends p.E");
aoqi@0 88 tryAndCheckThrown(lt, bytesForDprivateSubWhat("p/E"),
aoqi@0 89 "p.D extends p.E (p.E implements p.I, public m), private m",
aoqi@0 90 IllegalAccessError.class, "pD_ext_pE");
aoqi@0 91
aoqi@0 92 System.out.println("TRYING p.D.m ABSTRACT interface-invoked as p.I.m");
aoqi@0 93 tryAndCheckThrown(lt, bytesForD(),
aoqi@0 94 "D extends abstract C, no m",
aoqi@0 95 AbstractMethodError.class, "pD_ext_pC");
aoqi@0 96
aoqi@0 97 System.out.println("TRYING q.D.m PACKAGE interface-invoked as p.I.m");
aoqi@0 98 tryAndCheckThrown(lt, "q.D", bytesForDsomeAccess("q/D", 0),
aoqi@0 99 "q.D implements p.I, protected m", IllegalAccessError.class,
aoqi@0 100 "qD_m_pp_imp_pI");
aoqi@0 101
aoqi@0 102 // Note jar file name is used in the plural-arg case.
aoqi@0 103 System.out.println("TRYING p.D.m PRIVATE interface-invoked as p.I.m");
aoqi@0 104 tryAndCheckThrown(lt, bytesForDsomeAccess("p/D", ACC_PRIVATE),
aoqi@0 105 "p.D implements p.I, private m",
aoqi@0 106 IllegalAccessError.class, "pD_m_pri_imp_pI");
aoqi@0 107
aoqi@0 108 // Plural-arg test.
aoqi@0 109 System.out.println("TRYING p.D.m PRIVATE MANY ARG interface-invoked as p.I.m");
aoqi@0 110 tryAndCheckThrownMany(lt, bytesForDsomeAccess("p/D", ACC_PRIVATE),
aoqi@0 111 "p.D implements p.I, private m", IllegalAccessError.class);
aoqi@0 112
aoqi@0 113 if (lt.size() > 0) {
aoqi@0 114 System.out.flush();
aoqi@0 115 Thread.sleep(250); // This de-interleaves output and error in Netbeans, sigh.
aoqi@0 116 for (Throwable th : lt)
aoqi@0 117 System.err.println(th);
aoqi@0 118 throw new Error("Test failed, there were " + lt.size() + " failures listed above");
aoqi@0 119 } else {
aoqi@0 120 System.out.println("ALL PASS, HOORAY!");
aoqi@0 121 }
aoqi@0 122 }
aoqi@0 123
aoqi@0 124 /**
aoqi@0 125 * The bytes for D, a NOT abstract class extending abstract class C without
aoqi@0 126 * supplying an implementation for abstract method m. There is a default
aoqi@0 127 * method in the interface I, but it should lose to the abstract class.
aoqi@0 128 *
aoqi@0 129 * @return
aoqi@0 130 * @throws Exception
aoqi@0 131 */
aoqi@0 132 public static byte[] bytesForD() throws Exception {
aoqi@0 133
aoqi@0 134 ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES
aoqi@0 135 | ClassWriter.COMPUTE_MAXS);
aoqi@0 136 MethodVisitor mv;
aoqi@0 137
aoqi@0 138 cw.visit(V1_8, ACC_PUBLIC + ACC_SUPER, "p/D", null, "p/C", null);
aoqi@0 139
aoqi@0 140 {
aoqi@0 141 mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
aoqi@0 142 mv.visitCode();
aoqi@0 143 mv.visitVarInsn(ALOAD, 0);
aoqi@0 144 mv.visitMethodInsn(INVOKESPECIAL, "p/C", "<init>", "()V");
aoqi@0 145 mv.visitInsn(RETURN);
aoqi@0 146 mv.visitMaxs(0, 0);
aoqi@0 147 mv.visitEnd();
aoqi@0 148 }
aoqi@0 149 cw.visitEnd();
aoqi@0 150
aoqi@0 151 return cw.toByteArray();
aoqi@0 152 }
aoqi@0 153
aoqi@0 154 /**
aoqi@0 155 * The bytes for D, implements I, does not extend C, declares m()I with
aoqi@0 156 * access method_acc.
aoqi@0 157 *
aoqi@0 158 * @param d_name Name of class defined
aoqi@0 159 * @param method_acc Accessibility of that class's method m.
aoqi@0 160 * @return
aoqi@0 161 * @throws Exception
aoqi@0 162 */
aoqi@0 163 public static byte[] bytesForDsomeAccess(String d_name, int method_acc) throws Exception {
aoqi@0 164 return bytesForSomeDsubSomethingSomeAccess(d_name, "java/lang/Object", method_acc);
aoqi@0 165 }
aoqi@0 166
aoqi@0 167 /**
aoqi@0 168 * The bytes for D implements I, extends some class, declares m()I as
aoqi@0 169 * private.
aoqi@0 170 *
aoqi@0 171 * Invokeinterface of I.m applied to this D should throw IllegalAccessError
aoqi@0 172 *
aoqi@0 173 * @param sub_what The name of the class that D will extend.
aoqi@0 174 * @return
aoqi@0 175 * @throws Exception
aoqi@0 176 */
aoqi@0 177 public static byte[] bytesForDprivateSubWhat(String sub_what) throws Exception {
aoqi@0 178 return bytesForSomeDsubSomethingSomeAccess("p/D", sub_what, ACC_PRIVATE);
aoqi@0 179 }
aoqi@0 180
aoqi@0 181 /**
aoqi@0 182 * Returns the bytes for a class with name d_name (presumably "D" in some
aoqi@0 183 * package), extending some class with name sub_what, implementing p.I,
aoqi@0 184 * and defining two methods m() and m(11args) with access method_acc.
aoqi@0 185 *
aoqi@0 186 * @param d_name Name of class that is defined
aoqi@0 187 * @param sub_what Name of class that it extends
aoqi@0 188 * @param method_acc Accessibility of method(s) m in defined class.
aoqi@0 189 * @return
aoqi@0 190 * @throws Exception
aoqi@0 191 */
aoqi@0 192 public static byte[] bytesForSomeDsubSomethingSomeAccess
aoqi@0 193 (String d_name, String sub_what, int method_acc)
aoqi@0 194 throws Exception {
aoqi@0 195
aoqi@0 196 ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES
aoqi@0 197 | ClassWriter.COMPUTE_MAXS);
aoqi@0 198 MethodVisitor mv;
aoqi@0 199 String[] interfaces = {"p/I"};
aoqi@0 200
aoqi@0 201 cw.visit(V1_8, ACC_PUBLIC + ACC_SUPER, d_name, null, sub_what, interfaces);
aoqi@0 202 {
aoqi@0 203 mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
aoqi@0 204 mv.visitCode();
aoqi@0 205 mv.visitVarInsn(ALOAD, 0);
aoqi@0 206 mv.visitMethodInsn(INVOKESPECIAL, sub_what, "<init>", "()V");
aoqi@0 207 mv.visitInsn(RETURN);
aoqi@0 208 mv.visitMaxs(0, 0);
aoqi@0 209 mv.visitEnd();
aoqi@0 210 }
aoqi@0 211 // int m() {return 3;}
aoqi@0 212 {
aoqi@0 213 mv = cw.visitMethod(method_acc, "m", "()I", null, null);
aoqi@0 214 mv.visitCode();
aoqi@0 215 mv.visitLdcInsn(new Integer(3));
aoqi@0 216 mv.visitInsn(IRETURN);
aoqi@0 217 mv.visitMaxs(0, 0);
aoqi@0 218 mv.visitEnd();
aoqi@0 219 }
aoqi@0 220 // int m(11args) {return 3;}
aoqi@0 221 {
aoqi@0 222 mv = cw.visitMethod(method_acc, "m", "(BCSIJ"
aoqi@0 223 + "Ljava/lang/Object;"
aoqi@0 224 + "Ljava/lang/Object;"
aoqi@0 225 + "Ljava/lang/Object;"
aoqi@0 226 + "Ljava/lang/Object;"
aoqi@0 227 + "Ljava/lang/Object;"
aoqi@0 228 + "Ljava/lang/Object;"
aoqi@0 229 + ")I", null, null);
aoqi@0 230 mv.visitCode();
aoqi@0 231 mv.visitLdcInsn(new Integer(3));
aoqi@0 232 mv.visitInsn(IRETURN);
aoqi@0 233 mv.visitMaxs(0, 0);
aoqi@0 234 mv.visitEnd();
aoqi@0 235 }
aoqi@0 236 cw.visitEnd();
aoqi@0 237 return cw.toByteArray();
aoqi@0 238 }
aoqi@0 239
aoqi@0 240 /**
aoqi@0 241 * The bytecodes for a class p/T defining a methods test() and test(11args)
aoqi@0 242 * that contain an invokeExact of a particular methodHandle, I.m.
aoqi@0 243 *
aoqi@0 244 * Test will be passed values that may imperfectly implement I,
aoqi@0 245 * and thus may in turn throw exceptions.
aoqi@0 246 *
aoqi@0 247 * @return
aoqi@0 248 * @throws Exception
aoqi@0 249 */
aoqi@0 250 public static byte[] bytesForT() throws Exception {
aoqi@0 251
aoqi@0 252 ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES
aoqi@0 253 | ClassWriter.COMPUTE_MAXS);
aoqi@0 254 MethodVisitor mv;
aoqi@0 255
aoqi@0 256 cw.visit(V1_8, ACC_PUBLIC + ACC_SUPER, "p/T", null, "java/lang/Object", null);
aoqi@0 257 {
aoqi@0 258 mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
aoqi@0 259 mv.visitCode();
aoqi@0 260 mv.visitVarInsn(ALOAD, 0);
aoqi@0 261 mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
aoqi@0 262 mv.visitInsn(RETURN);
aoqi@0 263 mv.visitMaxs(0, 0);
aoqi@0 264 mv.visitEnd();
aoqi@0 265 }
aoqi@0 266 // static int test(I)
aoqi@0 267 {
aoqi@0 268 mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "test", "(Lp/I;)I", null, null);
aoqi@0 269 mv.visitCode();
aoqi@0 270 mv.visitLdcInsn(new Handle(Opcodes.H_INVOKEINTERFACE, "p/I", "m", "()I"));
aoqi@0 271 mv.visitVarInsn(ALOAD, 0);
aoqi@0 272 mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandle",
aoqi@0 273 "invokeExact", "(Lp/I;)I");
aoqi@0 274 mv.visitInsn(IRETURN);
aoqi@0 275 mv.visitMaxs(0, 0);
aoqi@0 276 mv.visitEnd();
aoqi@0 277 }
aoqi@0 278 // static int test(I,11args)
aoqi@0 279 {
aoqi@0 280 mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "test", "(Lp/I;BCSIJLjava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)I", null, null);
aoqi@0 281 mv.visitCode();
aoqi@0 282 mv.visitLdcInsn(new Handle(Opcodes.H_INVOKEINTERFACE, "p/I", "m", "(BCSIJLjava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)I"));
aoqi@0 283 mv.visitVarInsn(ALOAD, 0);
aoqi@0 284 mv.visitVarInsn(ILOAD, 1);
aoqi@0 285 mv.visitVarInsn(ILOAD, 2);
aoqi@0 286 mv.visitVarInsn(ILOAD, 3);
aoqi@0 287 mv.visitVarInsn(ILOAD, 4);
aoqi@0 288 mv.visitVarInsn(LLOAD, 5);
aoqi@0 289 mv.visitVarInsn(ALOAD, 7);
aoqi@0 290 mv.visitVarInsn(ALOAD, 8);
aoqi@0 291 mv.visitVarInsn(ALOAD, 9);
aoqi@0 292 mv.visitVarInsn(ALOAD, 10);
aoqi@0 293 mv.visitVarInsn(ALOAD, 11);
aoqi@0 294 mv.visitVarInsn(ALOAD, 12);
aoqi@0 295 mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandle",
aoqi@0 296 "invokeExact", "(Lp/I;BCSIJLjava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)I");
aoqi@0 297 mv.visitInsn(IRETURN);
aoqi@0 298 mv.visitMaxs(0, 0);
aoqi@0 299 mv.visitEnd();
aoqi@0 300 }
aoqi@0 301 cw.visitEnd();
aoqi@0 302 return cw.toByteArray();
aoqi@0 303 }
aoqi@0 304
aoqi@0 305 private static void tryAndCheckThrown(
aoqi@0 306 List<Throwable> lt, byte[] dBytes, String what, Class<?> expected, String jar_name)
aoqi@0 307 throws Throwable {
aoqi@0 308 tryAndCheckThrown(lt, "p.D", dBytes, what, expected, jar_name);
aoqi@0 309 }
aoqi@0 310
aoqi@0 311 private static void tryAndCheckThrown(List<Throwable> lt, String d_name, byte[] dBytes, String what, Class<?> expected, String jar_name)
aoqi@0 312 throws Throwable {
aoqi@0 313
aoqi@0 314 System.out.println("Methodhandle invokeExact I.m() for instance of " + what);
aoqi@0 315 ByteClassLoader bcl1 = new ByteClassLoader(jar_name, readJarFiles, writeJarFiles);
aoqi@0 316 try {
aoqi@0 317 Class<?> d1 = bcl1.loadBytes(d_name, dBytes);
aoqi@0 318 Class<?> t1 = bcl1.loadBytes("p.T", bytesForT());
aoqi@0 319 invokeTest(t1, d1, expected, lt);
aoqi@0 320 } finally {
aoqi@0 321 // Not necessary for others -- all class files are written in this call.
aoqi@0 322 // (unless the VM crashes first).
aoqi@0 323 bcl1.close();
aoqi@0 324 }
aoqi@0 325
aoqi@0 326 System.out.println("Reflection invoke I.m() for instance of " + what);
aoqi@0 327 ByteClassLoader bcl3 = new ByteClassLoader(jar_name, readJarFiles, false);
aoqi@0 328 Class<?> d3 = bcl3.loadBytes(d_name, dBytes);
aoqi@0 329 Class<?> t3 = bcl3.loadClass("p.Treflect");
aoqi@0 330 invokeTest(t3, d3, expected, lt);
aoqi@0 331
aoqi@0 332 System.out.println("Bytecode invokeInterface I.m() for instance of " + what);
aoqi@0 333 ByteClassLoader bcl2 = new ByteClassLoader(jar_name, readJarFiles, false);
aoqi@0 334 Class<?> d2 = bcl2.loadBytes(d_name, dBytes);
aoqi@0 335 Class<?> t2 = bcl2.loadClass("p.Tdirect");
aoqi@0 336 badGoodBadGood(t2, d2, expected, lt);
aoqi@0 337 }
aoqi@0 338
aoqi@0 339 private static void invokeTest(Class<?> t, Class<?> d, Class<?> expected, List<Throwable> lt)
aoqi@0 340 throws Throwable {
aoqi@0 341 try {
aoqi@0 342 Method m = t.getMethod("test", p.I.class);
aoqi@0 343 Object o = d.newInstance();
aoqi@0 344 Object result = m.invoke(null, o);
aoqi@0 345 if (expected != null) {
aoqi@0 346 System.out.println("FAIL, Expected " + expected.getName()
aoqi@0 347 + " wrapped in InvocationTargetException, but nothing was thrown");
aoqi@0 348 lt.add(new Error("Exception " + expected.getName() + " was not thrown"));
aoqi@0 349 } else {
aoqi@0 350 System.out.println("PASS, saw expected return.");
aoqi@0 351 }
aoqi@0 352 } catch (InvocationTargetException e) {
aoqi@0 353 Throwable th = e.getCause();
aoqi@0 354 th.printStackTrace(System.out);
aoqi@0 355 if (expected != null) {
aoqi@0 356 if (expected.isInstance(th)) {
aoqi@0 357 System.out.println("PASS, saw expected exception (" + expected.getName() + ").");
aoqi@0 358 } else {
aoqi@0 359 System.out.println("FAIL, Expected " + expected.getName()
aoqi@0 360 + " wrapped in InvocationTargetException, saw " + th);
aoqi@0 361 lt.add(th);
aoqi@0 362 }
aoqi@0 363 } else {
aoqi@0 364 System.out.println("FAIL, expected no exception, saw " + th);
aoqi@0 365 lt.add(th);
aoqi@0 366 }
aoqi@0 367 }
aoqi@0 368 System.out.println();
aoqi@0 369 }
aoqi@0 370
aoqi@0 371 /* Many-arg versions of above */
aoqi@0 372 private static void tryAndCheckThrownMany(List<Throwable> lt, byte[] dBytes, String what, Class<?> expected)
aoqi@0 373 throws Throwable {
aoqi@0 374
aoqi@0 375 System.out.println("Methodhandle invokeExact I.m(11params) for instance of " + what);
aoqi@0 376 ByteClassLoader bcl1 = new ByteClassLoader("p.D", readJarFiles, false);
aoqi@0 377 try {
aoqi@0 378 Class<?> d1 = bcl1.loadBytes("p.D", dBytes);
aoqi@0 379 Class<?> t1 = bcl1.loadBytes("p.T", bytesForT());
aoqi@0 380 invokeTestMany(t1, d1, expected, lt);
aoqi@0 381 } finally {
aoqi@0 382 bcl1.close(); // Not necessary for others -- all class files are written in this call.
aoqi@0 383 }
aoqi@0 384
aoqi@0 385 {
aoqi@0 386 System.out.println("Bytecode invokeInterface I.m(11params) for instance of " + what);
aoqi@0 387 ByteClassLoader bcl2 = new ByteClassLoader("pD_m_pri_imp_pI", readJarFiles, false);
aoqi@0 388 Class<?> d2 = bcl2.loadBytes("p.D", dBytes);
aoqi@0 389 Class<?> t2 = bcl2.loadClass("p.Tdirect");
aoqi@0 390 badGoodBadGoodMany(t2, d2, expected, lt);
aoqi@0 391
aoqi@0 392 }
aoqi@0 393 {
aoqi@0 394 System.out.println("Reflection invokeInterface I.m(11params) for instance of " + what);
aoqi@0 395 ByteClassLoader bcl2 = new ByteClassLoader("pD_m_pri_imp_pI", readJarFiles, false);
aoqi@0 396 Class<?> d2 = bcl2.loadBytes("p.D", dBytes);
aoqi@0 397 Class<?> t2 = bcl2.loadClass("p.Treflect");
aoqi@0 398 invokeTestMany(t2, d2, expected, lt);
aoqi@0 399 }
aoqi@0 400 }
aoqi@0 401
aoqi@0 402 private static void invokeTestMany(Class<?> t, Class<?> d, Class<?> expected, List<Throwable> lt)
aoqi@0 403 throws Throwable {
aoqi@0 404 try {
aoqi@0 405 Method m = t.getMethod("test", p.I.class,
aoqi@0 406 Byte.TYPE, Character.TYPE, Short.TYPE, Integer.TYPE, Long.TYPE,
aoqi@0 407 Object.class, Object.class, Object.class,
aoqi@0 408 Object.class, Object.class, Object.class);
aoqi@0 409 Object o = d.newInstance();
aoqi@0 410 Byte b = 1;
aoqi@0 411 Character c = 2;
aoqi@0 412 Short s = 3;
aoqi@0 413 Integer i = 4;
aoqi@0 414 Long j = 5L;
aoqi@0 415 Object o1 = b;
aoqi@0 416 Object o2 = c;
aoqi@0 417 Object o3 = s;
aoqi@0 418 Object o4 = i;
aoqi@0 419 Object o5 = j;
aoqi@0 420 Object o6 = "6";
aoqi@0 421
aoqi@0 422 Object result = m.invoke(null, o, b, c, s, i, j,
aoqi@0 423 o1, o2, o3, o4, o5, o6);
aoqi@0 424 if (expected != null) {
aoqi@0 425 System.out.println("FAIL, Expected " + expected.getName()
aoqi@0 426 + " wrapped in InvocationTargetException, but nothing was thrown");
aoqi@0 427 lt.add(new Error("Exception " + expected.getName()
aoqi@0 428 + " was not thrown"));
aoqi@0 429 } else {
aoqi@0 430 System.out.println("PASS, saw expected return.");
aoqi@0 431 }
aoqi@0 432 } catch (InvocationTargetException e) {
aoqi@0 433 Throwable th = e.getCause();
aoqi@0 434 th.printStackTrace(System.out);
aoqi@0 435 if (expected != null) {
aoqi@0 436 if (expected.isInstance(th)) {
aoqi@0 437 System.out.println("PASS, saw expected exception ("
aoqi@0 438 + expected.getName() + ").");
aoqi@0 439 } else {
aoqi@0 440 System.out.println("FAIL, Expected " + expected.getName()
aoqi@0 441 + " wrapped in InvocationTargetException, saw " + th);
aoqi@0 442 lt.add(th);
aoqi@0 443 }
aoqi@0 444 } else {
aoqi@0 445 System.out.println("FAIL, expected no exception, saw " + th);
aoqi@0 446 lt.add(th);
aoqi@0 447 }
aoqi@0 448 }
aoqi@0 449 System.out.println();
aoqi@0 450 }
aoqi@0 451
aoqi@0 452 /**
aoqi@0 453 * This tests a peculiar idiom for tickling the bug on older VMs that lack
aoqi@0 454 * methodhandles. The bug (if not fixed) acts in the following way:
aoqi@0 455 *
aoqi@0 456 * When a broken receiver is passed to the first execution of an invokeinterface
aoqi@0 457 * bytecode, the illegal access is detected before the effects of resolution are
aoqi@0 458 * cached for later use, and so repeated calls with a broken receiver will always
aoqi@0 459 * throw the correct error.
aoqi@0 460 *
aoqi@0 461 * If, however, a good receiver is passed to the invokeinterface, the effects of
aoqi@0 462 * resolution will be successfully cached. A subsequent execution with a broken
aoqi@0 463 * receiver will reuse the cached information, skip the detailed resolution work,
aoqi@0 464 * and instead encounter a null pointer. By convention, that is the encoding for a
aoqi@0 465 * missing abstract method, and an AbstractMethodError is thrown -- not the expected
aoqi@0 466 * IllegalAccessError.
aoqi@0 467 *
aoqi@0 468 * @param t2 Test invocation class
aoqi@0 469 * @param d2 Test receiver class
aoqi@0 470 * @param expected expected exception type
aoqi@0 471 * @param lt list of unexpected throwables seen
aoqi@0 472 */
aoqi@0 473 private static void badGoodBadGood(Class<?> t2, Class<?> d2, Class<?> expected, List<Throwable> lt)
aoqi@0 474 throws Throwable {
aoqi@0 475 System.out.println(" Error input 1st time");
aoqi@0 476 invokeTest(t2, d2, expected, lt);
aoqi@0 477 System.out.println(" Good input (instance of Dok)");
aoqi@0 478 invokeTest(t2, Dok.class, null, lt);
aoqi@0 479 System.out.println(" Error input 2nd time");
aoqi@0 480 invokeTest(t2, d2, expected, lt);
aoqi@0 481 System.out.println(" Good input (instance of Dok)");
aoqi@0 482 invokeTest(t2, Dok.class, null, lt);
aoqi@0 483 }
aoqi@0 484
aoqi@0 485 private static void badGoodBadGoodMany(Class<?> t2, Class<?> d2, Class<?> expected, List<Throwable> lt)
aoqi@0 486 throws Throwable {
aoqi@0 487 System.out.println(" Error input 1st time");
aoqi@0 488 invokeTestMany(t2, d2, expected, lt);
aoqi@0 489 System.out.println(" Good input (instance of Dok)");
aoqi@0 490 invokeTestMany(t2, Dok.class, null, lt);
aoqi@0 491 System.out.println(" Error input 2nd time");
aoqi@0 492 invokeTestMany(t2, d2, expected, lt);
aoqi@0 493 System.out.println(" Good input (instance of Dok)");
aoqi@0 494 invokeTestMany(t2, Dok.class, null, lt);
aoqi@0 495 }
aoqi@0 496 }

mercurial