Path: blob/master/test/jdk/com/sun/tools/attach/RedefineAgent.java
41153 views
/*1* Copyright (c) 2006, 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*/2223/*24*25*26* @bug 6439234 644694127*28* Test attach used in concert with transformers and redefineClasses.29*30* 6439234 java.lang.instrument: 8 JCK JPLIS tests fail when running in Live phase (no transform events)31* 6446941 java.lang.instrument: multiple agent attach fails (first agent chooses capabilities)32*/33import java.net.Socket;34import java.net.InetSocketAddress;35import java.io.IOException;36import java.util.Arrays;37import java.security.ProtectionDomain;38import java.lang.instrument.ClassFileTransformer;39import java.lang.instrument.Instrumentation;40import java.lang.instrument.ClassDefinition;4142public class RedefineAgent implements ClassFileTransformer {4344static byte[] classfilebytes;45static final String targetName = "RedefineDummy";46static final String targetNameSlashes = "RedefineDummy";47static boolean gotRedefineTransform = false;4849// test transform and capture class bytes for redefine50public byte[] transform(ClassLoader loader,51String className,52Class<?> classBeingRedefined,53ProtectionDomain protectionDomain,54byte[] classfileBuffer) {55if (className.equals(targetNameSlashes)) {56if (classBeingRedefined == null) {57System.out.println("Got bytes for: " + className);58classfilebytes = Arrays.copyOf(classfileBuffer, classfileBuffer.length);59} else {60gotRedefineTransform = true;61}62}63return null;64}6566// test transform and redefine for an attached agent67public static void testRedefine(Instrumentation inst) throws Exception {68Class[] classes = inst.getAllLoadedClasses();69for (Class k : classes) {70if (k.getName().equals(targetName)) {71throw new Exception("RedefineAgent Test error: class " + targetName + " has already been loaded.");72}73}74inst.addTransformer(new RedefineAgent());75ClassLoader.getSystemClassLoader().loadClass(targetName);76classes = inst.getAllLoadedClasses();77Class targetClass = null;78for (Class k : classes) {79if (k.getName().equals(targetName)) {80targetClass = k;81break;82}83}84if (targetClass == null) {85throw new Exception("RedefineAgent Test error: class " + targetName + " not loaded.");86}87if (classfilebytes == null) {88throw new Exception("RedefineAgent Error(6439234): no transform call for class " + targetName);89}90ClassDefinition cd = new ClassDefinition(targetClass, classfilebytes);91inst.redefineClasses(cd);92System.out.println("RedefineAgent did redefine.");93if (gotRedefineTransform) {94System.out.println("RedefineAgent got redefine transform.");95} else {96throw new Exception("RedefineAgent Error(6439234): no transform call for redefine " + targetName);97}98}99100public static void agentmain(String args, Instrumentation inst) throws Exception {101System.out.println("RedefineAgent running...");102System.out.println("RedefineAgent redefine supported: " + inst.isRedefineClassesSupported());103int port = Integer.parseInt(args);104System.out.println("RedefineAgent connecting back to Tool....");105Socket s = new Socket();106s.connect( new InetSocketAddress(port) );107System.out.println("RedefineAgent connected to Tool.");108109testRedefine(inst);110111s.close();112}113114}115116117