diff options
Diffstat (limited to 'src/utils/lombok')
7 files changed, 102 insertions, 80 deletions
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); } } |