use of com.dexels.navajo.compiler.tsl.custom.CustomJavaFileObject in project navajo by Dexels.
the class OSGiJavaCompilerImplementation method getJavaSourceFileObject.
private JavaFileObject getJavaSourceFileObject(String className, InputStream contents) throws IOException {
JavaFileObject so = null;
className = className.replace("\\", "/");
so = new CustomJavaFileObject(className + Kind.SOURCE.extension, URI.create("file:///" + className.replace('.', '/') + Kind.SOURCE.extension), contents, Kind.SOURCE);
return so;
}
use of com.dexels.navajo.compiler.tsl.custom.CustomJavaFileObject in project navajo by Dexels.
the class OSGiJavaCompilerImplementation method compile.
@Override
public byte[] compile(String className, InputStream source) throws IOException, CompilationException {
JavaFileObject javaSource = getJavaSourceFileObject(className, source);
Iterable<? extends JavaFileObject> fileObjects = Arrays.asList(javaSource);
final Writer sw = new StringWriter();
DiagnosticListener<JavaFileObject> compilerOutputListener = jfo -> {
try {
sw.write("Compilation problem. Line in .java file: " + jfo.getLineNumber() + ", error: " + jfo.getMessage(Locale.ENGLISH) + "\n");
} catch (IOException e) {
logger.error("Compilation problem: ", e);
}
};
StringWriter swe = new StringWriter();
List<String> options = new ArrayList<>();
options.add("-nowarn");
options.add("-target");
options.add("1.8");
CompilationTask task = compiler.getTask(swe, customJavaFileManager, compilerOutputListener, options, null, fileObjects);
boolean success = task.call();
if (!success) {
throw new CompilationException(sw.toString() + "\n" + swe.toString());
}
CustomJavaFileObject jfo = (CustomJavaFileObject) customJavaFileManager.getJavaFileForInput(StandardLocation.CLASS_OUTPUT, className, Kind.CLASS);
if (jfo == null) {
logger.error("Compilation failed: {} \n {}", sw, swe);
return null;
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
IOUtils.copy(jfo.openInputStream(), baos);
return baos.toByteArray();
}
Aggregations