compile
Compile from memory/filesystem to memory/filesystem, easily
io.earcam.instrumental.compile.Compiler
- Convenience functionality on top of javax.tools.JavaCompiler
- Compile to/from the file-system, or entirely in-memory (e.g. from source as String to class as byte array)
- Fluid builder with specific support for annotation processors
- Create native headers in arbitrary locations (in-mem/on-disk)
- Find sourcecode for classes within maven module or it’s dependencies (on proviso that source jar has been downloaded)
Provides a complete in-memory experience:
- Using instrumental.archive, via instrumental.compile.glue, compiler output can be added to an in-memory archive
- By using instrumental.archive.glue in-memory archives may be used as the source of compiler dependencies (coupled with instrumental.archive.maven to download Maven dependency graphs as compilation deps).
Note: currently the Eclipse compiler only supports input from the filesystem.
Examples
Take the source as a String, then compile it to a map of byte arrays:
final String fqn = "com.acme.Thing"; final String text = "" + "package com.acme; \n" + " \n" + "class Thing { \n" + " int fortyTwo() \n" + " { \n" + " return 42; \n" + " } \n" + "} \n"; @Test void map() { Map<String, byte[]> map; map = compiling() .versionAt(latestSupported()) .source(from(text)) .compile(toByteArrays()); byte[] bytes = map.get(Names.typeToResourceName(fqn)); assertValidClass(bytes, fqn); }
Find the source for the specified class, then compile it the filesystem:
compiling() .versionAt(SourceVersion.latestSupported()) .source(foundFor(Pojo.class)) .compile(toFileSystem(path)); Path compiled = path.resolve(Names.typeToResourceName(Pojo.class)); byte[] bytes = Files.readAllBytes(compiled); assertValidClass(bytes, Pojo.class.getCanonicalName());
(source).