Path: blob/master/test/hotspot/jtreg/testlibrary/jvmti/TransformerAgent.java
40948 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*/2223import java.lang.instrument.ClassFileTransformer;24import java.lang.instrument.IllegalClassFormatException;25import java.lang.instrument.Instrumentation;26import java.security.ProtectionDomain;27import java.util.HashMap;2829// This is a test utility class used to transform30// specified classes via initial transformation (ClassFileLoadHook).31// Names of classes to be transformed are supplied as arguments,32// the phrase to be transformed is a hard-coded predefined33// fairly unique phrase.3435public class TransformerAgent {36private static String[] classesToTransform;373839private static void log(String msg) {40System.out.println("TransformerAgent: " + msg);41}424344// arguments are comma-separated list of classes to transform45public static void premain(String agentArguments, Instrumentation instrumentation) {46log("premain() is called, arguments = " + agentArguments);47classesToTransform = agentArguments.split(",");48instrumentation.addTransformer(new SimpleTransformer(), /*canRetransform=*/true);49}505152public static void agentmain(String args, Instrumentation inst) throws Exception {53log("agentmain() is called");54premain(args, inst);55}565758static class SimpleTransformer implements ClassFileTransformer {59public byte[] transform(ClassLoader loader, String name, Class<?> classBeingRedefined,60ProtectionDomain pd, byte[] buffer) throws IllegalClassFormatException {61try {62log("SimpleTransformer called for: " + name + "@" + incrCounter(name));63if (!shouldTransform(name))64return null;6566log("transforming: class name = " + name);67int nrOfReplacements = TransformUtil.replace(buffer, TransformUtil.BeforePattern,68TransformUtil.AfterPattern);69log("replaced the string, nrOfReplacements = " + nrOfReplacements);70} catch (Throwable t) {71// The retransform native code that called this method does not propagate72// exceptions. Instead of getting an uninformative generic error, catch73// problems here and print it, then exit.74log("Transformation failed!");75t.printStackTrace();76System.exit(1);77}78return buffer;79}8081// Check class name pattern, since test should only transform certain classes82private static boolean shouldTransform(String name) {83for (String match : classesToTransform) {84if (name.matches(match)) {85log("shouldTransform: match-found, match = " + match);86return true;87}88}8990return false;91}92}939495static HashMap<String, Integer> counterMap = new HashMap<>();9697static Integer incrCounter(String className) {98Integer i = counterMap.get(className);99if (i == null) {100i = Integer.valueOf(1);101} else {102i = Integer.valueOf(i.intValue() + 1);103}104counterMap.put(className, i);105return i;106}107}108109110