4

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.

1 Answer 1

1

class IllegalExceptionThrowsCheck extends Check {

First, the base class is no longer Check. It is AbstractCheck. Check was removed a while ago, and you CLI command says you are using 8.10.1, so I don't know how you got this to check to compile with a non-existent base class Check.

java -jar lib/checkstyle-8.10.1-all.jar com.puppycrawl.tools.checkstyle.Main -c custom_check.xml samples/Test.java

Second, since your not mentioning your project/jar in this command line, checkstyle will never find your class. You are telling the JVM only about checkstyle-8.10.1-all.jar and not about your project and it's class files.

I recommend packaging your project into a jar and then include that in your command line. Example: java -classpath MyCustom.jar;checkstyle-8.10.1-all.jar com.puppycrawl.tools.checkstyle.Main -c config.xml Check.java
http://checkstyle.sourceforge.net/cmdline.html#Usage_by_Classpath_update

Third, you can change your setter to:

public void setIllegalExceptionThrows(String... illegalExceptionThrowsStr) {
    this.illegalExceptionThrows = Arrays.asList(illegalExceptionThrowsStr);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.