Search in sources :

Example 36 with JavaCompiler

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

the class T6557752 method main.

public static void main(String[] args) throws IOException {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    task = (JavacTask) compiler.getTask(null, null, null, null, null, List.of(new MyFileObject()));
    Iterable<? extends CompilationUnitTree> asts = task.parse();
    task.analyze();
    trees = Trees.instance(task);
    MyVisitor myVisitor = new MyVisitor();
    for (CompilationUnitTree ast : asts) {
        myVisitor.compilationUnit = ast;
        myVisitor.scan(ast, null);
    }
    if (!myVisitor.foundError) {
        throw new AssertionError("Expected error not found!");
    }
}
Also used : CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) JavaCompiler(javax.tools.JavaCompiler)

Example 37 with JavaCompiler

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

the class T6550655 method main.

public static void main(String[] args) throws Exception {
    String SCRATCH_DIR = System.getProperty("user.dir");
    JavaCompiler javacTool = ToolProvider.getSystemJavaCompiler();
    int n = 0;
    for (TestKind testKind : TestKind.values()) {
        for (EnumActionKind actionKind : EnumActionKind.values()) {
            File testDir = new File(SCRATCH_DIR, "test" + n);
            new T6550655(javacTool, testDir, testKind, actionKind).test();
            n++;
        }
    }
    if (nerrors > 0) {
        throw new AssertionError("Some errors have been detected");
    }
}
Also used : JavaCompiler(javax.tools.JavaCompiler) File(java.io.File)

Example 38 with JavaCompiler

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

the class Launcher method main.

public static void main(String... args) {
    JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
    JFileChooser fileChooser;
    Preferences prefs = Preferences.userNodeForPackage(Launcher.class);
    if (args.length > 0)
        fileChooser = new JFileChooser(args[0]);
    else {
        String fileName = prefs.get("recent.file", null);
        fileChooser = new JFileChooser();
        if (fileName != null) {
            fileChooser = new JFileChooser();
            fileChooser.setSelectedFile(new File(fileName));
        }
    }
    if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
        String fileName = fileChooser.getSelectedFile().getPath();
        prefs.put("recent.file", fileName);
        javac.run(System.in, null, null, "-d", "/tmp", fileName);
    }
}
Also used : JFileChooser(javax.swing.JFileChooser) JavaCompiler(javax.tools.JavaCompiler) Preferences(java.util.prefs.Preferences) File(java.io.File)

Example 39 with JavaCompiler

use of javax.tools.JavaCompiler 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 40 with JavaCompiler

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

the class TestCircularClassfile method check.

static void check(String destPath, ClassName clazz, ClassName sup) throws Exception {
    File destDir = new File(workDir, destPath);
    destDir.mkdir();
    final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    JavacTask ct = (JavacTask) tool.getTask(null, null, null, Arrays.asList("-d", destPath), null, Arrays.asList(initialSources));
    ct.generate();
    File fileToRemove = new File(destPath, clazz.name + ".class");
    fileToRemove.delete();
    JavaSource newSource = new JavaSource(clazz, sup);
    DiagnosticChecker checker = new DiagnosticChecker();
    ct = (JavacTask) tool.getTask(null, null, checker, Arrays.asList("-cp", destPath), null, Arrays.asList(newSource));
    ct.analyze();
    if (!checker.errorFound) {
        throw new AssertionError(newSource.source);
    }
}
Also used : JavaCompiler(javax.tools.JavaCompiler) JavacTask(com.sun.source.util.JavacTask)

Aggregations

JavaCompiler (javax.tools.JavaCompiler)101 StandardJavaFileManager (javax.tools.StandardJavaFileManager)43 JavaFileObject (javax.tools.JavaFileObject)39 File (java.io.File)31 DiagnosticCollector (javax.tools.DiagnosticCollector)24 ArrayList (java.util.ArrayList)22 JavacTask (com.sun.source.util.JavacTask)20 IOException (java.io.IOException)17 CompilationTask (javax.tools.JavaCompiler.CompilationTask)16 SimpleJavaFileObject (javax.tools.SimpleJavaFileObject)15 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)11 Diagnostic (javax.tools.Diagnostic)11 PrintWriter (java.io.PrintWriter)9 Test (org.junit.Test)9 JavacTaskImpl (com.sun.tools.javac.api.JavacTaskImpl)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 FileOutputStream (java.io.FileOutputStream)5 StringWriter (java.io.StringWriter)5 JavaFileManager (javax.tools.JavaFileManager)5 OutputStream (java.io.OutputStream)4