test/runtime/RedefineObject/Agent.java

Wed, 22 Jan 2014 12:37:28 -0800

author
katleman
date
Wed, 22 Jan 2014 12:37:28 -0800
changeset 6575
2bac854670c0
parent 6198
55fb97c4c58d
child 6876
710a3c8b516e
permissions
-rw-r--r--

Added tag jdk8u5-b05 for changeset b90de55aca30

coleenp@5100 1 /*
mikael@6198 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
coleenp@5100 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
coleenp@5100 4 *
coleenp@5100 5 * This code is free software; you can redistribute it and/or modify it
coleenp@5100 6 * under the terms of the GNU General Public License version 2 only, as
coleenp@5100 7 * published by the Free Software Foundation.
coleenp@5100 8 *
coleenp@5100 9 * This code is distributed in the hope that it will be useful, but WITHOUT
coleenp@5100 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
coleenp@5100 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
coleenp@5100 12 * version 2 for more details (a copy is included in the LICENSE file that
coleenp@5100 13 * accompanied this code).
coleenp@5100 14 *
coleenp@5100 15 * You should have received a copy of the GNU General Public License version
coleenp@5100 16 * 2 along with this work; if not, write to the Free Software Foundation,
coleenp@5100 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
coleenp@5100 18 *
coleenp@5100 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
coleenp@5100 20 * or visit www.oracle.com if you need additional information or have any
coleenp@5100 21 * questions.
coleenp@5100 22 */
coleenp@5100 23 import java.security.*;
coleenp@5100 24 import java.lang.instrument.*;
coleenp@5508 25 import java.lang.reflect.*;
coleenp@5100 26
coleenp@5100 27 public class Agent implements ClassFileTransformer {
coleenp@5100 28 public synchronized byte[] transform(final ClassLoader classLoader,
coleenp@5100 29 final String className,
coleenp@5100 30 Class<?> classBeingRedefined,
coleenp@5100 31 ProtectionDomain protectionDomain,
coleenp@5100 32 byte[] classfileBuffer) {
coleenp@5508 33 System.out.println("Transforming class " + className);
coleenp@5100 34 return classfileBuffer;
coleenp@5100 35 }
coleenp@5100 36
coleenp@5508 37 public static void redefine(String agentArgs, Instrumentation instrumentation, Class to_redefine) {
coleenp@5100 38
coleenp@5100 39 try {
coleenp@5508 40 instrumentation.retransformClasses(to_redefine);
coleenp@5100 41 } catch (Exception e) {
coleenp@5100 42 e.printStackTrace();
coleenp@5100 43 }
coleenp@5100 44
coleenp@5508 45 }
coleenp@5508 46
coleenp@5508 47 public static void premain(String agentArgs, Instrumentation instrumentation) {
coleenp@5508 48 Agent transformer = new Agent();
coleenp@5508 49 instrumentation.addTransformer(transformer, true);
coleenp@5508 50
coleenp@5508 51 // Redefine java/lang/Object and java/lang/reflect/Method.invoke and
coleenp@5508 52 // java/lang/ClassLoader
coleenp@5508 53 Class object_class = Object.class;
coleenp@5508 54 redefine(agentArgs, instrumentation, object_class);
coleenp@5508 55
coleenp@5508 56 Class method_class = Method.class;
coleenp@5508 57 redefine(agentArgs, instrumentation, method_class);
coleenp@5508 58
coleenp@5508 59 Class loader_class = ClassLoader.class;
coleenp@5508 60 redefine(agentArgs, instrumentation, loader_class);
coleenp@5508 61
coleenp@5100 62 instrumentation.removeTransformer(transformer);
coleenp@5100 63 }
coleenp@5100 64
coleenp@5100 65 public static void main(String[] args) {
coleenp@5100 66 byte[] ba = new byte[0];
coleenp@5100 67
coleenp@5100 68 // If it survives 1000 GC's, it's good.
coleenp@5100 69 for (int i = 0; i < 1000 ; i++) {
coleenp@5100 70 System.gc();
coleenp@5100 71 ba.clone();
coleenp@5100 72 }
coleenp@5508 73 try {
coleenp@5508 74 // Use java/lang/reflect/Method.invoke to call
coleenp@5508 75 WalkThroughInvoke a = new WalkThroughInvoke();
coleenp@5508 76 Class aclass = WalkThroughInvoke.class;
coleenp@5508 77 Method m = aclass.getMethod("stackWalk");
coleenp@5508 78 m.invoke(a);
coleenp@5508 79 } catch (Exception x) {
coleenp@5508 80 x.printStackTrace();
coleenp@5508 81 }
coleenp@5100 82 }
coleenp@5100 83 }

mercurial