I want to run a simple custom check implemented in checkstyle on a java program samples/Test.java. And I came across the below error.
com.puppycrawl.tools.checkstyle.api.CheckstyleException: cannot initialize module TreeWalker - Unable to instantiate 'com.checkstyle.checks.IllegalExceptionThrowsCheck' class, it is also not possible to instantiate it as null.
Pardon me, even though the error makes sense, I couldn't make any progress on fixing it. I have posted the complete error at the end of the post. Below is the implementation of the custom check. IllegalExceptionThrowsCheck.java
package com.checkstyle.checks;
import java.util.Arrays;
import java.util.List;
import com.puppycrawl.tools.checkstyle.api.Check;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
public final class IllegalExceptionThrowsCheck extends Check {
List<String> illegalExceptionThrows;
/** {@inheritDoc} */
public int[] getDefaultTokens() {
return new int[] { TokenTypes.LITERAL_THROWS };
}
/** {@inheritDoc} */
public int[] getRequiredTokens() {
return getDefaultTokens();
}
/** {@inheritDoc} */
public void visitToken(DetailAST aDetailAST) {
DetailAST identDetailAST = aDetailAST.findFirstToken(TokenTypes.IDENT);
if (identDetailAST == null) {
return;
}
if (illegalExceptionThrows.contains(identDetailAST.getText())) {
log(aDetailAST, "Illegal Throws Clause -> "
+ identDetailAST.getText());
}
}
public void setIllegalExceptionThrows(String illegalExceptionThrowsStr) {
this.illegalExceptionThrows = Arrays.asList(illegalExceptionThrowsStr
.split(","));
}
}
And the checkstyle configuration is
custom-checkstyle.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC "-//Puppy Crawl//DTD Check Configuration 1.2//EN" "http://www.puppycrawl.com/dtds/configuration_1_2.dtd">
<module name="Checker">
<property name="severity" value="error" />
<module name="TreeWalker">
<property name="severity" value="error" />
<module name="com.checkstyle.checks.IllegalExceptionThrowsCheck">
<property name="severity" value="error" />
<property name="illegalExceptionThrows" value="Exception" />
</module>
</module>
</module>
The package structure is
Project
|
|->src
| |
| |->com
| |
| |->checkstyle
| |
| |-> checks
| |
| |-> IllegalExceptionThrowsCheck.java
|
|
|-> lib
| |
| |->checkstyle-8.10.1-all.jar
|
|
|-> Samples
|
| -> Test.java
The command I ran to execute the check on java program was
java -jar lib/checkstyle-8.10.1-all.jar com.puppycrawl.tools.checkstyle.Main -c custom_check.xml samples/Test.java
Any comments would be greatly helpful.
APPENDIX
Complete error log
com.puppycrawl.tools.checkstyle.api.CheckstyleException: cannot initialize module TreeWalker - Unable to instantiate 'com.checkstyle.checks.IllegalExceptionThrowsCheck' class, it is also not possible to instantiate it as null. Please recheck that class name is specified as canonical name or read how to configure short name usage http://checkstyle.sourceforge.net/config.html#Packages. Please also recheck that provided ClassLoader to Checker is configured correctly. at com.puppycrawl.tools.checkstyle.Checker.setupChild(Checker.java:460) at com.puppycrawl.tools.checkstyle.api.AutomaticBean.configure(AutomaticBean.java:198) at com.puppycrawl.tools.checkstyle.Main.runCheckstyle(Main.java:550) at com.puppycrawl.tools.checkstyle.Main.runCli(Main.java:465) at com.puppycrawl.tools.checkstyle.Main.main(Main.java:219) Caused by: com.puppycrawl.tools.checkstyle.api.CheckstyleException: Unable to instantiate 'com.checkstyle.checks.IllegalExceptionThrowsCheck' class, it is also not possible to instantiate it as null. Please recheck that class name is specified as canonical name or read how to configure short name usage http://checkstyle.sourceforge.net/config.html#Packages. Please also recheck that provided ClassLoader to Checker is configured correctly. at com.puppycrawl.tools.checkstyle.PackageObjectFactory.createModule(PackageObjectFactory.java:208) at com.puppycrawl.tools.checkstyle.TreeWalker.setupChild(TreeWalker.java:151) at com.puppycrawl.tools.checkstyle.api.AutomaticBean.configure(AutomaticBean.java:198) at com.puppycrawl.tools.checkstyle.Checker.setupChild(Checker.java:455) ... 4 more Checkstyle ends with 1 errors.