diff --git a/pom.xml b/pom.xml index 9ce6f71..68632bd 100644 --- a/pom.xml +++ b/pom.xml @@ -49,6 +49,11 @@ maven-jdk-tools-wrapper 0.1 + + org.mdkt.compiler + InMemoryJavaCompiler + 1.3.0 + diff --git a/src/main/java/io/kaitai/struct/visualizer/VisualizerPanel.java b/src/main/java/io/kaitai/struct/visualizer/VisualizerPanel.java index cbd8fa6..01d6357 100644 --- a/src/main/java/io/kaitai/struct/visualizer/VisualizerPanel.java +++ b/src/main/java/io/kaitai/struct/visualizer/VisualizerPanel.java @@ -135,7 +135,7 @@ private void parseFileWithKSY(String ksyFileName, String binaryFileName) throws paramNames.add(p.group(1)); } - final Class ksyClass = InMemoryJavaCompiler.compile(DEST_PACKAGE + "." + m.group(1), javaSrc); + final Class ksyClass = InMemoryJavaCompiler.newInstance().compile(DEST_PACKAGE + "." + m.group(1), javaSrc); struct = construct(ksyClass, paramNames, binaryFileName); // Find and run "_read" that does actual parsing diff --git a/src/main/java/org/mdkt/compiler/CompiledCode.java b/src/main/java/org/mdkt/compiler/CompiledCode.java deleted file mode 100644 index e6715cc..0000000 --- a/src/main/java/org/mdkt/compiler/CompiledCode.java +++ /dev/null @@ -1,33 +0,0 @@ -package org.mdkt.compiler; - -import javax.tools.SimpleJavaFileObject; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.OutputStream; -import java.net.URI; - -/** - * Created by trung on 5/3/15. - */ -public class CompiledCode extends SimpleJavaFileObject { - private ByteArrayOutputStream baos = new ByteArrayOutputStream(); - private String className; - - public CompiledCode(String className) throws Exception { - super(new URI(className), Kind.CLASS); - this.className = className; - } - - public String getClassName() { - return className; - } - - @Override - public OutputStream openOutputStream() throws IOException { - return baos; - } - - public byte[] getByteCode() { - return baos.toByteArray(); - } -} diff --git a/src/main/java/org/mdkt/compiler/DynamicClassLoader.java b/src/main/java/org/mdkt/compiler/DynamicClassLoader.java deleted file mode 100644 index a436ec2..0000000 --- a/src/main/java/org/mdkt/compiler/DynamicClassLoader.java +++ /dev/null @@ -1,30 +0,0 @@ -package org.mdkt.compiler; - -import java.util.HashMap; -import java.util.Map; - -/** - * Created by trung on 5/3/15. - */ -public class DynamicClassLoader extends ClassLoader { - - private Map customCompiledCode = new HashMap<>(); - - public DynamicClassLoader(ClassLoader parent) { - super(parent); - } - - public void addCode(CompiledCode cc) { - customCompiledCode.put(cc.getName(), cc); - } - - @Override - protected Class findClass(String name) throws ClassNotFoundException { - CompiledCode cc = customCompiledCode.get(name); - if (cc == null) { - return super.findClass(name); - } - byte[] byteCode = cc.getByteCode(); - return defineClass(name, byteCode, 0, byteCode.length); - } -} diff --git a/src/main/java/org/mdkt/compiler/ExtendedStandardJavaFileManager.java b/src/main/java/org/mdkt/compiler/ExtendedStandardJavaFileManager.java deleted file mode 100644 index 1dc8a41..0000000 --- a/src/main/java/org/mdkt/compiler/ExtendedStandardJavaFileManager.java +++ /dev/null @@ -1,58 +0,0 @@ -package org.mdkt.compiler; - -import java.io.FileNotFoundException; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import javax.tools.FileObject; -import javax.tools.ForwardingJavaFileManager; -import javax.tools.JavaFileManager; -import javax.tools.JavaFileObject; - -/** - * Created by trung on 5/3/15. Edited by turpid-monkey on 9/25/15, completed - * support for multiple compile units. - */ -public class ExtendedStandardJavaFileManager extends - ForwardingJavaFileManager { - - private List compiledCode = new ArrayList(); - private DynamicClassLoader cl; - - /** - * Creates a new instance of ForwardingJavaFileManager. - * - * @param fileManager - * delegate to this file manager - * @param cl - */ - protected ExtendedStandardJavaFileManager(JavaFileManager fileManager, - DynamicClassLoader cl) { - super(fileManager); - this.cl = cl; - } - - @Override - public JavaFileObject getJavaFileForOutput( - JavaFileManager.Location location, String className, - JavaFileObject.Kind kind, FileObject sibling) throws IOException { - - try { - CompiledCode innerClass = new CompiledCode(className); - compiledCode.add(innerClass); - cl.addCode(innerClass); - return innerClass; - } catch (Exception e) { - throw new RuntimeException( - "Error while creating in-memory output file for " - + className, e); - } - } - - @Override - public ClassLoader getClassLoader(JavaFileManager.Location location) { - return cl; - } -} diff --git a/src/main/java/org/mdkt/compiler/InMemoryJavaCompiler.java b/src/main/java/org/mdkt/compiler/InMemoryJavaCompiler.java deleted file mode 100644 index 628fbc2..0000000 --- a/src/main/java/org/mdkt/compiler/InMemoryJavaCompiler.java +++ /dev/null @@ -1,81 +0,0 @@ -package org.mdkt.compiler; - -import java.io.IOException; -import java.util.Collection; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; - -import javax.tools.Diagnostic; -import javax.tools.DiagnosticListener; -import javax.tools.JavaCompiler; -import javax.tools.JavaFileObject; -import javax.tools.ToolProvider; - -/** - * Created by trung on 5/3/15. - * Edited by turpid-monkey on 9/25/15, added support for multiple, dependent compile units. - */ -public class InMemoryJavaCompiler { - JavaCompiler javac; - DynamicClassLoader classLoader; - - Map clazzCode = new HashMap(); - - public InMemoryJavaCompiler(ClassLoader parent) { - this(ToolProvider.getSystemJavaCompiler(), parent); - } - - public InMemoryJavaCompiler(JavaCompiler javac, ClassLoader parent) { - this.javac = javac; - this.classLoader = new DynamicClassLoader(parent); - } - - public InMemoryJavaCompiler() { - this(ToolProvider.getSystemJavaCompiler(), ClassLoader - .getSystemClassLoader()); - } - - public void addSource(String className, String sourceCodeInText) - throws Exception { - clazzCode.put(className, new SourceCode(className, sourceCodeInText)); - } - - public Map> compileAll() throws Exception { - Collection compilationUnits = clazzCode.values(); - CompiledCode[] code; - - code = new CompiledCode[compilationUnits.size()]; - Iterator iter = compilationUnits.iterator(); - for (int i=0; i> classes = new HashMap>(); - for (String className : clazzCode.keySet()) { - classes.put(className, classLoader.loadClass(className)); - } - return classes; - } - - public static Class compile(String className, String sourceCodeInText) - throws Exception { - InMemoryJavaCompiler comp = new InMemoryJavaCompiler(); - comp.addSource(className, sourceCodeInText); - Map> clzzes = comp.compileAll(); - Class result = clzzes.get(className); - return result; - } - - public DynamicClassLoader getClassLoader() { - return classLoader; - } -} diff --git a/src/main/java/org/mdkt/compiler/SourceCode.java b/src/main/java/org/mdkt/compiler/SourceCode.java deleted file mode 100644 index 9c7a9e7..0000000 --- a/src/main/java/org/mdkt/compiler/SourceCode.java +++ /dev/null @@ -1,29 +0,0 @@ -package org.mdkt.compiler; - -import javax.tools.SimpleJavaFileObject; -import java.io.IOException; -import java.net.URI; - -/** - * Created by trung on 5/3/15. - */ -public class SourceCode extends SimpleJavaFileObject { - private String contents = null; - private String className; - - public SourceCode(String className, String contents) throws Exception { - super(URI.create("string:///" + className.replace('.', '/') - + Kind.SOURCE.extension), Kind.SOURCE); - this.contents = contents; - this.className = className; - } - - public String getClassName() { - return className; - } - - public CharSequence getCharContent(boolean ignoreEncodingErrors) - throws IOException { - return contents; - } -}