pmd/pmd_rules_java_bestpractices.html

3981 lines
245 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Rules which enforce generally accepted best practices.">
<meta name="keywords" content=" Best Practices, AbstractClassWithoutAbstractMethod, AccessorClassGeneration, AccessorMethodGeneration, ArrayIsStoredDirectly, AvoidMessageDigestField, AvoidPrintStackTrace, AvoidReassigningCatchVariables, AvoidReassigningLoopVariables, AvoidReassigningParameters, AvoidStringBufferField, AvoidUsingHardCodedIP, CheckResultSet, ConstantsInInterface, DefaultLabelNotLastInSwitchStmt, DoubleBraceInitialization, ForLoopCanBeForeach, ForLoopVariableCount, GuardLogStatement, JUnit4SuitesShouldUseSuiteAnnotation, JUnit4TestShouldUseAfterAnnotation, JUnit4TestShouldUseBeforeAnnotation, JUnit4TestShouldUseTestAnnotation, JUnit5TestShouldBePackagePrivate, JUnitAssertionsShouldIncludeMessage, JUnitTestContainsTooManyAsserts, JUnitTestsShouldIncludeAssert, JUnitUseExpected, LiteralsFirstInComparisons, LooseCoupling, MethodReturnsInternalArray, MissingOverride, OneDeclarationPerLine, PositionLiteralsFirstInCaseInsensitiveComparisons, PositionLiteralsFirstInComparisons, PreserveStackTrace, PrimitiveWrapperInstantiation, ReplaceEnumerationWithIterator, ReplaceHashtableWithMap, ReplaceVectorWithList, SimplifiableTestAssertion, SwitchStmtsShouldHaveDefault, SystemPrintln, UnusedAssignment, UnusedFormalParameter, UnusedImports, UnusedLocalVariable, UnusedPrivateField, UnusedPrivateMethod, UseAssertEqualsInsteadOfAssertTrue, UseAssertNullInsteadOfAssertTrue, UseAssertSameInsteadOfAssertTrue, UseAssertTrueInsteadOfAssertEquals, UseCollectionIsEmpty, UseStandardCharsets, UseTryWithResources, UseVarargs, WhileLoopWithLiteralBoolean">
<title>Best Practices | PMD Source Code Analyzer</title>
<link rel="stylesheet" href="css/syntax.css">
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
<!--<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">-->
<link rel="stylesheet" href="css/modern-business.css">
<link rel="stylesheet" href="css/lavish-bootstrap.css">
<link rel="stylesheet" href="css/customstyles.css">
<link rel="stylesheet" href="css/theme-blue.css">
<link rel="stylesheet" href="css/pmd-customstyles.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
<script src="js/jquery.navgoco.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/anchor-js/2.0.0/anchor.min.js"></script>
<script src="js/toc.js"></script>
<script src="js/customscripts.js"></script>
<link rel="shortcut icon" href="images/favicon.ico" type="image/x-icon">
<link rel="icon" href="images/favicon.ico" type="image/x-icon">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
<link rel="alternate" type="application/rss+xml" title="" href="https://pmd.github.io/pmd/feed.xml">
<script>
$(document).ready(function() {
// Initialize navgoco with default options
$("#mysidebar").navgoco({
caretHtml: '',
accordion: true,
openClass: 'active', // open
save: false, // leave false or nav highlighting doesn't work right
cookie: {
name: 'navgoco',
expires: false,
path: '/'
},
slide: {
duration: 400,
easing: 'swing'
}
});
$("#collapseAll").click(function(e) {
e.preventDefault();
$("#mysidebar").navgoco('toggle', false);
});
$("#expandAll").click(function(e) {
e.preventDefault();
$("#mysidebar").navgoco('toggle', true);
});
});
</script>
<script>
$(function () {
$('[data-toggle="tooltip"]').tooltip()
})
</script>
<script>
$(document).ready(function() {
$("#tg-sb-link").click(function() {
$("#tg-sb-sidebar").toggle();
$("#tg-sb-content").toggleClass('col-md-9');
$("#tg-sb-content").toggleClass('col-md-12');
$("#tg-sb-icon").toggleClass('fa-toggle-on');
$("#tg-sb-icon").toggleClass('fa-toggle-off');
});
});
</script>
</head>
<body>
<!-- Content is offset by the height of the topnav bar. -->
<!-- There's already a padding-top rule in modern-business.css, but it apparently doesn't work on Firefox 60 and Chrome 67 -->
<div id="topbar-content-offset">
<!-- Navigation -->
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container topnavlinks">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="fa fa-home fa-lg navbar-brand" href="index.html">&nbsp;<span class="projectTitle"> PMD Source Code Analyzer Project</span></a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
<!-- toggle sidebar button -->
<li><a id="tg-sb-link" href="#"><i id="tg-sb-icon" class="fa fa-toggle-on"></i> Nav</a></li>
<!-- entries without drop-downs appear here -->
<li><a href="https://github.com/pmd/pmd/releases/latest" target="_blank">Download</a></li>
<li><a href="https://github.com/pmd/pmd" target="_blank">Fork us on github</a></li>
<!-- entries with drop-downs appear here -->
<!-- conditional logic to control which topnav appears for the audience defined in the configuration file.-->
<!--comment out this block if you want to hide search-->
<li>
<!--start search-->
<div id="search-demo-container">
<input type="text" id="search-input" placeholder="search...">
<ul id="results-container"></ul>
</div>
<script src="js/jekyll-search.js" type="text/javascript"></script>
<script type="text/javascript">
SimpleJekyllSearch.init({
searchInput: document.getElementById('search-input'),
resultsContainer: document.getElementById('results-container'),
dataSource: 'search.json',
searchResultTemplate: '<li><a href="{url}" title="Best Practices">{title}</a></li>',
noResultsText: 'No results found.',
limit: 10,
fuzzy: true,
})
</script>
<!--end search-->
</li>
</ul>
</div>
</div>
<!-- /.container -->
</nav>
<!-- Page Content -->
<div class="container">
<div class="col-lg-12">&nbsp;</div>
<!-- Content Row -->
<div class="row">
<!-- Sidebar Column -->
<div class="col-md-3" id="tg-sb-sidebar">
<ul id="mysidebar" class="nav">
<li class="sidebarTitle">PMD 6.39.0-SNAPSHOT</li>
<li>
<a href="#">About</a>
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="pmd_release_notes.html">Release notes</a></li>
<li><a href="pmd_next_major_development.html">PMD 7.0.0 development</a></li>
<li><a href="pmd_about_help.html">Getting help</a></li>
</ul>
</li>
<li>
<a href="#">User Documentation</a>
<ul>
<li><a href="pmd_userdocs_installation.html">Installation and basic CLI usage</a></li>
<li><a href="pmd_userdocs_making_rulesets.html">Making rulesets</a></li>
<li><a href="pmd_userdocs_configuring_rules.html">Configuring rules</a></li>
<li><a href="pmd_userdocs_best_practices.html">Best practices</a></li>
<li><a href="pmd_userdocs_suppressing_warnings.html">Suppressing warnings</a></li>
<li><a href="pmd_userdocs_incremental_analysis.html">Incremental analysis</a></li>
<li><a href="pmd_userdocs_cli_reference.html">PMD CLI reference</a></li>
<li><a href="pmd_userdocs_report_formats.html">PMD Report formats</a></li>
<li class="subfolders">
<a href="#">CPD reference</a>
<ul>
<li><a href="pmd_userdocs_cpd.html">Copy-paste detection</a></li>
<li><a href="pmd_userdocs_cpd_report_formats.html">CPD Report formats</a></li>
</ul>
</li>
<li class="subfolders">
<a href="#">Extending PMD</a>
<ul>
<li><a href="pmd_userdocs_extending_writing_rules_intro.html">Introduction to writing rules</a></li>
<li><a href="pmd_userdocs_extending_your_first_rule.html">Your first rule</a></li>
<li><a href="pmd_userdocs_extending_writing_xpath_rules.html">XPath rules</a></li>
<li><a href="pmd_userdocs_extending_writing_java_rules.html">Java rules</a></li>
<li><a href="pmd_userdocs_extending_designer_reference.html">Rule designer reference</a></li>
<li><a href="pmd_userdocs_extending_defining_properties.html">Defining rule properties</a></li>
<li><a href="pmd_userdocs_extending_metrics_howto.html">Using and defining code metrics</a></li>
<li><a href="pmd_userdocs_extending_rule_guidelines.html">Rule guidelines</a></li>
<li><a href="pmd_userdocs_extending_testing.html">Testing your rules</a></li>
</ul>
</li>
<li class="subfolders">
<a href="#">Tools / Integrations</a>
<ul>
<li><a href="pmd_userdocs_tools_maven.html">Maven PMD Plugin</a></li>
<li><a href="pmd_userdocs_tools_gradle.html">Gradle</a></li>
<li><a href="pmd_userdocs_tools_ant.html">Ant</a></li>
<li><a href="pmd_userdocs_tools_java_api.html">PMD Java API</a></li>
<li><a href="pmd_userdocs_tools_ci.html">CI integrations</a></li>
<li><a href="pmd_userdocs_tools.html">Other Tools / Integrations</a></li>
</ul>
</li>
</ul>
</li>
<li>
<a href="#">Rule Reference</a>
<ul>
<li class="subfolders">
<a href="#">Apex Rules</a>
<ul>
<li><a href="pmd_rules_apex.html">Index</a></li>
<li><a href="pmd_rules_apex_bestpractices.html">Best Practices</a></li>
<li><a href="pmd_rules_apex_codestyle.html">Code Style</a></li>
<li><a href="pmd_rules_apex_design.html">Design</a></li>
<li><a href="pmd_rules_apex_documentation.html">Documentation</a></li>
<li><a href="pmd_rules_apex_errorprone.html">Error Prone</a></li>
<li><a href="pmd_rules_apex_performance.html">Performance</a></li>
<li><a href="pmd_rules_apex_security.html">Security</a></li>
</ul>
</li>
<li class="subfolders">
<a href="#">Ecmascript Rules</a>
<ul>
<li><a href="pmd_rules_ecmascript.html">Index</a></li>
<li><a href="pmd_rules_ecmascript_bestpractices.html">Best Practices</a></li>
<li><a href="pmd_rules_ecmascript_codestyle.html">Code Style</a></li>
<li><a href="pmd_rules_ecmascript_errorprone.html">Error Prone</a></li>
</ul>
</li>
<li class="subfolders">
<a href="#">Java Rules</a>
<ul>
<li><a href="pmd_rules_java.html">Index</a></li>
<li class="active"><a href="pmd_rules_java_bestpractices.html">Best Practices</a></li>
<li><a href="pmd_rules_java_codestyle.html">Code Style</a></li>
<li><a href="pmd_rules_java_design.html">Design</a></li>
<li><a href="pmd_rules_java_documentation.html">Documentation</a></li>
<li><a href="pmd_rules_java_errorprone.html">Error Prone</a></li>
<li><a href="pmd_rules_java_multithreading.html">Multithreading</a></li>
<li><a href="pmd_rules_java_performance.html">Performance</a></li>
<li><a href="pmd_rules_java_security.html">Security</a></li>
</ul>
</li>
<li class="subfolders">
<a href="#">Java Server Pages Rules</a>
<ul>
<li><a href="pmd_rules_jsp.html">Index</a></li>
<li><a href="pmd_rules_jsp_bestpractices.html">Best Practices</a></li>
<li><a href="pmd_rules_jsp_codestyle.html">Code Style</a></li>
<li><a href="pmd_rules_jsp_design.html">Design</a></li>
<li><a href="pmd_rules_jsp_errorprone.html">Error Prone</a></li>
<li><a href="pmd_rules_jsp_security.html">Security</a></li>
</ul>
</li>
<li class="subfolders">
<a href="#">Maven POM Rules</a>
<ul>
<li><a href="pmd_rules_pom.html">Index</a></li>
<li><a href="pmd_rules_pom_errorprone.html">Error Prone</a></li>
</ul>
</li>
<li class="subfolders">
<a href="#">Modelica Rules</a>
<ul>
<li><a href="pmd_rules_modelica.html">Index</a></li>
<li><a href="pmd_rules_modelica_bestpractices.html">Best Practices</a></li>
</ul>
</li>
<li class="subfolders">
<a href="#">PLSQL Rules</a>
<ul>
<li><a href="pmd_rules_plsql.html">Index</a></li>
<li><a href="pmd_rules_plsql_bestpractices.html">Best Practices</a></li>
<li><a href="pmd_rules_plsql_codestyle.html">Code Style</a></li>
<li><a href="pmd_rules_plsql_design.html">Design</a></li>
<li><a href="pmd_rules_plsql_errorprone.html">Error Prone</a></li>
</ul>
</li>
<li class="subfolders">
<a href="#">Salesforce VisualForce Rules</a>
<ul>
<li><a href="pmd_rules_vf.html">Index</a></li>
<li><a href="pmd_rules_vf_security.html">Security</a></li>
</ul>
</li>
<li class="subfolders">
<a href="#">VM Rules</a>
<ul>
<li><a href="pmd_rules_vm.html">Index</a></li>
<li><a href="pmd_rules_vm_bestpractices.html">Best Practices</a></li>
<li><a href="pmd_rules_vm_design.html">Design</a></li>
<li><a href="pmd_rules_vm_errorprone.html">Error Prone</a></li>
</ul>
</li>
<li class="subfolders">
<a href="#">XML Rules</a>
<ul>
<li><a href="pmd_rules_xml.html">Index</a></li>
<li><a href="pmd_rules_xml_errorprone.html">Error Prone</a></li>
</ul>
</li>
<li class="subfolders">
<a href="#">XSL Rules</a>
<ul>
<li><a href="pmd_rules_xsl.html">Index</a></li>
<li><a href="pmd_rules_xsl_codestyle.html">Code Style</a></li>
<li><a href="pmd_rules_xsl_performance.html">Performance</a></li>
</ul>
</li>
</ul>
</li>
<li>
<a href="#">Language Specific Documentation</a>
<ul>
<li><a href="pmd_languages_jsp.html">JSP Support</a></li>
<li><a href="pmd_java_metrics_index.html">Java code metrics</a></li>
<li><a href="pmd_apex_metrics_index.html">Apex code metrics</a></li>
<li><a href="pmd_languages_plsql.html">PLSQL</a></li>
</ul>
</li>
<li>
<a href="#">Developer Documentation</a>
<ul>
<li><a href="pmd_devdocs_development.html">Developer resources</a></li>
<li><a href="pmd_devdocs_building.html">Building PMD from source</a></li>
<li><a href="https://github.com/pmd/pmd/blob/master/CONTRIBUTING.md" target="_blank">Contributing</a></li>
<li><a href="pmd_devdocs_writing_documentation.html">Writing documentation</a></li>
<li><a href="pmd_devdocs_roadmap.html">Roadmap</a></li>
<li><a href="pmd_devdocs_how_pmd_works.html">How PMD works</a></li>
<li><a href="pmd_devdocs_pmdtester.html">Pmdtester</a></li>
<li><a href="pmd_devdocs_rule_deprecation_policy.html">Rule Deprecation Policy</a></li>
<li class="subfolders">
<a href="#">Major contributions</a>
<ul>
<li><a href="pmd_devdocs_major_rule_guidelines.html">Rule Guidelines</a></li>
<li><a href="pmd_devdocs_major_adding_new_language.html">Adding a new language</a></li>
<li><a href="pmd_devdocs_major_adding_new_cpd_language.html">Adding a new CPD language</a></li>
<li><a href="pmd_devdocs_major_adding_new_metrics_framework.html">Adding metrics support to a language</a></li>
</ul>
</li>
<li class="subfolders">
<a href="#">Experimental features</a>
<ul>
<li><a href="pmd_devdocs_experimental_ast_dump.html">Creating (XML) dump of the AST</a></li>
</ul>
</li>
</ul>
</li>
<li>
<a href="#">Project documentation</a>
<ul>
<li class="subfolders">
<a href="#">Trivia about PMD</a>
<ul>
<li><a href="pmd_projectdocs_trivia_news.html">PMD in the press</a></li>
<li><a href="pmd_projectdocs_trivia_products.html">Products & books related to PMD</a></li>
<li><a href="pmd_projectdocs_trivia_similarprojects.html">Similar projects</a></li>
<li><a href="pmd_projectdocs_trivia_meaning.html">What does 'PMD' mean?</a></li>
</ul>
</li>
<li><a href="pmd_projectdocs_faq.html">FAQ</a></li>
<li><a href="license.html">License</a></li>
<li><a href="pmd_projectdocs_credits.html">Credits</a></li>
<li><a href="pmd_release_notes_old.html">Old release notes</a></li>
<li class="subfolders">
<a href="#">Project management</a>
<ul>
<li><a href="pmd_projectdocs_committers_infrastructure.html">Infrastructure</a></li>
<li><a href="pmd_projectdocs_committers_releasing.html">Release process</a></li>
<li><a href="pmd_projectdocs_committers_merging_pull_requests.html">Merging pull requests</a></li>
<li><a href="pmd_projectdocs_committers_main_landing_page.html">Main Landing page</a></li>
</ul>
</li>
</ul>
</li>
<!-- if you aren't using the accordion, uncomment this block:
<p class="external">
<a href="#" id="collapseAll">Collapse All</a> | <a href="#" id="expandAll">Expand All</a>
</p>
-->
</ul>
<!-- this highlights the active parent class in the navgoco sidebar. this is critical so that the parent expands when you're viewing a page. This must appear below the sidebar code above. Otherwise, if placed inside customscripts.js, the script runs before the sidebar code runs and the class never gets inserted.-->
<script>$("li.active").parents('li').toggleClass("active");</script>
</div>
<!-- Content Column -->
<div class="col-md-9" id="tg-sb-content">
<div class="post-header">
<h1 class="post-title-main">Best Practices</h1>
</div>
<div class="post-content">
<div class="summary">Rules which enforce generally accepted best practices.</div>
<!-- this handles the automatic toc. use ## for subheads to auto-generate the on-page minitoc. if you use html tags, you must supply an ID for the heading element in order for it to appear in the minitoc. -->
<script>
$( document ).ready(function() {
// Handler for .ready() called.
$('#toc').toc({ minimumHeaders: 0, listType: 'ul', showSpeed: 0, headers: 'h2,h3,h4' });
});
</script>
<div id="toc"></div>
<a target="_blank" href="https://github.com/pmd/pmd/blob/master/docs/../pmd-java/src/main/resources/category/java/bestpractices.xml" class="btn btn-default githubEditButton" role="button"><i class="fa fa-github fa-lg"></i> Edit me</a>
<!-- DO NOT EDIT THIS FILE. This file is generated from file ../pmd-java/src/main/resources/category/java/bestpractices.xml. -->
<h2 id="abstractclasswithoutabstractmethod">AbstractClassWithoutAbstractMethod</h2>
<p><strong>Since:</strong> PMD 3.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>The abstract class does not contain any abstract methods. An abstract class suggests
an incomplete implementation, which is to be completed by subclasses implementing the
abstract methods. If the class is intended to be used as a base class only (not to be instantiated
directly) a protected constructor can be provided prevent direct instantiation.</p>
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/AbstractClassWithoutAbstractMethodRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.AbstractClassWithoutAbstractMethodRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">abstract</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
<span class="kt">void</span> <span class="kt">int</span> <span class="nf">method1</span><span class="o">()</span> <span class="o">{</span> <span class="o">...</span> <span class="o">}</span>
<span class="kt">void</span> <span class="kt">int</span> <span class="nf">method2</span><span class="o">()</span> <span class="o">{</span> <span class="o">...</span> <span class="o">}</span>
<span class="c1">// consider using abstract methods or removing</span>
<span class="c1">// the abstract modifier and adding protected constructors</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>Use this rule by referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/AbstractClassWithoutAbstractMethod"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="accessorclassgeneration">AccessorClassGeneration</h2>
<p><strong>Since:</strong> PMD 1.04</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Instantiation by way of private constructors from outside of the constructors class often causes the
generation of an accessor. A factory method, or non-privatization of the constructor can eliminate this
situation. The generated class file is actually an interface. It gives the accessing class the ability
to invoke a new hidden package scope constructor that takes the interface as a supplementary parameter.
This turns a private constructor effectively into one with package scope, and is challenging to discern.</p>
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/AccessorClassGenerationRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.AccessorClassGenerationRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Outer</span> <span class="o">{</span>
<span class="kt">void</span> <span class="nf">method</span><span class="o">(){</span>
<span class="nc">Inner</span> <span class="n">ic</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">Inner</span><span class="o">();</span><span class="c1">//Causes generation of accessor class</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">Inner</span> <span class="o">{</span>
<span class="kd">private</span> <span class="nf">Inner</span><span class="o">(){}</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>Use this rule by referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/AccessorClassGeneration"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="accessormethodgeneration">AccessorMethodGeneration</h2>
<p><strong>Since:</strong> PMD 5.5.4</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>When accessing private fields / methods from another class, the Java compiler will generate accessor methods
with package-private visibility. This adds overhead, and to the dex method count on Android. This situation can
be avoided by changing the visibility of the field / method from private to package-private.</p>
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/AccessorMethodGenerationRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.AccessorMethodGenerationRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">OuterClass</span> <span class="o">{</span>
<span class="kd">private</span> <span class="kt">int</span> <span class="n">counter</span><span class="o">;</span>
<span class="cm">/* package */</span> <span class="kt">int</span> <span class="n">id</span><span class="o">;</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">InnerClass</span> <span class="o">{</span>
<span class="nc">InnerClass</span><span class="o">()</span> <span class="o">{</span>
<span class="nc">OuterClass</span><span class="o">.</span><span class="na">this</span><span class="o">.</span><span class="na">counter</span><span class="o">++;</span> <span class="c1">// wrong accessor method will be generated</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kt">int</span> <span class="nf">getOuterClassId</span><span class="o">()</span> <span class="o">{</span>
<span class="k">return</span> <span class="nc">OuterClass</span><span class="o">.</span><span class="na">this</span><span class="o">.</span><span class="na">id</span><span class="o">;</span> <span class="c1">// id is package-private, no accessor method needed</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>Use this rule by referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/AccessorMethodGeneration"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="arrayisstoreddirectly">ArrayIsStoredDirectly</h2>
<p><strong>Since:</strong> PMD 2.2</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Constructors and methods receiving arrays should clone objects and store the copy.
This prevents future changes from the user from affecting the original array.</p>
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/ArrayIsStoredDirectlyRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.ArrayIsStoredDirectlyRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
<span class="kd">private</span> <span class="nc">String</span> <span class="o">[]</span> <span class="n">x</span><span class="o">;</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">foo</span> <span class="o">(</span><span class="nc">String</span> <span class="o">[]</span> <span class="n">param</span><span class="o">)</span> <span class="o">{</span>
<span class="c1">// Don't do this, make a copy of the array at least</span>
<span class="k">this</span><span class="o">.</span><span class="na">x</span><span class="o">=</span><span class="n">param</span><span class="o">;</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>This rule has the following properties:</strong></p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Default Value</th>
<th>Description</th>
<th>Multivalued</th>
</tr>
</thead>
<tbody>
<tr>
<td>allowPrivate</td>
<td>true</td>
<td>If true, allow private methods/constructors to store arrays directly</td>
<td>no</td>
</tr>
</tbody>
</table>
<p><strong>Use this rule with the default properties by just referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/ArrayIsStoredDirectly"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<p><strong>Use this rule and customize it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/ArrayIsStoredDirectly"</span><span class="nt">&gt;</span>
<span class="nt">&lt;properties&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"allowPrivate"</span> <span class="na">value=</span><span class="s">"true"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;/properties&gt;</span>
<span class="nt">&lt;/rule&gt;</span>
</code></pre></div></div>
<h2 id="avoidmessagedigestfield">AvoidMessageDigestField</h2>
<p><strong>Since:</strong> PMD 6.18.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Declaring a MessageDigest instance as a field make this instance directly available to multiple threads.
Such sharing of MessageDigest instances should be avoided if possible since it leads to wrong results
if the access is not synchronized correctly.
Just create a new instance and use it locally, where you need it.
Creating a new instance is easier than synchronizing access to a shared instance.</p>
<p><strong>This rule is defined by the following XPath expression:</strong></p>
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">FieldDeclaration</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'java.security.MessageDigest'</span><span class="p">)]</span><span class="w">
</span></code></pre></div></div>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">import</span> <span class="nn">java.security.MessageDigest</span><span class="o">;</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">AvoidMessageDigestFieldExample</span> <span class="o">{</span>
<span class="kd">private</span> <span class="kd">final</span> <span class="nc">MessageDigest</span> <span class="n">sharedMd</span><span class="o">;</span>
<span class="kd">public</span> <span class="nf">AvoidMessageDigestFieldExample</span><span class="o">()</span> <span class="kd">throws</span> <span class="nc">Exception</span> <span class="o">{</span>
<span class="n">sharedMd</span> <span class="o">=</span> <span class="nc">MessageDigest</span><span class="o">.</span><span class="na">getInstance</span><span class="o">(</span><span class="s">"SHA-256"</span><span class="o">);</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kt">byte</span><span class="o">[]</span> <span class="nf">calculateHashShared</span><span class="o">(</span><span class="kt">byte</span><span class="o">[]</span> <span class="n">data</span><span class="o">)</span> <span class="o">{</span>
<span class="c1">// sharing a MessageDigest like this without synchronizing access</span>
<span class="c1">// might lead to wrong results</span>
<span class="n">sharedMd</span><span class="o">.</span><span class="na">reset</span><span class="o">();</span>
<span class="n">sharedMd</span><span class="o">.</span><span class="na">update</span><span class="o">(</span><span class="n">data</span><span class="o">);</span>
<span class="k">return</span> <span class="n">sharedMd</span><span class="o">.</span><span class="na">digest</span><span class="o">();</span>
<span class="o">}</span>
<span class="c1">// better</span>
<span class="kd">public</span> <span class="kt">byte</span><span class="o">[]</span> <span class="nf">calculateHash</span><span class="o">(</span><span class="kt">byte</span><span class="o">[]</span> <span class="n">data</span><span class="o">)</span> <span class="kd">throws</span> <span class="nc">Exception</span> <span class="o">{</span>
<span class="nc">MessageDigest</span> <span class="n">md</span> <span class="o">=</span> <span class="nc">MessageDigest</span><span class="o">.</span><span class="na">getInstance</span><span class="o">(</span><span class="s">"SHA-256"</span><span class="o">);</span>
<span class="n">md</span><span class="o">.</span><span class="na">update</span><span class="o">(</span><span class="n">data</span><span class="o">);</span>
<span class="k">return</span> <span class="n">md</span><span class="o">.</span><span class="na">digest</span><span class="o">();</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>Use this rule by referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/AvoidMessageDigestField"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="avoidprintstacktrace">AvoidPrintStackTrace</h2>
<p><strong>Since:</strong> PMD 3.2</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Avoid printStackTrace(); use a logger call instead.</p>
<p><strong>This rule is defined by the following XPath expression:</strong></p>
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">PrimaryExpression</span><span class="p">[</span><span class="w">
</span><span class="p">(</span><span class="w"> </span><span class="nt">PrimaryPrefix</span><span class="p">[</span><span class="nt">Name</span><span class="p">[</span><span class="nf">contains</span><span class="p">(</span><span class="na">@Image</span><span class="p">,</span><span class="s1">'printStackTrace'</span><span class="p">)]]</span><span class="w">
</span><span class="p">|</span><span class="w"> </span><span class="nt">PrimarySuffix</span><span class="p">[</span><span class="na">@Image</span><span class="err">=</span><span class="s1">'printStackTrace'</span><span class="p">]</span><span class="w">
</span><span class="p">)</span><span class="o">/</span><span class="kn">following-sibling::</span><span class="o">*</span><span class="p">[</span><span class="m">1</span><span class="p">][</span><span class="kn">self::</span><span class="nt">PrimarySuffix</span><span class="o">/</span><span class="nt">Arguments</span><span class="p">[</span><span class="na">@Size</span><span class="err">=</span><span class="m">0</span><span class="p">]]</span><span class="w">
</span><span class="p">]</span><span class="w">
</span></code></pre></div></div>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
<span class="kt">void</span> <span class="nf">bar</span><span class="o">()</span> <span class="o">{</span>
<span class="k">try</span> <span class="o">{</span>
<span class="c1">// do something</span>
<span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="nc">Exception</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
<span class="n">e</span><span class="o">.</span><span class="na">printStackTrace</span><span class="o">();</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>Use this rule by referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/AvoidPrintStackTrace"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="avoidreassigningcatchvariables">AvoidReassigningCatchVariables</h2>
<p><strong>Since:</strong> PMD 6.27.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Reassigning exception variables caught in a catch statement should be avoided because of:</p>
<p>1) If it is needed, multi catch can be easily added and code will still compile.</p>
<p>2) Following the principle of least surprise we want to make sure that a variable caught in a catch statement
is always the one thrown in a try block.</p>
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/AvoidReassigningCatchVariablesRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.AvoidReassigningCatchVariablesRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">foo</span><span class="o">()</span> <span class="o">{</span>
<span class="k">try</span> <span class="o">{</span>
<span class="c1">// do something</span>
<span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="nc">Exception</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
<span class="n">e</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">NullPointerException</span><span class="o">();</span> <span class="c1">// not recommended</span>
<span class="o">}</span>
<span class="k">try</span> <span class="o">{</span>
<span class="c1">// do something</span>
<span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="nc">MyException</span> <span class="o">|</span> <span class="nc">ServerException</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
<span class="n">e</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">RuntimeException</span><span class="o">();</span> <span class="c1">// won't compile</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>Use this rule by referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/AvoidReassigningCatchVariables"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="avoidreassigningloopvariables">AvoidReassigningLoopVariables</h2>
<p><strong>Since:</strong> PMD 6.11.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Reassigning loop variables can lead to hard-to-find bugs. Prevent or limit how these variables can be changed.</p>
<p>In foreach-loops, configured by the <code class="language-plaintext highlighter-rouge">foreachReassign</code> property:</p>
<ul>
<li><code class="language-plaintext highlighter-rouge">deny</code>: Report any reassignment of the loop variable in the loop body. <em>This is the default.</em></li>
<li><code class="language-plaintext highlighter-rouge">allow</code>: Dont check the loop variable.</li>
<li><code class="language-plaintext highlighter-rouge">firstOnly</code>: Report any reassignments of the loop variable, except as the first statement in the loop body.
<em>This is useful if some kind of normalization or clean-up of the value before using is permitted, but any other change of the variable is not.</em></li>
</ul>
<p>In for-loops, configured by the <code class="language-plaintext highlighter-rouge">forReassign</code> property:</p>
<ul>
<li><code class="language-plaintext highlighter-rouge">deny</code>: Report any reassignment of the control variable in the loop body. <em>This is the default.</em></li>
<li><code class="language-plaintext highlighter-rouge">allow</code>: Dont check the control variable.</li>
<li><code class="language-plaintext highlighter-rouge">skip</code>: Report any reassignments of the control variable, except conditional increments/decrements (<code class="language-plaintext highlighter-rouge">++</code>, <code class="language-plaintext highlighter-rouge">--</code>, <code class="language-plaintext highlighter-rouge">+=</code>, <code class="language-plaintext highlighter-rouge">-=</code>).
<em>This prevents accidental reassignments or unconditional increments of the control variable.</em></li>
</ul>
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/AvoidReassigningLoopVariablesRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.AvoidReassigningLoopVariablesRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
<span class="kd">private</span> <span class="kt">void</span> <span class="nf">foo</span><span class="o">()</span> <span class="o">{</span>
<span class="k">for</span> <span class="o">(</span><span class="nc">String</span> <span class="n">s</span> <span class="o">:</span> <span class="n">listOfStrings</span><span class="o">())</span> <span class="o">{</span>
<span class="n">s</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="na">trim</span><span class="o">();</span> <span class="c1">// OK, when foreachReassign is "firstOnly" or "allow"</span>
<span class="n">doSomethingWith</span><span class="o">(</span><span class="n">s</span><span class="o">);</span>
<span class="n">s</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="na">toUpper</span><span class="o">();</span> <span class="c1">// OK, when foreachReassign is "allow"</span>
<span class="n">doSomethingElseWith</span><span class="o">(</span><span class="n">s</span><span class="o">);</span>
<span class="o">}</span>
<span class="k">for</span> <span class="o">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="o">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">10</span><span class="o">;</span> <span class="n">i</span><span class="o">++)</span> <span class="o">{</span>
<span class="k">if</span> <span class="o">(</span><span class="n">check</span><span class="o">(</span><span class="n">i</span><span class="o">))</span> <span class="o">{</span>
<span class="n">i</span><span class="o">++;</span> <span class="c1">// OK, when forReassign is "skip" or "allow"</span>
<span class="o">}</span>
<span class="n">i</span> <span class="o">=</span> <span class="mi">5</span><span class="o">;</span> <span class="c1">// OK, when forReassign is "allow"</span>
<span class="n">doSomethingWith</span><span class="o">(</span><span class="n">i</span><span class="o">);</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>This rule has the following properties:</strong></p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Default Value</th>
<th>Description</th>
<th>Multivalued</th>
</tr>
</thead>
<tbody>
<tr>
<td>foreachReassign</td>
<td>deny</td>
<td>how/if foreach control variables may be reassigned</td>
<td>no</td>
</tr>
<tr>
<td>forReassign</td>
<td>deny</td>
<td>how/if for control variables may be reassigned</td>
<td>no</td>
</tr>
</tbody>
</table>
<p><strong>Use this rule with the default properties by just referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/AvoidReassigningLoopVariables"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<p><strong>Use this rule and customize it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/AvoidReassigningLoopVariables"</span><span class="nt">&gt;</span>
<span class="nt">&lt;properties&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"foreachReassign"</span> <span class="na">value=</span><span class="s">"deny"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"forReassign"</span> <span class="na">value=</span><span class="s">"deny"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;/properties&gt;</span>
<span class="nt">&lt;/rule&gt;</span>
</code></pre></div></div>
<h2 id="avoidreassigningparameters">AvoidReassigningParameters</h2>
<p><strong>Since:</strong> PMD 1.0</p>
<p><strong>Priority:</strong> Medium High (2)</p>
<p>Reassigning values to incoming parameters of a method or constructor is not recommended, as this can
make the code more difficult to understand. The code is often read with the assumption that parameter values
dont change and an assignment violates therefore the principle of least astonishment. This is especially a
problem if the parameter is documented e.g. in the methods javadoc and the new content differs from the original
documented content.</p>
<p>Use temporary local variables instead. This allows you to assign a new name, which makes the code better
understandable.</p>
<p>Note that this rule considers both methods and constructors. If there are multiple assignments for a formal
parameter, then only the first assignment is reported.</p>
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/AvoidReassigningParametersRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.AvoidReassigningParametersRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Hello</span> <span class="o">{</span>
<span class="kd">private</span> <span class="kt">void</span> <span class="nf">greet</span><span class="o">(</span><span class="nc">String</span> <span class="n">name</span><span class="o">)</span> <span class="o">{</span>
<span class="n">name</span> <span class="o">=</span> <span class="n">name</span><span class="o">.</span><span class="na">trim</span><span class="o">();</span>
<span class="nc">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">"Hello "</span> <span class="o">+</span> <span class="n">name</span><span class="o">);</span>
<span class="c1">// preferred</span>
<span class="nc">String</span> <span class="n">trimmedName</span> <span class="o">=</span> <span class="n">name</span><span class="o">.</span><span class="na">trim</span><span class="o">();</span>
<span class="nc">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">"Hello "</span> <span class="o">+</span> <span class="n">trimmedName</span><span class="o">);</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>Use this rule by referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/AvoidReassigningParameters"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="avoidstringbufferfield">AvoidStringBufferField</h2>
<p><strong>Since:</strong> PMD 4.2</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>StringBuffers/StringBuilders can grow considerably, and so may become a source of memory leaks
if held within objects with long lifetimes.</p>
<p><strong>This rule is defined by the following XPath expression:</strong></p>
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">FieldDeclaration</span><span class="o">/</span><span class="nt">Type</span><span class="o">/</span><span class="nt">ReferenceType</span><span class="o">/</span><span class="nt">ClassOrInterfaceType</span><span class="p">[</span><span class="na">@Image</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'StringBuffer'</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="na">@Image</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'StringBuilder'</span><span class="p">]</span><span class="w">
</span></code></pre></div></div>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
<span class="kd">private</span> <span class="nc">StringBuffer</span> <span class="n">buffer</span><span class="o">;</span> <span class="c1">// potential memory leak as an instance variable;</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>Use this rule by referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/AvoidStringBufferField"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="avoidusinghardcodedip">AvoidUsingHardCodedIP</h2>
<p><strong>Since:</strong> PMD 4.1</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Application with hard-coded IP addresses can become impossible to deploy in some cases.
Externalizing IP adresses is preferable.</p>
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/AvoidUsingHardCodedIPRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.AvoidUsingHardCodedIPRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
<span class="kd">private</span> <span class="nc">String</span> <span class="n">ip</span> <span class="o">=</span> <span class="s">"127.0.0.1"</span><span class="o">;</span> <span class="c1">// not recommended</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>This rule has the following properties:</strong></p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Default Value</th>
<th>Description</th>
<th>Multivalued</th>
</tr>
</thead>
<tbody>
<tr>
<td>checkAddressTypes</td>
<td>IPv4 mapped IPv6 | IPv6 | IPv4</td>
<td>Check for IP address types.</td>
<td>yes. Delimiter is |.</td>
</tr>
</tbody>
</table>
<p><strong>Use this rule with the default properties by just referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/AvoidUsingHardCodedIP"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<p><strong>Use this rule and customize it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/AvoidUsingHardCodedIP"</span><span class="nt">&gt;</span>
<span class="nt">&lt;properties&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"checkAddressTypes"</span> <span class="na">value=</span><span class="s">"IPv4 mapped IPv6|IPv6|IPv4"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;/properties&gt;</span>
<span class="nt">&lt;/rule&gt;</span>
</code></pre></div></div>
<h2 id="checkresultset">CheckResultSet</h2>
<p><strong>Since:</strong> PMD 4.1</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Always check the return values of navigation methods (next, previous, first, last) of a ResultSet.
If the value return is false, it should be handled properly.</p>
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/CheckResultSetRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.CheckResultSetRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nc">Statement</span> <span class="n">stat</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="na">createStatement</span><span class="o">();</span>
<span class="nc">ResultSet</span> <span class="n">rst</span> <span class="o">=</span> <span class="n">stat</span><span class="o">.</span><span class="na">executeQuery</span><span class="o">(</span><span class="s">"SELECT name FROM person"</span><span class="o">);</span>
<span class="n">rst</span><span class="o">.</span><span class="na">next</span><span class="o">();</span> <span class="c1">// what if it returns false? bad form</span>
<span class="nc">String</span> <span class="n">firstName</span> <span class="o">=</span> <span class="n">rst</span><span class="o">.</span><span class="na">getString</span><span class="o">(</span><span class="mi">1</span><span class="o">);</span>
<span class="nc">Statement</span> <span class="n">stat</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="na">createStatement</span><span class="o">();</span>
<span class="nc">ResultSet</span> <span class="n">rst</span> <span class="o">=</span> <span class="n">stat</span><span class="o">.</span><span class="na">executeQuery</span><span class="o">(</span><span class="s">"SELECT name FROM person"</span><span class="o">);</span>
<span class="k">if</span> <span class="o">(</span><span class="n">rst</span><span class="o">.</span><span class="na">next</span><span class="o">())</span> <span class="o">{</span> <span class="c1">// result is properly examined and used</span>
<span class="nc">String</span> <span class="n">firstName</span> <span class="o">=</span> <span class="n">rst</span><span class="o">.</span><span class="na">getString</span><span class="o">(</span><span class="mi">1</span><span class="o">);</span>
<span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
<span class="c1">// handle missing data</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>Use this rule by referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/CheckResultSet"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="constantsininterface">ConstantsInInterface</h2>
<p><strong>Since:</strong> PMD 5.5</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Avoid constants in interfaces. Interfaces should define types, constants are implementation details
better placed in classes or enums. See Effective Java, item 19.</p>
<p><strong>This rule is defined by the following XPath expression:</strong></p>
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">ClassOrInterfaceDeclaration</span><span class="p">[</span><span class="na">@Interface</span><span class="err">=</span><span class="w"> </span><span class="nf">true</span><span class="p">()][</span><span class="nv">$ignoreIfHasMethods</span><span class="err">=</span><span class="w"> </span><span class="nf">false</span><span class="p">()</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="ow">not</span><span class="p">(.</span><span class="o">//</span><span class="nt">MethodDeclaration</span><span class="p">)]</span><span class="o">//</span><span class="nt">FieldDeclaration</span><span class="w">
</span></code></pre></div></div>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">interface</span> <span class="nc">ConstantInterface</span> <span class="o">{</span>
<span class="kd">public</span> <span class="kd">static</span> <span class="kd">final</span> <span class="kt">int</span> <span class="no">CONST1</span> <span class="o">=</span> <span class="mi">1</span><span class="o">;</span> <span class="c1">// violation, no fields allowed in interface!</span>
<span class="kd">static</span> <span class="kd">final</span> <span class="kt">int</span> <span class="no">CONST2</span> <span class="o">=</span> <span class="mi">1</span><span class="o">;</span> <span class="c1">// violation, no fields allowed in interface!</span>
<span class="kd">final</span> <span class="kt">int</span> <span class="no">CONST3</span> <span class="o">=</span> <span class="mi">1</span><span class="o">;</span> <span class="c1">// violation, no fields allowed in interface!</span>
<span class="kt">int</span> <span class="no">CONST4</span> <span class="o">=</span> <span class="mi">1</span><span class="o">;</span> <span class="c1">// violation, no fields allowed in interface!</span>
<span class="o">}</span>
<span class="c1">// with ignoreIfHasMethods = false</span>
<span class="kd">public</span> <span class="kd">interface</span> <span class="nc">AnotherConstantInterface</span> <span class="o">{</span>
<span class="kd">public</span> <span class="kd">static</span> <span class="kd">final</span> <span class="kt">int</span> <span class="no">CONST1</span> <span class="o">=</span> <span class="mi">1</span><span class="o">;</span> <span class="c1">// violation, no fields allowed in interface!</span>
<span class="kt">int</span> <span class="nf">anyMethod</span><span class="o">();</span>
<span class="o">}</span>
<span class="c1">// with ignoreIfHasMethods = true</span>
<span class="kd">public</span> <span class="kd">interface</span> <span class="nc">YetAnotherConstantInterface</span> <span class="o">{</span>
<span class="kd">public</span> <span class="kd">static</span> <span class="kd">final</span> <span class="kt">int</span> <span class="no">CONST1</span> <span class="o">=</span> <span class="mi">1</span><span class="o">;</span> <span class="c1">// no violation</span>
<span class="kt">int</span> <span class="nf">anyMethod</span><span class="o">();</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>This rule has the following properties:</strong></p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Default Value</th>
<th>Description</th>
<th>Multivalued</th>
</tr>
</thead>
<tbody>
<tr>
<td>ignoreIfHasMethods</td>
<td>true</td>
<td>Whether to ignore constants in interfaces if the interface defines any methods</td>
<td>no</td>
</tr>
</tbody>
</table>
<p><strong>Use this rule with the default properties by just referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/ConstantsInInterface"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<p><strong>Use this rule and customize it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/ConstantsInInterface"</span><span class="nt">&gt;</span>
<span class="nt">&lt;properties&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"ignoreIfHasMethods"</span> <span class="na">value=</span><span class="s">"true"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;/properties&gt;</span>
<span class="nt">&lt;/rule&gt;</span>
</code></pre></div></div>
<h2 id="defaultlabelnotlastinswitchstmt">DefaultLabelNotLastInSwitchStmt</h2>
<p><strong>Since:</strong> PMD 1.5</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>By convention, the default label should be the last label in a switch statement.</p>
<p><strong>This rule is defined by the following XPath expression:</strong></p>
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">SwitchStatement</span><span class="w">
</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="nt">SwitchLabel</span><span class="p">[</span><span class="nf">position</span><span class="p">()</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="nf">last</span><span class="p">()][</span><span class="na">@Default</span><span class="err">=</span><span class="w"> </span><span class="nf">true</span><span class="p">()])]</span><span class="w">
</span><span class="p">[</span><span class="nt">SwitchLabel</span><span class="p">[</span><span class="na">@Default</span><span class="err">=</span><span class="w"> </span><span class="nf">true</span><span class="p">()]]</span><span class="w">
</span></code></pre></div></div>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
<span class="kt">void</span> <span class="nf">bar</span><span class="o">(</span><span class="kt">int</span> <span class="n">a</span><span class="o">)</span> <span class="o">{</span>
<span class="k">switch</span> <span class="o">(</span><span class="n">a</span><span class="o">)</span> <span class="o">{</span>
<span class="k">case</span> <span class="mi">1</span><span class="o">:</span> <span class="c1">// do something</span>
<span class="k">break</span><span class="o">;</span>
<span class="k">default</span><span class="o">:</span> <span class="c1">// the default case should be last, by convention</span>
<span class="k">break</span><span class="o">;</span>
<span class="k">case</span> <span class="mi">2</span><span class="o">:</span>
<span class="k">break</span><span class="o">;</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>Use this rule by referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/DefaultLabelNotLastInSwitchStmt"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="doublebraceinitialization">DoubleBraceInitialization</h2>
<p><strong>Since:</strong> PMD 6.16.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Double brace initialisation is a pattern to initialise eg collections concisely. But it implicitly
generates a new .class file, and the object holds a strong reference to the enclosing object. For those
reasons, it is preferable to initialize the object normally, even though its verbose.</p>
<p>This rule counts any anonymous class which only has a single initializer as an instance of double-brace
initialization. There is currently no way to find out whether a method called in the initializer is not
accessible from outside the anonymous class, and those legit cases should be suppressed for the time being.</p>
<p><strong>This rule is defined by the following XPath expression:</strong></p>
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">AllocationExpression</span><span class="o">/</span><span class="nt">ClassOrInterfaceBody</span><span class="p">[</span><span class="nf">count</span><span class="p">(</span><span class="err">*</span><span class="p">)</span><span class="err">=</span><span class="m">1</span><span class="p">]</span><span class="err">/*</span><span class="o">/</span><span class="nt">Initializer</span><span class="p">[</span><span class="na">@Static</span><span class="err">=</span><span class="nf">false</span><span class="p">()]</span><span class="w">
</span></code></pre></div></div>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">// this is double-brace initialization</span>
<span class="k">return</span> <span class="k">new</span> <span class="nc">ArrayList</span><span class="o">&lt;</span><span class="nc">String</span><span class="o">&gt;(){{</span>
<span class="n">add</span><span class="o">(</span><span class="s">"a"</span><span class="o">);</span>
<span class="n">add</span><span class="o">(</span><span class="s">"b"</span><span class="o">);</span>
<span class="n">add</span><span class="o">(</span><span class="s">"c"</span><span class="o">);</span>
<span class="o">}};</span>
<span class="c1">// the better way is to not create an anonymous class:</span>
<span class="nc">List</span><span class="o">&lt;</span><span class="nc">String</span><span class="o">&gt;</span> <span class="n">a</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">ArrayList</span><span class="o">&lt;&gt;();</span>
<span class="n">a</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="s">"a"</span><span class="o">);</span>
<span class="n">a</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="s">"b"</span><span class="o">);</span>
<span class="n">a</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="s">"c"</span><span class="o">);</span>
<span class="k">return</span> <span class="n">a</span><span class="o">;</span>
</code></pre></div></div>
<p><strong>Use this rule by referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/DoubleBraceInitialization"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="forloopcanbeforeach">ForLoopCanBeForeach</h2>
<p><strong>Since:</strong> PMD 6.0.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p><strong>Minimum Language Version:</strong> Java 1.5</p>
<p>Reports loops that can be safely replaced with the foreach syntax. The rule considers loops over
lists, arrays and iterators. A loop is safe to replace if it only uses the index variable to
access an element of the list or array, only has one update statement, and loops through <em>every</em>
element of the list or array left to right.</p>
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/ForLoopCanBeForeachRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.ForLoopCanBeForeachRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">MyClass</span> <span class="o">{</span>
<span class="kt">void</span> <span class="nf">loop</span><span class="o">(</span><span class="nc">List</span><span class="o">&lt;</span><span class="nc">String</span><span class="o">&gt;</span> <span class="n">l</span><span class="o">)</span> <span class="o">{</span>
<span class="k">for</span> <span class="o">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">l</span><span class="o">.</span><span class="na">size</span><span class="o">();</span> <span class="n">i</span><span class="o">++)</span> <span class="o">{</span> <span class="c1">// pre Java 1.5</span>
<span class="nc">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">l</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">i</span><span class="o">));</span>
<span class="o">}</span>
<span class="k">for</span> <span class="o">(</span><span class="nc">String</span> <span class="n">s</span> <span class="o">:</span> <span class="n">l</span><span class="o">)</span> <span class="o">{</span> <span class="c1">// post Java 1.5</span>
<span class="nc">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">s</span><span class="o">);</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>Use this rule by referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/ForLoopCanBeForeach"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="forloopvariablecount">ForLoopVariableCount</h2>
<p><strong>Since:</strong> PMD 6.11.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Having a lot of control variables in a for loop makes it harder to see what range of values
the loop iterates over. By default this rule allows a regular for loop with only one variable.</p>
<p><strong>This rule is defined by the following XPath expression:</strong></p>
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">ForInit</span><span class="o">/</span><span class="nt">LocalVariableDeclaration</span><span class="p">[</span><span class="nf">count</span><span class="p">(</span><span class="nt">VariableDeclarator</span><span class="p">)</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="nv">$maximumVariables</span><span class="p">]</span><span class="w">
</span></code></pre></div></div>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">// this will be reported with the default setting of at most one control variable in a for loop</span>
<span class="k">for</span> <span class="o">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="o">,</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">10</span><span class="o">;</span> <span class="n">i</span><span class="o">++,</span> <span class="n">j</span> <span class="o">+=</span> <span class="mi">2</span><span class="o">)</span> <span class="o">{</span>
<span class="n">foo</span><span class="o">();</span>
</code></pre></div></div>
<p><strong>This rule has the following properties:</strong></p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Default Value</th>
<th>Description</th>
<th>Multivalued</th>
</tr>
</thead>
<tbody>
<tr>
<td>maximumVariables</td>
<td>1</td>
<td>A regular for statement will have 1 control variable</td>
<td>no</td>
</tr>
</tbody>
</table>
<p><strong>Use this rule with the default properties by just referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/ForLoopVariableCount"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<p><strong>Use this rule and customize it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/ForLoopVariableCount"</span><span class="nt">&gt;</span>
<span class="nt">&lt;properties&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"maximumVariables"</span> <span class="na">value=</span><span class="s">"1"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;/properties&gt;</span>
<span class="nt">&lt;/rule&gt;</span>
</code></pre></div></div>
<h2 id="guardlogstatement">GuardLogStatement</h2>
<p><strong>Since:</strong> PMD 5.1.0</p>
<p><strong>Priority:</strong> Medium High (2)</p>
<p>Whenever using a log level, one should check if the loglevel is actually enabled, or
otherwise skip the associate String creation and manipulation.</p>
<p>An alternative to checking the log level are substituting parameters, formatters or lazy logging
with lambdas. The available alternatives depend on the actual logging framework.</p>
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/GuardLogStatementRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.GuardLogStatementRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">// Add this for performance</span>
<span class="k">if</span> <span class="o">(</span><span class="n">log</span><span class="o">.</span><span class="na">isDebugEnabled</span><span class="o">())</span> <span class="o">{</span>
<span class="n">log</span><span class="o">.</span><span class="na">debug</span><span class="o">(</span><span class="s">"log something"</span> <span class="o">+</span> <span class="n">param1</span> <span class="o">+</span> <span class="s">" and "</span> <span class="o">+</span> <span class="n">param2</span> <span class="o">+</span> <span class="s">"concat strings"</span><span class="o">);</span>
<span class="o">}</span>
<span class="c1">// Avoid the guarding if statement with substituting parameters</span>
<span class="n">log</span><span class="o">.</span><span class="na">debug</span><span class="o">(</span><span class="s">"log something {} and {}"</span><span class="o">,</span> <span class="n">param1</span><span class="o">,</span> <span class="n">param2</span><span class="o">);</span>
<span class="c1">// Avoid the guarding if statement with formatters</span>
<span class="n">log</span><span class="o">.</span><span class="na">debug</span><span class="o">(</span><span class="s">"log something %s and %s"</span><span class="o">,</span> <span class="n">param1</span><span class="o">,</span> <span class="n">param2</span><span class="o">);</span>
<span class="c1">// Avoid the guarding if statement with lazy logging and lambdas</span>
<span class="n">log</span><span class="o">.</span><span class="na">debug</span><span class="o">(</span><span class="s">"log something expensive: {}"</span><span class="o">,</span> <span class="o">()</span> <span class="o">-&gt;</span> <span class="n">calculateExpensiveLoggingText</span><span class="o">());</span>
</code></pre></div></div>
<p><strong>This rule has the following properties:</strong></p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Default Value</th>
<th>Description</th>
<th>Multivalued</th>
</tr>
</thead>
<tbody>
<tr>
<td>logLevels</td>
<td>trace , debug , info , warn , error , log , finest , finer , fine , info , warning , severe</td>
<td>LogLevels to guard</td>
<td>yes. Delimiter is ,.</td>
</tr>
<tr>
<td>guardsMethods</td>
<td>isTraceEnabled , isDebugEnabled , isInfoEnabled , isWarnEnabled , isErrorEnabled , isLoggable</td>
<td>Method use to guard the log statement</td>
<td>yes. Delimiter is ,.</td>
</tr>
</tbody>
</table>
<p><strong>Use this rule with the default properties by just referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/GuardLogStatement"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<p><strong>Use this rule and customize it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/GuardLogStatement"</span><span class="nt">&gt;</span>
<span class="nt">&lt;properties&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"logLevels"</span> <span class="na">value=</span><span class="s">"trace,debug,info,warn,error,log,finest,finer,fine,info,warning,severe"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"guardsMethods"</span> <span class="na">value=</span><span class="s">"isTraceEnabled,isDebugEnabled,isInfoEnabled,isWarnEnabled,isErrorEnabled,isLoggable"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;/properties&gt;</span>
<span class="nt">&lt;/rule&gt;</span>
</code></pre></div></div>
<h2 id="junit4suitesshouldusesuiteannotation">JUnit4SuitesShouldUseSuiteAnnotation</h2>
<p><strong>Since:</strong> PMD 4.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>In JUnit 3, test suites are indicated by the suite() method. In JUnit 4, suites are indicated
through the @RunWith(Suite.class) annotation.</p>
<p><strong>This rule is defined by the following XPath expression:</strong></p>
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">ClassOrInterfaceBodyDeclaration</span><span class="w">
</span><span class="p">[</span><span class="nt">MethodDeclaration</span><span class="p">[</span><span class="na">@Name</span><span class="err">=</span><span class="s1">'suite'</span><span class="p">]</span><span class="o">/</span><span class="nt">ResultType</span><span class="o">/</span><span class="nt">Type</span><span class="o">/</span><span class="nt">ReferenceType</span><span class="o">/</span><span class="nt">ClassOrInterfaceType</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'junit.framework.Test'</span><span class="p">)]]</span><span class="w">
</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="nt">MethodDeclaration</span><span class="o">/</span><span class="nt">Block</span><span class="o">/</span><span class="nt">BlockStatement</span><span class="o">/</span><span class="nt">Statement</span><span class="o">/</span><span class="nt">ReturnStatement</span><span class="o">//</span><span class="nt">ClassOrInterfaceType</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'junit.framework.JUnit4TestAdapter'</span><span class="p">)])]</span><span class="w">
</span></code></pre></div></div>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">BadExample</span> <span class="kd">extends</span> <span class="nc">TestCase</span><span class="o">{</span>
<span class="kd">public</span> <span class="kd">static</span> <span class="nc">Test</span> <span class="nf">suite</span><span class="o">(){</span>
<span class="k">return</span> <span class="k">new</span> <span class="nf">Suite</span><span class="o">();</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="nd">@RunWith</span><span class="o">(</span><span class="nc">Suite</span><span class="o">.</span><span class="na">class</span><span class="o">)</span>
<span class="nd">@SuiteClasses</span><span class="o">(</span> <span class="o">{</span> <span class="nc">TestOne</span><span class="o">.</span><span class="na">class</span><span class="o">,</span> <span class="nc">TestTwo</span><span class="o">.</span><span class="na">class</span> <span class="o">})</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">GoodTest</span> <span class="o">{</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>Use this rule by referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/JUnit4SuitesShouldUseSuiteAnnotation"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="junit4testshoulduseafterannotation">JUnit4TestShouldUseAfterAnnotation</h2>
<p><strong>Since:</strong> PMD 4.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>In JUnit 3, the tearDown method was used to clean up all data entities required in running tests.
JUnit 4 skips the tearDown method and executes all methods annotated with @After after running each test.
JUnit 5 introduced @AfterEach and @AfterAll annotations to execute methods after each test or after all tests in the class, respectively.</p>
<p><strong>This rule is defined by the following XPath expression:</strong></p>
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">ClassOrInterfaceBodyDeclaration</span><span class="w">
</span><span class="p">[</span><span class="nt">MethodDeclaration</span><span class="p">[</span><span class="na">@Name</span><span class="err">=</span><span class="s1">'tearDown'</span><span class="p">]]</span><span class="w">
</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="nt">Annotation</span><span class="err">/*</span><span class="o">/</span><span class="nt">Name</span><span class="p">[</span><span class="w">
</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.After'</span><span class="p">)</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.AfterEach'</span><span class="p">)</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.AfterAll'</span><span class="p">)</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.testng.annotations.AfterMethod'</span><span class="p">)])]</span><span class="w">
</span></code></pre></div></div>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">MyTest</span> <span class="o">{</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">tearDown</span><span class="o">()</span> <span class="o">{</span>
<span class="n">bad</span><span class="o">();</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">MyTest2</span> <span class="o">{</span>
<span class="nd">@After</span> <span class="kd">public</span> <span class="kt">void</span> <span class="nf">tearDown</span><span class="o">()</span> <span class="o">{</span>
<span class="n">good</span><span class="o">();</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>Use this rule by referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/JUnit4TestShouldUseAfterAnnotation"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="junit4testshouldusebeforeannotation">JUnit4TestShouldUseBeforeAnnotation</h2>
<p><strong>Since:</strong> PMD 4.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>In JUnit 3, the setUp method was used to set up all data entities required in running tests.
JUnit 4 skips the setUp method and executes all methods annotated with @Before before all tests.
JUnit 5 introduced @BeforeEach and @BeforeAll annotations to execute methods before each test or before all tests in the class, respectively.</p>
<p><strong>This rule is defined by the following XPath expression:</strong></p>
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">ClassOrInterfaceBodyDeclaration</span><span class="w">
</span><span class="p">[</span><span class="nt">MethodDeclaration</span><span class="p">[</span><span class="na">@Name</span><span class="err">=</span><span class="s1">'setUp'</span><span class="p">]]</span><span class="w">
</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="nt">Annotation</span><span class="err">/*</span><span class="o">/</span><span class="nt">Name</span><span class="p">[</span><span class="w">
</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.Before'</span><span class="p">)</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.BeforeEach'</span><span class="p">)</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.BeforeAll'</span><span class="p">)</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.testng.annotations.BeforeMethod'</span><span class="p">)])]</span><span class="w">
</span></code></pre></div></div>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">MyTest</span> <span class="o">{</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">setUp</span><span class="o">()</span> <span class="o">{</span>
<span class="n">bad</span><span class="o">();</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">MyTest2</span> <span class="o">{</span>
<span class="nd">@Before</span> <span class="kd">public</span> <span class="kt">void</span> <span class="nf">setUp</span><span class="o">()</span> <span class="o">{</span>
<span class="n">good</span><span class="o">();</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>Use this rule by referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/JUnit4TestShouldUseBeforeAnnotation"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="junit4testshouldusetestannotation">JUnit4TestShouldUseTestAnnotation</h2>
<p><strong>Since:</strong> PMD 4.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>In JUnit 3, the framework executed all methods which started with the word test as a unit test.
In JUnit 4, only methods annotated with the @Test annotation are executed.
In JUnit 5, one of the following annotations should be used for tests: @Test, @RepeatedTest, @TestFactory, @TestTemplate or @ParameterizedTest.</p>
<p><strong>This rule is defined by the following XPath expression:</strong></p>
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">ClassOrInterfaceDeclaration</span><span class="p">[</span><span class="w">
</span><span class="nf">matches</span><span class="p">(</span><span class="na">@SimpleName</span><span class="p">,</span><span class="w"> </span><span class="nv">$testClassPattern</span><span class="p">)</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="nt">ExtendsList</span><span class="o">/</span><span class="nt">ClassOrInterfaceType</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'junit.framework.TestCase'</span><span class="p">)]]</span><span class="w">
</span><span class="o">/</span><span class="nt">ClassOrInterfaceBody</span><span class="o">/</span><span class="nt">ClassOrInterfaceBodyDeclaration</span><span class="p">[</span><span class="nt">MethodDeclaration</span><span class="p">[</span><span class="na">@Public</span><span class="err">=</span><span class="nf">true</span><span class="p">()</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="nf">starts-with</span><span class="p">(</span><span class="na">@Name</span><span class="p">,</span><span class="w"> </span><span class="s1">'test'</span><span class="p">)]]</span><span class="w">
</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="nt">Annotation</span><span class="o">//</span><span class="nt">Name</span><span class="p">[</span><span class="w">
</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.Test'</span><span class="p">)</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.Test'</span><span class="p">)</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.RepeatedTest'</span><span class="p">)</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.TestFactory'</span><span class="p">)</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.TestTemplate'</span><span class="p">)</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.params.ParameterizedTest'</span><span class="p">)</span><span class="w">
</span><span class="p">])]</span><span class="w">
</span></code></pre></div></div>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">MyTest</span> <span class="o">{</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">testBad</span><span class="o">()</span> <span class="o">{</span>
<span class="n">doSomething</span><span class="o">();</span>
<span class="o">}</span>
<span class="nd">@Test</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">testGood</span><span class="o">()</span> <span class="o">{</span>
<span class="n">doSomething</span><span class="o">();</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>This rule has the following properties:</strong></p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Default Value</th>
<th>Description</th>
<th>Multivalued</th>
</tr>
</thead>
<tbody>
<tr>
<td>testClassPattern</td>
<td>Test</td>
<td>The regex pattern used to identify test classes</td>
<td>no</td>
</tr>
</tbody>
</table>
<p><strong>Use this rule with the default properties by just referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/JUnit4TestShouldUseTestAnnotation"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<p><strong>Use this rule and customize it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/JUnit4TestShouldUseTestAnnotation"</span><span class="nt">&gt;</span>
<span class="nt">&lt;properties&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"testClassPattern"</span> <span class="na">value=</span><span class="s">"Test"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;/properties&gt;</span>
<span class="nt">&lt;/rule&gt;</span>
</code></pre></div></div>
<h2 id="junit5testshouldbepackageprivate">JUnit5TestShouldBePackagePrivate</h2>
<p><strong>Since:</strong> PMD 6.35.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Reports JUnit 5 test classes and methods that are not package-private.
Contrary to JUnit 4 tests, which required public visibility to be run by the engine,
JUnit 5 tests can also be run if theyre package-private. Marking them as such
is a good practice to limit their visibility.</p>
<p>Test methods are identified as those which use <code class="language-plaintext highlighter-rouge">@Test</code>, <code class="language-plaintext highlighter-rouge">@RepeatedTest</code>,
<code class="language-plaintext highlighter-rouge">@TestFactory</code>, <code class="language-plaintext highlighter-rouge">@TestTemplate</code> or <code class="language-plaintext highlighter-rouge">@ParameterizedTest</code>.</p>
<p><strong>This rule is defined by the following XPath expression:</strong></p>
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">ClassOrInterfaceDeclaration</span><span class="p">[</span><span class="w">
</span><span class="c">(: a Junit 5 test class, ie, it has methods with the annotation :)</span><span class="w">
</span><span class="na">@Interface</span><span class="err">=</span><span class="nf">false</span><span class="p">()</span><span class="w"> </span><span class="ow">and</span><span class="w">
</span><span class="nt">ClassOrInterfaceBody</span><span class="o">/</span><span class="nt">ClassOrInterfaceBodyDeclaration</span><span class="w">
</span><span class="p">[</span><span class="nt">Annotation</span><span class="o">//</span><span class="nt">Name</span><span class="p">[</span><span class="w">
</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.Test'</span><span class="p">)</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.RepeatedTest'</span><span class="p">)</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.TestFactory'</span><span class="p">)</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.TestTemplate'</span><span class="p">)</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.params.ParameterizedTest'</span><span class="p">)</span><span class="w">
</span><span class="p">]]</span><span class="w">
</span><span class="p">[</span><span class="nt">MethodDeclaration</span><span class="p">]</span><span class="w">
</span><span class="p">]</span><span class="err">/</span><span class="p">(</span><span class="w">
</span><span class="kn">self::</span><span class="o">*</span><span class="p">[</span><span class="na">@Abstract</span><span class="err">=</span><span class="nf">false</span><span class="p">()</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="p">(</span><span class="na">@Public</span><span class="err">=</span><span class="nf">true</span><span class="p">()</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="na">@Protected</span><span class="err">=</span><span class="nf">true</span><span class="p">())]</span><span class="w">
</span><span class="p">|</span><span class="w"> </span><span class="nt">ClassOrInterfaceBody</span><span class="o">/</span><span class="nt">ClassOrInterfaceBodyDeclaration</span><span class="w">
</span><span class="p">[</span><span class="nt">Annotation</span><span class="o">//</span><span class="nt">Name</span><span class="p">[</span><span class="w">
</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.Test'</span><span class="p">)</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.RepeatedTest'</span><span class="p">)</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.TestFactory'</span><span class="p">)</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.TestTemplate'</span><span class="p">)</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.params.ParameterizedTest'</span><span class="p">)</span><span class="w">
</span><span class="p">]]</span><span class="w">
</span><span class="o">/</span><span class="nt">MethodDeclaration</span><span class="p">[</span><span class="na">@Public</span><span class="err">=</span><span class="nf">true</span><span class="p">()</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="na">@Protected</span><span class="err">=</span><span class="nf">true</span><span class="p">()]</span><span class="w">
</span><span class="p">)</span><span class="w">
</span></code></pre></div></div>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">class</span> <span class="nc">MyTest</span> <span class="o">{</span> <span class="c1">// not public, that's fine</span>
<span class="nd">@Test</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">testBad</span><span class="o">()</span> <span class="o">{</span> <span class="o">}</span> <span class="c1">// should not have a public modifier</span>
<span class="nd">@Test</span>
<span class="kd">protected</span> <span class="kt">void</span> <span class="nf">testAlsoBad</span><span class="o">()</span> <span class="o">{</span> <span class="o">}</span> <span class="c1">// should not have a protected modifier</span>
<span class="nd">@Test</span>
<span class="kd">private</span> <span class="kt">void</span> <span class="nf">testNoRun</span><span class="o">()</span> <span class="o">{</span> <span class="o">}</span> <span class="c1">// should not have a private modifier</span>
<span class="nd">@Test</span>
<span class="kt">void</span> <span class="nf">testGood</span><span class="o">()</span> <span class="o">{</span> <span class="o">}</span> <span class="c1">// package private as expected</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>Use this rule by referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/JUnit5TestShouldBePackagePrivate"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="junitassertionsshouldincludemessage">JUnitAssertionsShouldIncludeMessage</h2>
<p><strong>Since:</strong> PMD 1.04</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>JUnit assertions should include an informative message - i.e., use the three-argument version of
assertEquals(), not the two-argument version.</p>
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/JUnitAssertionsShouldIncludeMessageRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.JUnitAssertionsShouldIncludeMessageRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="kd">extends</span> <span class="nc">TestCase</span> <span class="o">{</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">testSomething</span><span class="o">()</span> <span class="o">{</span>
<span class="n">assertEquals</span><span class="o">(</span><span class="s">"foo"</span><span class="o">,</span> <span class="s">"bar"</span><span class="o">);</span>
<span class="c1">// Use the form:</span>
<span class="c1">// assertEquals("Foo does not equals bar", "foo", "bar");</span>
<span class="c1">// instead</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>Use this rule by referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/JUnitAssertionsShouldIncludeMessage"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="junittestcontainstoomanyasserts">JUnitTestContainsTooManyAsserts</h2>
<p><strong>Since:</strong> PMD 5.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Unit tests should not contain too many asserts. Many asserts are indicative of a complex test, for which
it is harder to verify correctness. Consider breaking the test scenario into multiple, shorter test scenarios.
Customize the maximum number of assertions used by this Rule to suit your needs.</p>
<p>This rule checks for JUnit4, JUnit5 and TestNG Tests, as well as methods starting with "test".</p>
<p><strong>This rule is defined by the following XPath expression:</strong></p>
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">MethodDeclaration</span><span class="p">[</span><span class="na">@Name</span><span class="p">[</span><span class="nf">matches</span><span class="p">(.,</span><span class="s1">'^test'</span><span class="p">)]</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="p">..</span><span class="o">/</span><span class="nt">Annotation</span><span class="o">/</span><span class="nt">MarkerAnnotation</span><span class="o">/</span><span class="nt">Name</span><span class="p">[</span><span class="w">
</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.Test'</span><span class="p">)</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.Test'</span><span class="p">)</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.RepeatedTest'</span><span class="p">)</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.TestFactory'</span><span class="p">)</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.TestTemplate'</span><span class="p">)</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.params.ParameterizedTest'</span><span class="p">)</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.testng.annotations.Test'</span><span class="p">)</span><span class="w">
</span><span class="p">]]</span><span class="w">
</span><span class="p">[</span><span class="nf">count</span><span class="p">(.</span><span class="o">//</span><span class="nt">PrimaryPrefix</span><span class="o">/</span><span class="nt">Name</span><span class="p">[</span><span class="na">@Image</span><span class="p">[</span><span class="nf">matches</span><span class="p">(.,</span><span class="s1">'^assert'</span><span class="p">)]])</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="nv">$maximumAsserts</span><span class="p">]</span><span class="w">
</span></code></pre></div></div>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">MyTestCase</span> <span class="kd">extends</span> <span class="nc">TestCase</span> <span class="o">{</span>
<span class="c1">// Ok</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">testMyCaseWithOneAssert</span><span class="o">()</span> <span class="o">{</span>
<span class="kt">boolean</span> <span class="n">myVar</span> <span class="o">=</span> <span class="kc">false</span><span class="o">;</span>
<span class="n">assertFalse</span><span class="o">(</span><span class="s">"should be false"</span><span class="o">,</span> <span class="n">myVar</span><span class="o">);</span>
<span class="o">}</span>
<span class="c1">// Bad, too many asserts (assuming max=1)</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">testMyCaseWithMoreAsserts</span><span class="o">()</span> <span class="o">{</span>
<span class="kt">boolean</span> <span class="n">myVar</span> <span class="o">=</span> <span class="kc">false</span><span class="o">;</span>
<span class="n">assertFalse</span><span class="o">(</span><span class="s">"myVar should be false"</span><span class="o">,</span> <span class="n">myVar</span><span class="o">);</span>
<span class="n">assertEquals</span><span class="o">(</span><span class="s">"should equals false"</span><span class="o">,</span> <span class="kc">false</span><span class="o">,</span> <span class="n">myVar</span><span class="o">);</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>This rule has the following properties:</strong></p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Default Value</th>
<th>Description</th>
<th>Multivalued</th>
</tr>
</thead>
<tbody>
<tr>
<td>maximumAsserts</td>
<td>1</td>
<td>Maximum number of Asserts in a test method</td>
<td>no</td>
</tr>
</tbody>
</table>
<p><strong>Use this rule with the default properties by just referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/JUnitTestContainsTooManyAsserts"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<p><strong>Use this rule and customize it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/JUnitTestContainsTooManyAsserts"</span><span class="nt">&gt;</span>
<span class="nt">&lt;properties&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"maximumAsserts"</span> <span class="na">value=</span><span class="s">"1"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;/properties&gt;</span>
<span class="nt">&lt;/rule&gt;</span>
</code></pre></div></div>
<h2 id="junittestsshouldincludeassert">JUnitTestsShouldIncludeAssert</h2>
<p><strong>Since:</strong> PMD 2.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>JUnit tests should include at least one assertion. This makes the tests more robust, and using assert
with messages provide the developer a clearer idea of what the test does.</p>
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/JUnitTestsShouldIncludeAssertRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.JUnitTestsShouldIncludeAssertRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="kd">extends</span> <span class="nc">TestCase</span> <span class="o">{</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">testSomething</span><span class="o">()</span> <span class="o">{</span>
<span class="nc">Bar</span> <span class="n">b</span> <span class="o">=</span> <span class="n">findBar</span><span class="o">();</span>
<span class="c1">// This is better than having a NullPointerException</span>
<span class="c1">// assertNotNull("bar not found", b);</span>
<span class="n">b</span><span class="o">.</span><span class="na">work</span><span class="o">();</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>Use this rule by referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/JUnitTestsShouldIncludeAssert"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="junituseexpected">JUnitUseExpected</h2>
<p><strong>Since:</strong> PMD 4.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>In JUnit4, use the @Test(expected) annotation to denote tests that should throw exceptions.</p>
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/JUnitUseExpectedRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.JUnitUseExpectedRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">MyTest</span> <span class="o">{</span>
<span class="nd">@Test</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">testBad</span><span class="o">()</span> <span class="o">{</span>
<span class="k">try</span> <span class="o">{</span>
<span class="n">doSomething</span><span class="o">();</span>
<span class="n">fail</span><span class="o">(</span><span class="s">"should have thrown an exception"</span><span class="o">);</span>
<span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="nc">Exception</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="nd">@Test</span><span class="o">(</span><span class="n">expected</span><span class="o">=</span><span class="nc">Exception</span><span class="o">.</span><span class="na">class</span><span class="o">)</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">testGood</span><span class="o">()</span> <span class="o">{</span>
<span class="n">doSomething</span><span class="o">();</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>Use this rule by referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/JUnitUseExpected"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="literalsfirstincomparisons">LiteralsFirstInComparisons</h2>
<p><strong>Since:</strong> PMD 6.24.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Position literals first in all String comparisons, if the second argument is null then NullPointerExceptions
can be avoided, they will just return false. Note that switching literal positions for compareTo and
compareToIgnoreCase may change the result, see examples.</p>
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/LiteralsFirstInComparisonsRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.LiteralsFirstInComparisonsRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
<span class="kt">boolean</span> <span class="nf">bar</span><span class="o">(</span><span class="nc">String</span> <span class="n">x</span><span class="o">)</span> <span class="o">{</span>
<span class="k">return</span> <span class="n">x</span><span class="o">.</span><span class="na">equals</span><span class="o">(</span><span class="s">"2"</span><span class="o">);</span> <span class="c1">// should be "2".equals(x)</span>
<span class="o">}</span>
<span class="kt">boolean</span> <span class="nf">bar</span><span class="o">(</span><span class="nc">String</span> <span class="n">x</span><span class="o">)</span> <span class="o">{</span>
<span class="k">return</span> <span class="n">x</span><span class="o">.</span><span class="na">equalsIgnoreCase</span><span class="o">(</span><span class="s">"2"</span><span class="o">);</span> <span class="c1">// should be "2".equalsIgnoreCase(x)</span>
<span class="o">}</span>
<span class="kt">boolean</span> <span class="nf">bar</span><span class="o">(</span><span class="nc">String</span> <span class="n">x</span><span class="o">)</span> <span class="o">{</span>
<span class="k">return</span> <span class="o">(</span><span class="n">x</span><span class="o">.</span><span class="na">compareTo</span><span class="o">(</span><span class="s">"bar"</span><span class="o">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="o">);</span> <span class="c1">// should be: "bar".compareTo(x) &lt; 0</span>
<span class="o">}</span>
<span class="kt">boolean</span> <span class="nf">bar</span><span class="o">(</span><span class="nc">String</span> <span class="n">x</span><span class="o">)</span> <span class="o">{</span>
<span class="k">return</span> <span class="o">(</span><span class="n">x</span><span class="o">.</span><span class="na">compareToIgnoreCase</span><span class="o">(</span><span class="s">"bar"</span><span class="o">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="o">);</span> <span class="c1">// should be: "bar".compareToIgnoreCase(x) &lt; 0</span>
<span class="o">}</span>
<span class="kt">boolean</span> <span class="nf">bar</span><span class="o">(</span><span class="nc">String</span> <span class="n">x</span><span class="o">)</span> <span class="o">{</span>
<span class="k">return</span> <span class="n">x</span><span class="o">.</span><span class="na">contentEquals</span><span class="o">(</span><span class="s">"bar"</span><span class="o">);</span> <span class="c1">// should be "bar".contentEquals(x)</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>Use this rule by referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/LiteralsFirstInComparisons"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="loosecoupling">LooseCoupling</h2>
<p><strong>Since:</strong> PMD 0.7</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>The use of implementation types (i.e., HashSet) as object references limits your ability to use alternate
implementations in the future as requirements change. Whenever available, referencing objects
by their interface types (i.e, Set) provides much more flexibility.</p>
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/LooseCouplingRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.LooseCouplingRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">import</span> <span class="nn">java.util.ArrayList</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">java.util.HashSet</span><span class="o">;</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">Bar</span> <span class="o">{</span>
<span class="c1">// sub-optimal approach</span>
<span class="kd">private</span> <span class="nc">ArrayList</span><span class="o">&lt;</span><span class="nc">SomeType</span><span class="o">&gt;</span> <span class="n">list</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">ArrayList</span><span class="o">&lt;&gt;();</span>
<span class="kd">public</span> <span class="nc">HashSet</span><span class="o">&lt;</span><span class="nc">SomeType</span><span class="o">&gt;</span> <span class="nf">getFoo</span><span class="o">()</span> <span class="o">{</span>
<span class="k">return</span> <span class="k">new</span> <span class="nc">HashSet</span><span class="o">&lt;</span><span class="nc">SomeType</span><span class="o">&gt;();</span>
<span class="o">}</span>
<span class="c1">// preferred approach</span>
<span class="kd">private</span> <span class="nc">List</span><span class="o">&lt;</span><span class="nc">SomeType</span><span class="o">&gt;</span> <span class="n">list</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">ArrayList</span><span class="o">&lt;&gt;();</span>
<span class="kd">public</span> <span class="nc">Set</span><span class="o">&lt;</span><span class="nc">SomeType</span><span class="o">&gt;</span> <span class="nf">getFoo</span><span class="o">()</span> <span class="o">{</span>
<span class="k">return</span> <span class="k">new</span> <span class="nc">HashSet</span><span class="o">&lt;</span><span class="nc">SomeType</span><span class="o">&gt;();</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>Use this rule by referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/LooseCoupling"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="methodreturnsinternalarray">MethodReturnsInternalArray</h2>
<p><strong>Since:</strong> PMD 2.2</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Exposing internal arrays to the caller violates object encapsulation since elements can be
removed or replaced outside of the object that owns it. It is safer to return a copy of the array.</p>
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/MethodReturnsInternalArrayRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.MethodReturnsInternalArrayRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">SecureSystem</span> <span class="o">{</span>
<span class="nc">UserData</span> <span class="o">[]</span> <span class="n">ud</span><span class="o">;</span>
<span class="kd">public</span> <span class="nc">UserData</span> <span class="o">[]</span> <span class="n">getUserData</span><span class="o">()</span> <span class="o">{</span>
<span class="c1">// Don't return directly the internal array, return a copy</span>
<span class="k">return</span> <span class="n">ud</span><span class="o">;</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>Use this rule by referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/MethodReturnsInternalArray"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="missingoverride">MissingOverride</h2>
<p><strong>Since:</strong> PMD 6.2.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p><strong>Minimum Language Version:</strong> Java 1.5</p>
<p>Annotating overridden methods with @Override ensures at compile time that
the method really overrides one, which helps refactoring and clarifies intent.</p>
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/MissingOverrideRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.MissingOverrideRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="kd">implements</span> <span class="nc">Runnable</span> <span class="o">{</span>
<span class="c1">// This method is overridden, and should have an @Override annotation</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">run</span><span class="o">()</span> <span class="o">{</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>Use this rule by referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/MissingOverride"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="onedeclarationperline">OneDeclarationPerLine</h2>
<p><strong>Since:</strong> PMD 5.0</p>
<p><strong>Priority:</strong> Medium Low (4)</p>
<p>Java allows the use of several variables declaration of the same type on one line. However, it
can lead to quite messy code. This rule looks for several declarations on the same line.</p>
<p><strong>This rule is defined by the following XPath expression:</strong></p>
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">LocalVariableDeclaration</span><span class="w">
</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="kn">parent::</span><span class="nt">ForInit</span><span class="p">)]</span><span class="w">
</span><span class="p">[</span><span class="nf">count</span><span class="p">(</span><span class="nt">VariableDeclarator</span><span class="p">)</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="m">1</span><span class="p">]</span><span class="w">
</span><span class="p">[</span><span class="nv">$strictMode</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="nf">count</span><span class="p">(</span><span class="nf">distinct-values</span><span class="p">(</span><span class="nt">VariableDeclarator</span><span class="err">/</span><span class="na">@BeginLine</span><span class="p">))</span><span class="w"> </span><span class="p">!</span><span class="err">=</span><span class="w"> </span><span class="nf">count</span><span class="p">(</span><span class="nt">VariableDeclarator</span><span class="p">)]</span><span class="w">
</span><span class="p">|</span><span class="w">
</span><span class="o">//</span><span class="nt">FieldDeclaration</span><span class="w">
</span><span class="p">[</span><span class="nf">count</span><span class="p">(</span><span class="nt">VariableDeclarator</span><span class="p">)</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="m">1</span><span class="p">]</span><span class="w">
</span><span class="p">[</span><span class="nv">$strictMode</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="nf">count</span><span class="p">(</span><span class="nf">distinct-values</span><span class="p">(</span><span class="nt">VariableDeclarator</span><span class="err">/</span><span class="na">@BeginLine</span><span class="p">))</span><span class="w"> </span><span class="p">!</span><span class="err">=</span><span class="w"> </span><span class="nf">count</span><span class="p">(</span><span class="nt">VariableDeclarator</span><span class="p">)]</span><span class="w">
</span></code></pre></div></div>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nc">String</span> <span class="n">name</span><span class="o">;</span> <span class="c1">// separate declarations</span>
<span class="nc">String</span> <span class="n">lastname</span><span class="o">;</span>
<span class="nc">String</span> <span class="n">name</span><span class="o">,</span> <span class="n">lastname</span><span class="o">;</span> <span class="c1">// combined declaration, a violation</span>
<span class="nc">String</span> <span class="n">name</span><span class="o">,</span>
<span class="n">lastname</span><span class="o">;</span> <span class="c1">// combined declaration on multiple lines, no violation by default.</span>
<span class="c1">// Set property strictMode to true to mark this as violation.</span>
</code></pre></div></div>
<p><strong>This rule has the following properties:</strong></p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Default Value</th>
<th>Description</th>
<th>Multivalued</th>
</tr>
</thead>
<tbody>
<tr>
<td>strictMode</td>
<td>false</td>
<td>If true, mark combined declaration even if the declarations are on separate lines.</td>
<td>no</td>
</tr>
</tbody>
</table>
<p><strong>Use this rule with the default properties by just referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/OneDeclarationPerLine"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<p><strong>Use this rule and customize it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/OneDeclarationPerLine"</span><span class="nt">&gt;</span>
<span class="nt">&lt;properties&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"strictMode"</span> <span class="na">value=</span><span class="s">"false"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;/properties&gt;</span>
<span class="nt">&lt;/rule&gt;</span>
</code></pre></div></div>
<h2 id="positionliteralsfirstincaseinsensitivecomparisons">PositionLiteralsFirstInCaseInsensitiveComparisons</h2>
<p><span style="border-radius: 0.25em; color: #fff; padding: 0.2em 0.6em 0.3em; display: inline; background-color: #d9534f;">Deprecated</span></p>
<p><strong>Since:</strong> PMD 5.1</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Position literals first in comparisons, if the second argument is null then NullPointerExceptions
can be avoided, they will just return false.</p>
<p>This rule is replaced by the more general rule <a href="pmd_rules_java_bestpractices.html#literalsfirstincomparisons"><code class="language-plaintext highlighter-rouge">LiteralsFirstInComparisons</code></a>.</p>
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/PositionLiteralsFirstInCaseInsensitiveComparisonsRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.PositionLiteralsFirstInCaseInsensitiveComparisonsRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
<span class="kt">boolean</span> <span class="nf">bar</span><span class="o">(</span><span class="nc">String</span> <span class="n">x</span><span class="o">)</span> <span class="o">{</span>
<span class="k">return</span> <span class="n">x</span><span class="o">.</span><span class="na">equalsIgnoreCase</span><span class="o">(</span><span class="s">"2"</span><span class="o">);</span> <span class="c1">// should be "2".equalsIgnoreCase(x)</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>Use this rule by referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/PositionLiteralsFirstInCaseInsensitiveComparisons"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="positionliteralsfirstincomparisons">PositionLiteralsFirstInComparisons</h2>
<p><span style="border-radius: 0.25em; color: #fff; padding: 0.2em 0.6em 0.3em; display: inline; background-color: #d9534f;">Deprecated</span></p>
<p><strong>Since:</strong> PMD 3.3</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Position literals first in comparisons, if the second argument is null then NullPointerExceptions
can be avoided, they will just return false.</p>
<p>This rule is replaced by the more general rule <a href="pmd_rules_java_bestpractices.html#literalsfirstincomparisons"><code class="language-plaintext highlighter-rouge">LiteralsFirstInComparisons</code></a>.</p>
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/PositionLiteralsFirstInComparisonsRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.PositionLiteralsFirstInComparisonsRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
<span class="kt">boolean</span> <span class="nf">bar</span><span class="o">(</span><span class="nc">String</span> <span class="n">x</span><span class="o">)</span> <span class="o">{</span>
<span class="k">return</span> <span class="n">x</span><span class="o">.</span><span class="na">equals</span><span class="o">(</span><span class="s">"2"</span><span class="o">);</span> <span class="c1">// should be "2".equals(x)</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>Use this rule by referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/PositionLiteralsFirstInComparisons"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="preservestacktrace">PreserveStackTrace</h2>
<p><strong>Since:</strong> PMD 3.7</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Throwing a new exception from a catch block without passing the original exception into the
new exception will cause the original stack trace to be lost making it difficult to debug
effectively.</p>
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/PreserveStackTraceRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.PreserveStackTraceRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
<span class="kt">void</span> <span class="nf">good</span><span class="o">()</span> <span class="o">{</span>
<span class="k">try</span><span class="o">{</span>
<span class="nc">Integer</span><span class="o">.</span><span class="na">parseInt</span><span class="o">(</span><span class="s">"a"</span><span class="o">);</span>
<span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="nc">Exception</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nf">Exception</span><span class="o">(</span><span class="n">e</span><span class="o">);</span> <span class="c1">// first possibility to create exception chain</span>
<span class="o">}</span>
<span class="k">try</span> <span class="o">{</span>
<span class="nc">Integer</span><span class="o">.</span><span class="na">parseInt</span><span class="o">(</span><span class="s">"a"</span><span class="o">);</span>
<span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="nc">Exception</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
<span class="k">throw</span> <span class="o">(</span><span class="nc">IllegalStateException</span><span class="o">)</span><span class="k">new</span> <span class="nc">IllegalStateException</span><span class="o">().</span><span class="na">initCause</span><span class="o">(</span><span class="n">e</span><span class="o">);</span> <span class="c1">// second possibility to create exception chain.</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="kt">void</span> <span class="nf">bad</span><span class="o">()</span> <span class="o">{</span>
<span class="k">try</span><span class="o">{</span>
<span class="nc">Integer</span><span class="o">.</span><span class="na">parseInt</span><span class="o">(</span><span class="s">"a"</span><span class="o">);</span>
<span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="nc">Exception</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nf">Exception</span><span class="o">(</span><span class="n">e</span><span class="o">.</span><span class="na">getMessage</span><span class="o">());</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>Use this rule by referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/PreserveStackTrace"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="primitivewrapperinstantiation">PrimitiveWrapperInstantiation</h2>
<p><strong>Since:</strong> PMD 6.37.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Reports usages of primitive wrapper constructors. They are deprecated
since Java 9 and should not be used. Even before Java 9, they can
be replaced with usage of the corresponding static <code class="language-plaintext highlighter-rouge">valueOf</code> factory method
(which may be automatically inserted by the compiler since Java 1.5).
This has the advantage that it may reuse common instances instead of creating
a new instance each time.</p>
<p>Note that for <code class="language-plaintext highlighter-rouge">Boolean</code>, the named constants <code class="language-plaintext highlighter-rouge">Boolean.TRUE</code> and <code class="language-plaintext highlighter-rouge">Boolean.FALSE</code>
are preferred instead of <code class="language-plaintext highlighter-rouge">Boolean.valueOf</code>.</p>
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/PrimitiveWrapperInstantiationRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.PrimitiveWrapperInstantiationRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
<span class="kd">private</span> <span class="nc">Integer</span> <span class="no">ZERO</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">Integer</span><span class="o">(</span><span class="mi">0</span><span class="o">);</span> <span class="c1">// violation</span>
<span class="kd">private</span> <span class="nc">Integer</span> <span class="no">ZERO1</span> <span class="o">=</span> <span class="nc">Integer</span><span class="o">.</span><span class="na">valueOf</span><span class="o">(</span><span class="mi">0</span><span class="o">);</span> <span class="c1">// better</span>
<span class="kd">private</span> <span class="nc">Integer</span> <span class="no">ZERO1</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span> <span class="c1">// even better</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>Use this rule by referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/PrimitiveWrapperInstantiation"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="replaceenumerationwithiterator">ReplaceEnumerationWithIterator</h2>
<p><strong>Since:</strong> PMD 3.4</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Consider replacing Enumeration usages with the newer java.util.Iterator</p>
<p><strong>This rule is defined by the following XPath expression:</strong></p>
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">ImplementsList</span><span class="o">/</span><span class="nt">ClassOrInterfaceType</span><span class="p">[</span><span class="na">@Image</span><span class="err">=</span><span class="s1">'Enumeration'</span><span class="p">]</span><span class="w">
</span></code></pre></div></div>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="kd">implements</span> <span class="nc">Enumeration</span> <span class="o">{</span>
<span class="kd">private</span> <span class="kt">int</span> <span class="n">x</span> <span class="o">=</span> <span class="mi">42</span><span class="o">;</span>
<span class="kd">public</span> <span class="kt">boolean</span> <span class="nf">hasMoreElements</span><span class="o">()</span> <span class="o">{</span>
<span class="k">return</span> <span class="kc">true</span><span class="o">;</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="nc">Object</span> <span class="nf">nextElement</span><span class="o">()</span> <span class="o">{</span>
<span class="k">return</span> <span class="nc">String</span><span class="o">.</span><span class="na">valueOf</span><span class="o">(</span><span class="n">i</span><span class="o">++);</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>Use this rule by referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/ReplaceEnumerationWithIterator"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="replacehashtablewithmap">ReplaceHashtableWithMap</h2>
<p><strong>Since:</strong> PMD 3.4</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Consider replacing Hashtable usage with the newer java.util.Map if thread safety is not required.</p>
<p><strong>This rule is defined by the following XPath expression:</strong></p>
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">Type</span><span class="o">/</span><span class="nt">ReferenceType</span><span class="o">/</span><span class="nt">ClassOrInterfaceType</span><span class="p">[</span><span class="na">@Image</span><span class="err">=</span><span class="s1">'Hashtable'</span><span class="p">]</span><span class="w">
</span></code></pre></div></div>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
<span class="kt">void</span> <span class="nf">bar</span><span class="o">()</span> <span class="o">{</span>
<span class="nc">Hashtable</span> <span class="n">h</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">Hashtable</span><span class="o">();</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>Use this rule by referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/ReplaceHashtableWithMap"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="replacevectorwithlist">ReplaceVectorWithList</h2>
<p><strong>Since:</strong> PMD 3.4</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Consider replacing Vector usages with the newer java.util.ArrayList if expensive thread-safe operations are not required.</p>
<p><strong>This rule is defined by the following XPath expression:</strong></p>
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">Type</span><span class="o">/</span><span class="nt">ReferenceType</span><span class="o">/</span><span class="nt">ClassOrInterfaceType</span><span class="p">[</span><span class="na">@Image</span><span class="err">=</span><span class="s1">'Vector'</span><span class="p">]</span><span class="w">
</span></code></pre></div></div>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
<span class="kt">void</span> <span class="nf">bar</span><span class="o">()</span> <span class="o">{</span>
<span class="nc">Vector</span> <span class="n">v</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">Vector</span><span class="o">();</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>Use this rule by referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/ReplaceVectorWithList"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="simplifiabletestassertion">SimplifiableTestAssertion</h2>
<p><strong>Since:</strong> PMD 6.37.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Reports test assertions that may be simplified using a more specific
assertion method. This enables better error messages, and makes the
assertions more readable.</p>
<p>The rule only applies within test classes for the moment. It replaces
the deprecated rules <a href="pmd_rules_java_bestpractices.html#useassertequalsinsteadofasserttrue"><code class="language-plaintext highlighter-rouge">UseAssertEqualsInsteadOfAssertTrue</code></a>,
<a href="pmd_rules_java_bestpractices.html#useassertnullinsteadofasserttrue"><code class="language-plaintext highlighter-rouge">UseAssertNullInsteadOfAssertTrue</code></a>, <a href="pmd_rules_java_bestpractices.html#useassertsameinsteadofasserttrue"><code class="language-plaintext highlighter-rouge">UseAssertSameInsteadOfAssertTrue</code></a>,
<a href="pmd_rules_java_bestpractices.html#useasserttrueinsteadofassertequals"><code class="language-plaintext highlighter-rouge">UseAssertTrueInsteadOfAssertEquals</code></a>, and <a href="pmd_rules_java_design.html#simplifybooleanassertion"><code class="language-plaintext highlighter-rouge">SimplifyBooleanAssertion</code></a>.</p>
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/SimplifiableTestAssertionRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.SimplifiableTestAssertionRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">import</span> <span class="nn">org.junit.Test</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">static</span> <span class="n">org</span><span class="o">.</span><span class="na">junit</span><span class="o">.</span><span class="na">Assert</span><span class="o">.*;</span>
<span class="kd">class</span> <span class="nc">SomeTestClass</span> <span class="o">{</span>
<span class="nc">Object</span> <span class="n">a</span><span class="o">,</span><span class="n">b</span><span class="o">;</span>
<span class="nd">@Test</span>
<span class="kt">void</span> <span class="nf">testMethod</span><span class="o">()</span> <span class="o">{</span>
<span class="n">assertTrue</span><span class="o">(</span><span class="n">a</span><span class="o">.</span><span class="na">equals</span><span class="o">(</span><span class="n">b</span><span class="o">));</span> <span class="c1">// could be assertEquals(a, b);</span>
<span class="n">assertTrue</span><span class="o">(!</span><span class="n">a</span><span class="o">.</span><span class="na">equals</span><span class="o">(</span><span class="n">b</span><span class="o">));</span> <span class="c1">// could be assertNotEquals(a, b);</span>
<span class="n">assertTrue</span><span class="o">(!</span><span class="n">something</span><span class="o">);</span> <span class="c1">// could be assertFalse(something);</span>
<span class="n">assertFalse</span><span class="o">(!</span><span class="n">something</span><span class="o">);</span> <span class="c1">// could be assertTrue(something);</span>
<span class="n">assertTrue</span><span class="o">(</span><span class="n">a</span> <span class="o">==</span> <span class="n">b</span><span class="o">);</span> <span class="c1">// could be assertSame(a, b);</span>
<span class="n">assertTrue</span><span class="o">(</span><span class="n">a</span> <span class="o">!=</span> <span class="n">b</span><span class="o">);</span> <span class="c1">// could be assertNotSame(a, b);</span>
<span class="n">assertTrue</span><span class="o">(</span><span class="n">a</span> <span class="o">==</span> <span class="kc">null</span><span class="o">);</span> <span class="c1">// could be assertNull(a);</span>
<span class="n">assertTrue</span><span class="o">(</span><span class="n">a</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">);</span> <span class="c1">// could be assertNotNull(a);</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>Use this rule by referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/SimplifiableTestAssertion"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="switchstmtsshouldhavedefault">SwitchStmtsShouldHaveDefault</h2>
<p><strong>Since:</strong> PMD 1.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Switch statements should be exhaustive, to make their control flow
easier to follow. This can be achieved by adding a <code class="language-plaintext highlighter-rouge">default</code> case, or,
if the switch is on an enum type, by ensuring there is one switch branch
for each enum constant.</p>
<p><strong>This rule is defined by the following XPath expression:</strong></p>
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">SwitchStatement</span><span class="p">[</span><span class="na">@DefaultCase</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="nf">false</span><span class="p">()</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="na">@ExhaustiveEnumSwitch</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="nf">false</span><span class="p">()]</span><span class="w">
</span></code></pre></div></div>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{{</span>
<span class="kt">int</span> <span class="n">x</span> <span class="o">=</span> <span class="mi">2</span><span class="o">;</span>
<span class="k">switch</span> <span class="o">(</span><span class="n">x</span><span class="o">)</span> <span class="o">{</span>
<span class="k">case</span> <span class="mi">1</span><span class="o">:</span> <span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">6</span><span class="o">;</span>
<span class="k">case</span> <span class="mi">2</span><span class="o">:</span> <span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">8</span><span class="o">;</span>
<span class="c1">// missing default: here</span>
<span class="o">}</span>
<span class="o">}}</span>
</code></pre></div></div>
<p><strong>Use this rule by referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/SwitchStmtsShouldHaveDefault"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="systemprintln">SystemPrintln</h2>
<p><strong>Since:</strong> PMD 2.1</p>
<p><strong>Priority:</strong> Medium High (2)</p>
<p>References to System.(out|err).print are usually intended for debugging purposes and can remain in
the codebase even in production code. By using a logger one can enable/disable this behaviour at
will (and by priority) and avoid clogging the Standard out log.</p>
<p><strong>This rule is defined by the following XPath expression:</strong></p>
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">Name</span><span class="p">[</span><span class="w">
</span><span class="nf">starts-with</span><span class="p">(</span><span class="na">@Image</span><span class="p">,</span><span class="w"> </span><span class="s1">'System.out.print'</span><span class="p">)</span><span class="w">
</span><span class="ow">or</span><span class="w">
</span><span class="nf">starts-with</span><span class="p">(</span><span class="na">@Image</span><span class="p">,</span><span class="w"> </span><span class="s1">'System.err.print'</span><span class="p">)</span><span class="w">
</span><span class="p">]</span><span class="w">
</span></code></pre></div></div>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">class</span> <span class="nc">Foo</span><span class="o">{</span>
<span class="nc">Logger</span> <span class="n">log</span> <span class="o">=</span> <span class="nc">Logger</span><span class="o">.</span><span class="na">getLogger</span><span class="o">(</span><span class="nc">Foo</span><span class="o">.</span><span class="na">class</span><span class="o">.</span><span class="na">getName</span><span class="o">());</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">testA</span> <span class="o">()</span> <span class="o">{</span>
<span class="nc">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">"Entering test"</span><span class="o">);</span>
<span class="c1">// Better use this</span>
<span class="n">log</span><span class="o">.</span><span class="na">fine</span><span class="o">(</span><span class="s">"Entering test"</span><span class="o">);</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>Use this rule by referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/SystemPrintln"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="unusedassignment">UnusedAssignment</h2>
<p><strong>Since:</strong> PMD 6.26.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Reports assignments to variables that are never used before the variable is overwritten,
or goes out of scope. Unused assignments are those for which</p>
<ol>
<li>The variable is never read after the assignment, or</li>
<li>The assigned value is always overwritten by other assignments before the next read of
the variable.</li>
</ol>
<p>The rule doesnt consider assignments to fields except for those of <code class="language-plaintext highlighter-rouge">this</code> in a constructor,
or static fields of the current class in static initializers.</p>
<p>The rule may be suppressed with the standard <code class="language-plaintext highlighter-rouge">@SuppressWarnings("unused")</code> tag.</p>
<p>The rule subsumes <a href="pmd_rules_java_bestpractices.html#unusedlocalvariable"><code class="language-plaintext highlighter-rouge">UnusedLocalVariable</code></a>, and <a href="pmd_rules_java_bestpractices.html#unusedformalparameter"><code class="language-plaintext highlighter-rouge">UnusedFormalParameter</code></a>.
Those violations are filtered
out by default, in case you already have enabled those rules, but may be enabled with the property
<code class="language-plaintext highlighter-rouge">reportUnusedVariables</code>. Variables whose name starts with <code class="language-plaintext highlighter-rouge">ignored</code> or <code class="language-plaintext highlighter-rouge">unused</code> are filtered out, as
is standard practice for exceptions.</p>
<p>Limitations:</p>
<ul>
<li>The rule currently cannot know which method calls throw exceptions, or which exceptions they throw.
In the body of a try block, every method or constructor call is assumed to throw. This may cause false-negatives.
The only other language construct that is assumed to throw is the <code class="language-plaintext highlighter-rouge">throw</code> statement, in particular,
things like <code class="language-plaintext highlighter-rouge">assert</code> statements, or NullPointerExceptions on dereference are ignored.</li>
<li>The rule cannot resolve assignments across constructors, when theyre called with the special
<code class="language-plaintext highlighter-rouge">this(...)</code> syntax. This may cause false-negatives.</li>
</ul>
<p>Both of those limitations may be partly relaxed in PMD 7.</p>
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UnusedAssignmentRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.UnusedAssignmentRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">class</span> <span class="nc">A</span> <span class="o">{</span>
<span class="c1">// this field initializer is redundant,</span>
<span class="c1">// it is always overwritten in the constructor</span>
<span class="kt">int</span> <span class="n">f</span> <span class="o">=</span> <span class="mi">1</span><span class="o">;</span>
<span class="no">A</span><span class="o">(</span><span class="kt">int</span> <span class="n">f</span><span class="o">)</span> <span class="o">{</span>
<span class="k">this</span><span class="o">.</span><span class="na">f</span> <span class="o">=</span> <span class="n">f</span><span class="o">;</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">class</span> <span class="nc">B</span> <span class="o">{</span>
<span class="kt">int</span> <span class="nf">method</span><span class="o">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">,</span> <span class="kt">int</span> <span class="n">j</span><span class="o">)</span> <span class="o">{</span>
<span class="c1">// this initializer is redundant,</span>
<span class="c1">// it is overwritten in all branches of the `if`</span>
<span class="kt">int</span> <span class="n">k</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span>
<span class="c1">// Both the assignments to k are unused, because k is</span>
<span class="c1">// not read after the if/else</span>
<span class="c1">// This may hide a bug: the programmer probably wanted to return k</span>
<span class="k">if</span> <span class="o">(</span><span class="n">i</span> <span class="o">&lt;</span> <span class="n">j</span><span class="o">)</span>
<span class="n">k</span> <span class="o">=</span> <span class="n">i</span><span class="o">;</span>
<span class="k">else</span>
<span class="n">k</span> <span class="o">=</span> <span class="n">j</span><span class="o">;</span>
<span class="k">return</span> <span class="n">j</span><span class="o">;</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">class</span> <span class="nc">C</span> <span class="o">{</span>
<span class="kt">int</span> <span class="nf">method</span><span class="o">()</span> <span class="o">{</span>
<span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span>
<span class="n">checkSomething</span><span class="o">(++</span><span class="n">i</span><span class="o">);</span>
<span class="n">checkSomething</span><span class="o">(++</span><span class="n">i</span><span class="o">);</span>
<span class="n">checkSomething</span><span class="o">(++</span><span class="n">i</span><span class="o">);</span>
<span class="n">checkSomething</span><span class="o">(++</span><span class="n">i</span><span class="o">);</span>
<span class="c1">// That last increment is not reported unless</span>
<span class="c1">// the property `checkUnusedPrefixIncrement` is</span>
<span class="c1">// set to `true`</span>
<span class="c1">// Technically it could be written (i+1), but it</span>
<span class="c1">// is not very important</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">class</span> <span class="nc">C</span> <span class="o">{</span>
<span class="c1">// variables that are truly unused (at most assigned to, but never accessed)</span>
<span class="c1">// are only reported if property `reportUnusedVariables` is true</span>
<span class="kt">void</span> <span class="nf">method</span><span class="o">(</span><span class="kt">int</span> <span class="n">param</span><span class="o">)</span> <span class="o">{</span> <span class="o">}</span> <span class="c1">// for example this method parameter</span>
<span class="c1">// even then, you can suppress the violation with an annotation:</span>
<span class="kt">void</span> <span class="nf">method</span><span class="o">(</span><span class="nd">@SuppressWarning</span><span class="o">(</span><span class="s">"unused"</span><span class="o">)</span> <span class="kt">int</span> <span class="n">param</span><span class="o">)</span> <span class="o">{</span> <span class="o">}</span> <span class="c1">// no violation, even if `reportUnusedVariables` is true</span>
<span class="c1">// For catch parameters, or for resources which don't need to be used explicitly,</span>
<span class="c1">// you can give a name that starts with "ignored" to ignore such warnings</span>
<span class="o">{</span>
<span class="k">try</span> <span class="o">(</span><span class="nc">Something</span> <span class="n">ignored</span> <span class="o">=</span> <span class="nc">Something</span><span class="o">.</span><span class="na">create</span><span class="o">())</span> <span class="o">{</span>
<span class="c1">// even if ignored is unused, it won't be flagged</span>
<span class="c1">// its purpose might be to side-effect in the create/close routines</span>
<span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="nc">Exception</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span> <span class="c1">// this is unused and will cause a warning if `reportUnusedVariables` is true</span>
<span class="c1">// you should choose a name that starts with "ignored"</span>
<span class="k">return</span><span class="o">;</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>This rule has the following properties:</strong></p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Default Value</th>
<th>Description</th>
<th>Multivalued</th>
</tr>
</thead>
<tbody>
<tr>
<td>checkUnusedPrefixIncrement</td>
<td>false</td>
<td>Report expressions like ++i that may be replaced with (i + 1)</td>
<td>no</td>
</tr>
<tr>
<td>reportUnusedVariables</td>
<td>false</td>
<td>Report variables that are only initialized, and never read at all. The rule UnusedVariable already cares for that, but you can enable it if needed</td>
<td>no</td>
</tr>
</tbody>
</table>
<p><strong>Use this rule with the default properties by just referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/UnusedAssignment"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<p><strong>Use this rule and customize it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/UnusedAssignment"</span><span class="nt">&gt;</span>
<span class="nt">&lt;properties&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"checkUnusedPrefixIncrement"</span> <span class="na">value=</span><span class="s">"false"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"reportUnusedVariables"</span> <span class="na">value=</span><span class="s">"false"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;/properties&gt;</span>
<span class="nt">&lt;/rule&gt;</span>
</code></pre></div></div>
<h2 id="unusedformalparameter">UnusedFormalParameter</h2>
<p><strong>Since:</strong> PMD 0.8</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Reports parameters of methods and constructors that are not referenced them in the method body.
Parameters whose name starts with <code class="language-plaintext highlighter-rouge">ignored</code> or <code class="language-plaintext highlighter-rouge">unused</code> are filtered out.</p>
<p>Removing unused formal parameters from public methods could cause a ripple effect through the code base.
Hence, by default, this rule only considers private methods. To include non-private methods, set the
<code class="language-plaintext highlighter-rouge">checkAll</code> property to <code class="language-plaintext highlighter-rouge">true</code>.</p>
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UnusedFormalParameterRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.UnusedFormalParameterRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
<span class="kd">private</span> <span class="kt">void</span> <span class="nf">bar</span><span class="o">(</span><span class="nc">String</span> <span class="n">howdy</span><span class="o">)</span> <span class="o">{</span>
<span class="c1">// howdy is not used</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>This rule has the following properties:</strong></p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Default Value</th>
<th>Description</th>
<th>Multivalued</th>
</tr>
</thead>
<tbody>
<tr>
<td>checkAll</td>
<td>false</td>
<td>Check all methods, including non-private ones</td>
<td>no</td>
</tr>
</tbody>
</table>
<p><strong>Use this rule with the default properties by just referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/UnusedFormalParameter"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<p><strong>Use this rule and customize it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/UnusedFormalParameter"</span><span class="nt">&gt;</span>
<span class="nt">&lt;properties&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"checkAll"</span> <span class="na">value=</span><span class="s">"false"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;/properties&gt;</span>
<span class="nt">&lt;/rule&gt;</span>
</code></pre></div></div>
<h2 id="unusedimports">UnusedImports</h2>
<p><span style="border-radius: 0.25em; color: #fff; padding: 0.2em 0.6em 0.3em; display: inline; background-color: #d9534f;">Deprecated</span></p>
<p><strong>Since:</strong> PMD 1.0</p>
<p><strong>Priority:</strong> Medium Low (4)</p>
<p>Reports import statements that are not used within the file. This also reports
duplicate imports, and imports from the same package. The simplest fix is just
to delete those imports.</p>
<p>This rule is deprecated since PMD 6.34.0. Use the rule <a href="pmd_rules_java_codestyle.html#unnecessaryimport"><code class="language-plaintext highlighter-rouge">UnnecessaryImport</code></a>
from category codestyle instead.</p>
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UnusedImportsRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.UnusedImportsRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">import</span> <span class="nn">java.io.File</span><span class="o">;</span> <span class="c1">// not referenced or required</span>
<span class="kn">import</span> <span class="nn">java.util.*</span><span class="o">;</span> <span class="c1">// not referenced or required</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{}</span>
</code></pre></div></div>
<p><strong>Use this rule by referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/UnusedImports"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="unusedlocalvariable">UnusedLocalVariable</h2>
<p><strong>Since:</strong> PMD 0.1</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Detects when a local variable is declared and/or assigned, but not used.
Variables whose name starts with <code class="language-plaintext highlighter-rouge">ignored</code> or <code class="language-plaintext highlighter-rouge">unused</code> are filtered out.</p>
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UnusedLocalVariableRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.UnusedLocalVariableRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">doSomething</span><span class="o">()</span> <span class="o">{</span>
<span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">5</span><span class="o">;</span> <span class="c1">// Unused</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>Use this rule by referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/UnusedLocalVariable"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="unusedprivatefield">UnusedPrivateField</h2>
<p><strong>Since:</strong> PMD 0.1</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Detects when a private field is declared and/or assigned a value, but not used.</p>
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UnusedPrivateFieldRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.UnusedPrivateFieldRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Something</span> <span class="o">{</span>
<span class="kd">private</span> <span class="kd">static</span> <span class="kt">int</span> <span class="no">FOO</span> <span class="o">=</span> <span class="mi">2</span><span class="o">;</span> <span class="c1">// Unused</span>
<span class="kd">private</span> <span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">5</span><span class="o">;</span> <span class="c1">// Unused</span>
<span class="kd">private</span> <span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">6</span><span class="o">;</span>
<span class="kd">public</span> <span class="kt">int</span> <span class="nf">addOne</span><span class="o">()</span> <span class="o">{</span>
<span class="k">return</span> <span class="n">j</span><span class="o">++;</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>This rule has the following properties:</strong></p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Default Value</th>
<th>Description</th>
<th>Multivalued</th>
</tr>
</thead>
<tbody>
<tr>
<td>ignoredAnnotations</td>
<td>lombok.Setter | lombok.Getter | lombok.Builder | lombok.Data | lombok.RequiredArgsConstructor | lombok.AllArgsConstructor | lombok.Value | lombok.NoArgsConstructor | java.lang.Deprecated | javafx.fxml.FXML | lombok.experimental.Delegate | lombok.EqualsAndHashCode</td>
<td>Fully qualified names of the annotation types that should be ignored by this rule</td>
<td>yes. Delimiter is |.</td>
</tr>
</tbody>
</table>
<p><strong>Use this rule with the default properties by just referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/UnusedPrivateField"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<p><strong>Use this rule and customize it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/UnusedPrivateField"</span><span class="nt">&gt;</span>
<span class="nt">&lt;properties&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"ignoredAnnotations"</span> <span class="na">value=</span><span class="s">"lombok.Setter|lombok.Getter|lombok.Builder|lombok.Data|lombok.RequiredArgsConstructor|lombok.AllArgsConstructor|lombok.Value|lombok.NoArgsConstructor|java.lang.Deprecated|javafx.fxml.FXML|lombok.experimental.Delegate|lombok.EqualsAndHashCode"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;/properties&gt;</span>
<span class="nt">&lt;/rule&gt;</span>
</code></pre></div></div>
<h2 id="unusedprivatemethod">UnusedPrivateMethod</h2>
<p><strong>Since:</strong> PMD 0.7</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Unused Private Method detects when a private method is declared but is unused.</p>
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UnusedPrivateMethodRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.UnusedPrivateMethodRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Something</span> <span class="o">{</span>
<span class="kd">private</span> <span class="kt">void</span> <span class="nf">foo</span><span class="o">()</span> <span class="o">{}</span> <span class="c1">// unused</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>This rule has the following properties:</strong></p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Default Value</th>
<th>Description</th>
<th>Multivalued</th>
</tr>
</thead>
<tbody>
<tr>
<td>ignoredAnnotations</td>
<td>java.lang.Deprecated</td>
<td>Fully qualified names of the annotation types that should be ignored by this rule</td>
<td>yes. Delimiter is |.</td>
</tr>
</tbody>
</table>
<p><strong>Use this rule with the default properties by just referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/UnusedPrivateMethod"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<p><strong>Use this rule and customize it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/UnusedPrivateMethod"</span><span class="nt">&gt;</span>
<span class="nt">&lt;properties&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"ignoredAnnotations"</span> <span class="na">value=</span><span class="s">"java.lang.Deprecated"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;/properties&gt;</span>
<span class="nt">&lt;/rule&gt;</span>
</code></pre></div></div>
<h2 id="useassertequalsinsteadofasserttrue">UseAssertEqualsInsteadOfAssertTrue</h2>
<p><span style="border-radius: 0.25em; color: #fff; padding: 0.2em 0.6em 0.3em; display: inline; background-color: #d9534f;">Deprecated</span></p>
<p><strong>Since:</strong> PMD 3.1</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>This rule detects JUnit assertions in object equality. These assertions should be made by more specific methods, like assertEquals.</p>
<p>Deprecated since PMD 6.37.0, use <a href="pmd_rules_java_bestpractices.html#simplifiabletestassertion"><code class="language-plaintext highlighter-rouge">SimplifiableTestAssertion</code></a> instead.</p>
<p><strong>This rule is defined by the following XPath expression:</strong></p>
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">ClassOrInterfaceDeclaration</span><span class="p">[</span><span class="w">
</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'junit.framework.TestCase'</span><span class="p">)</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="p">.</span><span class="o">//</span><span class="nt">MarkerAnnotation</span><span class="o">/</span><span class="nt">Name</span><span class="p">[</span><span class="w">
</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.Test'</span><span class="p">)</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.Test'</span><span class="p">)</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.RepeatedTest'</span><span class="p">)</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.TestFactory'</span><span class="p">)</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.TestTemplate'</span><span class="p">)</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.params.ParameterizedTest'</span><span class="p">)</span><span class="w">
</span><span class="p">]</span><span class="w">
</span><span class="p">]</span><span class="w">
</span><span class="o">//</span><span class="nt">PrimaryExpression</span><span class="p">[</span><span class="w">
</span><span class="nt">PrimaryPrefix</span><span class="o">/</span><span class="nt">Name</span><span class="p">[</span><span class="na">@Image</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'assertTrue'</span><span class="p">]</span><span class="w">
</span><span class="p">][</span><span class="w">
</span><span class="nt">PrimarySuffix</span><span class="o">/</span><span class="nt">Arguments</span><span class="o">/</span><span class="nt">ArgumentList</span><span class="o">/</span><span class="nt">Expression</span><span class="o">/</span><span class="nt">PrimaryExpression</span><span class="o">/</span><span class="nt">PrimaryPrefix</span><span class="o">/</span><span class="nt">Name</span><span class="w">
</span><span class="p">[</span><span class="nf">ends-with</span><span class="p">(</span><span class="na">@Image</span><span class="p">,</span><span class="w"> </span><span class="s1">'.equals'</span><span class="p">)]</span><span class="w">
</span><span class="p">]</span><span class="w">
</span></code></pre></div></div>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">FooTest</span> <span class="kd">extends</span> <span class="nc">TestCase</span> <span class="o">{</span>
<span class="kt">void</span> <span class="nf">testCode</span><span class="o">()</span> <span class="o">{</span>
<span class="nc">Object</span> <span class="n">a</span><span class="o">,</span> <span class="n">b</span><span class="o">;</span>
<span class="n">assertTrue</span><span class="o">(</span><span class="n">a</span><span class="o">.</span><span class="na">equals</span><span class="o">(</span><span class="n">b</span><span class="o">));</span> <span class="c1">// bad usage</span>
<span class="n">assertEquals</span><span class="o">(</span><span class="s">"a should equals b"</span><span class="o">,</span> <span class="n">a</span><span class="o">,</span> <span class="n">b</span><span class="o">);</span> <span class="c1">// good usage</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>Use this rule by referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/UseAssertEqualsInsteadOfAssertTrue"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="useassertnullinsteadofasserttrue">UseAssertNullInsteadOfAssertTrue</h2>
<p><span style="border-radius: 0.25em; color: #fff; padding: 0.2em 0.6em 0.3em; display: inline; background-color: #d9534f;">Deprecated</span></p>
<p><strong>Since:</strong> PMD 3.5</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>This rule detects JUnit assertions in object references equality. These assertions should be made by
more specific methods, like assertNull, assertNotNull.</p>
<p>Deprecated since PMD 6.37.0, use <a href="pmd_rules_java_bestpractices.html#simplifiabletestassertion"><code class="language-plaintext highlighter-rouge">SimplifiableTestAssertion</code></a> instead.</p>
<p><strong>This rule is defined by the following XPath expression:</strong></p>
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">ClassOrInterfaceDeclaration</span><span class="p">[</span><span class="w">
</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'junit.framework.TestCase'</span><span class="p">)</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="p">.</span><span class="o">//</span><span class="nt">MarkerAnnotation</span><span class="o">/</span><span class="nt">Name</span><span class="p">[</span><span class="w">
</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.Test'</span><span class="p">)</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.Test'</span><span class="p">)</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.RepeatedTest'</span><span class="p">)</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.TestFactory'</span><span class="p">)</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.TestTemplate'</span><span class="p">)</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.params.ParameterizedTest'</span><span class="p">)</span><span class="w">
</span><span class="p">]</span><span class="w">
</span><span class="p">]</span><span class="w">
</span><span class="o">//</span><span class="nt">PrimaryExpression</span><span class="p">[</span><span class="w">
</span><span class="nt">PrimaryPrefix</span><span class="o">/</span><span class="nt">Name</span><span class="p">[</span><span class="na">@Image</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'assertTrue'</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="na">@Image</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'assertFalse'</span><span class="p">]</span><span class="w">
</span><span class="p">][</span><span class="w">
</span><span class="nt">PrimarySuffix</span><span class="o">/</span><span class="nt">Arguments</span><span class="o">/</span><span class="nt">ArgumentList</span><span class="p">[</span><span class="w">
</span><span class="nt">Expression</span><span class="o">/</span><span class="nt">EqualityExpression</span><span class="o">/</span><span class="nt">PrimaryExpression</span><span class="o">/</span><span class="nt">PrimaryPrefix</span><span class="o">/</span><span class="nt">Literal</span><span class="o">/</span><span class="nt">NullLiteral</span><span class="w">
</span><span class="p">]</span><span class="w">
</span><span class="p">]</span><span class="w">
</span></code></pre></div></div>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">FooTest</span> <span class="kd">extends</span> <span class="nc">TestCase</span> <span class="o">{</span>
<span class="kt">void</span> <span class="nf">testCode</span><span class="o">()</span> <span class="o">{</span>
<span class="nc">Object</span> <span class="n">a</span> <span class="o">=</span> <span class="n">doSomething</span><span class="o">();</span>
<span class="n">assertTrue</span><span class="o">(</span><span class="n">a</span><span class="o">==</span><span class="kc">null</span><span class="o">);</span> <span class="c1">// bad usage</span>
<span class="n">assertNull</span><span class="o">(</span><span class="n">a</span><span class="o">);</span> <span class="c1">// good usage</span>
<span class="n">assertTrue</span><span class="o">(</span><span class="n">a</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">);</span> <span class="c1">// bad usage</span>
<span class="n">assertNotNull</span><span class="o">(</span><span class="n">a</span><span class="o">);</span> <span class="c1">// good usage</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>Use this rule by referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/UseAssertNullInsteadOfAssertTrue"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="useassertsameinsteadofasserttrue">UseAssertSameInsteadOfAssertTrue</h2>
<p><span style="border-radius: 0.25em; color: #fff; padding: 0.2em 0.6em 0.3em; display: inline; background-color: #d9534f;">Deprecated</span></p>
<p><strong>Since:</strong> PMD 3.1</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>This rule detects JUnit assertions in object references equality. These assertions should be made
by more specific methods, like assertSame, assertNotSame.</p>
<p>Deprecated since PMD 6.37.0, use <a href="pmd_rules_java_bestpractices.html#simplifiabletestassertion"><code class="language-plaintext highlighter-rouge">SimplifiableTestAssertion</code></a> instead.</p>
<p><strong>This rule is defined by the following XPath expression:</strong></p>
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">ClassOrInterfaceDeclaration</span><span class="p">[</span><span class="w">
</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'junit.framework.TestCase'</span><span class="p">)</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="p">.</span><span class="o">//</span><span class="nt">MarkerAnnotation</span><span class="o">/</span><span class="nt">Name</span><span class="p">[</span><span class="w">
</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.Test'</span><span class="p">)</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.Test'</span><span class="p">)</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.RepeatedTest'</span><span class="p">)</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.TestFactory'</span><span class="p">)</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.TestTemplate'</span><span class="p">)</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.params.ParameterizedTest'</span><span class="p">)</span><span class="w">
</span><span class="p">]</span><span class="w">
</span><span class="p">]</span><span class="w">
</span><span class="o">//</span><span class="nt">PrimaryExpression</span><span class="p">[</span><span class="w">
</span><span class="nt">PrimaryPrefix</span><span class="o">/</span><span class="nt">Name</span><span class="p">[</span><span class="na">@Image</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'assertTrue'</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="na">@Image</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'assertFalse'</span><span class="p">]</span><span class="w">
</span><span class="p">]</span><span class="w">
</span><span class="p">[</span><span class="w">
</span><span class="nt">PrimarySuffix</span><span class="o">/</span><span class="nt">Arguments</span><span class="o">/</span><span class="nt">ArgumentList</span><span class="o">/</span><span class="nt">Expression</span><span class="o">/</span><span class="nt">EqualityExpression</span><span class="w">
</span><span class="p">[</span><span class="ow">not</span><span class="p">(.</span><span class="o">//</span><span class="nt">NullLiteral</span><span class="p">)]</span><span class="w">
</span><span class="p">]</span><span class="w">
</span></code></pre></div></div>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">FooTest</span> <span class="kd">extends</span> <span class="nc">TestCase</span> <span class="o">{</span>
<span class="kt">void</span> <span class="nf">testCode</span><span class="o">()</span> <span class="o">{</span>
<span class="nc">Object</span> <span class="n">a</span><span class="o">,</span> <span class="n">b</span><span class="o">;</span>
<span class="n">assertTrue</span><span class="o">(</span><span class="n">a</span> <span class="o">==</span> <span class="n">b</span><span class="o">);</span> <span class="c1">// bad usage</span>
<span class="n">assertSame</span><span class="o">(</span><span class="n">a</span><span class="o">,</span> <span class="n">b</span><span class="o">);</span> <span class="c1">// good usage</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>Use this rule by referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/UseAssertSameInsteadOfAssertTrue"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="useasserttrueinsteadofassertequals">UseAssertTrueInsteadOfAssertEquals</h2>
<p><span style="border-radius: 0.25em; color: #fff; padding: 0.2em 0.6em 0.3em; display: inline; background-color: #d9534f;">Deprecated</span></p>
<p><strong>Since:</strong> PMD 5.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>When asserting a value is the same as a literal or Boxed boolean, use assertTrue/assertFalse, instead of assertEquals.</p>
<p>Deprecated since PMD 6.37.0, use <a href="pmd_rules_java_bestpractices.html#simplifiabletestassertion"><code class="language-plaintext highlighter-rouge">SimplifiableTestAssertion</code></a> instead.</p>
<p><strong>This rule is defined by the following XPath expression:</strong></p>
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">PrimaryExpression</span><span class="p">[</span><span class="nt">PrimaryPrefix</span><span class="o">/</span><span class="nt">Name</span><span class="p">[</span><span class="na">@Image</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'assertEquals'</span><span class="p">]]</span><span class="w">
</span><span class="p">[</span><span class="w">
</span><span class="nt">PrimarySuffix</span><span class="o">/</span><span class="nt">Arguments</span><span class="o">/</span><span class="nt">ArgumentList</span><span class="o">/</span><span class="nt">Expression</span><span class="o">/</span><span class="nt">PrimaryExpression</span><span class="o">/</span><span class="nt">PrimaryPrefix</span><span class="o">/</span><span class="nt">Literal</span><span class="o">/</span><span class="nt">BooleanLiteral</span><span class="w">
</span><span class="ow">or</span><span class="w">
</span><span class="nt">PrimarySuffix</span><span class="o">/</span><span class="nt">Arguments</span><span class="o">/</span><span class="nt">ArgumentList</span><span class="o">/</span><span class="nt">Expression</span><span class="o">/</span><span class="nt">PrimaryExpression</span><span class="o">/</span><span class="nt">PrimaryPrefix</span><span class="w">
</span><span class="o">/</span><span class="nt">Name</span><span class="p">[(</span><span class="na">@Image</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'Boolean.TRUE'</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="na">@Image</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'Boolean.FALSE'</span><span class="p">)]</span><span class="w">
</span><span class="p">]</span><span class="w">
</span></code></pre></div></div>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">MyTestCase</span> <span class="kd">extends</span> <span class="nc">TestCase</span> <span class="o">{</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">testMyCase</span><span class="o">()</span> <span class="o">{</span>
<span class="kt">boolean</span> <span class="n">myVar</span> <span class="o">=</span> <span class="kc">true</span><span class="o">;</span>
<span class="c1">// Ok</span>
<span class="n">assertTrue</span><span class="o">(</span><span class="s">"myVar is true"</span><span class="o">,</span> <span class="n">myVar</span><span class="o">);</span>
<span class="c1">// Bad</span>
<span class="n">assertEquals</span><span class="o">(</span><span class="s">"myVar is true"</span><span class="o">,</span> <span class="kc">true</span><span class="o">,</span> <span class="n">myVar</span><span class="o">);</span>
<span class="c1">// Bad</span>
<span class="n">assertEquals</span><span class="o">(</span><span class="s">"myVar is false"</span><span class="o">,</span> <span class="kc">false</span><span class="o">,</span> <span class="n">myVar</span><span class="o">);</span>
<span class="c1">// Bad</span>
<span class="n">assertEquals</span><span class="o">(</span><span class="s">"myVar is true"</span><span class="o">,</span> <span class="nc">Boolean</span><span class="o">.</span><span class="na">TRUE</span><span class="o">,</span> <span class="n">myVar</span><span class="o">);</span>
<span class="c1">// Bad</span>
<span class="n">assertEquals</span><span class="o">(</span><span class="s">"myVar is false"</span><span class="o">,</span> <span class="nc">Boolean</span><span class="o">.</span><span class="na">FALSE</span><span class="o">,</span> <span class="n">myVar</span><span class="o">);</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>Use this rule by referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/UseAssertTrueInsteadOfAssertEquals"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="usecollectionisempty">UseCollectionIsEmpty</h2>
<p><strong>Since:</strong> PMD 3.9</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>The isEmpty() method on java.util.Collection is provided to determine if a collection has any elements.
Comparing the value of size() to 0 does not convey intent as well as the isEmpty() method.</p>
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UseCollectionIsEmptyRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.UseCollectionIsEmptyRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
<span class="kt">void</span> <span class="nf">good</span><span class="o">()</span> <span class="o">{</span>
<span class="nc">List</span> <span class="n">foo</span> <span class="o">=</span> <span class="n">getList</span><span class="o">();</span>
<span class="k">if</span> <span class="o">(</span><span class="n">foo</span><span class="o">.</span><span class="na">isEmpty</span><span class="o">())</span> <span class="o">{</span>
<span class="c1">// blah</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="kt">void</span> <span class="nf">bad</span><span class="o">()</span> <span class="o">{</span>
<span class="nc">List</span> <span class="n">foo</span> <span class="o">=</span> <span class="n">getList</span><span class="o">();</span>
<span class="k">if</span> <span class="o">(</span><span class="n">foo</span><span class="o">.</span><span class="na">size</span><span class="o">()</span> <span class="o">==</span> <span class="mi">0</span><span class="o">)</span> <span class="o">{</span>
<span class="c1">// blah</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>Use this rule by referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/UseCollectionIsEmpty"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="usestandardcharsets">UseStandardCharsets</h2>
<p><strong>Since:</strong> PMD 6.34.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p><strong>Minimum Language Version:</strong> Java 1.7</p>
<p>Starting with Java 7, StandardCharsets provides constants for common Charset objects, such as UTF-8.
Using the constants is less error prone, and can provide a small performance advantage compared to <code class="language-plaintext highlighter-rouge">Charset.forName(...)</code>
since no scan across the internal <code class="language-plaintext highlighter-rouge">Charset</code> caches is needed.</p>
<p><strong>This rule is defined by the following XPath expression:</strong></p>
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">PrimaryExpression</span><span class="p">[</span><span class="nt">PrimaryPrefix</span><span class="o">/</span><span class="nt">Name</span><span class="err">/</span><span class="na">@Image</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'Charset.forName'</span><span class="p">]</span><span class="w">
</span><span class="o">/</span><span class="nt">PrimarySuffix</span><span class="o">/</span><span class="nt">Arguments</span><span class="o">/</span><span class="nt">ArgumentList</span><span class="o">/</span><span class="nt">Expression</span><span class="o">/</span><span class="nt">PrimaryExpression</span><span class="o">/</span><span class="nt">PrimaryPrefix</span><span class="w">
</span><span class="o">/</span><span class="nt">Literal</span><span class="p">[</span><span class="na">@Image</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="p">(</span><span class="s1">'"US-ASCII"'</span><span class="p">,</span><span class="w"> </span><span class="s1">'"ISO-8859-1"'</span><span class="p">,</span><span class="w"> </span><span class="s1">'"UTF-8"'</span><span class="p">,</span><span class="w"> </span><span class="s1">'"UTF-16BE"'</span><span class="p">,</span><span class="w"> </span><span class="s1">'"UTF-16LE"'</span><span class="p">,</span><span class="w"> </span><span class="s1">'"UTF-16"'</span><span class="p">)]</span><span class="w">
</span></code></pre></div></div>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">UseStandardCharsets</span> <span class="o">{</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">run</span><span class="o">()</span> <span class="o">{</span>
<span class="c1">// looking up the charset dynamically</span>
<span class="k">try</span> <span class="o">(</span><span class="nc">OutputStreamWriter</span> <span class="n">osw</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">OutputStreamWriter</span><span class="o">(</span><span class="n">out</span><span class="o">,</span> <span class="nc">Charset</span><span class="o">.</span><span class="na">forName</span><span class="o">(</span><span class="s">"UTF-8"</span><span class="o">)))</span> <span class="o">{</span>
<span class="n">osw</span><span class="o">.</span><span class="na">write</span><span class="o">(</span><span class="s">"test"</span><span class="o">);</span>
<span class="o">}</span>
<span class="c1">// best to use StandardCharsets</span>
<span class="k">try</span> <span class="o">(</span><span class="nc">OutputStreamWriter</span> <span class="n">osw</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">OutputStreamWriter</span><span class="o">(</span><span class="n">out</span><span class="o">,</span> <span class="nc">StandardCharsets</span><span class="o">.</span><span class="na">UTF_8</span><span class="o">))</span> <span class="o">{</span>
<span class="n">osw</span><span class="o">.</span><span class="na">write</span><span class="o">(</span><span class="s">"test"</span><span class="o">);</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>Use this rule by referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/UseStandardCharsets"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="usetrywithresources">UseTryWithResources</h2>
<p><strong>Since:</strong> PMD 6.12.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p><strong>Minimum Language Version:</strong> Java 1.7</p>
<p>Java 7 introduced the try-with-resources statement. This statement ensures that each resource is closed at the end
of the statement. It avoids the need of explicitly closing the resources in a finally block. Additionally exceptions
are better handled: If an exception occurred both in the <code class="language-plaintext highlighter-rouge">try</code> block and <code class="language-plaintext highlighter-rouge">finally</code> block, then the exception from
the try block was suppressed. With the <code class="language-plaintext highlighter-rouge">try</code>-with-resources statement, the exception thrown from the try-block is
preserved.</p>
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UseTryWithResourcesRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.UseTryWithResourcesRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">TryWithResources</span> <span class="o">{</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">run</span><span class="o">()</span> <span class="o">{</span>
<span class="nc">InputStream</span> <span class="n">in</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span>
<span class="k">try</span> <span class="o">{</span>
<span class="n">in</span> <span class="o">=</span> <span class="n">openInputStream</span><span class="o">();</span>
<span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="n">in</span><span class="o">.</span><span class="na">read</span><span class="o">();</span>
<span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="nc">IOException</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
<span class="n">e</span><span class="o">.</span><span class="na">printStackTrace</span><span class="o">();</span>
<span class="o">}</span> <span class="k">finally</span> <span class="o">{</span>
<span class="k">try</span> <span class="o">{</span>
<span class="k">if</span> <span class="o">(</span><span class="n">in</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">)</span> <span class="n">in</span><span class="o">.</span><span class="na">close</span><span class="o">();</span>
<span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="nc">IOException</span> <span class="n">ignored</span><span class="o">)</span> <span class="o">{</span>
<span class="c1">// ignored</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="c1">// better use try-with-resources</span>
<span class="k">try</span> <span class="o">(</span><span class="nc">InputStream</span> <span class="n">in2</span> <span class="o">=</span> <span class="n">openInputStream</span><span class="o">())</span> <span class="o">{</span>
<span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="n">in2</span><span class="o">.</span><span class="na">read</span><span class="o">();</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>This rule has the following properties:</strong></p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Default Value</th>
<th>Description</th>
<th>Multivalued</th>
</tr>
</thead>
<tbody>
<tr>
<td>closeMethods</td>
<td>close , closeQuietly</td>
<td>Method names in finally block, which trigger this rule</td>
<td>yes. Delimiter is ,.</td>
</tr>
</tbody>
</table>
<p><strong>Use this rule with the default properties by just referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/UseTryWithResources"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<p><strong>Use this rule and customize it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/UseTryWithResources"</span><span class="nt">&gt;</span>
<span class="nt">&lt;properties&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"closeMethods"</span> <span class="na">value=</span><span class="s">"close,closeQuietly"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;/properties&gt;</span>
<span class="nt">&lt;/rule&gt;</span>
</code></pre></div></div>
<h2 id="usevarargs">UseVarargs</h2>
<p><strong>Since:</strong> PMD 5.0</p>
<p><strong>Priority:</strong> Medium Low (4)</p>
<p><strong>Minimum Language Version:</strong> Java 1.5</p>
<p>Java 5 introduced the varargs parameter declaration for methods and constructors. This syntactic
sugar provides flexibility for users of these methods and constructors, allowing them to avoid
having to deal with the creation of an array.</p>
<p>Byte arrays in any method and String arrays in <code class="language-plaintext highlighter-rouge">public static void main(String[])</code> methods are ignored.</p>
<p><strong>This rule is defined by the following XPath expression:</strong></p>
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">FormalParameters</span><span class="o">/</span><span class="nt">FormalParameter</span><span class="w">
</span><span class="p">[</span><span class="nf">position</span><span class="p">()</span><span class="err">=</span><span class="nf">last</span><span class="p">()]</span><span class="w">
</span><span class="p">[</span><span class="nt">VariableDeclaratorId</span><span class="err">/</span><span class="na">@ArrayType</span><span class="err">=</span><span class="nf">true</span><span class="p">()]</span><span class="w">
</span><span class="p">[</span><span class="na">@Varargs</span><span class="err">=</span><span class="nf">false</span><span class="p">()]</span><span class="w">
</span><span class="p">[</span><span class="ow">not</span><span class="w"> </span><span class="p">(.</span><span class="o">/</span><span class="nt">Type</span><span class="p">[</span><span class="na">@ArrayType</span><span class="err">=</span><span class="nf">true</span><span class="p">()]</span><span class="o">/</span><span class="nt">ReferenceType</span><span class="p">[</span><span class="nt">PrimitiveType</span><span class="p">[</span><span class="na">@Image</span><span class="err">=</span><span class="s1">'byte'</span><span class="p">]])]</span><span class="w">
</span><span class="p">[</span><span class="ow">not</span><span class="w"> </span><span class="p">(.</span><span class="o">/</span><span class="nt">Type</span><span class="o">/</span><span class="nt">ReferenceType</span><span class="p">[</span><span class="nt">ClassOrInterfaceType</span><span class="p">[</span><span class="na">@Image</span><span class="err">=</span><span class="s1">'Byte'</span><span class="p">]])]</span><span class="w">
</span><span class="p">[</span><span class="ow">not</span><span class="w"> </span><span class="p">(.</span><span class="o">/</span><span class="nt">Type</span><span class="o">/</span><span class="nt">PrimitiveType</span><span class="p">[</span><span class="na">@Image</span><span class="err">=</span><span class="s1">'byte'</span><span class="p">])]</span><span class="w">
</span><span class="p">[</span><span class="ow">not</span><span class="w"> </span><span class="p">(</span><span class="kn">ancestor::</span><span class="nt">MethodDeclaration</span><span class="o">/</span><span class="kn">preceding-sibling::</span><span class="nt">Annotation</span><span class="err">/*</span><span class="o">/</span><span class="nt">Name</span><span class="p">[</span><span class="na">@Image</span><span class="err">=</span><span class="s1">'Override'</span><span class="p">])]</span><span class="w">
</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="w">
</span><span class="kn">ancestor::</span><span class="nt">MethodDeclaration</span><span class="w">
</span><span class="p">[</span><span class="w"> </span><span class="na">@Public</span><span class="err">=</span><span class="nf">true</span><span class="p">()</span><span class="w">
</span><span class="ow">and</span><span class="w"> </span><span class="na">@Static</span><span class="err">=</span><span class="nf">true</span><span class="p">()</span><span class="w">
</span><span class="ow">and</span><span class="w"> </span><span class="kn">child::</span><span class="nt">ResultType</span><span class="p">[</span><span class="na">@Void</span><span class="err">=</span><span class="nf">true</span><span class="p">()]</span><span class="w">
</span><span class="ow">and</span><span class="w"> </span><span class="na">@Name</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'main'</span><span class="w">
</span><span class="ow">and</span><span class="w"> </span><span class="na">@Arity</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="m">1</span><span class="w">
</span><span class="p">]</span><span class="w">
</span><span class="c">(: Type of the formal parameter here. :)</span><span class="w">
</span><span class="ow">and</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'java.lang.String[]'</span><span class="p">)</span><span class="w">
</span><span class="p">)]</span><span class="w">
</span></code></pre></div></div>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">foo</span><span class="o">(</span><span class="nc">String</span> <span class="n">s</span><span class="o">,</span> <span class="nc">Object</span><span class="o">[]</span> <span class="n">args</span><span class="o">)</span> <span class="o">{</span>
<span class="c1">// Do something here...</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">bar</span><span class="o">(</span><span class="nc">String</span> <span class="n">s</span><span class="o">,</span> <span class="nc">Object</span><span class="o">...</span> <span class="n">args</span><span class="o">)</span> <span class="o">{</span>
<span class="c1">// Ahh, varargs tastes much better...</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>Use this rule by referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/UseVarargs"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="whileloopwithliteralboolean">WhileLoopWithLiteralBoolean</h2>
<p><strong>Since:</strong> PMD 6.13.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p><code class="language-plaintext highlighter-rouge">do {} while (true);</code> requires reading the end of the statement before it is
apparent that it loops forever, whereas <code class="language-plaintext highlighter-rouge">while (true) {}</code> is easier to understand.</p>
<p><code class="language-plaintext highlighter-rouge">do {} while (false);</code> is redundant, and if an inner variable scope is required,
a block <code class="language-plaintext highlighter-rouge">{}</code> is sufficient.</p>
<p><code class="language-plaintext highlighter-rouge">while (false) {}</code> will never execute the block and can be removed in its entirety.</p>
<p><strong>This rule is defined by the following XPath expression:</strong></p>
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">DoStatement</span><span class="p">[</span><span class="nt">Expression</span><span class="o">/</span><span class="nt">PrimaryExpression</span><span class="o">/</span><span class="nt">PrimaryPrefix</span><span class="o">/</span><span class="nt">Literal</span><span class="o">/</span><span class="nt">BooleanLiteral</span><span class="p">]</span><span class="w"> </span><span class="p">|</span><span class="w">
</span><span class="o">//</span><span class="nt">WhileStatement</span><span class="p">[</span><span class="nt">Expression</span><span class="o">/</span><span class="nt">PrimaryExpression</span><span class="o">/</span><span class="nt">PrimaryPrefix</span><span class="o">/</span><span class="nt">Literal</span><span class="o">/</span><span class="nt">BooleanLiteral</span><span class="p">[</span><span class="na">@True</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="nf">false</span><span class="p">()]]</span><span class="w">
</span></code></pre></div></div>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Example</span> <span class="o">{</span>
<span class="o">{</span>
<span class="k">while</span> <span class="o">(</span><span class="kc">true</span><span class="o">)</span> <span class="o">{</span> <span class="o">}</span> <span class="c1">// allowed</span>
<span class="k">while</span> <span class="o">(</span><span class="kc">false</span><span class="o">)</span> <span class="o">{</span> <span class="o">}</span> <span class="c1">// disallowed</span>
<span class="k">do</span> <span class="o">{</span> <span class="o">}</span> <span class="k">while</span> <span class="o">(</span><span class="kc">true</span><span class="o">);</span> <span class="c1">// disallowed</span>
<span class="k">do</span> <span class="o">{</span> <span class="o">}</span> <span class="k">while</span> <span class="o">(</span><span class="kc">false</span><span class="o">);</span> <span class="c1">// disallowed</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>Use this rule by referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/WhileLoopWithLiteralBoolean"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<div class="tags">
</div>
</div>
<hr class="shaded"/>
<footer>
<div class="row">
<div class="col-lg-12 footer">
&copy;2021 PMD Open Source Project. All rights reserved. <br />
Site last generated: Aug 28, 2021 <br />
<p><img src="images/pmd-logo-small.png" alt="Company logo"/></p>
</div>
</div>
</footer>
</div>
<!-- /.row -->
</div>
<!-- /.container -->
</div>
</div>
</body>
</html>