jdk/test/java/lang/instrument/RedefineBigClassAgent.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.net.*;
       
    26 import java.util.*;
       
    27 import java.io.*;
       
    28 
       
    29 public class RedefineBigClassAgent {
       
    30     // N_REDEFINES = 2000 made my Solaris X86 box crawl on its knees
       
    31     // with the Server VM. The timeout is 600 seconds, but the test
       
    32     // ran for 675 seconds so it took the harness 75 seconds to kill
       
    33     // the test.
       
    34     private static int N_REDEFINES = 1000;
       
    35     public static Class clz;
       
    36     public static volatile boolean doneRedefining = false;
       
    37 
       
    38     // just read the original class and redefine in a loop via a Timer
       
    39     public static void premain(String agentArgs, final Instrumentation inst) throws Exception {
       
    40         String s = agentArgs.substring(0, agentArgs.indexOf(".class"));
       
    41         clz = Class.forName(s.replace('/', '.'));
       
    42         ClassLoader loader =
       
    43             RedefineBigClassAgent.class.getClassLoader();
       
    44         URL classURL = loader.getResource(agentArgs);
       
    45         if (classURL == null) {
       
    46             throw new Exception("Cannot find class: " + agentArgs);
       
    47         }
       
    48 
       
    49         int         redefineLength;
       
    50         InputStream redefineStream;
       
    51 
       
    52         System.out.println("Reading test class from " + classURL);
       
    53         if (classURL.getProtocol().equals("file")) {
       
    54             File f = new File(classURL.getFile());
       
    55             redefineStream = new FileInputStream(f);
       
    56             redefineLength = (int) f.length();
       
    57         } else {
       
    58             URLConnection conn = classURL.openConnection();
       
    59             redefineStream = conn.getInputStream();
       
    60             redefineLength = conn.getContentLength();
       
    61         }
       
    62 
       
    63         final byte[] buffer = new byte[redefineLength];
       
    64         new BufferedInputStream(redefineStream).read(buffer);
       
    65 
       
    66         System.gc();  // throw away anything we can before we start testing
       
    67 
       
    68         new Timer(true).schedule(new TimerTask() {
       
    69             public void run() {
       
    70                 try {
       
    71                     int i;
       
    72                     System.out.println("Redefining");
       
    73                     ClassDefinition cld = new ClassDefinition(clz, buffer);
       
    74                     for (i = 0; i < N_REDEFINES; i++) {
       
    75                         inst.redefineClasses(new ClassDefinition[] { cld });
       
    76                         System.gc();  // throw away anything we can
       
    77                     }
       
    78                     System.out.println("Redefined " + i + " times.");
       
    79                     RedefineBigClassAgent.doneRedefining = true;
       
    80                 }
       
    81                 catch (Exception e) { e.printStackTrace(); }
       
    82             }
       
    83         }, 500);
       
    84     }
       
    85 }