< prev index next >

test/hotspot/jtreg/runtime/exceptionMsgs/IllegalAccessError/IllegalAccessErrorTest.java

Print this page




 124         mv.visitInsn(RETURN);
 125         mv.visitMaxs(0, 0);
 126         mv.visitEnd();
 127 
 128         // run()
 129         String tc = targetClass.replace(".", "/");
 130         mv = cw.visitMethod(ACC_PUBLIC, "run", "()V", null, null);
 131         mv.visitMethodInsn(INVOKESTATIC, tc, targetMethod, "()V", false);
 132         mv.visitInsn(RETURN);
 133         mv.visitMaxs(0, 0);
 134         mv.visitEnd();
 135 
 136         cw.visitEnd();
 137         return cw.toByteArray();
 138     }
 139 
 140     // Private method that should raise IllegalAccessError when called.
 141     private static void iae4_m() { }
 142 
 143     private static String expectedErrorMessage4 =
 144         "class test.Runner4 tried to access private method 'void test.IllegalAccessErrorTest.iae4_m()' " +
 145         "(test.Runner4 and test.IllegalAccessErrorTest are in unnamed module of loader 'app')";
 146 
 147     // Test according to java/lang/invoke/DefineClassTest.java
 148     public static void test4_privateMethod() throws Exception {
 149         final String THIS_PACKAGE = IllegalAccessErrorTest.class.getPackageName();
 150         final String THIS_CLASS   = IllegalAccessErrorTest.class.getName();
 151         final String CLASS_NAME   = THIS_PACKAGE + ".Runner4";
 152         Lookup lookup = lookup();
 153 
 154         // private
 155         byte[] classBytes = iae4_generateRunner(CLASS_NAME, THIS_CLASS, "iae4_m");
 156         Class<?> clazz = lookup.defineClass(classBytes);
 157         Runnable r = (Runnable) clazz.getDeclaredConstructor().newInstance();
 158         try {
 159             r.run();
 160             throw new RuntimeException("Expected IllegalAccessError was not thrown.");
 161         } catch (IllegalAccessError exc) {
 162             String errorMsg = exc.getMessage();
 163             if (!errorMsg.equals(expectedErrorMessage4)) {
 164                 System.out.println("Expected: " + expectedErrorMessage4 + "\n" +


 247         Class<?> cl = loader.loadClass(IAE6_B.class.getName());
 248         Method m = cl.getDeclaredMethod("create", new Class[0]);
 249         m.setAccessible(true);
 250 
 251         try {
 252             m.invoke(null, new Object[0]);
 253             throw new RuntimeException("Expected IllegalAccessError was not thrown.");
 254         } catch (InvocationTargetException e) {
 255             IllegalAccessError iae = (IllegalAccessError) e.getCause();
 256             String errorMsg = iae.getMessage();
 257             if (!errorMsg.contains(expectedErrorMessage6)) {
 258                 System.out.println("Expected: " + expectedErrorMessage6 + "id)\n" +
 259                                    "but got:  " + errorMsg);
 260                 throw new RuntimeException("Wrong error message of IllegalAccessError.");
 261             }
 262             System.out.println("Passed with message: " + errorMsg);
 263         }
 264     }
 265 
 266     private static String expectedErrorMessage7_1 =
 267         "class test.IAE78_B tried to access method 'void test.IAE78_A.<init>()' " +
 268         "(test.IAE78_B is in unnamed module of loader 'test7_method_CL' @";
 269     private static String expectedErrorMessage7_2 =
 270         "; test.IAE78_A is in unnamed module of loader 'app')";
 271 
 272     // Similar to test4.
 273     public static void test7_method() throws Exception {
 274         ClassLoader base = IllegalAccessErrorTest.class.getClassLoader();
 275         IAE_Loader2 loader = new IAE_Loader2("test7_method_CL", base.getParent(), base, new String[0],
 276                 new String[] {IAE78_A.class.getName()});
 277         Class<?> cl = loader.loadClass(IAE78_B.class.getName());
 278         Method m = cl.getDeclaredMethod("create", new Class[0]);
 279 
 280         try {
 281             m.invoke(null, new Object[0]);
 282         } catch (InvocationTargetException e) {
 283             IllegalAccessError iae = (IllegalAccessError) e.getCause();
 284             String errorMsg = iae.getMessage();
 285             if (!(errorMsg.contains(expectedErrorMessage7_1) &&
 286                   errorMsg.contains(expectedErrorMessage7_2))) {
 287                 System.out.println("Expected: " + expectedErrorMessage7_1 + "id" + expectedErrorMessage7_2 + "\n" +




 124         mv.visitInsn(RETURN);
 125         mv.visitMaxs(0, 0);
 126         mv.visitEnd();
 127 
 128         // run()
 129         String tc = targetClass.replace(".", "/");
 130         mv = cw.visitMethod(ACC_PUBLIC, "run", "()V", null, null);
 131         mv.visitMethodInsn(INVOKESTATIC, tc, targetMethod, "()V", false);
 132         mv.visitInsn(RETURN);
 133         mv.visitMaxs(0, 0);
 134         mv.visitEnd();
 135 
 136         cw.visitEnd();
 137         return cw.toByteArray();
 138     }
 139 
 140     // Private method that should raise IllegalAccessError when called.
 141     private static void iae4_m() { }
 142 
 143     private static String expectedErrorMessage4 =
 144         "class test.Runner4 tried to access private method test.IllegalAccessErrorTest.iae4_m()V " +
 145         "(test.Runner4 and test.IllegalAccessErrorTest are in unnamed module of loader 'app')";
 146 
 147     // Test according to java/lang/invoke/DefineClassTest.java
 148     public static void test4_privateMethod() throws Exception {
 149         final String THIS_PACKAGE = IllegalAccessErrorTest.class.getPackageName();
 150         final String THIS_CLASS   = IllegalAccessErrorTest.class.getName();
 151         final String CLASS_NAME   = THIS_PACKAGE + ".Runner4";
 152         Lookup lookup = lookup();
 153 
 154         // private
 155         byte[] classBytes = iae4_generateRunner(CLASS_NAME, THIS_CLASS, "iae4_m");
 156         Class<?> clazz = lookup.defineClass(classBytes);
 157         Runnable r = (Runnable) clazz.getDeclaredConstructor().newInstance();
 158         try {
 159             r.run();
 160             throw new RuntimeException("Expected IllegalAccessError was not thrown.");
 161         } catch (IllegalAccessError exc) {
 162             String errorMsg = exc.getMessage();
 163             if (!errorMsg.equals(expectedErrorMessage4)) {
 164                 System.out.println("Expected: " + expectedErrorMessage4 + "\n" +


 247         Class<?> cl = loader.loadClass(IAE6_B.class.getName());
 248         Method m = cl.getDeclaredMethod("create", new Class[0]);
 249         m.setAccessible(true);
 250 
 251         try {
 252             m.invoke(null, new Object[0]);
 253             throw new RuntimeException("Expected IllegalAccessError was not thrown.");
 254         } catch (InvocationTargetException e) {
 255             IllegalAccessError iae = (IllegalAccessError) e.getCause();
 256             String errorMsg = iae.getMessage();
 257             if (!errorMsg.contains(expectedErrorMessage6)) {
 258                 System.out.println("Expected: " + expectedErrorMessage6 + "id)\n" +
 259                                    "but got:  " + errorMsg);
 260                 throw new RuntimeException("Wrong error message of IllegalAccessError.");
 261             }
 262             System.out.println("Passed with message: " + errorMsg);
 263         }
 264     }
 265 
 266     private static String expectedErrorMessage7_1 =
 267         "class test.IAE78_B tried to access method test.IAE78_A.<init>()V " +
 268         "(test.IAE78_B is in unnamed module of loader 'test7_method_CL' @";
 269     private static String expectedErrorMessage7_2 =
 270         "; test.IAE78_A is in unnamed module of loader 'app')";
 271 
 272     // Similar to test4.
 273     public static void test7_method() throws Exception {
 274         ClassLoader base = IllegalAccessErrorTest.class.getClassLoader();
 275         IAE_Loader2 loader = new IAE_Loader2("test7_method_CL", base.getParent(), base, new String[0],
 276                 new String[] {IAE78_A.class.getName()});
 277         Class<?> cl = loader.loadClass(IAE78_B.class.getName());
 278         Method m = cl.getDeclaredMethod("create", new Class[0]);
 279 
 280         try {
 281             m.invoke(null, new Object[0]);
 282         } catch (InvocationTargetException e) {
 283             IllegalAccessError iae = (IllegalAccessError) e.getCause();
 284             String errorMsg = iae.getMessage();
 285             if (!(errorMsg.contains(expectedErrorMessage7_1) &&
 286                   errorMsg.contains(expectedErrorMessage7_2))) {
 287                 System.out.println("Expected: " + expectedErrorMessage7_1 + "id" + expectedErrorMessage7_2 + "\n" +


< prev index next >