我想在checkstyle.xml中添加自定义规则,以便可以在codacy仪表板中查看它们。我这样做时正面临以下问题。
我编写了一个自定义类,以检查未使用的私有方法。
类。它在android studio和
中显示实例化错误
checkstyle文档没有提供有关如何
实例化自定义规则类,并将其添加为模块。
Checklist.xml已使用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