How to link a cusom check to a checkstyle.

349 views
Skip to first unread message

Alexey Verein

unread,
Jul 31, 2014, 7:20:27 AM7/31/14
to check...@googlegroups.com
Hi.

I'm trying to add custom check to the checkstyle. I'm following the tutorial: http://checkstyle.sourceforge.net/writingchecks.html and got stuck on the "Integrate Your Check" step.

I've created a MethodLimitCheck.java file with the contents exactly the same as in the tutorial. Then I've packed it into a jar: jar cf mycompanychecks.jar MethodLimitCheck.java

Then I put this jar and a config.xml with contents exactly as in the tutorial in the directory with the checkstyle-5.7-all.jar.

When I try to run

 java -classpath mycompanychecks.jar:checkstyle-5.7-all.jar      com.puppycrawl.tools.checkstyle.Main      -c config.xml -r . 

I get:

Unable to create Checker: cannot initialize module TreeWalker - Unable to instantiate com.mycompany.checks.MethodLimitCheck
com.puppycrawl.tools.checkstyle.api.CheckstyleException: cannot initialize module TreeWalker - Unable to instantiate com.mycompany.checks.MethodLimitCheck
at com.puppycrawl.tools.checkstyle.Checker.setupChild(Checker.java:179)
at com.puppycrawl.tools.checkstyle.api.AutomaticBean.configure(AutomaticBean.java:184)
at com.puppycrawl.tools.checkstyle.Main.createChecker(Main.java:143)
at com.puppycrawl.tools.checkstyle.Main.main(Main.java:120)
Caused by: com.puppycrawl.tools.checkstyle.api.CheckstyleException: Unable to instantiate com.mycompany.checks.MethodLimitCheck
at com.puppycrawl.tools.checkstyle.PackageObjectFactory.createModule(PackageObjectFactory.java:155)
at com.puppycrawl.tools.checkstyle.TreeWalker.setupChild(TreeWalker.java:161)
at com.puppycrawl.tools.checkstyle.api.AutomaticBean.configure(AutomaticBean.java:184)
at com.puppycrawl.tools.checkstyle.Checker.setupChild(Checker.java:158)
... 3 more
Caused by: com.puppycrawl.tools.checkstyle.api.CheckstyleException: Unable to instantiate com.mycompany.checks.MethodLimitCheckCheck
at com.puppycrawl.tools.checkstyle.PackageObjectFactory.doMakeObject(PackageObjectFactory.java:98)
at com.puppycrawl.tools.checkstyle.PackageObjectFactory.createModule(PackageObjectFactory.java:152)
... 6 more

If i remove the lines

    <module name="com.mycompany.checks.MethodLimitCheck">
      <property name="max" value="45"/>
    </module>

from the config the check succeeds.

What am I doing wrong?

Roman Ivanov

unread,
Aug 5, 2014, 2:27:25 PM8/5/14
to Alexey Verein, check...@googlegroups.com

Hi Alexey,

Please share all sources where i can see a problem easily.

For now i could presume that " jar cf mycompanychecks.jar MethodLimitCheck.java " does not create proper jar with correct folder structure for package.

Thanks,
Roman Ivanov

--
You received this message because you are subscribed to the Google Groups "checkstyle" group.
To unsubscribe from this group and stop receiving emails from it, send an email to checkstyle+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alexey Verein

unread,
Aug 6, 2014, 11:53:06 AM8/6/14
to check...@googlegroups.com, tpo...@gmail.com
Hi.

The mycompanychecks.jar contains one files inside it (MethodsCountCheck.java) and one folder (META-INF).
The contents of the java file is the same as in the tutorial:

package com.mycompany.checks;
import com.puppycrawl.tools.checkstyle.api.*;

public class MethodLimitCheck extends Check
{
    private static final int DEFAULT_MAX = 30;
    private int max = DEFAULT_MAX;

    @Override
    public int[] getDefaultTokens()
    {
        return new int[]{TokenTypes.CLASS_DEF, TokenTypes.INTERFACE_DEF};
    }

    @Override
    public void visitToken(DetailAST ast)
    {
        // find the OBJBLOCK node below the CLASS_DEF/INTERFACE_DEF
        DetailAST objBlock = ast.findFirstToken(TokenTypes.OBJBLOCK);
        // count the number of direct children of the OBJBLOCK
        // that are METHOD_DEFS
        int methodDefs = objBlock.getChildCount(TokenTypes.METHOD_DEF);
        // report error if limit is reached
        if (methodDefs > this.max) {
            log(ast.getLineNo(),
                "too many methods, only " + this.max + " are allowed");
        }
   }
}


I've created it using an IDE (IntelliJ IDEA), and imported the checkstyle 5.7 jar. But this jar is not included in the mycompanychecks.jar, may be this is the reason. If yes, how to properly include it?

The content of the config file is as follows:

<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
    "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
<module name="Checker">
  <module name="TreeWalker">
          <!-- your standard Checks that come with Checkstyle -->
    <module name="UpperEll"/>
    <module name="MethodLength"/>
          <!-- your Check goes here -->
    <module name="com.mycompany.checks.MethodLimitCheck">
      <property name="max" value="45"/>
    </module>
  </module>
</module>


Thank you.

Roman Ivanov

unread,
Aug 9, 2014, 3:33:02 PM8/9/14
to Alexey Verein, check...@googlegroups.com
Hi Alexey,​

project that you play with should be posted on Github , without this - no help will provided.
I need to see all details/nuances, attaching result jar to mail-thread is also good idea.

thanks,
Roman Ivanov

Alexey Verein

unread,
Aug 11, 2014, 5:35:38 AM8/11/14
to check...@googlegroups.com, tpo...@gmail.com
Hello again!

Please, take a look at the project: https://github.com/tpom6oh-/MyChecks

This is how i try to launch the checkstyle:

java -classpath checkswithfolders.jar:checkstyle-5.7-all.jar com.puppycrawl.tools.checkstyle.Main -c custom_config.xml -r .

and

java -classpath checkswithoutfolders.jar:checkstyle-5.7-all.jar com.puppycrawl.tools.checkstyle.Main -c custom_config.xml -r .

and I get the same results:

java -classpath checkswithfolders.jar:checkstyle-5.7-all.jar com.puppycrawl.tools.checkstyle.Main -c custom_config.xml -r .
java -classpath checkswithoutfolders.jar:checkstyle-5.7-all.jar com.puppycrawl.tools.checkstyle.Main -c custom_config.xml -r .
Unable to create Checker: cannot initialize module TreeWalker - Unable to instantiate com.mychecks.MethodLimitCheck
com.puppycrawl.tools.checkstyle.api.CheckstyleException: cannot initialize module TreeWalker - Unable to instantiate com.mychecks.MethodLimitCheck
at com.puppycrawl.tools.checkstyle.Checker.setupChild(Checker.java:179)
at com.puppycrawl.tools.checkstyle.api.AutomaticBean.configure(AutomaticBean.java:184)
at com.puppycrawl.tools.checkstyle.Main.createChecker(Main.java:143)
at com.puppycrawl.tools.checkstyle.Main.main(Main.java:120)
Caused by: com.puppycrawl.tools.checkstyle.api.CheckstyleException: Unable to instantiate com.mychecks.MethodLimitCheck
at com.puppycrawl.tools.checkstyle.PackageObjectFactory.createModule(PackageObjectFactory.java:155)
at com.puppycrawl.tools.checkstyle.TreeWalker.setupChild(TreeWalker.java:161)
at com.puppycrawl.tools.checkstyle.api.AutomaticBean.configure(AutomaticBean.java:184)
at com.puppycrawl.tools.checkstyle.Checker.setupChild(Checker.java:158)
... 3 more
Caused by: com.puppycrawl.tools.checkstyle.api.CheckstyleException: Unable to instantiate com.mychecks.MethodLimitCheckCheck
at com.puppycrawl.tools.checkstyle.PackageObjectFactory.doMakeObject(PackageObjectFactory.java:98)
at com.puppycrawl.tools.checkstyle.PackageObjectFactory.createModule(PackageObjectFactory.java:152)
... 6 more


But if I use the standard_config.xml:

java -classpath checkswithoutfolders.jar:checkstyle-5.7-all.jar      com.puppycrawl.tools.checkstyle.Main      -c standard_config.xml -r .
Starting audit...
Audit done.


The checkstyle is working.

Daniil Yaroslavtsev

unread,
Aug 14, 2014, 8:01:25 AM8/14/14
to check...@googlegroups.com, tpo...@gmail.com
1. Your jar file is wrong. It contains java file instead of compiled class file. Please refer to Oracle docs for more details.

From http://docs.oracle.com/javase/tutorial/deployment/jar/index.html :
Typically a JAR file contains the class files and auxiliary resources associated with applets and applications.

I have attached proper jar file with class file inside to let you test it out.

2. Your custom check does not contain setter 'setMax' while in config you are trying to inject "max" property into your check.

So, when you will use file attached, it will throw an error like below:

15:44 $ java -classpath checkswithfolders_proper.jar:checkstyle-5.7-all.jar com.puppycrawl.tools.checkstyle.Main -c custom_config.xml -r .
Unable to create Checker: cannot initialize module TreeWalker - Property 'max' in module com.mychecks.MethodLimitCheck does not exist, please check the documentation
com.puppycrawl.tools.checkstyle.api.CheckstyleException: cannot initialize module TreeWalker - Property 'max' in module com.mychecks.MethodLimitCheck does not exist, please check the documentation

    at com.puppycrawl.tools.checkstyle.Checker.setupChild(Checker.java:179)
    at com.puppycrawl.tools.checkstyle.api.AutomaticBean.configure(AutomaticBean.java:184)
    at com.puppycrawl.tools.checkstyle.Main.createChecker(Main.java:143)
    at com.puppycrawl.tools.checkstyle.Main.main(Main.java:120)
Caused by: com.puppycrawl.tools.checkstyle.api.CheckstyleException: Property 'max' in module com.mychecks.MethodLimitCheck does not exist, please check the documentation
    at com.puppycrawl.tools.checkstyle.api.AutomaticBean.configure(AutomaticBean.java:142)
    at com.puppycrawl.tools.checkstyle.TreeWalker.setupChild(TreeWalker.java:168)
checkswithfolders_proper.jar
Message has been deleted

Alexey Verein

unread,
Aug 19, 2014, 1:23:20 AM8/19/14
to check...@googlegroups.com, tpo...@gmail.com
It works, thank you very much.

Roman Ivanov

unread,
Aug 19, 2014, 10:49:19 AM8/19/14
to Alexey Verein, check...@googlegroups.com
Hi Alexey,

It would be nice if you provide instruction "how to do that" for future non-experienced developers. 

thanks,
Roman Ivanov

Roman Ivanov

unread,
Sep 16, 2014, 4:53:57 PM9/16/14
to check...@googlegroups.com, tpo...@gmail.com
Hi Alexey,

Example of usage with separate JAR,  is already there:

it is good to recheck that required class is in JAR, by command:
$ jar -tf checkswithoutfolders.jar  | grep -E "MethodLimitCheckCheck.class$"
com/mychecks/MethodLimitCheckCheck.class

thanks,
Roman Ivanov
Reply all
Reply to author
Forward
0 new messages