Search |
||
How to compile on the fly?Posted by malenkov on December 17, 2008 at 12:00 AM PST
A To start, let's define the class that will contain the source code to compile. To do so, override the class Source extends SimpleJavaFileObject { private final String content; Source(String name, Kind kind, String content) { super(URI.create("memo:///" + name.replace('.', '/') + kind.extension), kind); this.content = content; } @Override public CharSequence getCharContent(boolean ignore) { return this.content; } } Further, create a class to obtain the compiled binary code. Override the class Output extends SimpleJavaFileObject { private final ByteArrayOutputStream baos = new ByteArrayOutputStream(); Output(String name, Kind kind) { super(URI.create("memo:///" + name.replace('.', '/') + kind.extension), kind); } byte[] toByteArray() { return this.baos.toByteArray(); } @Override public ByteArrayOutputStream openOutputStream() { return this.baos; } } Now create a class the compiler will use to manage files. The class MemoryFileManager extends ForwardingJavaFileManager<JavaFileManager> { private final Map<String, Output> map = new HashMap<String, Output>(); MemoryFileManager(JavaCompiler compiler) { super(compiler.getStandardFileManager(null, null, null)); } @Override public Output getJavaFileForOutput (Location location, String name, Kind kind, FileObject source) { Output mc = new Output(name, kind); this.map.put(name, mc); return mc; } } Finally, create a custom class loader, that compiles all sources when initiated (in its constructor). Later the loader creates a class MemoryClassLoader extends ClassLoader { private final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); private final MemoryFileManager manager = new MemoryFileManager(this.compiler); public MemoryClassLoader(String classname, String filecontent) { this(Collections.singletonMap(classname, filecontent)); } public MemoryClassLoader(Map<String, String> map) { List<Source> list = new ArrayList<Source>(); for (Map.Entry<String, String> entry : map.entrySet()) { list.add(new Source(entry.getKey(), Kind.SOURCE, entry.getValue())); } this.compiler.getTask(null, this.manager, null, null, null, list).call(); } @Override protected Class<?> findClass(String name) throws ClassNotFoundException { synchronized (this.manager) { Output mc = this.manager.map.remove(name); if (mc != null) { byte[] array = mc.toByteArray(); return defineClass(name, array, 0, array.length); } } return super.findClass(name); } } Let's take a look at the example that calculates private static final String CLASS = "Test"; private static final String METHOD = "execute"; private static final String EXPRESSION = "Math.cos(Math.PI/6)"; private static final String CONTENT = "public class " + CLASS + " {" + " public static Object " + METHOD + "() {" + " return " + EXPRESSION + ";" + " }" + "}"; public static void main(String[] args) throws Exception { MemoryClassLoader mcl = new MemoryClassLoader(CLASS, CONTENT); System.out.println(mcl.loadClass(CLASS).getMethod(METHOD).invoke(null)); } The source file of the example is available. »
Related Topics >>
J2SE Comments
Comments are listed in date ascending order (oldest first)
|
||
|
|