test/hotspot/jtreg/runtime/RedefineObject/Agent.java
branchniosocketimpl-branch
changeset 57277 d2b2a4edbfe7
parent 57275 222fa5ed1c91
parent 54216 f10ca228b22f
child 57278 bf925a3ee68a
equal deleted inserted replaced
57275:222fa5ed1c91 57277:d2b2a4edbfe7
     1 /*
       
     2  * Copyright (c) 2013, 2014, 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.*;
       
    26 
       
    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     }
       
    36 
       
    37     public static void redefine(String agentArgs, Instrumentation instrumentation, Class to_redefine) {
       
    38 
       
    39         try {
       
    40             instrumentation.retransformClasses(to_redefine);
       
    41         } catch (Exception e) {
       
    42             e.printStackTrace();
       
    43         }
       
    44 
       
    45     }
       
    46 
       
    47     public static void premain(String agentArgs, Instrumentation instrumentation) {
       
    48         Agent transformer = new Agent();
       
    49         instrumentation.addTransformer(transformer, true);
       
    50 
       
    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);
       
    55 
       
    56         Class method_class = Method.class;
       
    57         redefine(agentArgs, instrumentation, method_class);
       
    58 
       
    59         Class loader_class = ClassLoader.class;
       
    60         redefine(agentArgs, instrumentation, loader_class);
       
    61 
       
    62         instrumentation.removeTransformer(transformer);
       
    63     }
       
    64 
       
    65     public static void main(String[] args) {
       
    66         byte[] ba = new byte[0];
       
    67 
       
    68         // If it survives 100 GC's, it's good.
       
    69         for (int i = 0; i < 100 ; 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 }