test/runtime/RedefineObject/Agent.java

Thu, 19 Sep 2013 09:36:51 -0700

author
cl
date
Thu, 19 Sep 2013 09:36:51 -0700
changeset 5664
34aa07e92d22
parent 5508
85147f28faba
child 6198
55fb97c4c58d
permissions
-rw-r--r--

Added tag jdk8-b108 for changeset 85072013aad4

     1 /*
     2  * Copyright (c) 2013, 2013, 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 import java.security.*;
    24 import java.lang.instrument.*;
    25 import java.lang.reflect.*;
    27 public class Agent implements ClassFileTransformer {
    28     public synchronized byte[] transform(final ClassLoader classLoader,
    29                                          final String className,
    30                                          Class<?> classBeingRedefined,
    31                                          ProtectionDomain protectionDomain,
    32                                          byte[] classfileBuffer) {
    33         System.out.println("Transforming class " + className);
    34         return classfileBuffer;
    35     }
    37     public static void redefine(String agentArgs, Instrumentation instrumentation, Class to_redefine) {
    39         try {
    40             instrumentation.retransformClasses(to_redefine);
    41         } catch (Exception e) {
    42             e.printStackTrace();
    43         }
    45     }
    47     public static void premain(String agentArgs, Instrumentation instrumentation) {
    48         Agent transformer = new Agent();
    49         instrumentation.addTransformer(transformer, true);
    51         // Redefine java/lang/Object and java/lang/reflect/Method.invoke and
    52         // java/lang/ClassLoader
    53         Class object_class = Object.class;
    54         redefine(agentArgs, instrumentation, object_class);
    56         Class method_class = Method.class;
    57         redefine(agentArgs, instrumentation, method_class);
    59         Class loader_class = ClassLoader.class;
    60         redefine(agentArgs, instrumentation, loader_class);
    62         instrumentation.removeTransformer(transformer);
    63     }
    65     public static void main(String[] args) {
    66         byte[] ba = new byte[0];
    68         // If it survives 1000 GC's, it's good.
    69         for (int i = 0; i < 1000 ; i++) {
    70             System.gc();
    71             ba.clone();
    72         }
    73         try {
    74             // Use java/lang/reflect/Method.invoke to call
    75             WalkThroughInvoke a = new WalkThroughInvoke();
    76             Class aclass = WalkThroughInvoke.class;
    77             Method m = aclass.getMethod("stackWalk");
    78             m.invoke(a);
    79         } catch (Exception x) {
    80             x.printStackTrace();
    81         }
    82     }
    83 }

mercurial