diff --git a/pmd/etc/changelog.txt b/pmd/etc/changelog.txt index cbfe7008cc..f8a9fe0eb7 100644 --- a/pmd/etc/changelog.txt +++ b/pmd/etc/changelog.txt @@ -1,6 +1,7 @@ ???? ??, 2012 - 5.0.2: Fixed bug 1044: Unknown option: -excludemarker +Fixed bug 1048: CommentContent Rule, String Index out of range Exception November 28, 2012 - 5.0.1: diff --git a/pmd/src/main/java/net/sourceforge/pmd/lang/java/rule/comments/AbstractCommentRule.java b/pmd/src/main/java/net/sourceforge/pmd/lang/java/rule/comments/AbstractCommentRule.java index eac18d15cf..795fcdd045 100644 --- a/pmd/src/main/java/net/sourceforge/pmd/lang/java/rule/comments/AbstractCommentRule.java +++ b/pmd/src/main/java/net/sourceforge/pmd/lang/java/rule/comments/AbstractCommentRule.java @@ -1,3 +1,6 @@ +/** + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ package net.sourceforge.pmd.lang.java.rule.comments; import java.util.ArrayList; @@ -95,7 +98,7 @@ public abstract class AbstractCommentRule extends AbstractJavaRule { continue; } - if (line.charAt(0) == '*') { + if (line.length() > 0 && line.charAt(0) == '*') { filteredLines.add(line.substring(1)); continue; } @@ -122,7 +125,7 @@ public abstract class AbstractCommentRule extends AbstractJavaRule { continue; } - if (line.charAt(0) == '*') { + if (line.length() > 0 && line.charAt(0) == '*') { filteredLines.add(line.substring(1)); continue; } diff --git a/pmd/src/test/java/net/sourceforge/pmd/lang/java/rule/comments/AbstractCommentRuleTest.java b/pmd/src/test/java/net/sourceforge/pmd/lang/java/rule/comments/AbstractCommentRuleTest.java new file mode 100644 index 0000000000..2e53c62a22 --- /dev/null +++ b/pmd/src/test/java/net/sourceforge/pmd/lang/java/rule/comments/AbstractCommentRuleTest.java @@ -0,0 +1,34 @@ +/** + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ +package net.sourceforge.pmd.lang.java.rule.comments; + +import static org.junit.Assert.assertEquals; +import net.sourceforge.pmd.lang.java.ast.FormalComment; +import net.sourceforge.pmd.lang.java.ast.MultiLineComment; +import net.sourceforge.pmd.lang.java.ast.Token; + +import org.junit.Test; + +public class AbstractCommentRuleTest { + + private AbstractCommentRule testSubject = new AbstractCommentRule() {}; + + /** + * Blank lines in comments should not raise an exception. + * See bug #1048. + */ + @Test + public void testFilteredCommentIn() { + Token token = new Token(); + token.image = "/* multi line comment with blank lines\n\n\n */"; + + String filtered = testSubject.filteredCommentIn(new MultiLineComment(token)); + assertEquals("multi line comment with blank lines", filtered); + + token.image = "/** a formal comment with blank lines\n\n\n */"; + filtered = testSubject.filteredCommentIn(new FormalComment(token)); + assertEquals("a formal comment with blank lines", filtered); + } + +}