Remove subclasses of Comment
This commit is contained in:
@ -18,6 +18,10 @@ import net.sourceforge.pmd.lang.ast.Node;
|
||||
*/
|
||||
public interface Reportable {
|
||||
|
||||
// todo add optional method to get the nearest node, to implement
|
||||
// suppression that depends on tree structure (eg annotations) for
|
||||
// not just nodes, for example, for comments or individual tokens
|
||||
|
||||
/**
|
||||
* Returns the location at which this element should be reported.
|
||||
*
|
||||
|
@ -545,7 +545,7 @@ SPECIAL_TOKEN :
|
||||
if (startOfNOPMD != -1) {
|
||||
suppressMap.put(matchedToken.getBeginLine(), matchedToken.getImage().substring(startOfNOPMD + suppressMarker.length()));
|
||||
}
|
||||
comments.add(new SingleLineComment(matchedToken));
|
||||
comments.add(new Comment(matchedToken));
|
||||
}
|
||||
}
|
||||
|
||||
@ -566,7 +566,7 @@ SPECIAL_TOKEN :
|
||||
<IN_MULTI_LINE_COMMENT>
|
||||
SPECIAL_TOKEN :
|
||||
{
|
||||
<MULTI_LINE_COMMENT: "*/" > { comments.add(new MultiLineComment(matchedToken)); } : DEFAULT
|
||||
<MULTI_LINE_COMMENT: "*/" > { comments.add(new Comment(matchedToken)); } : DEFAULT
|
||||
}
|
||||
|
||||
<IN_FORMAL_COMMENT,IN_MULTI_LINE_COMMENT>
|
||||
|
@ -4,8 +4,12 @@
|
||||
|
||||
package net.sourceforge.pmd.lang.java.ast;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import net.sourceforge.pmd.internal.util.IteratorUtil;
|
||||
import net.sourceforge.pmd.lang.ast.GenericToken;
|
||||
import net.sourceforge.pmd.lang.ast.impl.javacc.JavaccToken;
|
||||
import net.sourceforge.pmd.lang.ast.impl.javacc.JjtreeNode;
|
||||
import net.sourceforge.pmd.util.document.Chars;
|
||||
import net.sourceforge.pmd.util.document.FileLocation;
|
||||
import net.sourceforge.pmd.util.document.Reportable;
|
||||
@ -14,11 +18,9 @@ import net.sourceforge.pmd.util.document.Reportable;
|
||||
* Wraps a comment token to provide some utilities.
|
||||
* This is not a node, it's not part of the tree anywhere,
|
||||
* just convenient.
|
||||
*
|
||||
* TODO subclasses are useless
|
||||
* TODO maybe move part of this into pmd core
|
||||
*/
|
||||
public abstract class Comment implements Reportable {
|
||||
public class Comment implements Reportable {
|
||||
//TODO maybe move part of this into pmd core
|
||||
|
||||
private final JavaccToken token;
|
||||
|
||||
@ -106,7 +108,7 @@ public abstract class Comment implements Reportable {
|
||||
* Trim the start of the provided line to remove a comment
|
||||
* markup opener ({@code //, /*, /**, *}) or closer {@code * /}.
|
||||
*/
|
||||
public static Chars removeCommentMarkup(Chars line) {
|
||||
private static Chars removeCommentMarkup(Chars line) {
|
||||
line = line.trim().removeSuffix("*/");
|
||||
int subseqFrom = 0;
|
||||
if (line.startsWith('/', 0)) {
|
||||
@ -121,4 +123,16 @@ public abstract class Comment implements Reportable {
|
||||
}
|
||||
return line.subSequence(subseqFrom, line.length()).trim();
|
||||
}
|
||||
|
||||
private static Stream<JavaccToken> getSpecialCommentsIn(JjtreeNode<?> node) {
|
||||
return GenericToken.streamRange(node.getFirstToken(), node.getLastToken())
|
||||
.flatMap(it -> IteratorUtil.toStream(GenericToken.previousSpecials(it).iterator()));
|
||||
}
|
||||
|
||||
public static Stream<JavaccToken> getLeadingComments(JavaNode node) {
|
||||
if (node instanceof AccessNode) {
|
||||
node = ((AccessNode) node).getModifiers();
|
||||
}
|
||||
return getSpecialCommentsIn(node).filter(Comment::isComment);
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,9 @@ import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
|
||||
import net.sourceforge.pmd.lang.ast.impl.javacc.JavaccToken;
|
||||
|
||||
/**
|
||||
* A wrapper for Javadoc {@link Comment}s.
|
||||
*/
|
||||
public class FormalComment extends Comment {
|
||||
|
||||
private JavadocCommentOwner owner;
|
||||
|
@ -1,16 +0,0 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd.lang.java.ast;
|
||||
|
||||
import net.sourceforge.pmd.lang.ast.impl.javacc.JavaccToken;
|
||||
|
||||
public class MultiLineComment extends Comment {
|
||||
|
||||
public MultiLineComment(JavaccToken t) {
|
||||
super(t);
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd.lang.java.ast;
|
||||
|
||||
import net.sourceforge.pmd.lang.ast.impl.javacc.JavaccToken;
|
||||
|
||||
public class SingleLineComment extends Comment {
|
||||
|
||||
public SingleLineComment(JavaccToken t) {
|
||||
super(t);
|
||||
}
|
||||
|
||||
}
|
@ -8,8 +8,6 @@ import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import net.sourceforge.pmd.lang.ast.GenericToken;
|
||||
import net.sourceforge.pmd.lang.ast.impl.javacc.JavaccToken;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTAnnotationTypeDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTAnyTypeDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
|
||||
@ -17,7 +15,6 @@ import net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTEnumDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTModifierList;
|
||||
import net.sourceforge.pmd.lang.java.ast.AccessNode;
|
||||
import net.sourceforge.pmd.lang.java.ast.Comment;
|
||||
import net.sourceforge.pmd.lang.java.rule.AbstractIgnoredAnnotationRule;
|
||||
@ -124,16 +121,9 @@ public class CommentDefaultAccessModifierRule extends AbstractIgnoredAnnotationR
|
||||
}
|
||||
|
||||
private boolean hasOkComment(AccessNode node) {
|
||||
ASTModifierList modifiers = node.getModifiers();
|
||||
Pattern regex = getProperty(REGEX_DESCRIPTOR);
|
||||
for (JavaccToken token : GenericToken.range(modifiers.getFirstToken(), modifiers.getLastToken())) {
|
||||
for (JavaccToken special : GenericToken.previousSpecials(token)) {
|
||||
if (Comment.isComment(special)) {
|
||||
return regex.matcher(special.getImageCs()).matches();
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return Comment.getLeadingComments(node)
|
||||
.anyMatch(it -> regex.matcher(it.getImageCs()).matches());
|
||||
}
|
||||
|
||||
private boolean shouldReportTypeDeclaration(ASTAnyTypeDeclaration decl) {
|
||||
|
@ -86,8 +86,7 @@ public class CommentSizeRule extends AbstractJavaRule {
|
||||
|
||||
List<Integer> indices = new ArrayList<>();
|
||||
int i = 0;
|
||||
for (Chars line : comment.getText().lines()) {
|
||||
line = Comment.removeCommentMarkup(line);
|
||||
for (Chars line : comment.filteredLines()) {
|
||||
if (line.length() > maxLength) {
|
||||
indices.add(i);
|
||||
}
|
||||
|
@ -7,6 +7,8 @@ package net.sourceforge.pmd.lang.java.ast;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.instanceOf;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@ -30,10 +32,13 @@ public class CommentAssignmentTest extends BaseNonParserTest {
|
||||
|
||||
Comment comment = node.getComments().get(0);
|
||||
|
||||
assertThat(comment, instanceOf(MultiLineComment.class));
|
||||
assertFalse(comment.isSingleLine());
|
||||
assertFalse(comment.hasJavadocContent());
|
||||
assertEquals("multi line comment with blank lines", StringUtils.join(comment.filteredLines(), ' '));
|
||||
|
||||
comment = node.getComments().get(1);
|
||||
assertFalse(comment.isSingleLine());
|
||||
assertTrue(comment.hasJavadocContent());
|
||||
assertThat(comment, instanceOf(FormalComment.class));
|
||||
assertEquals("a formal comment with blank lines", StringUtils.join(comment.filteredLines(), ' '));
|
||||
}
|
||||
@ -51,7 +56,7 @@ public class CommentAssignmentTest extends BaseNonParserTest {
|
||||
+ " /** Comment 3 */\n"
|
||||
+ " public void method2() {}" + "}");
|
||||
|
||||
List<ASTMethodDeclaration> methods = node.findDescendantsOfType(ASTMethodDeclaration.class);
|
||||
List<ASTMethodDeclaration> methods = node.descendants(ASTMethodDeclaration.class).toList();
|
||||
assertCommentEquals(methods.get(0), "/** Comment 1 */");
|
||||
assertCommentEquals(methods.get(1), "/** Comment 3 */");
|
||||
}
|
||||
@ -69,7 +74,7 @@ public class CommentAssignmentTest extends BaseNonParserTest {
|
||||
+ " /** Comment 3 */\n"
|
||||
+ " public void method2() {}" + "}");
|
||||
|
||||
List<ASTMethodDeclaration> methods = node.findDescendantsOfType(ASTMethodDeclaration.class);
|
||||
List<ASTMethodDeclaration> methods = node.descendants(ASTMethodDeclaration.class).toList();
|
||||
assertCommentEquals(methods.get(0), "/** Comment 1 */");
|
||||
assertCommentEquals(methods.get(1), "/** Comment 2 */");
|
||||
}
|
||||
|
Reference in New Issue
Block a user