diff options
Diffstat (limited to 'src/delombok')
-rw-r--r-- | src/delombok/lombok/delombok/Delombok.java | 50 | ||||
-rw-r--r-- | src/delombok/lombok/delombok/DelombokResult.java | 8 |
2 files changed, 15 insertions, 43 deletions
diff --git a/src/delombok/lombok/delombok/Delombok.java b/src/delombok/lombok/delombok/Delombok.java index fb93b1ce..a639a1f6 100644 --- a/src/delombok/lombok/delombok/Delombok.java +++ b/src/delombok/lombok/delombok/Delombok.java @@ -44,7 +44,7 @@ import java.util.Map; import javax.tools.DiagnosticListener; import javax.tools.JavaFileObject; -import lombok.javac.Comment; +import lombok.javac.CommentCatcher; import lombok.javac.LombokOptions; import com.sun.tools.javac.main.JavaCompiler; @@ -357,15 +357,8 @@ public class Delombok { if (sourcepath != null) options.put(OptionName.SOURCEPATH, sourcepath); options.put("compilePolicy", "attr"); - - registerCommentsCollectingScannerFactory(context); - JavaCompiler compiler = new JavaCompiler(context); - - Map<JCCompilationUnit, com.sun.tools.javac.util.List<Comment>> commentsMap = new IdentityHashMap<JCCompilationUnit, com.sun.tools.javac.util.List<Comment>>(); - setInCompiler(compiler, context, commentsMap); - - compiler.keepComments = true; - compiler.genEndPos = true; + CommentCatcher catcher = CommentCatcher.create(context); + JavaCompiler compiler = catcher.getCompiler(); List<JCCompilationUnit> roots = new ArrayList<JCCompilationUnit>(); Map<JCCompilationUnit, File> baseMap = new IdentityHashMap<JCCompilationUnit, File>(); @@ -389,7 +382,7 @@ public class Delombok { JavaCompiler delegate = compiler.processAnnotations(compiler.enterTrees(toJavacList(roots))); for (JCCompilationUnit unit : roots) { - DelombokResult result = new DelombokResult(commentsMap.get(unit), unit, force || options.changed.contains(unit)); + DelombokResult result = new DelombokResult(catcher.getComments(unit), unit, force || options.isChanged(unit)); if (verbose) feedback.printf("File: %s [%s]\n", unit.sourcefile.getName(), result.isChanged() ? "delomboked" : "unchanged"); Writer rawWriter; if (presetWriter != null) rawWriter = presetWriter; @@ -399,7 +392,11 @@ public class Delombok { try { result.print(writer); } finally { - writer.close(); + if (output != null) { + writer.close(); + } else { + writer.flush(); + } } } delegate.close(); @@ -407,35 +404,6 @@ public class Delombok { return true; } - public static void setInCompiler(JavaCompiler compiler, Context context, Map<JCCompilationUnit, com.sun.tools.javac.util.List<Comment>> commentsMap) { - - try { - if (JavaCompiler.version().startsWith("1.6")) { - Class<?> parserFactory = Class.forName("lombok.javac.java6.CommentCollectingParserFactory"); - parserFactory.getMethod("setInCompiler",JavaCompiler.class, Context.class, Map.class).invoke(null, compiler, context, commentsMap); - } else { - Class<?> parserFactory = Class.forName("lombok.javac.java7.CommentCollectingParserFactory"); - parserFactory.getMethod("setInCompiler",JavaCompiler.class, Context.class, Map.class).invoke(null, compiler, context, commentsMap); - } - } catch (Exception e) { - if (e instanceof RuntimeException) throw (RuntimeException)e; - throw new RuntimeException(e); - } - } - - public static void registerCommentsCollectingScannerFactory(Context context) { - try { - if (JavaCompiler.version().startsWith("1.6")) { - Class.forName("lombok.javac.java6.CommentCollectingScannerFactory").getMethod("preRegister", Context.class).invoke(null, context); - } else { - Class.forName("lombok.javac.java7.CommentCollectingScannerFactory").getMethod("preRegister", Context.class).invoke(null, context); - } - } catch (Exception e) { - if (e instanceof RuntimeException) throw (RuntimeException)e; - throw new RuntimeException(e); - } - } - private static String canonical(File dir) { try { return dir.getCanonicalPath(); diff --git a/src/delombok/lombok/delombok/DelombokResult.java b/src/delombok/lombok/delombok/DelombokResult.java index 11755707..0ed39607 100644 --- a/src/delombok/lombok/delombok/DelombokResult.java +++ b/src/delombok/lombok/delombok/DelombokResult.java @@ -23,13 +23,13 @@ import java.io.IOException; import java.io.Writer; import java.util.Date; +import java.util.List; import javax.tools.JavaFileObject; import lombok.javac.Comment; import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; -import com.sun.tools.javac.util.List; public class DelombokResult { private final List<Comment> comments; @@ -55,7 +55,11 @@ public class DelombokResult { out.write(String.valueOf(new Date())); out.write(System.getProperty("line.separator")); - compilationUnit.accept(new PrettyCommentsPrinter(out, compilationUnit, comments)); + com.sun.tools.javac.util.List<Comment> comments_; + if (comments instanceof com.sun.tools.javac.util.List) comments_ = (com.sun.tools.javac.util.List<Comment>) comments; + else comments_ = com.sun.tools.javac.util.List.from(comments.toArray(new Comment[0])); + + compilationUnit.accept(new PrettyCommentsPrinter(out, compilationUnit, comments_)); } public boolean isChanged() { |