Java Code Examples for jdk.test.lib.compiler.InMemoryJavaCompiler#compile()

The following examples show how to use jdk.test.lib.compiler.InMemoryJavaCompiler#compile() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NestedUnsafe.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String args[]) throws Exception {
    Unsafe unsafe = Unsafe.getUnsafe();

    // The anonymous class calls defineAnonymousClass creating a nested anonymous class.
    byte klassbuf2[] = InMemoryJavaCompiler.compile("p.TestClass2",
        "package p; " +
        "import jdk.internal.misc.Unsafe; " +
        "public class TestClass2 { " +
        "    public static void doit() throws Throwable { " +
        "        Unsafe unsafe = jdk.internal.misc.Unsafe.getUnsafe(); " +
        "        Class klass2 = unsafe.defineAnonymousClass(TestClass2.class, p.NestedUnsafe.klassbuf, new Object[0]); " +
        "        unsafe.ensureClassInitialized(klass2); " +
        "        Class[] dArgs = new Class[2]; " +
        "        dArgs[0] = String.class; " +
        "        dArgs[1] = String.class; " +
        "        try { " +
        "            klass2.getMethod(\"concat\", dArgs).invoke(null, \"CC\", \"DD\"); " +
        "        } catch (Throwable ex) { " +
        "            throw new RuntimeException(\"Exception: \" + ex.toString()); " +
        "        } " +
        "} } ",
        "--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED");

    Class klass2 = unsafe.defineAnonymousClass(p.NestedUnsafe.class, klassbuf2, new Object[0]);
    try {
        klass2.getMethod("doit").invoke(null);
        throw new RuntimeException("Expected exception not thrown");
    } catch (Throwable ex) {
        Throwable iae = ex.getCause();
        if (!iae.toString().contains(
            "IllegalArgumentException: Host class p/NestedUnsafe and anonymous class q/TestClass")) {
            throw new RuntimeException("Exception: " + iae.toString());
        }
    }
}
 
Example 2
Source File: NestedUnsafe2.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String args[]) throws Exception {
    Unsafe unsafe = Unsafe.getUnsafe();

    // The anonymous class calls defineAnonymousClass creating a nested anonymous class.
    byte klassbuf2[] = InMemoryJavaCompiler.compile("TestClass2",
        "import jdk.internal.misc.Unsafe; " +
        "public class TestClass2 { " +
        "    public static void doit() throws Throwable { " +
        "        Unsafe unsafe = jdk.internal.misc.Unsafe.getUnsafe(); " +
        "        Class klass2 = unsafe.defineAnonymousClass(TestClass2.class, p.NestedUnsafe2.klassbuf, new Object[0]); " +
        "        unsafe.ensureClassInitialized(klass2); " +
        "        Class[] dArgs = new Class[2]; " +
        "        dArgs[0] = String.class; " +
        "        dArgs[1] = String.class; " +
        "        try { " +
        "            klass2.getMethod(\"concat\", dArgs).invoke(null, \"CC\", \"DD\"); " +
        "        } catch (Throwable ex) { " +
        "            throw new RuntimeException(\"Exception: \" + ex.toString()); " +
        "        } " +
        "} } ",
        "--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED");

    Class klass2 = unsafe.defineAnonymousClass(p.NestedUnsafe2.class, klassbuf2, new Object[0]);
    try {
        klass2.getMethod("doit").invoke(null);
        throw new RuntimeException("Expected exception not thrown");
    } catch (Throwable ex) {
        Throwable iae = ex.getCause();
        if (!iae.toString().contains(
            "IllegalArgumentException: Host class p/NestedUnsafe2 and anonymous class q/TestClass")) {
            throw new RuntimeException("Exception: " + iae.toString());
        }
    }
}
 
Example 3
Source File: TestMetaspacePerfCounters.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private static Class<?> compileAndLoad(String name, String source) throws Exception {
    byte[] byteCode = InMemoryJavaCompiler.compile(name, source);
    return ByteCodeLoader.load(name, byteCode);
}
 
Example 4
Source File: GetSysPkgTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String args[]) throws Throwable {
    if (args.length == 0 || !args[0].equals("do_tests")) {

        // Create a package found via -Xbootclasspath/a
        String source = "package BootLdr_package; " +
                        "public class BootLdrPkg { " +
                        "    public int mth() { return 4; } " +
                        "}";
        byte[] klassbuf =
            InMemoryJavaCompiler.compile("BootLdr_package.BootLdrPkg", source);
        ClassFileInstaller.writeClassToDisk("BootLdr_package/BootLdrPkg", klassbuf, "bl_dir");

        // Create a package found via -cp.
        source = "package GetSysPkg_package; " +
                 "public class GetSysClass { " +
                 "    public int mth() { return 4; } " +
                 "}";
        klassbuf =
            InMemoryJavaCompiler.compile("GetSysPkg_package.GetSysClass", source);
        ClassFileInstaller.writeClassToDisk("GetSysPkg_package/GetSysClass", klassbuf);

        ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-Xbootclasspath/a:bl_dir",
            "--add-opens=java.base/jdk.internal.loader=ALL-UNNAMED", "-cp", "." + File.pathSeparator +
            System.getProperty("test.classes"), "GetSysPkgTest", "do_tests");
        OutputAnalyzer output = new OutputAnalyzer(pb.start());
        output.shouldHaveExitValue(0);
        return;
    }

    getPkg("java/lang", "jrt:/java.base");
    getPkg("javax/script", null);          // Package not defined

    // Test a package that does not yet have any referenced classes.
    // Note: if another class in com/sun/crypto/provider/ happens to get
    //       loaded or if class PrivateKeyInfo disappears from this package
    //       then this test will fail.
    getPkg("com/sun/crypto/provider", null);
    // Now make sure a class in the package is referenced.
    Class newClass = Class.forName("com.sun.crypto.provider.PrivateKeyInfo");
    getPkg("com/sun/crypto/provider", "jrt:/java.base");

    getPkg("java/nio/charset", "jrt:/java.base");

    // Test a package in a module not owned by boot loader.
    Class clss = Class.forName("jdk.nio.zipfs.ZipPath");
    if (clss == null)
        throw new RuntimeException("Could not find class jdk.nio.zipfs.ZipPath");
    getPkg("javax/activation", null);       // Not owned by boot loader

    // Test a package not in jimage file.
    clss = Class.forName("GetSysPkg_package.GetSysClass");
    if (clss == null)
        throw new RuntimeException("Could not find class GetSysPkg_package.GetSysClass");
    getPkg("GetSysPkg_package", null);

    // Access a class with a package in a boot loader module other than java.base
    clss = Class.forName("java.awt.Button");
    if (clss == null)
        throw new RuntimeException("Could not find class java.awt.Button");
    getPkg("java/awt", "jrt:/java.desktop");

    // Test getting the package location from a class found via -Xbootclasspath/a
    clss = Class.forName("BootLdr_package.BootLdrPkg");
    if (clss == null)
        throw new RuntimeException("Could not find class BootLdr_package.BootLdrPkg");
    String bootldrPkg = (String)invoke(findMethod("getSystemPackageLocation"), null, "BootLdr_package");
    if (bootldrPkg == null) {
        throw new RuntimeException("Expected BootLdr_package to return non-null value");
    }
    if (!bootldrPkg.equals("bl_dir")) {
        throw new RuntimeException("Expected BootLdr_package to return bl_dir, got: " + bootldrPkg);
    }

    // Test when package's class reference is an array.
    // Note: if another class in javax/crypto happens to get loaded
    //       or if class AEADBadTagException disappears from this package
    //       then this test will fail.
    getPkg("javax/crypto", null);
    javax.crypto.AEADBadTagException[] blah = new javax.crypto.AEADBadTagException[3];
    getPkg("javax/crypto", "jrt:/java.base");

}
 
Example 5
Source File: RedefineClassHelper.java    From TencentKona-8 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Redefine a class
 *
 * @param clazz Class to redefine
 * @param javacode String with the new java code for the class to be redefined
 */
public static void redefineClass(Class clazz, String javacode) throws Exception {
    byte[] bytecode = InMemoryJavaCompiler.compile(clazz.getName(), javacode);
    redefineClass(clazz, bytecode);
}
 
Example 6
Source File: RedefineClassHelper.java    From openjdk-jdk8u with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Redefine a class
 *
 * @param clazz Class to redefine
 * @param javacode String with the new java code for the class to be redefined
 */
public static void redefineClass(Class clazz, String javacode) throws Exception {
    byte[] bytecode = InMemoryJavaCompiler.compile(clazz.getName(), javacode);
    redefineClass(clazz, bytecode);
}
 
Example 7
Source File: RedefineClassHelper.java    From openjdk-jdk9 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Redefine a class
 *
 * @param clazz Class to redefine
 * @param javacode String with the new java code for the class to be redefined
 */
public static void redefineClass(Class clazz, String javacode) throws Exception {
    byte[] bytecode = InMemoryJavaCompiler.compile(clazz.getName(), javacode);
    redefineClass(clazz, bytecode);
}