PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/pmd-4.2.5/docs/suppressing.html

#
HTML | 124 lines | 93 code | 31 blank | 0 comment | 0 complexity | dd61389de636a50cd9737fb49abf67c8 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head><title>PMD - Suppressing warnings</title><style type="text/css" media="all">
  2. @import url("./style/maven-base.css");
  3. @import url("./style/maven-theme.css");</style><link rel="stylesheet" href="./style/print.css" type="text/css" media="print"></link><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"></meta><meta name="author" content="Tom Copeland"></meta><meta name="email" content="tom@infoether.com"></meta></head><body class="composite"><div id="banner"><a href="http://pmd.sourceforge.net/" id="organizationLogo"><img alt="InfoEther" src="http://sourceforge.net/sflogo.php?group_id=56262&amp;type=5"></img></a><a href="http://pmd.sourceforge.net/" id="projectLogo"><img alt="PMD" src="./images/pmd_logo_small.jpg"></img></a><div class="clear"><hr></hr></div></div><div id="breadcrumbs"><div class="xleft"></div><div class="xright">
  4. <a href="http://pmdapplied.com/" class="externalLink" title="External Link">**** Get the book! ****</a>
  5. <span class="separator">|</span>
  6. <a href="http://sourceforge.net/projects/pmd" class="externalLink" title="External Link">SourceForge.net Project Page</a>
  7. <span class="separator">|</span>
  8. <a href="http://sourceforge.net" class="externalLink" title="External Link">Hosted by SourceForge</a>
  9. </div><div class="clear"><hr></hr></div></div><div id="leftColumn"><div id="navcolumn"><div id="menuOverview"><h5>Overview</h5><ul><li class="none"><a href="http://sourceforge.net/project/showfiles.php?group_id=56262&amp;package_id=51441&amp;release_id=632850" class="externalLink" title="External Link">Download PMD 4.2.5</a></li><li class="none"><a href="http://sourceforge.net/project/shownotes.php?release_id=632850&amp;group_id=56262" class="externalLink" title="External Link">What's new in PMD 4.2.5</a></li><li class="none"><a href="news.html">PMD in the news</a></li><li class="none"><a href="products.html">PMD-related products and books</a></li><li class="none"><a href="bestpractices.html">Best practices</a></li><li class="none"><a href="future.html">Future directions</a></li><li class="none"><a href="similar-projects.html">Similar projects</a></li><li class="none"><a href="credits.html">Credits</a></li><li class="none"><a href="license.html">License</a></li><li class="none"><a href="meaning.html">What does 'PMD' mean?</a></li></ul></div><div id="menuUsage"><h5>Usage</h5><ul><li class="none"><a href="installing.html">Installation</a></li><li class="none"><a href="running.html">Command line usage</a></li><li class="none"><a href="ant-task.html">Ant task usage</a></li><li class="none"><a href="maven-plugin.html">Maven plugin usage</a></li><li class="none"><a href="mvn-plugin.html">Mvn plugin usage</a></li><li class="none"><a href="integrations.html">IDE plugin usage</a></li><li class="none"><strong><a href="suppressing.html">Suppressing warnings</a></strong></li><li class="none"><a href="cpd.html">Finding duplicated code</a></li><li class="none"><a href="jspsupport.html">JSP support</a></li></ul></div><div id="menuCustomizing_PMD"><h5>Customizing PMD</h5><ul><li class="none"><a href="compiling.html">Compiling PMD</a></li><li class="none"><a href="howtowritearule.html">How to write a rule</a></li><li class="none"><a href="xpathruletutorial.html">Writing XPath rules</a></li><li class="none"><a href="howtomakearuleset.html">How to make a rule set</a></li><li class="none"><a href="howitworks.html">How it works</a></li><li class="none"><a href="rule-guidelines.html">Rule guidelines</a></li></ul></div><div id="menuFor_example"><h5>For example</h5><ul><li class="none"><a href="scoreboard.html">Run PMD on a Sourceforge project</a></li></ul></div><div id="menuRule_Sets"><h5>Rule Sets</h5><ul><li class="none"><a href="rules/index.html">Index</a></li><li class="none"><a href="rules/android.html">Android</a></li><li class="none"><a href="rules/basic.html">Basic</a></li><li class="none"><a href="rules/braces.html">Braces</a></li><li class="none"><a href="rules/codesize.html">Code Size</a></li><li class="none"><a href="rules/clone.html">Clone</a></li><li class="none"><a href="rules/controversial.html">Controversial</a></li><li class="none"><a href="rules/coupling.html">Coupling</a></li><li class="none"><a href="rules/design.html">Design</a></li><li class="none"><a href="rules/finalizers.html">Finalizers</a></li><li class="none"><a href="rules/imports.html">Import Statements</a></li><li class="none"><a href="rules/j2ee.html">J2EE</a></li><li class="none"><a href="rules/javabeans.html">Javabeans</a></li><li class="none"><a href="rules/junit.html">JUnit Tests</a></li><li class="none"><a href="rules/logging-java.html">Logging (Java)</a></li><li class="none"><a href="rules/logging-jakarta-commons.html">Logging (Jakarta)</a></li><li class="none"><a href="rules/migrating.html">Migrating</a></li><li class="none"><a href="rules/naming.html">Naming</a></li><li class="none"><a href="rules/optimizations.html">Optimizations</a></li><li class="none"><a href="rules/strictexception.html">Strict Exceptions</a></li><li class="none"><a href="rules/strings.html">Strings</a></li><li class="none"><a href="rules/sunsecure.html">Sun Security</a></li><li class="none"><a href="rules/unusedcode.html">Unused Code</a></li><li class="none"><a href="rules/basic-jsp.html">Java Server Pages</a></li><li class="none"><a href="rules/basic-jsf.html">Java Server Faces</a></li></ul></div><div id="menuProject_Documentation"><h5>Project Documentation</h5><ul><li class="none"><a href="index.html">About</a></li><li class="collapsed"><a href="project-info.html">Project Info</a></li><li class="collapsed"><a href="maven-reports.html">Project Reports</a></li><li class="none"><a href="development-process.html">Development Process</a></li></ul></div><div id="legend"><h5>Legend</h5><ul><li class="externalLink">External Link</li><li class="newWindow">Opens in a new window</li></ul></div><a href="http://maven.apache.org/" title="Built by Maven" id="poweredBy"><img alt="Built by Maven" src="./images/logos/mavenlogo_builtby_w.png"></img></a></div></div><div id="bodyColumn"><div class="contentBox"><div class="section"><a name="Suppressing_warnings"></a><h2>Suppressing warnings</h2>
  10. <div class="subsection"><a name="Annotations"></a><h3>Annotations</h3>
  11. <p>You can use a JDK 1.5 annotation to suppress PMD warnings, like this:</p>
  12. <div class="source"><pre>
  13. // This will suppress all the PMD warnings in this class
  14. @SuppressWarnings("PMD")
  15. public class Bar {
  16. void bar() {
  17. int foo;
  18. }
  19. }
  20. </pre></div>
  21. <p>Or you can suppress one rule with an annotation like this:</p>
  22. <div class="source"><pre>
  23. // This will suppress UnusedLocalVariable warnings in this class
  24. @SuppressWarnings("PMD.UnusedLocalVariable")
  25. public class Bar {
  26. void bar() {
  27. int foo;
  28. }
  29. }
  30. </pre></div>
  31. <p>PMD also obeys the JDK annotation @SuppressWarnings("unused"), which will apply to all rules in the unused ruleset</p>
  32. <div class="source"><pre>
  33. // This will suppress UnusedLocalVariable and UnusedPrivateMethod warnings in this class
  34. @SuppressWarnings("unused")
  35. public class Bar {
  36. void bar() {
  37. int foo;
  38. }
  39. private void foobar(){}
  40. }
  41. </pre></div>
  42. </div>
  43. <div class="subsection"><a name="NOPMD"></a><h3>NOPMD</h3>
  44. <p>Alternatively, you can tell PMD to ignore a specific line by using the "NOPMD" marker, like this:</p>
  45. <div class="source"><pre>
  46. public class Bar {
  47. // 'bar' is accessed by a native method, so we want to suppress warnings for it
  48. private int bar; //NOPMD
  49. }
  50. </pre></div>
  51. <p>You can use whatever text string you want to suppress warnings, for example, here's
  52. how to use TURN_OFF_WARNINGS as the suppressor:</p>
  53. <div class="source"><pre>
  54. $ cat Foo.java
  55. public class Foo {
  56. void bar() {
  57. int x = 2; // TURN_OFF_WARNINGS
  58. }
  59. }
  60. $ ./pmd.sh Foo.java text unusedcode -excludemarker TURN_OFF_WARNINGS
  61. No problems found!
  62. UnusedLocalVariable rule violation suppressed by //NOPMD in /home/tom/pmd/pmd/bin/Foo.java
  63. </pre></div>
  64. <p>Note that PMD expects the //NOPMD marker to be on the same line as the violation. So, for
  65. example, if you want to suppress an "empty if statement" warning, you'll need to place it on
  66. the line containing the "if" keyword, e.g.:</p>
  67. <div class="source"><pre>
  68. $ cat ~/tmp/Foo.java
  69. public class Foo {
  70. void bar() {
  71. int x = 42;
  72. if (x &gt; 5) { // NOPMD
  73. }
  74. }
  75. }
  76. $ java net.sourceforge.pmd.PMD ~/tmp/Foo.java text basic
  77. No problems found!
  78. $
  79. </pre></div>
  80. <p>A message placed after the NOPMD marker will get placed in the report, e.g.:</p>
  81. <div class="source"><pre>
  82. public class Foo {
  83. void bar() {
  84. try {
  85. bar();
  86. } catch (FileNotFoundException e) {} // NOPMD - this surely will never happen
  87. }
  88. }
  89. }
  90. </pre></div>
  91. </div>
  92. <p>Suggestions? Comments? Post them <a href="http://sourceforge.net/forum/forum.php?forum_id=188192" class="externalLink" title="External Link">here</a>. Thanks!</p>
  93. </div></div></div><div class="clear"><hr></hr></div><div id="footer"><div class="xright">Š 2002-2009, InfoEther</div><div class="clear"><hr></hr></div></div></body></html>