无法在android studio的checkstyle.xml中为Codacy添加自定义规则
我想在checkstyle.xml中添加自定义规则,以便在codacy仪表板中查看它们。在这样做时,我面临以下问题无法在android studio的checkstyle.xml中为Codacy添加自定义规则,android,xml,android-studio-3.0,checkstyle,codacy,Android,Xml,Android Studio 3.0,Checkstyle,Codacy,我想在checkstyle.xml中添加自定义规则,以便在codacy仪表板中查看它们。在这样做时,我面临以下问题 我已经编写了一个自定义类来检查a 上课。它在android studio中显示实例化错误, checkstyle文档没有提供有关如何 实例化自定义规则类并将其添加为模块 正在使用Codacy仪表板配置Checklist.xml,但未显示 仪表板本身的详细信息 即使是codacy文档也说我们可以添加自定义XML规则,但我不这么认为 无法为其中的XML和从Github克隆的项目添加自定
上课。它在android studio中显示实例化错误,
checkstyle文档没有提供有关如何 实例化自定义规则类并将其添加为模块
<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>
}
为此,我遇到了一个错误,比如
无法实例化UnusedPrivateMethodCheckCheckCheck
您的屏幕截图显示的代码与粘贴的代码不同<代码>检查和抽象用法检查
不是最新检查样式的类。我建议使用最新版本的checkstyle。如果您需要旧版本checkstyle的支持,请指定您正在使用的版本。另外,请复制/粘贴无法实例化的错误消息的全文。如果我想起来,codacy不支持自定义规则correctly@rveach感谢您的评论,因为它确实帮助我深入了解了Checkstyle结构,并且根据您的建议,我已经替换了Checkstyle最新版本8.1中不推荐的类并替换了屏幕截图,其中清楚地显示了无法实例化的错误message@pedrorijo91我关注codacy文档,其中提到可以使用不同的插件(如Checkstyle)定制代码模式。我无法为布局xml文件自定义规则。我正在寻求帮助。你的截图显示了不同的代码,然后是粘贴的代码<代码>检查
和抽象用法检查
不是最新检查样式的类。我建议使用最新版本的checkstyle。如果您需要旧版本checkstyle的支持,请指定您正在使用的版本。另外,请复制/粘贴无法实例化的错误消息的全文。如果我想起来,codacy不支持自定义规则correctly@rveach感谢您的评论,因为它确实帮助我深入了解了Checkstyle结构,并且根据您的建议,我已经替换了Checkstyle最新版本8.1中不推荐的类并替换了屏幕截图,其中清楚地显示了无法实例化的错误message@pedrorijo91我关注codacy文档,其中提到可以使用不同的插件(如Checkstyle)定制代码模式。我无法为布局xml文件自定义规则。我也在寻求帮助
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;
}