1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
/*
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.atomic.AtomicInteger;
/* @test
* @bug 8027351 8148940
* @summary Basic test of the finalize method
*/
public class FinalizeOverride {
// finalizedCount is incremented when the finalize method is invoked
private static AtomicInteger finalizedCount = new AtomicInteger();
// finalizedSum and privateFinalizedInvoke are used to verify
// the right overrided finalize method is invoked
private static AtomicInteger finalizedSum = new AtomicInteger();
private static volatile boolean privateFinalizeInvoked = false;
public static void main(String[] argvs) throws IOException {
patchPrivateFinalize();
test(new Base(10), 10);
test(new Subclass(20), 0);
test(new SubSubclass(30), 30);
test(new PublicFinalize(40), 40*100+40);
test(new PrivateFinalize(50), 50);
test(new NoOverride(60), 60);
}
static void test(Object o, int expected) {
int count = finalizedCount.get();
int sum = finalizedSum.get();
privateFinalizeInvoked = false;
// force GC and finalization
o = null;
while (finalizedCount.get() != (count+1)) {
System.gc();
System.runFinalization();
// Running System.gc() and System.runFinalization() in a
// tight loop can trigger frequent safepointing that slows
// down the VM and, as a result, the test. (With the
// HotSpot VM, the effect of frequent safepointing is
// especially noticeable if the test is run with the
// -Xcomp flag.) Sleeping for a second after every
// garbage collection and finalization cycle gives the VM
// time to make progress.
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Main thread interrupted, continuing execution.");
}
}
if (privateFinalizeInvoked) {
throw new RuntimeException("private finalize method invoked");
}
if (finalizedCount.get() != (count+1)) {
throw new RuntimeException("Unexpected count=" + finalizedCount +
" expected=" + (count+1));
}
if (finalizedSum.get() != (sum+expected)) {
throw new RuntimeException("Unexpected sum=" + finalizedSum +
" prev=" + sum + " value=" + expected);
}
}
static void patchPrivateFinalize() throws IOException {
// patch the private f_nal_ze method name to "finalize"
String testClasses = System.getProperty("test.classes", ".");
Path p = Paths.get(testClasses, "FinalizeOverride$PrivateFinalize.class");
byte[] bytes = Files.readAllBytes(p);
int len = "f_nal_ze".length();
for (int i=0; i < bytes.length-len; i++) {
if (bytes[i] == 'f' &&
bytes[i+1] == '_' &&
bytes[i+2] == 'n' &&
bytes[i+3] == 'a' &&
bytes[i+4] == 'l' &&
bytes[i+5] == '_' &&
bytes[i+6] == 'z' &&
bytes[i+7] == 'e')
{
// s%_%i%
bytes[i+1] = 'i';
bytes[i+5] = 'i';
break;
}
}
Files.write(p, bytes);
}
static class Base {
protected int value;
Base(int v) {
this.value = v;
}
int called() {
finalizedSum.addAndGet(value);
return value;
}
protected void finalize() {
System.out.println("Base.finalize() sum += " + called());
finalizedCount.incrementAndGet();
}
}
static class PublicFinalize extends Base {
PublicFinalize(int v) {
super(v);
}
public void finalize() {
finalizedSum.addAndGet(value * 100);
System.out.println("PublicFinalize.finalize() sum += " + called() +
"+"+value+"*100");
finalizedCount.incrementAndGet();
}
}
static class Subclass extends Base {
Subclass(int v) {
super(v);
}
protected void finalize() {
// no value added to sum
System.out.println("Subclass.finalize() sum += 0");
finalizedCount.incrementAndGet();
}
}
static class SubSubclass extends Subclass {
SubSubclass(int v) {
super(v);
}
protected final void finalize() {
finalizedSum.addAndGet(value);
System.out.println("SubSubclass.finalize() sum +=" +value);
finalizedCount.incrementAndGet();
}
}
static class PrivateFinalize extends Base {
PrivateFinalize(int v) {
super(v);
}
private void f_nal_ze() {
// finalization catches any exception
System.out.println("Error: private finalize invoked!!");
privateFinalizeInvoked = true;
finalizedCount.incrementAndGet();
}
}
static class NoOverride extends PrivateFinalize {
NoOverride(int v) {
super(v);
}
}
}
|