RedefineClassWithNativeMethodAgent.java revision 13901:b2a69d66dc65
1285SN/A/*
2462SN/A * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
3285SN/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4285SN/A *
5285SN/A * This code is free software; you can redistribute it and/or modify it
6285SN/A * under the terms of the GNU General Public License version 2 only, as
7285SN/A * published by the Free Software Foundation.
8285SN/A *
9285SN/A * This code is distributed in the hope that it will be useful, but WITHOUT
10285SN/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11285SN/A * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12285SN/A * version 2 for more details (a copy is included in the LICENSE file that
13285SN/A * accompanied this code).
14285SN/A *
15285SN/A * You should have received a copy of the GNU General Public License version
16285SN/A * 2 along with this work; if not, write to the Free Software Foundation,
17285SN/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18285SN/A *
19285SN/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20285SN/A * or visit www.oracle.com if you need additional information or have any
21285SN/A * questions.
22285SN/A */
23285SN/A
24285SN/Aimport java.io.InputStream;
25285SN/Aimport java.lang.instrument.ClassDefinition;
26285SN/Aimport java.lang.instrument.Instrumentation;
27285SN/Aimport java.lang.reflect.Module;
28285SN/Aimport java.util.Timer;
29285SN/Aimport java.util.TimerTask;
30285SN/A
31285SN/Apublic class RedefineClassWithNativeMethodAgent {
32285SN/A    static Class clz;
33285SN/A
34285SN/A    // just read the original class and redefine it via a Timer
35285SN/A    public static void premain(String agentArgs, final Instrumentation inst) throws Exception {
36285SN/A        String s = agentArgs.substring(0, agentArgs.indexOf(".class"));
37285SN/A        clz = Class.forName(s.replace('/', '.'));
38285SN/A        InputStream in;
39285SN/A        Module m = clz.getModule();
40285SN/A        if (m != null) {
41285SN/A            in = m.getResourceAsStream(agentArgs);
42285SN/A        } else {
43285SN/A            ClassLoader loader =
44285SN/A                RedefineClassWithNativeMethodAgent.class.getClassLoader();
45285SN/A            in = loader.getResourceAsStream(agentArgs);
46285SN/A        }
47285SN/A        if (in == null) {
48367SN/A            throw new Exception("Cannot find class: " + agentArgs);
49285SN/A        }
50285SN/A        byte[] buffer = in.readAllBytes();
51285SN/A
52285SN/A        new Timer(true).schedule(new TimerTask() {
53285SN/A            public void run() {
54285SN/A                try {
55285SN/A                    System.out.println("Instrumenting");
56285SN/A                    ClassDefinition cld = new ClassDefinition(clz, buffer);
57285SN/A                    inst.redefineClasses(new ClassDefinition[] { cld });
58285SN/A                }
59285SN/A                catch (Exception e) { e.printStackTrace(); }
60285SN/A            }
61285SN/A        }, 500);
62285SN/A    }
63285SN/A}
64285SN/A