diff options
author | 2024-03-12 14:28:59 +0000 | |
---|---|---|
committer | 2024-03-14 14:29:03 +0000 | |
commit | 30ee8539ccf4dcae18b428bc98133296b0238e3f (patch) | |
tree | e77d0f6e283c9e4f746d6464d5877a7ada45d50c | |
parent | 8686d25b2aa32dd002ed8fc861a8446f1cdd6365 (diff) |
Allow for JDK 21 exception in 174-escaping-instance-of-bad-class
Bug: 313924276
Test: ./art/test/testrunner/run_build_test_target.py -j80 art-test-javac
Change-Id: I0662ba936e0bdbb5ca2c54ecc4276964c2f9dbe2
-rw-r--r-- | test/174-escaping-instance-of-bad-class/src/Main.java | 56 |
1 files changed, 22 insertions, 34 deletions
diff --git a/test/174-escaping-instance-of-bad-class/src/Main.java b/test/174-escaping-instance-of-bad-class/src/Main.java index cee978ad85..dda3092383 100644 --- a/test/174-escaping-instance-of-bad-class/src/Main.java +++ b/test/174-escaping-instance-of-bad-class/src/Main.java @@ -32,23 +32,13 @@ public class Main { try { bad.foo(); } catch (NoClassDefFoundError ncdfe) { - // On RI, the NCDFE has no cause. On ART, the badClinit is the cause. - if (ncdfe.getCause() == badClinit || ncdfe.getCause() == null) { - System.out.println("Caught NoClassDefFoundError."); - } else { - ncdfe.printStackTrace(); - } + checkNcdfe(ncdfe, badClinit); } // Call bar() on the escaped instance of Bad. try { bad.bar(); } catch (NoClassDefFoundError ncdfe) { - // On RI, the NCDFE has no cause. On ART, the badClinit is the cause. - if (ncdfe.getCause() == badClinit || ncdfe.getCause() == null) { - System.out.println("Caught NoClassDefFoundError."); - } else { - ncdfe.printStackTrace(); - } + checkNcdfe(ncdfe, badClinit); } // Test that we handle bad instance correctly in the resolution trampoline. @@ -67,36 +57,21 @@ public class Main { try { badSuper.foo(); } catch (NoClassDefFoundError ncdfe) { - // On RI, the NCDFE has no cause. On ART, the badClinit is the cause. - if (ncdfe.getCause() == badClinit || ncdfe.getCause() == null) { - System.out.println("Caught NoClassDefFoundError."); - } else { - ncdfe.printStackTrace(); - } + checkNcdfe(ncdfe, badClinit); } // Call BadSub.bar() on the escaped instance of BadSub. try { badSub.bar(); } catch (NoClassDefFoundError ncdfe) { - // On RI, the NCDFE has no cause. On ART, the badClinit is the cause. - if (ncdfe.getCause() == badClinit || ncdfe.getCause() == null) { - System.out.println("Caught NoClassDefFoundError."); - } else { - ncdfe.printStackTrace(); - } + checkNcdfe(ncdfe, badClinit); } // Test that we can even create instances of BadSub with erroneous superclass BadSuper. try { new BadSub(-1, -2).bar(); } catch (NoClassDefFoundError ncdfe) { - // On RI, the NCDFE has no cause. On ART, the badClinit is the cause. - if (ncdfe.getCause() == badClinit || ncdfe.getCause() == null) { - System.out.println("Caught NoClassDefFoundError."); - } else { - ncdfe.printStackTrace(); - } + checkNcdfe(ncdfe, badClinit); } // Test that we cannot create instances of BadSuper from BadSub. @@ -104,13 +79,26 @@ public class Main { badSub.allocSuper(11111); // Should throw. System.out.println("Allocated BadSuper!"); } catch (NoClassDefFoundError ncdfe) { - // On RI, the NCDFE has no cause. On ART, the badClinit is the cause. - if (ncdfe.getCause() == badClinit || ncdfe.getCause() == null) { + checkNcdfe(ncdfe, badClinit); + } + } + + private static void checkNcdfe(NoClassDefFoundError ncdfe, Throwable expectedCause) { + // On RI, the NCDFE has no cause. On ART, the badClinit is the cause. + if (ncdfe.getCause() == expectedCause || ncdfe.getCause() == null) { + System.out.println("Caught NoClassDefFoundError."); + return; + } + // On newer RI, the NCDFE has an ExceptionInInitializerError cause which contains the string of + // the original error, but have a null cause and exception. + if (ncdfe.getCause() instanceof ExceptionInInitializerError) { + ExceptionInInitializerError cause = (ExceptionInInitializerError) ncdfe.getCause(); + if (cause.getException() == null) { System.out.println("Caught NoClassDefFoundError."); - } else { - ncdfe.printStackTrace(); + return; } } + ncdfe.printStackTrace(); } public static Bad bad; |