Java Code Examples for com.sun.tools.javac.api.JavacTool#create()

The following examples show how to use com.sun.tools.javac.api.JavacTool#create() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestDocComments.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * method-run.
 */
void run() throws Exception {
    File testSrc = new File(System.getProperty("test.src"));
    File file = new File(testSrc, "TestDocComments.java");

    JavacTool tool = JavacTool.create();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);

    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    Iterable<? extends JavaFileObject> fileObjects = fm.getJavaFileObjects(file);
    JavacTask task = tool.getTask(pw, fm, null, null, null, fileObjects);
    Iterable<? extends CompilationUnitTree> units = task.parse();
    Trees trees = Trees.instance(task);

    CommentScanner s = new CommentScanner();
    int n = s.scan(units, trees);

    if (n != 12)
        error("Unexpected number of doc comments found: " + n);

    if (errors > 0)
        throw new Exception(errors + " errors occurred");
}
 
Example 2
Source File: NativeHeaderTest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/** Combo test to run all test cases in all modes. */
void run() throws Exception {
    javac = JavacTool.create();
    fm = javac.getStandardFileManager(null, null, null);

    for (RunKind rk: RunKind.values()) {
        for (GenKind gk: GenKind.values()) {
            for (Method m: getClass().getDeclaredMethods()) {
                Annotation a = m.getAnnotation(Test.class);
                if (a != null) {
                    init(rk, gk, m.getName());
                    try {
                        m.invoke(this, new Object[] { rk, gk });
                    } catch (InvocationTargetException e) {
                        Throwable cause = e.getCause();
                        throw (cause instanceof Exception) ? ((Exception) cause) : e;
                    }
                    System.err.println();
                }
            }
        }
    }
    System.err.println(testCount + " tests" + ((errorCount == 0) ? "" : ", " + errorCount + " errors"));
    if (errorCount > 0)
        throw new Exception(errorCount + " errors found");
}
 
Example 3
Source File: ArrayCreationTree.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    PrintWriter out = new PrintWriter(System.out, true);
    JavacTool tool = JavacTool.create();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    File testSrc = new File(System.getProperty("test.src"));
    Iterable<? extends JavaFileObject> f =
        fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, "ArrayCreationTree.java")));
    JavacTask task = tool.getTask(out, fm, null, null, null, f);
    Iterable<? extends CompilationUnitTree> trees = task.parse();
    out.flush();

    Scanner s = new Scanner();
    for (CompilationUnitTree t: trees)
        s.scan(t, null);

}
 
Example 4
Source File: T6403466.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
    JavacTool tool = JavacTool.create();

    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    Iterable<? extends JavaFileObject> files =
        fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrcDir, self + ".java")));

    Iterable<String> options = Arrays.asList("-processorpath", testClassDir,
                                             "-processor", self,
                                             "-s", ".",
                                             "-d", ".");
    JavacTask task = tool.getTask(out, fm, null, options, null, files);

    VerifyingTaskListener vtl = new VerifyingTaskListener(new File(testSrcDir, self + ".out"));
    task.setTaskListener(vtl);

    if (!task.call())
        throw new AssertionError("compilation failed");

    if (vtl.iter.hasNext() || vtl.errors)
        throw new AssertionError("comparison against golden file failed.");
}
 
Example 5
Source File: T6403466.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
    JavacTool tool = JavacTool.create();

    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    Iterable<? extends JavaFileObject> files =
        fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrcDir, self + ".java")));

    Iterable<String> options = Arrays.asList("-processorpath", testClassDir,
                                             "-processor", self,
                                             "-s", ".",
                                             "-d", ".");
    JavacTask task = tool.getTask(out, fm, null, options, null, files);

    VerifyingTaskListener vtl = new VerifyingTaskListener(new File(testSrcDir, self + ".out"));
    task.setTaskListener(vtl);

    if (!task.call())
        throw new AssertionError("compilation failed");

    if (vtl.iter.hasNext() || vtl.errors)
        throw new AssertionError("comparison against golden file failed.");
}
 
Example 6
Source File: AbstractTreeScannerTest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Read a file.
 * @param file the file to be read
 * @return the tree for the content of the file
 * @throws IOException if any IO errors occur
 * @throws TreePosTest.ParseException if any errors occur while parsing the file
 */
JCCompilationUnit read(File file) throws IOException, ParseException {
    JavacTool tool = JavacTool.create();
    r.errors = 0;
    Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(file);
    JavacTask task = tool.getTask(pw, fm, r, Collections.<String>emptyList(), null, files);
    Iterable<? extends CompilationUnitTree> trees = task.parse();
    pw.flush();
    if (r.errors > 0)
        throw new ParseException(sw.toString());
    Iterator<? extends CompilationUnitTree> iter = trees.iterator();
    if (!iter.hasNext())
        throw new Error("no trees found");
    JCCompilationUnit t = (JCCompilationUnit) iter.next();
    if (iter.hasNext())
        throw new Error("too many trees found");
    return t;
}
 
Example 7
Source File: T6993305.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
void run() throws Exception {
    File testSrc = new File(System.getProperty("test.src"));

    JavacTool tool = JavacTool.create();
    try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {

        File f = new File(testSrc, T6993305.class.getSimpleName() + ".java");
        Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjects(f);
        JavacTask task = tool.getTask(null, fm, null, null, null, fos);
        Iterable<? extends CompilationUnitTree> cus = task.parse();

        TestScanner s = new TestScanner();
        s.scan(cus, task);

        if (errors > 0)
            throw new Exception(errors + " errors occurred");
    }
}
 
Example 8
Source File: AbstractTreeScannerTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Read a file.
 * @param file the file to be read
 * @return the tree for the content of the file
 * @throws IOException if any IO errors occur
 * @throws TreePosTest.ParseException if any errors occur while parsing the file
 */
JCCompilationUnit read(File file) throws IOException, ParseException {
    JavacTool tool = JavacTool.create();
    r.errors = 0;
    Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(file);
    JavacTask task = tool.getTask(pw, fm, r, Collections.<String>emptyList(), null, files);
    Iterable<? extends CompilationUnitTree> trees = task.parse();
    pw.flush();
    if (r.errors > 0)
        throw new ParseException(sw.toString());
    Iterator<? extends CompilationUnitTree> iter = trees.iterator();
    if (!iter.hasNext())
        throw new Error("no trees found");
    JCCompilationUnit t = (JCCompilationUnit) iter.next();
    if (iter.hasNext())
        throw new Error("too many trees found");
    return t;
}
 
Example 9
Source File: NativeHeaderTest.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/** Combo test to run all test cases in all modes. */
void run() throws Exception {
    javac = JavacTool.create();
    fm = javac.getStandardFileManager(null, null, null);

    for (RunKind rk: RunKind.values()) {
        for (GenKind gk: GenKind.values()) {
            for (Method m: getClass().getDeclaredMethods()) {
                Annotation a = m.getAnnotation(Test.class);
                if (a != null) {
                    init(rk, gk, m.getName());
                    try {
                        m.invoke(this, new Object[] { rk, gk });
                    } catch (InvocationTargetException e) {
                        Throwable cause = e.getCause();
                        throw (cause instanceof Exception) ? ((Exception) cause) : e;
                    }
                    System.err.println();
                }
            }
        }
    }
    System.err.println(testCount + " tests" + ((errorCount == 0) ? "" : ", " + errorCount + " errors"));
    if (errorCount > 0)
        throw new Exception(errorCount + " errors found");
}
 
Example 10
Source File: DocTreePathScannerTest.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
void run() throws Exception {
    List<File> files = new ArrayList<File>();
    File testSrc = new File(System.getProperty("test.src"));
    for (File f: testSrc.listFiles()) {
        if (f.isFile() && f.getName().endsWith(".java"))
            files.add(f);
    }

    JavacTool javac = JavacTool.create();
    StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null);

    Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(files);

    JavacTask t = javac.getTask(null, fm, null, null, null, fos);
    DocTrees trees = DocTrees.instance(t);

    Iterable<? extends CompilationUnitTree> units = t.parse();

    DeclScanner ds = new DeclScanner(trees);
    for (CompilationUnitTree unit: units) {
        ds.scan(unit, null);
    }

    if (errors > 0)
        throw new Exception(errors + " errors occurred");
}
 
Example 11
Source File: TreePosTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Read a file.
 * @param file the file to be read
 * @return the tree for the content of the file
 * @throws IOException if any IO errors occur
 * @throws TreePosTest.ParseException if any errors occur while parsing the file
 */
JCCompilationUnit read(File file) throws IOException, ParseException {
    JavacTool tool = JavacTool.create();
    r.errors = 0;
    Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(file);
    JavacTask task = tool.getTask(pw, fm, r, Collections.<String>emptyList(), null, files);
    Iterable<? extends CompilationUnitTree> trees = task.parse();
    pw.flush();
    if (r.errors > 0)
        throw new ParseException(sw.toString());
    Iterator<? extends CompilationUnitTree> iter = trees.iterator();
    if (!iter.hasNext())
        throw new Error("no trees found");
    JCCompilationUnit t = (JCCompilationUnit) iter.next();
    if (iter.hasNext())
        throw new Error("too many trees found");
    return t;
}
 
Example 12
Source File: TreePosTest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Read a file.
 * @param file the file to be read
 * @return the tree for the content of the file
 * @throws IOException if any IO errors occur
 * @throws TreePosTest.ParseException if any errors occur while parsing the file
 */
JCCompilationUnit read(File file) throws IOException, ParseException {
    JavacTool tool = JavacTool.create();
    r.errors = 0;
    Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(file);
    JavacTask task = tool.getTask(pw, fm, r, Collections.<String>emptyList(), null, files);
    Iterable<? extends CompilationUnitTree> trees = task.parse();
    pw.flush();
    if (r.errors > 0)
        throw new ParseException(sw.toString());
    Iterator<? extends CompilationUnitTree> iter = trees.iterator();
    if (!iter.hasNext())
        throw new Error("no trees found");
    JCCompilationUnit t = (JCCompilationUnit) iter.next();
    if (iter.hasNext())
        throw new Error("too many trees found");
    return t;
}
 
Example 13
Source File: T6358168.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
static void testAnnotationProcessing(JavacFileManager fm, List<JavaFileObject> files) throws Throwable {
    Context context = new Context();

    String[] args = {
            "-XprintRounds",
            "-processorpath", testClasses,
            "-processor", self,
            "-d", "."
    };

    JavacTool tool = JavacTool.create();
    JavacTask task = tool.getTask(null, fm, null, List.from(args), null, files, context);
    // no need in this simple case to call task.prepareCompiler(false)

    JavaCompiler compiler = JavaCompiler.instance(context);
    compiler.compile(files);
    try {
        compiler.compile(files);
        throw new Error("Error: AssertionError not thrown after second call of compile");
    } catch (AssertionError e) {
        System.err.println("Exception from compiler (expected): " + e);
    }
}
 
Example 14
Source File: TestTrees.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
void run() throws IOException {

        JavacTool tool = JavacTool.create();

        DiagnosticListener<JavaFileObject> dl = new DiagnosticListener<JavaFileObject>() {
                public void report(Diagnostic d) {
                    error(d.toString());
                }
            };

        StandardJavaFileManager fm = tool.getStandardFileManager(dl, null, null);
        Iterable<? extends JavaFileObject> files =
            fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrcDir, self + ".java")));

        Iterable<String> opts = Arrays.asList("-d", ".");

        System.err.println("simple compilation, no processing");
        JavacTask task = tool.getTask(out, fm, dl, opts, null, files);
        task.setTaskListener(new MyTaskListener(task));
        if (!task.call())
            throw new AssertionError("compilation failed");

        opts =  Arrays.asList("-d", ".", "-processorpath", testClassDir, "-processor", self);

        System.err.println();
        System.err.println("compilation with processing");
        task = tool.getTask(out, fm, dl,opts, null, files);
        if (!task.call())
            throw new AssertionError("compilation failed");

        if (errors > 0)
            throw new AssertionError(errors + " errors occurred");
    }
 
Example 15
Source File: GenStubs.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public 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);
}
 
Example 16
Source File: Test.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/** Doc comment: run */
void run() throws Exception {
    File testSrc = new File(System.getProperty("test.src"));
    File thisFile = new File(testSrc, getClass().getName() + ".java");
    JavacTool javac = JavacTool.create();
    StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null);
    fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(new File(".")));
    Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjects(thisFile);
    testAnnoProcessor(javac, fm, fos, out, EXPECT_DOC_COMMENTS);
    testTaskListener(javac, fm, fos, out, EXPECT_DOC_COMMENTS);

    if (errors > 0)
        throw new Exception(errors + " errors occurred");
}
 
Example 17
Source File: T6397044.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    String srcDir = System.getProperty("test.src", ".");
    String self = T6397044.class.getName();
    JavacTool tool = JavacTool.create();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    Iterable<? extends JavaFileObject> files
        = fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(srcDir, self + ".java")));
    JavacTask task = tool.getTask(null, fm, null, null, null, files);
    Iterable<? extends CompilationUnitTree> trees = task.parse();
    Checker checker = new Checker();
    for (CompilationUnitTree tree: trees)
        checker.check(tree);
}
 
Example 18
Source File: RunTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
void testInit(boolean expectOK, String[] args, List<? extends JavaFileObject> files) {
    JavacTool javac = JavacTool.create();
    JavacTask task = javac.getTask(null, null, null, null, null, files);
    try {
        DocLint dl = new DocLint();
        dl.init(task, args, true);
        if (!expectOK)
            error("expected IllegalArgumentException not thrown");
        task.call();
    } catch (IllegalArgumentException e) {
        System.err.println(e);
        if (expectOK)
            error("unexpected IllegalArgumentException caught");
    }
}
 
Example 19
Source File: SimpleDocTreeVisitorTest.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
void run() throws Exception {
    List<File> files = new ArrayList<File>();
    File testSrc = new File(System.getProperty("test.src"));
    for (File f: testSrc.listFiles()) {
        if (f.isFile() && f.getName().endsWith(".java"))
            files.add(f);
    }

    JavacTool javac = JavacTool.create();
    StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null);

    Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(files);

    JavacTask t = javac.getTask(null, fm, null, null, null, fos);
    DocTrees trees = DocTrees.instance(t);

    Iterable<? extends CompilationUnitTree> units = t.parse();

    Set<DocTree.Kind> found = EnumSet.noneOf(DocTree.Kind.class);
    DeclScanner ds = new DeclScanner(trees, found);
    for (CompilationUnitTree unit: units) {
        ds.scan(unit, null);
    }

    for (DocTree.Kind k: DocTree.Kind.values()) {
        if (!found.contains(k) && k != DocTree.Kind.OTHER)
            error("not found: " + k);
    }

    if (errors > 0)
        throw new Exception(errors + " errors occurred");
}
 
Example 20
Source File: T6348193.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public static void test(NoYes secMgr, NoGoodBad config, NoYes proc) throws IOException {
    if (verbose)
        System.err.println("secMgr:" + secMgr + " config:" + config + " proc:" + proc);

    if (secMgr == NoYes.YES && System.getSecurityManager() == null)
        System.setSecurityManager(new NoLoaderSecurityManager());

    installConfigFile(config);

    processed.delete();

    List<String> args = new ArrayList<String>();
    //args.add("-XprintRounds");
    if (proc == NoYes.YES) {
        args.add("-processor");
        args.add(myName);
    }
    args.add("-processorpath");
    args.add(System.getProperty("java.class.path"));
    args.add("-d");
    args.add(".");

    JavacTool t = JavacTool.create(); // avoid using class loader

    MyDiagListener dl = new MyDiagListener();
    PrintWriter out = new PrintWriter(System.err, true);
    StandardJavaFileManager fm = t.getStandardFileManager(dl, null, null);
    File file = new File(System.getProperty("test.src"), myName+".java");
    Iterable<? extends JavaFileObject> files =
        fm.getJavaFileObjectsFromFiles(Arrays.asList(file));
    boolean ok = t.getTask(out, null, dl, args, null, files).call();

    if (config == NoGoodBad.GOOD || proc == NoYes.YES) {
        if (secMgr == NoYes.YES) {
            if (dl.last == null)
                throw new AssertionError("Security manager installed, and processors present, "
                                         + " but no diagnostic received");
        }
        else {
            if (!processed.exists())
                throw new AssertionError("No security manager installed, and processors present, "
                                         + " but no processing occurred");
        }
    }
    else if (config == NoGoodBad.BAD) {
        // TODO: should verify that no compiler crash occurred
        // needs revised JSR199 spec
    }
    else {
        if (processed.exists())
            throw new AssertionError("No processors present, but processing occurred!");
    }

    if (verbose)
        System.err.println("OK");
}