1
0
Fork 0

First commit

This commit is contained in:
Florian 2015-12-23 18:38:34 +01:00
commit 58c64ab472
62 changed files with 29291 additions and 0 deletions

View file

@ -0,0 +1,24 @@
package mrdev023.utils.compile;
import java.io.*;
import java.net.*;
import javax.tools.*;
import javax.tools.JavaFileObject.*;
public class CompiledCode extends SimpleJavaFileObject {
private ByteArrayOutputStream baos = new ByteArrayOutputStream();
public CompiledCode(String className) throws Exception {
super(new URI(className), Kind.CLASS);
}
@Override
public OutputStream openOutputStream() throws IOException {
return baos;
}
public byte[] getByteCode() {
return baos.toByteArray();
}
}

View file

@ -0,0 +1,28 @@
package mrdev023.utils.compile;
import java.util.*;
import mrdev023.*;
public class DynamicClassLoader extends ClassLoader {
private Map<String, CompiledCode> customCompiledCode = new HashMap<>();
public DynamicClassLoader(ClassLoader parent) {
super(parent);
}
public void setCode(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);
}
}

View file

@ -0,0 +1,36 @@
package mrdev023.utils.compile;
import java.io.*;
import javax.tools.*;
import mrdev023.*;
public class ExtendedStandardJavaFileManager extends ForwardingJavaFileManager<JavaFileManager> {
private CompiledCode compiledCode;
private DynamicClassLoader cl;
/**
* Creates a new instance of ForwardingJavaFileManager.
*
* @param fileManager delegate to this file manager
* @param cl
*/
protected ExtendedStandardJavaFileManager(JavaFileManager fileManager, CompiledCode compiledCode, DynamicClassLoader cl) {
super(fileManager);
this.compiledCode = compiledCode;
this.cl = cl;
this.cl.setCode(compiledCode);
}
@Override
public JavaFileObject getJavaFileForOutput(JavaFileManager.Location location, String className, JavaFileObject.Kind kind, FileObject sibling) throws IOException {
return compiledCode;
}
@Override
public ClassLoader getClassLoader(JavaFileManager.Location location) {
return cl;
}
}

View file

@ -0,0 +1,27 @@
package mrdev023.utils.compile;
import java.util.*;
import javax.tools.*;
import mrdev023.*;
public class InMemoryJavaCompiler {
static JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
public static Class<?> compile(String className, String sourceCodeInText) throws Exception {
// System.setProperty("java.home", "C:\\Program Files\\Java\\jdk1.8.0_51\\bin");
SourceCode sourceCode = new SourceCode(className, sourceCodeInText);
CompiledCode compiledCode = new CompiledCode(className);
Iterable<? extends JavaFileObject> compilationUnits = Arrays.asList(sourceCode);
DynamicClassLoader cl = new DynamicClassLoader(ClassLoader.getSystemClassLoader());
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
ExtendedStandardJavaFileManager fileManager = new ExtendedStandardJavaFileManager(javac.getStandardFileManager(diagnostics, null, null),compiledCode, cl);
JavaCompiler.CompilationTask task = javac.getTask(null, fileManager, null, null, null, compilationUnits);
boolean result = task.call();
if(!result)System.out.println(diagnostics.getDiagnostics());
if(cl != null)
return cl.loadClass(className);
else return null;
}
}

View file

@ -0,0 +1,20 @@
package mrdev023.utils.compile;
import java.io.*;
import java.net.*;
import javax.tools.*;
import javax.tools.JavaFileObject.*;
public class SourceCode extends SimpleJavaFileObject {
private String contents = null;
public SourceCode(String className, String contents) throws Exception {
super(URI.create("string:///" + className.replace('.', '/') + Kind.SOURCE.extension), Kind.SOURCE);
this.contents = contents;
}
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
return contents;
}
}