Skip to content

Commit

Permalink
Merge pull request alibaba#138 from kerie/master
Browse files Browse the repository at this point in the history
1.author标签不区分大小写,2.在有注解的情况下正确关联注释与节点
  • Loading branch information
SeanCai authored Oct 26, 2017
2 parents 2a24e02 + fd58312 commit d9792ef
Show file tree
Hide file tree
Showing 8 changed files with 270 additions and 148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import java.util.TreeMap;

import com.alibaba.p3c.pmd.I18nResources;
import com.alibaba.p3c.pmd.lang.java.rule.util.CommentUtils;
import com.alibaba.p3c.pmd.lang.java.rule.util.NodeSortUtils;

import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
Expand Down Expand Up @@ -75,19 +75,19 @@ protected SortedMap<Integer, Node> orderedCommentsAndExpressions(ASTCompilationU

// expression nodes
List<ASTExpression> expressionNodes = cUnit.findDescendantsOfType(ASTExpression.class);
CommentUtils.addNodesToSortedMap(itemsByLineNumber, expressionNodes);
NodeSortUtils.addNodesToSortedMap(itemsByLineNumber, expressionNodes);

// filed declaration nodes
List<ASTFieldDeclaration> fieldNodes =
cUnit.findDescendantsOfType(ASTFieldDeclaration.class);
CommentUtils.addNodesToSortedMap(itemsByLineNumber, fieldNodes);
NodeSortUtils.addNodesToSortedMap(itemsByLineNumber, fieldNodes);

// enum constant nodes
List<ASTEnumConstant> enumConstantNodes =
cUnit.findDescendantsOfType(ASTEnumConstant.class);
CommentUtils.addNodesToSortedMap(itemsByLineNumber, enumConstantNodes);
NodeSortUtils.addNodesToSortedMap(itemsByLineNumber, enumConstantNodes);

CommentUtils.addNodesToSortedMap(itemsByLineNumber, cUnit.getComments());
NodeSortUtils.addNodesToSortedMap(itemsByLineNumber, cUnit.getComments());

return itemsByLineNumber;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
*/
public class ClassMustHaveAuthorRule extends AbstractAliCommentRule {

private static final Pattern AUTHOR_PATTERN = Pattern.compile(".*@author.*", Pattern.DOTALL);
private static final Pattern AUTHOR_PATTERN = Pattern.compile(".*@author.*",
Pattern.DOTALL | Pattern.CASE_INSENSITIVE);

private static final String MESSAGE_KEY_PREFIX = "java.comment.ClassMustHaveAuthorRule.violation.msg";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@
package com.alibaba.p3c.pmd.lang.java.rule.comment;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.SortedMap;
import java.util.TreeMap;

import com.alibaba.p3c.pmd.I18nResources;
import com.alibaba.p3c.pmd.lang.java.rule.util.CommentUtils;
import com.alibaba.p3c.pmd.lang.java.rule.util.NodeSortUtils;

import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.java.ast.ASTAnnotation;
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
Expand All @@ -43,7 +45,7 @@

/**
* [Mandatory] Javadoc should be used for classes, class variables and methods.
* The format should be '\/** comment **\/', rather than '// xxx'.
* The format should be '\/** comment *\/', rather than '// xxx'.
*
* @author keriezhang
* @date 2016/12/14
Expand Down Expand Up @@ -155,8 +157,13 @@ protected void assignCommentsToDeclarations(ASTCompilationUnit cUnit) {
if (value instanceof AbstractJavaNode) {
AbstractJavaNode node = (AbstractJavaNode)value;

// skip annotation node, we will deal with it later.
if (node instanceof ASTAnnotation) {
continue;
}

// Check if comment is one line above class, field, method.
if (lastComment != null && isCommentOneLineBefore(lastComment, lastNode, node)) {
if (lastComment != null && isCommentOneLineBefore(itemsByLineNumber, lastComment, lastNode, node)) {
node.comment(lastComment);
lastComment = null;
}
Expand All @@ -172,28 +179,31 @@ protected SortedMap<Integer, Node> orderedComments(ASTCompilationUnit cUnit) {

SortedMap<Integer, Node> itemsByLineNumber = new TreeMap<>();

CommentUtils.addNodesToSortedMap(itemsByLineNumber, cUnit.getComments());
NodeSortUtils.addNodesToSortedMap(itemsByLineNumber, cUnit.getComments());

List<ASTAnnotation> annotations = cUnit.findDescendantsOfType(ASTAnnotation.class);
NodeSortUtils.addNodesToSortedMap(itemsByLineNumber, annotations);

List<ASTClassOrInterfaceDeclaration> classDecl =
cUnit.findDescendantsOfType(ASTClassOrInterfaceDeclaration.class);
CommentUtils.addNodesToSortedMap(itemsByLineNumber, classDecl);
NodeSortUtils.addNodesToSortedMap(itemsByLineNumber, classDecl);

List<ASTFieldDeclaration> fields = cUnit.findDescendantsOfType(ASTFieldDeclaration.class);
CommentUtils.addNodesToSortedMap(itemsByLineNumber, fields);
NodeSortUtils.addNodesToSortedMap(itemsByLineNumber, fields);

List<ASTMethodDeclaration> methods = cUnit.findDescendantsOfType(ASTMethodDeclaration.class);
CommentUtils.addNodesToSortedMap(itemsByLineNumber, methods);
NodeSortUtils.addNodesToSortedMap(itemsByLineNumber, methods);

List<ASTConstructorDeclaration> constructors = cUnit.findDescendantsOfType(ASTConstructorDeclaration.class);
CommentUtils.addNodesToSortedMap(itemsByLineNumber, constructors);
NodeSortUtils.addNodesToSortedMap(itemsByLineNumber, constructors);

List<ASTEnumDeclaration> enumDecl = cUnit.findDescendantsOfType(ASTEnumDeclaration.class);
CommentUtils.addNodesToSortedMap(itemsByLineNumber, enumDecl);
NodeSortUtils.addNodesToSortedMap(itemsByLineNumber, enumDecl);

return itemsByLineNumber;
}

private boolean isCommentOneLineBefore(Comment lastComment, Node lastNode, Node node) {
private boolean isCommentOneLineBefore(SortedMap<Integer, Node> items, Comment lastComment, Node lastNode, Node node) {
ASTClassOrInterfaceBodyDeclaration parentClass =
node.getFirstParentOfType(ASTClassOrInterfaceBodyDeclaration.class);

Expand All @@ -207,7 +217,31 @@ private boolean isCommentOneLineBefore(Comment lastComment, Node lastNode, Node
return false;
}

return lastComment.getEndLine() + 1 == node.getBeginLine();
// check if there is nothing in the middle except annotations.
SortedMap<Integer, Node> subMap = items.subMap(NodeSortUtils.generateIndex(lastComment),
NodeSortUtils.generateIndex(node));
Iterator<Entry<Integer, Node>> iter = subMap.entrySet().iterator();

// skip the first comment node.
iter.next();
int lastEndLine = lastComment.getEndLine();

while (iter.hasNext()) {
Entry<Integer, Node> entry = iter.next();
Node value = entry.getValue();

// only annotation node is allowed between comment and node.
if (!(value instanceof ASTAnnotation)) {
return false;
}

// allow annotation node after comment.
if (lastEndLine + 1 == value.getBeginLine()) {
lastEndLine = value.getEndLine();
}
}

return lastEndLine + 1 == node.getBeginLine();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import java.util.TreeMap;

import com.alibaba.p3c.pmd.I18nResources;
import com.alibaba.p3c.pmd.lang.java.rule.util.CommentUtils;
import com.alibaba.p3c.pmd.lang.java.rule.util.NodeSortUtils;

import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
Expand Down Expand Up @@ -65,12 +65,12 @@ private SortedMap<Integer, Node> orderedCommentsAndEnumDeclarations(ASTCompilati
SortedMap<Integer, Node> itemsByLineNumber = new TreeMap<>();

List<ASTEnumDeclaration> enumDecl = cUnit.findDescendantsOfType(ASTEnumDeclaration.class);
CommentUtils.addNodesToSortedMap(itemsByLineNumber, enumDecl);
NodeSortUtils.addNodesToSortedMap(itemsByLineNumber, enumDecl);

List<ASTEnumConstant> contantDecl = cUnit.findDescendantsOfType(ASTEnumConstant.class);
CommentUtils.addNodesToSortedMap(itemsByLineNumber, contantDecl);
NodeSortUtils.addNodesToSortedMap(itemsByLineNumber, contantDecl);

CommentUtils.addNodesToSortedMap(itemsByLineNumber, cUnit.getComments());
NodeSortUtils.addNodesToSortedMap(itemsByLineNumber, cUnit.getComments());

return itemsByLineNumber;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import java.util.TreeMap;
import java.util.regex.Pattern;

import com.alibaba.p3c.pmd.lang.java.rule.util.CommentUtils;
import com.alibaba.p3c.pmd.lang.java.rule.util.NodeSortUtils;

import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.java.ast.ASTBlockStatement;
Expand Down Expand Up @@ -144,25 +144,25 @@ protected SortedMap<Integer, Node> orderedCommentsAndDeclarations(ASTCompilation

List<ASTImportDeclaration> importDecl = cUnit
.findDescendantsOfType(ASTImportDeclaration.class);
CommentUtils.addNodesToSortedMap(itemsByLineNumber, importDecl);
NodeSortUtils.addNodesToSortedMap(itemsByLineNumber, importDecl);

List<ASTClassOrInterfaceDeclaration> classDecl = cUnit
.findDescendantsOfType(ASTClassOrInterfaceDeclaration.class);
CommentUtils.addNodesToSortedMap(itemsByLineNumber, classDecl);
NodeSortUtils.addNodesToSortedMap(itemsByLineNumber, classDecl);

List<ASTFieldDeclaration> fields = cUnit.findDescendantsOfType(ASTFieldDeclaration.class);
CommentUtils.addNodesToSortedMap(itemsByLineNumber, fields);
NodeSortUtils.addNodesToSortedMap(itemsByLineNumber, fields);

List<ASTMethodDeclaration> methods = cUnit.findDescendantsOfType(ASTMethodDeclaration.class);
CommentUtils.addNodesToSortedMap(itemsByLineNumber, methods);
NodeSortUtils.addNodesToSortedMap(itemsByLineNumber, methods);

List<ASTConstructorDeclaration> constructors = cUnit.findDescendantsOfType(ASTConstructorDeclaration.class);
CommentUtils.addNodesToSortedMap(itemsByLineNumber, constructors);
NodeSortUtils.addNodesToSortedMap(itemsByLineNumber, constructors);

List<ASTBlockStatement> blockStatements = cUnit.findDescendantsOfType(ASTBlockStatement.class);
CommentUtils.addNodesToSortedMap(itemsByLineNumber, blockStatements);
NodeSortUtils.addNodesToSortedMap(itemsByLineNumber, blockStatements);

CommentUtils.addNodesToSortedMap(itemsByLineNumber, cUnit.getComments());
NodeSortUtils.addNodesToSortedMap(itemsByLineNumber, cUnit.getComments());

return itemsByLineNumber;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* @date 2016/11/21
*
*/
public class CommentUtils {
public class NodeSortUtils {

/**
* add node to SortedMap with sequence to determine comment location
Expand All @@ -36,8 +36,16 @@ public class CommentUtils {
*/
public static void addNodesToSortedMap(SortedMap<Integer, Node> map, List<? extends Node> nodes) {
for (Node node : nodes) {
// sorted by line and column
map.put((node.getBeginLine() << 16) + node.getBeginColumn(), node);
map.put(generateIndex(node), node);
}
}

/**
* set order according to node begin line and begin column
* @param node node to sort
* @return generated index
*/
public static int generateIndex(Node node) {
return (node.getBeginLine() << 16) + node.getBeginColumn();
}
}
Loading

0 comments on commit d9792ef

Please sign in to comment.