mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-20 02:17:52 +00:00
Renamed Comment to CommentInfo.
This commit is contained in:
1 parent
3e18007af3
commit
d713dab0c3
10 files changed
+83
-56
No files matched your search
@@ -27,16 +27,16 @@ import java.util.List;
|
||||
|
||||
import javax.tools.JavaFileObject;
|
||||
|
||||
import lombok.javac.Comment;
|
||||
import lombok.javac.CommentInfo;
|
||||
|
||||
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
|
||||
|
||||
public class DelombokResult {
|
||||
private final List<Comment> comments;
|
||||
private final List<CommentInfo> comments;
|
||||
private final JCCompilationUnit compilationUnit;
|
||||
private final boolean changed;
|
||||
|
||||
public DelombokResult(List<Comment> comments, JCCompilationUnit compilationUnit, boolean changed) {
|
||||
public DelombokResult(List<CommentInfo> comments, JCCompilationUnit compilationUnit, boolean changed) {
|
||||
this.comments = comments;
|
||||
this.compilationUnit = compilationUnit;
|
||||
this.changed = changed;
|
||||
@@ -55,9 +55,9 @@ public class DelombokResult {
|
||||
out.write(String.valueOf(new Date()));
|
||||
out.write(System.getProperty("line.separator"));
|
||||
|
||||
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]));
|
||||
com.sun.tools.javac.util.List<CommentInfo> comments_;
|
||||
if (comments instanceof com.sun.tools.javac.util.List) comments_ = (com.sun.tools.javac.util.List<CommentInfo>) comments;
|
||||
else comments_ = com.sun.tools.javac.util.List.from(comments.toArray(new CommentInfo[0]));
|
||||
|
||||
compilationUnit.accept(new PrettyCommentsPrinter(out, compilationUnit, comments_));
|
||||
}
|
||||
|
||||
@@ -39,10 +39,10 @@ import java.lang.reflect.Method;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import lombok.javac.Comment;
|
||||
import lombok.javac.CommentInfo;
|
||||
import lombok.javac.Javac;
|
||||
import lombok.javac.Comment.EndConnection;
|
||||
import lombok.javac.Comment.StartConnection;
|
||||
import lombok.javac.CommentInfo.EndConnection;
|
||||
import lombok.javac.CommentInfo.StartConnection;
|
||||
|
||||
import com.sun.source.tree.Tree;
|
||||
import com.sun.tools.javac.code.BoundKind;
|
||||
@@ -199,7 +199,7 @@ public class PrettyCommentsPrinter extends JCTree.Visitor {
|
||||
}
|
||||
}
|
||||
|
||||
private List<Comment> comments;
|
||||
private List<CommentInfo> comments;
|
||||
private final JCCompilationUnit cu;
|
||||
private boolean onNewLine = true;
|
||||
private boolean aligned = false;
|
||||
@@ -209,7 +209,7 @@ public class PrettyCommentsPrinter extends JCTree.Visitor {
|
||||
private boolean needsNewLine = false;
|
||||
private boolean needsAlign = false;
|
||||
|
||||
public PrettyCommentsPrinter(Writer out, JCCompilationUnit cu, List<Comment> comments) {
|
||||
public PrettyCommentsPrinter(Writer out, JCCompilationUnit cu, List<CommentInfo> comments) {
|
||||
this.out = out;
|
||||
this.comments = comments;
|
||||
this.cu = cu;
|
||||
@@ -222,7 +222,7 @@ public class PrettyCommentsPrinter extends JCTree.Visitor {
|
||||
private void consumeComments(int till) throws IOException {
|
||||
boolean prevNewLine = onNewLine;
|
||||
boolean found = false;
|
||||
Comment head = comments.head;
|
||||
CommentInfo head = comments.head;
|
||||
while (comments.nonEmpty() && head.pos < till) {
|
||||
printComment(head);
|
||||
comments = comments.tail;
|
||||
@@ -235,7 +235,7 @@ public class PrettyCommentsPrinter extends JCTree.Visitor {
|
||||
|
||||
private void consumeTrailingComments(int from) throws IOException {
|
||||
boolean prevNewLine = onNewLine;
|
||||
Comment head = comments.head;
|
||||
CommentInfo head = comments.head;
|
||||
boolean stop = false;
|
||||
while (comments.nonEmpty() && head.prevEndPos == from && !stop && !(head.start == StartConnection.ON_NEXT_LINE || head.start == StartConnection.START_OF_LINE)) {
|
||||
from = head.endPos;
|
||||
@@ -249,7 +249,7 @@ public class PrettyCommentsPrinter extends JCTree.Visitor {
|
||||
}
|
||||
}
|
||||
|
||||
private void printComment(Comment comment) throws IOException {
|
||||
private void printComment(CommentInfo comment) throws IOException {
|
||||
prepareComment(comment.start);
|
||||
print(comment.content);
|
||||
switch (comment.end) {
|
||||
|
||||
@@ -21,24 +21,23 @@
|
||||
*/
|
||||
package lombok.javac;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.WeakHashMap;
|
||||
|
||||
import com.sun.tools.javac.main.JavaCompiler;
|
||||
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
|
||||
import com.sun.tools.javac.util.Context;
|
||||
import com.sun.tools.javac.util.List;
|
||||
|
||||
public class CommentCatcher {
|
||||
private final JavaCompiler compiler;
|
||||
private final Map<JCCompilationUnit, List<Comment>> commentsMap;
|
||||
private final Map<JCCompilationUnit, List<CommentInfo>> commentsMap;
|
||||
|
||||
public static CommentCatcher create(Context context) {
|
||||
registerCommentsCollectingScannerFactory(context);
|
||||
JavaCompiler compiler = new JavaCompiler(context);
|
||||
|
||||
Map<JCCompilationUnit, List<Comment>> commentsMap = new WeakHashMap<JCCompilationUnit, List<Comment>>();
|
||||
Map<JCCompilationUnit, List<CommentInfo>> commentsMap = new WeakHashMap<JCCompilationUnit, List<CommentInfo>>();
|
||||
setInCompiler(compiler, context, commentsMap);
|
||||
|
||||
compiler.keepComments = true;
|
||||
@@ -47,7 +46,7 @@ public class CommentCatcher {
|
||||
return new CommentCatcher(compiler, commentsMap);
|
||||
}
|
||||
|
||||
private CommentCatcher(JavaCompiler compiler, Map<JCCompilationUnit, List<Comment>> commentsMap) {
|
||||
private CommentCatcher(JavaCompiler compiler, Map<JCCompilationUnit, List<CommentInfo>> commentsMap) {
|
||||
this.compiler = compiler;
|
||||
this.commentsMap = commentsMap;
|
||||
}
|
||||
@@ -56,9 +55,9 @@ public class CommentCatcher {
|
||||
return compiler;
|
||||
}
|
||||
|
||||
public List<Comment> getComments(JCCompilationUnit ast) {
|
||||
List<Comment> list = commentsMap.get(ast);
|
||||
return list == null ? Collections.<Comment>emptyList() : list;
|
||||
public List<CommentInfo> getComments(JCCompilationUnit ast) {
|
||||
List<CommentInfo> list = commentsMap.get(ast);
|
||||
return list == null ? List.<CommentInfo>nil() : list;
|
||||
}
|
||||
|
||||
private static void registerCommentsCollectingScannerFactory(Context context) {
|
||||
@@ -74,7 +73,7 @@ public class CommentCatcher {
|
||||
}
|
||||
}
|
||||
|
||||
private static void setInCompiler(JavaCompiler compiler, Context context, Map<JCCompilationUnit, List<Comment>> commentsMap) {
|
||||
private static void setInCompiler(JavaCompiler compiler, Context context, Map<JCCompilationUnit, List<CommentInfo>> commentsMap) {
|
||||
|
||||
try {
|
||||
if (JavaCompiler.version().startsWith("1.6")) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2009 The Project Lombok Authors.
|
||||
* Copyright (C) 2009-2011 The Project Lombok Authors.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
@@ -21,17 +21,31 @@
|
||||
*/
|
||||
package lombok.javac;
|
||||
|
||||
public final class Comment {
|
||||
public final class CommentInfo {
|
||||
public enum StartConnection {
|
||||
/* Comment's start immediately follows a newline. */
|
||||
START_OF_LINE,
|
||||
|
||||
/* Comment's start does not immediately follow a newline, but there is a newline between this
|
||||
* and the previous comment (or pos 0 if first comment). */
|
||||
ON_NEXT_LINE,
|
||||
|
||||
/* Comment's start immediately follows previous comment's end (or pos 0 if first comment). */
|
||||
DIRECT_AFTER_PREVIOUS,
|
||||
|
||||
/* Comment's start does not immediately follow previous comment's end, but there is no newline in
|
||||
* between this and previous comment (or pos 0 if first comment). */
|
||||
AFTER_PREVIOUS
|
||||
}
|
||||
|
||||
public enum EndConnection {
|
||||
/* Comment is followed immediately by another node (not whitespace, not newline). */
|
||||
DIRECT_AFTER_COMMENT,
|
||||
|
||||
/* Comment is followed by some non-newline whitespace then by another node. */
|
||||
AFTER_COMMENT,
|
||||
|
||||
/* Comment is followed by optionally some whitespace then a newline. */
|
||||
ON_NEXT_LINE
|
||||
}
|
||||
|
||||
@@ -42,7 +56,7 @@ public final class Comment {
|
||||
public final StartConnection start;
|
||||
public final EndConnection end;
|
||||
|
||||
public Comment(int prevEndPos, int pos, int endPos, String content, StartConnection start, EndConnection end) {
|
||||
public CommentInfo(int prevEndPos, int pos, int endPos, String content, StartConnection start, EndConnection end) {
|
||||
this.pos = pos;
|
||||
this.prevEndPos = prevEndPos;
|
||||
this.endPos = endPos;
|
||||
@@ -51,6 +65,10 @@ public final class Comment {
|
||||
this.end = end;
|
||||
}
|
||||
|
||||
public boolean isJavadoc() {
|
||||
return content.startsWith("/**");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%d: %s (%s,%s)", pos, content, start, end);
|
||||
@@ -2,7 +2,7 @@ package lombok.javac.java6;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import lombok.javac.Comment;
|
||||
import lombok.javac.CommentInfo;
|
||||
|
||||
import com.sun.tools.javac.parser.EndPosParser;
|
||||
import com.sun.tools.javac.parser.Lexer;
|
||||
@@ -12,10 +12,10 @@ import com.sun.tools.javac.util.List;
|
||||
|
||||
class CommentCollectingParser extends EndPosParser {
|
||||
|
||||
private final Map<JCCompilationUnit, List<Comment>> commentsMap;
|
||||
private final Map<JCCompilationUnit, List<CommentInfo>> commentsMap;
|
||||
private final Lexer lexer;
|
||||
|
||||
protected CommentCollectingParser(Parser.Factory fac, Lexer S, boolean keepDocComments, Map<JCCompilationUnit, List<Comment>> commentsMap) {
|
||||
protected CommentCollectingParser(Parser.Factory fac, Lexer S, boolean keepDocComments, Map<JCCompilationUnit, List<CommentInfo>> commentsMap) {
|
||||
super(fac, S, keepDocComments);
|
||||
lexer = S;
|
||||
this.commentsMap = commentsMap;
|
||||
@@ -24,7 +24,7 @@ class CommentCollectingParser extends EndPosParser {
|
||||
@Override public JCCompilationUnit compilationUnit() {
|
||||
JCCompilationUnit result = super.compilationUnit();
|
||||
if (lexer instanceof CommentCollectingScanner) {
|
||||
List<Comment> comments = ((CommentCollectingScanner)lexer).getComments();
|
||||
List<CommentInfo> comments = ((CommentCollectingScanner)lexer).getComments();
|
||||
commentsMap.put(result, comments);
|
||||
}
|
||||
return result;
|
||||
|
||||
@@ -3,7 +3,7 @@ package lombok.javac.java6;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Map;
|
||||
|
||||
import lombok.javac.Comment;
|
||||
import lombok.javac.CommentInfo;
|
||||
|
||||
import com.sun.tools.javac.main.JavaCompiler;
|
||||
import com.sun.tools.javac.parser.Lexer;
|
||||
@@ -13,13 +13,13 @@ 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;
|
||||
private final Map<JCCompilationUnit, List<CommentInfo>> commentsMap;
|
||||
|
||||
static Context.Key<Parser.Factory> key() {
|
||||
return parserFactoryKey;
|
||||
}
|
||||
|
||||
protected CommentCollectingParserFactory(Context context, Map<JCCompilationUnit, List<Comment>> commentsMap) {
|
||||
protected CommentCollectingParserFactory(Context context, Map<JCCompilationUnit, List<CommentInfo>> commentsMap) {
|
||||
super(context);
|
||||
this.commentsMap = commentsMap;
|
||||
}
|
||||
@@ -32,7 +32,7 @@ public class CommentCollectingParserFactory extends Parser.Factory {
|
||||
//Either way this will work out.
|
||||
}
|
||||
|
||||
public static void setInCompiler(JavaCompiler compiler, Context context, Map<JCCompilationUnit, List<Comment>> commentsMap) {
|
||||
public static void setInCompiler(JavaCompiler compiler, Context context, Map<JCCompilationUnit, List<CommentInfo>> commentsMap) {
|
||||
context.put(CommentCollectingParserFactory.key(), (Parser.Factory)null);
|
||||
Field field;
|
||||
try {
|
||||
|
||||
@@ -23,9 +23,9 @@ package lombok.javac.java6;
|
||||
|
||||
import java.nio.CharBuffer;
|
||||
|
||||
import lombok.javac.Comment;
|
||||
import lombok.javac.Comment.EndConnection;
|
||||
import lombok.javac.Comment.StartConnection;
|
||||
import lombok.javac.CommentInfo;
|
||||
import lombok.javac.CommentInfo.EndConnection;
|
||||
import lombok.javac.CommentInfo.StartConnection;
|
||||
|
||||
import com.sun.tools.javac.parser.Scanner;
|
||||
import com.sun.tools.javac.util.List;
|
||||
@@ -33,7 +33,7 @@ import com.sun.tools.javac.util.ListBuffer;
|
||||
|
||||
|
||||
public class CommentCollectingScanner extends Scanner {
|
||||
private final ListBuffer<Comment> comments = ListBuffer.lb();
|
||||
private final ListBuffer<CommentInfo> comments = ListBuffer.lb();
|
||||
private int endComment = 0;
|
||||
|
||||
public CommentCollectingScanner(CommentCollectingScannerFactory factory, CharBuffer charBuffer) {
|
||||
@@ -54,14 +54,19 @@ public class CommentCollectingScanner extends Scanner {
|
||||
StartConnection start = determineStartConnection(prevEndPos, pos);
|
||||
EndConnection end = determineEndConnection(endPos);
|
||||
|
||||
Comment comment = new Comment(prevEndPos, pos, endPos, content, start, end);
|
||||
CommentInfo comment = new CommentInfo(prevEndPos, pos, endPos, content, start, end);
|
||||
comments.append(comment);
|
||||
}
|
||||
|
||||
private EndConnection determineEndConnection(int pos) {
|
||||
boolean first = true;
|
||||
for (int i = pos;; i++) {
|
||||
char c = getRawCharacters(i, i + 1)[0];
|
||||
char c;
|
||||
try {
|
||||
c = getRawCharacters(i, i + 1)[0];
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
c = '\n';
|
||||
}
|
||||
if (isNewLine(c)) {
|
||||
return EndConnection.ON_NEXT_LINE;
|
||||
}
|
||||
@@ -93,7 +98,7 @@ public class CommentCollectingScanner extends Scanner {
|
||||
return c == '\n' || c == '\r';
|
||||
}
|
||||
|
||||
public List<Comment> getComments() {
|
||||
public List<CommentInfo> getComments() {
|
||||
return comments.toList();
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@ package lombok.javac.java7;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import lombok.javac.Comment;
|
||||
import lombok.javac.CommentInfo;
|
||||
|
||||
import com.sun.tools.javac.parser.EndPosParser;
|
||||
import com.sun.tools.javac.parser.Lexer;
|
||||
@@ -11,11 +11,11 @@ import com.sun.tools.javac.parser.ParserFactory;
|
||||
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
|
||||
|
||||
class CommentCollectingParser extends EndPosParser {
|
||||
private final Map<JCCompilationUnit, List<Comment>> commentsMap;
|
||||
private final Map<JCCompilationUnit, List<CommentInfo>> commentsMap;
|
||||
private final Lexer lexer;
|
||||
|
||||
protected CommentCollectingParser(ParserFactory fac, Lexer S,
|
||||
boolean keepDocComments, boolean keepLineMap, Map<JCCompilationUnit, List<Comment>> commentsMap) {
|
||||
boolean keepDocComments, boolean keepLineMap, Map<JCCompilationUnit, List<CommentInfo>> commentsMap) {
|
||||
super(fac, S, keepDocComments, keepLineMap);
|
||||
lexer = S;
|
||||
this.commentsMap = commentsMap;
|
||||
@@ -24,7 +24,7 @@ class CommentCollectingParser extends EndPosParser {
|
||||
public JCCompilationUnit parseCompilationUnit() {
|
||||
JCCompilationUnit result = super.parseCompilationUnit();
|
||||
if (lexer instanceof CommentCollectingScanner) {
|
||||
List<Comment> comments = ((CommentCollectingScanner)lexer).getComments();
|
||||
List<CommentInfo> comments = ((CommentCollectingScanner)lexer).getComments();
|
||||
commentsMap.put(result, comments);
|
||||
}
|
||||
return result;
|
||||
|
||||
@@ -4,7 +4,7 @@ import java.lang.reflect.Field;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import lombok.javac.Comment;
|
||||
import lombok.javac.CommentInfo;
|
||||
|
||||
import com.sun.tools.javac.main.JavaCompiler;
|
||||
import com.sun.tools.javac.parser.Lexer;
|
||||
@@ -15,14 +15,14 @@ import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
|
||||
import com.sun.tools.javac.util.Context;
|
||||
|
||||
public class CommentCollectingParserFactory extends ParserFactory {
|
||||
private final Map<JCCompilationUnit, List<Comment>> commentsMap;
|
||||
private final Map<JCCompilationUnit, List<CommentInfo>> commentsMap;
|
||||
private final Context context;
|
||||
|
||||
static Context.Key<ParserFactory> key() {
|
||||
return parserFactoryKey;
|
||||
}
|
||||
|
||||
protected CommentCollectingParserFactory(Context context, Map<JCCompilationUnit, List<Comment>> commentsMap) {
|
||||
protected CommentCollectingParserFactory(Context context, Map<JCCompilationUnit, List<CommentInfo>> commentsMap) {
|
||||
super(context);
|
||||
this.context = context;
|
||||
this.commentsMap = commentsMap;
|
||||
@@ -38,7 +38,7 @@ public class CommentCollectingParserFactory extends ParserFactory {
|
||||
//Either way this will work out.
|
||||
}
|
||||
|
||||
public static void setInCompiler(JavaCompiler compiler, Context context, Map<JCCompilationUnit, List<Comment>> commentsMap) {
|
||||
public static void setInCompiler(JavaCompiler compiler, Context context, Map<JCCompilationUnit, List<CommentInfo>> commentsMap) {
|
||||
context.put(CommentCollectingParserFactory.key(), (ParserFactory)null);
|
||||
Field field;
|
||||
try {
|
||||
|
||||
@@ -23,16 +23,16 @@ package lombok.javac.java7;
|
||||
|
||||
import java.nio.CharBuffer;
|
||||
|
||||
import lombok.javac.Comment;
|
||||
import lombok.javac.Comment.EndConnection;
|
||||
import lombok.javac.Comment.StartConnection;
|
||||
import lombok.javac.CommentInfo;
|
||||
import lombok.javac.CommentInfo.EndConnection;
|
||||
import lombok.javac.CommentInfo.StartConnection;
|
||||
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 ListBuffer<Comment> comments = ListBuffer.lb();
|
||||
private final ListBuffer<CommentInfo> comments = ListBuffer.lb();
|
||||
private int endComment = 0;
|
||||
|
||||
public CommentCollectingScanner(CommentCollectingScannerFactory factory, CharBuffer charBuffer) {
|
||||
@@ -53,14 +53,19 @@ public class CommentCollectingScanner extends Scanner {
|
||||
StartConnection start = determineStartConnection(prevEndPos, pos);
|
||||
EndConnection end = determineEndConnection(endPos);
|
||||
|
||||
Comment comment = new Comment(prevEndPos, pos, endPos, content, start, end);
|
||||
CommentInfo comment = new CommentInfo(prevEndPos, pos, endPos, content, start, end);
|
||||
comments.append(comment);
|
||||
}
|
||||
|
||||
private EndConnection determineEndConnection(int pos) {
|
||||
boolean first = true;
|
||||
for (int i = pos;; i++) {
|
||||
char c = getRawCharacters(i, i + 1)[0];
|
||||
char c;
|
||||
try {
|
||||
c = getRawCharacters(i, i + 1)[0];
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
c = '\n';
|
||||
}
|
||||
if (isNewLine(c)) {
|
||||
return EndConnection.ON_NEXT_LINE;
|
||||
}
|
||||
@@ -92,7 +97,7 @@ public class CommentCollectingScanner extends Scanner {
|
||||
return c == '\n' || c == '\r';
|
||||
}
|
||||
|
||||
public List<Comment> getComments() {
|
||||
public List<CommentInfo> getComments() {
|
||||
return comments.toList();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user