JavaCompiler from JDK 1.6: how to write class bytes directly to byte[] array?

百般思念 提交于 2019-11-30 15:09:54

问题


So I recently learned of the new JavaCompiler API available in JDK 1.6. This makes it very simple to compile a String to a .class file directly from running code:

String className = "Foo";
String sourceCode = "...";

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

List<JavaSourceFromString> unitsToCompile = new ArrayList<JavaSourceFromString>() 
    {{ 
         add(new JavaSourceFromString(className, sourceCode)); 
    }};

StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
compiler.getTask(null, fileManager, null, null, null, unitsToCompile).call();
fileManager.close();    

ByteArrayOutputStream bos = new ByteArrayOutputStream();
FileInputStream fis = new FileInputStream(className + ".class");
IOUtils.copyStream(fis, bos);

return bos.toByteArray();

You can grab the source to JavaSourceFromString from the Javadoc.

This will very handily compile sourceCode to Foo.class in the current working directory.

My question is: is it possible to compile straight to a byte[] array, and avoid the messiness of dealing with File I/O altogether?


回答1:


Maybe you could create your own javax.tools.JavaFileManager implementing class where you would return your own implementation of javax.tools.FileObject which would then write it out to memory instead to disk. So for your subclass of javax.tools.FileObject Writer openWriter() throws IOException method you would return a java.io.StringWriter. All the methods should be converted to their String counterparts.




回答2:


The reason that there is no standard API to write bytecodes to a byte array is that compiling a single Java source file may result in multiple bytecode files. For example, any source file with nested / inner / anonymous classes will result in multiple bytecode files.

If you roll your own JavaFileManager, you will need to deal with this situation.




回答3:


The demo application that shipped with the JSR 199 API had an in-memory compile-from-string example (which is indeed using a MemoryFileManager). Maybe have a look at it here or here (these samples are a bit outdated though, they will require slight changes). Also maybe check the How to compile on the fly? article on Java.net.

PS: I didn't look at all the details, but I'm don't think it handles the cases mentioned by Stephen C.



来源:https://stackoverflow.com/questions/2130039/javacompiler-from-jdk-1-6-how-to-write-class-bytes-directly-to-byte-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!