aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoel Spilker <r.spilker@gmail.com>2011-10-31 23:19:25 +0100
committerRoel Spilker <r.spilker@gmail.com>2011-10-31 23:19:25 +0100
commit70f778daa560a899abe91a4908cd37c70ff1f3b4 (patch)
treefbf77f10ebffc9da1ab59be25c4ff6499f9dc54a
parented177bb7822f460bf7e3b07a1f5754f127842a63 (diff)
downloadlombok-70f778daa560a899abe91a4908cd37c70ff1f3b4.tar.gz
lombok-70f778daa560a899abe91a4908cd37c70ff1f3b4.tar.bz2
lombok-70f778daa560a899abe91a4908cd37c70ff1f3b4.zip
Fixed delombok making a mess of comments (issue 284) for javac 6. delombok in java7 is now completely broken but we'll fix that next.
-rw-r--r--src/delombok/lombok/delombok/Delombok.java44
-rw-r--r--src/utils/lombok/javac/Comments.java52
-rw-r--r--src/utils/lombok/javac/java6/CommentCollectingParser.java32
-rw-r--r--src/utils/lombok/javac/java6/CommentCollectingParserFactory.java43
-rw-r--r--src/utils/lombok/javac/java6/CommentCollectingScanner.java18
-rw-r--r--src/utils/lombok/javac/java6/CommentCollectingScannerFactory.java8
-rw-r--r--src/utils/lombok/javac/java7/CommentCollectingScanner.java21
-rw-r--r--src/utils/lombok/javac/java7/CommentCollectingScannerFactory.java8
8 files changed, 140 insertions, 86 deletions
diff --git a/src/delombok/lombok/delombok/Delombok.java b/src/delombok/lombok/delombok/Delombok.java
index e069fa5f..c6d3444a 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.Comments;
+import lombok.javac.Comment;
import lombok.javac.LombokOptions;
import com.sun.tools.javac.main.JavaCompiler;
@@ -357,24 +357,27 @@ public class Delombok {
if (sourcepath != null) options.put(OptionName.SOURCEPATH, sourcepath);
options.put("compilePolicy", "attr");
+
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;
List<JCCompilationUnit> roots = new ArrayList<JCCompilationUnit>();
- Map<JCCompilationUnit, Comments> commentsMap = new IdentityHashMap<JCCompilationUnit, Comments>();
Map<JCCompilationUnit, File> baseMap = new IdentityHashMap<JCCompilationUnit, File>();
+ registerCommentsCollectingScannerFactory(context);
+
compiler.initProcessAnnotations(Collections.singleton(new lombok.javac.apt.Processor()));
for (File fileToParse : filesToParse) {
- Comments comments = new Comments();
- comments.register(context);
@SuppressWarnings("deprecation")
JCCompilationUnit unit = compiler.parse(fileToParse.getAbsolutePath());
- commentsMap.put(unit, comments);
baseMap.put(unit, fileToBase.get(fileToParse));
roots.add(unit);
}
@@ -386,7 +389,7 @@ public class Delombok {
JavaCompiler delegate = compiler.processAnnotations(compiler.enterTrees(toJavacList(roots)));
for (JCCompilationUnit unit : roots) {
- DelombokResult result = new DelombokResult(commentsMap.get(unit).getComments().toList(), unit, force || options.changed.contains(unit));
+ DelombokResult result = new DelombokResult(commentsMap.get(unit), unit, force || options.changed.contains(unit));
if (verbose) feedback.printf("File: %s [%s]\n", unit.sourcefile.getName(), result.isChanged() ? "delomboked" : "unchanged");
Writer rawWriter;
if (presetWriter != null) rawWriter = presetWriter;
@@ -404,6 +407,35 @@ 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/utils/lombok/javac/Comments.java b/src/utils/lombok/javac/Comments.java
deleted file mode 100644
index 2e8ceb26..00000000
--- a/src/utils/lombok/javac/Comments.java
+++ /dev/null
@@ -1,52 +0,0 @@
-/*
- * Copyright © 2011 Reinier Zwitserloot, Roel Spilker and Robbert Jan Grootjans.
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-package lombok.javac;
-
-import com.sun.tools.javac.main.JavaCompiler;
-import com.sun.tools.javac.util.Context;
-
-public class Comments {
- public void register(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);
- }
- context.put(Comments.class, (Comments) null);
- context.put(Comments.class, this);
- }
-
- private com.sun.tools.javac.util.ListBuffer<Comment> comments = com.sun.tools.javac.util.ListBuffer.lb();
-
- public com.sun.tools.javac.util.ListBuffer<Comment> getComments() {
- return comments;
- }
-
- public void add(Comment comment) {
- comments.append(comment);
- }
-}
diff --git a/src/utils/lombok/javac/java6/CommentCollectingParser.java b/src/utils/lombok/javac/java6/CommentCollectingParser.java
new file mode 100644
index 00000000..bc33bf71
--- /dev/null
+++ b/src/utils/lombok/javac/java6/CommentCollectingParser.java
@@ -0,0 +1,32 @@
+package lombok.javac.java6;
+
+import java.util.Map;
+
+import lombok.javac.Comment;
+
+import com.sun.tools.javac.parser.EndPosParser;
+import com.sun.tools.javac.parser.Lexer;
+import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
+import com.sun.tools.javac.util.List;
+
+class CommentCollectingParser extends EndPosParser {
+
+ private final Map<JCCompilationUnit, List<Comment>> commentsMap;
+ private final Lexer lexer;
+
+ protected CommentCollectingParser(Factory fac, Lexer S, boolean keepDocComments, Map<JCCompilationUnit, List<Comment>> commentsMap) {
+ super(fac, S, keepDocComments);
+ lexer = S;
+ this.commentsMap = commentsMap;
+
+ }
+
+ @Override public JCCompilationUnit compilationUnit() {
+ JCCompilationUnit result = super.compilationUnit();
+ if (lexer instanceof CommentCollectingScanner) {
+ List<Comment> comments = ((CommentCollectingScanner)lexer).getComments();
+ commentsMap.put(result, comments);
+ }
+ return result;
+ }
+} \ No newline at end of file
diff --git a/src/utils/lombok/javac/java6/CommentCollectingParserFactory.java b/src/utils/lombok/javac/java6/CommentCollectingParserFactory.java
new file mode 100644
index 00000000..ef8b22c4
--- /dev/null
+++ b/src/utils/lombok/javac/java6/CommentCollectingParserFactory.java
@@ -0,0 +1,43 @@
+package lombok.javac.java6;
+
+import java.lang.reflect.Field;
+import java.util.Map;
+
+import lombok.javac.Comment;
+
+import com.sun.tools.javac.main.JavaCompiler;
+import com.sun.tools.javac.parser.Lexer;
+import com.sun.tools.javac.parser.Parser;
+import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
+import com.sun.tools.javac.util.Context;
+import com.sun.tools.javac.util.List;
+
+public class CommentCollectingParserFactory extends Parser.Factory {
+
+ private final Map<JCCompilationUnit, List<Comment>> commentsMap;
+
+ static Context.Key<Parser.Factory> key() {
+ return parserFactoryKey;
+ }
+
+ protected CommentCollectingParserFactory(Context context, Map<JCCompilationUnit, List<Comment>> commentsMap) {
+ super(context);
+ this.commentsMap = commentsMap;
+ }
+
+ @Override public Parser newParser(Lexer S, boolean keepDocComments, boolean genEndPos) {
+ return new CommentCollectingParser(this, S, keepDocComments, commentsMap);
+ }
+
+ public static void setInCompiler(JavaCompiler compiler, Context context, Map<JCCompilationUnit, List<Comment>> commentsMap) {
+ context.put(CommentCollectingParserFactory.key(), (Parser.Factory)null);
+ Field field;
+ try {
+ field = JavaCompiler.class.getDeclaredField("parserFactory");
+ field.setAccessible(true);
+ field.set(compiler, new CommentCollectingParserFactory(context, commentsMap));
+ } catch (Exception e) {
+ throw new IllegalStateException("Could not set comment sensitive parser in the compiler", e);
+ }
+ }
+} \ No newline at end of file
diff --git a/src/utils/lombok/javac/java6/CommentCollectingScanner.java b/src/utils/lombok/javac/java6/CommentCollectingScanner.java
index 363e04ba..8fa0a766 100644
--- a/src/utils/lombok/javac/java6/CommentCollectingScanner.java
+++ b/src/utils/lombok/javac/java6/CommentCollectingScanner.java
@@ -24,24 +24,24 @@ package lombok.javac.java6;
import java.nio.CharBuffer;
import lombok.javac.Comment;
-import lombok.javac.Comments;
import lombok.javac.Comment.EndConnection;
import lombok.javac.Comment.StartConnection;
import com.sun.tools.javac.parser.Scanner;
+import com.sun.tools.javac.util.List;
+import com.sun.tools.javac.util.ListBuffer;
+
public class CommentCollectingScanner extends Scanner {
- private final Comments comments;
+ private final ListBuffer<Comment> comments = ListBuffer.lb();
private int endComment = 0;
- public CommentCollectingScanner(CommentCollectingScannerFactory factory, CharBuffer charBuffer, Comments comments) {
+ public CommentCollectingScanner(CommentCollectingScannerFactory factory, CharBuffer charBuffer) {
super(factory, charBuffer);
- this.comments = comments;
}
- public CommentCollectingScanner(CommentCollectingScannerFactory factory, char[] input, int inputLength, Comments comments) {
+ public CommentCollectingScanner(CommentCollectingScannerFactory factory, char[] input, int inputLength) {
super(factory, input, inputLength);
- this.comments = comments;
}
@Override
@@ -55,7 +55,7 @@ public class CommentCollectingScanner extends Scanner {
EndConnection end = determineEndConnection(endPos);
Comment comment = new Comment(prevEndPos, pos, endPos, content, start, end);
- comments.add(comment);
+ comments.append(comment);
}
private EndConnection determineEndConnection(int pos) {
@@ -92,4 +92,8 @@ public class CommentCollectingScanner extends Scanner {
private boolean isNewLine(char c) {
return c == '\n' || c == '\r';
}
+
+ public List<Comment> getComments() {
+ return comments.toList();
+ }
}
diff --git a/src/utils/lombok/javac/java6/CommentCollectingScannerFactory.java b/src/utils/lombok/javac/java6/CommentCollectingScannerFactory.java
index c259a4cf..92784440 100644
--- a/src/utils/lombok/javac/java6/CommentCollectingScannerFactory.java
+++ b/src/utils/lombok/javac/java6/CommentCollectingScannerFactory.java
@@ -23,13 +23,10 @@ package lombok.javac.java6;
import java.nio.CharBuffer;
-import lombok.javac.Comments;
-
import com.sun.tools.javac.parser.Scanner;
import com.sun.tools.javac.util.Context;
public class CommentCollectingScannerFactory extends Scanner.Factory {
- private final Context context;
public static void preRegister(final Context context) {
if (context.get(scannerFactoryKey) == null) {
@@ -48,13 +45,12 @@ public class CommentCollectingScannerFactory extends Scanner.Factory {
/** Create a new scanner factory. */
protected CommentCollectingScannerFactory(Context context) {
super(context);
- this.context = context;
}
@Override
public Scanner newScanner(CharSequence input) {
if (input instanceof CharBuffer) {
- return new CommentCollectingScanner(this, (CharBuffer)input, context.get(Comments.class));
+ return new CommentCollectingScanner(this, (CharBuffer)input);
}
char[] array = input.toString().toCharArray();
return newScanner(array, array.length);
@@ -62,6 +58,6 @@ public class CommentCollectingScannerFactory extends Scanner.Factory {
@Override
public Scanner newScanner(char[] input, int inputLength) {
- return new CommentCollectingScanner(this, input, inputLength, context.get(Comments.class));
+ return new CommentCollectingScanner(this, input, inputLength);
}
} \ No newline at end of file
diff --git a/src/utils/lombok/javac/java7/CommentCollectingScanner.java b/src/utils/lombok/javac/java7/CommentCollectingScanner.java
index 2c588175..c1aec385 100644
--- a/src/utils/lombok/javac/java7/CommentCollectingScanner.java
+++ b/src/utils/lombok/javac/java7/CommentCollectingScanner.java
@@ -26,22 +26,21 @@ import java.nio.CharBuffer;
import lombok.javac.Comment;
import lombok.javac.Comment.EndConnection;
import lombok.javac.Comment.StartConnection;
-import lombok.javac.Comments;
+import com.sun.tools.javac.util.List;
+import com.sun.tools.javac.util.ListBuffer;
import com.sun.tools.javac.parser.Scanner;
public class CommentCollectingScanner extends Scanner {
- private final Comments comments;
+ private final ListBuffer<Comment> comments = ListBuffer.lb();
private int endComment = 0;
- public CommentCollectingScanner(CommentCollectingScannerFactory factory, CharBuffer charBuffer, Comments comments) {
+ public CommentCollectingScanner(CommentCollectingScannerFactory factory, CharBuffer charBuffer) {
super(factory, charBuffer);
- this.comments = comments;
}
- public CommentCollectingScanner(CommentCollectingScannerFactory factory, char[] input, int inputLength, Comments comments) {
+ public CommentCollectingScanner(CommentCollectingScannerFactory factory, char[] input, int inputLength) {
super(factory, input, inputLength);
- this.comments = comments;
}
@Override
@@ -55,7 +54,7 @@ public class CommentCollectingScanner extends Scanner {
EndConnection end = determineEndConnection(endPos);
Comment comment = new Comment(prevEndPos, pos, endPos, content, start, end);
- comments.add(comment);
+ comments.append(comment);
}
private EndConnection determineEndConnection(int pos) {
@@ -72,7 +71,7 @@ public class CommentCollectingScanner extends Scanner {
return first ? EndConnection.DIRECT_AFTER_COMMENT : EndConnection.AFTER_COMMENT;
}
}
-
+
private StartConnection determineStartConnection(int from, int to) {
if (from == to) {
return StartConnection.DIRECT_AFTER_PREVIOUS;
@@ -88,8 +87,12 @@ public class CommentCollectingScanner extends Scanner {
}
return StartConnection.AFTER_PREVIOUS;
}
-
+
private boolean isNewLine(char c) {
return c == '\n' || c == '\r';
}
+
+ public List<Comment> getComments() {
+ return comments.toList();
+ }
}
diff --git a/src/utils/lombok/javac/java7/CommentCollectingScannerFactory.java b/src/utils/lombok/javac/java7/CommentCollectingScannerFactory.java
index c6cf4011..757f44a1 100644
--- a/src/utils/lombok/javac/java7/CommentCollectingScannerFactory.java
+++ b/src/utils/lombok/javac/java7/CommentCollectingScannerFactory.java
@@ -23,14 +23,11 @@ package lombok.javac.java7;
import java.nio.CharBuffer;
-import lombok.javac.Comments;
-
import com.sun.tools.javac.parser.Scanner;
import com.sun.tools.javac.parser.ScannerFactory;
import com.sun.tools.javac.util.Context;
public class CommentCollectingScannerFactory extends ScannerFactory {
- private final Context context;
public static void preRegister(final Context context) {
if (context.get(scannerFactoryKey) == null) {
@@ -49,13 +46,12 @@ public class CommentCollectingScannerFactory extends ScannerFactory {
/** Create a new scanner factory. */
protected CommentCollectingScannerFactory(Context context) {
super(context);
- this.context = context;
}
@Override
public Scanner newScanner(CharSequence input, boolean keepDocComments) {
if (input instanceof CharBuffer) {
- return new CommentCollectingScanner(this, (CharBuffer)input, context.get(Comments.class));
+ return new CommentCollectingScanner(this, (CharBuffer)input);
}
char[] array = input.toString().toCharArray();
return newScanner(array, array.length, keepDocComments);
@@ -63,6 +59,6 @@ public class CommentCollectingScannerFactory extends ScannerFactory {
@Override
public Scanner newScanner(char[] input, int inputLength, boolean keepDocComments) {
- return new CommentCollectingScanner(this, input, inputLength, context.get(Comments.class));
+ return new CommentCollectingScanner(this, input, inputLength);
}
}