
{"id":705,"date":"2014-11-18T20:25:27","date_gmt":"2014-11-18T20:25:27","guid":{"rendered":"http:\/\/www.codergears.com\/Blog\/?p=705"},"modified":"2014-11-24T09:40:20","modified_gmt":"2014-11-24T09:40:20","slug":"make-the-most-of-pmd-findbugs-and-checkstyle-results","status":"publish","type":"post","link":"https:\/\/codergears.com\/Blog\/?p=705","title":{"rendered":"Make the most of Pmd, Findbugs and CheckStyle results."},"content":{"rendered":"<p style=\"color: #555555;\">Many Java static analysis tools exist right there, each one focus on a specific area and has its advantages, we can enumerate:<\/p>\n<ul style=\"color: #555555;\">\n<li><b>Pmd<\/b>\u00a0which is a static rule-set based Java source code analyzer that identifies potential problems like:\n<ul>\n<li>Possible bugs\u2014Empty try\/catch\/finally\/switch blocks.<\/li>\n<li>Dead code\u2014Unused local variables, parameters and private methods.<\/li>\n<li>Empty if\/while statements.<\/li>\n<li>Overcomplicated expressions\u2014Unnecessary if statements, for loops that could be while loops.<\/li>\n<li>Suboptimal code\u2014Wasteful String\/StringBuffer usage.<\/li>\n<\/ul>\n<\/li>\n<li><b>FindBugs<\/b>\u00a0which looks for bugs in Java code. It uses static analysis to identify hundreds of different potential types of errors in Java programs.<\/li>\n<li><b>Checkstyle<\/b>\u00a0defines a set of available modules, each of which provides rules checking with a configurable level of strictness (mandatory, optional\u2026). Each rule can raise notifications, warnings, and errors.<\/li>\n<\/ul>\n<p><!--more--><\/p>\n<p style=\"color: #555555;\"><span id=\"more-876\"><\/span><br \/>\nMany ways exist to explore the results of these tools:<\/p>\n<ul style=\"color: #555555;\">\n<li><b>XML format<\/b>: XML files could be generated from each of these tools, and it can be used to create an HTML report or used by another tool to explore the analysis result.<\/li>\n<li><b>HTML format<\/b>: HTML report is the prefered way to generate reports and share them between the team, and you can create your custom report by using an xsl stylesheet.<\/li>\n<li><b>IDE Plugins<\/b>: almost all known IDE provides plugins for these tools, which gives the possibility to discover all violations from the source code.<\/li>\n<\/ul>\n<p style=\"color: #555555;\">One of the problems with code quality tools is that they tend to overwhelm developers with problems that aren\u2019t really problems \u2014 that is, false positives. When false positives occur, developers learn to ignore the output of the tool or abandon it altogether.<\/p>\n<p style=\"color: #555555;\">To explore better their result, it\u2019s interesting\u00a0to have a way to focus only on what we want and gives to developers a useful view.<\/p>\n<h2 style=\"font-weight: bold; color: #555555;\">JArchitect and CQLinq<\/h2>\n<p style=\"color: #555555;\"><a style=\"color: #2970a6;\" href=\"http:\/\/www.jarchitect.com\/\">JArchitect<\/a>\u00a0is another static analysis tool which complements the other ones, it uses a code query langage based on Linq ( CQLinq) to query the code base like a database.<\/p>\n<p style=\"color: #555555;\">Let\u2019s take as example the source code of the PDT core (the Php plugin for eclipse). and discover how we can explore the analysis result of these tools from JArchitect.<\/p>\n<p style=\"color: #555555;\">JArchitect provides by default many useful queries to request these tools, and all these queries could be customized easilly.<\/p>\n<p style=\"color: #555555;\"><a style=\"color: #2970a6;\" href=\"http:\/\/javadepend.files.wordpress.com\/2013\/11\/plugins2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-885\" src=\"http:\/\/javadepend.files.wordpress.com\/2013\/11\/plugins2.png?w=595&amp;h=202\" alt=\"plugins2\" width=\"595\" height=\"202\" \/><\/a><\/p>\n<p style=\"color: #555555;\">Let\u2019s discover some CQLinq queries:<\/p>\n<h3 style=\"font-weight: bold; color: #555555;\">Get all issues:<\/h3>\n<p style=\"color: #555555;\">The request to get all issues is very simple. However, as you can see it\u2019s not very interesting, indeed it\u2019s a challenge to treat\u00a0a result with 232 725 issues.<\/p>\n<p style=\"color: #555555;\"><a style=\"color: #2970a6;\" href=\"http:\/\/javadepend.files.wordpress.com\/2013\/11\/plugins1.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-883\" src=\"http:\/\/javadepend.files.wordpress.com\/2013\/11\/plugins1.png?w=595\" alt=\"plugins1\" \/><\/a><\/p>\n<p style=\"color: #555555;\">To treat\u00a0better the result of these tools we can filter it \u00a0and focus only on what we want.<\/p>\n<h3 style=\"font-weight: bold; color: #555555;\">Request by tool<\/h3>\n<p style=\"color: #555555;\">We can modify the first request and add a criteria about the tool concerned.<\/p>\n<p style=\"color: #555555;\"><a style=\"color: #2970a6;\" href=\"http:\/\/javadepend.files.wordpress.com\/2013\/11\/plugins3.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-886\" src=\"http:\/\/javadepend.files.wordpress.com\/2013\/11\/plugins3.png?w=595\" alt=\"plugins3\" \/><\/a><\/p>\n<h3 style=\"font-weight: bold; color: #555555;\">Request by ruleset<\/h3>\n<p style=\"color: #555555;\">We can also filter by issue ruleset :<\/p>\n<p style=\"color: #555555;\"><a style=\"color: #2970a6;\" href=\"http:\/\/javadepend.files.wordpress.com\/2013\/11\/plugins20.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-938\" src=\"http:\/\/javadepend.files.wordpress.com\/2013\/11\/plugins20.png?w=595\" alt=\"plugins20\" \/><\/a><\/p>\n<h3 style=\"font-weight: bold; color: #555555;\">Request by priority<\/h3>\n<p style=\"color: #555555;\">We can also filter by priority:<\/p>\n<p style=\"color: #555555;\"><a style=\"color: #2970a6;\" href=\"http:\/\/javadepend.files.wordpress.com\/2013\/11\/plugins8.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-911\" src=\"http:\/\/javadepend.files.wordpress.com\/2013\/11\/plugins8.png?w=595\" alt=\"plugins8\" \/><\/a><\/p>\n<h3 style=\"font-weight: bold; color: #555555;\">Most recurrent issues<\/h3>\n<p style=\"color: #555555;\">It\u2019s interesting to know which issues are the most reported by these tools.<\/p>\n<p style=\"color: #555555;\"><a style=\"color: #2970a6;\" href=\"http:\/\/javadepend.files.wordpress.com\/2013\/11\/plugins10.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-913\" src=\"http:\/\/javadepend.files.wordpress.com\/2013\/11\/plugins10.png?w=595\" alt=\"plugins10\" \/><\/a><\/p>\n<h3 style=\"font-weight: bold; color: #555555;\">Classes having most issues<\/h3>\n<p style=\"color: #555555;\">It\u2019s very interesting to know the classes which contains many violations<\/p>\n<p style=\"color: #555555;\"><a style=\"color: #2970a6;\" href=\"http:\/\/javadepend.files.wordpress.com\/2013\/11\/plugins16.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-923\" src=\"http:\/\/javadepend.files.wordpress.com\/2013\/11\/plugins16.png?w=595\" alt=\"plugins16\" \/><\/a><\/p>\n<p style=\"color: #555555;\">As we can observe CheckStyle report thousand of issues and many of them could be ignored.<\/p>\n<p style=\"color: #555555;\">The previous query is interesting, but it\u2019s not give us exactly the classes with lack of quality, another useful metric to take into account is the NBLinesOfCode, for that we can modify the previous request and\u00a0calculate the ratio between the Issues count and the NBLinesofCode.<\/p>\n<p style=\"color: #555555;\"><a style=\"color: #2970a6;\" href=\"http:\/\/javadepend.files.wordpress.com\/2013\/11\/plugins17.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-925\" src=\"http:\/\/javadepend.files.wordpress.com\/2013\/11\/plugins17.png?w=595\" alt=\"plugins17\" \/><\/a><\/p>\n<p style=\"color: #555555;\">What\u2019s very strange in this result is that the ratio of the 8 first classes is more than 200, in this case we have more than 200 issues by code line.<\/p>\n<p style=\"color: #555555;\">To explains this behavior let\u2019s take a look at some lines from\u00a0the CompilerAstParser:<\/p>\n<p style=\"color: #555555;\"><a style=\"color: #2970a6;\" href=\"http:\/\/javadepend.files.wordpress.com\/2013\/11\/plugins18.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-926\" src=\"http:\/\/javadepend.files.wordpress.com\/2013\/11\/plugins18.png?w=595&amp;h=486\" alt=\"plugins18\" width=\"595\" height=\"486\" \/><\/a><\/p>\n<p style=\"color: #555555;\">The NbLinesOfCode is the number of statements and not the number of physical lines, this Class declare many arrays , each one is declared by thousand of physical lines. However, each array declaration is considered as one statement.\u00a0And as shown before for the most recurrent issues query, the following rule\u00a0<b>\u2018+\u2019 should be on a new line.<\/b>\u00a0is violated thousand of times for each array. Maybe it\u2019s better to remove these kind of rules from the CheckStyle configuration file.<\/p>\n<h3 style=\"font-weight: bold; color: #555555;\">Most popular methods having issues<\/h3>\n<p style=\"color: #555555;\">When the static analysis tools report the issues, it\u2019s useful to locate which the prioritary issues to resolve? specially if it concerns bugs.<br \/>\nIt\u2019s true that a bug could exist in a specific method, but what interesting to know is how many methods are impacted by the bug, and the popular method are the most used ones and it\u2019s better to resolve them quickly.<\/p>\n<p style=\"color: #555555;\"><a style=\"color: #2970a6;\" href=\"http:\/\/javadepend.files.wordpress.com\/2013\/11\/plugins11.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-914\" src=\"http:\/\/javadepend.files.wordpress.com\/2013\/11\/plugins11.png?w=595\" alt=\"plugins11\" \/><\/a><\/p>\n<p style=\"color: #555555;\">Using CQLinq we can combine the result of all these tools and also the result of JArchitect to create more elaborated queries, and add these checks to the build process.<\/p>\n<h2 style=\"font-weight: bold; color: #555555;\">Issues Trend<\/h2>\n<p style=\"color: #555555;\">Having issues in a project is not an exception, we can say that\u2019s normal, however we have to check the quality trend of the project. Indeed it\u2019s a bad indicator if the number of issues grows after changes and evolutions.<br \/>\nJArchitect provides the\u00a0<b>Trend Monitoring<\/b>\u00a0feature to create trend charts. Trend charts are made of trend metrics values logged over time at analysis time. More than 50 trend metrics are available per default and it is easy to create your own trend metrics.<\/p>\n<p style=\"color: #555555;\">Let\u2019s create a trend metric for the Pmd issues:<\/p>\n<p style=\"color: #555555;\"><a style=\"color: #2970a6;\" href=\"http:\/\/javadepend.files.wordpress.com\/2013\/11\/plugins6.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-892\" src=\"http:\/\/javadepend.files.wordpress.com\/2013\/11\/plugins6.png?w=595\" alt=\"plugins6\" \/><\/a><\/p>\n<p style=\"color: #555555;\">And after you can easily create the trend chart to monitor the previous trend metric and add it to the JArchitect dashboard.<\/p>\n<p style=\"color: #555555;\"><a style=\"color: #2970a6;\" href=\"http:\/\/javadepend.files.wordpress.com\/2013\/11\/plugins19.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-932\" src=\"http:\/\/javadepend.files.wordpress.com\/2013\/11\/plugins19.png?w=595&amp;h=349\" alt=\"plugins19\" width=\"595\" height=\"349\" \/><\/a><\/p>\n<p style=\"color: #555555;\">With this trend chart we can monitor the evolution of the Pmd issues, and try to understand the reasons when the metric grows over versions.<\/p>\n<h2 style=\"font-weight: bold; color: #555555;\">Customize the JArchitect report<\/h2>\n<p style=\"color: #555555;\">JArchitect make possible to append extra report sections in the HTML report that lists some CQLinq queries.<br \/>\nIn the CQLinq Query Explorer panel, a particular CQLinq group reported is bordered with an orange rectangle.<\/p>\n<p style=\"color: #555555;\"><a style=\"color: #2970a6;\" href=\"http:\/\/javadepend.files.wordpress.com\/2013\/11\/plugins12.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-915\" src=\"http:\/\/javadepend.files.wordpress.com\/2013\/11\/plugins12.png?w=595&amp;h=180\" alt=\"plugins12\" width=\"595\" height=\"180\" \/><\/a><\/p>\n<p style=\"color: #555555;\">You can also add to the report the Pmd trend chart:<\/p>\n<p style=\"color: #555555;\"><a style=\"color: #2970a6;\" href=\"http:\/\/javadepend.files.wordpress.com\/2013\/11\/plugins15.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-920\" src=\"http:\/\/javadepend.files.wordpress.com\/2013\/11\/plugins15.png?w=595&amp;h=332\" alt=\"plugins15\" width=\"595\" height=\"332\" \/><\/a><\/p>\n<p style=\"color: #555555;\">And in the HTML report these added sections are accesible from the menu:<\/p>\n<p style=\"color: #555555;\"><a style=\"color: #2970a6;\" href=\"http:\/\/javadepend.files.wordpress.com\/2013\/11\/plugins13.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-916\" src=\"http:\/\/javadepend.files.wordpress.com\/2013\/11\/plugins13.png?w=595&amp;h=295\" alt=\"plugins13\" width=\"595\" height=\"295\" \/><\/a><\/p>\n<p style=\"color: #555555;\">And here\u2019s the page added in the report for the Pmd CQLinq queries:<\/p>\n<p style=\"color: #555555;\"><a style=\"color: #2970a6;\" href=\"http:\/\/javadepend.files.wordpress.com\/2013\/11\/plugins14.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-917\" src=\"http:\/\/javadepend.files.wordpress.com\/2013\/11\/plugins14.png?w=595&amp;h=346\" alt=\"plugins14\" width=\"595\" height=\"346\" \/><\/a><\/p>\n<h2 style=\"font-weight: bold; color: #555555;\">Conclusion<\/h2>\n<p style=\"color: #555555;\">JArchitect is open to other static analysis tools, and you can also plug your customized tool easily as descibed\u00a0<a style=\"color: #2970a6;\" href=\"http:\/\/www.jarchitect.com\/JArchitectPlugin.aspx\">here<\/a>. This way you can use all the JArchitect features to explore\u00a0better the result from the known java static analysis tools.<\/p>\n<div id=\"jp-post-flair\" class=\"sharedaddy sd-like-enabled sd-sharing-enabled\" style=\"color: #555555;\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Many Java static analysis tools exist right there, each one focus on a specific area and has its advantages, we can enumerate: Pmd\u00a0which is a static rule-set based Java source code analyzer that identifies potential problems like: Possible bugs\u2014Empty try\/catch\/finally\/switch blocks. Dead code\u2014Unused local variables, parameters and private methods. Empty if\/while statements. Overcomplicated expressions\u2014Unnecessary if &hellip; <a href=\"https:\/\/codergears.com\/Blog\/?p=705\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Make the most of Pmd, Findbugs and CheckStyle results.&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-705","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/705","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=705"}],"version-history":[{"count":10,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/705\/revisions"}],"predecessor-version":[{"id":818,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/705\/revisions\/818"}],"wp:attachment":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=705"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=705"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=705"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}