< prev index next >

test/hotspot/jtreg/runtime/LoaderConstraints/itableLdrConstraint/Test.java

Print this page
rev 49654 : 8199852: Print more information about class loaders in LinkageErrors.


  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  */
  23 
  24 /*
  25  * @test
  26  * @bug 8186092
  27  * @compile ../common/Foo.java
  28  *          ../common/J.java
  29  *          I.java
  30  *          ../common/C.jasm
  31  *          Task.java
  32  *          ../common/PreemptingClassLoader.java
  33  * @run main/othervm Test
  34  */
  35 
  36 public class Test {
  37 









  38     // Test that the error message is correct when a loader constraint error is
  39     // detected during itable creation.
  40     //
  41     // In this test, during itable creation for class C, method "m()LFoo;" for
  42     // C's super interface I has a different class Foo than the selected method's
  43     // type super interface J.  The selected method is not an overpass method nor
  44     // otherwise excluded from loader constraint checking.  So, a LinkageError
  45     // exception should be thrown because the loader constraint check will fail.
  46     public static void main(String... args) throws Exception {
  47         Class<?> c = Foo.class; // forces standard class loader to load Foo
  48         ClassLoader l = new PreemptingClassLoader("Task", "Foo", "C", "I");
  49         Runnable r = (Runnable) l.loadClass("Task").newInstance();





























  50         try {
  51             r.run();
  52             throw new RuntimeException("Expected LinkageError exception not thrown");
  53         } catch (LinkageError e) {
  54             if (!e.getMessage().contains(
  55                 "loader constraint violation in interface itable initialization for class C:")) {
  56                 throw new RuntimeException("Wrong LinkageError exception thrown: " + e.getMessage());


  57             }

  58         }
  59     }






  60 }


  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  */
  23 
  24 /*
  25  * @test
  26  * @bug 8186092
  27  * @compile ../common/Foo.java
  28  *          ../common/J.java
  29  *          I.java
  30  *          ../common/C.jasm
  31  *          Task.java
  32  *          ../common/PreemptingClassLoader.java
  33  * @run main/othervm Test
  34  */
  35 
  36 public class Test {
  37 
  38     static String expectedErrorMessage1 =
  39         "loader constraint violation in interface itable initialization for class test.C: " +
  40         "when selecting method test.I.m()Ltest/Foo; the class loader instance of PreemptingClassLoader " +
  41         "(parent: \"app\" jdk.internal.loader.ClassLoaders$AppClassLoader) " +
  42         "for super interface test.I, and the class loader \"app\" " +
  43         "(instance of jdk.internal.loader.ClassLoaders$AppClassLoader, " +
  44         "parent: \"platform\" jdk.internal.loader.ClassLoaders$PlatformClassLoader) " +
  45         "of the selected method's type, test.J have different Class objects for the type test.Foo used in the signature";
  46 
  47     // Test that the error message is correct when a loader constraint error is
  48     // detected during itable creation.
  49     //
  50     // In this test, during itable creation for class C, method "m()LFoo;" for
  51     // C's super interface I has a different class Foo than the selected method's
  52     // type super interface J.  The selected method is not an overpass method nor
  53     // otherwise excluded from loader constraint checking.  So, a LinkageError
  54     // exception should be thrown because the loader constraint check will fail.
  55     public static void test1() throws Exception {
  56         Class<?> c = test.Foo.class; // forces standard class loader to load Foo
  57         ClassLoader l = new PreemptingClassLoader("test.Task", "test.Foo", "test.C", "test.I");
  58         Runnable r = (Runnable) l.loadClass("test.Task").newInstance();
  59         try {
  60             r.run();
  61             throw new RuntimeException("Expected LinkageError exception not thrown");
  62         } catch (LinkageError e) {
  63             String errorMsg = e.getMessage();
  64             if (!errorMsg.equals(expectedErrorMessage1)) {
  65                 System.out.println("Expected: " + expectedErrorMessage1 + "\n" +
  66                                    "but got:  " + errorMsg);
  67                 throw new RuntimeException("Wrong LinkageError exception thrown: " + errorMsg);
  68             }
  69             System.out.println("Passed with message: " + errorMsg);
  70         }
  71     }
  72 
  73     static String expectedErrorMessage2 =
  74         "loader constraint violation in interface itable initialization for class test.C: " +
  75         "when selecting method test.I.m()Ltest/Foo; the class loader \"ItableLdrCnstrnt_Test_Loader\" " +
  76         "(instance of PreemptingClassLoader, parent: \"app\" " +
  77         "jdk.internal.loader.ClassLoaders$AppClassLoader) for super interface test.I, " +
  78         "and the class loader \"app\" (instance of jdk.internal.loader.ClassLoaders$AppClassLoader, " +
  79         "parent: \"platform\" jdk.internal.loader.ClassLoaders$PlatformClassLoader) " +
  80         "of the selected method's type, test.J have different Class objects for the type test.Foo used in the signature";
  81 
  82     // Same as test1, but ClassLoader has a name.
  83     public static void test2() throws Exception {
  84         Class<?> c = test.Foo.class; // Forces standard class loader to load Foo.
  85         String[] classNames = {"test.Task", "test.Foo", "test.C", "test.I"};    
  86         ClassLoader l = new PreemptingClassLoader("ItableLdrCnstrnt_Test_Loader", classNames);    
  87         Runnable r = (Runnable) l.loadClass("test.Task").newInstance();
  88         try {
  89             r.run();
  90             throw new RuntimeException("Expected LinkageError exception not thrown");
  91         } catch (LinkageError e) {
  92             String errorMsg = e.getMessage();
  93             if (!errorMsg.equals(expectedErrorMessage2)) {
  94                 System.out.println("Expected: " + expectedErrorMessage2 + "\n" +
  95                                    "but got:  " + errorMsg);
  96                 throw new RuntimeException("Wrong LinkageError exception thrown: " + errorMsg);
  97             }
  98             System.out.println("Passed with message: " + errorMsg);
  99         }
 100     }
 101 
 102     public static void main(String... args) throws Exception {
 103         test1();
 104         test2();
 105     }
 106         
 107 }
< prev index next >