Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/ref/FinalizeOverride.java
38813 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.ByteArrayOutputStream;24import java.io.IOException;25import java.nio.file.Files;26import java.nio.file.Path;27import java.nio.file.Paths;28import java.util.concurrent.atomic.AtomicInteger;2930/* @test31* @bug 802735132* @summary Basic test of the finalize method33*/3435public class FinalizeOverride {36// finalizedCount is incremented when the finalize method is invoked37private static AtomicInteger finalizedCount = new AtomicInteger();3839// finalizedSum and privateFinalizedInvoke are used to verify40// the right overrided finalize method is invoked41private static AtomicInteger finalizedSum = new AtomicInteger();42private static volatile boolean privateFinalizeInvoked = false;4344public static void main(String[] argvs) throws IOException {45patchPrivateFinalize();4647test(new Base(10), 10);48test(new Subclass(20), 0);49test(new SubSubclass(30), 30);50test(new PublicFinalize(40), 40*100+40);51test(new PrivateFinalize(50), 50);52test(new NoOverride(60), 60);53}5455static void test(Object o, int expected) {56int count = finalizedCount.get();57int sum = finalizedSum.get();58privateFinalizeInvoked = false;5960// force GC and finalization61o = null;62while (finalizedCount.get() != (count+1)) {63System.gc();64System.runFinalization();65}6667if (privateFinalizeInvoked) {68throw new RuntimeException("private finalize method invoked");69}70if (finalizedCount.get() != (count+1)) {71throw new RuntimeException("Unexpected count=" + finalizedCount +72" expected=" + (count+1));73}74if (finalizedSum.get() != (sum+expected)) {75throw new RuntimeException("Unexpected sum=" + finalizedSum +76" prev=" + sum + " value=" + expected);77}78}7980static void patchPrivateFinalize() throws IOException {81// patch the private f_nal_ze method name to "finalize"82String testClasses = System.getProperty("test.classes", ".");83Path p = Paths.get(testClasses, "FinalizeOverride$PrivateFinalize.class");84byte[] bytes = Files.readAllBytes(p);85int len = "f_nal_ze".length();86for (int i=0; i < bytes.length-len; i++) {87if (bytes[i] == 'f' &&88bytes[i+1] == '_' &&89bytes[i+2] == 'n' &&90bytes[i+3] == 'a' &&91bytes[i+4] == 'l' &&92bytes[i+5] == '_' &&93bytes[i+6] == 'z' &&94bytes[i+7] == 'e')95{96// s%_%i%97bytes[i+1] = 'i';98bytes[i+5] = 'i';99break;100}101}102Files.write(p, bytes);103}104105static class Base {106protected int value;107Base(int v) {108this.value = v;109}110int called() {111finalizedSum.addAndGet(value);112return value;113}114protected void finalize() {115System.out.println("Base.finalize() sum += " + called());116finalizedCount.incrementAndGet();117}118}119static class PublicFinalize extends Base {120PublicFinalize(int v) {121super(v);122}123public void finalize() {124finalizedSum.addAndGet(value * 100);125System.out.println("PublicFinalize.finalize() sum += " + called() +126"+"+value+"*100");127finalizedCount.incrementAndGet();128}129}130static class Subclass extends Base {131Subclass(int v) {132super(v);133}134protected void finalize() {135// no value added to sum136System.out.println("Subclass.finalize() sum += 0");137finalizedCount.incrementAndGet();138}139}140static class SubSubclass extends Subclass {141SubSubclass(int v) {142super(v);143}144protected final void finalize() {145finalizedSum.addAndGet(value);146System.out.println("SubSubclass.finalize() sum +=" +value);147finalizedCount.incrementAndGet();148}149}150static class PrivateFinalize extends Base {151PrivateFinalize(int v) {152super(v);153}154private void f_nal_ze() {155// finalization catches any exception156System.out.println("Error: private finalize invoked!!");157privateFinalizeInvoked = true;158finalizedCount.incrementAndGet();159}160}161static class NoOverride extends PrivateFinalize {162NoOverride(int v) {163super(v);164}165}166}167168169