Custom Check

152 views
Skip to first unread message

gabriel...@gmail.com

unread,
Dec 21, 2020, 7:01:13 PM12/21/20
to checkstyle
Hi all,

I have been trying to figure out how to work with checkstyle for some time now and I cannot seem to get a simple custom check project working.
Here's what I have accomplished:
  • Edited the google_checks.xml file in the plug-in so that line limit is 80 instead of 100.
  • Editedgoogle_checks.xml file in the plug-in so that the indentations are by 4 not by 2. 
- I ran mvn clean package -Passembly, then with the jar made I ran java -jar [jar-file created] -c google_checks.xml [my java file]

All I really want to do now is:
  • Use the previous checks for a specific java file.
  • Have a custom checks project that also includes a method limit check (like the one on the Checkstyle website) and an "over-looping" check (probably using the NestedForDepth module).
  • Have those google_checks AND my custom checks in one project so that I can use them in command line to check specific Java files.
I have been using the maven instruction on this (as seen in https://maven.apache.org/plugins/maven-checkstyle-plugin/examples/custom-developed-checkstyle.html) as I don't find the explanation inthe checkstyle website to be very straight forward.

I'm genuinely so lost, I just want to help my professor automate some of his grading. If there are any simple instruction videos some of you know of, please do share. 

- Gabriel

Roman Ivanov

unread,
Dec 21, 2020, 11:54:12 PM12/21/20
to gabriel...@gmail.com, checkstyle
Take a look at https://github.com/sevntu-checkstyle/checkstyle-samples/tree/master/maven-project

You should have single config with all Checks, make sure that  both jars ( standard and ja With  custom Checks) in classpath.

You can take a look at https://github.com/checkstyle/contribution/blob/master/checkstyle-tester/my_check.xml as example of maven project that use standard and jar with custom Checks. Just uncomment Check from sevntu in config to make it work.

You can find bunch of examples at 

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/checkstyle/635a580d-6ae6-4545-94dd-dde2c9e18abdn%40googlegroups.com.

gabriel...@gmail.com

unread,
Jan 3, 2021, 8:58:01 PM1/3/21
to checkstyle
I don't see any JAR files in that repo and I'm unsure on how I can use it. For the checkstyle-tester/my_check.xml, I don't know how that project is using it.

Should I be: creating a JAR file of the regular checkstyle repo, then a JAR file with a custom project, then creating a single config file (that I create by partially copying from the google_checks), put those 3 in the same directory, then running checkstyle:checkstyle from my command line? I feel like this is really simple to do but the documentation is overcomplicating it.

gabriel...@gmail.com

unread,
Jan 3, 2021, 10:09:47 PM1/3/21
to checkstyle
I tried using CreateYourOwnCheckStyleCheck. It's on the older end so I had to add in a version updating into the poms so that it would stop throwing errors on 'mvn install'. After that I changed the custom check java file to be an AbstractCheck instead to follow current guidelines. Now when I run 'mvn install' I get build failure that is described in AntiHungarianCheckShould.txt. When I try to do 'mvn checkstyle:checkstyle' within the TestProject (as described to do in the repo README), I get another build failure stating that it "Failed during checkstyle configuration: cannot initialize module TreeWalker instantiate com.blundell.checks.AntiHungarian: Unable to instantiate com./bundell.checks.AntiHungarianCheck".
com.blundell.checks.AntiHungarianCheckShould.txt

Roman Ivanov

unread,
Jan 3, 2021, 10:21:09 PM1/3/21
to gabriel...@gmail.com, checkstyle
> Should I be: creating a JAR file of the regular checkstyle repo, then a JAR file with a custom project, then creating a single config file (that I create by partially copying from the google_checks),

YES

> put those 3 in the same directory

only if you are using "java -jar ..." or "java -cp ..." to execute 
looks like you are using maven, so, please make sure both jars are in pom.xml.
I already sent you examples how to configure pom with custom jar.

> Failed during checkstyle configuration: cannot initialize module TreeWalker instantiate com.blundell.checks.AntiHungarian: Unable to instantiate com./bundell.checks.AntiHungarianCheck

make sure jar with custom check is in pom.xml and available in maven repository.
and make sure name of Check in config is fully qualified name of class.


shiva pullepu

unread,
Jan 5, 2021, 7:30:15 PM1/5/21
to Roman Ivanov, gabriel...@gmail.com, checkstyle
Hi Roman, With respect to below comment, having custom check jar in "local" maven repository should work right? or should be in maven central? 

> Failed during checkstyle configuration: cannot initialize module TreeWalker instantiate com.blundell.checks.AntiHungarian: Unable to instantiate com./bundell.checks.AntiHungarianCheck

make sure jar with custom check is in pom.xml and available in maven repository.
and make sure name of Check in config is fully qualified name of class.

Thanks

Message has been deleted

nmancus1

unread,
Jan 5, 2021, 9:09:59 PM1/5/21
to checkstyle
Hi Gabriel,
It would be helpful if you could share your pom.xml and config, if possible. You can use https://gist.github.com/, or point us to your repo if it is on github.

Thanks
- Nick
Reply all
Reply to author
Forward
0 new messages