46 patchPrivateFinalize(); 47 48 test(new Base(10), 10); 49 test(new Subclass(20), 0); 50 test(new SubSubclass(30), 30); 51 test(new PublicFinalize(40), 40*100+40); 52 test(new PrivateFinalize(50), 50); 53 test(new NoOverride(60), 60); 54 } 55 56 static void test(Object o, int expected) { 57 int count = finalizedCount.get(); 58 int sum = finalizedSum.get(); 59 privateFinalizeInvoked = false; 60 61 // force GC and finalization 62 o = null; 63 while (finalizedCount.get() != (count+1)) { 64 System.gc(); 65 System.runFinalization(); 66 } 67 68 if (privateFinalizeInvoked) { 69 throw new RuntimeException("private finalize method invoked"); 70 } 71 if (finalizedCount.get() != (count+1)) { 72 throw new RuntimeException("Unexpected count=" + finalizedCount + 73 " expected=" + (count+1)); 74 } 75 if (finalizedSum.get() != (sum+expected)) { 76 throw new RuntimeException("Unexpected sum=" + finalizedSum + 77 " prev=" + sum + " value=" + expected); 78 } 79 } 80 81 static void patchPrivateFinalize() throws IOException { 82 // patch the private f_nal_ze method name to "finalize" 83 String testClasses = System.getProperty("test.classes", "."); 84 Path p = Paths.get(testClasses, "FinalizeOverride$PrivateFinalize.class"); 85 byte[] bytes = Files.readAllBytes(p); | 46 patchPrivateFinalize(); 47 48 test(new Base(10), 10); 49 test(new Subclass(20), 0); 50 test(new SubSubclass(30), 30); 51 test(new PublicFinalize(40), 40*100+40); 52 test(new PrivateFinalize(50), 50); 53 test(new NoOverride(60), 60); 54 } 55 56 static void test(Object o, int expected) { 57 int count = finalizedCount.get(); 58 int sum = finalizedSum.get(); 59 privateFinalizeInvoked = false; 60 61 // force GC and finalization 62 o = null; 63 while (finalizedCount.get() != (count+1)) { 64 System.gc(); 65 System.runFinalization(); 66 try { 67 Thread.sleep(1000); 68 } catch (InterruptedException e) { 69 System.out.println("Main thread interrupted, continuing execution."); 70 } 71 } 72 73 if (privateFinalizeInvoked) { 74 throw new RuntimeException("private finalize method invoked"); 75 } 76 if (finalizedCount.get() != (count+1)) { 77 throw new RuntimeException("Unexpected count=" + finalizedCount + 78 " expected=" + (count+1)); 79 } 80 if (finalizedSum.get() != (sum+expected)) { 81 throw new RuntimeException("Unexpected sum=" + finalizedSum + 82 " prev=" + sum + " value=" + expected); 83 } 84 } 85 86 static void patchPrivateFinalize() throws IOException { 87 // patch the private f_nal_ze method name to "finalize" 88 String testClasses = System.getProperty("test.classes", "."); 89 Path p = Paths.get(testClasses, "FinalizeOverride$PrivateFinalize.class"); 90 byte[] bytes = Files.readAllBytes(p); |