Search in sources :

Example 26 with CompilationTask

use of javax.tools.JavaCompiler.CompilationTask in project otter by alibaba.

the class JdkCompileTask method compile.

public synchronized Map<String, Class> compile(final Map<String, CharSequence> classes, final DiagnosticCollector<JavaFileObject> diagnosticsList) throws JdkCompileException {
    Map<String, Class> compiled = new HashMap<String, Class>();
    List<JavaFileObject> sources = new ArrayList<JavaFileObject>();
    for (Entry<String, CharSequence> entry : classes.entrySet()) {
        String qualifiedClassName = entry.getKey();
        CharSequence javaSource = entry.getValue();
        if (javaSource != null) {
            final int dotPos = qualifiedClassName.lastIndexOf('.');
            final String className = dotPos == -1 ? qualifiedClassName : qualifiedClassName.substring(dotPos + 1);
            final String packageName = dotPos == -1 ? "" : qualifiedClassName.substring(0, dotPos);
            final JavaFileObjectImpl source = new JavaFileObjectImpl(className, javaSource);
            sources.add(source);
            javaFileManager.putFileForInput(StandardLocation.SOURCE_PATH, packageName, className + JAVA_EXTENSION, source);
        }
    }
    // Get a CompliationTask from the compiler and compile the sources
    final CompilationTask task = compiler.getTask(null, javaFileManager, diagnostics, options, null, sources);
    final Boolean result = task.call();
    if (result == null || !result.booleanValue()) {
        throw new JdkCompileException("Compilation failed.", classes.keySet(), diagnostics);
    }
    try {
        // put it in the output map
        for (String qualifiedClassName : classes.keySet()) {
            final Class<T> newClass = loadClass(qualifiedClassName);
            compiled.put(qualifiedClassName, (Class<?>) newClass);
        }
        return compiled;
    } catch (ClassNotFoundException e) {
        throw new JdkCompileException(classes.keySet(), e, diagnostics);
    } catch (IllegalArgumentException e) {
        throw new JdkCompileException(classes.keySet(), e, diagnostics);
    } catch (SecurityException e) {
        throw new JdkCompileException(classes.keySet(), e, diagnostics);
    }
}
Also used : HashMap(java.util.HashMap) JavaFileObjectImpl(com.alibaba.otter.shared.common.utils.compile.model.JavaFileObjectImpl) ArrayList(java.util.ArrayList) CompilationTask(javax.tools.JavaCompiler.CompilationTask) JdkCompileException(com.alibaba.otter.shared.common.utils.compile.exception.JdkCompileException) JavaFileObject(javax.tools.JavaFileObject)

Example 27 with CompilationTask

use of javax.tools.JavaCompiler.CompilationTask in project ceylon-compiler by ceylon.

the class JavahTask method run.

public boolean run() throws Util.Exit {
    Util util = new Util(log, diagnosticListener);
    if (noArgs || help) {
        showHelp();
        // treat noArgs as an error for purposes of exit code
        return help;
    }
    if (version || fullVersion) {
        showVersion(fullVersion);
        return true;
    }
    util.verbose = verbose;
    Gen g;
    if (llni)
        g = new LLNI(doubleAlign, util);
    else {
        // if (stubs)
        // throw new BadArgs("jni.no.stubs");
        g = new JNI(util);
    }
    if (ofile != null) {
        if (!(fileManager instanceof StandardJavaFileManager)) {
            diagnosticListener.report(createDiagnostic("err.cant.use.option.for.fm", "-o"));
            return false;
        }
        Iterable<? extends JavaFileObject> iter = ((StandardJavaFileManager) fileManager).getJavaFileObjectsFromFiles(Collections.singleton(ofile));
        JavaFileObject fo = iter.iterator().next();
        g.setOutFile(fo);
    } else {
        if (odir != null) {
            if (!(fileManager instanceof StandardJavaFileManager)) {
                diagnosticListener.report(createDiagnostic("err.cant.use.option.for.fm", "-d"));
                return false;
            }
            if (!odir.exists())
                if (!odir.mkdirs())
                    util.error("cant.create.dir", odir.toString());
            try {
                ((StandardJavaFileManager) fileManager).setLocation(StandardLocation.CLASS_OUTPUT, Collections.singleton(odir));
            } catch (IOException e) {
                Object msg = e.getLocalizedMessage();
                if (msg == null) {
                    msg = e;
                }
                diagnosticListener.report(createDiagnostic("err.ioerror", odir, msg));
                return false;
            }
        }
        g.setFileManager(fileManager);
    }
    /*
         * Force set to false will turn off smarts about checking file
         * content before writing.
         */
    g.setForce(force);
    if (fileManager instanceof JavahFileManager)
        ((JavahFileManager) fileManager).setIgnoreSymbolFile(true);
    JavaCompiler c = ToolProvider.getSystemJavaCompiler();
    List<String> opts = new ArrayList<String>();
    opts.add("-proc:only");
    opts.addAll(javac_extras);
    CompilationTask t = c.getTask(log, fileManager, diagnosticListener, opts, internalize(classes), null);
    JavahProcessor p = new JavahProcessor(g);
    t.setProcessors(Collections.singleton(p));
    boolean ok = t.call();
    if (p.exit != null)
        throw new Util.Exit(p.exit);
    return ok;
}
Also used : ArrayList(java.util.ArrayList) JavaCompiler(javax.tools.JavaCompiler) IOException(java.io.IOException) CompilationTask(javax.tools.JavaCompiler.CompilationTask) JavaFileObject(javax.tools.JavaFileObject) StandardJavaFileManager(javax.tools.StandardJavaFileManager) JavaFileObject(javax.tools.JavaFileObject)

Example 28 with CompilationTask

use of javax.tools.JavaCompiler.CompilationTask in project ceylon-compiler by ceylon.

the class TestJavacTask_Multiple method test.

void test(JavaCompiler comp, StandardJavaFileManager fm, TestKind tk) {
    System.err.println("test " + tk);
    File testSrc = new File(System.getProperty("test.src"));
    String thisClassName = TestJavacTask_Multiple.class.getName();
    Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(new File(testSrc, thisClassName + ".java"));
    List<CompilationTask> tasks = new ArrayList<CompilationTask>();
    for (int i = 1; i <= MAX_TASKS; i++) {
        File tmpDir = new File(tk + "_" + i);
        tmpDir.mkdirs();
        List<String> options = Arrays.asList("-d", tmpDir.getPath());
        CompilationTask t = comp.getTask(null, fm, null, options, null, files);
        ((JavacTask) t).setTaskListener(createTaskListener(tk, i));
        tasks.add(t);
    }
    for (CompilationTask t : tasks) count += tk.test(t);
    System.err.println();
}
Also used : CompilationTask(javax.tools.JavaCompiler.CompilationTask)

Example 29 with CompilationTask

use of javax.tools.JavaCompiler.CompilationTask in project ceylon-compiler by ceylon.

the class TestJavacTask_Lock method test.

void test(MethodKind first, MethodKind second) {
    System.err.println("test: " + first + ", " + second);
    File testSrc = new File(System.getProperty("test.src"));
    String thisClassName = TestJavacTask_Lock.class.getName();
    Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(new File(testSrc, thisClassName + ".java"));
    File tmpDir = new File(first + "_" + second);
    tmpDir.mkdirs();
    List<String> options = Arrays.asList("-d", tmpDir.getPath());
    CompilationTask t = comp.getTask(null, fm, null, options, null, files);
    try {
        first.test(t);
        second.test(t);
        error("No exception thrown");
    } catch (IllegalStateException e) {
        System.err.println("Expected exception caught: " + e);
    } catch (Exception e) {
        error("Unexpected exception caught: " + e);
        e.printStackTrace(System.err);
    }
}
Also used : CompilationTask(javax.tools.JavaCompiler.CompilationTask)

Example 30 with CompilationTask

use of javax.tools.JavaCompiler.CompilationTask in project ceylon-compiler by ceylon.

the class T7031108 method compile.

void compile(JavaCompiler comp, JavaFileManager fm, DiagnosticListener<JavaFileObject> dl, String processor, JavaFileObject... files) throws Exception {
    System.err.println("compile processor:" + processor + ", files:" + Arrays.asList(files));
    List<String> opts = new ArrayList<String>();
    if (processor != null) {
        // opts.add("-verbose");
        opts.addAll(Arrays.asList("-processor", processor));
    }
    CompilationTask task = comp.getTask(null, fm, dl, opts, null, Arrays.asList(files));
    boolean ok = task.call();
    if (dl == null && !ok)
        throw new Exception("compilation failed");
}
Also used : CompilationTask(javax.tools.JavaCompiler.CompilationTask)

Aggregations

CompilationTask (javax.tools.JavaCompiler.CompilationTask)84 JavaCompiler (javax.tools.JavaCompiler)38 File (java.io.File)33 JavaFileObject (javax.tools.JavaFileObject)32 StandardJavaFileManager (javax.tools.StandardJavaFileManager)28 DiagnosticCollector (javax.tools.DiagnosticCollector)27 IOException (java.io.IOException)20 ArrayList (java.util.ArrayList)18 SimpleJavaFileObject (javax.tools.SimpleJavaFileObject)14 URLClassLoader (java.net.URLClassLoader)10 StringWriter (java.io.StringWriter)9 URL (java.net.URL)6 JavaFileManager (javax.tools.JavaFileManager)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 Test (org.junit.Test)5 OutputStream (java.io.OutputStream)4 PrintWriter (java.io.PrintWriter)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 FileInputStream (java.io.FileInputStream)3 FileOutputStream (java.io.FileOutputStream)3