Package com.sun.source.util

Examples of com.sun.source.util.JavacTask


    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());
            }
            public void finished(TaskEvent e) { }
        });

        try {
            Iterable<? extends CompilationUnitTree> trees = task.parse();
            task.analyze();
            List<Pair<JCCompilationUnit, JCTree>> res = new ArrayList<>();
            //System.out.println("Try to add pairs. Elems are " + analyzedElems);
            for (CompilationUnitTree t : trees) {
               JCCompilationUnit cu = (JCCompilationUnit)t;
               for (JCTree def : cu.defs) {
View Full Code Here


        DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<JavaFileObject>();
        StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnosticCollector, null, null);
        Iterable<? extends JavaFileObject> sourceFileObjects = fileManager.getJavaFileObjects(sourceFiles);
        System.err.println("1- javac given java source JavaFileObjects " + sourceFileObjects);
        JavaCompiler.CompilationTask task = compiler.getTask(compilerOutputStream, fileManager, null, compileOptions, null, sourceFileObjects);
        JavacTask javacTask = (JavacTask) task;

        Iterable<? extends CompilationUnitTree> parsedTrees = javacTask.parse();
        Iterable<? extends Element> analyzedTrees = javacTask.analyze();
        Iterable<? extends JavaFileObject> generatedFiles = javacTask.generate();

        System.err.println("2- parsed:" + size(parsedTrees) + " analysed:" + size(analyzedTrees) + " generated:" + size(generatedFiles));

        System.err.print("3-");
        for (JavaFileObject f : generatedFiles)
View Full Code Here

        }
    }
    static Trees trees;
    public static void main(String[] args) throws IOException {
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        JavacTask task = (JavacTask) compiler.getTask(null, null, null, null, null, List.of(new MyFileObject()));
        trees = Trees.instance(task);
        Iterable<? extends CompilationUnitTree> asts = task.parse();
        task.analyze();
        for (CompilationUnitTree ast : asts) {
            new MyVisitor().scan(ast, null);
        }
    }
View Full Code Here

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

        };
        List<? extends JavaFileObject> files = Arrays.asList(sfo);
        String bootPath = System.getProperty("sun.boot.class.path");
        List<String> opts = Arrays.asList("-bootclasspath",  bootPath, "-Xjcov");
        JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
        JavacTask ct = (JavacTask)tool.getTask(null, null, null,opts,null,files);
        ct.analyze();
    }
View Full Code Here

        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)
            s.scan(t, null);
View Full Code Here

            return source;
        }
    }

    void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception {
        JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker,
                null, null, Arrays.asList(source));
        ct.analyze();
        check();
    }
View Full Code Here

    }

    static void compileAndCheck(ParameterType ptA, ClassType ctA, ParameterType ptB, ClassType ctB) throws Exception {
        JavaSource source = new JavaSource(ptA.instantiate(ctA), ptB.instantiate(ctB));
        final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
        JavacTask ct = (JavacTask)tool.getTask(null, null, null,
                null, null, Arrays.asList(source));
        ct.setProcessors(Arrays.asList(new ContainTypesTester(ParameterType.contains(ptA, ctA, ptB, ctB), source)));
        System.err.println("A = " + ptA +" / " + ptA.instantiate(ctA));
        System.err.println("B = " + ptB +" / " + ptB.instantiate(ctB));
        System.err.println("Source = " + source.source);
        ct.analyze();
    }
View Full Code Here

public class T6598108 {
    public static void main(String[] args) throws Exception {
        final String bootPath = System.getProperty("sun.boot.class.path"); //NOI18N
        final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
        assert tool != null;
        final JavacTask ct = (JavacTask)tool.getTask(null, null, null, Arrays.asList("-bootclasspath",  bootPath), null, Arrays.asList(new MyFileObject()));

        CompilationUnitTree cut = ct.parse().iterator().next();
        TreePath tp = new TreePath(new TreePath(cut), cut.getTypeDecls().get(0));
        Scope s = Trees.instance(ct).getScope(tp);
        TypeElement type = ct.getElements().getTypeElement("com.sun.java.util.jar.pack.Package.File");

        if (Trees.instance(ct).isAccessible(s, type)) {
            //com.sun.java.util.jar.pack.Package.File is a public innerclass inside a non-accessible class, so
            //"false" would be expected here.
            throw new IllegalStateException("");
View Full Code Here

    static Map<ClassFile, List<Method>> compile(Implementation implB, Implementation implC, Implementation implD, String destPath) throws Exception {
        File destDir = new File(workDir, destPath); destDir.mkdir();
        final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
        JavaSource source = new JavaSource(implB, implC, implD);
        JavacTask ct = (JavacTask)tool.getTask(null, null, null,
                Arrays.asList("-d", destPath), null, Arrays.asList(source));
        ct.generate();
        Map<ClassFile, List<Method>> members = new HashMap<>();
        addMembers(destDir, members);
        return members;
    }
View Full Code Here

TOP

Related Classes of com.sun.source.util.JavacTask

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.