diff --git a/test/hotspot/jtreg/runtime/cds/appcds/dynamicArchive/LambdaProxyCallerIsHidden.java b/test/hotspot/jtreg/runtime/cds/appcds/dynamicArchive/LambdaProxyCallerIsHidden.java index 198551a6123..13abce572e7 100644 --- a/test/hotspot/jtreg/runtime/cds/appcds/dynamicArchive/LambdaProxyCallerIsHidden.java +++ b/test/hotspot/jtreg/runtime/cds/appcds/dynamicArchive/LambdaProxyCallerIsHidden.java @@ -37,7 +37,8 @@ * jdk/test/lib/compiler/InMemoryJavaCompiler * jdk/test/lib/compiler/InMemoryJavaCompiler$FileManagerWrapper$1 * jdk/test/lib/compiler/InMemoryJavaCompiler$FileManagerWrapper - * jdk/test/lib/compiler/InMemoryJavaCompiler$MemoryJavaFileObject + * jdk/test/lib/compiler/InMemoryJavaCompiler$SourceFile + * jdk/test/lib/compiler/InMemoryJavaCompiler$ClassFile * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. LambdaProxyCallerIsHidden */ diff --git a/test/hotspot/jtreg/runtime/cds/appcds/dynamicArchive/RegularHiddenClass.java b/test/hotspot/jtreg/runtime/cds/appcds/dynamicArchive/RegularHiddenClass.java index cc05e144444..f20b50efba2 100644 --- a/test/hotspot/jtreg/runtime/cds/appcds/dynamicArchive/RegularHiddenClass.java +++ b/test/hotspot/jtreg/runtime/cds/appcds/dynamicArchive/RegularHiddenClass.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -35,7 +35,8 @@ * jdk/test/lib/compiler/InMemoryJavaCompiler * jdk/test/lib/compiler/InMemoryJavaCompiler$FileManagerWrapper$1 * jdk/test/lib/compiler/InMemoryJavaCompiler$FileManagerWrapper - * jdk/test/lib/compiler/InMemoryJavaCompiler$MemoryJavaFileObject + * jdk/test/lib/compiler/InMemoryJavaCompiler$SourceFile + * jdk/test/lib/compiler/InMemoryJavaCompiler$ClassFile * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. RegularHiddenClass */ diff --git a/test/hotspot/jtreg/runtime/cds/appcds/redefineClass/RedefineBasicTest.java b/test/hotspot/jtreg/runtime/cds/appcds/redefineClass/RedefineBasicTest.java index 81d6e20321e..acbd0c948be 100644 --- a/test/hotspot/jtreg/runtime/cds/appcds/redefineClass/RedefineBasicTest.java +++ b/test/hotspot/jtreg/runtime/cds/appcds/redefineClass/RedefineBasicTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2016, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -47,7 +47,8 @@ public class RedefineBasicTest { "jdk/test/lib/compiler/InMemoryJavaCompiler", "jdk/test/lib/compiler/InMemoryJavaCompiler$FileManagerWrapper", "jdk/test/lib/compiler/InMemoryJavaCompiler$FileManagerWrapper$1", - "jdk/test/lib/compiler/InMemoryJavaCompiler$MemoryJavaFileObject" + "jdk/test/lib/compiler/InMemoryJavaCompiler$SourceFile", + "jdk/test/lib/compiler/InMemoryJavaCompiler$ClassFile" }; public static void main(String[] args) throws Exception { diff --git a/test/hotspot/jtreg/runtime/cds/appcds/redefineClass/RedefineRunningMethods_Shared.java b/test/hotspot/jtreg/runtime/cds/appcds/redefineClass/RedefineRunningMethods_Shared.java index 35b93f30059..a9920ff43a8 100644 --- a/test/hotspot/jtreg/runtime/cds/appcds/redefineClass/RedefineRunningMethods_Shared.java +++ b/test/hotspot/jtreg/runtime/cds/appcds/redefineClass/RedefineRunningMethods_Shared.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2016, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -50,7 +50,8 @@ public class RedefineRunningMethods_Shared { "jdk/test/lib/compiler/InMemoryJavaCompiler", "jdk/test/lib/compiler/InMemoryJavaCompiler$FileManagerWrapper", "jdk/test/lib/compiler/InMemoryJavaCompiler$FileManagerWrapper$1", - "jdk/test/lib/compiler/InMemoryJavaCompiler$MemoryJavaFileObject" + "jdk/test/lib/compiler/InMemoryJavaCompiler$SourceFile", + "jdk/test/lib/compiler/InMemoryJavaCompiler$ClassFile" }; public static void main(String[] args) throws Exception { diff --git a/test/hotspot/jtreg/vmTestbase/gc/g1/unloading/bytecode/BytecodeGeneratorFactory.java b/test/hotspot/jtreg/vmTestbase/gc/g1/unloading/bytecode/BytecodeGeneratorFactory.java index cad856f553e..73f74794394 100644 --- a/test/hotspot/jtreg/vmTestbase/gc/g1/unloading/bytecode/BytecodeGeneratorFactory.java +++ b/test/hotspot/jtreg/vmTestbase/gc/g1/unloading/bytecode/BytecodeGeneratorFactory.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2014, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -26,7 +26,7 @@ import java.util.Map; import java.util.Random; -import vm.share.InMemoryJavaCompiler; +import jdk.test.lib.compiler.InMemoryJavaCompiler; /** * BytecodeFactory that employs in memory compilation. @@ -44,12 +44,10 @@ public BytecodeGeneratorFactory(long seed) { @Override public Bytecode createBytecode(String className) { - Map sources = new HashMap(); - sources.put(className, sourceGenerator.generateSource(className, + byte[] bytecode = InMemoryJavaCompiler.compile(className, sourceGenerator.generateSource(className, "public static void main() { System.out.println(\"From main method in in-mem-compiled code " + random.nextGaussian() + " + str_bytesToReplace0 str_bytesToReplace1\"); }\n " + "public static int methodForCompilation(Object object) { int i = object.hashCode(); i = i * 2000 / 1994 + 153; return i; }\n")); - byte[] bytecode = InMemoryJavaCompiler.compile(sources).values().iterator().next(); return new Bytecode(className, bytecode); } diff --git a/test/hotspot/jtreg/vmTestbase/metaspace/staticReferences/StaticReferences.java b/test/hotspot/jtreg/vmTestbase/metaspace/staticReferences/StaticReferences.java index 690f2a67517..1edf70d3a8c 100644 --- a/test/hotspot/jtreg/vmTestbase/metaspace/staticReferences/StaticReferences.java +++ b/test/hotspot/jtreg/vmTestbase/metaspace/staticReferences/StaticReferences.java @@ -53,7 +53,7 @@ import java.util.Map; import java.util.Random; -import vm.share.InMemoryJavaCompiler; +import jdk.test.lib.compiler.InMemoryJavaCompiler; import nsk.share.gc.GCTestBase; import nsk.share.test.ExecutionController; import nsk.share.test.Stresser; @@ -210,9 +210,7 @@ private void checkStaticFields(Class clazz) { } private byte[] generateAndCompile(int[] fieldQuantities) { - Map sources = new HashMap(); - sources.put("A", generateSource(fieldQuantities)); - return InMemoryJavaCompiler.compile(sources).values().iterator().next(); + return InMemoryJavaCompiler.compile("A", generateSource(fieldQuantities)); } private StringBuffer generateSource(int[] fieldQuantities) { diff --git a/test/hotspot/jtreg/vmTestbase/metaspace/stressDictionary/StressDictionary.java b/test/hotspot/jtreg/vmTestbase/metaspace/stressDictionary/StressDictionary.java index b4c44a9a059..0db2d24fa8f 100644 --- a/test/hotspot/jtreg/vmTestbase/metaspace/stressDictionary/StressDictionary.java +++ b/test/hotspot/jtreg/vmTestbase/metaspace/stressDictionary/StressDictionary.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -43,7 +43,7 @@ import nsk.share.gc.GCTestBase; import nsk.share.test.*; -import vm.share.InMemoryJavaCompiler; +import jdk.test.lib.compiler.InMemoryJavaCompiler; /** * There is a data structure named "dictionary" in class BlockFreelist. It stores @@ -178,10 +178,8 @@ public void run() { } private byte[] generateAndCompile() { - Map sources = new HashMap(); String className = "MyClass" + classesCounter.incrementAndGet(); - sources.put(className, generateSource(className)); - return InMemoryJavaCompiler.compile(sources).values().iterator().next(); + return InMemoryJavaCompiler.compile(className, generateSource(className)); } private CharSequence generateSource(String className) { diff --git a/test/hotspot/jtreg/vmTestbase/metaspace/stressHierarchy/common/generateHierarchy/GenerateHierarchyHelper.java b/test/hotspot/jtreg/vmTestbase/metaspace/stressHierarchy/common/generateHierarchy/GenerateHierarchyHelper.java index daccf644393..be6f1ebe85f 100644 --- a/test/hotspot/jtreg/vmTestbase/metaspace/stressHierarchy/common/generateHierarchy/GenerateHierarchyHelper.java +++ b/test/hotspot/jtreg/vmTestbase/metaspace/stressHierarchy/common/generateHierarchy/GenerateHierarchyHelper.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -24,7 +24,7 @@ import java.util.*; -import vm.share.InMemoryJavaCompiler; +import jdk.test.lib.compiler.InMemoryJavaCompiler; import jdk.test.lib.Utils; public class GenerateHierarchyHelper { diff --git a/test/hotspot/jtreg/vmTestbase/nsk/jvmti/RedefineClasses/StressRedefine.java b/test/hotspot/jtreg/vmTestbase/nsk/jvmti/RedefineClasses/StressRedefine.java index bbdb6fe6fe0..c8b3fb4d2ce 100644 --- a/test/hotspot/jtreg/vmTestbase/nsk/jvmti/RedefineClasses/StressRedefine.java +++ b/test/hotspot/jtreg/vmTestbase/nsk/jvmti/RedefineClasses/StressRedefine.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -21,7 +21,6 @@ * questions. */ -//package nsk.jvmti.RedefineClasses.StressRedefine; package nsk.jvmti.RedefineClasses; @@ -41,7 +40,7 @@ import nsk.share.test.Stresser; import nsk.share.test.Tests; -import vm.share.InMemoryJavaCompiler; +import jdk.test.lib.compiler.InMemoryJavaCompiler; /** * There is a data structure named "dictionary" in class BlockFreelist. It stores @@ -191,9 +190,7 @@ private void runIt() { } private static byte[] generateAndCompile() { - Map sources = new HashMap(); - sources.put(GenerateSourceHelper.CLASS_NAME, GenerateSourceHelper.generateSource()); - return InMemoryJavaCompiler.compile(sources).values().iterator().next(); + return InMemoryJavaCompiler.compile(GenerateSourceHelper.CLASS_NAME, GenerateSourceHelper.generateSource()); } // Auxiliary classloader. Used only once at the beginning. diff --git a/test/hotspot/jtreg/vmTestbase/vm/share/InMemoryJavaCompiler.java b/test/hotspot/jtreg/vmTestbase/vm/share/InMemoryJavaCompiler.java deleted file mode 100644 index 6f5f1c0cbcd..00000000000 --- a/test/hotspot/jtreg/vmTestbase/vm/share/InMemoryJavaCompiler.java +++ /dev/null @@ -1,125 +0,0 @@ -/* - * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ -package vm.share; - -import javax.tools.FileObject; -import javax.tools.ForwardingJavaFileManager; -import javax.tools.JavaCompiler; -import javax.tools.JavaFileManager; -import javax.tools.JavaFileObject; -import javax.tools.SimpleJavaFileObject; -import javax.tools.ToolProvider; -import java.io.ByteArrayOutputStream; -import java.io.StringWriter; -import java.io.Writer; -import java.net.URI; -import java.util.Collection; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.Map; -import java.util.Map.Entry; - - -public class InMemoryJavaCompiler { - - public static Map compile(Map inputMap) { - Collection sourceFiles = new LinkedList(); - for (Entry entry : inputMap.entrySet()) { - sourceFiles.add(new SourceFile(entry.getKey(), entry.getValue())); - } - - JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); - FileManager fileManager = new FileManager(compiler.getStandardFileManager(null, null, null)); - - Writer writer = new StringWriter(); - Boolean exitCode = compiler.getTask(writer, fileManager, null, null, null, sourceFiles).call(); - if (!exitCode) { - System.out.println("*********** javac output begin ***********"); - System.out.println(writer.toString()); - System.out.println("*********** javac output end ***********"); - if (writer.toString().contains("java.lang.OutOfMemoryError")) { - System.out.println("Got OOME while performing in memory compilation. It happens on weak hosts and there is nothing we can do. "); - throw new OutOfMemoryError("Got OOME while performing in memory compilation."); - } - throw new RuntimeException("Test bug: in memory compilation failed."); - } - return fileManager.getByteCode(); - } - - // Wraper for class file - static class ClassFile extends SimpleJavaFileObject { - - private final ByteArrayOutputStream baos = new ByteArrayOutputStream(); - - protected ClassFile(String name) { - super(URI.create("memo:///" + name.replace('.', '/') + Kind.CLASS.extension), Kind.CLASS); - } - - @Override - public ByteArrayOutputStream openOutputStream() { return this.baos; } - - byte[] toByteArray() { return baos.toByteArray(); } - } - - // File manager which spawns ClassFile instances by demand - static class FileManager extends ForwardingJavaFileManager { - - private Map classesMap = new HashMap(); - - protected FileManager(JavaFileManager fileManager) { - super(fileManager); - } - - @Override - public ClassFile getJavaFileForOutput(Location location, String name, JavaFileObject.Kind kind, FileObject source) { - ClassFile classFile = new ClassFile(name); - classesMap.put(name, classFile); - return classFile; - } - - public Map getByteCode() { - Map result = new HashMap(); - for (Entry entry : classesMap.entrySet()) { - result.put(entry.getKey(), entry.getValue().toByteArray()); - } - return result; - } - } - - // Wrapper for source file - static class SourceFile extends SimpleJavaFileObject { - - private CharSequence sourceCode; - - public SourceFile(String name, CharSequence sourceCode) { - super(URI.create("memo:///" + name.replace('.', '/') + Kind.SOURCE.extension), Kind.SOURCE); - this.sourceCode = sourceCode; - } - - @Override - public CharSequence getCharContent(boolean ignore) { - return this.sourceCode; - } - } - -} diff --git a/test/lib/jdk/test/lib/compiler/InMemoryJavaCompiler.java b/test/lib/jdk/test/lib/compiler/InMemoryJavaCompiler.java index 6016e48bf4e..4722ef3b67a 100644 --- a/test/lib/jdk/test/lib/compiler/InMemoryJavaCompiler.java +++ b/test/lib/jdk/test/lib/compiler/InMemoryJavaCompiler.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -26,11 +26,18 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.io.StringWriter; +import java.io.Writer; import java.net.URI; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +import java.util.LinkedList; import java.util.List; +import java.util.Map; +import java.util.Map.Entry; import javax.tools.FileObject; import javax.tools.ForwardingJavaFileManager; @@ -76,36 +83,6 @@ * */ public class InMemoryJavaCompiler { - private static class MemoryJavaFileObject extends SimpleJavaFileObject { - private final String className; - private final CharSequence sourceCode; - private final ByteArrayOutputStream byteCode; - - public MemoryJavaFileObject(String className, CharSequence sourceCode) { - super(URI.create("string:///" + className.replace('.','/') + Kind.SOURCE.extension), Kind.SOURCE); - this.className = className; - this.sourceCode = sourceCode; - this.byteCode = new ByteArrayOutputStream(); - } - - @Override - public CharSequence getCharContent(boolean ignoreEncodingErrors) { - return sourceCode; - } - - @Override - public OutputStream openOutputStream() throws IOException { - return byteCode; - } - - public byte[] getByteCode() { - return byteCode.toByteArray(); - } - - public String getClassName() { - return className; - } - } private static class FileManagerWrapper extends ForwardingJavaFileManager { private static final Location PATCH_LOCATION = new Location() { @@ -119,12 +96,13 @@ public boolean isOutputLocation() { return false; } }; - private final MemoryJavaFileObject file; + private final SourceFile srcFile; + private ClassFile clsFile; private final String moduleOverride; - public FileManagerWrapper(MemoryJavaFileObject file, String moduleOverride) { + public FileManagerWrapper(SourceFile file, String moduleOverride) { super(getCompiler().getStandardFileManager(null, null, null)); - this.file = file; + this.srcFile = file; this.moduleOverride = moduleOverride; } @@ -132,16 +110,17 @@ public FileManagerWrapper(MemoryJavaFileObject file, String moduleOverride) { public JavaFileObject getJavaFileForOutput(Location location, String className, Kind kind, FileObject sibling) throws IOException { - if (!file.getClassName().equals(className)) { - throw new IOException("Expected class with name " + file.getClassName() + + if (!srcFile.getClassName().equals(className)) { + throw new IOException("Expected class with name " + srcFile.getClassName() + ", but got " + className); } - return file; + clsFile = new ClassFile(className); + return clsFile; } @Override public Location getLocationForModule(Location location, JavaFileObject fo) throws IOException { - if (fo == file && moduleOverride != null) { + if (fo == srcFile && moduleOverride != null) { return PATCH_LOCATION; } return super.getLocationForModule(location, fo); @@ -160,6 +139,100 @@ public boolean hasLocation(Location location) { return super.hasLocation(location) || location == StandardLocation.PATCH_MODULE_PATH; } + public byte[] getByteCode() { + return clsFile.toByteArray(); + } + + } + + // Wraper for class file + static class ClassFile extends SimpleJavaFileObject { + + private final ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + protected ClassFile(String name) { + super(URI.create("memo:///" + name.replace('.', '/') + Kind.CLASS.extension), Kind.CLASS); + } + + @Override + public ByteArrayOutputStream openOutputStream() { return this.baos; } + + byte[] toByteArray() { return baos.toByteArray(); } + } + + // File manager which spawns ClassFile instances by demand + static class FileManager extends ForwardingJavaFileManager { + + private Map classesMap = new HashMap(); + + protected FileManager(JavaFileManager fileManager) { + super(fileManager); + } + + @Override + public ClassFile getJavaFileForOutput(Location location, String name, JavaFileObject.Kind kind, FileObject source) { + ClassFile classFile = new ClassFile(name); + classesMap.put(name, classFile); + return classFile; + } + + public Map getByteCode() { + Map result = new HashMap(); + for (Entry entry : classesMap.entrySet()) { + result.put(entry.getKey(), entry.getValue().toByteArray()); + } + return result; + } + } + + // Wrapper for source file + static class SourceFile extends SimpleJavaFileObject { + + private CharSequence sourceCode; + private String className; + + public SourceFile(String name, CharSequence sourceCode) { + super(URI.create("memo:///" + name.replace('.', '/') + Kind.SOURCE.extension), Kind.SOURCE); + this.sourceCode = sourceCode; + this.className = name; + } + + @Override + public CharSequence getCharContent(boolean ignore) { + return this.sourceCode; + } + + public String getClassName() { + return this.className; + } + } + + /** + * Compiles the list of classes with the given map of name and source code. + * This overloaded version of compile is useful for batch compile use cases. + * + * @param inputMap The map containing the name of the class and corresponding source code + * @throws RuntimeException if the compilation did not succeed + * @return The resulting byte code from the compilation + */ + public static Map compile(Map inputMap) { + Collection sourceFiles = new LinkedList(); + for (Entry entry : inputMap.entrySet()) { + sourceFiles.add(new SourceFile(entry.getKey(), entry.getValue())); + } + + JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); + FileManager fileManager = new FileManager(compiler.getStandardFileManager(null, null, null)); + + Writer writer = new StringWriter(); + Boolean exitCode = compiler.getTask(writer, fileManager, null, null, null, sourceFiles).call(); + if (!exitCode) { + System.out.println("*********** javac output begin ***********"); + System.out.println(writer.toString()); + System.out.println("*********** javac output end ***********"); + throw new RuntimeException("Test bug: in memory compilation failed."); + } + return fileManager.getByteCode(); } /** @@ -173,7 +246,7 @@ public boolean hasLocation(Location location) { * @return The resulting byte code from the compilation */ public static byte[] compile(String className, CharSequence sourceCode, String... options) { - MemoryJavaFileObject file = new MemoryJavaFileObject(className, sourceCode); + SourceFile file = new SourceFile(className, sourceCode); List opts = new ArrayList<>(); String moduleOverride = null; for (String opt : options) { @@ -183,13 +256,13 @@ public static byte[] compile(String className, CharSequence sourceCode, String.. opts.add(opt); } } - try (JavaFileManager fileManager = new FileManagerWrapper(file, moduleOverride)) { + try (FileManagerWrapper fileManager = new FileManagerWrapper(file, moduleOverride)) { CompilationTask task = getCompiler().getTask(null, fileManager, null, opts, null, Arrays.asList(file)); if (!task.call()) { throw new RuntimeException("Could not compile " + className + " with source code " + sourceCode); } - return file.getByteCode(); + return fileManager.getByteCode(); } catch (IOException ioe) { throw new RuntimeException(ioe); }