pmd/pmd_rules_java_errorprone.html
PMD CI (pmd-bot) c4fd3f862f Update documentation
2020-11-12 18:11:14 +00:00

5278 lines
377 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 to detect constructs that are either broken, extremely confusing or prone to runtime errors.">
<meta name="keywords" content=" Error Prone, AssignmentInOperand, AssignmentToNonFinalStatic, AvoidAccessibilityAlteration, AvoidAssertAsIdentifier, AvoidBranchingStatementAsLastInLoop, AvoidCallingFinalize, AvoidCatchingNPE, AvoidCatchingThrowable, AvoidDecimalLiteralsInBigDecimalConstructor, AvoidDuplicateLiterals, AvoidEnumAsIdentifier, AvoidFieldNameMatchingMethodName, AvoidFieldNameMatchingTypeName, AvoidInstanceofChecksInCatchClause, AvoidLiteralsInIfCondition, AvoidLosingExceptionInformation, AvoidMultipleUnaryOperators, AvoidUsingOctalValues, BadComparison, BeanMembersShouldSerialize, BrokenNullCheck, CallSuperFirst, CallSuperLast, CheckSkipResult, ClassCastExceptionWithToArray, CloneMethodMustBePublic, CloneMethodMustImplementCloneable, CloneMethodReturnTypeMustMatchClassName, CloneThrowsCloneNotSupportedException, CloseResource, CompareObjectsWithEquals, ConstructorCallsOverridableMethod, DataflowAnomalyAnalysis, DetachedTestCase, DoNotCallGarbageCollectionExplicitly, DoNotCallSystemExit, DoNotExtendJavaLangThrowable, DoNotHardCodeSDCard, DoNotTerminateVM, DoNotThrowExceptionInFinally, DontImportSun, DontUseFloatTypeForLoopIndices, EmptyCatchBlock, EmptyFinalizer, EmptyFinallyBlock, EmptyIfStmt, EmptyInitializer, EmptyStatementBlock, EmptyStatementNotInLoop, EmptySwitchStatements, EmptySynchronizedBlock, EmptyTryBlock, EmptyWhileStmt, EqualsNull, FinalizeDoesNotCallSuperFinalize, FinalizeOnlyCallsSuperFinalize, FinalizeOverloaded, FinalizeShouldBeProtected, IdempotentOperations, ImportFromSamePackage, InstantiationToGetClass, InvalidSlf4jMessageFormat, InvalidLogMessageFormat, JumbledIncrementer, JUnitSpelling, JUnitStaticSuite, LoggerIsNotStaticFinal, MethodWithSameNameAsEnclosingClass, MisplacedNullCheck, MissingBreakInSwitch, MissingSerialVersionUID, MissingStaticMethodInNonInstantiatableClass, MoreThanOneLogger, NonCaseLabelInSwitchStatement, NonStaticInitializer, NullAssignment, OverrideBothEqualsAndHashcode, ProperCloneImplementation, ProperLogger, ReturnEmptyArrayRatherThanNull, ReturnFromFinallyBlock, SimpleDateFormatNeedsLocale, SingleMethodSingleton, SingletonClassReturningNewInstance, StaticEJBFieldShouldBeFinal, StringBufferInstantiationWithChar, SuspiciousEqualsMethodName, SuspiciousHashcodeMethodName, SuspiciousOctalEscape, TestClassWithoutTestCases, UnconditionalIfStatement, UnnecessaryBooleanAssertion, UnnecessaryCaseChange, UnnecessaryConversionTemporary, UnusedNullCheckInEquals, UseCorrectExceptionLogging, UseEqualsToCompareStrings, UselessOperationOnImmutable, UseLocaleWithCaseConversions, UseProperClassLoader">
<title>Error Prone | 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="Error Prone">{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.30.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><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 class="active"><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>
</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_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_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">Error Prone</h1>
</div>
<div class="post-content">
<div class="summary">Rules to detect constructs that are either broken, extremely confusing or prone to runtime errors.</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/errorprone.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/errorprone.xml. -->
<h2 id="assignmentinoperand">AssignmentInOperand</h2>
<p><strong>Since:</strong> PMD 1.03</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Avoid assignments in operands; this can make code more complicated and harder to read.</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/errorprone/AssignmentInOperandRule.java">net.sourceforge.pmd.lang.java.rule.errorprone.AssignmentInOperandRule</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="kt">void</span> <span class="nf">bar</span><span class="o">()</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">if</span> <span class="o">((</span><span class="n">x</span> <span class="o">=</span> <span class="n">getX</span><span class="o">())</span> <span class="o">==</span> <span class="mi">3</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">"3!"</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>allowIf</td>
<td>false</td>
<td>Allow assignment within the conditional expression of an if statement</td>
<td>no</td>
</tr>
<tr>
<td>allowFor</td>
<td>false</td>
<td>Allow assignment within the conditional expression of a for statement</td>
<td>no</td>
</tr>
<tr>
<td>allowWhile</td>
<td>false</td>
<td>Allow assignment within the conditional expression of a while statement</td>
<td>no</td>
</tr>
<tr>
<td>allowIncrementDecrement</td>
<td>false</td>
<td>Allow increment or decrement operators within the conditional expression of an if, for, or while statement</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/errorprone.xml/AssignmentInOperand"</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/errorprone.xml/AssignmentInOperand"</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">"allowIf"</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">"allowFor"</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">"allowWhile"</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">"allowIncrementDecrement"</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="assignmenttononfinalstatic">AssignmentToNonFinalStatic</h2>
<p><strong>Since:</strong> PMD 2.2</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Identifies a possible unsafe usage of a static field.</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/errorprone/AssignmentToNonFinalStaticRule.java">net.sourceforge.pmd.lang.java.rule.errorprone.AssignmentToNonFinalStaticRule</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">StaticField</span> <span class="o">{</span>
<span class="kd">static</span> <span class="kt">int</span> <span class="n">x</span><span class="o">;</span>
<span class="kd">public</span> <span class="nf">FinalFields</span><span class="o">(</span><span class="kt">int</span> <span class="n">y</span><span class="o">)</span> <span class="o">{</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">y</span><span class="o">;</span> <span class="c1">// unsafe</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/errorprone.xml/AssignmentToNonFinalStatic"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="avoidaccessibilityalteration">AvoidAccessibilityAlteration</h2>
<p><strong>Since:</strong> PMD 4.1</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Methods such as getDeclaredConstructors(), getDeclaredConstructor(Class[]) and setAccessible(),
as the interface PrivilegedAction, allow for the runtime alteration of variable, class, or
method visibility, even if they are private. This violates the principle of encapsulation.</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="p">(</span><span class="nt">PrimarySuffix</span><span class="p">[</span><span class="w">
</span><span class="nf">ends-with</span><span class="p">(</span><span class="na">@Image</span><span class="p">,</span><span class="s1">'getDeclaredConstructors'</span><span class="p">)</span><span class="w">
</span><span class="ow">or</span><span class="w">
</span><span class="nf">ends-with</span><span class="p">(</span><span class="na">@Image</span><span class="p">,</span><span class="s1">'getDeclaredConstructor'</span><span class="p">)</span><span class="w">
</span><span class="ow">or</span><span class="w">
</span><span class="nf">ends-with</span><span class="p">(</span><span class="na">@Image</span><span class="p">,</span><span class="s1">'setAccessible'</span><span class="p">)</span><span class="w">
</span><span class="p">])</span><span class="w">
</span><span class="ow">or</span><span class="w">
</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="w">
</span><span class="nf">ends-with</span><span class="p">(</span><span class="na">@Image</span><span class="p">,</span><span class="s1">'getDeclaredConstructor'</span><span class="p">)</span><span class="w">
</span><span class="ow">or</span><span class="w">
</span><span class="nf">ends-with</span><span class="p">(</span><span class="na">@Image</span><span class="p">,</span><span class="s1">'getDeclaredConstructors'</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="s1">'AccessibleObject.setAccessible'</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="ow">and</span><span class="w">
</span><span class="p">(</span><span class="o">//</span><span class="nt">ImportDeclaration</span><span class="o">/</span><span class="nt">Name</span><span class="p">[</span><span class="w">
</span><span class="nf">contains</span><span class="p">(</span><span class="na">@Image</span><span class="p">,</span><span class="s1">'java.security.PrivilegedAction'</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="kn">import</span> <span class="nn">java.lang.reflect.AccessibleObject</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">java.lang.reflect.Method</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">java.security.PrivilegedAction</span><span class="o">;</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">Violation</span> <span class="o">{</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">invalidCallsInMethod</span><span class="o">()</span> <span class="kd">throws</span> <span class="nc">SecurityException</span><span class="o">,</span> <span class="nc">NoSuchMethodException</span> <span class="o">{</span>
<span class="c1">// Possible call to forbidden getDeclaredConstructors</span>
<span class="nc">Class</span><span class="o">[]</span> <span class="n">arrayOfClass</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">Class</span><span class="o">[</span><span class="mi">1</span><span class="o">];</span>
<span class="k">this</span><span class="o">.</span><span class="na">getClass</span><span class="o">().</span><span class="na">getDeclaredConstructors</span><span class="o">();</span>
<span class="k">this</span><span class="o">.</span><span class="na">getClass</span><span class="o">().</span><span class="na">getDeclaredConstructor</span><span class="o">(</span><span class="n">arrayOfClass</span><span class="o">);</span>
<span class="nc">Class</span> <span class="n">clazz</span> <span class="o">=</span> <span class="k">this</span><span class="o">.</span><span class="na">getClass</span><span class="o">();</span>
<span class="n">clazz</span><span class="o">.</span><span class="na">getDeclaredConstructor</span><span class="o">(</span><span class="n">arrayOfClass</span><span class="o">);</span>
<span class="n">clazz</span><span class="o">.</span><span class="na">getDeclaredConstructors</span><span class="o">();</span>
<span class="c1">// Possible call to forbidden setAccessible</span>
<span class="n">clazz</span><span class="o">.</span><span class="na">getMethod</span><span class="o">(</span><span class="s">""</span><span class="o">,</span> <span class="n">arrayOfClass</span><span class="o">).</span><span class="na">setAccessible</span><span class="o">(</span><span class="kc">false</span><span class="o">);</span>
<span class="nc">AccessibleObject</span><span class="o">.</span><span class="na">setAccessible</span><span class="o">(</span><span class="kc">null</span><span class="o">,</span> <span class="kc">false</span><span class="o">);</span>
<span class="nc">Method</span><span class="o">.</span><span class="na">setAccessible</span><span class="o">(</span><span class="kc">null</span><span class="o">,</span> <span class="kc">false</span><span class="o">);</span>
<span class="nc">Method</span><span class="o">[]</span> <span class="n">methodsArray</span> <span class="o">=</span> <span class="n">clazz</span><span class="o">.</span><span class="na">getMethods</span><span class="o">();</span>
<span class="kt">int</span> <span class="n">nbMethod</span><span class="o">;</span>
<span class="k">for</span> <span class="o">(</span> <span class="n">nbMethod</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span> <span class="n">nbMethod</span> <span class="o">&lt;</span> <span class="n">methodsArray</span><span class="o">.</span><span class="na">length</span><span class="o">;</span> <span class="n">nbMethod</span><span class="o">++</span> <span class="o">)</span> <span class="o">{</span>
<span class="n">methodsArray</span><span class="o">[</span><span class="n">nbMethod</span><span class="o">].</span><span class="na">setAccessible</span><span class="o">(</span><span class="kc">false</span><span class="o">);</span>
<span class="o">}</span>
<span class="c1">// Possible call to forbidden PrivilegedAction</span>
<span class="nc">PrivilegedAction</span> <span class="n">priv</span> <span class="o">=</span> <span class="o">(</span><span class="nc">PrivilegedAction</span><span class="o">)</span> <span class="k">new</span> <span class="nc">Object</span><span class="o">();</span> <span class="n">priv</span><span class="o">.</span><span class="na">run</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/errorprone.xml/AvoidAccessibilityAlteration"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="avoidassertasidentifier">AvoidAssertAsIdentifier</h2>
<p><strong>Since:</strong> PMD 3.4</p>
<p><strong>Priority:</strong> Medium High (2)</p>
<p>Use of the term assert will conflict with newer versions of Java since it is a reserved word.</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">VariableDeclaratorId</span><span class="p">[</span><span class="na">@Name</span><span class="err">=</span><span class="s1">'assert'</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">A</span> <span class="o">{</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
<span class="nc">String</span> <span class="k">assert</span> <span class="o">=</span> <span class="s">"foo"</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/errorprone.xml/AvoidAssertAsIdentifier"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="avoidbranchingstatementaslastinloop">AvoidBranchingStatementAsLastInLoop</h2>
<p><strong>Since:</strong> PMD 5.0</p>
<p><strong>Priority:</strong> Medium High (2)</p>
<p>Using a branching statement as the last part of a loop may be a bug, and/or is confusing.
Ensure that the usage is not a bug, or consider using another approach.</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/errorprone/AvoidBranchingStatementAsLastInLoopRule.java">net.sourceforge.pmd.lang.java.rule.errorprone.AvoidBranchingStatementAsLastInLoopRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">// unusual use of branching statement in a 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">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">i</span><span class="o">*</span><span class="n">i</span> <span class="o">&lt;=</span> <span class="mi">25</span><span class="o">)</span> <span class="o">{</span>
<span class="k">continue</span><span class="o">;</span>
<span class="o">}</span>
<span class="k">break</span><span class="o">;</span>
<span class="o">}</span>
<span class="c1">// this makes more sense...</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">i</span><span class="o">*</span><span class="n">i</span> <span class="o">&gt;</span> <span class="mi">25</span><span class="o">)</span> <span class="o">{</span>
<span class="k">break</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>checkBreakLoopTypes</td>
<td>for | do | while</td>
<td>List of loop types in which break statements will be checked</td>
<td>yes. Delimiter is |.</td>
</tr>
<tr>
<td>checkContinueLoopTypes</td>
<td>for | do | while</td>
<td>List of loop types in which continue statements will be checked</td>
<td>yes. Delimiter is |.</td>
</tr>
<tr>
<td>checkReturnLoopTypes</td>
<td>for | do | while</td>
<td>List of loop types in which return statements will be checked</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/errorprone.xml/AvoidBranchingStatementAsLastInLoop"</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/errorprone.xml/AvoidBranchingStatementAsLastInLoop"</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">"checkBreakLoopTypes"</span> <span class="na">value=</span><span class="s">"for|do|while"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"checkContinueLoopTypes"</span> <span class="na">value=</span><span class="s">"for|do|while"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"checkReturnLoopTypes"</span> <span class="na">value=</span><span class="s">"for|do|while"</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="avoidcallingfinalize">AvoidCallingFinalize</h2>
<p><strong>Since:</strong> PMD 3.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>The method Object.finalize() is called by the garbage collector on an object when garbage collection determines
that there are no more references to the object. It should not be invoked by application logic.</p>
<p>Note that Oracle has declared Object.finalize() as deprecated since JDK 9.</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/errorprone/AvoidCallingFinalizeRule.java">net.sourceforge.pmd.lang.java.rule.errorprone.AvoidCallingFinalizeRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">void</span> <span class="nf">foo</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="k">new</span> <span class="nc">Bar</span><span class="o">();</span>
<span class="n">b</span><span class="o">.</span><span class="na">finalize</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/errorprone.xml/AvoidCallingFinalize"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="avoidcatchingnpe">AvoidCatchingNPE</h2>
<p><strong>Since:</strong> PMD 1.8</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Code should never throw NullPointerExceptions under normal circumstances. A catch block may hide the
original error, causing other, more subtle problems later on.</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">CatchStatement</span><span class="o">/</span><span class="nt">FormalParameter</span><span class="o">/</span><span class="nt">Type</span><span class="w">
</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">'NullPointerException'</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="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">NullPointerException</span> <span class="n">npe</span><span class="o">)</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/errorprone.xml/AvoidCatchingNPE"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="avoidcatchingthrowable">AvoidCatchingThrowable</h2>
<p><strong>Since:</strong> PMD 1.2</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Catching Throwable errors is not recommended since its scope is very broad. It includes runtime issues such as
OutOfMemoryError that should be exposed and managed separately.</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/errorprone/AvoidCatchingThrowableRule.java">net.sourceforge.pmd.lang.java.rule.errorprone.AvoidCatchingThrowableRule</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="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">Throwable</span> <span class="n">th</span><span class="o">)</span> <span class="o">{</span> <span class="c1">// should not catch Throwable</span>
<span class="n">th</span><span class="o">.</span><span class="na">printStackTrace</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/errorprone.xml/AvoidCatchingThrowable"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="avoiddecimalliteralsinbigdecimalconstructor">AvoidDecimalLiteralsInBigDecimalConstructor</h2>
<p><strong>Since:</strong> PMD 3.4</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>One might assume that the result of "new BigDecimal(0.1)" is exactly equal to 0.1, but it is actually
equal to .1000000000000000055511151231257827021181583404541015625.
This is because 0.1 cannot be represented exactly as a double (or as a binary fraction of any finite
length). Thus, the long value that is being passed in to the constructor is not exactly equal to 0.1,
appearances notwithstanding.</p>
<p>The (String) constructor, on the other hand, is perfectly predictable: new BigDecimal("0.1") is
exactly equal to 0.1, as one would expect. Therefore, it is generally recommended that the
(String) constructor be used in preference to this one.</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="p">[</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'java.math.BigDecimal'</span><span class="p">)]</span><span class="w">
</span><span class="p">[</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="w">
</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">'float'</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">'java.lang.Float'</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">'double'</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">'java.lang.Double'</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></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">BigDecimal</span> <span class="n">bd</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">BigDecimal</span><span class="o">(</span><span class="mf">1.123</span><span class="o">);</span> <span class="c1">// loss of precision, this would trigger the rule</span>
<span class="nc">BigDecimal</span> <span class="n">bd</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">BigDecimal</span><span class="o">(</span><span class="s">"1.123"</span><span class="o">);</span> <span class="c1">// preferred approach</span>
<span class="nc">BigDecimal</span> <span class="n">bd</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">BigDecimal</span><span class="o">(</span><span class="mi">12</span><span class="o">);</span> <span class="c1">// preferred approach, ok for integer values</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/errorprone.xml/AvoidDecimalLiteralsInBigDecimalConstructor"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="avoidduplicateliterals">AvoidDuplicateLiterals</h2>
<p><strong>Since:</strong> PMD 1.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Code containing duplicate String literals can usually be improved by declaring the String as a constant field.</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/errorprone/AvoidDuplicateLiteralsRule.java">net.sourceforge.pmd.lang.java.rule.errorprone.AvoidDuplicateLiteralsRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">private</span> <span class="kt">void</span> <span class="nf">bar</span><span class="o">()</span> <span class="o">{</span>
<span class="n">buz</span><span class="o">(</span><span class="s">"Howdy"</span><span class="o">);</span>
<span class="n">buz</span><span class="o">(</span><span class="s">"Howdy"</span><span class="o">);</span>
<span class="n">buz</span><span class="o">(</span><span class="s">"Howdy"</span><span class="o">);</span>
<span class="n">buz</span><span class="o">(</span><span class="s">"Howdy"</span><span class="o">);</span>
<span class="o">}</span>
<span class="kd">private</span> <span class="kt">void</span> <span class="nf">buz</span><span class="o">(</span><span class="nc">String</span> <span class="n">x</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>exceptionfile</td>
<td> </td>
<td><span style="border-radius: 0.25em; color: #fff; padding: 0.2em 0.6em 0.3em; display: inline; background-color: #d9534f; font-size: 75%;">Deprecated</span> (Use exceptionList property) File containing strings to skip (one string per line), only used if ignore list is not set. File must be UTF-8 encoded.</td>
<td>no</td>
</tr>
<tr>
<td>separator</td>
<td>,</td>
<td>Ignore list separator</td>
<td>no</td>
</tr>
<tr>
<td>maxDuplicateLiterals</td>
<td>4</td>
<td>Max duplicate literals</td>
<td>no</td>
</tr>
<tr>
<td>minimumLength</td>
<td>3</td>
<td>Minimum string length to check</td>
<td>no</td>
</tr>
<tr>
<td>skipAnnotations</td>
<td>false</td>
<td>Skip literals within annotations</td>
<td>no</td>
</tr>
<tr>
<td>exceptionList</td>
<td> </td>
<td>List of literals to ignore. A literal is ignored if its image can be found in this list. Components of this list should not be surrounded by double quotes.</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/errorprone.xml/AvoidDuplicateLiterals"</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/errorprone.xml/AvoidDuplicateLiterals"</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">"separator"</span> <span class="na">value=</span><span class="s">","</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"maxDuplicateLiterals"</span> <span class="na">value=</span><span class="s">"4"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"minimumLength"</span> <span class="na">value=</span><span class="s">"3"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"skipAnnotations"</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">"exceptionList"</span> <span class="na">value=</span><span class="s">""</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="avoidenumasidentifier">AvoidEnumAsIdentifier</h2>
<p><strong>Since:</strong> PMD 3.4</p>
<p><strong>Priority:</strong> Medium High (2)</p>
<p>Use of the term enum will conflict with newer versions of Java since it is a reserved word.</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">VariableDeclaratorId</span><span class="p">[</span><span class="na">@Name</span><span class="err">=</span><span class="s1">'enum'</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">A</span> <span class="o">{</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
<span class="nc">String</span> <span class="kd">enum</span> <span class="o">=</span> <span class="s">"foo"</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/errorprone.xml/AvoidEnumAsIdentifier"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="avoidfieldnamematchingmethodname">AvoidFieldNameMatchingMethodName</h2>
<p><strong>Since:</strong> PMD 3.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>It can be confusing to have a field name with the same name as a method. While this is permitted,
having information (field) and actions (method) is not clear naming. Developers versed in
Smalltalk often prefer this approach as the methods denote accessor methods.</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/errorprone/AvoidFieldNameMatchingMethodNameRule.java">net.sourceforge.pmd.lang.java.rule.errorprone.AvoidFieldNameMatchingMethodNameRule</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="nc">Object</span> <span class="n">bar</span><span class="o">;</span>
<span class="c1">// bar is data or an action or both?</span>
<span class="kt">void</span> <span class="nf">bar</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/errorprone.xml/AvoidFieldNameMatchingMethodName"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="avoidfieldnamematchingtypename">AvoidFieldNameMatchingTypeName</h2>
<p><strong>Since:</strong> PMD 3.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>It is somewhat confusing to have a field name matching the declaring class name.
This probably means that type and/or field names should be chosen more carefully.</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/errorprone/AvoidFieldNameMatchingTypeNameRule.java">net.sourceforge.pmd.lang.java.rule.errorprone.AvoidFieldNameMatchingTypeNameRule</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">Bar</span> <span class="o">{</span>
<span class="kt">int</span> <span class="n">foo</span><span class="o">;</span> <span class="c1">// There is probably a better name that can be used</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/errorprone.xml/AvoidFieldNameMatchingTypeName"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="avoidinstanceofchecksincatchclause">AvoidInstanceofChecksInCatchClause</h2>
<p><strong>Since:</strong> PMD 3.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Each caught exception type should be handled in its own catch clause.</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">CatchStatement</span><span class="o">/</span><span class="nt">FormalParameter</span><span class="w">
</span><span class="o">/</span><span class="kn">following-sibling::</span><span class="nt">Block</span><span class="o">//</span><span class="nt">InstanceOfExpression</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="w">
</span><span class="na">@Image</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="p">.</span><span class="o">/</span><span class="kn">ancestor::</span><span class="nt">Block</span><span class="o">/</span><span class="kn">preceding-sibling::</span><span class="nt">FormalParameter</span><span class="w">
</span><span class="o">/</span><span class="nt">VariableDeclaratorId</span><span class="err">/</span><span class="na">@Name</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="k">try</span> <span class="o">{</span> <span class="c1">// Avoid this</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">ee</span><span class="o">)</span> <span class="o">{</span>
<span class="k">if</span> <span class="o">(</span><span class="n">ee</span> <span class="k">instanceof</span> <span class="nc">IOException</span><span class="o">)</span> <span class="o">{</span>
<span class="n">cleanup</span><span class="o">();</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="k">try</span> <span class="o">{</span> <span class="c1">// Prefer this:</span>
<span class="c1">// do something</span>
<span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="nc">IOException</span> <span class="n">ee</span><span class="o">)</span> <span class="o">{</span>
<span class="n">cleanup</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/errorprone.xml/AvoidInstanceofChecksInCatchClause"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="avoidliteralsinifcondition">AvoidLiteralsInIfCondition</h2>
<p><strong>Since:</strong> PMD 4.2.6</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Avoid using hard-coded literals in conditional statements. By declaring them as static variables
or private members with descriptive names maintainability is enhanced. By default, the literals "-1" and "0" are ignored.
More exceptions can be defined with the property "ignoreMagicNumbers".</p>
<p>The rule doesnt consider deeper expressions by default, but this can be enabled via the property <code class="language-plaintext highlighter-rouge">ignoreExpressions</code>.
With this property set to false, if-conditions like <code class="language-plaintext highlighter-rouge">i == 1 + 5</code> are reported as well. Note that in that case,
the property ignoreMagicNumbers is not taken into account, if there are multiple literals involved in such an expression.</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="c">(: simple case - no deep expressions :)</span><span class="w">
</span><span class="o">//</span><span class="nt">IfStatement</span><span class="p">[</span><span class="nv">$ignoreExpressions</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="nf">true</span><span class="p">()]</span><span class="o">/</span><span class="nt">Expression</span><span class="err">/*</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="w">
</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="nt">NullLiteral</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">BooleanLiteral</span><span class="p">)]</span><span class="w">
</span><span class="p">[</span><span class="nf">empty</span><span class="p">(</span><span class="kr">in</span><span class="nf">dex-of</span><span class="p">(</span><span class="kr">to</span><span class="nf">kenize</span><span class="p">(</span><span class="nv">$ignoreMagicNumbers</span><span class="p">,</span><span class="w"> </span><span class="s1">'\s*,\s*'</span><span class="p">),</span><span class="w"> </span><span class="na">@Image</span><span class="p">))]</span><span class="w">
</span><span class="p">|</span><span class="w">
</span><span class="c">(: consider also deeper expressions :)</span><span class="w">
</span><span class="o">//</span><span class="nt">IfStatement</span><span class="p">[</span><span class="nv">$ignoreExpressions</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="nf">false</span><span class="p">()]</span><span class="o">/</span><span class="nt">Expression</span><span class="err">//</span><span class="o">*</span><span class="p">[</span><span class="nf">local-name</span><span class="p">()</span><span class="w"> </span><span class="p">!</span><span class="err">=</span><span class="w"> </span><span class="s1">'UnaryExpression'</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="na">@Operator</span><span class="w"> </span><span class="p">!</span><span class="err">=</span><span class="w"> </span><span class="s1">'-'</span><span class="p">]</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="w">
</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="nt">NullLiteral</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">BooleanLiteral</span><span class="p">)]</span><span class="w">
</span><span class="p">[</span><span class="nf">empty</span><span class="p">(</span><span class="kr">in</span><span class="nf">dex-of</span><span class="p">(</span><span class="kr">to</span><span class="nf">kenize</span><span class="p">(</span><span class="nv">$ignoreMagicNumbers</span><span class="p">,</span><span class="w"> </span><span class="s1">'\s*,\s*'</span><span class="p">),</span><span class="w"> </span><span class="na">@Image</span><span class="p">))]</span><span class="w">
</span><span class="p">|</span><span class="w">
</span><span class="c">(: consider negative literals :)</span><span class="w">
</span><span class="o">//</span><span class="nt">IfStatement</span><span class="p">[</span><span class="nv">$ignoreExpressions</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="nf">false</span><span class="p">()]</span><span class="o">/</span><span class="nt">Expression</span><span class="o">//</span><span class="nt">UnaryExpression</span><span class="p">[</span><span class="na">@Operator</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'-'</span><span class="p">]</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="w">
</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="nt">NullLiteral</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">BooleanLiteral</span><span class="p">)]</span><span class="w">
</span><span class="p">[</span><span class="nf">empty</span><span class="p">(</span><span class="kr">in</span><span class="nf">dex-of</span><span class="p">(</span><span class="kr">to</span><span class="nf">kenize</span><span class="p">(</span><span class="nv">$ignoreMagicNumbers</span><span class="p">,</span><span class="w"> </span><span class="s1">'\s*,\s*'</span><span class="p">),</span><span class="w"> </span><span class="nf">concat</span><span class="p">(</span><span class="s1">'-'</span><span class="p">,</span><span class="w"> </span><span class="na">@Image</span><span class="p">)))]</span><span class="w">
</span><span class="p">|</span><span class="w">
</span><span class="c">(: consider multiple literals in expressions :)</span><span class="w">
</span><span class="o">//</span><span class="nt">IfStatement</span><span class="p">[</span><span class="nv">$ignoreExpressions</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="nf">false</span><span class="p">()]</span><span class="o">/</span><span class="nt">Expression</span><span class="p">[</span><span class="nf">count</span><span class="p">(</span><span class="err">*</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="w">
</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="nt">NullLiteral</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">BooleanLiteral</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></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">private</span> <span class="kd">static</span> <span class="kd">final</span> <span class="kt">int</span> <span class="no">MAX_NUMBER_OF_REQUESTS</span> <span class="o">=</span> <span class="mi">10</span><span class="o">;</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">checkRequests</span><span class="o">()</span> <span class="o">{</span>
<span class="k">if</span> <span class="o">(</span><span class="n">i</span> <span class="o">==</span> <span class="mi">10</span><span class="o">)</span> <span class="o">{</span> <span class="c1">// magic number, buried in a method</span>
<span class="n">doSomething</span><span class="o">();</span>
<span class="o">}</span>
<span class="k">if</span> <span class="o">(</span><span class="n">i</span> <span class="o">==</span> <span class="no">MAX_NUMBER_OF_REQUESTS</span><span class="o">)</span> <span class="o">{</span> <span class="c1">// preferred approach</span>
<span class="n">doSomething</span><span class="o">();</span>
<span class="o">}</span>
<span class="k">if</span> <span class="o">(</span><span class="n">aString</span><span class="o">.</span><span class="na">indexOf</span><span class="o">(</span><span class="sc">'.'</span><span class="o">)</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span><span class="o">)</span> <span class="o">{}</span> <span class="c1">// magic number -1, by default ignored</span>
<span class="k">if</span> <span class="o">(</span><span class="n">aString</span><span class="o">.</span><span class="na">indexOf</span><span class="o">(</span><span class="sc">'.'</span><span class="o">)</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="o">)</span> <span class="o">{</span> <span class="o">}</span> <span class="c1">// alternative approach</span>
<span class="k">if</span> <span class="o">(</span><span class="n">aDouble</span> <span class="o">&gt;</span> <span class="mf">0.0</span><span class="o">)</span> <span class="o">{}</span> <span class="c1">// magic number 0.0</span>
<span class="k">if</span> <span class="o">(</span><span class="n">aDouble</span> <span class="o">&gt;=</span> <span class="nc">Double</span><span class="o">.</span><span class="na">MIN_VALUE</span><span class="o">)</span> <span class="o">{}</span> <span class="c1">// preferred approach</span>
<span class="c1">// with rule property "ignoreExpressions" set to "false"</span>
<span class="k">if</span> <span class="o">(</span><span class="n">i</span> <span class="o">==</span> <span class="n">pos</span> <span class="o">+</span> <span class="mi">5</span><span class="o">)</span> <span class="o">{}</span> <span class="c1">// violation: magic number 5 within an (additive) expression</span>
<span class="k">if</span> <span class="o">(</span><span class="n">i</span> <span class="o">==</span> <span class="n">pos</span> <span class="o">+</span> <span class="no">SUFFIX_LENGTH</span><span class="o">)</span> <span class="o">{}</span> <span class="c1">// preferred approach</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>ignoreMagicNumbers</td>
<td>-1,0</td>
<td>Comma-separated list of magic numbers, that should be ignored</td>
<td>no</td>
</tr>
<tr>
<td>ignoreExpressions</td>
<td>true</td>
<td>If true, only literals in simple if conditions are considered. Otherwise literals in expressions are checked, too.</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/errorprone.xml/AvoidLiteralsInIfCondition"</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/errorprone.xml/AvoidLiteralsInIfCondition"</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">"ignoreMagicNumbers"</span> <span class="na">value=</span><span class="s">"-1,0"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"ignoreExpressions"</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="avoidlosingexceptioninformation">AvoidLosingExceptionInformation</h2>
<p><strong>Since:</strong> PMD 4.2.6</p>
<p><strong>Priority:</strong> Medium High (2)</p>
<p>Statements in a catch block that invoke accessors on the exception without using the information
only add to code size. Either remove the invocation, or use the return result.</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">CatchStatement</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">StatementExpression</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="w">
</span><span class="na">@Image</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="nf">concat</span><span class="p">(..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="o">/</span><span class="nt">FormalParameter</span><span class="o">/</span><span class="nt">VariableDeclaratorId</span><span class="err">/</span><span class="na">@Name</span><span class="p">,</span><span class="w"> </span><span class="s1">'.getMessage'</span><span class="p">)</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="nf">concat</span><span class="p">(..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="o">/</span><span class="nt">FormalParameter</span><span class="o">/</span><span class="nt">VariableDeclaratorId</span><span class="err">/</span><span class="na">@Name</span><span class="p">,</span><span class="w"> </span><span class="s1">'.getLocalizedMessage'</span><span class="p">)</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="nf">concat</span><span class="p">(..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="o">/</span><span class="nt">FormalParameter</span><span class="o">/</span><span class="nt">VariableDeclaratorId</span><span class="err">/</span><span class="na">@Name</span><span class="p">,</span><span class="w"> </span><span class="s1">'.getCause'</span><span class="p">)</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="nf">concat</span><span class="p">(..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="o">/</span><span class="nt">FormalParameter</span><span class="o">/</span><span class="nt">VariableDeclaratorId</span><span class="err">/</span><span class="na">@Name</span><span class="p">,</span><span class="w"> </span><span class="s1">'.getStackTrace'</span><span class="p">)</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="nf">concat</span><span class="p">(..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="o">/</span><span class="nt">FormalParameter</span><span class="o">/</span><span class="nt">VariableDeclaratorId</span><span class="err">/</span><span class="na">@Name</span><span class="p">,</span><span class="w"> </span><span class="s1">'.toString'</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="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">SomeException</span> <span class="n">se</span><span class="o">)</span> <span class="o">{</span>
<span class="n">se</span><span class="o">.</span><span class="na">getMessage</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/errorprone.xml/AvoidLosingExceptionInformation"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="avoidmultipleunaryoperators">AvoidMultipleUnaryOperators</h2>
<p><strong>Since:</strong> PMD 4.2</p>
<p><strong>Priority:</strong> Medium High (2)</p>
<p>The use of multiple unary operators may be problematic, and/or confusing.
Ensure that the intended usage is not a bug, or consider simplifying the expression.</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/errorprone/AvoidMultipleUnaryOperatorsRule.java">net.sourceforge.pmd.lang.java.rule.errorprone.AvoidMultipleUnaryOperatorsRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">// These are typo bugs, or at best needlessly complex and confusing:</span>
<span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="o">-</span> <span class="o">-</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="o">+</span> <span class="o">-</span> <span class="o">+</span><span class="mi">1</span><span class="o">;</span>
<span class="kt">int</span> <span class="n">z</span> <span class="o">=</span> <span class="o">~~</span><span class="mi">2</span><span class="o">;</span>
<span class="kt">boolean</span> <span class="n">b</span> <span class="o">=</span> <span class="o">!!</span><span class="kc">true</span><span class="o">;</span>
<span class="kt">boolean</span> <span class="n">c</span> <span class="o">=</span> <span class="o">!!!</span><span class="kc">true</span><span class="o">;</span>
<span class="c1">// These are better:</span>
<span class="kt">int</span> <span class="n">i</span> <span class="o">=</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="o">-</span><span class="mi">1</span><span class="o">;</span>
<span class="kt">int</span> <span class="n">z</span> <span class="o">=</span> <span class="mi">2</span><span class="o">;</span>
<span class="kt">boolean</span> <span class="n">b</span> <span class="o">=</span> <span class="kc">true</span><span class="o">;</span>
<span class="kt">boolean</span> <span class="n">c</span> <span class="o">=</span> <span class="kc">false</span><span class="o">;</span>
<span class="c1">// And these just make your brain hurt:</span>
<span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="o">~-</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="o">-~</span><span class="mi">7</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/errorprone.xml/AvoidMultipleUnaryOperators"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="avoidusingoctalvalues">AvoidUsingOctalValues</h2>
<p><strong>Since:</strong> PMD 3.9</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Integer literals should not start with zero since this denotes that the rest of literal will be
interpreted as an octal value.</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/errorprone/AvoidUsingOctalValuesRule.java">net.sourceforge.pmd.lang.java.rule.errorprone.AvoidUsingOctalValuesRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mo">012</span><span class="o">;</span> <span class="c1">// set i with 10 not 12</span>
<span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mo">010</span><span class="o">;</span> <span class="c1">// set j with 8 not 10</span>
<span class="n">k</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="c1">// set k with 80 not 120</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>strict</td>
<td>false</td>
<td>Detect violations between 00 and 07</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/errorprone.xml/AvoidUsingOctalValues"</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/errorprone.xml/AvoidUsingOctalValues"</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">"strict"</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="badcomparison">BadComparison</h2>
<p><strong>Since:</strong> PMD 1.8</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Avoid equality comparisons with Double.NaN. Due to the implicit lack of representation
precision when comparing floating point numbers these are likely to cause logic errors.</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">EqualityExpression</span><span class="p">[</span><span class="na">@Image</span><span class="err">=</span><span class="s1">'=='</span><span class="p">]</span><span class="w">
</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="err">=</span><span class="s1">'Double.NaN'</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="na">@Image</span><span class="err">=</span><span class="s1">'Float.NaN'</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="kt">boolean</span> <span class="n">x</span> <span class="o">=</span> <span class="o">(</span><span class="n">y</span> <span class="o">==</span> <span class="nc">Double</span><span class="o">.</span><span class="na">NaN</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/errorprone.xml/BadComparison"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="beanmembersshouldserialize">BeanMembersShouldSerialize</h2>
<p><strong>Since:</strong> PMD 1.1</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>If a class is a bean, or is referenced by a bean directly or indirectly it needs to be serializable.
Member variables need to be marked as transient, static, or have accessor methods in the class. Marking
variables as transient is the safest and easiest modification. Accessor methods should follow the Java
naming conventions, i.e. for a variable named foo, getFoo() and setFoo() accessor methods should be provided.</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/errorprone/BeanMembersShouldSerializeRule.java">net.sourceforge.pmd.lang.java.rule.errorprone.BeanMembersShouldSerializeRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">private</span> <span class="kd">transient</span> <span class="kt">int</span> <span class="n">someFoo</span><span class="o">;</span> <span class="c1">// good, it's transient</span>
<span class="kd">private</span> <span class="kd">static</span> <span class="kt">int</span> <span class="n">otherFoo</span><span class="o">;</span> <span class="c1">// also OK</span>
<span class="kd">private</span> <span class="kt">int</span> <span class="n">moreFoo</span><span class="o">;</span> <span class="c1">// OK, has proper accessors, see below</span>
<span class="kd">private</span> <span class="kt">int</span> <span class="n">badFoo</span><span class="o">;</span> <span class="c1">// bad, should be marked transient</span>
<span class="kd">private</span> <span class="kt">void</span> <span class="nf">setMoreFoo</span><span class="o">(</span><span class="kt">int</span> <span class="n">moreFoo</span><span class="o">){</span>
<span class="k">this</span><span class="o">.</span><span class="na">moreFoo</span> <span class="o">=</span> <span class="n">moreFoo</span><span class="o">;</span>
<span class="o">}</span>
<span class="kd">private</span> <span class="kt">int</span> <span class="nf">getMoreFoo</span><span class="o">(){</span>
<span class="k">return</span> <span class="k">this</span><span class="o">.</span><span class="na">moreFoo</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.Data | lombok.Getter | lombok.Value</td>
<td>Fully qualified names of the annotation types that should be ignored by this rule</td>
<td>yes. Delimiter is |.</td>
</tr>
<tr>
<td>prefix</td>
<td> </td>
<td>A variable prefix to skip, i.e., m_</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/errorprone.xml/BeanMembersShouldSerialize"</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/errorprone.xml/BeanMembersShouldSerialize"</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.Data|lombok.Getter|lombok.Value"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"prefix"</span> <span class="na">value=</span><span class="s">""</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="brokennullcheck">BrokenNullCheck</h2>
<p><strong>Since:</strong> PMD 3.8</p>
<p><strong>Priority:</strong> Medium High (2)</p>
<p>The null check is broken since it will throw a NullPointerException itself.
It is likely that you used || instead of &amp;&amp; or vice versa.</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/errorprone/BrokenNullCheckRule.java">net.sourceforge.pmd.lang.java.rule.errorprone.BrokenNullCheckRule</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="nc">String</span> <span class="nf">bar</span><span class="o">(</span><span class="nc">String</span> <span class="n">string</span><span class="o">)</span> <span class="o">{</span>
<span class="c1">// should be &amp;&amp;</span>
<span class="k">if</span> <span class="o">(</span><span class="n">string</span><span class="o">!=</span><span class="kc">null</span> <span class="o">||</span> <span class="o">!</span><span class="n">string</span><span class="o">.</span><span class="na">equals</span><span class="o">(</span><span class="s">""</span><span class="o">))</span>
<span class="k">return</span> <span class="n">string</span><span class="o">;</span>
<span class="c1">// should be ||</span>
<span class="k">if</span> <span class="o">(</span><span class="n">string</span><span class="o">==</span><span class="kc">null</span> <span class="o">&amp;&amp;</span> <span class="n">string</span><span class="o">.</span><span class="na">equals</span><span class="o">(</span><span class="s">""</span><span class="o">))</span>
<span class="k">return</span> <span class="n">string</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/errorprone.xml/BrokenNullCheck"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="callsuperfirst">CallSuperFirst</h2>
<p><strong>Since:</strong> PMD 4.2.5</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Super should be called at the start of the method</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="w">
</span><span class="na">@Name</span><span class="err">=</span><span class="s1">'onCreate'</span><span class="w"> </span><span class="ow">or</span><span class="w">
</span><span class="na">@Name</span><span class="err">=</span><span class="s1">'onConfigurationChanged'</span><span class="w"> </span><span class="ow">or</span><span class="w">
</span><span class="na">@Name</span><span class="err">=</span><span class="s1">'onPostCreate'</span><span class="w"> </span><span class="ow">or</span><span class="w">
</span><span class="na">@Name</span><span class="err">=</span><span class="s1">'onPostResume'</span><span class="w"> </span><span class="ow">or</span><span class="w">
</span><span class="na">@Name</span><span class="err">=</span><span class="s1">'onRestart'</span><span class="w"> </span><span class="ow">or</span><span class="w">
</span><span class="na">@Name</span><span class="err">=</span><span class="s1">'onRestoreInstanceState'</span><span class="w"> </span><span class="ow">or</span><span class="w">
</span><span class="na">@Name</span><span class="err">=</span><span class="s1">'onResume'</span><span class="w"> </span><span class="ow">or</span><span class="w">
</span><span class="na">@Name</span><span class="err">=</span><span class="s1">'onStart'</span><span class="w">
</span><span class="p">]</span><span class="w">
</span><span class="o">/</span><span class="nt">Block</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="w">
</span><span class="p">(</span><span class="nt">BlockStatement</span><span class="p">[</span><span class="m">1</span><span class="p">]</span><span class="o">/</span><span class="nt">Statement</span><span class="o">/</span><span class="nt">StatementExpression</span><span class="o">/</span><span class="nt">PrimaryExpression</span><span class="p">[.</span><span class="o">/</span><span class="nt">PrimaryPrefix</span><span class="p">[</span><span class="na">@SuperModifier</span><span class="err">=</span><span class="w"> </span><span class="nf">true</span><span class="p">()]]</span><span class="o">/</span><span class="nt">PrimarySuffix</span><span class="p">[</span><span class="na">@Image</span><span class="err">=</span><span class="w"> </span><span class="kn">ancestor::</span><span class="nt">MethodDeclaration</span><span class="err">/</span><span class="na">@Name</span><span class="p">]))]</span><span class="w">
</span><span class="p">[</span><span class="kn">ancestor::</span><span class="nt">ClassOrInterfaceDeclaration</span><span class="p">[</span><span class="nt">ExtendsList</span><span class="o">/</span><span class="nt">ClassOrInterfaceType</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">'android.app.Activity'</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">'android.app.Application'</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">'android.app.Service'</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">DummyActivity</span> <span class="kd">extends</span> <span class="nc">Activity</span> <span class="o">{</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">onCreate</span><span class="o">(</span><span class="nc">Bundle</span> <span class="n">bundle</span><span class="o">)</span> <span class="o">{</span>
<span class="c1">// missing call to super.onCreate(bundle)</span>
<span class="n">foo</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/errorprone.xml/CallSuperFirst"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="callsuperlast">CallSuperLast</h2>
<p><strong>Since:</strong> PMD 4.2.5</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Super should be called at the end of the method</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="w">
</span><span class="na">@Name</span><span class="err">=</span><span class="s1">'finish'</span><span class="w"> </span><span class="ow">or</span><span class="w">
</span><span class="na">@Name</span><span class="err">=</span><span class="s1">'onDestroy'</span><span class="w"> </span><span class="ow">or</span><span class="w">
</span><span class="na">@Name</span><span class="err">=</span><span class="s1">'onPause'</span><span class="w"> </span><span class="ow">or</span><span class="w">
</span><span class="na">@Name</span><span class="err">=</span><span class="s1">'onSaveInstanceState'</span><span class="w"> </span><span class="ow">or</span><span class="w">
</span><span class="na">@Name</span><span class="err">=</span><span class="s1">'onStop'</span><span class="w"> </span><span class="ow">or</span><span class="w">
</span><span class="na">@Name</span><span class="err">=</span><span class="s1">'onTerminate'</span><span class="w">
</span><span class="p">]</span><span class="w">
</span><span class="o">/</span><span class="nt">Block</span><span class="o">/</span><span class="nt">BlockStatement</span><span class="p">[</span><span class="nf">last</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">Statement</span><span class="o">/</span><span class="nt">StatementExpression</span><span class="o">/</span><span class="nt">PrimaryExpression</span><span class="p">[.</span><span class="o">/</span><span class="nt">PrimaryPrefix</span><span class="p">[</span><span class="na">@SuperModifier</span><span class="err">=</span><span class="w"> </span><span class="nf">true</span><span class="p">()]]</span><span class="o">/</span><span class="nt">PrimarySuffix</span><span class="p">[</span><span class="na">@Image</span><span class="err">=</span><span class="w"> </span><span class="kn">ancestor::</span><span class="nt">MethodDeclaration</span><span class="err">/</span><span class="na">@Name</span><span class="p">])]</span><span class="w">
</span><span class="p">[</span><span class="kn">ancestor::</span><span class="nt">ClassOrInterfaceDeclaration</span><span class="p">[</span><span class="nt">ExtendsList</span><span class="o">/</span><span class="nt">ClassOrInterfaceType</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">'android.app.Activity'</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">'android.app.Application'</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">'android.app.Service'</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">DummyActivity</span> <span class="kd">extends</span> <span class="nc">Activity</span> <span class="o">{</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">onPause</span><span class="o">()</span> <span class="o">{</span>
<span class="n">foo</span><span class="o">();</span>
<span class="c1">// missing call to super.onPause()</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/errorprone.xml/CallSuperLast"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="checkskipresult">CheckSkipResult</h2>
<p><strong>Since:</strong> PMD 5.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>The skip() method may skip a smaller number of bytes than requested. Check the returned value to find out if it was the case or not.</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/errorprone/CheckSkipResultRule.java">net.sourceforge.pmd.lang.java.rule.errorprone.CheckSkipResultRule</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">FileInputStream</span> <span class="n">_s</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">FileInputStream</span><span class="o">(</span><span class="s">"file"</span><span class="o">);</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">skip</span><span class="o">(</span><span class="kt">int</span> <span class="n">n</span><span class="o">)</span> <span class="kd">throws</span> <span class="nc">IOException</span> <span class="o">{</span>
<span class="n">_s</span><span class="o">.</span><span class="na">skip</span><span class="o">(</span><span class="n">n</span><span class="o">);</span> <span class="c1">// You are not sure that exactly n bytes are skipped</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">skipExactly</span><span class="o">(</span><span class="kt">int</span> <span class="n">n</span><span class="o">)</span> <span class="kd">throws</span> <span class="nc">IOException</span> <span class="o">{</span>
<span class="k">while</span> <span class="o">(</span><span class="n">n</span> <span class="o">!=</span> <span class="mi">0</span><span class="o">)</span> <span class="o">{</span>
<span class="kt">long</span> <span class="n">skipped</span> <span class="o">=</span> <span class="n">_s</span><span class="o">.</span><span class="na">skip</span><span class="o">(</span><span class="n">n</span><span class="o">);</span>
<span class="k">if</span> <span class="o">(</span><span class="n">skipped</span> <span class="o">==</span> <span class="mi">0</span><span class="o">)</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nf">EOFException</span><span class="o">();</span>
<span class="n">n</span> <span class="o">-=</span> <span class="n">skipped</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/errorprone.xml/CheckSkipResult"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="classcastexceptionwithtoarray">ClassCastExceptionWithToArray</h2>
<p><strong>Since:</strong> PMD 3.4</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>When deriving an array of a specific class from your Collection, one should provide an array of
the same class as the parameter of the toArray() method. Doing otherwise you will will result
in a ClassCastException.</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">CastExpression</span><span class="p">[</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="p">!</span><span class="err">=</span><span class="w"> </span><span class="s2">"Object"</span><span class="p">]]</span><span class="w">
</span><span class="o">/</span><span class="nt">PrimaryExpression</span><span class="w">
</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="nf">ends-with</span><span class="p">(</span><span class="na">@Image</span><span class="p">,</span><span class="w"> </span><span class="s1">'.toArray'</span><span class="p">)]]</span><span class="w">
</span><span class="p">[</span><span class="nt">PrimarySuffix</span><span class="o">/</span><span class="nt">Arguments</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="err">*</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">PrimarySuffix</span><span class="p">)</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="m">1</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">Collection</span> <span class="n">c</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">ArrayList</span><span class="o">();</span>
<span class="nc">Integer</span> <span class="n">obj</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">Integer</span><span class="o">(</span><span class="mi">1</span><span class="o">);</span>
<span class="n">c</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="n">obj</span><span class="o">);</span>
<span class="c1">// this would trigger the rule (and throw a ClassCastException if executed)</span>
<span class="nc">Integer</span><span class="o">[]</span> <span class="n">a</span> <span class="o">=</span> <span class="o">(</span><span class="nc">Integer</span> <span class="o">[])</span><span class="n">c</span><span class="o">.</span><span class="na">toArray</span><span class="o">();</span>
<span class="c1">// this is fine and will not trigger the rule</span>
<span class="nc">Integer</span><span class="o">[]</span> <span class="n">b</span> <span class="o">=</span> <span class="o">(</span><span class="nc">Integer</span> <span class="o">[])</span><span class="n">c</span><span class="o">.</span><span class="na">toArray</span><span class="o">(</span><span class="k">new</span> <span class="nc">Integer</span><span class="o">[</span><span class="n">c</span><span class="o">.</span><span class="na">size</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/errorprone.xml/ClassCastExceptionWithToArray"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="clonemethodmustbepublic">CloneMethodMustBePublic</h2>
<p><strong>Since:</strong> PMD 5.4.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>The java Manual says "By convention, classes that implement this interface should override
Object.clone (which is protected) with a public method."</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">@Public</span><span class="err">=</span><span class="w"> </span><span class="nf">false</span><span class="p">()]</span><span class="w">
</span><span class="p">[</span><span class="na">@Name</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'clone'</span><span class="p">]</span><span class="w">
</span><span class="p">[</span><span class="na">@Arity</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="m">0</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">Cloneable</span> <span class="o">{</span>
<span class="nd">@Override</span>
<span class="kd">protected</span> <span class="nc">Object</span> <span class="nf">clone</span><span class="o">()</span> <span class="kd">throws</span> <span class="nc">CloneNotSupportedException</span> <span class="o">{</span> <span class="c1">// Violation, must be public</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="kd">implements</span> <span class="nc">Cloneable</span> <span class="o">{</span>
<span class="nd">@Override</span>
<span class="kd">protected</span> <span class="nc">Foo</span> <span class="nf">clone</span><span class="o">()</span> <span class="o">{</span> <span class="c1">// Violation, must be public</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="kd">implements</span> <span class="nc">Cloneable</span> <span class="o">{</span>
<span class="nd">@Override</span>
<span class="kd">public</span> <span class="nc">Object</span> <span class="nf">clone</span><span class="o">()</span> <span class="c1">// Ok</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/errorprone.xml/CloneMethodMustBePublic"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="clonemethodmustimplementcloneable">CloneMethodMustImplementCloneable</h2>
<p><strong>Since:</strong> PMD 1.9</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>The method clone() should only be implemented if the class implements the Cloneable interface with the exception of
a final method that only throws CloneNotSupportedException.</p>
<p>The rule can also detect, if the class implements or extends a Cloneable class.</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/errorprone/CloneMethodMustImplementCloneableRule.java">net.sourceforge.pmd.lang.java.rule.errorprone.CloneMethodMustImplementCloneableRule</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="kd">public</span> <span class="nc">Object</span> <span class="nf">clone</span><span class="o">()</span> <span class="kd">throws</span> <span class="nc">CloneNotSupportedException</span> <span class="o">{</span>
<span class="k">return</span> <span class="n">foo</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/errorprone.xml/CloneMethodMustImplementCloneable"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="clonemethodreturntypemustmatchclassname">CloneMethodReturnTypeMustMatchClassName</h2>
<p><strong>Since:</strong> PMD 5.4.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p><strong>Minimum Language Version:</strong> Java 1.5</p>
<p>If a class implements cloneable the return type of the method clone() must be the class name. That way, the caller
of the clone method doesnt need to cast the returned clone to the correct type.</p>
<p>Note: This is only possible with Java 1.5 or higher.</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="w">
</span><span class="p">[</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">'clone'</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">0</span><span class="w">
</span><span class="ow">and</span><span class="w"> </span><span class="ow">not</span><span class="w"> </span><span class="p">(</span><span class="nt">ResultType</span><span class="o">//</span><span class="nt">ClassOrInterfaceType</span><span class="err">/</span><span class="na">@Image</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="kn">ancestor::</span><span class="nt">ClassOrInterfaceDeclaration</span><span class="p">[</span><span class="m">1</span><span class="p">]</span><span class="err">/</span><span class="na">@SimpleName</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="kd">implements</span> <span class="nc">Cloneable</span> <span class="o">{</span>
<span class="nd">@Override</span>
<span class="kd">protected</span> <span class="nc">Object</span> <span class="nf">clone</span><span class="o">()</span> <span class="o">{</span> <span class="c1">// Violation, Object must be Foo</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="kd">implements</span> <span class="nc">Cloneable</span> <span class="o">{</span>
<span class="nd">@Override</span>
<span class="kd">public</span> <span class="nc">Foo</span> <span class="nf">clone</span><span class="o">()</span> <span class="o">{</span> <span class="c1">//Ok</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/errorprone.xml/CloneMethodReturnTypeMustMatchClassName"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="clonethrowsclonenotsupportedexception">CloneThrowsCloneNotSupportedException</h2>
<p><strong>Since:</strong> PMD 1.9</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>The method clone() should throw a CloneNotSupportedException.</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="w">
</span><span class="p">[</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">'clone'</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">0</span><span class="w">
</span><span class="ow">and</span><span class="w"> </span><span class="nf">count</span><span class="p">(</span><span class="nt">NameList</span><span class="o">/</span><span class="nt">Name</span><span class="p">[</span><span class="nf">contains</span><span class="w">
</span><span class="p">(</span><span class="na">@Image</span><span class="p">,</span><span class="s1">'CloneNotSupportedException'</span><span class="p">)])</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="m">0</span><span class="w">
</span><span class="p">]</span><span class="w">
</span><span class="p">[</span><span class="w">
</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="o">/</span><span class="nt">ClassOrInterfaceDeclaration</span><span class="p">[</span><span class="na">@Final</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="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">MyClass</span> <span class="kd">implements</span> <span class="nc">Cloneable</span><span class="o">{</span>
<span class="kd">public</span> <span class="nc">Object</span> <span class="nf">clone</span><span class="o">()</span> <span class="o">{</span> <span class="c1">// will cause an error</span>
<span class="nc">MyClass</span> <span class="n">clone</span> <span class="o">=</span> <span class="o">(</span><span class="nc">MyClass</span><span class="o">)</span><span class="kd">super</span><span class="o">.</span><span class="na">clone</span><span class="o">();</span>
<span class="k">return</span> <span class="n">clone</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/errorprone.xml/CloneThrowsCloneNotSupportedException"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="closeresource">CloseResource</h2>
<p><strong>Since:</strong> PMD 1.2.2</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Ensure that resources (like <code class="language-plaintext highlighter-rouge">java.sql.Connection</code>, <code class="language-plaintext highlighter-rouge">java.sql.Statement</code>, and <code class="language-plaintext highlighter-rouge">java.sql.ResultSet</code> objects
and any subtype of <code class="language-plaintext highlighter-rouge">java.lang.AutoCloseable</code>) are always closed after use.
Failing to do so might result in resource leaks.</p>
<p>Note: It suffices to configure the super type, e.g. <code class="language-plaintext highlighter-rouge">java.lang.AutoClosable</code>, so that this rule automatically triggers
on any subtype (e.g. <code class="language-plaintext highlighter-rouge">java.io.FileInputStream</code>). Additionally specifying <code class="language-plaintext highlighter-rouge">java.sql.Connection</code> helps in detecting
the types, if the type resolution / auxclasspath is not correctly setup.</p>
<p>Note: Since PMD 6.16.0 the default value for the property <code class="language-plaintext highlighter-rouge">types</code> contains <code class="language-plaintext highlighter-rouge">java.lang.AutoCloseable</code> and detects
now cases where the standard <code class="language-plaintext highlighter-rouge">java.io.*Stream</code> classes are involved. In order to restore the old behaviour,
just remove "AutoCloseable" from the types.</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/errorprone/CloseResourceRule.java">net.sourceforge.pmd.lang.java.rule.errorprone.CloseResourceRule</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">Bar</span> <span class="o">{</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">withSQL</span><span class="o">()</span> <span class="o">{</span>
<span class="nc">Connection</span> <span class="n">c</span> <span class="o">=</span> <span class="n">pool</span><span class="o">.</span><span class="na">getConnection</span><span class="o">();</span>
<span class="k">try</span> <span class="o">{</span>
<span class="c1">// do stuff</span>
<span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="nc">SQLException</span> <span class="n">ex</span><span class="o">)</span> <span class="o">{</span>
<span class="c1">// handle exception</span>
<span class="o">}</span> <span class="k">finally</span> <span class="o">{</span>
<span class="c1">// oops, should close the connection using 'close'!</span>
<span class="c1">// c.close();</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">withFile</span><span class="o">()</span> <span class="o">{</span>
<span class="nc">InputStream</span> <span class="n">file</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">FileInputStream</span><span class="o">(</span><span class="k">new</span> <span class="nc">File</span><span class="o">(</span><span class="s">"/tmp/foo"</span><span class="o">));</span>
<span class="k">try</span> <span class="o">{</span>
<span class="kt">int</span> <span class="n">c</span> <span class="o">=</span> <span class="n">file</span><span class="o">.</span><span class="na">in</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="c1">// handle exception</span>
<span class="o">}</span> <span class="k">finally</span> <span class="o">{</span>
<span class="c1">// TODO: close file</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>closeTargets</td>
<td> </td>
<td>Methods which may close this resource</td>
<td>yes. Delimiter is ,.</td>
</tr>
<tr>
<td>types</td>
<td>java.lang.AutoCloseable , java.sql.Connection , java.sql.Statement , java.sql.ResultSet</td>
<td>Affected types</td>
<td>yes. Delimiter is ,.</td>
</tr>
<tr>
<td>closeAsDefaultTarget</td>
<td>true</td>
<td>Consider close as a target by default</td>
<td>no</td>
</tr>
<tr>
<td>allowedResourceTypes</td>
<td>java.io.ByteArrayOutputStream | java.io.ByteArrayInputStream | java.io.StringWriter | java.io.CharArrayWriter | java.util.stream.Stream | java.util.stream.IntStream | java.util.stream.LongStream | java.util.stream.DoubleStream</td>
<td>Exact class names that do not need to be closed</td>
<td>yes. Delimiter is |.</td>
</tr>
<tr>
<td>closeNotInFinally</td>
<td>false</td>
<td>Detect if close (or other closeTargets) is called outside of a finally-block</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/errorprone.xml/CloseResource"</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/errorprone.xml/CloseResource"</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">"closeTargets"</span> <span class="na">value=</span><span class="s">""</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"types"</span> <span class="na">value=</span><span class="s">"java.lang.AutoCloseable,java.sql.Connection,java.sql.Statement,java.sql.ResultSet"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"closeAsDefaultTarget"</span> <span class="na">value=</span><span class="s">"true"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"allowedResourceTypes"</span> <span class="na">value=</span><span class="s">"java.io.ByteArrayOutputStream|java.io.ByteArrayInputStream|java.io.StringWriter|java.io.CharArrayWriter|java.util.stream.Stream|java.util.stream.IntStream|java.util.stream.LongStream|java.util.stream.DoubleStream"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"closeNotInFinally"</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="compareobjectswithequals">CompareObjectsWithEquals</h2>
<p><strong>Since:</strong> PMD 3.2</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Use equals() to compare object references; avoid comparing them with ==.</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/errorprone/CompareObjectsWithEqualsRule.java">net.sourceforge.pmd.lang.java.rule.errorprone.CompareObjectsWithEqualsRule</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">a</span><span class="o">,</span> <span class="nc">String</span> <span class="n">b</span><span class="o">)</span> <span class="o">{</span>
<span class="k">return</span> <span class="n">a</span> <span class="o">==</span> <span class="n">b</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/errorprone.xml/CompareObjectsWithEquals"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="constructorcallsoverridablemethod">ConstructorCallsOverridableMethod</h2>
<p><strong>Since:</strong> PMD 1.04</p>
<p><strong>Priority:</strong> High (1)</p>
<p>Calling overridable methods during construction poses a risk of invoking methods on an incompletely
constructed object and can be difficult to debug.
It may leave the sub-class unable to construct its superclass or forced to replicate the construction
process completely within itself, losing the ability to call super(). If the default constructor
contains a call to an overridable method, the subclass may be completely uninstantiable. Note that
this includes method calls throughout the control flow graph - i.e., if a constructor Foo() calls a
private method bar() that calls a public method buz(), this denotes a problem.</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/errorprone/ConstructorCallsOverridableMethodRule.java">net.sourceforge.pmd.lang.java.rule.errorprone.ConstructorCallsOverridableMethodRule</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">SeniorClass</span> <span class="o">{</span>
<span class="kd">public</span> <span class="nf">SeniorClass</span><span class="o">(){</span>
<span class="n">toString</span><span class="o">();</span> <span class="c1">//may throw NullPointerException if overridden</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="nc">String</span> <span class="nf">toString</span><span class="o">(){</span>
<span class="k">return</span> <span class="s">"IAmSeniorClass"</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">JuniorClass</span> <span class="kd">extends</span> <span class="nc">SeniorClass</span> <span class="o">{</span>
<span class="kd">private</span> <span class="nc">String</span> <span class="n">name</span><span class="o">;</span>
<span class="kd">public</span> <span class="nf">JuniorClass</span><span class="o">(){</span>
<span class="kd">super</span><span class="o">();</span> <span class="c1">//Automatic call leads to NullPointerException</span>
<span class="n">name</span> <span class="o">=</span> <span class="s">"JuniorClass"</span><span class="o">;</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="nc">String</span> <span class="nf">toString</span><span class="o">(){</span>
<span class="k">return</span> <span class="n">name</span><span class="o">.</span><span class="na">toUpperCase</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/errorprone.xml/ConstructorCallsOverridableMethod"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="dataflowanomalyanalysis">DataflowAnomalyAnalysis</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.9</p>
<p><strong>Priority:</strong> Low (5)</p>
<p>The dataflow analysis tracks local definitions, undefinitions and references to variables on different paths on the data flow.
From those informations there can be found various problems.</p>
<ol>
<li>DU - Anomaly: A recently defined variable is undefined. These anomalies may appear in normal source text.</li>
<li>DD - Anomaly: A recently defined variable is redefined. This is ominous but dont have to be a bug.</li>
</ol>
<p>This rule is deprecated. Use <a href="pmd_rules_java_bestpractices.html#unusedassignment"><code class="language-plaintext highlighter-rouge">UnusedAssignment</code></a> in category bestpractices 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/errorprone/DataflowAnomalyAnalysisRule.java">net.sourceforge.pmd.lang.java.rule.errorprone.DataflowAnomalyAnalysisRule</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="kt">void</span> <span class="nf">foo</span><span class="o">()</span> <span class="o">{</span>
<span class="kt">int</span> <span class="n">buz</span> <span class="o">=</span> <span class="mi">5</span><span class="o">;</span>
<span class="n">buz</span> <span class="o">=</span> <span class="mi">6</span><span class="o">;</span> <span class="c1">// redefinition of buz -&gt; dd-anomaly</span>
<span class="n">foo</span><span class="o">(</span><span class="n">buz</span><span class="o">);</span>
<span class="n">buz</span> <span class="o">=</span> <span class="mi">2</span><span class="o">;</span>
<span class="o">}</span> <span class="c1">// buz is undefined when leaving scope -&gt; du-anomaly</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>maxPaths</td>
<td>1000</td>
<td>Maximum number of checked paths per method. A lower value will increase the performance of the rule but may decrease anomalies found.</td>
<td>no</td>
</tr>
<tr>
<td>maxViolations</td>
<td>100</td>
<td>Maximum number of anomalies per class</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/errorprone.xml/DataflowAnomalyAnalysis"</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/errorprone.xml/DataflowAnomalyAnalysis"</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">"maxPaths"</span> <span class="na">value=</span><span class="s">"1000"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"maxViolations"</span> <span class="na">value=</span><span class="s">"100"</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="detachedtestcase">DetachedTestCase</h2>
<p><strong>Since:</strong> PMD 6.13.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>The method appears to be a test case since it has public or default visibility,
non-static access, no arguments, no return value, has no annotations, but is a
member of a class that has one or more JUnit test cases. If it is a utility
method, it should likely have private visibility. If it is an ignored test, it
should be annotated with @Test and @Ignore.</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="o">/</span><span class="nt">ClassOrInterfaceBodyDeclaration</span><span class="o">/</span><span class="nt">Annotation</span><span class="err">/*</span><span class="o">/</span><span class="nt">Name</span><span class="w">
</span><span class="p">[</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="ow">not</span><span class="p">(</span><span class="nt">Annotation</span><span class="p">)]</span><span class="w">
</span><span class="p">[</span><span class="nt">MethodDeclaration</span><span class="p">[(</span><span class="na">@Public</span><span class="w"> </span><span class="err">=</span><span class="w"> </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">@PackagePrivate</span><span class="w"> </span><span class="err">=</span><span class="w"> </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="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="nt">ResultType</span><span class="p">[</span><span class="na">@Void</span><span class="w"> </span><span class="err">=</span><span class="w"> </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="nt">MethodDeclarator</span><span class="o">/</span><span class="nt">FormalParameters</span><span class="p">[</span><span class="na">@Size</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="m">0</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></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="nd">@Test</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">someTest</span><span class="o">()</span> <span class="o">{</span>
<span class="o">}</span>
<span class="c1">// violation: Not annotated</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">someOtherTest</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/errorprone.xml/DetachedTestCase"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="donotcallgarbagecollectionexplicitly">DoNotCallGarbageCollectionExplicitly</h2>
<p><strong>Since:</strong> PMD 4.2</p>
<p><strong>Priority:</strong> Medium High (2)</p>
<p>Calls to System.gc(), Runtime.getRuntime().gc(), and System.runFinalization() are not advised. Code should have the
same behavior whether the garbage collection is disabled using the option -Xdisableexplicitgc or not.
Moreover, "modern" jvms do a very good job handling garbage collections. If memory usage issues unrelated to memory
leaks develop within an application, it should be dealt with JVM options rather than within the code itself.</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="p">(</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.'</span><span class="p">)</span><span class="w"> </span><span class="ow">and</span><span class="w">
</span><span class="p">(</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.gc'</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.runFinalization'</span><span class="p">)))</span><span class="w"> </span><span class="ow">or</span><span class="w">
</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="s1">'Runtime.getRuntime'</span><span class="p">)</span><span class="w"> </span><span class="ow">and</span><span class="w">
</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="o">/</span><span class="nt">PrimarySuffix</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="s1">'gc'</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></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">GCCall</span> <span class="o">{</span>
<span class="kd">public</span> <span class="nf">GCCall</span><span class="o">()</span> <span class="o">{</span>
<span class="c1">// Explicit gc call !</span>
<span class="nc">System</span><span class="o">.</span><span class="na">gc</span><span class="o">();</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="c1">// Explicit gc call !</span>
<span class="nc">Runtime</span><span class="o">.</span><span class="na">getRuntime</span><span class="o">().</span><span class="na">gc</span><span class="o">();</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="nf">explicitGCcall</span><span class="o">()</span> <span class="o">{</span>
<span class="c1">// Explicit gc call !</span>
<span class="nc">System</span><span class="o">.</span><span class="na">gc</span><span class="o">();</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="c1">// Explicit gc call !</span>
<span class="nc">Runtime</span><span class="o">.</span><span class="na">getRuntime</span><span class="o">().</span><span class="na">gc</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/errorprone.xml/DoNotCallGarbageCollectionExplicitly"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="donotcallsystemexit">DoNotCallSystemExit</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>This rule has been renamed. Use instead: <a href="#donotterminatevm">DoNotTerminateVM</a></p>
<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 4.1</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Web applications should not call <code class="language-plaintext highlighter-rouge">System.exit()</code>, since only the web container or the
application server should stop the JVM. Otherwise a web application would terminate all other applications
running on the same application server.</p>
<p>This rule also checks for the equivalent calls <code class="language-plaintext highlighter-rouge">Runtime.getRuntime().exit()</code> and <code class="language-plaintext highlighter-rouge">Runtime.getRuntime().halt()</code>.</p>
<p>This rule was called <em>DoNotCallSystemExit</em> until PMD 6.29.0.</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="s1">'System.exit'</span><span class="p">)</span><span class="w">
</span><span class="ow">or</span><span class="w">
</span><span class="p">(</span><span class="nf">starts-with</span><span class="p">(</span><span class="na">@Image</span><span class="p">,</span><span class="s1">'Runtime.getRuntime'</span><span class="p">)</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="o">/</span><span class="nt">PrimarySuffix</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="s1">'exit'</span><span class="p">)</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="nf">ends-with</span><span class="p">(</span><span class="na">@Image</span><span class="p">,</span><span class="s1">'halt'</span><span class="p">)])</span><span class="w">
</span><span class="p">][</span><span class="ow">not</span><span class="p">(</span><span class="kn">ancestor::</span><span class="nt">MethodDeclaration</span><span class="p">[</span><span class="m">1</span><span class="p">][</span><span class="na">@Name</span><span class="err">=</span><span class="s2">"main"</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="na">@Static</span><span class="w"> </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="kt">void</span> <span class="nf">bar</span><span class="o">()</span> <span class="o">{</span>
<span class="nc">System</span><span class="o">.</span><span class="na">exit</span><span class="o">(</span><span class="mi">0</span><span class="o">);</span> <span class="c1">// never call this when running in an application server!</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="nc">Runtime</span><span class="o">.</span><span class="na">getRuntime</span><span class="o">().</span><span class="na">exit</span><span class="o">(</span><span class="mi">0</span><span class="o">);</span> <span class="c1">// never stop the JVM manually, the container will do this.</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/errorprone.xml/DoNotCallSystemExit"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="donotextendjavalangthrowable">DoNotExtendJavaLangThrowable</h2>
<p><strong>Since:</strong> PMD 6.0.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Extend Exception or RuntimeException instead of Throwable.</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="o">/</span><span class="nt">ExtendsList</span><span class="o">/</span><span class="nt">ClassOrInterfaceType</span><span class="w">
</span><span class="p">[</span><span class="na">@Image</span><span class="err">=</span><span class="s2">"Throwable"</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="na">@Image</span><span class="err">=</span><span class="s2">"java.lang.Throwable"</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">extends</span> <span class="nc">Throwable</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/errorprone.xml/DoNotExtendJavaLangThrowable"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="donothardcodesdcard">DoNotHardCodeSDCard</h2>
<p><strong>Since:</strong> PMD 4.2.6</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Use Environment.getExternalStorageDirectory() instead of "/sdcard"</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">Literal</span><span class="p">[</span><span class="nf">starts-with</span><span class="p">(</span><span class="na">@Image</span><span class="p">,</span><span class="s1">'"/sdcard'</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">MyActivity</span> <span class="kd">extends</span> <span class="nc">Activity</span> <span class="o">{</span>
<span class="kd">protected</span> <span class="kt">void</span> <span class="nf">foo</span><span class="o">()</span> <span class="o">{</span>
<span class="nc">String</span> <span class="n">storageLocation</span> <span class="o">=</span> <span class="s">"/sdcard/mypackage"</span><span class="o">;</span> <span class="c1">// hard-coded, poor approach</span>
<span class="n">storageLocation</span> <span class="o">=</span> <span class="nc">Environment</span><span class="o">.</span><span class="na">getExternalStorageDirectory</span><span class="o">()</span> <span class="o">+</span> <span class="s">"/mypackage"</span><span class="o">;</span> <span class="c1">// preferred approach</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/errorprone.xml/DoNotHardCodeSDCard"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="donotterminatevm">DoNotTerminateVM</h2>
<p><strong>Since:</strong> PMD 4.1</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Web applications should not call <code class="language-plaintext highlighter-rouge">System.exit()</code>, since only the web container or the
application server should stop the JVM. Otherwise a web application would terminate all other applications
running on the same application server.</p>
<p>This rule also checks for the equivalent calls <code class="language-plaintext highlighter-rouge">Runtime.getRuntime().exit()</code> and <code class="language-plaintext highlighter-rouge">Runtime.getRuntime().halt()</code>.</p>
<p>This rule was called <em>DoNotCallSystemExit</em> until PMD 6.29.0.</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="s1">'System.exit'</span><span class="p">)</span><span class="w">
</span><span class="ow">or</span><span class="w">
</span><span class="p">(</span><span class="nf">starts-with</span><span class="p">(</span><span class="na">@Image</span><span class="p">,</span><span class="s1">'Runtime.getRuntime'</span><span class="p">)</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="o">/</span><span class="nt">PrimarySuffix</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="s1">'exit'</span><span class="p">)</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="nf">ends-with</span><span class="p">(</span><span class="na">@Image</span><span class="p">,</span><span class="s1">'halt'</span><span class="p">)])</span><span class="w">
</span><span class="p">][</span><span class="ow">not</span><span class="p">(</span><span class="kn">ancestor::</span><span class="nt">MethodDeclaration</span><span class="p">[</span><span class="m">1</span><span class="p">][</span><span class="na">@Name</span><span class="err">=</span><span class="s2">"main"</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="na">@Static</span><span class="w"> </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="kt">void</span> <span class="nf">bar</span><span class="o">()</span> <span class="o">{</span>
<span class="nc">System</span><span class="o">.</span><span class="na">exit</span><span class="o">(</span><span class="mi">0</span><span class="o">);</span> <span class="c1">// never call this when running in an application server!</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="nc">Runtime</span><span class="o">.</span><span class="na">getRuntime</span><span class="o">().</span><span class="na">exit</span><span class="o">(</span><span class="mi">0</span><span class="o">);</span> <span class="c1">// never stop the JVM manually, the container will do this.</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/errorprone.xml/DoNotTerminateVM"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="donotthrowexceptioninfinally">DoNotThrowExceptionInFinally</h2>
<p><strong>Since:</strong> PMD 4.2</p>
<p><strong>Priority:</strong> Medium Low (4)</p>
<p>Throwing exceptions within a finally block is confusing since they may mask other exceptions
or code defects.
Note: This is a PMD implementation of the Lint4j rule "A throw in a finally block"</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">FinallyStatement</span><span class="p">[</span><span class="kn">descendant::</span><span class="nt">ThrowStatement</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">bar</span><span class="o">()</span> <span class="o">{</span>
<span class="k">try</span> <span class="o">{</span>
<span class="c1">// Here do some stuff</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">// Handling the issue</span>
<span class="o">}</span> <span class="k">finally</span> <span class="o">{</span>
<span class="c1">// is this really a good idea ?</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nf">Exception</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/errorprone.xml/DoNotThrowExceptionInFinally"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="dontimportsun">DontImportSun</h2>
<p><strong>Since:</strong> PMD 1.5</p>
<p><strong>Priority:</strong> Medium Low (4)</p>
<p>Avoid importing anything from the sun.* packages. These packages are not portable and are likely to change.</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/errorprone/DontImportSunRule.java">net.sourceforge.pmd.lang.java.rule.errorprone.DontImportSunRule</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">sun.misc.foo</span><span class="o">;</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/errorprone.xml/DontImportSun"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="dontusefloattypeforloopindices">DontUseFloatTypeForLoopIndices</h2>
<p><strong>Since:</strong> PMD 4.3</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Dont use floating point for loop indices. If you must use floating point, use double
unless youre certain that float provides enough precision and you have a compelling
performance need (space or time).</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">ForStatement</span><span class="o">/</span><span class="nt">ForInit</span><span class="o">/</span><span class="nt">LocalVariableDeclaration</span><span class="w">
</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="s2">"float"</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">Count</span> <span class="o">{</span>
<span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="o">(</span><span class="nc">String</span><span class="o">[]</span> <span class="n">args</span><span class="o">)</span> <span class="o">{</span>
<span class="kd">final</span> <span class="kt">int</span> <span class="no">START</span> <span class="o">=</span> <span class="mi">2000000000</span><span class="o">;</span>
<span class="kt">int</span> <span class="n">count</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span>
<span class="k">for</span> <span class="o">(</span><span class="kt">float</span> <span class="n">f</span> <span class="o">=</span> <span class="no">START</span><span class="o">;</span> <span class="n">f</span> <span class="o">&lt;</span> <span class="no">START</span> <span class="o">+</span> <span class="mi">50</span><span class="o">;</span> <span class="n">f</span><span class="o">++)</span>
<span class="n">count</span><span class="o">++;</span>
<span class="c1">//Prints 0 because (float) START == (float) (START + 50).</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">count</span><span class="o">);</span>
<span class="c1">//The termination test misbehaves due to floating point granularity.</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/errorprone.xml/DontUseFloatTypeForLoopIndices"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="emptycatchblock">EmptyCatchBlock</h2>
<p><strong>Since:</strong> PMD 0.1</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Empty Catch Block finds instances where an exception is caught, but nothing is done.
In most circumstances, this swallows an exception which should either be acted on
or reported.</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">CatchStatement</span><span class="w">
</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="nt">Block</span><span class="o">/</span><span class="nt">BlockStatement</span><span class="p">)]</span><span class="w">
</span><span class="p">[</span><span class="nv">$allowCommentedBlocks</span><span class="w"> </span><span class="p">!</span><span class="err">=</span><span class="w"> </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="nt">Block</span><span class="err">/</span><span class="na">@containsComment</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="p">[</span><span class="nt">FormalParameter</span><span class="o">/</span><span class="nt">Type</span><span class="o">/</span><span class="nt">ReferenceType</span><span class="w">
</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="p">!</span><span class="err">=</span><span class="w"> </span><span class="s1">'InterruptedException'</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="na">@Image</span><span class="w"> </span><span class="p">!</span><span class="err">=</span><span class="w"> </span><span class="s1">'CloneNotSupportedException'</span><span class="p">]</span><span class="w">
</span><span class="p">]</span><span class="w">
</span><span class="p">[</span><span class="nt">FormalParameter</span><span class="o">/</span><span class="nt">VariableDeclaratorId</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="nf">matches</span><span class="p">(</span><span class="na">@Name</span><span class="p">,</span><span class="w"> </span><span class="nv">$allowExceptionNameRegex</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="kt">void</span> <span class="nf">doSomething</span><span class="o">()</span> <span class="o">{</span>
<span class="k">try</span> <span class="o">{</span>
<span class="nc">FileInputStream</span> <span class="n">fis</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">FileInputStream</span><span class="o">(</span><span class="s">"/tmp/bugger"</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">ioe</span><span class="o">)</span> <span class="o">{</span>
<span class="c1">// not good</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>allowCommentedBlocks</td>
<td>false</td>
<td>Empty blocks containing comments will be skipped</td>
<td>no</td>
</tr>
<tr>
<td>allowExceptionNameRegex</td>
<td>^(ignored|expected)$</td>
<td>Empty blocks catching exceptions with names matching this regular expression will be skipped</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/errorprone.xml/EmptyCatchBlock"</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/errorprone.xml/EmptyCatchBlock"</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">"allowCommentedBlocks"</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">"allowExceptionNameRegex"</span> <span class="na">value=</span><span class="s">"^(ignored|expected)$"</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="emptyfinalizer">EmptyFinalizer</h2>
<p><strong>Since:</strong> PMD 1.5</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Empty finalize methods serve no purpose and should be removed. Note that Oracle has declared Object.finalize() as deprecated since JDK 9.</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="err">=</span><span class="s1">'finalize'</span><span class="p">][</span><span class="na">@Arity</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="m">0</span><span class="p">]</span><span class="w">
</span><span class="o">/</span><span class="nt">Block</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="err">*</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">protected</span> <span class="kt">void</span> <span class="nf">finalize</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/errorprone.xml/EmptyFinalizer"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="emptyfinallyblock">EmptyFinallyBlock</h2>
<p><strong>Since:</strong> PMD 0.4</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Empty finally blocks serve no purpose and should be removed.</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">FinallyStatement</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="nt">Block</span><span class="o">/</span><span class="nt">BlockStatement</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">bar</span><span class="o">()</span> <span class="o">{</span>
<span class="k">try</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="o">}</span> <span class="k">finally</span> <span class="o">{</span>
<span class="c1">// empty!</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/errorprone.xml/EmptyFinallyBlock"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="emptyifstmt">EmptyIfStmt</h2>
<p><strong>Since:</strong> PMD 0.1</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Empty If Statement finds instances where a condition is checked but nothing is done about it.</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">IfStatement</span><span class="o">/</span><span class="nt">Statement</span><span class="w">
</span><span class="p">[</span><span class="nt">EmptyStatement</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="nt">Block</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="err">*</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">x</span><span class="o">)</span> <span class="o">{</span>
<span class="k">if</span> <span class="o">(</span><span class="n">x</span> <span class="o">==</span> <span class="mi">0</span><span class="o">)</span> <span class="o">{</span>
<span class="c1">// empty!</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/errorprone.xml/EmptyIfStmt"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="emptyinitializer">EmptyInitializer</h2>
<p><strong>Since:</strong> PMD 5.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Empty initializers serve no purpose and should be removed.</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">Initializer</span><span class="o">/</span><span class="nt">Block</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="err">*</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">static</span> <span class="o">{}</span> <span class="c1">// Why ?</span>
<span class="o">{}</span> <span class="c1">// Again, why ?</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/errorprone.xml/EmptyInitializer"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="emptystatementblock">EmptyStatementBlock</h2>
<p><strong>Since:</strong> PMD 5.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Empty block statements serve no purpose and should be removed.</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">BlockStatement</span><span class="o">/</span><span class="nt">Statement</span><span class="o">/</span><span class="nt">Block</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="err">*</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="kt">int</span> <span class="n">_bar</span><span class="o">;</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">setBar</span><span class="o">(</span><span class="kt">int</span> <span class="n">bar</span><span class="o">)</span> <span class="o">{</span>
<span class="o">{</span> <span class="n">_bar</span> <span class="o">=</span> <span class="n">bar</span><span class="o">;</span> <span class="o">}</span> <span class="c1">// Why not?</span>
<span class="o">{}</span> <span class="c1">// But remove this.</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/errorprone.xml/EmptyStatementBlock"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="emptystatementnotinloop">EmptyStatementNotInLoop</h2>
<p><strong>Since:</strong> PMD 1.5</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>An empty statement (or a semicolon by itself) that is not used as the sole body of a for
or while loop is probably a bug. It could also be a double semicolon, which has no purpose
and should be removed.</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">EmptyStatement</span><span class="w">
</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="w">
</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="o">/</span><span class="nt">ForStatement</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="o">/</span><span class="nt">WhileStatement</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="o">/</span><span class="nt">BlockStatement</span><span class="o">/</span><span class="nt">ClassOrInterfaceDeclaration</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="o">/</span><span class="nt">ForStatement</span><span class="o">/</span><span class="nt">Statement</span><span class="p">[</span><span class="m">1</span><span class="p">]</span><span class="w">
</span><span class="o">/</span><span class="nt">Block</span><span class="p">[</span><span class="m">1</span><span class="p">]</span><span class="o">/</span><span class="nt">BlockStatement</span><span class="p">[</span><span class="m">1</span><span class="p">]</span><span class="o">/</span><span class="nt">Statement</span><span class="o">/</span><span class="nt">EmptyStatement</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="o">/</span><span class="nt">WhileStatement</span><span class="o">/</span><span class="nt">Statement</span><span class="p">[</span><span class="m">1</span><span class="p">]</span><span class="w">
</span><span class="o">/</span><span class="nt">Block</span><span class="p">[</span><span class="m">1</span><span class="p">]</span><span class="o">/</span><span class="nt">BlockStatement</span><span class="p">[</span><span class="m">1</span><span class="p">]</span><span class="o">/</span><span class="nt">Statement</span><span class="o">/</span><span class="nt">EmptyStatement</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="kt">void</span> <span class="nf">doit</span><span class="o">()</span> <span class="o">{</span>
<span class="c1">// this is probably not what you meant to do</span>
<span class="o">;</span>
<span class="c1">// the extra semicolon here this is not necessary</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">"look at the extra semicolon"</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/errorprone.xml/EmptyStatementNotInLoop"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="emptyswitchstatements">EmptySwitchStatements</h2>
<p><strong>Since:</strong> PMD 1.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Empty switch statements serve no purpose and should be removed.</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="nf">count</span><span class="p">(</span><span class="err">*</span><span class="p">)</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="m">1</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="kt">void</span> <span class="nf">bar</span><span class="o">()</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="c1">// once there was code here</span>
<span class="c1">// but it's been commented out or something</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/errorprone.xml/EmptySwitchStatements"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="emptysynchronizedblock">EmptySynchronizedBlock</h2>
<p><strong>Since:</strong> PMD 1.3</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Empty synchronized blocks serve no purpose and should be removed.</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">SynchronizedStatement</span><span class="o">/</span><span class="nt">Block</span><span class="p">[</span><span class="m">1</span><span class="p">][</span><span class="ow">not</span><span class="p">(</span><span class="err">*</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">bar</span><span class="o">()</span> <span class="o">{</span>
<span class="kd">synchronized</span> <span class="o">(</span><span class="k">this</span><span class="o">)</span> <span class="o">{</span>
<span class="c1">// empty!</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/errorprone.xml/EmptySynchronizedBlock"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="emptytryblock">EmptyTryBlock</h2>
<p><strong>Since:</strong> PMD 0.4</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Avoid empty try blocks - whats the point?</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">TryStatement</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="nt">ResourceSpecification</span><span class="p">)]</span><span class="o">/</span><span class="nt">Block</span><span class="p">[</span><span class="m">1</span><span class="p">][</span><span class="ow">not</span><span class="p">(</span><span class="err">*</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">bar</span><span class="o">()</span> <span class="o">{</span>
<span class="k">try</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="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/errorprone.xml/EmptyTryBlock"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="emptywhilestmt">EmptyWhileStmt</h2>
<p><strong>Since:</strong> PMD 0.2</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Empty While Statement finds all instances where a while statement does nothing.
If it is a timing loop, then you should use Thread.sleep() for it; if it is
a while loop that does a lot in the exit expression, rewrite it to make it clearer.</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">WhileStatement</span><span class="o">/</span><span class="nt">Statement</span><span class="p">[</span><span class="nt">Block</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="err">*</span><span class="p">)]</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="nt">EmptyStatement</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="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="kt">int</span> <span class="n">b</span><span class="o">)</span> <span class="o">{</span>
<span class="k">while</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="o">{</span>
<span class="c1">// empty!</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/errorprone.xml/EmptyWhileStmt"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="equalsnull">EqualsNull</h2>
<p><strong>Since:</strong> PMD 1.9</p>
<p><strong>Priority:</strong> High (1)</p>
<p>Tests for null should not use the equals() method. The == operator should be used 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="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">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="kn">following-sibling::</span><span class="kr">node()</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="nf">count</span><span class="p">(</span><span class="nt">Expression</span><span class="p">)</span><span class="err">=</span><span class="m">1</span><span class="p">]</span><span class="w">
</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">NullLiteral</span><span class="p">]</span><span class="w">
</span><span class="ow">or</span><span class="w">
</span><span class="nt">PrimarySuffix</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="kn">following-sibling::</span><span class="kr">node()</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="nf">count</span><span class="p">(</span><span class="nt">Expression</span><span class="p">)</span><span class="err">=</span><span class="m">1</span><span class="p">]</span><span class="w">
</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">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="nc">String</span> <span class="n">x</span> <span class="o">=</span> <span class="s">"foo"</span><span class="o">;</span>
<span class="k">if</span> <span class="o">(</span><span class="n">x</span><span class="o">.</span><span class="na">equals</span><span class="o">(</span><span class="kc">null</span><span class="o">))</span> <span class="o">{</span> <span class="c1">// bad form</span>
<span class="n">doSomething</span><span class="o">();</span>
<span class="o">}</span>
<span class="k">if</span> <span class="o">(</span><span class="n">x</span> <span class="o">==</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span> <span class="c1">// preferred</span>
<span class="n">doSomething</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/errorprone.xml/EqualsNull"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="finalizedoesnotcallsuperfinalize">FinalizeDoesNotCallSuperFinalize</h2>
<p><strong>Since:</strong> PMD 1.5</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>If the finalize() is implemented, its last action should be to call super.finalize. Note that Oracle has declared Object.finalize() as deprecated since JDK 9.</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="err">=</span><span class="s1">'finalize'</span><span class="p">][</span><span class="na">@Arity</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="m">0</span><span class="p">]</span><span class="w">
</span><span class="o">/</span><span class="nt">Block</span><span class="w">
</span><span class="o">/</span><span class="nt">BlockStatement</span><span class="p">[</span><span class="nf">last</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">Statement</span><span class="o">/</span><span class="nt">StatementExpression</span><span class="o">/</span><span class="nt">PrimaryExpression</span><span class="w">
</span><span class="p">[.</span><span class="o">/</span><span class="nt">PrimaryPrefix</span><span class="p">[</span><span class="na">@SuperModifier</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="o">/</span><span class="nt">PrimarySuffix</span><span class="p">[</span><span class="na">@Image</span><span class="err">=</span><span class="s1">'finalize'</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="p">[</span><span class="ow">not</span><span class="p">(</span><span class="nt">Statement</span><span class="o">/</span><span class="nt">TryStatement</span><span class="o">/</span><span class="nt">FinallyStatement</span><span class="w">
</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">StatementExpression</span><span class="o">/</span><span class="nt">PrimaryExpression</span><span class="w">
</span><span class="p">[.</span><span class="o">/</span><span class="nt">PrimaryPrefix</span><span class="p">[</span><span class="na">@SuperModifier</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="o">/</span><span class="nt">PrimarySuffix</span><span class="p">[</span><span class="na">@Image</span><span class="err">=</span><span class="s1">'finalize'</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></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">protected</span> <span class="kt">void</span> <span class="nf">finalize</span><span class="o">()</span> <span class="o">{</span>
<span class="n">something</span><span class="o">();</span>
<span class="c1">// neglected to call super.finalize()</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/errorprone.xml/FinalizeDoesNotCallSuperFinalize"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="finalizeonlycallssuperfinalize">FinalizeOnlyCallsSuperFinalize</h2>
<p><strong>Since:</strong> PMD 1.5</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>If the finalize() is implemented, it should do something besides just calling super.finalize(). Note that Oracle has declared Object.finalize() as deprecated since JDK 9.</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="err">=</span><span class="s1">'finalize'</span><span class="p">][</span><span class="na">@Arity</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="m">0</span><span class="p">]</span><span class="w">
</span><span class="o">/</span><span class="nt">Block</span><span class="p">[</span><span class="nf">count</span><span class="p">(</span><span class="nt">BlockStatement</span><span class="p">)</span><span class="err">=</span><span class="m">1</span><span class="p">]</span><span class="w">
</span><span class="o">/</span><span class="nt">BlockStatement</span><span class="p">[</span><span class="w">
</span><span class="nt">Statement</span><span class="o">/</span><span class="nt">StatementExpression</span><span class="o">/</span><span class="nt">PrimaryExpression</span><span class="w">
</span><span class="p">[.</span><span class="o">/</span><span class="nt">PrimaryPrefix</span><span class="p">[</span><span class="na">@SuperModifier</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="o">/</span><span class="nt">PrimarySuffix</span><span class="p">[</span><span class="na">@Image</span><span class="err">=</span><span class="s1">'finalize'</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">protected</span> <span class="kt">void</span> <span class="nf">finalize</span><span class="o">()</span> <span class="o">{</span>
<span class="kd">super</span><span class="o">.</span><span class="na">finalize</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/errorprone.xml/FinalizeOnlyCallsSuperFinalize"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="finalizeoverloaded">FinalizeOverloaded</h2>
<p><strong>Since:</strong> PMD 1.5</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Methods named finalize() should not have parameters. It is confusing and most likely an attempt to
overload Object.finalize(). It will not be called by the VM.</p>
<p>Note that Oracle has declared Object.finalize() as deprecated since JDK 9.</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="err">=</span><span class="s1">'finalize'</span><span class="p">][</span><span class="na">@Arity</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="m">0</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="c1">// this is confusing and probably a bug</span>
<span class="kd">protected</span> <span class="kt">void</span> <span class="nf">finalize</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="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/errorprone.xml/FinalizeOverloaded"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="finalizeshouldbeprotected">FinalizeShouldBeProtected</h2>
<p><strong>Since:</strong> PMD 1.1</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>When overriding the finalize(), the new method should be set as protected. If made public,
other classes may invoke it at inappropriate times.</p>
<p>Note that Oracle has declared Object.finalize() as deprecated since JDK 9.</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">@Protected</span><span class="err">=</span><span class="nf">false</span><span class="p">()][</span><span class="na">@Name</span><span class="err">=</span><span class="s1">'finalize'</span><span class="p">][</span><span class="na">@Arity</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="m">0</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="kt">void</span> <span class="nf">finalize</span><span class="o">()</span> <span class="o">{</span>
<span class="c1">// do something</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/errorprone.xml/FinalizeShouldBeProtected"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="idempotentoperations">IdempotentOperations</h2>
<p><strong>Since:</strong> PMD 2.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Avoid idempotent operations - they have no effect.</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/errorprone/IdempotentOperationsRule.java">net.sourceforge.pmd.lang.java.rule.errorprone.IdempotentOperationsRule</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">bar</span><span class="o">()</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="n">x</span> <span class="o">=</span> <span class="n">x</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/errorprone.xml/IdempotentOperations"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="importfromsamepackage">ImportFromSamePackage</h2>
<p><strong>Since:</strong> PMD 1.02</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>There is no need to import a type that lives in the same package.</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/errorprone/ImportFromSamePackageRule.java">net.sourceforge.pmd.lang.java.rule.errorprone.ImportFromSamePackageRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">package</span> <span class="nn">foo</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">foo.Buz</span><span class="o">;</span> <span class="c1">// no need for this</span>
<span class="kn">import</span> <span class="nn">foo.*</span><span class="o">;</span> <span class="c1">// or this</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">Bar</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/errorprone.xml/ImportFromSamePackage"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="instantiationtogetclass">InstantiationToGetClass</h2>
<p><strong>Since:</strong> PMD 2.0</p>
<p><strong>Priority:</strong> Medium Low (4)</p>
<p>Avoid instantiating an object just to call getClass() on it; use the .class public member 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">PrimarySuffix</span><span class="w">
</span><span class="p">[</span><span class="na">@Image</span><span class="err">=</span><span class="s1">'getClass'</span><span class="p">]</span><span class="w">
</span><span class="p">[</span><span class="kn">parent::</span><span class="nt">PrimaryExpression</span><span class="w">
</span><span class="p">[</span><span class="nt">PrimaryPrefix</span><span class="o">/</span><span class="nt">AllocationExpression</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">PrimarySuffix</span><span class="p">)</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="m">2</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="c1">// replace this</span>
<span class="nc">Class</span> <span class="n">c</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">String</span><span class="o">().</span><span class="na">getClass</span><span class="o">();</span>
<span class="c1">// with this:</span>
<span class="nc">Class</span> <span class="n">c</span> <span class="o">=</span> <span class="nc">String</span><span class="o">.</span><span class="na">class</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/errorprone.xml/InstantiationToGetClass"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="invalidlogmessageformat">InvalidLogMessageFormat</h2>
<p><strong>Since:</strong> PMD 5.5.0</p>
<p><strong>Priority:</strong> Low (5)</p>
<p>Check for messages in slf4j and log4j2 (since 6.19.0) loggers with non matching number of arguments and placeholders.</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/errorprone/InvalidLogMessageFormatRule.java">net.sourceforge.pmd.lang.java.rule.errorprone.InvalidLogMessageFormatRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="no">LOGGER</span><span class="o">.</span><span class="na">error</span><span class="o">(</span><span class="s">"forget the arg {}"</span><span class="o">);</span>
<span class="no">LOGGER</span><span class="o">.</span><span class="na">error</span><span class="o">(</span><span class="s">"too many args {}"</span><span class="o">,</span> <span class="s">"arg1"</span><span class="o">,</span> <span class="s">"arg2"</span><span class="o">);</span>
<span class="no">LOGGER</span><span class="o">.</span><span class="na">error</span><span class="o">(</span><span class="s">"param {}"</span><span class="o">,</span> <span class="s">"arg1"</span><span class="o">,</span> <span class="k">new</span> <span class="nc">IllegalStateException</span><span class="o">(</span><span class="s">"arg"</span><span class="o">));</span> <span class="c1">//The exception is shown separately, so is correct.</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/errorprone.xml/InvalidLogMessageFormat"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="invalidslf4jmessageformat">InvalidSlf4jMessageFormat</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>This rule has been renamed. Use instead: <a href="#invalidlogmessageformat">InvalidLogMessageFormat</a></p>
<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.5.0</p>
<p><strong>Priority:</strong> Low (5)</p>
<p>Check for messages in slf4j and log4j2 (since 6.19.0) loggers with non matching number of arguments and placeholders.</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/errorprone/InvalidLogMessageFormatRule.java">net.sourceforge.pmd.lang.java.rule.errorprone.InvalidLogMessageFormatRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="no">LOGGER</span><span class="o">.</span><span class="na">error</span><span class="o">(</span><span class="s">"forget the arg {}"</span><span class="o">);</span>
<span class="no">LOGGER</span><span class="o">.</span><span class="na">error</span><span class="o">(</span><span class="s">"too many args {}"</span><span class="o">,</span> <span class="s">"arg1"</span><span class="o">,</span> <span class="s">"arg2"</span><span class="o">);</span>
<span class="no">LOGGER</span><span class="o">.</span><span class="na">error</span><span class="o">(</span><span class="s">"param {}"</span><span class="o">,</span> <span class="s">"arg1"</span><span class="o">,</span> <span class="k">new</span> <span class="nc">IllegalStateException</span><span class="o">(</span><span class="s">"arg"</span><span class="o">));</span> <span class="c1">//The exception is shown separately, so is correct.</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/errorprone.xml/InvalidSlf4jMessageFormat"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="jumbledincrementer">JumbledIncrementer</h2>
<p><strong>Since:</strong> PMD 1.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Avoid jumbled loop incrementers - its usually a mistake, and is confusing even if intentional.</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">ForStatement</span><span class="w">
</span><span class="p">[</span><span class="w">
</span><span class="nt">ForUpdate</span><span class="o">/</span><span class="nt">StatementExpressionList</span><span class="o">/</span><span class="nt">StatementExpression</span><span class="o">/</span><span class="nt">PostfixExpression</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="err">/</span><span class="na">@Image</span><span class="w">
</span><span class="err">=</span><span class="w">
</span><span class="kn">ancestor::</span><span class="nt">ForStatement</span><span class="o">/</span><span class="nt">ForInit</span><span class="o">//</span><span class="nt">VariableDeclaratorId</span><span class="err">/</span><span class="na">@Name</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">JumbledIncrementerRule1</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">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="c1">// only references 'i'</span>
<span class="k">for</span> <span class="o">(</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="n">k</span> <span class="o">&lt;</span> <span class="mi">20</span><span class="o">;</span> <span class="n">i</span><span class="o">++)</span> <span class="o">{</span> <span class="c1">// references both 'i' and 'k'</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="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/errorprone.xml/JumbledIncrementer"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="junitspelling">JUnitSpelling</h2>
<p><strong>Since:</strong> PMD 1.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>In JUnit 3, the setUp method is used to set up all data entities required in running tests.
The tearDown method is used to clean up all data entities required in running tests.
You should not misspell method name if you want your test to set up and clean up everything correctly.</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/errorprone/JUnitSpellingRule.java">net.sourceforge.pmd.lang.java.rule.errorprone.JUnitSpellingRule</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">junit.framework.*</span><span class="o">;</span>
<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">setup</span><span class="o">()</span> <span class="o">{}</span> <span class="c1">// oops, should be setUp</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="c1">// oops, should be tearDown</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/errorprone.xml/JUnitSpelling"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="junitstaticsuite">JUnitStaticSuite</h2>
<p><strong>Since:</strong> PMD 1.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>The suite() method in a JUnit test needs to be both public and static.</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/errorprone/JUnitStaticSuiteRule.java">net.sourceforge.pmd.lang.java.rule.errorprone.JUnitStaticSuiteRule</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">junit.framework.*</span><span class="o">;</span>
<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">suite</span><span class="o">()</span> <span class="o">{}</span> <span class="c1">// oops, should be static</span>
<span class="kd">private</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">suite</span><span class="o">()</span> <span class="o">{}</span> <span class="c1">// oops, should be public</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/errorprone.xml/JUnitStaticSuite"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="loggerisnotstaticfinal">LoggerIsNotStaticFinal</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 2.0</p>
<p><strong>Priority:</strong> Medium High (2)</p>
<p>In most cases, the Logger reference can be declared as static and final.</p>
<p>This rule is deprecated and will be removed with PMD 7.0.0.
The rule is replaced by <a href="pmd_rules_java_errorprone.html#properlogger"><code class="language-plaintext highlighter-rouge">ProperLogger</code></a>.</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">VariableDeclarator</span><span class="w">
</span><span class="p">[</span><span class="kn">parent::</span><span class="nt">FieldDeclaration</span><span class="p">]</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="w">
</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">'Logger'</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">@Final</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="p">..[</span><span class="na">@Static</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="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="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="c1">// not recommended</span>
<span class="kd">static</span> <span class="kd">final</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="c1">// preferred approach</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/errorprone.xml/LoggerIsNotStaticFinal"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="methodwithsamenameasenclosingclass">MethodWithSameNameAsEnclosingClass</h2>
<p><strong>Since:</strong> PMD 1.5</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Non-constructor methods should not have the same name as the enclosing class.</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/errorprone/MethodWithSameNameAsEnclosingClassRule.java">net.sourceforge.pmd.lang.java.rule.errorprone.MethodWithSameNameAsEnclosingClassRule</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="kd">public</span> <span class="nf">MyClass</span><span class="o">()</span> <span class="o">{}</span> <span class="c1">// this is OK because it is a constructor</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">MyClass</span><span class="o">()</span> <span class="o">{}</span> <span class="c1">// this is bad because it is a method</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/errorprone.xml/MethodWithSameNameAsEnclosingClass"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="misplacednullcheck">MisplacedNullCheck</h2>
<p><strong>Since:</strong> PMD 3.5</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>The null check here is misplaced. If the variable is null a NullPointerException will be thrown.
Either the check is useless (the variable will never be "null") or it is incorrect.</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">ConditionalAndExpression</span><span class="w">
</span><span class="o">/</span><span class="nt">EqualityExpression</span><span class="w">
</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">'!='</span><span class="p">]</span><span class="w">
</span><span class="c">(: one side is null :)</span><span class="w">
</span><span class="p">[</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="p">]</span><span class="w">
</span><span class="c">(: other side checks for the variable used somewhere in the first child of conditional and expression :)</span><span class="w">
</span><span class="p">[</span><span class="kr">some</span><span class="w"> </span><span class="nv">$var</span><span class="w"> </span><span class="kr">in</span><span class="w"> </span><span class="kn">preceding-sibling::</span><span class="nt">PrimaryExpression</span><span class="o">//</span><span class="nt">Name</span><span class="w">
</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="kn">ancestor::</span><span class="nt">ConditionalOrExpression</span><span class="o">/</span><span class="nt">EqualityExpression</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">'=='</span><span class="p">])]</span><span class="w">
</span><span class="err">/</span><span class="na">@Image</span><span class="w">
</span><span class="kr">satisfies</span><span class="w"> </span><span class="nf">starts-with</span><span class="p">(</span><span class="nv">$var</span><span class="p">,</span><span class="w"> </span><span class="nf">concat</span><span class="p">(</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="err">/</span><span class="na">@Image</span><span class="p">,</span><span class="w"> </span><span class="s1">'.'</span><span class="p">))]</span><span class="w">
</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="w">
</span><span class="o">//</span><span class="nt">ConditionalOrExpression</span><span class="w">
</span><span class="o">/</span><span class="nt">EqualityExpression</span><span class="w">
</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">'=='</span><span class="p">]</span><span class="w">
</span><span class="c">(: one side is null :)</span><span class="w">
</span><span class="p">[</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="p">]</span><span class="w">
</span><span class="c">(: other side checks for the variable used somewhere in the first child of conditional or expression :)</span><span class="w">
</span><span class="p">[</span><span class="kr">some</span><span class="w"> </span><span class="nv">$var</span><span class="w"> </span><span class="kr">in</span><span class="w"> </span><span class="kn">preceding-sibling::</span><span class="nt">PrimaryExpression</span><span class="o">//</span><span class="nt">Name</span><span class="w">
</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="kn">ancestor::</span><span class="nt">ConditionalAndExpression</span><span class="o">/</span><span class="nt">EqualityExpression</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">'!='</span><span class="p">])]</span><span class="w">
</span><span class="err">/</span><span class="na">@Image</span><span class="w">
</span><span class="kr">satisfies</span><span class="w"> </span><span class="nf">starts-with</span><span class="p">(</span><span class="nv">$var</span><span class="p">,</span><span class="w"> </span><span class="nf">concat</span><span class="p">(</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="err">/</span><span class="na">@Image</span><span class="p">,</span><span class="w"> </span><span class="s1">'.'</span><span class="p">))]</span><span class="w">
</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></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="k">if</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">baz</span><span class="o">)</span> <span class="o">&amp;&amp;</span> <span class="n">a</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{}</span> <span class="c1">// a could be null, misplaced null check</span>
<span class="k">if</span> <span class="o">(</span><span class="n">a</span> <span class="o">!=</span> <span class="kc">null</span> <span class="o">&amp;&amp;</span> <span class="n">a</span><span class="o">.</span><span class="na">equals</span><span class="o">(</span><span class="n">baz</span><span class="o">))</span> <span class="o">{}</span> <span class="c1">// correct null check</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">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="k">if</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">baz</span><span class="o">)</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="o">{}</span> <span class="c1">// a could be null, misplaced null check</span>
<span class="k">if</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="n">a</span><span class="o">.</span><span class="na">equals</span><span class="o">(</span><span class="n">baz</span><span class="o">))</span> <span class="o">{}</span> <span class="c1">// correct null check</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/errorprone.xml/MisplacedNullCheck"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="missingbreakinswitch">MissingBreakInSwitch</h2>
<p><strong>Since:</strong> PMD 3.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Switch statements without break or return statements for each case option
may indicate problematic behaviour. Empty cases are ignored as these indicate an intentional fall-through.</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="nf">count</span><span class="p">(.</span><span class="o">//</span><span class="nt">BreakStatement</span><span class="p">)</span><span class="w">
</span><span class="o">+</span><span class="w"> </span><span class="nf">count</span><span class="p">(</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="p">)</span><span class="w">
</span><span class="o">+</span><span class="w"> </span><span class="nf">count</span><span class="p">(</span><span class="nt">BlockStatement</span><span class="o">//</span><span class="nt">Statement</span><span class="o">/</span><span class="nt">ContinueStatement</span><span class="p">)</span><span class="w">
</span><span class="o">+</span><span class="w"> </span><span class="nf">count</span><span class="p">(</span><span class="nt">BlockStatement</span><span class="o">//</span><span class="nt">Statement</span><span class="o">/</span><span class="nt">ThrowStatement</span><span class="p">)</span><span class="w">
</span><span class="o">+</span><span class="w"> </span><span class="nf">count</span><span class="p">(</span><span class="nt">BlockStatement</span><span class="o">//</span><span class="nt">Statement</span><span class="o">/</span><span class="nt">IfStatement</span><span class="p">[</span><span class="na">@Else</span><span class="err">=</span><span class="w"> </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="nt">Statement</span><span class="p">[</span><span class="m">2</span><span class="p">][</span><span class="nt">ReturnStatement</span><span class="p">|</span><span class="nt">ContinueStatement</span><span class="p">|</span><span class="nt">ThrowStatement</span><span class="p">]]</span><span class="o">/</span><span class="nt">Statement</span><span class="p">[</span><span class="m">1</span><span class="p">][</span><span class="nt">ReturnStatement</span><span class="p">|</span><span class="nt">ContinueStatement</span><span class="p">|</span><span class="nt">ThrowStatement</span><span class="p">])</span><span class="w">
</span><span class="o">+</span><span class="w"> </span><span class="nf">count</span><span class="p">(</span><span class="nt">SwitchLabel</span><span class="p">[</span><span class="w"> </span><span class="kn">following-sibling::</span><span class="kr">node()</span><span class="p">[</span><span class="m">1</span><span class="p">][</span><span class="kn">self::</span><span class="nt">SwitchLabel</span><span class="p">]</span><span class="w"> </span><span class="p">])</span><span class="w">
</span><span class="o">+</span><span class="w"> </span><span class="nf">count</span><span class="p">(</span><span class="nt">SwitchLabel</span><span class="p">[</span><span class="nf">count</span><span class="p">(</span><span class="kn">following-sibling::</span><span class="kr">node()</span><span class="p">)</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="m">0</span><span class="p">])</span><span class="w">
</span><span class="o">&lt;</span><span class="w"> </span><span class="nf">count</span><span class="w"> </span><span class="p">(</span><span class="nt">SwitchLabel</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="kt">void</span> <span class="nf">bar</span><span class="o">(</span><span class="kt">int</span> <span class="n">status</span><span class="o">)</span> <span class="o">{</span>
<span class="k">switch</span><span class="o">(</span><span class="n">status</span><span class="o">)</span> <span class="o">{</span>
<span class="k">case</span> <span class="nl">CANCELLED:</span>
<span class="n">doCancelled</span><span class="o">();</span>
<span class="c1">// break; hm, should this be commented out?</span>
<span class="k">case</span> <span class="nl">NEW:</span>
<span class="n">doNew</span><span class="o">();</span>
<span class="c1">// is this really a fall-through?</span>
<span class="k">case</span> <span class="nl">REMOVED:</span>
<span class="n">doRemoved</span><span class="o">();</span>
<span class="c1">// what happens if you add another case after this one?</span>
<span class="k">case</span> <span class="nl">OTHER:</span> <span class="c1">// empty case - this is interpreted as an intentional fall-through</span>
<span class="k">case</span> <span class="nl">ERROR:</span>
<span class="n">doErrorHandling</span><span class="o">();</span>
<span class="k">break</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/errorprone.xml/MissingBreakInSwitch"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="missingserialversionuid">MissingSerialVersionUID</h2>
<p><strong>Since:</strong> PMD 3.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Serializable classes should provide a serialVersionUID field.
The serialVersionUID field is also needed for abstract base classes. Each individual class in the inheritance
chain needs an own serialVersionUID field. See also <a href="https://stackoverflow.com/questions/893259/should-an-abstract-class-have-a-serialversionuid">Should an abstract class have a serialVersionUID</a>.</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="w">
</span><span class="p">[</span><span class="na">@Interface</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="p">[</span><span class="nf">count</span><span class="p">(</span><span class="nt">ClassOrInterfaceBody</span><span class="o">/</span><span class="nt">ClassOrInterfaceBodyDeclaration</span><span class="w">
</span><span class="o">/</span><span class="nt">FieldDeclaration</span><span class="o">/</span><span class="nt">VariableDeclarator</span><span class="o">/</span><span class="nt">VariableDeclaratorId</span><span class="p">[</span><span class="na">@Name</span><span class="err">=</span><span class="s1">'serialVersionUID'</span><span class="p">])</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="m">0</span><span class="p">]</span><span class="w">
</span><span class="p">[(</span><span class="nt">ImplementsList</span><span class="w"> </span><span class="p">|</span><span class="w"> </span><span class="nt">ExtendsList</span><span class="p">)</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">'java.io.Serializable'</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="n">java</span><span class="o">.</span><span class="na">io</span><span class="o">.</span><span class="na">Serializable</span> <span class="o">{</span>
<span class="nc">String</span> <span class="n">name</span><span class="o">;</span>
<span class="c1">// Define serialization id to avoid serialization related bugs</span>
<span class="c1">// i.e., public static final long serialVersionUID = 4328743;</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/errorprone.xml/MissingSerialVersionUID"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="missingstaticmethodinnoninstantiatableclass">MissingStaticMethodInNonInstantiatableClass</h2>
<p><strong>Since:</strong> PMD 3.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>A class that has private constructors and does not have any static methods or fields cannot be used.</p>
<p>When one of the private constructors is annotated with one of the annotations, then the class is not considered
non-instantiatable anymore and no violation will be reported.
See the property <code class="language-plaintext highlighter-rouge">annotations</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="na">@Nested</span><span class="err">=</span><span class="w"> </span><span class="nf">false</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="c">(: at least one constructor :)</span><span class="w">
</span><span class="p">.</span><span class="o">/</span><span class="nt">ClassOrInterfaceBody</span><span class="o">/</span><span class="nt">ClassOrInterfaceBodyDeclaration</span><span class="o">/</span><span class="nt">ConstructorDeclaration</span><span class="w">
</span><span class="ow">and</span><span class="w">
</span><span class="c">(: only private constructors :)</span><span class="nf">count</span><span class="p">(.</span><span class="o">/</span><span class="nt">ClassOrInterfaceBody</span><span class="o">/</span><span class="nt">ClassOrInterfaceBodyDeclaration</span><span class="o">/</span><span class="nt">ConstructorDeclaration</span><span class="p">)</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="nf">count</span><span class="p">(.</span><span class="o">/</span><span class="nt">ClassOrInterfaceBody</span><span class="o">/</span><span class="nt">ClassOrInterfaceBodyDeclaration</span><span class="o">/</span><span class="nt">ConstructorDeclaration</span><span class="p">[</span><span class="na">@Private</span><span class="err">=</span><span class="w"> </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="c">(: all constructors must not be annotated :)</span><span class="w">
</span><span class="p">(</span><span class="kr">every</span><span class="w"> </span><span class="nv">$x</span><span class="w"> </span><span class="kr">in</span><span class="w"> </span><span class="nv">$annotations</span><span class="w"> </span><span class="kr">satisfies</span><span class="w">
</span><span class="ow">not</span><span class="p">(.</span><span class="o">/</span><span class="nt">ClassOrInterfaceBody</span><span class="o">/</span><span class="nt">ClassOrInterfaceBodyDeclaration</span><span class="o">/</span><span class="nt">ConstructorDeclaration</span><span class="o">/</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="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="nv">$x</span><span class="p">)]))</span><span class="w">
</span><span class="p">)</span><span class="w">
</span><span class="ow">and</span><span class="w">
</span><span class="c">(: no static methods :)</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="na">@Static</span><span class="err">=</span><span class="w"> </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="c">(: no (public, package-private, protected) static fields :)</span><span class="ow">not</span><span class="p">(.</span><span class="o">//</span><span class="nt">FieldDeclaration</span><span class="p">[</span><span class="na">@Private</span><span class="err">=</span><span class="w"> </span><span class="nf">false</span><span class="p">()][</span><span class="na">@Static</span><span class="err">=</span><span class="w"> </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="c">(: no nested classes, that are public and static, and have no constructors at all or a public constructor :)</span><span class="w">
</span><span class="c">(: and have a method returning the outer class type :)</span><span class="w">
</span><span class="c">(: or the inner class extends the outer class :)</span><span class="ow">not</span><span class="p">(.</span><span class="o">//</span><span class="nt">ClassOrInterfaceDeclaration</span><span class="p">[</span><span class="na">@Nested</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="na">@Public</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="na">@Static</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="ow">not</span><span class="p">(.</span><span class="o">/</span><span class="nt">ClassOrInterfaceBody</span><span class="o">/</span><span class="nt">ClassOrInterfaceBodyDeclaration</span><span class="o">/</span><span class="nt">ConstructorDeclaration</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">ClassOrInterfaceBody</span><span class="o">/</span><span class="nt">ClassOrInterfaceBodyDeclaration</span><span class="o">/</span><span class="nt">ConstructorDeclaration</span><span class="p">[</span><span class="na">@Public</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="o">/</span><span class="nt">ClassOrInterfaceBody</span><span class="o">/</span><span class="nt">ClassOrInterfaceBodyDeclaration</span><span class="o">/</span><span class="nt">MethodDeclaration</span><span class="w">
</span><span class="p">[</span><span class="na">@Public</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="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="w">
</span><span class="p">[</span><span class="na">@Image</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="o">//</span><span class="nt">ClassOrInterfaceDeclaration</span><span class="p">[</span><span class="na">@Nested</span><span class="err">=</span><span class="w"> </span><span class="nf">false</span><span class="p">()]</span><span class="err">/</span><span class="na">@SimpleName</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="ow">or</span><span class="w"> </span><span class="p">(</span><span class="w">
</span><span class="p">.</span><span class="o">/</span><span class="nt">ExtendsList</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="o">//</span><span class="nt">ClassOrInterfaceDeclaration</span><span class="p">[</span><span class="na">@Nested</span><span class="err">=</span><span class="nf">false</span><span class="p">()]</span><span class="err">/</span><span class="na">@SimpleName</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="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 class is unusable, since it cannot be</span>
<span class="c1">// instantiated (private constructor),</span>
<span class="c1">// and no static method can be called.</span>
<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="nf">Foo</span><span class="o">()</span> <span class="o">{}</span>
<span class="kt">void</span> <span class="nf">foo</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>annotations</td>
<td>org.springframework.beans.factory.annotation.Autowired , javax.inject.Inject</td>
<td>If a constructor is annotated with one of these annotations, then the class is ignored.</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/errorprone.xml/MissingStaticMethodInNonInstantiatableClass"</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/errorprone.xml/MissingStaticMethodInNonInstantiatableClass"</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">"annotations"</span> <span class="na">value=</span><span class="s">"org.springframework.beans.factory.annotation.Autowired, javax.inject.Inject"</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="morethanonelogger">MoreThanOneLogger</h2>
<p><strong>Since:</strong> PMD 2.0</p>
<p><strong>Priority:</strong> Medium High (2)</p>
<p>Normally only one logger is used in each class. This rule supports slf4j, log4j, Java Util Logging and
log4j2 (since 6.19.0).</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/errorprone/MoreThanOneLoggerRule.java">net.sourceforge.pmd.lang.java.rule.errorprone.MoreThanOneLoggerRule</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="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="c1">// It is very rare to see two loggers on a class, normally</span>
<span class="c1">// log information is multiplexed by levels</span>
<span class="nc">Logger</span> <span class="n">log2</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="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/errorprone.xml/MoreThanOneLogger"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="noncaselabelinswitchstatement">NonCaseLabelInSwitchStatement</h2>
<p><strong>Since:</strong> PMD 1.5</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>A non-case label (e.g. a named break/continue label) was present in a switch statement.
This legal, but confusing. It is easy to mix up the case labels and the non-case labels.</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="o">//</span><span class="nt">BlockStatement</span><span class="o">/</span><span class="nt">Statement</span><span class="o">/</span><span class="nt">LabeledStatement</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="nl">mylabel:</span> <span class="c1">// this is legal, but confusing!</span>
<span class="k">break</span><span class="o">;</span>
<span class="k">default</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/errorprone.xml/NonCaseLabelInSwitchStatement"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="nonstaticinitializer">NonStaticInitializer</h2>
<p><strong>Since:</strong> PMD 1.5</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>A non-static initializer block will be called any time a constructor is invoked (just prior to
invoking the constructor). While this is a valid language construct, it is rarely used and is
confusing.</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">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="ow">not</span><span class="p">(</span><span class="kn">ancestor::</span><span class="o">*</span><span class="p">[</span><span class="m">3</span><span class="p">][</span><span class="kn">self::</span><span class="nt">AllocationExpression</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="kn">self::</span><span class="nt">EnumConstant</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">MyClass</span> <span class="o">{</span>
<span class="c1">// this block gets run before any call to a constructor</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">"I am about to construct myself"</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/errorprone.xml/NonStaticInitializer"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="nullassignment">NullAssignment</h2>
<p><strong>Since:</strong> PMD 1.02</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Assigning a "null" to a variable (outside of its declaration) is usually bad form. Sometimes, this type
of assignment is an indication that the programmer doesnt completely understand what is going on in the code.</p>
<p>NOTE: This sort of assignment may used in some cases to dereference objects and encourage garbage collection.</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/errorprone/NullAssignmentRule.java">net.sourceforge.pmd.lang.java.rule.errorprone.NullAssignmentRule</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="kt">void</span> <span class="nf">bar</span><span class="o">()</span> <span class="o">{</span>
<span class="nc">Object</span> <span class="n">x</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span> <span class="c1">// this is OK</span>
<span class="n">x</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">Object</span><span class="o">();</span>
<span class="c1">// big, complex piece of code here</span>
<span class="n">x</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span> <span class="c1">// this is not required</span>
<span class="c1">// big, complex piece of code here</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/errorprone.xml/NullAssignment"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="overridebothequalsandhashcode">OverrideBothEqualsAndHashcode</h2>
<p><strong>Since:</strong> PMD 0.4</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Override both public boolean Object.equals(Object other), and public int Object.hashCode(), or override neither. Even if you are inheriting a hashCode() from a parent class, consider implementing hashCode and explicitly delegating to your superclass.</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/errorprone/OverrideBothEqualsAndHashcodeRule.java">net.sourceforge.pmd.lang.java.rule.errorprone.OverrideBothEqualsAndHashcodeRule</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">Bar</span> <span class="o">{</span> <span class="c1">// poor, missing a hashcode() method</span>
<span class="kd">public</span> <span class="kt">boolean</span> <span class="nf">equals</span><span class="o">(</span><span class="nc">Object</span> <span class="n">o</span><span class="o">)</span> <span class="o">{</span>
<span class="c1">// do some comparison</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">Baz</span> <span class="o">{</span> <span class="c1">// poor, missing an equals() method</span>
<span class="kd">public</span> <span class="kt">int</span> <span class="nf">hashCode</span><span class="o">()</span> <span class="o">{</span>
<span class="c1">// return some hash value</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span> <span class="c1">// perfect, both methods provided</span>
<span class="kd">public</span> <span class="kt">boolean</span> <span class="nf">equals</span><span class="o">(</span><span class="nc">Object</span> <span class="n">other</span><span class="o">)</span> <span class="o">{</span>
<span class="c1">// do some comparison</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kt">int</span> <span class="nf">hashCode</span><span class="o">()</span> <span class="o">{</span>
<span class="c1">// return some hash value</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/errorprone.xml/OverrideBothEqualsAndHashcode"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="propercloneimplementation">ProperCloneImplementation</h2>
<p><strong>Since:</strong> PMD 1.4</p>
<p><strong>Priority:</strong> Medium High (2)</p>
<p>Object clone() should be implemented with super.clone().</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/errorprone/ProperCloneImplementationRule.java">net.sourceforge.pmd.lang.java.rule.errorprone.ProperCloneImplementationRule</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="kd">public</span> <span class="nc">Object</span> <span class="nf">clone</span><span class="o">(){</span>
<span class="k">return</span> <span class="k">new</span> <span class="nf">Foo</span><span class="o">();</span> <span class="c1">// This is bad</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/errorprone.xml/ProperCloneImplementation"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="properlogger">ProperLogger</h2>
<p><strong>Since:</strong> PMD 3.3</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>A logger should normally be defined private static final and be associated with the correct class.
<code class="language-plaintext highlighter-rouge">private final Log log;</code> is also allowed for rare cases where loggers need to be passed around,
with the restriction that the logger needs to be passed into the constructor.</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="w">
</span><span class="p">[</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="nv">$loggerClass</span><span class="p">)]]</span><span class="w">
</span><span class="p">[</span><span class="w">
</span><span class="c">(: check modifiers :)</span><span class="w">
</span><span class="p">(</span><span class="na">@Private</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">or</span><span class="w"> </span><span class="na">@Final</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="c">(: check logger name :)</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="p">(</span><span class="na">@Static</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="nt">VariableDeclarator</span><span class="o">/</span><span class="nt">VariableDeclaratorId</span><span class="p">[</span><span class="na">@Name</span><span class="w"> </span><span class="p">!</span><span class="err">=</span><span class="w"> </span><span class="nv">$staticLoggerName</span><span class="p">])</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="p">(</span><span class="na">@Static</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="nt">VariableDeclarator</span><span class="o">/</span><span class="nt">VariableDeclaratorId</span><span class="p">[</span><span class="na">@Name</span><span class="w"> </span><span class="p">!</span><span class="err">=</span><span class="w"> </span><span class="nv">$loggerName</span><span class="p">])</span><span class="w">
</span><span class="c">(: check logger argument type matches class or enum name :)</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">ArgumentList</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="p">!</span><span class="err">=</span><span class="w"> </span><span class="kn">ancestor::</span><span class="nt">ClassOrInterfaceDeclaration</span><span class="err">/</span><span class="na">@SimpleName</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">ArgumentList</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="p">!</span><span class="err">=</span><span class="w"> </span><span class="kn">ancestor::</span><span class="nt">EnumDeclaration</span><span class="err">/</span><span class="na">@SimpleName</span><span class="p">]</span><span class="w">
</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="c">(: special case - final logger initialized inside constructor :)</span><span class="w">
</span><span class="ow">not</span><span class="p">(</span><span class="nt">VariableDeclarator</span><span class="o">/</span><span class="nt">VariableInitializer</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="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="kn">ancestor::</span><span class="nt">ClassOrInterfaceBody</span><span class="o">//</span><span class="nt">ConstructorDeclaration</span><span class="o">//</span><span class="nt">StatementExpression</span><span class="w">
</span><span class="p">[</span><span class="nt">PrimaryExpression</span><span class="p">[</span><span class="nt">PrimaryPrefix</span><span class="p">[</span><span class="na">@ThisModifier</span><span class="p">]]</span><span class="o">/</span><span class="nt">PrimarySuffix</span><span class="p">[</span><span class="na">@Image</span><span class="err">=</span><span class="nv">$loggerName</span><span class="p">]]</span><span class="w">
</span><span class="p">[</span><span class="nt">AssignmentOperator</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">'='</span><span class="p">]]</span><span class="w">
</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">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="kn">ancestor::</span><span class="nt">ConstructorDeclaration</span><span class="o">//</span><span class="nt">FormalParameter</span><span class="o">/</span><span class="nt">VariableDeclaratorId</span><span class="err">/</span><span class="na">@Name</span><span class="p">]]</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">AllocationExpression</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></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="kd">static</span> <span class="kd">final</span> <span class="nc">Log</span> <span class="no">LOG</span> <span class="o">=</span> <span class="nc">LogFactory</span><span class="o">.</span><span class="na">getLog</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="c1">// proper way</span>
<span class="kd">protected</span> <span class="nc">Log</span> <span class="no">LOG</span> <span class="o">=</span> <span class="nc">LogFactory</span><span class="o">.</span><span class="na">getLog</span><span class="o">(</span><span class="nc">Testclass</span><span class="o">.</span><span class="na">class</span><span class="o">);</span> <span class="c1">// wrong approach</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>staticLoggerName</td>
<td>LOG</td>
<td>Name of the static Logger variable</td>
<td>no</td>
</tr>
<tr>
<td>loggerName</td>
<td>log</td>
<td>Name of the Logger instance variable</td>
<td>no</td>
</tr>
<tr>
<td>loggerClass</td>
<td>Log</td>
<td>Class name of the logger</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/errorprone.xml/ProperLogger"</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/errorprone.xml/ProperLogger"</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">"staticLoggerName"</span> <span class="na">value=</span><span class="s">"LOG"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"loggerName"</span> <span class="na">value=</span><span class="s">"log"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"loggerClass"</span> <span class="na">value=</span><span class="s">"Log"</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="returnemptyarrayratherthannull">ReturnEmptyArrayRatherThanNull</h2>
<p><strong>Since:</strong> PMD 4.2</p>
<p><strong>Priority:</strong> High (1)</p>
<p>For any method that returns an array, it is a better to return an empty array rather than a
null reference. This removes the need for null checking all results and avoids inadvertent
NullPointerExceptions.</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="w">
</span><span class="p">[</span><span class="w">
</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="p">[</span><span class="na">@ArrayType</span><span class="err">=</span><span class="w"> </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="p">(.</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">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">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">Example</span> <span class="o">{</span>
<span class="c1">// Not a good idea...</span>
<span class="kd">public</span> <span class="kt">int</span><span class="o">[]</span> <span class="nf">badBehavior</span><span class="o">()</span> <span class="o">{</span>
<span class="c1">// ...</span>
<span class="k">return</span> <span class="kc">null</span><span class="o">;</span>
<span class="o">}</span>
<span class="c1">// Good behavior</span>
<span class="kd">public</span> <span class="nc">String</span><span class="o">[]</span> <span class="nf">bonnePratique</span><span class="o">()</span> <span class="o">{</span>
<span class="c1">//...</span>
<span class="k">return</span> <span class="k">new</span> <span class="nc">String</span><span class="o">[</span><span class="mi">0</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/errorprone.xml/ReturnEmptyArrayRatherThanNull"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="returnfromfinallyblock">ReturnFromFinallyBlock</h2>
<p><strong>Since:</strong> PMD 1.05</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Avoid returning from a finally block, this can discard exceptions.</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">FinallyStatement</span><span class="o">//</span><span class="nt">ReturnStatement</span><span class="w"> </span><span class="kr">except</span><span class="w"> </span><span class="o">//</span><span class="nt">FinallyStatement</span><span class="err">//</span><span class="p">(</span><span class="nt">MethodDeclaration</span><span class="p">|</span><span class="nt">LambdaExpression</span><span class="p">)</span><span class="o">//</span><span class="nt">ReturnStatement</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">Bar</span> <span class="o">{</span>
<span class="kd">public</span> <span class="nc">String</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="k">throw</span> <span class="k">new</span> <span class="nf">Exception</span><span class="o">(</span> <span class="s">"My 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="k">throw</span> <span class="n">e</span><span class="o">;</span>
<span class="o">}</span> <span class="k">finally</span> <span class="o">{</span>
<span class="k">return</span> <span class="s">"A. O. K."</span><span class="o">;</span> <span class="c1">// return not recommended here</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/errorprone.xml/ReturnFromFinallyBlock"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="simpledateformatneedslocale">SimpleDateFormatNeedsLocale</h2>
<p><strong>Since:</strong> PMD 2.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Be sure to specify a Locale when creating SimpleDateFormat instances to ensure that locale-appropriate
formatting is used.</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="w">
</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">'SimpleDateFormat'</span><span class="p">]]</span><span class="w">
</span><span class="p">[</span><span class="nt">Arguments</span><span class="p">[</span><span class="na">@Size</span><span class="err">=</span><span class="m">1</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="c1">// Should specify Locale.US (or whatever)</span>
<span class="kd">private</span> <span class="nc">SimpleDateFormat</span> <span class="n">sdf</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">SimpleDateFormat</span><span class="o">(</span><span class="s">"pattern"</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/errorprone.xml/SimpleDateFormatNeedsLocale"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="singlemethodsingleton">SingleMethodSingleton</h2>
<p><strong>Since:</strong> PMD 5.4</p>
<p><strong>Priority:</strong> Medium High (2)</p>
<p>Some classes contain overloaded getInstance. The problem with overloaded getInstance methods
is that the instance created using the overloaded method is not cached and so,
for each call and new objects will be created for every invocation.</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/errorprone/SingleMethodSingletonRule.java">net.sourceforge.pmd.lang.java.rule.errorprone.SingleMethodSingletonRule</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">Singleton</span> <span class="o">{</span>
<span class="kd">private</span> <span class="kd">static</span> <span class="nc">Singleton</span> <span class="n">singleton</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">Singleton</span><span class="o">(</span> <span class="o">);</span>
<span class="kd">private</span> <span class="nf">Singleton</span><span class="o">(){</span> <span class="o">}</span>
<span class="kd">public</span> <span class="kd">static</span> <span class="nc">Singleton</span> <span class="nf">getInstance</span><span class="o">(</span> <span class="o">)</span> <span class="o">{</span>
<span class="k">return</span> <span class="n">singleton</span><span class="o">;</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kd">static</span> <span class="nc">Singleton</span> <span class="nf">getInstance</span><span class="o">(</span><span class="nc">Object</span> <span class="n">obj</span><span class="o">){</span>
<span class="nc">Singleton</span> <span class="n">singleton</span> <span class="o">=</span> <span class="o">(</span><span class="nc">Singleton</span><span class="o">)</span> <span class="n">obj</span><span class="o">;</span>
<span class="k">return</span> <span class="n">singleton</span><span class="o">;</span> <span class="c1">//violation</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/errorprone.xml/SingleMethodSingleton"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="singletonclassreturningnewinstance">SingletonClassReturningNewInstance</h2>
<p><strong>Since:</strong> PMD 5.4</p>
<p><strong>Priority:</strong> Medium High (2)</p>
<p>Some classes contain overloaded getInstance. The problem with overloaded getInstance methods
is that the instance created using the overloaded method is not cached and so,
for each call and new objects will be created for every invocation.</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/errorprone/SingletonClassReturningNewInstanceRule.java">net.sourceforge.pmd.lang.java.rule.errorprone.SingletonClassReturningNewInstanceRule</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">Singleton</span> <span class="o">{</span>
<span class="kd">private</span> <span class="kd">static</span> <span class="nc">Singleton</span> <span class="n">instance</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span>
<span class="kd">public</span> <span class="kd">static</span> <span class="nc">Singleton</span> <span class="nf">getInstance</span><span class="o">()</span> <span class="o">{</span>
<span class="kd">synchronized</span><span class="o">(</span><span class="nc">Singleton</span><span class="o">.</span><span class="na">class</span><span class="o">)</span> <span class="o">{</span>
<span class="k">return</span> <span class="k">new</span> <span class="nf">Singleton</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/errorprone.xml/SingletonClassReturningNewInstance"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="staticejbfieldshouldbefinal">StaticEJBFieldShouldBeFinal</h2>
<p><strong>Since:</strong> PMD 4.1</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>According to the J2EE specification, an EJB should not have any static fields
with write access. However, static read-only fields are allowed. This ensures proper
behavior especially when instances are distributed by the container on several JREs.</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="p">(</span><span class="w">
</span><span class="p">(.</span><span class="o">/</span><span class="nt">ImplementsList</span><span class="o">/</span><span class="nt">ClassOrInterfaceType</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="s1">'SessionBean'</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">ImplementsList</span><span class="o">/</span><span class="nt">ClassOrInterfaceType</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="s1">'EJBHome'</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">ImplementsList</span><span class="o">/</span><span class="nt">ClassOrInterfaceType</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="s1">'EJBLocalObject'</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">ImplementsList</span><span class="o">/</span><span class="nt">ClassOrInterfaceType</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="s1">'EJBLocalHome'</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">ExtendsList</span><span class="o">/</span><span class="nt">ClassOrInterfaceType</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="s1">'EJBObject'</span><span class="p">)])</span><span class="w">
</span><span class="p">)</span><span class="w">
</span><span class="ow">and</span><span class="w">
</span><span class="p">(.</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="w">
</span><span class="p">(.</span><span class="o">/</span><span class="nt">FieldDeclaration</span><span class="p">[</span><span class="na">@Static</span><span class="w"> </span><span class="err">=</span><span class="w"> </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="p">(.</span><span class="o">/</span><span class="nt">FieldDeclaration</span><span class="p">[</span><span class="na">@Final</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="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">SomeEJB</span> <span class="kd">extends</span> <span class="nc">EJBObject</span> <span class="kd">implements</span> <span class="nc">EJBLocalHome</span> <span class="o">{</span>
<span class="kd">private</span> <span class="kd">static</span> <span class="kt">int</span> <span class="nc">CountA</span><span class="o">;</span> <span class="c1">// poor, field can be edited</span>
<span class="kd">private</span> <span class="kd">static</span> <span class="kd">final</span> <span class="kt">int</span> <span class="nc">CountB</span><span class="o">;</span> <span class="c1">// preferred, read-only access</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/errorprone.xml/StaticEJBFieldShouldBeFinal"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="stringbufferinstantiationwithchar">StringBufferInstantiationWithChar</h2>
<p><strong>Since:</strong> PMD 3.9</p>
<p><strong>Priority:</strong> Medium Low (4)</p>
<p>Individual character values provided as initialization arguments will be converted into integers.
This can lead to internal buffer sizes that are larger than expected. Some examples:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>new StringBuffer() // 16
new StringBuffer(6) // 6
new StringBuffer("hello world") // 11 + 16 = 27
new StringBuffer('A') // chr(A) = 65
new StringBuffer("A") // 1 + 16 = 17
new StringBuilder() // 16
new StringBuilder(6) // 6
new StringBuilder("hello world") // 11 + 16 = 27
new StringBuilder('C') // chr(C) = 67
new StringBuilder("A") // 1 + 16 = 17
</code></pre></div></div>
<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">ClassOrInterfaceType</span><span class="w">
</span><span class="p">[</span><span class="na">@Image</span><span class="err">=</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="err">=</span><span class="s1">'StringBuilder'</span><span class="p">]</span><span class="w">
</span><span class="err">/</span><span class="p">..</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="w">
</span><span class="o">/</span><span class="nt">PrimaryPrefix</span><span class="o">/</span><span class="w">
</span><span class="nt">Literal</span><span class="w">
</span><span class="p">[</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="s2">"'"</span><span class="p">)]</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="s2">"'"</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">// misleading instantiation, these buffers</span>
<span class="c1">// are actually sized to 99 characters long</span>
<span class="nc">StringBuffer</span> <span class="n">sb1</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">StringBuffer</span><span class="o">(</span><span class="sc">'c'</span><span class="o">);</span>
<span class="nc">StringBuilder</span> <span class="n">sb2</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">StringBuilder</span><span class="o">(</span><span class="sc">'c'</span><span class="o">);</span>
<span class="c1">// in these forms, just single characters are allocated</span>
<span class="nc">StringBuffer</span> <span class="n">sb3</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">StringBuffer</span><span class="o">(</span><span class="s">"c"</span><span class="o">);</span>
<span class="nc">StringBuilder</span> <span class="n">sb4</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">StringBuilder</span><span class="o">(</span><span class="s">"c"</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/errorprone.xml/StringBufferInstantiationWithChar"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="suspiciousequalsmethodname">SuspiciousEqualsMethodName</h2>
<p><strong>Since:</strong> PMD 2.0</p>
<p><strong>Priority:</strong> Medium High (2)</p>
<p>The method name and parameter number are suspiciously close to <code class="language-plaintext highlighter-rouge">Object.equals</code>, which can denote an
intention to override it. However, the method does not override <code class="language-plaintext highlighter-rouge">Object.equals</code>, but overloads it instead.
Overloading <code class="language-plaintext highlighter-rouge">Object.equals</code> method is confusing for other programmers, error-prone and hard to maintain,
especially when using inheritance, because <code class="language-plaintext highlighter-rouge">@Override</code> annotations used in subclasses can provide a false
sense of security. For more information on <code class="language-plaintext highlighter-rouge">Object.equals</code> method, see Effective Java, 3rd Edition,
Item 10: Obey the general contract when overriding equals.</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="w"> </span><span class="err">=</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><span class="p">(</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="ow">and</span><span class="w"> </span><span class="ow">not</span><span class="w"> </span><span class="p">(</span><span class="nt">MethodDeclarator</span><span class="o">/</span><span class="nt">FormalParameters</span><span class="o">/</span><span class="nt">FormalParameter</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="w">
</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">'Object'</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">'java.lang.Object'</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="w"> </span><span class="p">(</span><span class="nt">ResultType</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="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'boolean'</span><span class="p">])</span><span class="w">
</span><span class="p">)</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="p">(</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">2</span><span class="w">
</span><span class="ow">and</span><span class="w"> </span><span class="nt">ResultType</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="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'boolean'</span><span class="p">]</span><span class="w">
</span><span class="ow">and</span><span class="w"> </span><span class="nt">MethodDeclarator</span><span class="o">/</span><span class="nt">FormalParameters</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">'Object'</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">'java.lang.Object'</span><span class="p">]</span><span class="w">
</span><span class="ow">and</span><span class="w"> </span><span class="ow">not</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="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="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">MethodDeclaration</span><span class="p">[</span><span class="na">@Name</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'equal'</span><span class="p">]</span><span class="w">
</span><span class="p">[</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="ow">and</span><span class="w"> </span><span class="nt">MethodDeclarator</span><span class="o">/</span><span class="nt">FormalParameters</span><span class="o">/</span><span class="nt">FormalParameter</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="w">
</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">'Object'</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">'java.lang.Object'</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">int</span> <span class="nf">equals</span><span class="o">(</span><span class="nc">Object</span> <span class="n">o</span><span class="o">)</span> <span class="o">{</span>
<span class="c1">// oops, this probably was supposed to be boolean equals</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kt">boolean</span> <span class="nf">equals</span><span class="o">(</span><span class="nc">String</span> <span class="n">s</span><span class="o">)</span> <span class="o">{</span>
<span class="c1">// oops, this probably was supposed to be equals(Object)</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kt">boolean</span> <span class="nf">equals</span><span class="o">(</span><span class="nc">Object</span> <span class="n">o1</span><span class="o">,</span> <span class="nc">Object</span> <span class="n">o2</span><span class="o">)</span> <span class="o">{</span>
<span class="c1">// oops, this probably was supposed to be equals(Object)</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/errorprone.xml/SuspiciousEqualsMethodName"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="suspicioushashcodemethodname">SuspiciousHashcodeMethodName</h2>
<p><strong>Since:</strong> PMD 1.5</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>The method name and return type are suspiciously close to hashCode(), which may denote an intention
to override the hashCode() 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/errorprone/SuspiciousHashcodeMethodNameRule.java">net.sourceforge.pmd.lang.java.rule.errorprone.SuspiciousHashcodeMethodNameRule</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">int</span> <span class="nf">hashcode</span><span class="o">()</span> <span class="o">{</span> <span class="c1">// oops, this probably was supposed to be 'hashCode'</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/errorprone.xml/SuspiciousHashcodeMethodName"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="suspiciousoctalescape">SuspiciousOctalEscape</h2>
<p><strong>Since:</strong> PMD 1.5</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>A suspicious octal escape sequence was found inside a String literal.
The Java language specification (section 3.10.6) says an octal
escape sequence inside a literal String shall consist of a backslash
followed by:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>OctalDigit | OctalDigit OctalDigit | ZeroToThree OctalDigit OctalDigit
</code></pre></div></div>
<p>Any octal escape sequence followed by non-octal digits can be confusing,
e.g. "\038" is interpreted as the octal escape sequence "\03" followed by
the literal character "8".</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/errorprone/SuspiciousOctalEscapeRule.java">net.sourceforge.pmd.lang.java.rule.errorprone.SuspiciousOctalEscapeRule</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="kt">void</span> <span class="nf">foo</span><span class="o">()</span> <span class="o">{</span>
<span class="c1">// interpreted as octal 12, followed by character '8'</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">"suspicious: \128"</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/errorprone.xml/SuspiciousOctalEscape"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="testclasswithouttestcases">TestClassWithoutTestCases</h2>
<p><strong>Since:</strong> PMD 3.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Test classes end with the suffix Test. Having a non-test class with that name is not a good practice,
since most people will assume it is a test case. Test classes have test methods named testXXX.</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/errorprone/TestClassWithoutTestCasesRule.java">net.sourceforge.pmd.lang.java.rule.errorprone.TestClassWithoutTestCasesRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">//Consider changing the name of the class if it is not a test</span>
<span class="c1">//Consider adding test methods if it is a test</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">CarTest</span> <span class="o">{</span>
<span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="o">(</span><span class="nc">String</span><span class="o">[]</span> <span class="n">args</span><span class="o">)</span> <span class="o">{</span>
<span class="c1">// do something</span>
<span class="o">}</span>
<span class="c1">// code</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/errorprone.xml/TestClassWithoutTestCases"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="unconditionalifstatement">UnconditionalIfStatement</h2>
<p><strong>Since:</strong> PMD 1.5</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Do not use "if" statements whose conditionals are always true or always false.</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">IfStatement</span><span class="o">/</span><span class="nt">Expression</span><span class="w">
</span><span class="p">[</span><span class="nf">count</span><span class="p">(</span><span class="nt">PrimaryExpression</span><span class="p">)</span><span class="err">=</span><span class="m">1</span><span class="p">]</span><span class="w">
</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></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">close</span><span class="o">()</span> <span class="o">{</span>
<span class="k">if</span> <span class="o">(</span><span class="kc">true</span><span class="o">)</span> <span class="o">{</span> <span class="c1">// fixed conditional, not recommended</span>
<span class="c1">// ...</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/errorprone.xml/UnconditionalIfStatement"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="unnecessarybooleanassertion">UnnecessaryBooleanAssertion</h2>
<p><strong>Since:</strong> PMD 3.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>A JUnit test assertion with a boolean literal is unnecessary since it always will evaluate to the same thing.
Consider using flow control (in case of assertTrue(false) or similar) or simply removing
statements like assertTrue(true) and assertFalse(false). If you just want a test to halt after finding
an error, use the fail() method and provide an indication message of why it did.</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">StatementExpression</span><span class="w">
</span><span class="p">[</span><span class="w">
</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="p">[</span><span class="na">@Image</span><span class="err">=</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="err">=</span><span class="s1">'assertFalse'</span><span class="p">]</span><span class="w">
</span><span class="ow">and</span><span class="w">
</span><span class="nt">PrimaryExpression</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="p">[</span><span class="w">
</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">UnaryExpressionNotPlusMinus</span><span class="p">[</span><span class="na">@Image</span><span class="err">=</span><span class="s1">'!'</span><span class="p">]</span><span class="w">
</span><span class="o">/</span><span class="nt">PrimaryExpression</span><span class="o">/</span><span class="nt">PrimaryPrefix</span><span class="p">[</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">Name</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="p">)</span><span class="err">=</span><span class="m">1</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></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">SimpleTest</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">testX</span><span class="o">()</span> <span class="o">{</span>
<span class="n">assertTrue</span><span class="o">(</span><span class="kc">true</span><span class="o">);</span> <span class="c1">// serves no real purpose</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/errorprone.xml/UnnecessaryBooleanAssertion"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="unnecessarycasechange">UnnecessaryCaseChange</h2>
<p><strong>Since:</strong> PMD 3.3</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Using equalsIgnoreCase() is faster than using toUpperCase/toLowerCase().equals()</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/errorprone/UnnecessaryCaseChangeRule.java">net.sourceforge.pmd.lang.java.rule.errorprone.UnnecessaryCaseChangeRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">boolean</span> <span class="n">answer1</span> <span class="o">=</span> <span class="n">buz</span><span class="o">.</span><span class="na">toUpperCase</span><span class="o">().</span><span class="na">equals</span><span class="o">(</span><span class="s">"baz"</span><span class="o">);</span> <span class="c1">// should be buz.equalsIgnoreCase("baz")</span>
<span class="kt">boolean</span> <span class="n">answer2</span> <span class="o">=</span> <span class="n">buz</span><span class="o">.</span><span class="na">toUpperCase</span><span class="o">().</span><span class="na">equalsIgnoreCase</span><span class="o">(</span><span class="s">"baz"</span><span class="o">);</span> <span class="c1">// another unnecessary toUpperCase()</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/errorprone.xml/UnnecessaryCaseChange"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="unnecessaryconversiontemporary">UnnecessaryConversionTemporary</h2>
<p><strong>Since:</strong> PMD 0.1</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Avoid the use temporary objects when converting primitives to Strings. Use the static conversion methods
on the wrapper classes 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/errorprone/UnnecessaryConversionTemporaryRule.java">net.sourceforge.pmd.lang.java.rule.errorprone.UnnecessaryConversionTemporaryRule</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="nc">String</span> <span class="nf">convert</span><span class="o">(</span><span class="kt">int</span> <span class="n">x</span><span class="o">)</span> <span class="o">{</span>
<span class="nc">String</span> <span class="n">foo</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">Integer</span><span class="o">(</span><span class="n">x</span><span class="o">).</span><span class="na">toString</span><span class="o">();</span> <span class="c1">// this wastes an object</span>
<span class="k">return</span> <span class="nc">Integer</span><span class="o">.</span><span class="na">toString</span><span class="o">(</span><span class="n">x</span><span class="o">);</span> <span class="c1">// preferred approach</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/errorprone.xml/UnnecessaryConversionTemporary"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="unusednullcheckinequals">UnusedNullCheckInEquals</h2>
<p><strong>Since:</strong> PMD 3.5</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>After checking an object reference for null, you should invoke equals() on that object rather than passing it to another objects equals() method.</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="p">(</span><span class="o">//</span><span class="nt">PrimaryPrefix</span><span class="p">[</span><span class="nf">ends-with</span><span class="p">(</span><span class="nt">Name</span><span class="err">/</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="ow">and</span><span class="w"> </span><span class="nt">Name</span><span class="err">/</span><span class="na">@Image</span><span class="w"> </span><span class="p">!</span><span class="err">=</span><span class="w"> </span><span class="s1">'Arrays.equals'</span><span class="p">]</span><span class="w"> </span><span class="p">|</span><span class="w"> </span><span class="o">//</span><span class="nt">PrimarySuffix</span><span class="p">[</span><span class="na">@Image</span><span class="err">=</span><span class="s1">'equals'</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="ow">not</span><span class="p">(..</span><span class="o">/</span><span class="nt">PrimaryPrefix</span><span class="o">/</span><span class="nt">Literal</span><span class="p">)])</span><span class="w">
</span><span class="o">/</span><span class="kn">following-sibling::</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="w">
</span><span class="o">/</span><span class="nt">PrimaryExpression</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="nt">PrimarySuffix</span><span class="p">)]</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="p">.</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="o">/</span><span class="nt">Expression</span><span class="o">/</span><span class="nt">ConditionalAndExpression</span><span class="w">
</span><span class="o">/</span><span class="nt">EqualityExpression</span><span class="p">[</span><span class="na">@Image</span><span class="err">=</span><span class="s2">"!="</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="ow">not</span><span class="p">(.</span><span class="o">/</span><span class="kn">preceding-sibling::</span><span class="err">*</span><span class="p">)</span><span class="w"> </span><span class="ow">and</span><span class="w">
</span><span class="p">.</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="p">]</span><span class="w">
</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="err">/</span><span class="na">@Image</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">Test</span> <span class="o">{</span>
<span class="kd">public</span> <span class="nc">String</span> <span class="nf">method1</span><span class="o">()</span> <span class="o">{</span> <span class="k">return</span> <span class="s">"ok"</span><span class="o">;}</span>
<span class="kd">public</span> <span class="nc">String</span> <span class="nf">method2</span><span class="o">()</span> <span class="o">{</span> <span class="k">return</span> <span class="kc">null</span><span class="o">;}</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">method</span><span class="o">(</span><span class="nc">String</span> <span class="n">a</span><span class="o">)</span> <span class="o">{</span>
<span class="nc">String</span> <span class="n">b</span><span class="o">;</span>
<span class="c1">// I don't know it method1() can be "null"</span>
<span class="c1">// but I know "a" is not null..</span>
<span class="c1">// I'd better write a.equals(method1())</span>
<span class="k">if</span> <span class="o">(</span><span class="n">a</span><span class="o">!=</span><span class="kc">null</span> <span class="o">&amp;&amp;</span> <span class="n">method1</span><span class="o">().</span><span class="na">equals</span><span class="o">(</span><span class="n">a</span><span class="o">))</span> <span class="o">{</span> <span class="c1">// will trigger the rule</span>
<span class="c1">//whatever</span>
<span class="o">}</span>
<span class="k">if</span> <span class="o">(</span><span class="n">method1</span><span class="o">().</span><span class="na">equals</span><span class="o">(</span><span class="n">a</span><span class="o">)</span> <span class="o">&amp;&amp;</span> <span class="n">a</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span> <span class="c1">// won't trigger the rule</span>
<span class="c1">//whatever</span>
<span class="o">}</span>
<span class="k">if</span> <span class="o">(</span><span class="n">a</span><span class="o">!=</span><span class="kc">null</span> <span class="o">&amp;&amp;</span> <span class="n">method1</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="o">{</span> <span class="c1">// won't trigger the rule</span>
<span class="c1">//whatever</span>
<span class="o">}</span>
<span class="k">if</span> <span class="o">(</span><span class="n">a</span><span class="o">!=</span><span class="kc">null</span> <span class="o">&amp;&amp;</span> <span class="s">"LITERAL"</span><span class="o">.</span><span class="na">equals</span><span class="o">(</span><span class="n">a</span><span class="o">))</span> <span class="o">{</span> <span class="c1">// won't trigger the rule</span>
<span class="c1">//whatever</span>
<span class="o">}</span>
<span class="k">if</span> <span class="o">(</span><span class="n">a</span><span class="o">!=</span><span class="kc">null</span> <span class="o">&amp;&amp;</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="s">"go"</span><span class="o">))</span> <span class="o">{</span> <span class="c1">// won't trigger the rule</span>
<span class="n">a</span><span class="o">=</span><span class="n">method2</span><span class="o">();</span>
<span class="k">if</span> <span class="o">(</span><span class="n">method1</span><span class="o">().</span><span class="na">equals</span><span class="o">(</span><span class="n">a</span><span class="o">))</span> <span class="o">{</span>
<span class="c1">//whatever</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/errorprone.xml/UnusedNullCheckInEquals"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="usecorrectexceptionlogging">UseCorrectExceptionLogging</h2>
<p><strong>Since:</strong> PMD 3.2</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>To make sure the full stacktrace is printed out, use the logging statement with two arguments: a String and a Throwable.</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">CatchStatement</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">StatementExpression</span><span class="w">
</span><span class="o">/</span><span class="nt">PrimaryExpression</span><span class="w">
</span><span class="p">[</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">starts-with</span><span class="p">(</span><span class="na">@Image</span><span class="p">,</span><span class="w">
</span><span class="nf">concat</span><span class="p">((</span><span class="kn">ancestor::</span><span class="nt">ClassOrInterfaceDeclaration</span><span class="o">/</span><span class="nt">ClassOrInterfaceBody</span><span class="o">/</span><span class="nt">ClassOrInterfaceBodyDeclaration</span><span class="o">/</span><span class="nt">FieldDeclaration</span><span class="w">
</span><span class="p">[</span><span class="nt">Type</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">'Log'</span><span class="p">]]</span><span class="w">
</span><span class="o">/</span><span class="nt">VariableDeclarator</span><span class="o">/</span><span class="nt">VariableDeclaratorId</span><span class="err">/</span><span class="na">@Name</span><span class="p">)[</span><span class="m">1</span><span class="p">],</span><span class="w"> </span><span class="s1">'.'</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="p">[</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="w"> </span><span class="m">1</span><span class="p">]]</span><span class="w">
</span><span class="p">[</span><span class="nt">PrimarySuffix</span><span class="o">/</span><span class="nt">Arguments</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="kn">ancestor::</span><span class="nt">CatchStatement</span><span class="o">/</span><span class="nt">FormalParameter</span><span class="o">/</span><span class="nt">VariableDeclaratorId</span><span class="err">/</span><span class="na">@Name</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">Main</span> <span class="o">{</span>
<span class="kd">private</span> <span class="kd">static</span> <span class="kd">final</span> <span class="nc">Log</span> <span class="n">_LOG</span> <span class="o">=</span> <span class="nc">LogFactory</span><span class="o">.</span><span class="na">getLog</span><span class="o">(</span> <span class="nc">Main</span><span class="o">.</span><span class="na">class</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="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">_LOG</span><span class="o">.</span><span class="na">error</span><span class="o">(</span> <span class="n">e</span> <span class="o">);</span> <span class="c1">//Wrong!</span>
<span class="o">}</span> <span class="k">catch</span><span class="o">(</span> <span class="nc">OtherException</span> <span class="n">oe</span> <span class="o">)</span> <span class="o">{</span>
<span class="n">_LOG</span><span class="o">.</span><span class="na">error</span><span class="o">(</span> <span class="n">oe</span><span class="o">.</span><span class="na">getMessage</span><span class="o">(),</span> <span class="n">oe</span> <span class="o">);</span> <span class="c1">//Correct</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/errorprone.xml/UseCorrectExceptionLogging"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="useequalstocomparestrings">UseEqualsToCompareStrings</h2>
<p><strong>Since:</strong> PMD 4.1</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Using == or != to compare strings only works if intern version is used on both sides.
Use the equals() method 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">EqualityExpression</span><span class="o">/</span><span class="nt">PrimaryExpression</span><span class="w">
</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="nt">PrimarySuffix</span><span class="p">)]</span><span class="w">
</span><span class="p">[</span><span class="nt">PrimaryPrefix</span><span class="o">/</span><span class="nt">Literal</span><span class="w">
</span><span class="p">[</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">'"'</span><span class="p">)]</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">'"'</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="kt">boolean</span> <span class="nf">test</span><span class="o">(</span><span class="nc">String</span> <span class="n">s</span><span class="o">)</span> <span class="o">{</span>
<span class="k">if</span> <span class="o">(</span><span class="n">s</span> <span class="o">==</span> <span class="s">"one"</span><span class="o">)</span> <span class="k">return</span> <span class="kc">true</span><span class="o">;</span> <span class="c1">// unreliable</span>
<span class="k">if</span> <span class="o">(</span><span class="s">"two"</span><span class="o">.</span><span class="na">equals</span><span class="o">(</span><span class="n">s</span><span class="o">))</span> <span class="k">return</span> <span class="kc">true</span><span class="o">;</span> <span class="c1">// better</span>
<span class="k">return</span> <span class="kc">false</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/errorprone.xml/UseEqualsToCompareStrings"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="uselessoperationonimmutable">UselessOperationOnImmutable</h2>
<p><strong>Since:</strong> PMD 3.5</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>An operation on an Immutable object (String, BigDecimal or BigInteger) wont change the object itself
since the result of the operation is a new object. Therefore, ignoring the operation result is an error.</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/errorprone/UselessOperationOnImmutableRule.java">net.sourceforge.pmd.lang.java.rule.errorprone.UselessOperationOnImmutableRule</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.math.*</span><span class="o">;</span>
<span class="kd">class</span> <span class="nc">Test</span> <span class="o">{</span>
<span class="kt">void</span> <span class="nf">method1</span><span class="o">()</span> <span class="o">{</span>
<span class="nc">BigDecimal</span> <span class="n">bd</span><span class="o">=</span><span class="k">new</span> <span class="nc">BigDecimal</span><span class="o">(</span><span class="mi">10</span><span class="o">);</span>
<span class="n">bd</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="k">new</span> <span class="nc">BigDecimal</span><span class="o">(</span><span class="mi">5</span><span class="o">));</span> <span class="c1">// this will trigger the rule</span>
<span class="o">}</span>
<span class="kt">void</span> <span class="nf">method2</span><span class="o">()</span> <span class="o">{</span>
<span class="nc">BigDecimal</span> <span class="n">bd</span><span class="o">=</span><span class="k">new</span> <span class="nc">BigDecimal</span><span class="o">(</span><span class="mi">10</span><span class="o">);</span>
<span class="n">bd</span> <span class="o">=</span> <span class="n">bd</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="k">new</span> <span class="nc">BigDecimal</span><span class="o">(</span><span class="mi">5</span><span class="o">));</span> <span class="c1">// this won't trigger the rule</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/errorprone.xml/UselessOperationOnImmutable"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="uselocalewithcaseconversions">UseLocaleWithCaseConversions</h2>
<p><strong>Since:</strong> PMD 2.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>When doing <code class="language-plaintext highlighter-rouge">String::toLowerCase()/toUpperCase()</code> conversions, use an explicit locale argument to specify the case transformation rules.</p>
<p>Using <code class="language-plaintext highlighter-rouge">String::toLowerCase()</code> without arguments implicitly uses <code class="language-plaintext highlighter-rouge">Locale::getDefault()</code>.
The problem is that the default locale depends on the current JVM setup (and usually on the system in which it is running).
Using the system default may be exactly what you want (e.g. if you are manipulating strings you got through standard input),
but it may as well not be the case (e.g. if you are getting the string over the network or a file, and the encoding is well-defined
and independent of the environment). In the latter case, using the default locale makes the case transformation brittle, as
it may yield unexpected results on a machine whose locale has other case translation rules. For example, in Turkish, the
uppercase form of <code class="language-plaintext highlighter-rouge">i</code> is <code class="language-plaintext highlighter-rouge">İ</code> (U+0130, not ASCII) and not <code class="language-plaintext highlighter-rouge">I</code> (U+0049) as in English.</p>
<p>The rule is intended to <em>force</em> developers to think about locales when dealing with strings. By taking a conscious decision about
the choice of locale at the time of writing, you reduce the risk of surprising behaviour down the line, and communicate your intent
to future readers.</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="w">
</span><span class="p">[</span><span class="w">
</span><span class="nt">PrimaryPrefix</span><span class="w">
</span><span class="p">[</span><span class="nt">Name</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">'toLowerCase'</span><span class="p">)</span><span class="w"> </span><span class="ow">or</span><span class="w"> </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">'toUpperCase'</span><span class="p">)]]</span><span class="w">
</span><span class="p">[</span><span class="kn">following-sibling::</span><span class="nt">PrimarySuffix</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="m">1</span><span class="p">]</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="ow">or</span><span class="w">
</span><span class="nt">PrimarySuffix</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">'toLowerCase'</span><span class="p">)</span><span class="w"> </span><span class="ow">or</span><span class="w"> </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">'toUpperCase'</span><span class="p">)]</span><span class="w">
</span><span class="p">[</span><span class="kn">following-sibling::</span><span class="nt">PrimarySuffix</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="m">1</span><span class="p">]</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><span class="p">[</span><span class="ow">not</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="nf">ends-with</span><span class="p">(</span><span class="na">@Image</span><span class="p">,</span><span class="w"> </span><span class="s1">'toHexString'</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">// violation - implicitly system-dependent conversion</span>
<span class="k">if</span> <span class="o">(</span><span class="n">x</span><span class="o">.</span><span class="na">toLowerCase</span><span class="o">().</span><span class="na">equals</span><span class="o">(</span><span class="s">"list"</span><span class="o">))</span> <span class="o">{}</span>
<span class="c1">// The above will not match "LIST" on a system with a Turkish locale.</span>
<span class="c1">// It could be replaced with</span>
<span class="k">if</span> <span class="o">(</span><span class="n">x</span><span class="o">.</span><span class="na">toLowerCase</span><span class="o">(</span><span class="nc">Locale</span><span class="o">.</span><span class="na">US</span><span class="o">).</span><span class="na">equals</span><span class="o">(</span><span class="s">"list"</span><span class="o">))</span> <span class="o">{</span> <span class="o">}</span>
<span class="c1">// or simply</span>
<span class="k">if</span> <span class="o">(</span><span class="n">x</span><span class="o">.</span><span class="na">equalsIgnoreCase</span><span class="o">(</span><span class="s">"list"</span><span class="o">))</span> <span class="o">{</span> <span class="o">}</span>
<span class="c1">// ok - system independent conversion</span>
<span class="nc">String</span> <span class="n">z</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="na">toLowerCase</span><span class="o">(</span><span class="nc">Locale</span><span class="o">.</span><span class="na">ROOT</span><span class="o">);</span>
<span class="c1">// ok - explicit system-dependent conversion</span>
<span class="nc">String</span> <span class="n">z2</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="na">toLowerCase</span><span class="o">(</span><span class="nc">Locale</span><span class="o">.</span><span class="na">getDefault</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/errorprone.xml/UseLocaleWithCaseConversions"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="useproperclassloader">UseProperClassLoader</h2>
<p><strong>Since:</strong> PMD 3.7</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>In J2EE, the getClassLoader() method might not work as expected. Use
Thread.currentThread().getContextClassLoader() 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">PrimarySuffix</span><span class="p">[</span><span class="na">@Image</span><span class="err">=</span><span class="s1">'getClassLoader'</span><span class="p">]</span><span class="w"> </span><span class="p">|</span><span class="w"> </span><span class="o">//</span><span class="nt">PrimaryPrefix</span><span class="p">[</span><span class="nf">ends-with</span><span class="p">(</span><span class="nt">Name</span><span class="err">/</span><span class="na">@Image</span><span class="p">,</span><span class="w"> </span><span class="s1">'.getClassLoader'</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="nc">ClassLoader</span> <span class="n">cl</span> <span class="o">=</span> <span class="nc">Bar</span><span class="o">.</span><span class="na">class</span><span class="o">.</span><span class="na">getClassLoader</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/errorprone.xml/UseProperClassLoader"</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;2020 PMD Open Source Project. All rights reserved. <br />
Site last generated: Nov 12, 2020 <br />
<p><img src="images/pmd-logo-small.png" alt="Company logo"/></p>
</div>
</div>
</footer>
</div>
<!-- /.row -->
</div>
<!-- /.container -->
</div>
</div>
</body>
</html>