Search in sources :

Example 6 with JavacTask

use of com.sun.source.util.JavacTask in project ceylon-compiler by ceylon.

the class T6733837 method exec.

public void exec() {
    JavaFileObject sfo = new SimpleJavaFileObject(URI.create("myfo:/Test.java"), Kind.SOURCE) {

        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
            return "\tclass ErroneousWithTab";
        }
    };
    StringWriter sw = new StringWriter();
    PrintWriter out = new PrintWriter(sw);
    List<? extends JavaFileObject> files = Arrays.asList(sfo);
    task = tool.getTask(sw, fm, null, null, null, files);
    try {
        ((JavacTask) task).analyze();
    } catch (Throwable t) {
        throw new Error("Compiler threw an exception");
    }
    System.err.println(sw.toString());
    if (!sw.toString().contains("/Test.java"))
        throw new Error("Bad source name in diagnostic");
}
Also used : SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) JavaFileObject(javax.tools.JavaFileObject) SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) StringWriter(java.io.StringWriter) JavacTask(com.sun.source.util.JavacTask) PrintWriter(java.io.PrintWriter)

Example 7 with JavacTask

use of com.sun.source.util.JavacTask in project ceylon-compiler by ceylon.

the class T6852595 method main.

public static void main(String[] args) throws IOException {
    JavaFileObject sfo = new SimpleJavaFileObject(URI.create("myfo:/Test.java"), Kind.SOURCE) {

        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
            return "class BadName { Object o = j; }";
        }
    };
    List<? extends JavaFileObject> files = Arrays.asList(sfo);
    JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    JavacTask ct = (JavacTask) tool.getTask(null, null, null, null, null, files);
    Iterable<? extends CompilationUnitTree> compUnits = ct.parse();
    CompilationUnitTree cu = compUnits.iterator().next();
    ClassTree cdef = (ClassTree) cu.getTypeDecls().get(0);
    JCVariableDecl vdef = (JCVariableDecl) cdef.getMembers().get(0);
    TreePath path = TreePath.getPath(cu, vdef.init);
    Trees.instance(ct).getScope(path);
}
Also used : SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) JavaFileObject(javax.tools.JavaFileObject) TreePath(com.sun.source.util.TreePath) JavaCompiler(javax.tools.JavaCompiler) JavacTask(com.sun.source.util.JavacTask)

Example 8 with JavacTask

use of com.sun.source.util.JavacTask in project ceylon-compiler by ceylon.

the class GenStubs method run.

boolean run(String sourcepath, File outdir, List<String> classes) {
    //System.err.println("run: sourcepath:" + sourcepath + " outdir:" + outdir + " classes:" + classes);
    if (sourcepath == null)
        throw new IllegalArgumentException("sourcepath not set");
    if (outdir == null)
        throw new IllegalArgumentException("source output dir not set");
    JavacTool tool = JavacTool.create();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    try {
        fm.setLocation(StandardLocation.SOURCE_OUTPUT, Collections.singleton(outdir));
        fm.setLocation(StandardLocation.SOURCE_PATH, splitPath(sourcepath));
        List<JavaFileObject> files = new ArrayList<JavaFileObject>();
        for (String c : classes) {
            JavaFileObject fo = fm.getJavaFileForInput(StandardLocation.SOURCE_PATH, c, JavaFileObject.Kind.SOURCE);
            if (fo == null)
                error("class not found: " + c);
            else
                files.add(fo);
        }
        JavacTask t = tool.getTask(null, fm, null, null, null, files);
        Iterable<? extends CompilationUnitTree> trees = t.parse();
        for (CompilationUnitTree tree : trees) {
            makeStub(fm, tree);
        }
    } catch (IOException e) {
        error("IO error " + e, e);
    }
    return (errors == 0);
}
Also used : JavaFileObject(javax.tools.JavaFileObject) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) JavacTool(com.sun.tools.javac.api.JavacTool) StandardJavaFileManager(javax.tools.StandardJavaFileManager) JavacTask(com.sun.source.util.JavacTask)

Example 9 with JavacTask

use of com.sun.source.util.JavacTask 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)

Example 10 with JavacTask

use of com.sun.source.util.JavacTask in project ceylon-compiler by ceylon.

the class Foo method main.

public static void main(String... args) throws IOException {
    String testSrc = System.getProperty("test.src", ".");
    String testClasses = System.getProperty("test.classes", ".");
    JavacTool tool = JavacTool.create();
    MyDiagListener dl = new MyDiagListener();
    StandardJavaFileManager fm = tool.getStandardFileManager(dl, null, null);
    fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(new File(testClasses)));
    Iterable<? extends JavaFileObject> files = fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, T6410706.class.getName() + ".java")));
    JavacTask task = tool.getTask(null, fm, dl, null, null, files);
    task.parse();
    task.analyze();
    task.generate();
    if (dl.notes != 2)
        throw new AssertionError(dl.notes + " notes given");
}
Also used : JavacTool(com.sun.tools.javac.api.JavacTool) StandardJavaFileManager(javax.tools.StandardJavaFileManager) JavacTask(com.sun.source.util.JavacTask) File(java.io.File)

Aggregations

JavacTask (com.sun.source.util.JavacTask)46 JavaCompiler (javax.tools.JavaCompiler)20 JavacTool (com.sun.tools.javac.api.JavacTool)12 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)10 JavaFileObject (javax.tools.JavaFileObject)9 StandardJavaFileManager (javax.tools.StandardJavaFileManager)7 SimpleJavaFileObject (javax.tools.SimpleJavaFileObject)6 Context (com.sun.tools.javac.util.Context)5 File (java.io.File)5 IOException (java.io.IOException)5 PrintWriter (java.io.PrintWriter)5 StringWriter (java.io.StringWriter)5 JavacFileManager (com.sun.tools.javac.file.JavacFileManager)4 ClassTree (com.sun.source.tree.ClassTree)3 Trees (com.sun.source.util.Trees)3 JCCompilationUnit (com.sun.tools.javac.tree.JCTree.JCCompilationUnit)3 Path (java.nio.file.Path)3 ArrayList (java.util.ArrayList)3 CeyloncTool (com.redhat.ceylon.compiler.java.tools.CeyloncTool)2 Tree (com.sun.source.tree.Tree)2