Search in sources :

Example 1 with CustomJavaFileObject

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;
}
Also used : CustomJavaFileObject(com.dexels.navajo.compiler.tsl.custom.CustomJavaFileObject) JavaFileObject(javax.tools.JavaFileObject) CustomJavaFileObject(com.dexels.navajo.compiler.tsl.custom.CustomJavaFileObject)

Example 2 with CustomJavaFileObject

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();
}
Also used : Kind(javax.tools.JavaFileObject.Kind) Arrays(java.util.Arrays) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JavaFileManager(javax.tools.JavaFileManager) LoggerFactory(org.slf4j.LoggerFactory) ArrayList(java.util.ArrayList) CustomClassLoader(com.dexels.navajo.compiler.tsl.custom.CustomClassLoader) CompilationTask(javax.tools.JavaCompiler.CompilationTask) Locale(java.util.Locale) Map(java.util.Map) URI(java.net.URI) Hashtable(java.util.Hashtable) CustomClassloaderJavaFileManager(com.dexels.navajo.compiler.tsl.custom.CustomClassloaderJavaFileManager) ServiceRegistration(org.osgi.framework.ServiceRegistration) StandardLocation(javax.tools.StandardLocation) Logger(org.slf4j.Logger) JavaCompiler(javax.tools.JavaCompiler) EclipseCompiler(org.eclipse.jdt.internal.compiler.tool.EclipseCompiler) StringWriter(java.io.StringWriter) CustomJavaFileObject(com.dexels.navajo.compiler.tsl.custom.CustomJavaFileObject) OSGiJavaCompiler(com.dexels.osgicompiler.OSGiJavaCompiler) IOException(java.io.IOException) BundleContext(org.osgi.framework.BundleContext) JavaFileObject(javax.tools.JavaFileObject) StandardJavaFileManager(javax.tools.StandardJavaFileManager) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) Writer(java.io.Writer) Optional(java.util.Optional) DiagnosticListener(javax.tools.DiagnosticListener) Dictionary(java.util.Dictionary) CompilationException(com.dexels.navajo.script.api.CompilationException) InputStream(java.io.InputStream) CompilationException(com.dexels.navajo.script.api.CompilationException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CompilationTask(javax.tools.JavaCompiler.CompilationTask) CustomJavaFileObject(com.dexels.navajo.compiler.tsl.custom.CustomJavaFileObject) JavaFileObject(javax.tools.JavaFileObject) StringWriter(java.io.StringWriter) CustomJavaFileObject(com.dexels.navajo.compiler.tsl.custom.CustomJavaFileObject) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Aggregations

CustomJavaFileObject (com.dexels.navajo.compiler.tsl.custom.CustomJavaFileObject)2 JavaFileObject (javax.tools.JavaFileObject)2 CustomClassLoader (com.dexels.navajo.compiler.tsl.custom.CustomClassLoader)1 CustomClassloaderJavaFileManager (com.dexels.navajo.compiler.tsl.custom.CustomClassloaderJavaFileManager)1 CompilationException (com.dexels.navajo.script.api.CompilationException)1 OSGiJavaCompiler (com.dexels.osgicompiler.OSGiJavaCompiler)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Dictionary (java.util.Dictionary)1 Hashtable (java.util.Hashtable)1 List (java.util.List)1 Locale (java.util.Locale)1 Map (java.util.Map)1 Optional (java.util.Optional)1