Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/ClassDeallocGC/ClassDeallocGC.java
41155 views
/*1* Copyright (c) 2002, 2020, 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*/2223/*24* @test25*26* @summary converted from VM Testbase gc/gctests/ClassDeallocGC.27* VM Testbase keywords: [gc]28* VM Testbase readme:29* This test is a slightly modified version of a test taken from a research paper30* by CE McDowell of UCSC titled 'No so static "static fields"'.31* To quote from the paper :32* " In this paper we have identified a problem with the Java Language33* Specification with regard to class unloading and static fields. We have34* provided an example that demonstrates how the current implementation of35* class unloading can result in a static field being reinitialized resulting36* in a program that changes its behavior depending upon when garbage collection occurs."37* In this test, the creation of the object first assigned to38* class_one_object (actually an instance of ClassOne) also creates an instance39* of ClassTwo which contains a static field. Once the references to the ClassOne40* object and the Class object are set to null, a call to the garbage41* collector results in both ClassOne and ClassTwo being unloaded. When42* the final assignment to class_one_object occurs, creating a new instance of ClassOne43* and a new instance of ClassTwo, the program should print out the value of counter in44* ClassTwo as 2. This would mean that class was not unloaded and the static field45* was not reinitialized. If the test prints out 1, the test has failed as the static46* field was reinitilized.47*48* @library /vmTestbase49* /test/lib50* @run main/othervm gc.gctests.ClassDeallocGC.ClassDeallocGC51*/5253package gc.gctests.ClassDeallocGC;5455import nsk.share.TestFailure;5657public class ClassDeallocGC58{59public static void main(String[] args)60throws java.io.IOException, ClassNotFoundException,61IllegalAccessException, InstantiationException62{63int count = 0, counter;64ClassOne class_one_object;6566/* load the class ClassOne and instantiate an instance of it */67/* ClassOne uses ClassTwo which will thus get loaded also */68Class theClass = Class.forName("gc.gctests.ClassDeallocGC.ClassOne");69class_one_object = (ClassOne)theClass.newInstance();7071/* remove all references to the class and the instance */72class_one_object = null;73theClass = null;7475System.gc(); /* force garbage collection which also unloads classes */7677/*loads and instantiates ClassOne again. ClassTwo also gets reloaded*/78class_one_object = (ClassOne) new ClassOne();79if ( (counter = class_one_object.getCounter()) == 2 ) {80System.out.println("Test Passed.");81} else {82throw new TestFailure("Test failed. counter = " + counter + ", should be 2.");83}84}85}8687class ClassOne {88ClassTwo class_two;89public ClassOne(){90class_two = new ClassTwo();91}92public int getCounter() {93return class_two.getCounter();94}95}969798class ClassTwo {99static int counter = 0;100public ClassTwo(){101counter++;102}103public int getCounter() {104return counter;105}106}107108109