-2

我想在 checkstyle 中添加自定义规则,以便我可以在 codacy Dashboard 中查看它们。但它也没有显示在 codacy 仪表板中,并且在 android studio 中也出现错误,请在下面找到详细信息。我编写了一个自定义类,它扩展了 Check 类,但无法在 checkstyle.xml 中添加该类。它显示在此处输入图像描述错误无法实例化该类。

<?xml version="1.0"?><!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.2//EN"
"http://www.puppycrawl.com/dtds/configuration_1_2.dtd">

<module name="Checker">
<module name="NewlineAtEndOfFile" />
<module name="FileLength" />
<module name="FileTabCharacter" />
<module name="TreeWalker">
    <module name="packageName.MethodCallWithoutObjectCreation" />
    <module name="ArrayTypeStyle" />
    <module name="UpperEll" />
    <module name="Indentation">
        <property name="caseIndent" value="4" />
    </module>
</module>

上面提到的模块可以在下面作为 java 类 MethodCallWithoutObjectCreation.java 找到

public class MethodCallWithoutObjectCreation extends Check {

    @Override
    public int[] getDefaultTokens() {
            return new int[]{TokenTypes.METHOD_DEF};
    }

    /**
     * @see com.puppycrawl.tools.checkstyle.api.Check#visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST)
     */
    @Override
    public void visitToken(DetailAST aast) {
            super.visitToken(aast);
            DetailAST slist = aast.findFirstToken(TokenTypes.SLIST);
            List<DetailAST> variables = DetailASTUtil.getDetailASTsForTypeInBranch(
                    slist, TokenTypes.VARIABLE_DEF);
            List<DetailAST> uninitializedVars = new ArrayList<DetailAST>();
            for (DetailAST variable : variables) {
                    boolean initialized = JavaClassUtil
                            .isVariableInitialized(variable);
                    if (!initialized)
                            uninitializedVars.add(variable);
            }

            for (DetailAST uninitializedVar : uninitializedVars) {
                    String uninitializedVarName = uninitializedVar.findFirstToken(
                            TokenTypes.IDENT).getText();
                    List<DetailAST> siblingsBelow = DetailASTUtil
                            .getAllSiblingsBelow(uninitializedVar);
                    List<FullIdentComparable> methodCallsFullIdents = new ArrayList<FullIdentComparable>();
                    List<FullIdentComparable> assignmentFullIdents = new ArrayList<FullIdentComparable>();
                    for (DetailAST siblingBelow : siblingsBelow) {
                            getListOfMethodCallsOnVariable(uninitializedVarName,
                                    siblingBelow, methodCallsFullIdents);
                            getListOfVariableAssignment(uninitializedVarName, siblingBelow,
                                    assignmentFullIdents);
                    }
                    Collections.sort(methodCallsFullIdents);
                    Collections.sort(assignmentFullIdents);
                    FullIdentComparable assignFullIdent = null;
                    if (assignmentFullIdents.size() > 0) {
                            assignFullIdent = assignmentFullIdents.get(0);
                    }

                    FullIdentComparable methodCallFullIdent = null;
                    if (methodCallsFullIdents.size() > 0)
                            methodCallFullIdent = methodCallsFullIdents.get(0);
                    if (methodCallFullIdent != null
                            && (methodCallFullIdent.compareTo(assignFullIdent) < 0))
                            log(uninitializedVar, "Method called without checking null "
                                    + methodCallFullIdent.getFullIdent().getText());
            }
    }

    private void getListOfMethodCallsOnVariable(String uninitializedVarName,
                                                DetailAST siblingBelow,
                                                List<FullIdentComparable> methodCallsFullIdents) {
            List<DetailAST> dotsInBranch = DetailASTUtil
                    .getDetailASTsForTypeInBranch(siblingBelow, TokenTypes.DOT);
            for (DetailAST dotInBranch : dotsInBranch) {
                    List<DetailAST> dotChildren = DetailASTUtil
                            .getDetailASTsForTypeInChildren(dotInBranch,
                                    TokenTypes.IDENT);
                    if (dotChildren != null) {
                            DetailAST dotChild = dotChildren.get(0);
                            if (dotChild.getText().equals(uninitializedVarName)) {
                                    FullIdent fullIdent = FullIdent
                                            .createFullIdent(dotInBranch);
                                    methodCallsFullIdents
                                            .add(new FullIdentComparable(fullIdent));
                            }
                    }
            }
    }

    private void getListOfVariableAssignment(String uninitializedVarName,
                                             DetailAST siblingBelow,
                                             List<FullIdentComparable> assignmentFullIdents) {
            List<DetailAST> assignmentsInBranch = DetailASTUtil
                    .getDetailASTsForTypeInBranch(siblingBelow, TokenTypes.ASSIGN);
            for (DetailAST assignInBranch : assignmentsInBranch) {
                    DetailAST ident = assignInBranch.findFirstToken(TokenTypes.IDENT);
                    if (ident != null) {
                            if (ident.getText().equals(uninitializedVarName)) {
                                    FullIdent fullIdent = FullIdent
                                            .createFullIdent(assignInBranch);
                                    assignmentFullIdents
                                            .add(new FullIdentComparable(fullIdent));
                            }
                    }
            }
    }

}

4

1 回答 1

1

目前 Codacy 不支持使用 checkstyle 为 xml 和 java 自定义规则,但您可以自定义它们的预定义规则,这些规则列在 Codacy Dashboard 的代码模式部分中

于 2019-03-13T06:59:02.100 回答