我想在 checkstyle.xml 中添加自定义规则,以便我可以在 codacy Dashboard 中查看它们。我在这样做时面临以下问题。
我编写了一个自定义类来检查类中未使用的私有方法
。它在 android studio 中显示实例化错误,并且
checkstyle 文档没有提供有关如何实例化自定义规则类并将其添加为模块的任何线索。Checklist.xml 正在配置 Codacy 仪表板,但未在仪表板本身上显示详细信息。
甚至 codacy 文档都说我们可以添加自定义 XML 规则,但我无法在其中添加自定义 XML 规则,并且从 Github 克隆的项目被识别为仅 java 项目。我在下面分享代码片段。
这是来自 android studio https://ibb.co/613PL9t的错误
这是 Checkstyle.xml
<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>
这是 UnusedPrivateMethodCheck.Java 类
public class UnusedPrivateMethodCheck
extends AbstractCheck {
/**
* Controls if checks skips serialization methods.
*/
private boolean mAllowSerializationMethods;
public int[] getDefaultTokens() {
return new int[]{
TokenTypes.METHOD_DEF,
};
}
@Override
public int[] getAcceptableTokens() {
return new int[0];
}
@Override
public int[] getRequiredTokens() {
return new int[0];
}
public String getErrorKey() {
return "unused.method";
}
public void setAllowSerializationMethods(boolean aFlag) {
mAllowSerializationMethods = aFlag;
}
public boolean mustCheckReferenceCount(DetailAST aAST) {
final DetailAST mods = aAST.findFirstToken(TokenTypes.MODIFIERS);
if ((mods == null)
|| (ScopeUtils.getScopeFromMods(mods) != Scope.PRIVATE)) {
return false;
}
return !mAllowSerializationMethods
|| !(isWriteObject(aAST) || isReadObject(aAST)
|| isWriteReplaceOrReadResolve(aAST));
}
/**
* Checks if a given method is writeObject().
*
* @param aAST method def to check
* @return true if this is a writeObject() definition
*/
private boolean isWriteObject(DetailAST aAST) {
// name is writeObject...
final DetailAST ident = aAST.findFirstToken(TokenTypes.IDENT);
if (!"writeObject".equals(ident.getText())) {
return false;
}
// returns void...
final DetailAST typeAST =
(DetailAST) aAST.findFirstToken(TokenTypes.TYPE).getFirstChild();
if (typeAST.getType() != TokenTypes.LITERAL_VOID) {
return false;
}
// should have one parameter...
final DetailAST params = aAST.findFirstToken(TokenTypes.PARAMETERS);
if (params == null || params.getChildCount() != 1) {
return false;
}
// and paramter's type should be java.io.ObjectOutputStream
final DetailAST type =
(DetailAST) ((DetailAST) params.getFirstChild())
.findFirstToken(TokenTypes.TYPE).getFirstChild();
final String typeName = FullIdent.createFullIdent(type).getText();
if (!"java.io.ObjectOutputStream".equals(typeName)
&& !"ObjectOutputStream".equals(typeName)) {
return false;
}
// and, finally, it should throws java.io.IOException
final DetailAST throwsAST =
aAST.findFirstToken(TokenTypes.LITERAL_THROWS);
if (throwsAST == null || throwsAST.getChildCount() != 1) {
return false;
}
final DetailAST expt = (DetailAST) throwsAST.getFirstChild();
final String exceptionName = FullIdent.createFullIdent(expt).getText();
if (!"java.io.IOException".equals(exceptionName)
&& !"IOException".equals(exceptionName)) {
return false;
}
return true;
}
/**
* Checks if a given method is readObject().
*
* @param aAST method def to check
* @return true if this is a readObject() definition
*/
private boolean isReadObject(DetailAST aAST) {
// name is readObject...
final DetailAST ident = aAST.findFirstToken(TokenTypes.IDENT);
if (!"readObject".equals(ident.getText())) {
return false;
}
// returns void...
final DetailAST typeAST =
(DetailAST) aAST.findFirstToken(TokenTypes.TYPE).getFirstChild();
if (typeAST.getType() != TokenTypes.LITERAL_VOID) {
return false;
}
// should have one parameter...
final DetailAST params = aAST.findFirstToken(TokenTypes.PARAMETERS);
if (params == null || params.getChildCount() != 1) {
return false;
}
// and paramter's type should be java.io.ObjectInputStream
final DetailAST type =
(DetailAST) ((DetailAST) params.getFirstChild())
.findFirstToken(TokenTypes.TYPE).getFirstChild();
final String typeName = FullIdent.createFullIdent(type).getText();
if (!"java.io.ObjectInputStream".equals(typeName)
&& !"ObjectInputStream".equals(typeName)) {
return false;
}
// and, finally, it should throws java.io.IOException
// and java.lang.ClassNotFoundException
final DetailAST throwsAST =
aAST.findFirstToken(TokenTypes.LITERAL_THROWS);
if (throwsAST == null || throwsAST.getChildCount() != 3) {
return false;
}
final DetailAST excpt1 = (DetailAST) throwsAST.getFirstChild();
final String exception1 = FullIdent.createFullIdent(excpt1).getText();
final String exception2 =
FullIdent.createFullIdent(throwsAST.getLastChild()).getText();
if (!"java.io.IOException".equals(exception1)
&& !"IOException".equals(exception1)
&& !"java.io.IOException".equals(exception2)
&& !"IOException".equals(exception2)
|| !"java.lang.ClassNotFoundException".equals(exception1)
&& !"ClassNotFoundException".equals(exception1)
&& !"java.lang.ClassNotFoundException".equals(exception2)
&& !"ClassNotFoundException".equals(exception2)) {
return false;
}
return true;
}
/**
* Checks if a given method is writeReplace() or readResolve().
*
* @param aAST method def to check
* @return true if this is a writeReplace() definition
*/
private boolean isWriteReplaceOrReadResolve(DetailAST aAST) {
// name is writeReplace or readResolve...
final DetailAST ident = aAST.findFirstToken(TokenTypes.IDENT);
if (!"writeReplace".equals(ident.getText())
&& !"readResolve".equals(ident.getText())) {
return false;
}
// returns Object...
final DetailAST typeAST =
(DetailAST) aAST.findFirstToken(TokenTypes.TYPE).getFirstChild();
if (typeAST.getType() != TokenTypes.DOT
&& typeAST.getType() != TokenTypes.IDENT) {
return false;
}
// should have no parameters...
final DetailAST params = aAST.findFirstToken(TokenTypes.PARAMETERS);
if (params != null && params.getChildCount() != 0) {
return false;
}
// and, finally, it should throws java.io.ObjectStreamException
final DetailAST throwsAST =
aAST.findFirstToken(TokenTypes.LITERAL_THROWS);
if (throwsAST == null || throwsAST.getChildCount() != 1) {
return false;
}
final DetailAST excpt = (DetailAST) throwsAST.getFirstChild();
final String exception = FullIdent.createFullIdent(excpt).getText();
if (!"java.io.ObjectStreamException".equals(exception)
&& !"ObjectStreamException".equals(exception)) {
return false;
}
return true;
}
}
为此,我收到类似的错误Unable to instantiate UnusedPrivateMethodCheckCheck