12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
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 import java.io.ByteArrayOutputStream;
25 import java.io.IOException;
26 import java.nio.file.Files;
27 import java.nio.file.Path;
28 import java.nio.file.Paths;
29 import java.util.concurrent.atomic.AtomicInteger;
30
31 /* @test
32 * @bug 8027351
33 * @summary Basic test of the finalize method
34 */
35
36 public class FinalizeOverride {
37 // finalizedCount is incremented when the finalize method is invoked
38 private static AtomicInteger finalizedCount = new AtomicInteger();
39
40 // finalizedSum and privateFinalizedInvoke are used to verify
41 // the right overrided finalize method is invoked
42 private static AtomicInteger finalizedSum = new AtomicInteger();
43 private static volatile boolean privateFinalizeInvoked = false;
44
45 public static void main(String[] argvs) throws IOException {
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);
|
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
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 import java.io.ByteArrayOutputStream;
25 import java.io.IOException;
26 import java.nio.file.Files;
27 import java.nio.file.Path;
28 import java.nio.file.Paths;
29 import java.util.concurrent.atomic.AtomicInteger;
30
31 /* @test
32 * @bug 8027351 8148940
33 * @summary Basic test of the finalize method
34 */
35
36 public class FinalizeOverride {
37 // finalizedCount is incremented when the finalize method is invoked
38 private static AtomicInteger finalizedCount = new AtomicInteger();
39
40 // finalizedSum and privateFinalizedInvoke are used to verify
41 // the right overrided finalize method is invoked
42 private static AtomicInteger finalizedSum = new AtomicInteger();
43 private static volatile boolean privateFinalizeInvoked = false;
44
45 public static void main(String[] argvs) throws IOException {
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 // Running System.gc() and System.runFinalization() in a
67 // tight loop can trigger frequent safepointing that slows
68 // down the VM and, as a result, the test. (With the
69 // HotSpot VM, the effect of frequent safepointing is
70 // especially noticeable if the test is run with the
71 // -Xcomp flag.) Sleeping for a second after every
72 // garbage collection and finalization cycle gives the VM
73 // time to make progress.
74 try {
75 Thread.sleep(1000);
76 } catch (InterruptedException e) {
77 System.out.println("Main thread interrupted, continuing execution.");
78 }
79 }
80
81 if (privateFinalizeInvoked) {
82 throw new RuntimeException("private finalize method invoked");
83 }
84 if (finalizedCount.get() != (count+1)) {
85 throw new RuntimeException("Unexpected count=" + finalizedCount +
86 " expected=" + (count+1));
87 }
88 if (finalizedSum.get() != (sum+expected)) {
89 throw new RuntimeException("Unexpected sum=" + finalizedSum +
90 " prev=" + sum + " value=" + expected);
91 }
92 }
93
94 static void patchPrivateFinalize() throws IOException {
95 // patch the private f_nal_ze method name to "finalize"
96 String testClasses = System.getProperty("test.classes", ".");
97 Path p = Paths.get(testClasses, "FinalizeOverride$PrivateFinalize.class");
98 byte[] bytes = Files.readAllBytes(p);
|