Search in sources :

Example 1 with CommentCatcher

use of lombok.javac.CommentCatcher in project lombok by rzwitserloot.

the class Delombok method delombok.

public boolean delombok() throws IOException {
    LombokOptions options = LombokOptionsFactory.getDelombokOptions(context);
    options.deleteLombokAnnotations();
    options.putJavacOption("ENCODING", charset.name());
    if (classpath != null)
        options.putJavacOption("CLASSPATH", classpath);
    if (sourcepath != null)
        options.putJavacOption("SOURCEPATH", sourcepath);
    if (bootclasspath != null)
        options.putJavacOption("BOOTCLASSPATH", bootclasspath);
    options.setFormatPreferences(new FormatPreferences(formatPrefs));
    options.put("compilePolicy", "check");
    CommentCatcher catcher = CommentCatcher.create(context);
    JavaCompiler compiler = catcher.getCompiler();
    List<JCCompilationUnit> roots = new ArrayList<JCCompilationUnit>();
    Map<JCCompilationUnit, File> baseMap = new IdentityHashMap<JCCompilationUnit, File>();
    compiler.initProcessAnnotations(Collections.singleton(new lombok.javac.apt.LombokProcessor()));
    for (File fileToParse : filesToParse) {
        @SuppressWarnings("deprecation") JCCompilationUnit unit = compiler.parse(fileToParse.getAbsolutePath());
        baseMap.put(unit, fileToBase.get(fileToParse));
        roots.add(unit);
    }
    if (compiler.errorCount() > 0) {
        // At least one parse error. No point continuing (a real javac run doesn't either).
        return false;
    }
    for (JCCompilationUnit unit : roots) {
        catcher.setComments(unit, new DocCommentIntegrator().integrate(catcher.getComments(unit), unit));
    }
    com.sun.tools.javac.util.List<JCCompilationUnit> trees = compiler.enterTrees(toJavacList(roots));
    JavaCompiler delegate = compiler.processAnnotations(trees);
    Object care = callAttributeMethodOnJavaCompiler(delegate, delegate.todo);
    callFlowMethodOnJavaCompiler(delegate, care);
    FormatPreferences fps = new FormatPreferences(formatPrefs);
    for (JCCompilationUnit unit : roots) {
        DelombokResult result = new DelombokResult(catcher.getComments(unit), unit, force || options.isChanged(unit), fps);
        if (verbose)
            feedback.printf("File: %s [%s%s]\n", unit.sourcefile.getName(), result.isChanged() ? "delomboked" : "unchanged", force && !options.isChanged(unit) ? " (forced)" : "");
        Writer rawWriter;
        if (presetWriter != null)
            rawWriter = createUnicodeEscapeWriter(presetWriter);
        else if (output == null)
            rawWriter = createStandardOutWriter();
        else
            rawWriter = createFileWriter(output, baseMap.get(unit), unit.sourcefile.toUri());
        BufferedWriter writer = new BufferedWriter(rawWriter);
        try {
            result.print(writer);
        } finally {
            if (output != null) {
                writer.close();
            } else {
                writer.flush();
            }
        }
    }
    delegate.close();
    return true;
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) IdentityHashMap(java.util.IdentityHashMap) CommentCatcher(lombok.javac.CommentCatcher) ArrayList(java.util.ArrayList) JavaCompiler(com.sun.tools.javac.main.JavaCompiler) BufferedWriter(java.io.BufferedWriter) LombokOptions(lombok.javac.LombokOptions) JavaFileObject(javax.tools.JavaFileObject) File(java.io.File) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer)

Aggregations

JavaCompiler (com.sun.tools.javac.main.JavaCompiler)1 JCCompilationUnit (com.sun.tools.javac.tree.JCTree.JCCompilationUnit)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 ArrayList (java.util.ArrayList)1 IdentityHashMap (java.util.IdentityHashMap)1 JavaFileObject (javax.tools.JavaFileObject)1 CommentCatcher (lombok.javac.CommentCatcher)1 LombokOptions (lombok.javac.LombokOptions)1