Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

两个检测的实现代码,放在这里,因为自己做,没按照标准来 #535

Closed
leonard99559 opened this issue Jul 1, 2019 · 3 comments

Comments

@leonard99559
Copy link

leonard99559 commented Jul 1, 2019

1.列不可超过120

public class ColumnTooLongRule extends AbstractAliRule {

    private static final int MAX_COLUMN_COUNT = 120;


    @Override
    public Object visit(ASTCompilationUnit node, Object data) {
        try {
            int endLine = node.getEndLine();
            if (endLine <= 0) {
                return super.visit(node, data);
            }
            List<Node> childrenNodes = node.findChildNodesWithXPath("//*[@EndColumn > 120]");
            Map<Integer, Node> columnKeyWithNodeValue = this.newHashMap(endLine);
            for (Node childNode : childrenNodes) {
                int endColumn = childNode.getEndColumn();
                if (columnKeyWithNodeValue.containsKey(endColumn)) {
                    continue;
                }
                if (endColumn <= MAX_COLUMN_COUNT) {
                    continue;
                }
                addViolationWithMessage(data, childNode, "java.other.ColumnTooLongRule.rule.msg");
                columnKeyWithNodeValue.put(endColumn, childNode);
            }
        } catch (JaxenException e) {
            e.printStackTrace();
        }
        return super.visit(node, data);
    }

    private <K, V> HashMap<K, V> newHashMap(int size) {
        int capacity = (int)(size/0.75F) + 1;
        return new HashMap<>(capacity);
    }
}
  1. 可变参数定义
public class VarargsParameterRule extends AbstractXpathRule {
    private static final String XPATH = "//FormalParameter[@Varargs = 'true' and ./Type[@TypeImage = 'Object']]";

    public VarargsParameterRule() {
        setXPath(XPATH);
    }

    @Override
    public void addViolation(Object data, Node node, String arg) {
        ViolationUtils.addViolationWithPrecisePosition(this, node, data,
                I18nResources.getMessage("java.other.VarargsParameterRule.rule.msg",
                        node.getImage()));
    }

}
@leonard99559
Copy link
Author

leonard99559 commented Jul 2, 2019

再添加一个equals和hashCode必须同时override

   @Override
    public Object visit(ASTCompilationUnit rootNode, Object data) {

        try {
            List<Node> nodeList = rootNode.findChildNodesWithXPath(
                    "//ClassOrInterfaceBodyDeclaration[./Annotation[@AnnotationName = 'Override']]" +
                            "//MethodDeclaration[@MethodName = 'hashCode' or @MethodName = 'equals']");
            if (nodeList.size() == 1) {
                addViolationWithMessage(data, nodeList.get(0),
                        "vivo.java.other.EqualsHashCodeRule.rule.msg");
            }
        } catch (JaxenException e) {
            e.printStackTrace();
        }
        return super.visit(rootNode, data);
    }

@Yuyang105
Copy link
Contributor

谢谢,第一条规则暂不适合进行扫描,第二三条,请走PR流程。

@leonard99559
Copy link
Author

#592 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants