1 /*
2  * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 package vm.share;
24 
25 import javax.tools.FileObject;
26 import javax.tools.ForwardingJavaFileManager;
27 import javax.tools.JavaCompiler;
28 import javax.tools.JavaFileManager;
29 import javax.tools.JavaFileObject;
30 import javax.tools.SimpleJavaFileObject;
31 import javax.tools.ToolProvider;
32 import java.io.ByteArrayOutputStream;
33 import java.io.StringWriter;
34 import java.io.Writer;
35 import java.net.URI;
36 import java.util.Collection;
37 import java.util.HashMap;
38 import java.util.LinkedList;
39 import java.util.Map;
40 import java.util.Map.Entry;
41 
42 
43 public class InMemoryJavaCompiler {
44 
compile(Map<String, ? extends CharSequence> inputMap)45     public static Map<String, byte[]> compile(Map<String, ? extends CharSequence> inputMap) {
46         Collection<JavaFileObject> sourceFiles = new LinkedList<JavaFileObject>();
47         for (Entry<String, ? extends CharSequence> entry : inputMap.entrySet()) {
48             sourceFiles.add(new SourceFile(entry.getKey(), entry.getValue()));
49         }
50 
51         JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
52         FileManager fileManager = new FileManager(compiler.getStandardFileManager(null, null, null));
53 
54         Writer writer = new StringWriter();
55         Boolean exitCode = compiler.getTask(writer, fileManager, null, null, null, sourceFiles).call();
56         if (!exitCode) {
57             System.out.println("*********** javac output begin ***********");
58             System.out.println(writer.toString());
59             System.out.println("*********** javac output end ***********");
60             if (writer.toString().contains("java.lang.OutOfMemoryError")) {
61                 System.out.println("Got OOME while performing in memory compilation. It happens on weak hosts and there is nothing we can do. ");
62                 throw new OutOfMemoryError("Got OOME while performing in memory compilation.");
63             }
64             throw new RuntimeException("Test bug: in memory compilation failed.");
65         }
66         return fileManager.getByteCode();
67     }
68 
69     // Wraper for class file
70     static class ClassFile extends SimpleJavaFileObject {
71 
72         private final ByteArrayOutputStream baos = new ByteArrayOutputStream();
73 
ClassFile(String name)74         protected ClassFile(String name) {
75             super(URI.create("memo:///" + name.replace('.', '/') + Kind.CLASS.extension), Kind.CLASS);
76         }
77 
78         @Override
openOutputStream()79         public ByteArrayOutputStream openOutputStream() { return this.baos; }
80 
toByteArray()81         byte[] toByteArray() { return baos.toByteArray(); }
82     }
83 
84     // File manager which spawns ClassFile instances by demand
85     static class FileManager extends ForwardingJavaFileManager<JavaFileManager> {
86 
87         private Map<String, ClassFile> classesMap = new HashMap<String, ClassFile>();
88 
FileManager(JavaFileManager fileManager)89         protected FileManager(JavaFileManager fileManager) {
90             super(fileManager);
91         }
92 
93         @Override
getJavaFileForOutput(Location location, String name, JavaFileObject.Kind kind, FileObject source)94         public ClassFile getJavaFileForOutput(Location location, String name, JavaFileObject.Kind kind, FileObject source) {
95             ClassFile classFile = new ClassFile(name);
96             classesMap.put(name, classFile);
97             return classFile;
98         }
99 
getByteCode()100         public Map<String, byte[]> getByteCode() {
101             Map<String, byte[]> result = new HashMap<String, byte[]>();
102             for (Entry<String, ClassFile> entry : classesMap.entrySet()) {
103                 result.put(entry.getKey(), entry.getValue().toByteArray());
104             }
105             return result;
106         }
107     }
108 
109     // Wrapper for source file
110     static class SourceFile extends SimpleJavaFileObject {
111 
112         private CharSequence sourceCode;
113 
SourceFile(String name, CharSequence sourceCode)114         public SourceFile(String name, CharSequence sourceCode) {
115             super(URI.create("memo:///" + name.replace('.', '/') + Kind.SOURCE.extension), Kind.SOURCE);
116             this.sourceCode = sourceCode;
117         }
118 
119         @Override
getCharContent(boolean ignore)120         public CharSequence getCharContent(boolean ignore) {
121             return this.sourceCode;
122         }
123     }
124 
125 }
126