Limiting Checkstyle to certain packages - checkstyle

I have a project with several hundred packages. It's mostly generated code from a conversion from COBOL to inline java so the vast majority of it (~11,000 files) is something I do not want to run checkstyle on. I do have a couple packages in the project that are "real" java that I have added to replace utilities that could not be generated by the code conversion and I do want to run checkstyle on those.
I'm having trouble getting the suppression to work properly.
This is the relevant fragment from the checkstyle.xml:
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
"http://checkstyle.sourceforge.net/dtds/configuration_1_3.dtd">
<!--
Checkstyle configuration that checks the Google coding conventions from Google Java Style
that can be found at https://google.github.io/styleguide/javaguide.html.
Checkstyle is very configurable. Be sure to read the documentation at
http://checkstyle.sf.net (or in your downloaded distribution).
To completely disable a check, just comment it out or delete it from the file.
Authors: Max Vetrenko, Ruslan Diachenko, Roman Ivanov.
-->
<module name = "Checker">
<property name="charset" value="UTF-8"/>
<property name="severity" value="warning"/>
<property name="fileExtensions" value="java, properties, xml"/>
<module name="FileTabCharacter">
<property name="eachLine" value="true"/>
</module>
<module name="SuppressionFilter">
<property name="file" value="${config_loc}/checkstyleSuppress.xml"/>
<property name="optional" value="false"/>
</module>
<module name="TreeWalker">
<module name="OuterTypeFilename"/>
<module name="IllegalTokenText">
<property name="tokens" value="STRING_LITERAL, CHAR_LITERAL"/>
<property name="format"
value="\\u00(09|0(a|A)|0(c|C)|0(d|D)|22|27|5(C|c))|\\(0(10|11|12|14|15|42|47)|134)"/>
<property name="message"
value="Consider using special escape sequence instead of octal value or Unicode escaped value."/>
</module>
<module name="AvoidEscapedUnicodeCharacters">
<property name="allowEscapesForControlCharacters" value="true"/>
<propert ...
I'm confident that the checkstyleSuppress.xml file is getting read because checkstyle will error out if I add a character to the file name to make it wrong.
The content of checkstyleSuppress.xml is as follows:
<?xml version="1.0"?>
<!DOCTYPE suppressions PUBLIC
"-//Checkstyle//DTD SuppressionFilter Configuration 1.2//EN"
"https://checkstyle.org/dtds/suppressions_1_2.dtd">
<suppressions>
<suppress files="[/\\].+" checks=".*"/>
<suppress checks=".*" files="app[\\/]dbio[\\/].java"/>
<suppress checks=".*" files="app[\\/]dbio[\\/]internal[\\/].java"/>
<suppress checks=".*" files="app[\\/]f17*[\\/].java"/>
<suppress checks=".*" files="app[\\/]f17*[\\/]internal[\\/].java"/>
</suppressions>
What I'm trying to do is suppress all checks for anything in the app.dbio and app.f17* packages. To see if my rules are running I added the first line in the file which I thought would suppress all checks on all files.
None of these filters are suppressing anything. Checkstyle checks all files in all packages (and reports over 400,000 violations, which is a bit too many to wade through for the ones I care about! lol)
Most projects are ground up, so I have previously used checkstyle with no suppression. This is my first attempt to limit the scope of the code review and it's not going as I had hoped. I'm not sure where my issue lies here.
Any suggestions are much appreciated.

I think BeforeExecutionExclusionFileFilter is the thing you are looking for. You can provide regexp(s) which matches packages you want to exclude, they will not be checked.

Related

How to tell Checkstyle to check all lines for linefeed / newline LF and not CRLF?

Is there a way to check if the file is using LFs only and no CRLFs or vice versa in checkstyle?
The only check which I have found is
NewlineAtEndOfFile
But I search for more as only at the end of the file?
Only to supplement the Michal Kordas's good answer.
Below is a slightly modified configuration, which will only match the first wrong newline and forbid also the Mac OS Line Endings (CR):
<module name="RegexpMultiline">
<property name="format" value="(?s:(\r\n|\r).*)"/>
<property name="message" value="CRLF and CR line endings are prohibited, but this file uses them."/>
</module>
It has been posted by Michael Vorburger in this discussion, so kudos for him - I've posted it here for completeness.
There is nothing out-of-the box in Checkstyle yet. If you want to prohibit CRLF you can use the following config:
<module name="RegexpMultiline">
<property name="format" value="\r\n"/>
<property name="message" value="CRLF line endings are prohibited"/>
</module>
and this one to ban LFs:
<module name="RegexpMultiline">
<property name="format" value="(?<!\r)\n"/>
<property name="message" value="LF line endings are prohibited"/>
</module>

NoClassDefFoundError when checkstyle is running

I have written a new checkstyle check as a filescanner. I modeled my junits after the code I found in the checkstyle code. The junits run just fine and everything looks good.
But then, I add the check to my project.
<module name="TreeWalker">
<property name="tabWidth" value="4" />
<module name="com.onuspride.codetools.checkstyles.DuplicateClassNames"/>
</module>
and my ant task
<taskdef resource="checkstyletask.properties">
<classpath refid="classpath" />
</taskdef>
<property name="checkstyle.suppressions.file" value="checkstyle/suppressions.xml" />
<property name="translation.severity" value="error" />
<target name="checkStyle" description="TestTask to evaluate the checkstyle system.">
<checkstyle config="checkstyle/checkstyle_checks.xml">
<fileset dir="${msg.src}" includes="**/*.java" />
<formatter type="plain" />
<formatter type="xml" toFile="${msg.build.jar}/checkstyle_errors.xml" />
<classpath refid="classpath" />
</checkstyle>
</target>
the duplicateclassnames class calls several classes in the same jar. For some reason, when ant runs it, ant finds the check class, but can't find the supporting classes, when they are all in the same jar file. here's what i get in ant
[checkstyle] [class]:0: Got an exception - java.lang.NoClassDefFoundError: com/onuspride/codetools/common/classpath/criteria/ClassNameCriteriaCollector
Im stumped. Ive checkd all the dependencies of my jar, they are all in the classpath, I don't understand how it can find one class file but not another in the same jar. Ive done all my dirty little tricks and I just don't get it.
any ideas?
You can do it like following :
Create plugin project and add your custom checks there.
Make appropriate changes to plugin.xml, checkstyle_packages.xml.
Export the project as Deployable Plug-ins and fragments (Export > Plug-in Developement)
Copy the jar file to Eclipse Plugin folde, so no need to install your custom check .
You can go through this tutorial for reference
To reduce effort, download a Sample Check, the file is here under the name net.sf.eclipsecs.sample
Just replace your source in src folder. Before replacing, refer this 3 files in src/net/sf/eclipsecs/sample/checks/ directory as you will need them in your com/onuspride/codetools/checkstyles/ directory :
checkstyle-metadata.properties
checkstyle-metadata.xml
messages.properties
After replacing the code, make appropriate changes in checkstyle_packages.xml file in src/ directory.
Extending Check is described nicely there.

Is there a checkstyle plugin for blacklisting words

Does someone know of a checkstyle plugin that can be configured with a set of words, and will mark all occurences of those words as warnings (in identifiers or in string values) ?
The project we're working on requires that all code is in english. But most people are not native speakers and they sometimes accidentally include non-english words in their code. We'd like to detect at least some of those mistakes and signal them.
Try this rule which checks for several non-English variants of "Cheers!", ignoring case:
<module name="Regexp">
<property name="format" value="(?i)(na zdrowie|kippis|a votre sante|prost)"/>
<property name="message" value="Use only English language"/>
<property name="illegalPattern" value="true"/>
</module>

Overriding/merging standard programmatic configuration with config file section

We have a standard set of conventions that we follow for logging from our various applications, and it is sensible to package this configuration as by-code configuration and include in a common assembly.
Occasionally, however, we need to override this config to more efficiently debug production issues. This is more easily accomplished by letting a developer or administrator add an NLog config section, which can be read in and override or add to configuration done programmatically.
Can this be done out of the box with NLog?
I know this isn't by-code (I'm not sure how to do it), but you can use <include> with XML config to override things like variables. Here's a sample that uses Web.config that overrides the "standard" NLog.config for other projects:
Web.config:
<configuration>
<configSections>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
</configSections>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<include file="${basedir}\bin\NLog.config" />
<variable name="fruit" value="Apples" />
</nlog>
</configuration>
NLog.config (that gets copied to bin):
<?xml version="1.0"?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<!-- Output Window -->
<target name="debug" xsi:type="Debugger" layout="${fruit}|${level:uppercase=true}|${logger}|${message}"></target>
</targets>
<rules>
<logger name="*" writeTo="debug" />
</rules>
</nlog>

Hibernate code generation in eclipse isnt generating all my tables (weird)

Im just getting my head around java and eclipse. Im trying to use the hibernate plugin to generate my java classes.
Yesterday I created a table in mysql and managed to (after a lot of messing around) get it to generate the code for this table.
Today I have added some tables to the database and tried to generate code for them.
Now I can see these tables in the table filters tab, but I can only see the Primary index fields. I can see all the fields in the table I created yesterday.
When I generate the code, I only get the class for yesterdays tables.
I just dont understand whats going on ..
Heres my hibernate.reveng.xml.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd" >
<hibernate-reverse-engineering>
<table-filter match-name=".*"></table-filter>
</hibernate-reverse-engineering>
Heres my hibernate.cfg.xml.
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.session_factory_name">SessionFactory</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/af</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.default_catalog">af</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hibernate.connection.pool_size">50</property>
<property name="current_session_context_class">thread</property>
</session-factory>
</hibernate-configuration>
These tables have identical structures in mySql.
Im just really confused now. How do I get all my tables to be generated?
Ok, I have worked it out.
It seems Hibernate tools do not like tablenames to contain an uppercase letter in them.
If I rename all my tables to be lower case, it all works fine!