8202100: Merge vm/share/InMemoryJavaCompiler w/ jdk/test/lib/compiler/InMemoryJavaCompiler

Reviewed-by: lmesnik
This commit is contained in:
Ramkumar Sunderbabu 2024-10-30 15:45:03 +00:00 committed by Leonid Mesnik
parent 821c514a13
commit 7404ddf24a
12 changed files with 142 additions and 198 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2023, 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
@ -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
*/

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 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
@ -52,7 +52,8 @@ public class RedefineCallerClassTest extends DynamicArchiveTestBase {
"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 {

View File

@ -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
*/

View File

@ -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 {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2023, 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
@ -48,7 +48,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 {

View File

@ -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.HashMap;
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 class BytecodeGeneratorFactory implements BytecodeFactory {
@Override
public Bytecode createBytecode(String className) {
Map<String, CharSequence> sources = new HashMap<String, CharSequence>();
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);
}

View File

@ -60,7 +60,7 @@ import nsk.share.test.ExecutionController;
import nsk.share.test.Stresser;
import nsk.share.test.TestBase;
import nsk.share.test.Tests;
import vm.share.InMemoryJavaCompiler;
import jdk.test.lib.compiler.InMemoryJavaCompiler;
/**
* Test checks that static fields will be initialized in new loaded class. Test performs in loop the following routine:
@ -210,9 +210,7 @@ public class StaticReferences extends GCTestBase {
}
private byte[] generateAndCompile(int[] fieldQuantities) {
Map<String, CharSequence> sources = new HashMap<String, CharSequence>();
sources.put("A", generateSource(fieldQuantities));
return InMemoryJavaCompiler.compile(sources).values().iterator().next();
return InMemoryJavaCompiler.compile("A", generateSource(fieldQuantities));
}
private StringBuffer generateSource(int[] fieldQuantities) {

View File

@ -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 java.util.concurrent.atomic.AtomicLong;
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 class StressDictionary extends GCTestBase {
}
private byte[] generateAndCompile() {
Map<String, CharSequence> sources = new HashMap<String, CharSequence>();
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) {

View File

@ -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 @@ package metaspace.stressHierarchy.common.generateHierarchy;
import java.util.*;
import vm.share.InMemoryJavaCompiler;
import jdk.test.lib.compiler.InMemoryJavaCompiler;
import jdk.test.lib.Utils;
public class GenerateHierarchyHelper {

View File

@ -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
@ -21,7 +21,6 @@
* questions.
*/
//package nsk.jvmti.RedefineClasses.StressRedefine;
package nsk.jvmti.RedefineClasses;
@ -41,7 +40,7 @@ import nsk.share.test.ExecutionController;
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
@ -203,9 +202,7 @@ public class StressRedefine extends GCTestBase {
}
private static byte[] generateAndCompile() {
Map<String, CharSequence> sources = new HashMap<String, CharSequence>();
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.

View File

@ -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<String, byte[]> compile(Map<String, ? extends CharSequence> inputMap) {
Collection<JavaFileObject> sourceFiles = new LinkedList<JavaFileObject>();
for (Entry<String, ? extends CharSequence> 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<JavaFileManager> {
private Map<String, ClassFile> classesMap = new HashMap<String, ClassFile>();
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<String, byte[]> getByteCode() {
Map<String, byte[]> result = new HashMap<String, byte[]>();
for (Entry<String, ClassFile> 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;
}
}
}

View File

@ -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 @@ package jdk.test.lib.compiler;
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 @@ import javax.tools.ToolProvider;
* </pre>
*/
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<JavaFileManager> {
private static final Location PATCH_LOCATION = new Location() {
@ -119,12 +96,13 @@ public class InMemoryJavaCompiler {
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 class InMemoryJavaCompiler {
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 class InMemoryJavaCompiler {
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<JavaFileManager> {
private Map<String, ClassFile> classesMap = new HashMap<String, ClassFile>();
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<String, byte[]> getByteCode() {
Map<String, byte[]> result = new HashMap<String, byte[]>();
for (Entry<String, ClassFile> 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<String, byte[]> compile(Map<String, ? extends CharSequence> inputMap) {
Collection<JavaFileObject> sourceFiles = new LinkedList<JavaFileObject>();
for (Entry<String, ? extends CharSequence> 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 class InMemoryJavaCompiler {
* @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<String> opts = new ArrayList<>();
String moduleOverride = null;
for (String opt : options) {
@ -183,13 +256,13 @@ public class InMemoryJavaCompiler {
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);
}