DynamicCompiler { public Map<String, Class<?>> compileMany(Map<String, CharSequence> sources, List<String> optionList) { List<JavaFileObject> sourceObjects = new ArrayList<JavaFileObject>(); for (String qualifiedName : sources.keySet()) { CharSequence sourceCode = sources.get(qualifiedName); String className = qualifiedName; String packageName = ""; int lastDot = qualifiedName.lastIndexOf('.'); if (lastDot >= 0) { packageName = qualifiedName.substring(0, lastDot); className = qualifiedName.substring(lastDot + 1); } InMemoryJavaFileObject sourceObject = new InMemoryJavaFileObject(className, sourceCode); this.inMemoryFileManager.prepareJavaFileObject(StandardLocation.SOURCE_PATH, packageName, className + ".java", sourceObject); sourceObjects.add(sourceObject); } CompilationTask task = this.javaCompiler.getTask(null, this.inMemoryFileManager, diagonosticListener, optionList, null, sourceObjects); Boolean result = task.call(); if (result == null || !result) { LOG.info("Compilation failed."); for (JavaFileObject sourceObject : sourceObjects) System.out.println(((InMemoryJavaFileObject) sourceObject).getCharContent(true)); throw new DynamicCompilerException("Compilation failed."); } try { Map<String, Class<?>> compiledClasses = new HashMap<String, Class<?>>(); for (String qualifiedName : sources.keySet()) { final Class<?> compiledClass = this.inMemoryClassLoader.loadClass(qualifiedName); compiledClasses.put(qualifiedName, compiledClass); } return compiledClasses; } catch (ClassNotFoundException e) { throw new DynamicCompilerException(e); } } DynamicCompiler(ClassLoader classLoader, DiagnosticListener<JavaFileObject> diagnosticListener); Class<?> compile(String name, CharSequence source, List<String> optionList); Map<String, Class<?>> compileMany(Map<String, CharSequence> sources, List<String> optionList);  }
@Test public void testCompileTwoClasses() throws Exception { ClassLoader classLoader = this.getClass().getClassLoader(); DiagnositicsLogger diagnosticsLogger = new DiagnositicsLogger(); DynamicCompiler compiler = new DynamicCompiler(classLoader, diagnosticsLogger); String src1 = "package com.joliciel.talismane.utils.compiler.foo;\n" + "import com.joliciel.talismane.utils.compiler.DynamicCompilerTest.HelloInterface;\n" + "public class Foo implements HelloInterface {\n" + " public String hello(String who) {\n" + " return \"Hello \" + who;\n" + " }\n" + " }"; String name1 = "com.joliciel.talismane.utils.compiler.foo.Foo"; String src2 = "package com.joliciel.talismane.utils.compiler.foo;\n" + "import com.joliciel.talismane.utils.compiler.DynamicCompilerTest.HelloInterface;\n" + "public class Bar implements HelloInterface {\n" + " public String hello(String who) {\n" + " Foo foo = new Foo();\n" + " return foo.hello(who) + \"!\";\n" + " }\n" + " }"; String name2 = "com.joliciel.talismane.utils.compiler.foo.Bar"; Map<String, CharSequence> sources = new LinkedHashMap<String, CharSequence>(); sources.put(name1, src1); sources.put(name2, src2); Map<String, Class<?>> classes = compiler.compileMany(sources, null); @SuppressWarnings("unchecked") Class<HelloInterface> helloClass = (Class<HelloInterface>) classes.get(name2); HelloInterface bar = helloClass.newInstance(); assertEquals("Hello world!", bar.hello("world")); }