diff --git a/pmd/etc/changelog.txt b/pmd/etc/changelog.txt index 8754a968da..297234bff8 100644 --- a/pmd/etc/changelog.txt +++ b/pmd/etc/changelog.txt @@ -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 diff --git a/pmd/etc/grammar/Java.jjt b/pmd/etc/grammar/Java.jjt index 724f690a00..72192e808b 100644 --- a/pmd/etc/grammar/Java.jjt +++ b/pmd/etc/grammar/Java.jjt @@ -163,7 +163,7 @@ public class JavaParser { } PARSER_END(JavaParser) TOKEN_MGR_DECLS : { - protected List formalComments = new ArrayList(); + protected List comments = new ArrayList(); private Map excludeMap = new HashMap(); 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 : SPECIAL_TOKEN : { - { formalComments.add(matchedToken); } : DEFAULT + { comments.add(new FormalComment(matchedToken)); } : DEFAULT } SPECIAL_TOKEN : { - : DEFAULT + { comments.add(new MultiLineComment(matchedToken)); } : DEFAULT } @@ -1079,7 +1080,7 @@ ASTCompilationUnit CompilationUnit() : ( < "~[]" > )? { - jjtThis.setFormalComments(token_source.formalComments); + jjtThis.setComments(token_source.comments); return jjtThis; } } diff --git a/pmd/src/net/sourceforge/pmd/ast/ASTCompilationUnit.java b/pmd/src/net/sourceforge/pmd/ast/ASTCompilationUnit.java index aa1e0c9d6a..0e41c42704 100644 --- a/pmd/src/net/sourceforge/pmd/ast/ASTCompilationUnit.java +++ b/pmd/src/net/sourceforge/pmd/ast/ASTCompilationUnit.java @@ -18,14 +18,14 @@ public class ASTCompilationUnit extends SimpleJavaTypeNode implements Compilatio super(p, id); } - private List formalComments; + private List comments; - public List getFormalComments() { - return formalComments; + public List getComments() { + return comments; } - public void setFormalComments(List formalComments) { - this.formalComments = formalComments; + public void setComments(List comments) { + this.comments = comments; } /** diff --git a/pmd/src/net/sourceforge/pmd/ast/CharStream.java b/pmd/src/net/sourceforge/pmd/ast/CharStream.java index ae077c64d7..d5a1a76236 100644 --- a/pmd/src/net/sourceforge/pmd/ast/CharStream.java +++ b/pmd/src/net/sourceforge/pmd/ast/CharStream.java @@ -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) */ diff --git a/pmd/src/net/sourceforge/pmd/ast/Comment.java b/pmd/src/net/sourceforge/pmd/ast/Comment.java new file mode 100644 index 0000000000..60a9270acb --- /dev/null +++ b/pmd/src/net/sourceforge/pmd/ast/Comment.java @@ -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; + } + +} diff --git a/pmd/src/net/sourceforge/pmd/ast/FormalComment.java b/pmd/src/net/sourceforge/pmd/ast/FormalComment.java new file mode 100644 index 0000000000..9b89321490 --- /dev/null +++ b/pmd/src/net/sourceforge/pmd/ast/FormalComment.java @@ -0,0 +1,9 @@ +package net.sourceforge.pmd.ast; + +public class FormalComment extends Comment { + + public FormalComment(Token t) { + super(t); + } + +} diff --git a/pmd/src/net/sourceforge/pmd/ast/JJTJavaParserState.java b/pmd/src/net/sourceforge/pmd/ast/JJTJavaParserState.java index 02c9b6fb17..bc25936757 100644 --- a/pmd/src/net/sourceforge/pmd/ast/JJTJavaParserState.java +++ b/pmd/src/net/sourceforge/pmd/ast/JJTJavaParserState.java @@ -120,4 +120,4 @@ public class JJTJavaParserState { } } } -/* JavaCC - OriginalChecksum=8adeb8c34464efb76188cd58df068d02 (do not edit this line) */ +/* JavaCC - OriginalChecksum=1f85d5a954d2dafb00b08e69b12b9bd3 (do not edit this line) */ diff --git a/pmd/src/net/sourceforge/pmd/ast/JavaCharStream.java b/pmd/src/net/sourceforge/pmd/ast/JavaCharStream.java index 239ee4fa3b..d71b3ee56f 100644 --- a/pmd/src/net/sourceforge/pmd/ast/JavaCharStream.java +++ b/pmd/src/net/sourceforge/pmd/ast/JavaCharStream.java @@ -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) */ diff --git a/pmd/src/net/sourceforge/pmd/ast/JavaParser.java b/pmd/src/net/sourceforge/pmd/ast/JavaParser.java index 40ea12e1be..97e8ebd436 100644 --- a/pmd/src/net/sourceforge/pmd/ast/JavaParser.java +++ b/pmd/src/net/sourceforge/pmd/ast/JavaParser.java @@ -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. */ diff --git a/pmd/src/net/sourceforge/pmd/ast/JavaParserTokenManager.java b/pmd/src/net/sourceforge/pmd/ast/JavaParserTokenManager.java index e6979bf058..4e3da3ec2a 100644 --- a/pmd/src/net/sourceforge/pmd/ast/JavaParserTokenManager.java +++ b/pmd/src/net/sourceforge/pmd/ast/JavaParserTokenManager.java @@ -6,7 +6,7 @@ import net.sourceforge.pmd.PMD; /** Token Manager. */ public class JavaParserTokenManager implements JavaParserConstants { - protected List formalComments = new ArrayList(); + protected List comments = new ArrayList(); private Map excludeMap = new HashMap(); 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; diff --git a/pmd/src/net/sourceforge/pmd/ast/JavaParserTreeConstants.java b/pmd/src/net/sourceforge/pmd/ast/JavaParserTreeConstants.java index 648e64f117..bb68609eef 100644 --- a/pmd/src/net/sourceforge/pmd/ast/JavaParserTreeConstants.java +++ b/pmd/src/net/sourceforge/pmd/ast/JavaParserTreeConstants.java @@ -226,4 +226,4 @@ public interface JavaParserTreeConstants "DefaultValue", }; } -/* JavaCC - OriginalChecksum=820b9c854bfffdbd622ba16bbdc68ff2 (do not edit this line) */ +/* JavaCC - OriginalChecksum=28e68740207e50a892e7959c0ef43ae0 (do not edit this line) */ diff --git a/pmd/src/net/sourceforge/pmd/ast/JavaParserVisitor.java b/pmd/src/net/sourceforge/pmd/ast/JavaParserVisitor.java index 86d9e9d86b..53d2271d7c 100644 --- a/pmd/src/net/sourceforge/pmd/ast/JavaParserVisitor.java +++ b/pmd/src/net/sourceforge/pmd/ast/JavaParserVisitor.java @@ -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) */ diff --git a/pmd/src/net/sourceforge/pmd/ast/MultiLineComment.java b/pmd/src/net/sourceforge/pmd/ast/MultiLineComment.java new file mode 100644 index 0000000000..74f6c48089 --- /dev/null +++ b/pmd/src/net/sourceforge/pmd/ast/MultiLineComment.java @@ -0,0 +1,9 @@ +package net.sourceforge.pmd.ast; + +public class MultiLineComment extends Comment { + + public MultiLineComment(Token t) { + super(t); + } + +} diff --git a/pmd/src/net/sourceforge/pmd/ast/ParseException.java b/pmd/src/net/sourceforge/pmd/ast/ParseException.java index e88cbda564..a41dfdb72d 100644 --- a/pmd/src/net/sourceforge/pmd/ast/ParseException.java +++ b/pmd/src/net/sourceforge/pmd/ast/ParseException.java @@ -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) */ diff --git a/pmd/src/net/sourceforge/pmd/ast/SingleLineComment.java b/pmd/src/net/sourceforge/pmd/ast/SingleLineComment.java new file mode 100644 index 0000000000..4f3b838813 --- /dev/null +++ b/pmd/src/net/sourceforge/pmd/ast/SingleLineComment.java @@ -0,0 +1,9 @@ +package net.sourceforge.pmd.ast; + +public class SingleLineComment extends Comment { + + public SingleLineComment(Token t) { + super(t); + } + +} diff --git a/pmd/src/net/sourceforge/pmd/ast/Token.java b/pmd/src/net/sourceforge/pmd/ast/Token.java index d9fee280f1..305170f35b 100644 --- a/pmd/src/net/sourceforge/pmd/ast/Token.java +++ b/pmd/src/net/sourceforge/pmd/ast/Token.java @@ -132,4 +132,4 @@ public class Token { } } -/* JavaCC - OriginalChecksum=db0cc77f0ac78d4798a0b95cf8c2feee (do not edit this line) */ +/* JavaCC - OriginalChecksum=0fc45836ac24c23412a256e5ad480628 (do not edit this line) */ diff --git a/pmd/src/net/sourceforge/pmd/ast/TokenMgrError.java b/pmd/src/net/sourceforge/pmd/ast/TokenMgrError.java index 6ad621b5b7..d0bc37dc28 100644 --- a/pmd/src/net/sourceforge/pmd/ast/TokenMgrError.java +++ b/pmd/src/net/sourceforge/pmd/ast/TokenMgrError.java @@ -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) */ diff --git a/pmd/src/net/sourceforge/pmd/rules/imports/UnusedImportsRule.java b/pmd/src/net/sourceforge/pmd/rules/imports/UnusedImportsRule.java index d87c8b4c00..94455a6488 100644 --- a/pmd/src/net/sourceforge/pmd/rules/imports/UnusedImportsRule.java +++ b/pmd/src/net/sourceforge/pmd/rules/imports/UnusedImportsRule.java @@ -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 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));