Using supressions by ID in checkstyle - java

I would like to make checkstyle ignore missing javadoc for #throws clauses in tests, but complain in non-tests.
Because supression files do not support subproperties of the JavadocMethod, I am forced to either ignore javadoc in tests altogether or not at all, with no granularity. This is also the conclusion of this other question.
Here's what I'm trying now: could I have two JavadocMethod modules with different id's (e.g. test and notest) in my checkstyle configuration, and selectively supress the test module on non-tests and viceversa?
I cannot get this to work, and I'm starting to think I can't have a duplicate module in checkstyle. Any insights?
Here are my rules regarding JavadocMethod in the configuration xml:
<module name="JavadocMethod">
<property name="id" value="nontest"/>
</module>
<module name="JavadocMethod">
<property name="id" value="test"/>
<property name="allowMissingThrowsTags" value="true"/>
</module>
And this is my supression file:
<suppressions>
<!-- Supress non-test-doc on tests -->
<suppress id="nontestdoc" files=".*(?:Test|IT).*java" />
<suppress id="testdoc" files=".*(?!Test|IT).*java" />
</suppressions>
Thanks!

You can have duplicate module in checkstyle.
Tried with 2 Indentation module with tab 2 and tab 4 respectively. And used the suppression.xml like following
<?xml version="1.0"?>
<!DOCTYPE suppressions PUBLIC
"-//Puppy Crawl//DTD Suppressions 1.1//EN"
"http://www.puppycrawl.com/dtds/suppressions_1_1.dtd">
<suppressions>
<suppress id="IndnA" files=".*\.java"/>
<suppress id="IndnB" files=".*\.java"/>
</suppressions>
and what is the regex .*(?:Test|IT).*java and .*(?!Test|IT).*java for? Why are you using LookAhead?
please check with files as .*\.java whether that is working or not.

Related

PMD rule for java to avoid using #Autowired annotation on class fields- to not use field injection

I want to have a pmd rule for my java project which will disallow field injections in Spring(want to use injection by constructor instead). is there any pmd rule for it, or can I create some to have it in my code analysis?
To not allow this:
#Autorired
private Object object;
thanks!
You could create an XPath rule with the following expression:
//Annotation
[pmd-java:typeIs('org.springframework.beans.factory.annotation.Autowired')]
[../FieldDeclaration]
To add this to your ruleset XML, see here:
<rule name="todo"
language="java"
message="todo."
class="net.sourceforge.pmd.lang.rule.XPathRule" >
<description>
TODO
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//Annotation
[pmd-java:typeIs('org.springframework.beans.factory.annotation.Autowired')]
[../FieldDeclaration]
]]>
</value>
</property>
</properties>
</rule>

PMD or Checkstyle check to prohibit usage of certain classes

Is there any PMD or Checkstyle rule available that could help me to prohibit usage of some certain classes in Java code?
In my case I'd like to ban all of the following in all possible contexts:
org.apache.commons.lang3.CharEncoding
org.apache.commons.lang.CharEncoding
org.apache.commons.codec.CharEncoding
I've found IllegalImport check, but it's about packages, not particular classes.
For PMD you can write rule like this:
<rule name="Prohibited classes"
language="java"
message="Avoid using these classes."
class="net.sourceforge.pmd.lang.rule.XPathRule" >
<description>
Avoid using these classes, there are better alternatives.
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//Name[pmd-java:typeIs('org.apache.commons.lang3.CharEncoding')] |
//Name[pmd-java:typeIs('org.apache.commons.lang.CharEncoding')] |
//Name[pmd-java:typeIs('org.apache.commons.codec.CharEncoding')]
]]>
</value>
</property>
</properties>
</rule>
Or //Name[starts-with(#Image, 'com.sun.')] to prohibit package import.
You can try to use these pmd extensions to blacklist classes and methods: https://github.com/LiveRamp/pmd_extensions

How can getters/setters be ignored in the PMD CommentRequired rule?

I want to use PMDs CommentRequired rule, but don't want it to be applied to java bean getters/setters.
Based on this answer I tried the following configuration:
<rule ref="rulesets/java/comments.xml/CommentRequired">
<properties>
<property name="violationSuppressXPath" value="./ancestor::MethodDeclaration/MethodDeclarator/NodeToken[#tokenImage='(get|is|set).*']"/>
<property name="fieldCommentRequirement" value="Ignored"/>
</properties>
</rule>
However PMD still reports that some getters in my code need a comment.
What is the correct XPath expression to suppress the rule for getters/setters and on which schema are the PMD XPath expressions based upon?
After reading the PMD XPath rule tutorial and playing around with the PMD Designer the following does work for me:
<rule ref="rulesets/java/comments.xml/CommentRequired">
<properties>
<property name="violationSuppressXPath" value="//ClassOrInterfaceBodyDeclaration/MethodDeclaration/MethodDeclarator[matches(#Image,'(get|is|set)[\p{Lu}].*') and not(FormalParameters/FormalParameter)]"/>
<property name="fieldCommentRequirement" value="Ignored"/>
</properties>
</rule>
To get a better grasp on the AST that PMD is using, one can have a look at the javadoc of it: http://pmd.sourceforge.net/pmd-5.1.0/apidocs/net/sourceforge/pmd/lang/java/ast/package-summary.html

Gwt and Sencha, did you forget yo inherit a required module.

I am trying to implement a BarChart in GWT 2.6 using Sencha GXT 3.1 beta.
It compiles fine but i do get several
14:40:40.102 [ERROR] [comav200] Line 71: No source code is available for type com.sencha.gxt.chart.client.chart.Chart; did you forget to inherit a required module?
Errors. I do use the import com.sencha.gxt.chart.client.chart.Chart; (and several more).
Anyone has any thoughts on why this occur or what i have forgotten?
Best regards,
Kerrai
Please validate below inherits in gwt.xml
<inherits name='com.sencha.gxt.chart.Chart' />
For detailed information and basic chart example please have a look at
Getting started with GXT charts
Copied form the above link if you don't want to go to the link.
Project module setup
Using charts requires a library be added to the GWT project xml module.
Add com.sencha.gxt.chart.Chart to the GWT project module:
<inherits name='com.sencha.gxt.chart.Chart' />
Project.gwt.xml module example: (This is a complete example and much of this is optional.)
Please validate your gwt.xml
<!-- Project.gwt.xml module file -->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 2.5.0//EN"
"http://google-web-toolkit.googlecode.com/svn/tags/2.5.0/distro-source/core/src/gwt-module.dtd">
<module rename-to='project'>
<!-- Inherit the core Web Toolkit stuff. -->
<inherits name='com.google.gwt.user.User' />
<!-- Other module inherits -->
<inherits name='com.sencha.gxt.ui.GXT' />
<!-- Add this for using GXT charts -->
<inherits name='com.sencha.gxt.chart.Chart' />
<!-- GXT Stylesheet -->
<stylesheet src="reset.css" />
<!-- Override the blue theme -->
<inherits name="com.sencha.gxt.theme.gray.Gray" />
<!-- Specify the app entry point class. -->
<entry-point class='com.sencha.gxt.project.client.ProjectEntryPoint' />
<!-- Specify the paths for translatable code -->
<source path='client' />
<source path='shared' />
</module>

How to ignore "id" in Short Variable rule using PMD

I am using PMD plugin (version 4.0.2) for Eclipse (Eclipse Kepler Java EE). I have configured a naming rule: ShortVariable.
This works fine except for parameters like "id" and "e". I want PMD to ignore these. So I searched for a way to ignore certain parameters. I found this link (although it's for phpmd) and tried it, yet I can't seem to get it working. My config file looks like this (XML):
<?xml version="1.0"?>
<ruleset name="My PMD ruleset"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
<description>
My PMD
</description>
<rule ref="rulesets/java/naming.xml/ShortVariable">
<property name="exceptions" value="id" />
</rule>
</ruleset>
When I try to import this ruleset using the eclipse plugin, it shows no possible rules to import.
Any ideas?
I found a solution to my problem here.
The resulting xml looks like this:
<?xml version="1.0"?>
<ruleset name="My PMD ruleset"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
<description>
My PMD
</description>
<rule ref="rulesets/java/naming.xml/ShortVariable">
<properties>
<property name="xpath">
<value>
//VariableDeclaratorId[(string-length(#Image) < 3) and (not (#Image='id'))]
[not(ancestor::ForInit)]
[not((ancestor::FormalParameter) and (ancestor::TryStatement))]
</value>
</property>
</properties>
</rule>
</ruleset>
To be able to ignore more variable names, repeat the following part:
and (not (#Image='myVariableToIgnore'))
The folowing XML is valid for PHP tool PHPMD 2.2.3
<?xml version="1.0"?>
<!DOCTYPE ruleset>
<ruleset
name="My PMD ruleset for symfony 2.5"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd"
>
<rule ref="rulesets/unusedcode.xml" />
<rule ref="rulesets/codesize.xml" />
<rule ref="rulesets/cleancode.xml" />
<rule ref="rulesets/controversial.xml" />
<rule ref="rulesets/design.xml" />
<rule ref="rulesets/naming.xml">
<exclude name="ShortVariable" />
</rule>
<rule ref="rulesets/naming.xml/ShortVariable">
<properties>
<property name="exceptions" value="id,em" />
</properties>
</rule>
</ruleset>
Update xml
<rule ref="category/java/codestyle.xml/ShortVariable">
<properties>
<property name="xpath">
<value>
//VariableDeclaratorId[(string-length(#Image) < 3) and (not (#Name='id'))]
[not(ancestor::ForInit)]
[not((ancestor::FormalParameter) and (ancestor::TryStatement))]
</value>
</property>
</properties>
</rule>

Categories

Resources