jdk.test.lib.compiler.InMemoryJavaCompiler Java Examples

The following examples show how to use jdk.test.lib.compiler.InMemoryJavaCompiler. 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: BootstrapRedefine.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    String source = "package java.lang;" +
                    "public class Object {" +
                    "    void dummy1() { return; }" +
                    "    void dummy2() { return; }" +
                    "    void dummy3() { return; }" +
                    "}";

    ClassFileInstaller.writeClassToDisk("java/lang/Object",
                                    InMemoryJavaCompiler.compile("java.lang.Object", source,
                                    "-Xmodule:java.base"),
                                    "mods/java.base");

    ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("--patch-module=java.base=mods/java.base", "-version");
    new OutputAnalyzer(pb.start())
        .shouldContain("Incompatible definition of java.lang.Object")
        .shouldHaveExitValue(1);
}
 
Example #2
Source File: PatchModuleTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    String source = "package javax.naming.spi; "                +
                    "public class NamingManager { "             +
                    "    static { "                             +
                    "        System.out.println(\"I pass!\"); " +
                    "    } "                                    +
                    "}";

    ClassFileInstaller.writeClassToDisk("javax/naming/spi/NamingManager",
         InMemoryJavaCompiler.compile("javax.naming.spi.NamingManager", source, "-Xmodule:java.naming"),
         "mods/java.naming");

    ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("--patch-module=java.naming=mods/java.naming",
         "PatchModuleMain", "javax.naming.spi.NamingManager");

    new OutputAnalyzer(pb.start())
        .shouldContain("I pass!")
        .shouldHaveExitValue(0);
}
 
Example #3
Source File: PatchModuleJavaBase.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    String source = "package java.lang; "                       +
                    "public class NewClass { "                  +
                    "    static { "                             +
                    "        System.out.println(\"I pass!\"); " +
                    "    } "                                    +
                    "}";

    ClassFileInstaller.writeClassToDisk("java/lang/NewClass",
         InMemoryJavaCompiler.compile("java.lang.NewClass", source, "-Xmodule:java.base"),
         "mods/java.base");

    ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("--patch-module=java.base=mods/java.base",
         "PatchModuleMain", "java.lang.NewClass");

    new OutputAnalyzer(pb.start())
        .shouldContain("I pass!")
        .shouldHaveExitValue(0);
}
 
Example #4
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 #5
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 #6
Source File: XbootcpNoVisibility.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 {

        String Vis3_B_src =
                "package sun.util;" +
                "public class Vis3_B { public void m() { System.out.println(\"In B's m()\"); } }";

        ClassFileInstaller.writeClassToDisk("sun/util/Vis3_B",
            InMemoryJavaCompiler.compile("sun.util.Vis3_B", Vis3_B_src), System.getProperty("test.classes"));

        String Vis3_A_src =
                "import sun.util.*;" +
                "public class Vis3_A {" +
                "    public static void main(String args[]) throws Exception {" +
                        // Try loading a class within a named package in a module which has been defined
                        // to the boot loader. In this situation, the class should only be attempted
                        // to be loaded from the boot loader's module path which consists of:
                        //   [--patch-module]; exploded build | "modules" jimage
                        //
                        // Since the class is located on the boot loader's append path via
                        // -Xbootclasspath/a specification, it should not be found.
                "       try {" +
                "               sun.util.Vis3_B b = new sun.util.Vis3_B();" +
                "       } catch (NoClassDefFoundError e) {" +
                "               System.out.println(\"XbootcpNoVisibility PASSED - " +
                                                "test should throw exception\\n\");" +
                "               return;" +
                "       }" +
                "       throw new RuntimeException(\"XbootcpNoVisibility FAILED - " +
                                                    "test should have thrown exception\");" +
                "    }" +
                "}";

        ClassFileInstaller.writeClassToDisk("Vis3_A",
                InMemoryJavaCompiler.compile("Vis3_A", Vis3_A_src), System.getProperty("test.classes"));

        new OutputAnalyzer(ProcessTools.createJavaProcessBuilder(
                "-Xbootclasspath/a:.",
                "Vis3_A")
            .start()).shouldContain("XbootcpNoVisibility PASSED");
    }
 
Example #7
Source File: PatchModule2Dirs.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 {
    String source1 = "package javax.naming.spi; "               +
                    "public class NamingManager { "             +
                    "    static { "                             +
                    "        System.out.println(\"I pass one!\"); " +
                    "    } "                                    +
                    "}";
    String source2 = "package java.beans; "                     +
                    "public class Encoder { "                   +
                    "    static { "                             +
                    "        System.out.println(\"I pass two!\"); " +
                    "    } "                                    +
                    "}";

    ClassFileInstaller.writeClassToDisk("javax/naming/spi/NamingManager",
         InMemoryJavaCompiler.compile("javax.naming.spi.NamingManager", source1, "-Xmodule:java.naming"),
         "mods/java.naming");

    ClassFileInstaller.writeClassToDisk("java/beans/Encoder",
         InMemoryJavaCompiler.compile("java.beans.Encoder", source2, "-Xmodule:java.desktop"),
         "mods2/java.desktop");

    ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
         "--patch-module=java.naming=mods/java.naming",
         "--patch-module=java.desktop=mods2/java.desktop",
         "PatchModule2DirsMain", "javax.naming.spi.NamingManager", "java.beans.Encoder");

    OutputAnalyzer oa = new OutputAnalyzer(pb.start());
    oa.shouldContain("I pass one!");
    oa.shouldContain("I pass two!");
    oa.shouldHaveExitValue(0);
}
 
Example #8
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 #9
Source File: PatchModuleClassList.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 {
    // Case 1. A class to be loaded by the boot class loader

    // Create a class file in the module java.naming. This class file
    // will be put in the javanaming.jar file.
    String source = "package javax.naming.spi; "                +
                    "public class NamingManager { "             +
                    "    static { "                             +
                    "        System.out.println(\"I pass!\"); " +
                    "    } "                                    +
                    "}";

    ClassFileInstaller.writeClassToDisk(BOOT_CLASS,
         InMemoryJavaCompiler.compile(BOOT_CLASS.replace('/', '.'), source, "-Xmodule:java.naming"),
         System.getProperty("test.classes"));

    // Build the jar file that will be used for the module "java.naming".
    BasicJarBuilder.build("javanaming", BOOT_CLASS);
    String moduleJar = BasicJarBuilder.getTestJar("javanaming.jar");

    String classList = "javanaming.list";
    ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
        true,
        "-XX:DumpLoadedClassList=" + classList,
        "--patch-module=java.naming=" + moduleJar,
        "PatchModuleMain", BOOT_CLASS.replace('/', '.'));
    new OutputAnalyzer(pb.start()).shouldHaveExitValue(0);

    // check the generated classlist file
    String content = new String(Files.readAllBytes(Paths.get(classList)));
    if (content.indexOf(BOOT_CLASS) >= 0) {
        throw new RuntimeException(BOOT_CLASS + " should not be in the classlist");
    }

    // Case 2. A class to be loaded by the platform class loader

    // Create a class file in the module java.transaction. This class file
    // will be put in the javatransaction.jar file.
    source = "package javax.transaction; "                 +
             "public class InvalidTransactionException { " +
             "    static { "                               +
             "        System.out.println(\"I pass!\"); "   +
             "    } "                                      +
             "}";

    ClassFileInstaller.writeClassToDisk(PLATFORM_CLASS,
         InMemoryJavaCompiler.compile(PLATFORM_CLASS.replace('/', '.'), source, "-Xmodule:java.transaction"),
         System.getProperty("test.classes"));

    // Build the jar file that will be used for the module "java.transaction".
    BasicJarBuilder.build("javatransaction", PLATFORM_CLASS);
    moduleJar = BasicJarBuilder.getTestJar("javatransaction.jar");

    classList = "javatransaction.list";
    pb = ProcessTools.createJavaProcessBuilder(
        true,
        "-XX:DumpLoadedClassList=" + classList,
        "--patch-module=java.naming=" + moduleJar,
        "PatchModuleMain", PLATFORM_CLASS.replace('/', '.'));
    new OutputAnalyzer(pb.start()).shouldHaveExitValue(0);

    // check the generated classlist file
    content = new String(Files.readAllBytes(Paths.get(classList)));
    if (content.indexOf(PLATFORM_CLASS) >= 0) {
        throw new RuntimeException(PLATFORM_CLASS + " should not be in the classlist");
    }

    // Case 3. A class to be loaded from the bootclasspath/a

    // Create a simple class file
    source = "public class Hello { "                         +
             "    public static void main(String args[]) { " +
             "        System.out.println(\"Hello\"); "       +
             "    } "                                        +
             "}";

    ClassFileInstaller.writeClassToDisk("Hello",
         InMemoryJavaCompiler.compile("Hello", source),
         System.getProperty("test.classes"));

    // Build hello.jar
    BasicJarBuilder.build("hello", "Hello");
    moduleJar = BasicJarBuilder.getTestJar("hello.jar");

    classList = "hello.list";
    pb = ProcessTools.createJavaProcessBuilder(
        true,
        "-XX:DumpLoadedClassList=" + classList,
        "-Xbootclasspath/a:" + moduleJar,
        "Hello");
    new OutputAnalyzer(pb.start()).shouldHaveExitValue(0);

    // check the generated classlist file
    content = new String(Files.readAllBytes(Paths.get(classList)));
    if (content.indexOf("Hello") < 0) {
        throw new RuntimeException("Hello should be in the classlist");
    }
}
 
Example #10
Source File: PatchModuleTestJarDir.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        // Create a class file in the module java.naming. This class file
        // will be put in the javanaming.jar file.
        String source = "package javax.naming.spi; "                    +
                        "public class NamingManager1 { "                +
                        "    static { "                                 +
                        "        System.out.println(\"I pass one!\"); " +
                        "    } "                                        +
                        "}";

        ClassFileInstaller.writeClassToDisk("javax/naming/spi/NamingManager1",
             InMemoryJavaCompiler.compile("javax.naming.spi.NamingManager1", source, "-Xmodule:java.naming"),
             System.getProperty("test.classes"));

        // Build the jar file that will be used for the module "java.naming".
        BasicJarBuilder.build("javanaming", "javax/naming/spi/NamingManager1");
        moduleJar = BasicJarBuilder.getTestJar("javanaming.jar");

        // Just to make sure we are not fooled by the class file being on the
        // class path where all the test classes are stored, write the NamingManager.class
        // file out again with output that does not contain what OutputAnalyzer
        // expects. This will provide confidence that the contents of the class
        // is truly coming from the jar file and not the class file.
        source = "package javax.naming.spi; "                +
                 "public class NamingManager1 { "            +
                 "    static { "                             +
                 "        System.out.println(\"Fail!\"); "   +
                 "    } "                                    +
                 "}";

        ClassFileInstaller.writeClassToDisk("javax/naming/spi/NamingManager1",
             InMemoryJavaCompiler.compile("javax.naming.spi.NamingManager1", source, "-Xmodule:java.naming"),
             System.getProperty("test.classes"));

        // Create a second class file in the module java.naming. This class file
        // will be put in the mods/java.naming directory.
        source = "package javax.naming.spi; "                    +
                 "public class NamingManager2 { "                +
                 "    static { "                                 +
                 "        System.out.println(\"I pass two!\"); " +
                 "    } "                                        +
                 "}";

        ClassFileInstaller.writeClassToDisk("javax/naming/spi/NamingManager2",
             InMemoryJavaCompiler.compile("javax.naming.spi.NamingManager2", source, "-Xmodule:java.naming"),
             (System.getProperty("test.classes") + "/mods/java.naming"));


        // Supply --patch-module with the name of the jar file for the module java.naming.
        ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("--patch-module=java.naming=" +
                                                                           moduleJar +
                                                                           File.pathSeparator +
                                                                           System.getProperty("test.classes") + "/mods/java.naming",
                                                                  "PatchModule2DirsMain",
                                                                  "javax.naming.spi.NamingManager1",
                                                                  "javax.naming.spi.NamingManager2");

        new OutputAnalyzer(pb.start())
            .shouldContain("I pass one!")
            .shouldContain("I pass two!")
            .shouldHaveExitValue(0);
    }
 
Example #11
Source File: PatchModuleCDS.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 {

        // Case 1: Test that --patch-module and -Xshare:dump are compatible
        String filename = "patch_module.jsa";
        ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
            "-XX:+UnlockDiagnosticVMOptions",
            "-XX:SharedArchiveFile=" + filename,
            "-Xshare:dump",
            "--patch-module=java.naming=no/such/directory",
            "-Xlog:class+path=info",
            "-version");
        new OutputAnalyzer(pb.start())
            .shouldContain("ro space:"); // Make sure archive got created.

        // Case 2: Test that directory in --patch-module is supported for CDS dumping
        // Create a class file in the module java.base.
        String source = "package javax.naming.spi; "                +
                        "public class NamingManager { "             +
                        "    static { "                             +
                        "        System.out.println(\"I pass!\"); " +
                        "    } "                                    +
                        "}";

        ClassFileInstaller.writeClassToDisk("javax/naming/spi/NamingManager",
             InMemoryJavaCompiler.compile("javax.naming.spi.NamingManager", source, "-Xmodule:java.naming"),
             System.getProperty("test.classes"));

        pb = ProcessTools.createJavaProcessBuilder(
            "-XX:+UnlockDiagnosticVMOptions",
            "-XX:SharedArchiveFile=" + filename,
            "-Xshare:dump",
            "--patch-module=java.base=" + System.getProperty("test.classes"),
            "-Xlog:class+path=info",
            "-version");
        new OutputAnalyzer(pb.start())
            .shouldContain("ro space:"); // Make sure archive got created.

        // Case 3a: Test CDS dumping with jar file in --patch-module
        BasicJarBuilder.build("javanaming", "javax/naming/spi/NamingManager");
        String moduleJar = BasicJarBuilder.getTestJar("javanaming.jar");
        pb = ProcessTools.createJavaProcessBuilder(
            "-XX:+UnlockDiagnosticVMOptions",
            "-XX:SharedArchiveFile=" + filename,
            "-Xshare:dump",
            "--patch-module=java.naming=" + moduleJar,
            "-Xlog:class+load",
            "-Xlog:class+path=info",
            "PatchModuleMain", "javax.naming.spi.NamingManager");
        new OutputAnalyzer(pb.start())
            .shouldContain("ro space:"); // Make sure archive got created.

        // Case 3b: Test CDS run with jar file in --patch-module
        pb = ProcessTools.createJavaProcessBuilder(
            "-XX:+UnlockDiagnosticVMOptions",
            "-XX:SharedArchiveFile=" + filename,
            "-Xshare:auto",
            "--patch-module=java.naming=" + moduleJar,
            "-Xlog:class+load",
            "-Xlog:class+path=info",
            "PatchModuleMain", "javax.naming.spi.NamingManager");
        new OutputAnalyzer(pb.start())
            .shouldContain("I pass!")
            .shouldHaveExitValue(0);
    }
 
Example #12
Source File: PatchModuleTestJar.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        // Create a class file in the module java.naming. This class file
        // will be put in the javanaming.jar file.
        String source = "package javax.naming.spi; "                +
                        "public class NamingManager { "             +
                        "    static { "                             +
                        "        System.out.println(\"I pass!\"); " +
                        "    } "                                    +
                        "}";

        ClassFileInstaller.writeClassToDisk("javax/naming/spi/NamingManager",
             InMemoryJavaCompiler.compile("javax.naming.spi.NamingManager", source, "-Xmodule:java.naming"),
             System.getProperty("test.classes"));

        // Build the jar file that will be used for the module "java.naming".
        BasicJarBuilder.build("javanaming", "javax/naming/spi/NamingManager");
        moduleJar = BasicJarBuilder.getTestJar("javanaming.jar");

        // Just to make sure we are not fooled by the class file being on the
        // class path where all the test classes are stored, write the NamingManager.class
        // file out again with output that does not contain what OutputAnalyzer
        // expects. This will provide confidence that the contents of the class
        // is truly coming from the jar file and not the class file.
        source =        "package javax.naming.spi; "                +
                        "public class NamingManager { "             +
                        "    static { "                             +
                        "        System.out.println(\"Fail!\"); "   +
                        "    } "                                    +
                        "}";

        ClassFileInstaller.writeClassToDisk("javax/naming/spi/NamingManager",
             InMemoryJavaCompiler.compile("javax.naming.spi.NamingManager", source, "-Xmodule:java.naming"),
             System.getProperty("test.classes"));

        // Supply --patch-module with the name of the jar file for the module java.naming.
        ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("--patch-module=java.naming=" + moduleJar,
             "PatchModuleMain", "javax.naming.spi.NamingManager");

        new OutputAnalyzer(pb.start())
            .shouldContain("I pass!")
            .shouldHaveExitValue(0);
    }
 
Example #13
Source File: PatchModuleVisibility.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 {

      String Vis2_B_src =
              "package p2;" +
              "public class Vis2_B {" +
              "    public void m() {" +
              "        System.out.println(\"In B's m()\");" +
              "    }" +
              "}";

      String Vis2_A_src =
              "import p2.*;" +
              "public class Vis2_A {" +
              "    public static void main(String args[]) throws Exception {" +
                      // Try loading a class within a newly introduced java.base
                      // package.  Make sure the class can be found via --patch-module.
              "        try {" +
              "            p2.Vis2_B b = new p2.Vis2_B();" +
              "            if (b.getClass().getClassLoader() != null) {" +
              "                throw new RuntimeException(\"PatchModuleVisibility FAILED - class B " +
                                                           "should be loaded by boot class loader\\n\");" +
              "            }" +
              "            b.m();" +
              "        } catch (Throwable e) {" +
              "            throw new RuntimeException(\"PatchModuleVisibility FAILED - test " +
                                                       "should not throw an error or exception\\n\");" +
              "        }" +
              "        System.out.println(\"PatchModuleVisibility PASSED\\n\");" +
              "    }" +
              "}";

      ClassFileInstaller.writeClassToDisk("p2/Vis2_B",
          InMemoryJavaCompiler.compile("p2.Vis2_B", Vis2_B_src), System.getProperty("test.classes"));
      ClassFileInstaller.writeClassToDisk("p2/Vis2_B", "mods2/java.base");

      ClassFileInstaller.writeClassToDisk("Vis2_A",
          InMemoryJavaCompiler.compile("Vis2_A", Vis2_A_src), System.getProperty("test.classes"));

      // Make sure the classes are actually being loaded from mods2
      Files.delete(Paths.get(System.getProperty("test.classes") +  File.separator +
                                                           "p2" + File.separator + "Vis2_B.class"));

      new OutputAnalyzer(ProcessTools.createJavaProcessBuilder(
              "--patch-module=java.base=mods2/java.base",
              "--add-exports=java.base/p2=ALL-UNNAMED",
              "Vis2_A")
          .start()).shouldHaveExitValue(0);
    }
 
Example #14
Source File: XbootcpVisibility.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 {

        String Vis1_B_src =
                "package p2;" +
                "public class Vis1_B { public void m() { System.out.println(\"In B's m()\"); } }";

        ClassFileInstaller.writeClassToDisk("p2/Vis1_B",
            InMemoryJavaCompiler.compile("p2.Vis1_B", Vis1_B_src), System.getProperty("test.classes"));
        ClassFileInstaller.writeClassToDisk("p2/Vis1_B", "mods1");

        String Vis1_C_src =
                "package p2;" +
                "public class Vis1_C { public void m() { System.out.println(\"In C's m()\"); } }";

        ClassFileInstaller.writeClassToDisk("p2/Vis1_C",
            InMemoryJavaCompiler.compile("p2.Vis1_C", Vis1_C_src), System.getProperty("test.classes"));
        ClassFileInstaller.writeClassToDisk("p2/Vis1_C", "mods1");

        String Vis1_A_src =
            "public class Vis1_A {" +
            "    public static void main(String args[]) throws Exception {" +
            // Try loading a class within a named package
            // in the unnamed module.
            // Ensure the class can still be loaded successfully by the
            // boot loader since it is located on -Xbootclasspath/a.
            "        try {" +
            "            p2.Vis1_B b = new p2.Vis1_B();" +
            "            if (b.getClass().getClassLoader() != null) {" +
            "              throw new RuntimeException(\"XbootcpVisibility FAILED - class B " +
                                                      "should be loaded by boot class loader\\n\");" +
            "            }" +
            "            b.m();" +
            // Now that the package p2 has been recorded in the
            // unnamed module within the boot loader's PackageEntryTable,
            // ensure that a different class within the same package
            // can be located on -Xbootclasspath/a as well.
            "            p2.Vis1_C c = new p2.Vis1_C();" +
            "            if (c.getClass().getClassLoader() != null) {" +
            "              throw new RuntimeException(\"XbootcpVisibility FAILED - class C " +
                                                       "should be loaded by boot class loader\\n\");" +
            "            }" +
            "            c.m();" +
            "        } catch (Exception e) {" +
            "            System.out.println(e);" +
            "            throw new RuntimeException(\"XbootcpVisibility FAILED - " +
                                                     "test should not throw exception\\n\");" +
            "        }" +
            "        System.out.println(\"XbootcpVisibility PASSED\\n\");" +
            "    }" +
            "}";

        ClassFileInstaller.writeClassToDisk("Vis1_A",
                InMemoryJavaCompiler.compile("Vis1_A", Vis1_A_src), System.getProperty("test.classes"));

        // Make sure the classes are actually being loaded from mods1
        Files.delete(Paths.get(System.getProperty("test.classes") +  File.separator +
                                                               "p2" + File.separator + "Vis1_B.class"));
        Files.delete(Paths.get(System.getProperty("test.classes") +  File.separator +
                                                               "p2" + File.separator + "Vis1_C.class"));

        new OutputAnalyzer(ProcessTools.createJavaProcessBuilder(
                "-Xbootclasspath/a:nonexistent.jar",
                "-Xbootclasspath/a:mods1",
                "Vis1_A")
            .start()).shouldHaveExitValue(0);
    }
 
Example #15
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 #16
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 #17
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);
}
 
Example #18
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);
}