Compile Java sources in-memory
| 241 | * Compile Java sources in-memory |
| 242 | */ |
| 243 | public static class InMemoryJavaCompiler { |
| 244 | private JavaCompiler javac; |
| 245 | private DynamicClassLoader classLoader; |
| 246 | private Iterable<String> options; |
| 247 | boolean ignoreWarnings = true; |
| 248 | |
| 249 | private Map<String, SourceCode> sourceCodes = new HashMap<String, SourceCode>(); |
| 250 | |
| 251 | public static InMemoryJavaCompiler newInstance() { |
| 252 | return new InMemoryJavaCompiler(); |
| 253 | } |
| 254 | |
| 255 | private InMemoryJavaCompiler() { |
| 256 | this.javac = ToolProvider.getSystemJavaCompiler(); |
| 257 | this.classLoader = new DynamicClassLoader(ClassLoader.getSystemClassLoader()); |
| 258 | } |
| 259 | |
| 260 | public InMemoryJavaCompiler useParentClassLoader(ClassLoader parent) { |
| 261 | this.classLoader = new DynamicClassLoader(parent); |
| 262 | return this; |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * @return the class loader used internally by the compiler |
| 267 | */ |
| 268 | public ClassLoader getClassloader() { |
| 269 | return classLoader; |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Options used by the compiler, e.g. '-Xlint:unchecked'. |
| 274 | * |
| 275 | * @param options |
| 276 | * @return |
| 277 | */ |
| 278 | public InMemoryJavaCompiler useOptions(String... options) { |
| 279 | this.options = Arrays.asList(options); |
| 280 | return this; |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Ignore non-critical compiler output, like unchecked/unsafe operation |
| 285 | * warnings. |
| 286 | * |
| 287 | * @return |
| 288 | */ |
| 289 | public InMemoryJavaCompiler ignoreWarnings() { |
| 290 | ignoreWarnings = true; |
| 291 | return this; |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Compile all sources |
| 296 | * |
| 297 | * @return Map containing instances of all compiled classes |
| 298 | * @throws Exception |
| 299 | */ |
| 300 | public Map<String, Class<?>> compileAll() throws Exception { |
nothing calls this directly
no outgoing calls
no test coverage detected