Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/RedefineClasses/RedefinePreviousVersions.java
41153 views
/*1* Copyright (c) 2016, 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* @bug 8165246 801031926* @summary Test has_previous_versions flag and processing during class unloading.27* @requires vm.jvmti28* @requires vm.opt.final.ClassUnloading29* @library /test/lib30* @modules java.base/jdk.internal.misc31* @modules java.compiler32* java.instrument33* jdk.jartool/sun.tools.jar34* @run driver RedefineClassHelper35* @run driver RedefinePreviousVersions test36*/3738import jdk.test.lib.process.ProcessTools;39import jdk.test.lib.process.OutputAnalyzer;4041// package access top-level classes to avoid problem with RedefineClassHelper42// and nested types.4344class RedefinePreviousVersions_B { }4546class RedefinePreviousVersions_Running {47public static volatile boolean stop = false;48public static volatile boolean running = false;49static void localSleep() {50try {51Thread.sleep(10); // sleep for 10 ms52} catch(InterruptedException ie) {53}54}5556public static void infinite() {57running = true;58while (!stop) { localSleep(); }59}60}61626364public class RedefinePreviousVersions {6566public static String newB =67"class RedefinePreviousVersions_B {" +68"}";6970public static String newRunning =71"class RedefinePreviousVersions_Running {" +72" public static volatile boolean stop = true;" +73" public static volatile boolean running = true;" +74" static void localSleep() { }" +75" public static void infinite() { }" +76"}";7778public static void main(String[] args) throws Exception {7980if (args.length > 0) {8182// java -javaagent:redefineagent.jar -Xlog:stuff RedefinePreviousVersions83ProcessBuilder pb = ProcessTools.createJavaProcessBuilder( "-javaagent:redefineagent.jar",84"-Xlog:redefine+class+iklass+add=trace,redefine+class+iklass+purge=trace",85"RedefinePreviousVersions");86new OutputAnalyzer(pb.start())87.shouldContain("Class unloading: has_previous_versions = false")88.shouldContain("Class unloading: has_previous_versions = true")89.shouldHaveExitValue(0);90return;91}9293// Redefine a class and create some garbage94// Since there are no methods running, the previous version is never added to the95// previous_version_list and the flag _has_previous_versions should stay false96RedefineClassHelper.redefineClass(RedefinePreviousVersions_B.class, newB);9798for (int i = 0; i < 10 ; i++) {99String s = new String("some garbage");100System.gc();101}102103// Start a class that has a method running104new Thread() {105public void run() {106RedefinePreviousVersions_Running.infinite();107}108}.start();109110while (!RedefinePreviousVersions_Running.running) {111Thread.sleep(10); // sleep for 10 ms112}113114// Since a method of newRunning is running, this class should be added to the previous_version_list115// of Running, and _has_previous_versions should return true at class unloading.116RedefineClassHelper.redefineClass(RedefinePreviousVersions_Running.class, newRunning);117118for (int i = 0; i < 10 ; i++) {119String s = new String("some garbage");120System.gc();121}122123// purge should clean everything up, except Xcomp it might not.124RedefinePreviousVersions_Running.stop = true;125126for (int i = 0; i < 10 ; i++) {127String s = new String("some garbage");128System.gc();129}130}131}132133134