Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/RedefineClasses/RedefinePreviousVersions.java
66646 views
/*1* Copyright (c) 2016, 2021, 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* @bug 8165246 801031926* @summary Test has_previous_versions flag and processing during class unloading.27* @requires vm.jvmti28* @requires vm.opt.final.ClassUnloading29* @requires vm.flagless30* @library /test/lib31* @modules java.base/jdk.internal.misc32* @modules java.compiler33* java.instrument34* jdk.jartool/sun.tools.jar35* @run driver RedefineClassHelper36* @run driver RedefinePreviousVersions test37*/3839import jdk.test.lib.process.ProcessTools;40import jdk.test.lib.process.OutputAnalyzer;4142// package access top-level classes to avoid problem with RedefineClassHelper43// and nested types.4445class RedefinePreviousVersions_B { }4647class RedefinePreviousVersions_Running {48public static volatile boolean stop = false;49public static volatile boolean running = false;50static void localSleep() {51try {52Thread.sleep(10); // sleep for 10 ms53} catch(InterruptedException ie) {54}55}5657public static void infinite() {58running = true;59while (!stop) { localSleep(); }60}61}62636465public class RedefinePreviousVersions {6667public static String newB =68"class RedefinePreviousVersions_B {" +69"}";7071public static String newRunning =72"class RedefinePreviousVersions_Running {" +73" public static volatile boolean stop = true;" +74" public static volatile boolean running = true;" +75" static void localSleep() { }" +76" public static void infinite() { }" +77"}";7879public static void main(String[] args) throws Exception {8081if (args.length > 0) {8283// java -javaagent:redefineagent.jar -Xlog:stuff RedefinePreviousVersions84ProcessBuilder pb = ProcessTools.createJavaProcessBuilder( "-javaagent:redefineagent.jar",85"-Xlog:redefine+class+iklass+add=trace,redefine+class+iklass+purge=trace",86"RedefinePreviousVersions");87new OutputAnalyzer(pb.start())88.shouldContain("Class unloading: has_previous_versions = false")89.shouldContain("Class unloading: has_previous_versions = true")90.shouldHaveExitValue(0);91return;92}9394// Redefine a class and create some garbage95// Since there are no methods running, the previous version is never added to the96// previous_version_list and the flag _has_previous_versions should stay false97RedefineClassHelper.redefineClass(RedefinePreviousVersions_B.class, newB);9899for (int i = 0; i < 10 ; i++) {100String s = new String("some garbage");101System.gc();102}103104// Start a class that has a method running105new Thread() {106public void run() {107RedefinePreviousVersions_Running.infinite();108}109}.start();110111while (!RedefinePreviousVersions_Running.running) {112Thread.sleep(10); // sleep for 10 ms113}114115// Since a method of newRunning is running, this class should be added to the previous_version_list116// of Running, and _has_previous_versions should return true at class unloading.117RedefineClassHelper.redefineClass(RedefinePreviousVersions_Running.class, newRunning);118119for (int i = 0; i < 10 ; i++) {120String s = new String("some garbage");121System.gc();122}123124// purge should clean everything up, except Xcomp it might not.125RedefinePreviousVersions_Running.stop = true;126127for (int i = 0; i < 10 ; i++) {128String s = new String("some garbage");129System.gc();130}131}132}133134135