diff options
author | Reinier Zwitserloot <reinier@zwitserloot.com> | 2011-11-21 23:53:00 +0100 |
---|---|---|
committer | Reinier Zwitserloot <reinier@zwitserloot.com> | 2011-11-21 23:53:00 +0100 |
commit | e24ac22aaa286cb91a0dcdbff1f95d09fb62f8ef (patch) | |
tree | 5bc8791be6f63268a959d4313ec383830aba3aa1 /src/delombok | |
parent | 3fd3ca32a68c728bebd04d41720f2d1f636a3695 (diff) | |
download | lombok-e24ac22aaa286cb91a0dcdbff1f95d09fb62f8ef.tar.gz lombok-e24ac22aaa286cb91a0dcdbff1f95d09fb62f8ef.tar.bz2 lombok-e24ac22aaa286cb91a0dcdbff1f95d09fb62f8ef.zip |
Made an API for creating a (oracle javac) JavaCompiler which tracks comments on a per Compilation Unit basis. The old way involved making reflective calls and detecting whether you need the 1.6 or the 1.7 variant to do this. These shenanigans are now hidden behind a nice API (lombok.javac.CommentCatcher).
Diffstat (limited to 'src/delombok')
-rw-r--r-- | src/delombok/lombok/delombok/Delombok.java | 44 | ||||
-rw-r--r-- | src/delombok/lombok/delombok/DelombokResult.java | 8 |
2 files changed, 10 insertions, 42 deletions
diff --git a/src/delombok/lombok/delombok/Delombok.java b/src/delombok/lombok/delombok/Delombok.java index 1bc550e1..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; @@ -411,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() { |