hotspot/test/testlibrary/RedefineClassHelper.java
author ctornqvi
Mon, 02 Jun 2014 19:08:18 +0200
changeset 24837 b9c998a3e9fd
child 30604 b8d532cb6420
permissions -rw-r--r--
8044364: runtime/RedefineFinalizer test fails on windows Summary: Rewrote the test in pure Java, added RedefineClassHelper utility class Reviewed-by: coleenp, allwin, gtriantafill, dsamersoff
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24837
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
     1
/*
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
     4
 *
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
     7
 * published by the Free Software Foundation.
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
     8
 *
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    13
 * accompanied this code).
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    14
 *
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    18
 *
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    21
 * questions.
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    22
 */
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    23
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    24
import java.io.PrintWriter;
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    25
import java.lang.instrument.*;
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    26
import com.oracle.java.testlibrary.*;
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    27
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    28
/*
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    29
 * Helper class to write tests that redefine classes.
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    30
 * When main method is run, it will create a redefineagent.jar that can be used
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    31
 * with the -javaagent option to support redefining classes in jtreg tests.
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    32
 *
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    33
 * See sample test in test/testlibrary_tests/RedefineClassTest.java
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    34
 */
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    35
public class RedefineClassHelper {
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    36
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    37
    public static Instrumentation instrumentation;
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    38
    public static void premain(String agentArgs, Instrumentation inst) {
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    39
        instrumentation = inst;
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    40
    }
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    41
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    42
    /**
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    43
     * Redefine a class
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    44
     *
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    45
     * @param clazz Class to redefine
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    46
     * @param javacode String with the new java code for the class to be redefined
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    47
     */
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    48
    public static void redefineClass(Class clazz, String javacode) throws Exception {
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    49
        byte[] bytecode = InMemoryJavaCompiler.compile(clazz.getName(), javacode);
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    50
        redefineClass(clazz, bytecode);
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    51
    }
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    52
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    53
    /**
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    54
     * Redefine a class
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    55
     *
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    56
     * @param clazz Class to redefine
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    57
     * @param bytecode byte[] with the new class
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    58
     */
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    59
    public static void redefineClass(Class clazz, byte[] bytecode) throws Exception {
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    60
        instrumentation.redefineClasses(new ClassDefinition(clazz, bytecode));
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    61
    }
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    62
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    63
    /**
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    64
     * Main method to be invoked before test to create the redefineagent.jar
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    65
     */
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    66
    public static void main(String[] args) throws Exception {
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    67
        ClassFileInstaller.main("RedefineClassHelper");
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    68
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    69
        PrintWriter pw = new PrintWriter("MANIFEST.MF");
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    70
        pw.println("Premain-Class: RedefineClassHelper");
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    71
        pw.println("Can-Redefine-Classes: true");
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    72
        pw.close();
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    73
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    74
        sun.tools.jar.Main jarTool = new sun.tools.jar.Main(System.out, System.err, "jar");
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    75
        if (!jarTool.run(new String[] { "-cmf", "MANIFEST.MF", "redefineagent.jar", "RedefineClassHelper.class" })) {
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    76
            throw new Exception("jar operation failed");
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    77
        }
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    78
    }
b9c998a3e9fd 8044364: runtime/RedefineFinalizer test fails on windows
ctornqvi
parents:
diff changeset
    79
}