Package com.sun.tools.javac.api

Examples of com.sun.tools.javac.api.JavacTool


public class T6410706 {
    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();

        // expect 2 notes:
View Full Code Here


            test(new File(testSrcDir, arg));
        }
    }

    void test(File test) throws Exception {
        JavacTool tool1 = JavacTool.create();
        StandardJavaFileManager fm = tool1.getStandardFileManager(null, null, null);
        Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(test);

        // parse test file into a tree, and write it out to a stringbuffer using Pretty
        JavacTask t1 = tool1.getTask(null, fm, null, null, null, files);
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        Iterable<? extends CompilationUnitTree> trees = t1.parse();
        for (CompilationUnitTree tree: trees) {
            new Pretty(pw, true).printExpr((JCTree) tree);
        }
        pw.close();

        final String out = sw.toString();
        System.err.println("generated code:\n" + out + "\n");

        // verify the generated code is valid Java by compiling it
        JavacTool tool2 = JavacTool.create();
        JavaFileObject fo = new SimpleJavaFileObject(URI.create("output"), JavaFileObject.Kind.SOURCE) {
            @Override
            public CharSequence getCharContent(boolean ignoreEncodingErrors) {
                return out;
            }
        };
        JavacTask t2 = tool2.getTask(null, fm, null, null, null, Collections.singleton(fo));
        boolean ok = t2.call();
        if (!ok)
            throw new Exception("compilation of generated code failed");

        File expectedClass = new File(test.getName().replace(".java", ".class"));
View Full Code Here

     * @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
     */
    List<Pair<JCCompilationUnit, JCTree>> read(File file) throws IOException, AttributionException {
        JavacTool tool = JavacTool.create();
        r.errors = 0;
        Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(file);
        String[] opts = { "-XDshouldStopPolicy=ATTR", "-XDverboseCompilePolicy" };
        JavacTask task = tool.getTask(pw, fm, r, Arrays.asList(opts), null, files);
        final List<Element> analyzedElems = new ArrayList<>();
        task.setTaskListener(new TaskListener() {
            public void started(TaskEvent e) {
                if (e.getKind() == TaskEvent.Kind.ANALYZE)
                        analyzedElems.add(e.getTypeElement());
View Full Code Here


public class T6345974 {
    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, "T6345974.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)
View Full Code Here

        new TestTrees().run();
    }

    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");
View Full Code Here

     */
    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);
View Full Code Here

    static final String testClassDir = System.getProperty("test.classes");
    static final String self = T6403466.class.getName();
    static PrintWriter out = new PrintWriter(System.err, true);

    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())
View Full Code Here

        File classesDir = createDir(testDir, "classes");

        File x = writeFile(new File(srcDir, "X.java"), src);

        DiagListener dl = new DiagListener();
        JavacTool tool = JavacTool.create();
        StandardJavaFileManager fm = tool.getStandardFileManager(dl, null, null);
        fm.setLocation(StandardLocation.CLASS_PATH,
                Arrays.asList(classesDir, new File(System.getProperty("test.classes"))));
        fm.setLocation(StandardLocation.CLASS_OUTPUT, Collections.singleton(classesDir));
        fm.setLocation(StandardLocation.SOURCE_OUTPUT, Collections.singleton(gensrcDir));
        List<String> args = new ArrayList<String>();
//        args.add("-XprintProcessorInfo");
        args.add("-XprintRounds");
        args.add("-Agen=" + gen);
        if (wk == WarningKind.YES)
            args.add("-Xlint:serial");
        Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(x);

        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        JavacTask task = tool.getTask(pw, fm, dl, args, null, files);
        task.setProcessors(Arrays.asList(new AnnoProc()));
        boolean ok = task.call();
        pw.close();

        System.err.println("ok:" + ok + " diags:" + dl.counts);
View Full Code Here

    }

    void run() throws Exception {
        File testSrc = new File(System.getProperty("test.src"));

        JavacTool tool = JavacTool.create();
        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);
View Full Code Here

        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, "
View Full Code Here

TOP

Related Classes of com.sun.tools.javac.api.JavacTool

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.