diff options
Diffstat (limited to 'src/utils/lombok/javac/java6')
4 files changed, 88 insertions, 13 deletions
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 |