All comment types are now stored in ASTCompilationUnit, not just formal ones
git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/branches/pmd/4.2.x@6398 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
@ -13,6 +13,7 @@ Fixed bug 2002722 - false + in UseStringBufferForStringAppends
|
||||
Fixed bug 2056318 - False positive for AvoidInstantiatingObjectsInLoops
|
||||
Optimizations and false positive fixes in PreserveStackTrace
|
||||
@SuppressWarnings("all") disables all warnings
|
||||
All comment types are now stored in ASTCompilationUnit, not just formal ones
|
||||
|
||||
New rule:
|
||||
Basic ruleset: EmptyInitializer
|
||||
|
@ -163,7 +163,7 @@ public class JavaParser {
|
||||
}
|
||||
PARSER_END(JavaParser)
|
||||
TOKEN_MGR_DECLS : {
|
||||
protected List<Token> formalComments = new ArrayList<Token>();
|
||||
protected List<Comment> comments = new ArrayList<Comment>();
|
||||
|
||||
private Map<Integer, String> excludeMap = new HashMap<Integer, String>();
|
||||
private String excludeMarker = PMD.EXCLUDE_MARKER;
|
||||
@ -191,6 +191,7 @@ SPECIAL_TOKEN :
|
||||
if (startOfNOPMD != -1) {
|
||||
excludeMap.put(matchedToken.beginLine, matchedToken.image.substring(startOfNOPMD + excludeMarker.length()));
|
||||
}
|
||||
comments.add(new SingleLineComment(matchedToken));
|
||||
}
|
||||
}
|
||||
|
||||
@ -206,13 +207,13 @@ MORE :
|
||||
<IN_FORMAL_COMMENT>
|
||||
SPECIAL_TOKEN :
|
||||
{
|
||||
<FORMAL_COMMENT: "*/" > { formalComments.add(matchedToken); } : DEFAULT
|
||||
<FORMAL_COMMENT: "*/" > { comments.add(new FormalComment(matchedToken)); } : DEFAULT
|
||||
}
|
||||
|
||||
<IN_MULTI_LINE_COMMENT>
|
||||
SPECIAL_TOKEN :
|
||||
{
|
||||
<MULTI_LINE_COMMENT: "*/" > : DEFAULT
|
||||
<MULTI_LINE_COMMENT: "*/" > { comments.add(new MultiLineComment(matchedToken)); } : DEFAULT
|
||||
}
|
||||
|
||||
<IN_FORMAL_COMMENT,IN_MULTI_LINE_COMMENT>
|
||||
@ -1079,7 +1080,7 @@ ASTCompilationUnit CompilationUnit() :
|
||||
( < "~[]" > )?
|
||||
<EOF>
|
||||
{
|
||||
jjtThis.setFormalComments(token_source.formalComments);
|
||||
jjtThis.setComments(token_source.comments);
|
||||
return jjtThis;
|
||||
}
|
||||
}
|
||||
|
@ -18,14 +18,14 @@ public class ASTCompilationUnit extends SimpleJavaTypeNode implements Compilatio
|
||||
super(p, id);
|
||||
}
|
||||
|
||||
private List<Token> formalComments;
|
||||
private List<Comment> comments;
|
||||
|
||||
public List<Token> getFormalComments() {
|
||||
return formalComments;
|
||||
public List<Comment> getComments() {
|
||||
return comments;
|
||||
}
|
||||
|
||||
public void setFormalComments(List<Token> formalComments) {
|
||||
this.formalComments = formalComments;
|
||||
public void setComments(List<Comment> comments) {
|
||||
this.comments = comments;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -109,4 +109,4 @@ public interface CharStream {
|
||||
void Done();
|
||||
|
||||
}
|
||||
/* JavaCC - OriginalChecksum=de0ca20122e444997542dbba91045e99 (do not edit this line) */
|
||||
/* JavaCC - OriginalChecksum=553f0aaece436274c2d7e7cdfd4b996e (do not edit this line) */
|
||||
|
43
pmd/src/net/sourceforge/pmd/ast/Comment.java
Normal file
43
pmd/src/net/sourceforge/pmd/ast/Comment.java
Normal file
@ -0,0 +1,43 @@
|
||||
package net.sourceforge.pmd.ast;
|
||||
|
||||
public abstract class Comment {
|
||||
|
||||
private String image;
|
||||
|
||||
private int beginLine = -1;
|
||||
|
||||
private int endLine;
|
||||
|
||||
private int beginColumn = -1;
|
||||
|
||||
private int endColumn;
|
||||
|
||||
protected Comment(Token t) {
|
||||
beginLine = t.beginLine;
|
||||
endLine = t.endLine;
|
||||
beginColumn = t.beginColumn;
|
||||
endColumn = t.endColumn;
|
||||
image = t.image;
|
||||
}
|
||||
|
||||
public String getImage() {
|
||||
return image;
|
||||
}
|
||||
|
||||
public int getBeginLine() {
|
||||
return beginLine;
|
||||
}
|
||||
|
||||
public int getEndLine() {
|
||||
return endLine;
|
||||
}
|
||||
|
||||
public int getBeginColumn() {
|
||||
return beginColumn;
|
||||
}
|
||||
|
||||
public int getEndColumn() {
|
||||
return endColumn;
|
||||
}
|
||||
|
||||
}
|
9
pmd/src/net/sourceforge/pmd/ast/FormalComment.java
Normal file
9
pmd/src/net/sourceforge/pmd/ast/FormalComment.java
Normal file
@ -0,0 +1,9 @@
|
||||
package net.sourceforge.pmd.ast;
|
||||
|
||||
public class FormalComment extends Comment {
|
||||
|
||||
public FormalComment(Token t) {
|
||||
super(t);
|
||||
}
|
||||
|
||||
}
|
@ -120,4 +120,4 @@ public class JJTJavaParserState {
|
||||
}
|
||||
}
|
||||
}
|
||||
/* JavaCC - OriginalChecksum=8adeb8c34464efb76188cd58df068d02 (do not edit this line) */
|
||||
/* JavaCC - OriginalChecksum=1f85d5a954d2dafb00b08e69b12b9bd3 (do not edit this line) */
|
||||
|
@ -613,4 +613,4 @@ public class JavaCharStream implements CharStream
|
||||
}
|
||||
|
||||
}
|
||||
/* JavaCC - OriginalChecksum=99b1c5d858d902d189800309041d1af5 (do not edit this line) */
|
||||
/* JavaCC - OriginalChecksum=f33dd07933a433624439aa53c746df7b (do not edit this line) */
|
||||
|
@ -169,7 +169,7 @@ public class JavaParser/*@bgen(jjtree)*/implements JavaParserTreeConstants, Java
|
||||
jj_consume_token(0);
|
||||
jjtree.closeNodeScope(jjtn000, true);
|
||||
jjtc000 = false;
|
||||
jjtn000.setFormalComments(token_source.formalComments);
|
||||
jjtn000.setComments(token_source.comments);
|
||||
{if (true) return jjtn000;}
|
||||
} catch (Throwable jjte000) {
|
||||
if (jjtc000) {
|
||||
@ -6258,16 +6258,6 @@ jjtn000.setModifiers(modifiers);
|
||||
finally { jj_save(50, xla); }
|
||||
}
|
||||
|
||||
private boolean jj_3R_233() {
|
||||
if (jj_3R_254()) return true;
|
||||
Token xsp;
|
||||
while (true) {
|
||||
xsp = jj_scanpos;
|
||||
if (jj_3R_267()) { jj_scanpos = xsp; break; }
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean jj_3R_223() {
|
||||
if (jj_3R_233()) return true;
|
||||
Token xsp;
|
||||
@ -9341,6 +9331,16 @@ jjtn000.setModifiers(modifiers);
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean jj_3R_233() {
|
||||
if (jj_3R_254()) return true;
|
||||
Token xsp;
|
||||
while (true) {
|
||||
xsp = jj_scanpos;
|
||||
if (jj_3R_267()) { jj_scanpos = xsp; break; }
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/** Generated Token Manager. */
|
||||
public JavaParserTokenManager token_source;
|
||||
/** Current token. */
|
||||
|
@ -6,7 +6,7 @@ import net.sourceforge.pmd.PMD;
|
||||
/** Token Manager. */
|
||||
public class JavaParserTokenManager implements JavaParserConstants
|
||||
{
|
||||
protected List<Token> formalComments = new ArrayList<Token>();
|
||||
protected List<Comment> comments = new ArrayList<Comment>();
|
||||
|
||||
private Map<Integer, String> excludeMap = new HashMap<Integer, String>();
|
||||
private String excludeMarker = PMD.EXCLUDE_MARKER;
|
||||
@ -2052,10 +2052,15 @@ void SkipLexicalActions(Token matchedToken)
|
||||
if (startOfNOPMD != -1) {
|
||||
excludeMap.put(matchedToken.beginLine, matchedToken.image.substring(startOfNOPMD + excludeMarker.length()));
|
||||
}
|
||||
comments.add(new SingleLineComment(matchedToken));
|
||||
break;
|
||||
case 9 :
|
||||
image.append(input_stream.GetSuffix(jjimageLen + (lengthOfMatch = jjmatchedPos + 1)));
|
||||
formalComments.add(matchedToken);
|
||||
comments.add(new FormalComment(matchedToken));
|
||||
break;
|
||||
case 10 :
|
||||
image.append(input_stream.GetSuffix(jjimageLen + (lengthOfMatch = jjmatchedPos + 1)));
|
||||
comments.add(new MultiLineComment(matchedToken));
|
||||
break;
|
||||
default :
|
||||
break;
|
||||
|
@ -226,4 +226,4 @@ public interface JavaParserTreeConstants
|
||||
"DefaultValue",
|
||||
};
|
||||
}
|
||||
/* JavaCC - OriginalChecksum=820b9c854bfffdbd622ba16bbdc68ff2 (do not edit this line) */
|
||||
/* JavaCC - OriginalChecksum=28e68740207e50a892e7959c0ef43ae0 (do not edit this line) */
|
||||
|
@ -113,4 +113,4 @@ public interface JavaParserVisitor
|
||||
public Object visit(ASTAnnotationMethodDeclaration node, Object data);
|
||||
public Object visit(ASTDefaultValue node, Object data);
|
||||
}
|
||||
/* JavaCC - OriginalChecksum=6f9222a800daef876471227b5e8c50ac (do not edit this line) */
|
||||
/* JavaCC - OriginalChecksum=c64246870a5e6312a32af1f3a34598f9 (do not edit this line) */
|
||||
|
9
pmd/src/net/sourceforge/pmd/ast/MultiLineComment.java
Normal file
9
pmd/src/net/sourceforge/pmd/ast/MultiLineComment.java
Normal file
@ -0,0 +1,9 @@
|
||||
package net.sourceforge.pmd.ast;
|
||||
|
||||
public class MultiLineComment extends Comment {
|
||||
|
||||
public MultiLineComment(Token t) {
|
||||
super(t);
|
||||
}
|
||||
|
||||
}
|
@ -195,4 +195,4 @@ public class ParseException extends RuntimeException {
|
||||
}
|
||||
|
||||
}
|
||||
/* JavaCC - OriginalChecksum=b4f9afb6a58b85d3a4d2bc9b198f3fe6 (do not edit this line) */
|
||||
/* JavaCC - OriginalChecksum=5bf0bfb617836a97bfd12d32d6bb6f27 (do not edit this line) */
|
||||
|
9
pmd/src/net/sourceforge/pmd/ast/SingleLineComment.java
Normal file
9
pmd/src/net/sourceforge/pmd/ast/SingleLineComment.java
Normal file
@ -0,0 +1,9 @@
|
||||
package net.sourceforge.pmd.ast;
|
||||
|
||||
public class SingleLineComment extends Comment {
|
||||
|
||||
public SingleLineComment(Token t) {
|
||||
super(t);
|
||||
}
|
||||
|
||||
}
|
@ -132,4 +132,4 @@ public class Token {
|
||||
}
|
||||
|
||||
}
|
||||
/* JavaCC - OriginalChecksum=db0cc77f0ac78d4798a0b95cf8c2feee (do not edit this line) */
|
||||
/* JavaCC - OriginalChecksum=0fc45836ac24c23412a256e5ad480628 (do not edit this line) */
|
||||
|
@ -138,4 +138,4 @@ public class TokenMgrError extends RuntimeException
|
||||
this(LexicalError(EOFSeen, lexState, errorLine, errorColumn, errorAfter, curChar), reason);
|
||||
}
|
||||
}
|
||||
/* JavaCC - OriginalChecksum=c88f657fc12e6e46d29d52591d929281 (do not edit this line) */
|
||||
/* JavaCC - OriginalChecksum=d33733d858767f2c00f2ebf3856808a6 (do not edit this line) */
|
||||
|
@ -8,13 +8,13 @@ import net.sourceforge.pmd.ast.ASTClassOrInterfaceType;
|
||||
import net.sourceforge.pmd.ast.ASTCompilationUnit;
|
||||
import net.sourceforge.pmd.ast.ASTImportDeclaration;
|
||||
import net.sourceforge.pmd.ast.ASTName;
|
||||
import net.sourceforge.pmd.ast.Comment;
|
||||
import net.sourceforge.pmd.ast.FormalComment;
|
||||
import net.sourceforge.pmd.ast.SimpleJavaNode;
|
||||
import net.sourceforge.pmd.ast.SimpleNode;
|
||||
import net.sourceforge.pmd.ast.Token;
|
||||
import net.sourceforge.pmd.rules.ImportWrapper;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
@ -26,7 +26,7 @@ public class UnusedImportsRule extends AbstractRule {
|
||||
public Object visit(ASTCompilationUnit node, Object data) {
|
||||
imports.clear();
|
||||
super.visit(node, data);
|
||||
visitFormalComments(node);
|
||||
visitComments(node);
|
||||
for (ImportWrapper wrapper : imports) {
|
||||
addViolation(data, wrapper.getNode(), wrapper.getFullName());
|
||||
}
|
||||
@ -52,14 +52,16 @@ public class UnusedImportsRule extends AbstractRule {
|
||||
|
||||
private static final Pattern[] PATTERNS = { SEE_PATTERN, LINK_PATTERNS, VALUE_PATTERN };
|
||||
|
||||
private void visitFormalComments(ASTCompilationUnit node) {
|
||||
private void visitComments(ASTCompilationUnit node) {
|
||||
if (imports.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
List<Token> formals = node.getFormalComments();
|
||||
for (Token formal: formals) {
|
||||
for (Comment comment: node.getComments()) {
|
||||
if (!(comment instanceof FormalComment)) {
|
||||
continue;
|
||||
}
|
||||
for (Pattern p: PATTERNS) {
|
||||
Matcher m = p.matcher(formal.image);
|
||||
Matcher m = p.matcher(comment.getImage());
|
||||
while (m.find()) {
|
||||
String s = m.group(1);
|
||||
ImportWrapper candidate = new ImportWrapper(s, s, new SimpleJavaNode(-1));
|
||||
|
Reference in New Issue
Block a user