Path: blob/master/test/hotspot/jtreg/vmTestbase/vm/share/InMemoryJavaCompiler.java
41153 views
/*1* Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22package vm.share;2324import javax.tools.FileObject;25import javax.tools.ForwardingJavaFileManager;26import javax.tools.JavaCompiler;27import javax.tools.JavaFileManager;28import javax.tools.JavaFileObject;29import javax.tools.SimpleJavaFileObject;30import javax.tools.ToolProvider;31import java.io.ByteArrayOutputStream;32import java.io.StringWriter;33import java.io.Writer;34import java.net.URI;35import java.util.Collection;36import java.util.HashMap;37import java.util.LinkedList;38import java.util.Map;39import java.util.Map.Entry;404142public class InMemoryJavaCompiler {4344public static Map<String, byte[]> compile(Map<String, ? extends CharSequence> inputMap) {45Collection<JavaFileObject> sourceFiles = new LinkedList<JavaFileObject>();46for (Entry<String, ? extends CharSequence> entry : inputMap.entrySet()) {47sourceFiles.add(new SourceFile(entry.getKey(), entry.getValue()));48}4950JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();51FileManager fileManager = new FileManager(compiler.getStandardFileManager(null, null, null));5253Writer writer = new StringWriter();54Boolean exitCode = compiler.getTask(writer, fileManager, null, null, null, sourceFiles).call();55if (!exitCode) {56System.out.println("*********** javac output begin ***********");57System.out.println(writer.toString());58System.out.println("*********** javac output end ***********");59if (writer.toString().contains("java.lang.OutOfMemoryError")) {60System.out.println("Got OOME while performing in memory compilation. It happens on weak hosts and there is nothing we can do. ");61throw new OutOfMemoryError("Got OOME while performing in memory compilation.");62}63throw new RuntimeException("Test bug: in memory compilation failed.");64}65return fileManager.getByteCode();66}6768// Wraper for class file69static class ClassFile extends SimpleJavaFileObject {7071private final ByteArrayOutputStream baos = new ByteArrayOutputStream();7273protected ClassFile(String name) {74super(URI.create("memo:///" + name.replace('.', '/') + Kind.CLASS.extension), Kind.CLASS);75}7677@Override78public ByteArrayOutputStream openOutputStream() { return this.baos; }7980byte[] toByteArray() { return baos.toByteArray(); }81}8283// File manager which spawns ClassFile instances by demand84static class FileManager extends ForwardingJavaFileManager<JavaFileManager> {8586private Map<String, ClassFile> classesMap = new HashMap<String, ClassFile>();8788protected FileManager(JavaFileManager fileManager) {89super(fileManager);90}9192@Override93public ClassFile getJavaFileForOutput(Location location, String name, JavaFileObject.Kind kind, FileObject source) {94ClassFile classFile = new ClassFile(name);95classesMap.put(name, classFile);96return classFile;97}9899public Map<String, byte[]> getByteCode() {100Map<String, byte[]> result = new HashMap<String, byte[]>();101for (Entry<String, ClassFile> entry : classesMap.entrySet()) {102result.put(entry.getKey(), entry.getValue().toByteArray());103}104return result;105}106}107108// Wrapper for source file109static class SourceFile extends SimpleJavaFileObject {110111private CharSequence sourceCode;112113public SourceFile(String name, CharSequence sourceCode) {114super(URI.create("memo:///" + name.replace('.', '/') + Kind.SOURCE.extension), Kind.SOURCE);115this.sourceCode = sourceCode;116}117118@Override119public CharSequence getCharContent(boolean ignore) {120return this.sourceCode;121}122}123124}125126127