Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/AttachOnDemand/attach030/attach030Agent00.java
40951 views
/*1* Copyright (c) 2007, 2018, 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*/22package nsk.jvmti.AttachOnDemand.attach030;2324import nsk.share.TestBug;25import nsk.share.aod.AbstractJarAgent;26import java.lang.instrument.Instrumentation;2728/*29* First agent tries to redefine class loaded by the target application, then agent itself loads30* class and redefines it31*/32public class attach030Agent00 extends AbstractJarAgent {3334private static final String classToRedefine1 = "nsk.jvmti.AttachOnDemand.attach030.ClassToRedefine01";3536private static final String classToRedefine2 = "nsk.jvmti.AttachOnDemand.attach030.ClassToRedefine02";3738protected void init(String[] args) {39if (pathToNewByteCode() == null)40throw new TestBug("Path to new byte code wasn't specified (" + pathToNewByteCodeOption + "=...)");41}4243protected void agentActions() throws Throwable {44boolean classWasFound = false;4546for (Class<?> klass : inst.getAllLoadedClasses()) {47/*48* When agent is running class 'classToRedefine1' should be49* already loaded by the target application50*/51if (klass.getName().equals(classToRedefine1)) {52classWasFound = true;53display("Trying to redefine class '" + classToRedefine1 + "'");54redefineClass(klass);55display("Class was redefined");56break;57}58}5960if (!classWasFound) {61setStatusFailed("Instrumentation.getAllLoadedClasses() didn't return class '" + classToRedefine1 + "'");62}6364/*65* Try to load and redefine 'classToRedefine2'66*/6768Class<?> klass = Class.forName(classToRedefine2);69display("Trying to redefine class '" + classToRedefine2 + "'");70redefineClass(klass);71display("Class was redefined");7273final String expectedString = "ClassToRedefine02: Class is redefined";74ClassToRedefine02 instance = new ClassToRedefine02();75String string = instance.getString();76display("ClassToRedefine02.getString(): " + string);77if (!string.equals(expectedString)) {78setStatusFailed("ClassToRedefine02.getString() returned unexpected value , expected is '" + expectedString + "'");79}80}8182public static void agentmain(String options, Instrumentation inst) {83new attach030Agent00().runJarAgent(options, inst);84}85}868788