1 /* 2 * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import java.lang.instrument.ClassFileTransformer; 25 import java.lang.instrument.Instrumentation; 26 import java.util.Map; 27 import java.util.Set; 28 29 /** 30 * Agent used by TraceUsageTest. The premain and agentmain methods invoke Instrumentation 31 * methods so the usages can be traced by the test. 32 */ 33 public class TraceUsageAgent { 34 public static void premain(String methodNames, Instrumentation inst) throws Exception { 35 test(methodNames, inst); 36 } 37 38 public static void agentmain(String methodNames, Instrumentation inst) throws Exception { 39 test(methodNames, inst); 40 } 41 42 private static void test(String methodNames, Instrumentation inst) throws Exception { 43 for (String methodName : methodNames.split(",")) { 44 switch (methodName) { 45 case "addTransformer" -> { 46 var transformer = new ClassFileTransformer() { }; 47 inst.addTransformer(transformer); 48 } 49 case "retransformClasses" -> { 50 inst.retransformClasses(Object.class); 51 } 52 case "redefineModule" -> { 53 Module base = Object.class.getModule(); 54 inst.redefineModule(base, Set.of(), Map.of(), Map.of(), Set.of(), Map.of()); 55 } 56 default -> { 57 throw new RuntimeException("Unknown method name: " + methodName); 58 } 59 } 60 } 61 } 62 }