Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/runtime/RedefineObject/Agent.java
32284 views
/*1* Copyright (c) 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*/22import java.security.*;23import java.lang.instrument.*;24import java.lang.reflect.*;2526public class Agent implements ClassFileTransformer {27public synchronized byte[] transform(final ClassLoader classLoader,28final String className,29Class<?> classBeingRedefined,30ProtectionDomain protectionDomain,31byte[] classfileBuffer) {32System.out.println("Transforming class " + className);33return classfileBuffer;34}3536public static void redefine(String agentArgs, Instrumentation instrumentation, Class to_redefine) {3738try {39instrumentation.retransformClasses(to_redefine);40} catch (Exception e) {41e.printStackTrace();42}4344}4546public static void premain(String agentArgs, Instrumentation instrumentation) {47Agent transformer = new Agent();48instrumentation.addTransformer(transformer, true);4950// Redefine java/lang/Object and java/lang/reflect/Method.invoke and51// java/lang/ClassLoader52Class object_class = Object.class;53redefine(agentArgs, instrumentation, object_class);5455Class method_class = Method.class;56redefine(agentArgs, instrumentation, method_class);5758Class loader_class = ClassLoader.class;59redefine(agentArgs, instrumentation, loader_class);6061instrumentation.removeTransformer(transformer);62}6364public static void main(String[] args) {65byte[] ba = new byte[0];6667// If it survives 1000 GC's, it's good.68for (int i = 0; i < 1000 ; i++) {69System.gc();70ba.clone();71}72try {73// Use java/lang/reflect/Method.invoke to call74WalkThroughInvoke a = new WalkThroughInvoke();75Class aclass = WalkThroughInvoke.class;76Method m = aclass.getMethod("stackWalk");77m.invoke(a);78} catch (Exception x) {79x.printStackTrace();80}81}82}838485