Package com.sun.source.util

Examples of com.sun.source.util.JavacTask


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


            return source;
        }
    }

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

    public static void main(String[] args) throws Exception {

        JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
        StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
        fm.setLocation(CLASS_PATH, Collections.<File>emptyList());
        JavacTask javac = (JavacTask)tool.getTask(null, fm, null, null, null, null);
        Elements elements = javac.getElements();

        final Set<String> packages = new LinkedHashSet<String>();

        int nestedClasses = 0;
        int classes = 0;

        for (JavaFileObject file : fm.list(PLATFORM_CLASS_PATH, "", EnumSet.of(CLASS), true)) {
            String type = fm.inferBinaryName(PLATFORM_CLASS_PATH, file);
            if (type.endsWith("package-info"))
                continue;
            try {
                TypeElement elem = elements.getTypeElement(type);
                if (elem == null && type.indexOf('$') > 0) {
                    nestedClasses++;
                    type = null;
                    continue;
                }
                classes++;
                packages.add(getPackage(elem).getQualifiedName().toString());
                elements.getTypeElement(type).getKind(); // force completion
                type = null;
            } finally {
                if (type != null)
                    System.err.println("Looking at " + type);
            }
        }
        javac = null;
        elements = null;

        javac = (JavacTask)tool.getTask(null, fm, null, null, null, null);
        elements = javac.getElements();

        for (String name : packages) {
            PackageElement pe = elements.getPackageElement(name);
            for (Element e : pe.getEnclosedElements()) {
                e.getSimpleName().getClass();
View Full Code Here

    static void test(XlintOption xlint, SuppressLevel suppressLevel, ResourceUsage usage1,
                ResourceUsage usage2, ResourceUsage usage3) throws Exception {
        final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
        JavaSource source = new JavaSource(suppressLevel, usage1, usage2, usage3);
        DiagnosticChecker dc = new DiagnosticChecker();
        JavacTask ct = (JavacTask)tool.getTask(null, fm, dc,
                Arrays.asList(xlint.getXlintOption()), null, Arrays.asList(source));
        ct.analyze();
        check(source, xlint, suppressLevel, usage1, usage2, usage3, dc);
    }
View Full Code Here

    public static void main(String[] args) throws Exception {
        File testSrc = new File(System.getProperty("test.src"));
        File thisSrc = new File(testSrc, T6963934.class.getSimpleName() + ".java");
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
        JavacTask task = (JavacTask) compiler.getTask(null, fileManager, null, null, null,
                fileManager.getJavaFileObjects(thisSrc));
        CompilationUnitTree tree = task.parse().iterator().next();
        int count = 0;
        for (ImportTree importTree : tree.getImports()) {
            System.out.println(importTree);
            count++;
        }
View Full Code Here

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

        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:
        // Note: T6410706.java uses or overrides a deprecated API.
        // Note: Recompile with -Xlint:deprecation for details.
View Full Code Here

        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"));
        if (!expectedClass.exists())
View Full Code Here

        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        StandardJavaFileManager manager =
                compiler.getStandardFileManager(null, null, null);
        Iterable<? extends JavaFileObject> units = manager.getJavaFileObjects(test_java);
        final StringWriter sw = new StringWriter();
        JavacTask task = (JavacTask) compiler.getTask(sw, manager, null, null,
                null, units);

        new TreeScanner<Boolean, Void>() {
            @Override
            public Boolean visitClass(ClassTree arg0, Void arg1) {
                sw.write(arg0.toString());
                return super.visitClass(arg0, arg1);
            }
        }.scan(task.parse(), null);

        System.out.println("output:");
        System.out.println(sw.toString());
        String found = sw.toString().replaceAll("\\s+", " ").trim();
        String expect = test.replaceAll("\\s+", " ").trim();
View Full Code Here

    }

    public static void main(String[] args) throws IOException {
        List<? extends JavaFileObject> files = Arrays.asList(new SourceFile());
        JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
        JavacTask ct = (JavacTask)tool.getTask(null, null, null, null, null, files);
        Context ctx = new Context();
        JavacFileManager.preRegister(ctx);
        checkImplementationCache(ct.analyze(), Types.instance(ctx));
    }
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.