Mercurial Hosting > luan
diff core/src/luan/impl/LuanJavaCompiler.java @ 665:41f8fdbc3a0a
compile modules
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 07 Apr 2016 17:06:22 -0600 |
parents | e038905512d3 |
children |
line wrap: on
line diff
--- a/core/src/luan/impl/LuanJavaCompiler.java Thu Apr 07 15:11:52 2016 -0600 +++ b/core/src/luan/impl/LuanJavaCompiler.java Thu Apr 07 17:06:22 2016 -0600 @@ -6,6 +6,8 @@ import java.io.IOException; import java.net.URI; import java.util.Collections; +import java.util.Map; +import java.util.HashMap; import javax.tools.FileObject; import javax.tools.JavaFileObject; import javax.tools.SimpleJavaFileObject; @@ -19,6 +21,38 @@ public final class LuanJavaCompiler { private LuanJavaCompiler() {} // never + private static class MyJavaFileObject extends SimpleJavaFileObject { + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + MyJavaFileObject() { + super(URI.create("whatever"),JavaFileObject.Kind.CLASS); + } + + @Override public OutputStream openOutputStream() { + return baos; + } + + byte[] byteCode(String sourceName) { + byte[] byteCode = baos.toByteArray(); + final int len = sourceName.length(); + int max = byteCode.length-len-3; + outer: + for( int i=0; true; i++ ) { + if( i > max ) + throw new RuntimeException("len="+len); + if( byteCode[i]==1 && (byteCode[i+1] << 8 | 0xFF & byteCode[i+2]) == len ) { + for( int j=i+3; j<i+3+len; j++ ) { + if( byteCode[j] != '$' ) + continue outer; + } + System.arraycopy(sourceName.getBytes(),0,byteCode,i+3,len); + break; + } + } + return byteCode; + } + } + public static Class compile(final String className,final String sourceName,final String code) throws ClassNotFoundException { final int len = sourceName.length(); StringBuilder sb = new StringBuilder(sourceName); @@ -35,16 +69,15 @@ return true; } }; - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - JavaFileObject classFile = new SimpleJavaFileObject(URI.create("whatever"),JavaFileObject.Kind.CLASS) { - @Override public OutputStream openOutputStream() { - return baos; - } - }; + final Map<String,MyJavaFileObject> map = new HashMap<String,MyJavaFileObject>(); JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); StandardJavaFileManager sjfm = compiler.getStandardFileManager(null,null,null); ForwardingJavaFileManager fjfm = new ForwardingJavaFileManager(sjfm) { @Override public JavaFileObject getJavaFileForOutput(JavaFileManager.Location location, String className, JavaFileObject.Kind kind, FileObject sibling) throws IOException { + if( map.containsKey(className) ) + throw new RuntimeException(className); + MyJavaFileObject classFile = new MyJavaFileObject(); + map.put(className,classFile); return classFile; } }; @@ -52,24 +85,11 @@ boolean b = compiler.getTask(out, fjfm, null, null, null, Collections.singletonList(sourceFile)).call(); if( !b ) throw new RuntimeException("\n"+out+"\ncode:\n"+code+"\n"); - final byte[] byteCode = baos.toByteArray(); - int max = byteCode.length-len-3; - outer: - for( int i=0; true; i++ ) { - if( i > max ) - throw new RuntimeException("len="+len); - if( byteCode[i]==1 && (byteCode[i+1] << 8 | 0xFF & byteCode[i+2]) == len ) { - for( int j=i+3; j<i+3+len; j++ ) { - if( byteCode[j] != '$' ) - continue outer; - } - System.arraycopy(sourceName.getBytes(),0,byteCode,i+3,len); - break; - } - } ClassLoader cl = new ClassLoader() { @Override protected Class<?> findClass(String name) throws ClassNotFoundException { - if( name.equals(className) ) { + MyJavaFileObject jfo = map.get(name); + if( jfo != null ) { + byte[] byteCode = jfo.byteCode(sourceName); return defineClass(name, byteCode, 0, byteCode.length); } return super.findClass(name);