jdk/test/java/lang/instrument/RetransformBigClassAgent.java
changeset 11359 9e53808b2128
equal deleted inserted replaced
11358:8821bb789678 11359:9e53808b2128
       
     1 /*
       
     2  * Copyright (c) 2011, 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.*;
       
    25 import java.util.*;
       
    26 
       
    27 public class RetransformBigClassAgent {
       
    28     private static int N_RETRANSFORMS = 1000;
       
    29     public static Class clz;
       
    30     public static volatile boolean doneRetransforming = false;
       
    31 
       
    32     // just retransform the class in a loop via a Timer
       
    33     public static void premain(String agentArgs, final Instrumentation inst) throws Exception {
       
    34         String s = agentArgs.substring(0, agentArgs.indexOf(".class"));
       
    35         clz = Class.forName(s.replace('/', '.'));
       
    36 
       
    37         ClassFileTransformer trans = new SimpleIdentityTransformer();
       
    38         inst.addTransformer(trans, true /* canRetransform */);
       
    39 
       
    40         System.gc();  // throw away anything we can before we start testing
       
    41 
       
    42         new Timer(true).schedule(new TimerTask() {
       
    43             public void run() {
       
    44                 try {
       
    45                     int i;
       
    46                     System.out.println("Retransforming");
       
    47                     for (i = 0; i < N_RETRANSFORMS; i++) {
       
    48                         inst.retransformClasses(clz);
       
    49                         System.gc();  // throw away anything we can
       
    50                     }
       
    51                     System.out.println("Retransformed " + i + " times.");
       
    52                     RetransformBigClassAgent.doneRetransforming = true;
       
    53                 }
       
    54                 catch (Exception e) { e.printStackTrace(); }
       
    55             }
       
    56         }, 500);
       
    57     }
       
    58 }