pmd/pmd_rules_java_errorprone.html

5388 lines
346 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, BrokenNullCheck, CallSuperFirst, CallSuperLast, CheckSkipResult, ClassCastExceptionWithToArray, CloneMethodMustBePublic, CloneMethodMustImplementCloneable, CloneMethodReturnTypeMustMatchClassName, CloseResource, CompareObjectsWithEquals, ComparisonWithNaN, ConfusingArgumentToVarargsMethod, ConstructorCallsOverridableMethod, DetachedTestCase, DoNotCallGarbageCollectionExplicitly, DoNotExtendJavaLangThrowable, DoNotHardCodeSDCard, DoNotTerminateVM, DoNotThrowExceptionInFinally, DontImportSun, DontUseFloatTypeForLoopIndices, EmptyCatchBlock, EmptyFinalizer, EqualsNull, FinalizeDoesNotCallSuperFinalize, FinalizeOnlyCallsSuperFinalize, FinalizeOverloaded, FinalizeShouldBeProtected, IdempotentOperations, ImplicitSwitchFallThrough, InstantiationToGetClass, InvalidLogMessageFormat, JumbledIncrementer, JUnitSpelling, JUnitStaticSuite, MethodWithSameNameAsEnclosingClass, MisplacedNullCheck, MissingSerialVersionUID, MissingStaticMethodInNonInstantiatableClass, MoreThanOneLogger, NonCaseLabelInSwitchStatement, NonSerializableClass, NonStaticInitializer, NullAssignment, OverrideBothEqualsAndHashcode, ProperCloneImplementation, ProperLogger, ReturnEmptyCollectionRatherThanNull, 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" type="text/css" href="assets/fontawesome-free-5.15.4-web/css/all.min.css">
<link rel="stylesheet" type="text/css" href="assets/bootstrap-4.5.2-dist/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="css/syntax.css">
<link rel="stylesheet" type="text/css" href="css/modern-business.css">
<link rel="stylesheet" type="text/css" href="css/customstyles.css">
<link rel="stylesheet" type="text/css" href="css/theme-green.css">
<link rel="stylesheet" type="text/css" href="css/pmd-customstyles.css">
<link rel="shortcut icon" href="images/logo/favicon.ico" type="image/x-icon">
<link rel="icon" href="images/logo/favicon.ico" type="image/x-icon">
<link rel="alternate" type="application/rss+xml" title="" href="feed.xml">
</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-expand-lg fixed-top navbar-dark">
<div class="container topnavlinks">
<a class="navbar-brand fas fa-home fa-lg" href="index.html">&nbsp;<span class="projectTitle"> PMD Source Code Analyzer Project</span></a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto mt-2 mt-lg-0"></ul>
<ul class="navbar-nav">
<!-- toggle sidebar button -->
<li class="nav-item"><a id="tg-sb-link" class="nav-link" href="#"><i id="tg-sb-icon" class="fas fa-toggle-on"></i> Nav</a></li>
<!-- entries without drop-downs appear here -->
<li class="nav-item"><a class="nav-link" href="https://github.com/pmd/pmd/releases/latest" target="_blank">Download</a></li>
<li class="nav-item"><a class="nav-link" 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.-->
</ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="search" placeholder="search..." id="search-input">
<ul id="results-container"></ul>
</form>
</div>
</div>
</nav>
<!-- Page Content -->
<div class="container-toc-wrapper">
<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 7.4.0-SNAPSHOT</li>
<div class="sidebarTitleDate">Release date: 26-July-2024</div>
<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_release_notes_pmd7.html">Release notes (PMD 7)</a></li>
<li><a href="pmd_about_help.html">Getting help</a></li>
<li><a href="pmd_about_release_policies.html">Release policies</a></li>
<li><a href="pmd_about_support_lifecycle.html">Support lifecycle</a></li>
</ul>
</li>
<li>
<a href="#">User Documentation</a>
<ul>
<li><a href="pmd_userdocs_migrating_to_pmd7.html">Migration Guide for PMD 7</a></li>
<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><a href="pmd_userdocs_3rdpartyrulesets.html">3rd party rulesets</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_rule_guidelines.html">Rule guidelines</a></li>
<li><a href="pmd_userdocs_extending_testing.html">Testing your rules</a></li>
<li><a href="pmd_userdocs_extending_ast_dump.html">Creating (XML) dump of the AST</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_bld.html">bld PMD Extension</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="#">HTML Rules</a>
<ul>
<li><a href="pmd_rules_html.html">Index</a></li>
<li><a href="pmd_rules_html_bestpractices.html">Best Practices</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="#">JavaScript 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="#">Kotlin Rules</a>
<ul>
<li><a href="pmd_rules_kotlin.html">Index</a></li>
<li><a href="pmd_rules_kotlin_bestpractices.html">Best Practices</a></li>
<li><a href="pmd_rules_kotlin_errorprone.html">Error Prone</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_visualforce.html">Index</a></li>
<li><a href="pmd_rules_visualforce_security.html">Security</a></li>
</ul>
</li>
<li class="subfolders">
<a href="#">Scala Rules</a>
<ul>
<li><a href="pmd_rules_scala.html">Index</a></li>
</ul>
</li>
<li class="subfolders">
<a href="#">Swift Rules</a>
<ul>
<li><a href="pmd_rules_swift.html">Index</a></li>
<li><a href="pmd_rules_swift_bestpractices.html">Best Practices</a></li>
<li><a href="pmd_rules_swift_errorprone.html">Error Prone</a></li>
</ul>
</li>
<li class="subfolders">
<a href="#">Velocity Template Language (VTL) Rules</a>
<ul>
<li><a href="pmd_rules_velocity.html">Index</a></li>
<li><a href="pmd_rules_velocity_bestpractices.html">Best Practices</a></li>
<li><a href="pmd_rules_velocity_design.html">Design</a></li>
<li><a href="pmd_rules_velocity_errorprone.html">Error Prone</a></li>
</ul>
</li>
<li class="subfolders">
<a href="#">WSDL Rules</a>
<ul>
<li><a href="pmd_rules_wsdl.html">Index</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_bestpractices.html">Best Practices</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_index.html">Overview</a></li>
<li><a href="pmd_languages_configuration.html">Language configuration</a></li>
<li><a href="pmd_languages_apex.html">Apex</a></li>
<li><a href="pmd_languages_cpp.html">C/C++</a></li>
<li><a href="pmd_languages_cs.html">C#</a></li>
<li><a href="pmd_languages_coco.html">Coco</a></li>
<li><a href="pmd_languages_dart.html">Dart</a></li>
<li><a href="pmd_languages_fortran.html">Fortran</a></li>
<li><a href="pmd_languages_gherkin.html">Gherkin</a></li>
<li><a href="pmd_languages_go.html">Go</a></li>
<li><a href="pmd_languages_html.html">HTML</a></li>
<li><a href="pmd_languages_java.html">Java</a></li>
<li><a href="pmd_languages_js_ts.html">JavaScript / TypeScript</a></li>
<li><a href="pmd_languages_jsp.html">JSP</a></li>
<li><a href="pmd_languages_julia.html">Julia</a></li>
<li><a href="pmd_languages_kotlin.html">Kotlin</a></li>
<li><a href="pmd_languages_lua.html">Lua</a></li>
<li><a href="pmd_languages_matlab.html">Matlab</a></li>
<li><a href="pmd_languages_modelica.html">Modelica</a></li>
<li><a href="pmd_languages_objectivec.html">Objective-C</a></li>
<li><a href="pmd_languages_perl.html">Perl</a></li>
<li><a href="pmd_languages_php.html">PHP</a></li>
<li><a href="pmd_languages_plsql.html">PLSQL</a></li>
<li><a href="pmd_languages_python.html">Python</a></li>
<li><a href="pmd_languages_ruby.html">Ruby</a></li>
<li><a href="pmd_languages_scala.html">Scala</a></li>
<li><a href="pmd_languages_swift.html">Swift</a></li>
<li><a href="pmd_languages_tsql.html">T-SQL</a></li>
<li><a href="pmd_languages_visualforce.html">Visualforce</a></li>
<li><a href="pmd_languages_velocity.html">Velocity Template Language (VTL)</a></li>
<li><a href="pmd_languages_xml.html">XML and XML dialects</a></li>
</ul>
</li>
<li>
<a href="#">Developer Documentation</a>
<ul>
<li><a href="pmd_devdocs_development.html">Developer resources</a></li>
<li><a href="pmd_devdocs_building.html">Building PMD from source</a></li>
<li><a href="https://github.com/pmd/pmd/blob/master/CONTRIBUTING.md" target="_blank">Contributing</a></li>
<li><a href="pmd_devdocs_writing_documentation.html">Writing documentation</a></li>
<li><a href="pmd_devdocs_roadmap.html">Roadmap</a></li>
<li><a href="pmd_devdocs_how_pmd_works.html">How PMD works</a></li>
<li><a href="pmd_devdocs_pmdtester.html">Pmdtester</a></li>
<li><a href="pmd_devdocs_rule_deprecation_policy.html">Rule Deprecation Policy</a></li>
<li class="subfolders">
<a href="#">Major contributions</a>
<ul>
<li><a href="pmd_devdocs_major_rule_guidelines.html">Rule Guidelines</a></li>
<li><a href="pmd_devdocs_major_adding_new_language_javacc.html">Adding a new language (JavaCC)</a></li>
<li><a href="pmd_devdocs_major_adding_new_language_antlr.html">Adding a new language (ANTLR)</a></li>
<li><a href="pmd_devdocs_major_adding_new_cpd_language.html">Adding a new CPD language</a></li>
</ul>
</li>
<li class="subfolders">
<a href="#">Experimental features</a>
<ul>
<li><a href="tag_experimental.html">List of experimental Features</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_logo.html">Logo</a></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><a href="pmd_projectdocs_decisions.html">Decisions</a></li>
<li class="subfolders">
<a href="#">Project management</a>
<ul>
<li><a href="pmd_projectdocs_committers_infrastructure.html">Infrastructure</a></li>
<li><a href="pmd_projectdocs_committers_releasing.html">Release process</a></li>
<li><a href="pmd_projectdocs_committers_merging_pull_requests.html">Merging pull requests</a></li>
<li><a href="pmd_projectdocs_committers_main_landing_page.html">Main Landing page</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
<!-- Content Column -->
<div class="col-md-9" id="tg-sb-content">
<header>
<div class="row">
<div class="col-lg-12">
<a href="./" role="button"
><i class="fa fa-home fa-lg"></i
></a>
» Error Prone
<a
target="_blank"
href="https://github.com/pmd/pmd/blob/master/docs/../pmd-java/src/main/resources/category/java/errorprone.xml"
class="float-right"
role="button"
><i class="fab fa-github fa-lg"></i> Edit on GitHub</a
>
</div>
</div>
<hr />
</header>
<div class="post-header">
<h1 class="post-title-main">Error Prone</h1>
</div>
<div class="post-content" data-github-edit-url="https://github.com/pmd/pmd/blob/master/docs/../pmd-java/src/main/resources/category/java/errorprone.xml">
<div class="summary">Rules to detect constructs that are either broken, extremely confusing or prone to runtime errors.</div>
<details id="inline-toc-details">
<summary>Table of Contents</summary>
<div id="inline-toc"><!-- empty, move TOC here when screen size too small --></div>
</details>
<!-- 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>
</tr>
</thead>
<tbody>
<tr>
<td>allowIf</td>
<td>false</td>
<td>Allow assignment within the conditional expression of an if statement</td>
</tr>
<tr>
<td>allowFor</td>
<td>false</td>
<td>Allow assignment within the conditional expression of a for statement</td>
</tr>
<tr>
<td>allowWhile</td>
<td>false</td>
<td>Allow assignment within the conditional expression of a while statement</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>
</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 <code class="language-plaintext highlighter-rouge">getDeclaredConstructors()</code>, <code class="language-plaintext highlighter-rouge">getDeclaredMethods()</code>, and <code class="language-plaintext highlighter-rouge">getDeclaredFields()</code> also
return private constructors, methods and fields. These can be made accessible by calling <code class="language-plaintext highlighter-rouge">setAccessible(true)</code>.
This gives access to normally protected data which violates the principle of encapsulation.</p>
<p>This rule detects calls to <code class="language-plaintext highlighter-rouge">setAccessible</code> and finds possible accessibility alterations.
If the call to <code class="language-plaintext highlighter-rouge">setAccessible</code> is wrapped within a <code class="language-plaintext highlighter-rouge">PrivilegedAction</code>, then the access alteration
is assumed to be deliberate and is not reported.</p>
<p>Note that with Java 17 the Security Manager, which is used for <code class="language-plaintext highlighter-rouge">PrivilegedAction</code> execution,
is deprecated: <a href="https://openjdk.org/jeps/411">JEP 411: Deprecate the Security Manager for Removal</a>.
For future-proof code, deliberate access alteration should be suppressed using the usual
suppression methods (e.g. by using <code class="language-plaintext highlighter-rouge">@SuppressWarnings</code> annotation).</p>
<p><strong>This rule is defined by the following XPath expression:</strong></p>
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">MethodCall</span><span class="p">[</span><span class="w">
</span><span class="nn">pmd-java:</span><span class="nf">matchesSig</span><span class="p">(</span><span class="s2">"java.lang.reflect.AccessibleObject#setAccessible(boolean)"</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">matchesSig</span><span class="p">(</span><span class="s2">"_#setAccessible(java.lang.reflect.AccessibleObject[],boolean)"</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">ArgumentList</span><span class="o">/</span><span class="nt">BooleanLiteral</span><span class="p">[</span><span class="na">@True</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="nf">false</span><span class="p">()])]</span><span class="w">
</span><span class="c">(: exclude anonymous privileged action classes :)</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">ConstructorCall</span><span class="p">[</span><span class="m">1</span><span class="p">][</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'java.security.PrivilegedAction'</span><span class="p">)]</span><span class="o">/</span><span class="nt">AnonymousClassDeclaration</span><span class="p">)]</span><span class="w">
</span><span class="c">(: exclude inner privileged action classes :)</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">ClassDeclaration</span><span class="p">[</span><span class="m">1</span><span class="p">][</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'java.security.PrivilegedAction'</span><span class="p">)])]</span><span class="w">
</span><span class="c">(: exclude privileged action lambdas :)</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">LambdaExpression</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'java.security.PrivilegedAction'</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.Constructor</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">java.lang.reflect.Field</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.AccessController</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">private</span> <span class="kt">void</span> <span class="nf">invalidSetAccessCalls</span><span class="o">()</span> <span class="kd">throws</span> <span class="nc">NoSuchMethodException</span><span class="o">,</span> <span class="nc">SecurityException</span> <span class="o">{</span>
<span class="nc">Constructor</span><span class="o">&lt;?&gt;</span> <span class="n">constructor</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="nc">String</span><span class="o">.</span><span class="na">class</span><span class="o">);</span>
<span class="c1">// call to forbidden setAccessible</span>
<span class="n">constructor</span><span class="o">.</span><span class="na">setAccessible</span><span class="o">(</span><span class="kc">true</span><span class="o">);</span>
<span class="nc">Method</span> <span class="n">privateMethod</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">getDeclaredMethod</span><span class="o">(</span><span class="s">"aPrivateMethod"</span><span class="o">);</span>
<span class="c1">// call to forbidden setAccessible</span>
<span class="n">privateMethod</span><span class="o">.</span><span class="na">setAccessible</span><span class="o">(</span><span class="kc">true</span><span class="o">);</span>
<span class="c1">// deliberate accessibility alteration</span>
<span class="nc">String</span> <span class="n">privateField</span> <span class="o">=</span> <span class="nc">AccessController</span><span class="o">.</span><span class="na">doPrivileged</span><span class="o">(</span><span class="k">new</span> <span class="nc">PrivilegedAction</span><span class="o">&lt;</span><span class="nc">String</span><span class="o">&gt;()</span> <span class="o">{</span>
<span class="nd">@Override</span>
<span class="kd">public</span> <span class="nc">String</span> <span class="nf">run</span><span class="o">()</span> <span class="o">{</span>
<span class="k">try</span> <span class="o">{</span>
<span class="nc">Field</span> <span class="n">field</span> <span class="o">=</span> <span class="nc">Violation</span><span class="o">.</span><span class="na">class</span><span class="o">.</span><span class="na">getDeclaredField</span><span class="o">(</span><span class="s">"aPrivateField"</span><span class="o">);</span>
<span class="n">field</span><span class="o">.</span><span class="na">setAccessible</span><span class="o">(</span><span class="kc">true</span><span class="o">);</span>
<span class="k">return</span> <span class="o">(</span><span class="nc">String</span><span class="o">)</span> <span class="n">field</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="kc">null</span><span class="o">);</span>
<span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="nc">ReflectiveOperationException</span> <span class="o">|</span> <span class="nc">SecurityException</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nf">RuntimeException</span><span class="o">(</span><span class="n">e</span><span class="o">);</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/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><strong>Maximum Language Version:</strong> Java 1.3</p>
<p>Use of the term <code class="language-plaintext highlighter-rouge">assert</code> will conflict with newer versions of Java since it is a reserved word.</p>
<p>Since Java 1.4, the token <code class="language-plaintext highlighter-rouge">assert</code> became a reserved word and using it as an identifier will
result in a compilation failure for Java 1.4 and later. This rule is therefore only useful
for old Java code before Java 1.4. It can be used to identify problematic code prior to a Java update.</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">VariableId</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>
</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>
</tr>
<tr>
<td>checkContinueLoopTypes</td>
<td>for , do , while</td>
<td>List of loop types in which continue statements will be checked</td>
</tr>
<tr>
<td>checkReturnLoopTypes</td>
<td>for , do , while</td>
<td>List of loop types in which return statements will be checked</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 XPath expression:</strong></p>
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">MethodCall</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">matchesSig</span><span class="p">(</span><span class="s2">"java.lang.Object#finalize()"</span><span class="p">)]</span><span class="w">
</span><span class="c">(: it's ok inside finalize :)</span><span class="w">
</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="nt">SuperExpression</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="kn">ancestor::</span><span class="o">*</span><span class="p">[</span><span class="kn">self::</span><span class="nt">MethodDeclaration</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="kn">self::</span><span class="nt">Initializer</span><span class="p">][</span><span class="m">1</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">'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="nt">VoidType</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">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">CatchClause</span><span class="o">/</span><span class="nt">CatchParameter</span><span class="o">/</span><span class="nt">ClassType</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">typeIsExactly</span><span class="p">(</span><span class="s1">'java.lang.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 XPath expression:</strong></p>
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">CatchParameter</span><span class="p">[</span><span class="nt">ClassType</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">typeIsExactly</span><span class="p">(</span><span class="s1">'java.lang.Throwable'</span><span class="p">)]]</span><span class="o">/</span><span class="nt">VariableId</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">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">ConstructorCall</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">matchesSig</span><span class="p">(</span><span class="s1">'java.math.BigDecimal#new(double)'</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>
</tr>
</thead>
<tbody>
<tr>
<td>maxDuplicateLiterals</td>
<td>4</td>
<td>Max duplicate literals</td>
</tr>
<tr>
<td>minimumLength</td>
<td>3</td>
<td>Minimum string length to check</td>
</tr>
<tr>
<td>skipAnnotations</td>
<td>false</td>
<td>Skip literals within annotations</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>
</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">"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><strong>Maximum Language Version:</strong> Java 1.4</p>
<p>Use of the term <code class="language-plaintext highlighter-rouge">enum</code> will conflict with newer versions of Java since it is a reserved word.</p>
<p>Since Java 1.5, the token <code class="language-plaintext highlighter-rouge">enum</code> became a reserved word and using it as an identifier will
result in a compilation failure for Java 1.5 and later. This rule is therefore only useful
for old Java code before Java 1.5. It can be used to identify problematic code prior to a Java update.</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">VariableId</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 XPath expression:</strong></p>
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">FieldDeclaration</span><span class="o">/</span><span class="nt">VariableDeclarator</span><span class="o">/</span><span class="nt">VariableId</span><span class="w">
</span><span class="p">[</span><span class="kr">some</span><span class="w"> </span><span class="nv">$method</span><span class="w"> </span><span class="kr">in</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="kn">self::</span><span class="nt">ClassBody</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="kn">self::</span><span class="nt">EnumBody</span><span class="p">]</span><span class="o">/</span><span class="nt">MethodDeclaration</span><span class="w">
</span><span class="kr">satisfies</span><span class="w"> </span><span class="nf">lower-case</span><span class="p">(</span><span class="na">@Name</span><span class="p">)</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="nf">lower-case</span><span class="p">(</span><span class="nv">$method</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">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 type 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 XPath expression:</strong></p>
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">FieldDeclaration</span><span class="o">/</span><span class="nt">VariableDeclarator</span><span class="o">/</span><span class="nt">VariableId</span><span class="w">
</span><span class="p">[</span><span class="nf">lower-case</span><span class="p">(</span><span class="na">@Name</span><span class="p">)</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="nf">lower-case</span><span class="p">(</span><span class="kn">ancestor::</span><span class="nt">ClassDeclaration</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></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">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>
<span class="kd">public</span> <span class="kd">interface</span> <span class="nc">Operation</span> <span class="o">{</span>
<span class="kt">int</span> <span class="no">OPERATION</span> <span class="o">=</span> <span class="mi">1</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">CatchParameter</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">InfixExpression</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">'instanceof'</span><span class="p">]</span><span class="w">
</span><span class="o">/</span><span class="nt">VariableAccess</span><span class="p">[</span><span class="na">@Name</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">CatchParameter</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="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 - this is always executed :)</span><span class="w">
</span><span class="o">//</span><span class="nt">IfStatement</span><span class="err">/</span><span class="o">*</span><span class="p">[</span><span class="m">1</span><span class="p">]</span><span class="err">/</span><span class="o">*</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">nodeIs</span><span class="p">(</span><span class="s1">'Literal'</span><span class="p">)]</span><span class="w">
</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="nn">pmd-java:</span><span class="nf">nodeIs</span><span class="p">(</span><span class="s1">'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="nn">pmd-java:</span><span class="nf">nodeIs</span><span class="p">(</span><span class="s1">'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="err">/</span><span class="o">*</span><span class="p">[</span><span class="m">1</span><span class="p">]</span><span class="err">//</span><span class="o">*</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="kn">self::</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="err">/</span><span class="o">*</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">nodeIs</span><span class="p">(</span><span class="s1">'Literal'</span><span class="p">)]</span><span class="w">
</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="nn">pmd-java:</span><span class="nf">nodeIs</span><span class="p">(</span><span class="s1">'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="nn">pmd-java:</span><span class="nf">nodeIs</span><span class="p">(</span><span class="s1">'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="err">/</span><span class="o">*</span><span class="p">[</span><span class="m">1</span><span class="p">]</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="err">/</span><span class="o">*</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">nodeIs</span><span class="p">(</span><span class="s1">'Literal'</span><span class="p">)]</span><span class="w">
</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="nn">pmd-java:</span><span class="nf">nodeIs</span><span class="p">(</span><span class="s1">'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="nn">pmd-java:</span><span class="nf">nodeIs</span><span class="p">(</span><span class="s1">'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="err">/</span><span class="o">*</span><span class="p">[</span><span class="m">1</span><span class="p">][</span><span class="nf">count</span><span class="p">(</span><span class="o">*</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">nodeIs</span><span class="p">(</span><span class="s1">'Literal'</span><span class="p">)]</span><span class="w">
</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="nn">pmd-java:</span><span class="nf">nodeIs</span><span class="p">(</span><span class="s1">'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="nn">pmd-java:</span><span class="nf">nodeIs</span><span class="p">(</span><span class="s1">'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="k">if</span> <span class="o">(</span><span class="n">i</span> <span class="o">==</span> <span class="mi">5</span> <span class="o">&amp;&amp;</span> <span class="s">"none"</span><span class="o">.</span><span class="na">equals</span><span class="o">(</span><span class="n">aString</span><span class="o">))</span> <span class="o">{}</span> <span class="c1">// 2 violations: magic number 5 and literal "none"</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>
</tr>
</thead>
<tbody>
<tr>
<td>ignoreMagicNumbers</td>
<td>-1,0</td>
<td>Comma-separated list of magic numbers, that should be ignored</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>
</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">CatchClause</span><span class="o">/</span><span class="nt">Block</span><span class="o">/</span><span class="nt">ExpressionStatement</span><span class="o">/</span><span class="nt">MethodCall</span><span class="p">[</span><span class="w">
</span><span class="nn">pmd-java:</span><span class="nf">matchesSig</span><span class="p">(</span><span class="s2">"java.lang.Throwable#getMessage()"</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">matchesSig</span><span class="p">(</span><span class="s2">"java.lang.Throwable#getLocalizedMessage()"</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">matchesSig</span><span class="p">(</span><span class="s2">"java.lang.Throwable#getCause()"</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">matchesSig</span><span class="p">(</span><span class="s2">"java.lang.Throwable#getStackTrace()"</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">matchesSig</span><span class="p">(</span><span class="s2">"java.lang.Object#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 XPath expression:</strong></p>
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c">(: Only report on the toplevel one :)</span><span class="w">
</span><span class="o">//</span><span class="nt">UnaryExpression</span><span class="p">[</span><span class="nt">UnaryExpression</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="kn">parent::</span><span class="nt">UnaryExpression</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">// 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>
</tr>
</thead>
<tbody>
<tr>
<td>strict</td>
<td>false</td>
<td>Detect violations between 00 and 07</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="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">ClassDeclaration</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">'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><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="err">=</span><span class="p">(</span><span class="s1">'onCreate'</span><span class="p">,</span><span class="w"> </span><span class="s1">'onConfigurationChanged'</span><span class="p">,</span><span class="w"> </span><span class="s1">'onPostCreate'</span><span class="p">,</span><span class="w"> </span><span class="s1">'onPostResume'</span><span class="p">,</span><span class="w"> </span><span class="s1">'onRestart'</span><span class="p">,</span><span class="w">
</span><span class="s1">'onRestoreInstanceState'</span><span class="p">,</span><span class="w"> </span><span class="s1">'onResume'</span><span class="p">,</span><span class="w"> </span><span class="s1">'onStart'</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">Block</span><span class="err">/</span><span class="o">*</span><span class="p">[</span><span class="m">1</span><span class="p">]</span><span class="o">/</span><span class="nt">MethodCall</span><span class="p">[</span><span class="nt">SuperExpression</span><span class="p">][</span><span class="na">@MethodName</span><span class="w"> </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></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">android.app.Activity</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">android.os.Bundle</span><span class="o">;</span>
<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">ClassDeclaration</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">'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><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="err">=</span><span class="p">(</span><span class="s1">'finish'</span><span class="p">,</span><span class="w"> </span><span class="s1">'onDestroy'</span><span class="p">,</span><span class="w"> </span><span class="s1">'onPause'</span><span class="p">,</span><span class="w"> </span><span class="s1">'onSaveInstanceState'</span><span class="p">,</span><span class="w"> </span><span class="s1">'onStop'</span><span class="p">,</span><span class="w"> </span><span class="s1">'onTerminate'</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">Block</span><span class="err">/</span><span class="o">*</span><span class="p">[</span><span class="nf">last</span><span class="p">()]</span><span class="o">/</span><span class="nt">MethodCall</span><span class="p">[</span><span class="nt">SuperExpression</span><span class="p">][</span><span class="na">@MethodName</span><span class="w"> </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></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">android.app.Activity</span><span class="o">;</span>
<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 <code class="language-plaintext highlighter-rouge">toArray()</code> method. Doing otherwise will result
in a <code class="language-plaintext highlighter-rouge">ClassCastException</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">CastExpression</span><span class="p">[</span><span class="nt">ArrayType</span><span class="o">/</span><span class="nt">ClassType</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="nn">pmd-java:</span><span class="nf">typeIsExactly</span><span class="p">(</span><span class="s1">'java.lang.Object'</span><span class="p">))]]</span><span class="w">
</span><span class="o">/</span><span class="nt">MethodCall</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">matchesSig</span><span class="p">(</span><span class="s2">"java.util.Collection#toArray()"</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="mi">0</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="ow">not</span><span class="p">(</span><span class="nn">pmd-java:</span><span class="ow">mod</span><span class="kr">if</span><span class="nf">iers</span><span class="p">()</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s2">"public"</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 <code class="language-plaintext highlighter-rouge">Cloneable</code> the return type of the method <code class="language-plaintext highlighter-rouge">clone()</code> 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: Such a covariant return type 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="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><span class="p">[</span><span class="nt">ClassType</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="w"> </span><span class="p">!</span><span class="err">=</span><span class="w"> </span><span class="kn">ancestor::</span><span class="nt">ClassDeclaration</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></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="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.AutoCloseable</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>
</tr>
</thead>
<tbody>
<tr>
<td>closeTargets</td>
<td> </td>
<td>Methods which may close this resource</td>
</tr>
<tr>
<td>types</td>
<td>java.lang.AutoCloseable , java.sql.Connection , java.sql.Statement , java.sql.ResultSet</td>
<td>Affected types</td>
</tr>
<tr>
<td>closeAsDefaultTarget</td>
<td>true</td>
<td>Consider close as a target by default</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>
</tr>
<tr>
<td>closeNotInFinally</td>
<td>false</td>
<td>Detect if close (or other closeTargets) is called outside of a finally-block</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 <code class="language-plaintext highlighter-rouge">equals()</code> to compare object references; avoid comparing them with <code class="language-plaintext highlighter-rouge">==</code>.</p>
<p>Since comparing objects with named constants is useful in some cases (eg, when
defining constants for sentinel values), the rule ignores comparisons against
fields with all-caps name (eg <code class="language-plaintext highlighter-rouge">this == SENTINEL</code>), which is a common naming
convention for constant fields.</p>
<p>You may allow some types to be compared by reference by listing the exceptions
in the <code class="language-plaintext highlighter-rouge">typesThatCompareByReference</code> property.</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">InfixExpression</span><span class="w">
</span><span class="p">[</span><span class="na">@Operator</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="p">(</span><span class="s2">"=="</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">count</span><span class="p">(</span><span class="o">*</span><span class="w">
</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="kn">self::</span><span class="nt">NullLiteral</span><span class="p">)]</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">'java.lang.Object'</span><span class="p">)]</span><span class="w">
</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="kr">some</span><span class="w"> </span><span class="nv">$t</span><span class="w"> </span><span class="kr">in</span><span class="w"> </span><span class="nv">$typesThatCompareByReference</span><span class="w"> </span><span class="kr">satisfies</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="nv">$t</span><span class="p">))]</span><span class="w">
</span><span class="p">)</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="m">2</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="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="w"> </span><span class="err">=</span><span class="w"> </span><span class="s2">"equals"</span><span class="p">])]</span><span class="w">
</span><span class="c">(: Is not a field access with an all-caps identifier :)</span><span class="w">
</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="nt">FieldAccess</span><span class="p">[</span><span class="nf">upper-case</span><span class="p">(</span><span class="na">@Name</span><span class="p">)</span><span class="err">=</span><span class="na">@Name</span><span class="p">]</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="nt">VariableAccess</span><span class="p">[</span><span class="nf">upper-case</span><span class="p">(</span><span class="na">@Name</span><span class="p">)</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">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>This rule has the following properties:</strong></p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Default Value</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>typesThatCompareByReference</td>
<td>java.lang.Enum , java.lang.Class</td>
<td>List of canonical type names for which reference comparison is allowed.</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/CompareObjectsWithEquals"</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/CompareObjectsWithEquals"</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">"typesThatCompareByReference"</span> <span class="na">value=</span><span class="s">"java.lang.Enum,java.lang.Class"</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="comparisonwithnan">ComparisonWithNaN</h2>
<p><strong>Since:</strong> PMD 6.36.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Reports comparisons with double and float <code class="language-plaintext highlighter-rouge">NaN</code> (Not-a-Number) values.
These are <a href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.21.1">specified</a>
to have unintuitive behavior: NaN is considered unequal to itself.
This means a check like <code class="language-plaintext highlighter-rouge">someDouble == Double.NaN</code> will always return
false, even if <code class="language-plaintext highlighter-rouge">someDouble</code> is really the NaN value. To test whether a
value is the NaN value, one should instead use <code class="language-plaintext highlighter-rouge">Double.isNaN(someDouble)</code>
(or <code class="language-plaintext highlighter-rouge">Float.isNaN</code>). The <code class="language-plaintext highlighter-rouge">!=</code> operator should be treated similarly.
Finally, comparisons like <code class="language-plaintext highlighter-rouge">someDouble &lt;= Double.NaN</code> are nonsensical
and will always evaluate to false.</p>
<p>This rule has been renamed from "BadComparison" in PMD 6.36.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">InfixExpression</span><span class="p">[</span><span class="na">@Operator</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="p">(</span><span class="s2">"=="</span><span class="p">,</span><span class="w"> </span><span class="s2">"!="</span><span class="p">,</span><span class="w"> </span><span class="s2">"&lt;="</span><span class="p">,</span><span class="w"> </span><span class="s2">"&gt;="</span><span class="p">,</span><span class="w"> </span><span class="s2">"&lt;"</span><span class="p">,</span><span class="w"> </span><span class="s2">"&gt;"</span><span class="p">)]</span><span class="o">/</span><span class="nt">FieldAccess</span><span class="p">[</span><span class="na">@Name</span><span class="err">=</span><span class="s1">'NaN'</span><span class="w"> </span><span class="ow">and</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">'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">'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="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/ComparisonWithNaN"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="confusingargumenttovarargsmethod">ConfusingArgumentToVarargsMethod</h2>
<p><strong>Since:</strong> PMD 7.1.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Reports a confusing argument passed to a varargs method.</p>
<p>This can occur when an array is passed as a single varargs argument, when the array type is not exactly the
type of array that the varargs method expects. If, that array is a subtype of the component type of the expected
array type, then it might not be clear what value the called varargs method will receive.
For instance if you have:</p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">void</span> <span class="nf">varargs</span><span class="o">(</span><span class="nc">Object</span><span class="o">...</span> <span class="n">parm</span><span class="o">);</span>
</code></pre></div></div>
<p>and call it like so:</p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">varargs</span><span class="o">(</span><span class="k">new</span> <span class="nc">String</span><span class="o">[]{</span><span class="s">"a"</span><span class="o">});</span>
</code></pre></div></div>
<p>it is not clear whether you intended the method to receive the value <code class="language-plaintext highlighter-rouge">new Object[]{ new String[] {"a"} }</code> or
just <code class="language-plaintext highlighter-rouge">new String[] {"a"}</code> (the latter happens). This confusion occurs because <code class="language-plaintext highlighter-rouge">String[]</code> is both a subtype
of <code class="language-plaintext highlighter-rouge">Object[]</code> and of <code class="language-plaintext highlighter-rouge">Object</code>. To clarify your intent in this case, use a cast or pass individual elements like so:</p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">// varargs call</span>
<span class="c1">// parm will be `new Object[] { "a" }`</span>
<span class="n">varargs</span><span class="o">(</span><span class="s">"a"</span><span class="o">);</span>
<span class="c1">// non-varargs call</span>
<span class="c1">// parm will be `new String[] { "a" }`</span>
<span class="n">varargs</span><span class="o">((</span><span class="nc">Object</span><span class="o">[])</span> <span class="k">new</span> <span class="nc">String</span><span class="o">[]{</span><span class="s">"a"</span><span class="o">});</span>
<span class="c1">// varargs call</span>
<span class="c1">// parm will be `new Object[] { new String[] { "a" } }`</span>
<span class="n">varargs</span><span class="o">((</span><span class="nc">Object</span><span class="o">)</span> <span class="k">new</span> <span class="nc">String</span><span class="o">[]{</span><span class="s">"a"</span><span class="o">});</span>
</code></pre></div></div>
<p>Another confusing case is when you pass <code class="language-plaintext highlighter-rouge">null</code> as the varargs argument. Here it is not clear whether you intended
to pass an array with a single null element, or a null array (the latter happens). This can similarly be clarified
with a cast.</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/ConfusingArgumentToVarargsMethodRule.java">net.sourceforge.pmd.lang.java.rule.errorprone.ConfusingArgumentToVarargsMethodRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">import</span> <span class="nn">java.util.Arrays</span><span class="o">;</span>
<span class="kd">abstract</span> <span class="kd">class</span> <span class="nc">C</span> <span class="o">{</span>
<span class="kd">abstract</span> <span class="kt">void</span> <span class="nf">varargs</span><span class="o">(</span><span class="nc">Object</span><span class="o">...</span> <span class="n">args</span><span class="o">);</span>
<span class="kd">static</span> <span class="o">{</span>
<span class="n">varargs</span><span class="o">(</span><span class="k">new</span> <span class="nc">String</span><span class="o">[]</span> <span class="o">{</span> <span class="s">"a"</span> <span class="o">});</span>
<span class="n">varargs</span><span class="o">(</span><span class="kc">null</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/ConfusingArgumentToVarargsMethod"</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>Reports calls to overridable methods on <code class="language-plaintext highlighter-rouge">this</code> during object initialization. These
are invoked on an incompletely constructed object and can be difficult to debug if overridden.
This is because the subclass usually assumes that the superclass is completely initialized
in all methods. If that is not the case, bugs can appear in the constructor, for instance,
some fields that are still null may cause a NullPointerException or be stored somewhere
else to blow up later.</p>
<p>To avoid this problem, only use methods that are static, private, or final in constructors.
Note that those methods also must not call overridable methods transitively to be safe.</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="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 Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/DetachedTestCaseRule.java">net.sourceforge.pmd.lang.java.rule.errorprone.DetachedTestCaseRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">MyTest</span> <span class="o">{</span>
<span class="nd">@Test</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">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 <code class="language-plaintext highlighter-rouge">System.gc()</code>, <code class="language-plaintext highlighter-rouge">Runtime.getRuntime().gc()</code>, and <code class="language-plaintext highlighter-rouge">System.runFinalization()</code> are not advised.
Code should have the same behavior whether the garbage collection is disabled using the option
<code class="language-plaintext highlighter-rouge">-Xdisableexplicitgc</code> or not.</p>
<p>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">MethodCall</span><span class="p">[</span><span class="w">
</span><span class="nn">pmd-java:</span><span class="nf">matchesSig</span><span class="p">(</span><span class="s2">"java.lang.System#gc()"</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">matchesSig</span><span class="p">(</span><span class="s2">"java.lang.Runtime#gc()"</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">matchesSig</span><span class="p">(</span><span class="s2">"java.lang.System#runFinalization()"</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">matchesSig</span><span class="p">(</span><span class="s2">"java.lang.Runtime#runFinalization()"</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="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">ClassDeclaration</span><span class="o">/</span><span class="nt">ExtendsList</span><span class="o">/</span><span class="nt">ClassType</span><span class="w">
</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">typeIsExactly</span><span class="p">(</span><span class="s1">'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">StringLiteral</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 has been renamed from "DoNotCallSystemExit" in 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="err">//</span><span class="p">(</span><span class="nt">MethodDeclaration</span><span class="p">[</span><span class="na">@MainMethod</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="nt">Initializer</span><span class="p">)</span><span class="o">//</span><span class="nt">MethodCall</span><span class="p">[</span><span class="w">
</span><span class="nn">pmd-java:</span><span class="nf">matchesSig</span><span class="p">(</span><span class="s2">"java.lang.System#exit(int)"</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">matchesSig</span><span class="p">(</span><span class="s2">"java.lang.Runtime#exit(int)"</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">matchesSig</span><span class="p">(</span><span class="s2">"java.lang.Runtime#halt(int)"</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="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">FinallyClause</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>If you find yourself having to depend on Sun APIs, confine this dependency to as
small a scope as possible, for instance by writing a stable wrapper class around
the unstable API. You can then suppress this rule in the implementation of the wrapper.</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">ImportDeclaration</span><span class="p">[</span><span class="nf">starts-with</span><span class="p">(</span><span class="na">@ImportedName</span><span class="p">,</span><span class="w"> </span><span class="s1">'sun.'</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">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">VariableId</span><span class="p">[</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></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">CatchClause</span><span class="p">[</span><span class="w">
</span><span class="nt">Block</span><span class="p">[</span><span class="w">
</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">0</span><span class="w">
</span><span class="ow">and</span><span class="w"> </span><span class="p">(</span><span class="nv">$allowCommentedBlocks</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">@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="w">
</span><span class="ow">and</span><span class="w"> </span><span class="nt">CatchParameter</span><span class="o">/</span><span class="nt">VariableId</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><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>
</tr>
</thead>
<tbody>
<tr>
<td>allowCommentedBlocks</td>
<td>false</td>
<td>Empty blocks containing comments will be skipped</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>
</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="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">MethodCall</span><span class="p">[</span><span class="na">@MethodName</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s2">"equals"</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="nt">ArgumentList</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="w"> </span><span class="ow">and</span><span class="w"> </span><span class="nt">NullLiteral</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="w"> </span><span class="err">=</span><span class="w"> </span><span class="s2">"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="err">/</span><span class="o">*</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">MethodCall</span><span class="p">[</span><span class="na">@MethodName</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s2">"finalize"</span><span class="p">]</span><span class="o">/</span><span class="nt">SuperExpression</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">FinallyClause</span><span class="o">/</span><span class="nt">Block</span><span class="o">/</span><span class="nt">ExpressionStatement</span><span class="o">/</span><span class="w">
</span><span class="nt">MethodCall</span><span class="p">[</span><span class="na">@MethodName</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s2">"finalize"</span><span class="p">]</span><span class="o">/</span><span class="nt">SuperExpression</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="p">[</span><span class="nt">Block</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="o">/</span><span class="nt">ExpressionStatement</span><span class="o">/</span><span class="nt">MethodCall</span><span class="p">[</span><span class="na">@MethodName</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s2">"finalize"</span><span class="p">][</span><span class="nt">SuperExpression</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">@Visibility</span><span class="w"> </span><span class="p">!</span><span class="err">=</span><span class="w"> </span><span class="s2">"protected"</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="implicitswitchfallthrough">ImplicitSwitchFallThrough</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>You can ignore a violation by commenting <code class="language-plaintext highlighter-rouge">// fallthrough</code> before the case label
which is reached by fallthrough, or with <code class="language-plaintext highlighter-rouge">@SuppressWarnings("fallthrough")</code>.</p>
<p>This rule has been renamed from "MissingBreakInSwitch" in PMD 6.37.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/ImplicitSwitchFallThroughRule.java">net.sourceforge.pmd.lang.java.rule.errorprone.ImplicitSwitchFallThroughRule</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="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="c1">// what happens if you add another case after this one?</span>
<span class="k">case</span> <span class="nl">REMOVED:</span>
<span class="n">doRemoved</span><span class="o">();</span>
<span class="c1">// fallthrough - this comment just clarifies that you want a fallthrough</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/ImplicitSwitchFallThrough"</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">MethodCall</span><span class="w">
</span><span class="p">[</span><span class="na">@MethodName</span><span class="err">=</span><span class="s1">'getClass'</span><span class="p">]</span><span class="w">
</span><span class="p">[</span><span class="nt">ConstructorCall</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>Since 6.32.0 in addition to parameterized message placeholders (<code class="language-plaintext highlighter-rouge">{}</code>) also format specifiers of string formatted
messages are supported (<code class="language-plaintext highlighter-rouge">%s</code>).</p>
<p>This rule has been renamed from "InvalidSlf4jMessageFormat" in PMD 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/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">"forget the arg %s"</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="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="ow">not</span><span class="p">(</span><span class="nt">ForInit</span><span class="p">)</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="nt">ForInit</span><span class="o">//</span><span class="nt">VariableId</span><span class="err">/</span><span class="na">@Name</span><span class="w"> </span><span class="p">!</span><span class="err">=</span><span class="w"> </span><span class="nt">ForUpdate</span><span class="o">//</span><span class="nt">VariableAccess</span><span class="err">/</span><span class="na">@Name</span><span class="p">]</span><span class="w">
</span><span class="p">[</span><span class="nt">ForUpdate</span><span class="o">//</span><span class="nt">VariableAccess</span><span class="p">[</span><span class="na">@AccessType</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'WRITE'</span><span class="p">]</span><span class="err">/</span><span class="na">@Name</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">VariableId</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="o">}</span>
</code></pre></div></div>
<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">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="methodwithsamenameasenclosingclass">MethodWithSameNameAsEnclosingClass</h2>
<p><strong>Since:</strong> PMD 1.5</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>A method should not have the same name as its containing class.
This would be confusing as it would look like a 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">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="kn">ancestor::</span><span class="nt">ClassDeclaration</span><span class="err">/</span><span class="na">@SimpleName</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="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 <code class="language-plaintext highlighter-rouge">NullPointerException</code> will be thrown.
Either the check is useless (the variable will never be <code class="language-plaintext highlighter-rouge">null</code>) 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">InfixExpression</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">'&amp;&amp;'</span><span class="p">]</span><span class="w">
</span><span class="o">/</span><span class="nt">InfixExpression</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="w">
</span><span class="c">(: one side is null :)</span><span class="w">
</span><span class="p">[</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="nt">VariableAccess</span><span class="p">]</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="err">*</span><span class="o">//</span><span class="nt">VariableAccess</span><span class="w">
</span><span class="p">[</span><span class="kn">parent::</span><span class="nt">MethodCall</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="kn">parent::</span><span class="nt">FieldAccess</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">InfixExpression</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="w">
</span><span class="err">/</span><span class="na">@Name</span><span class="w">
</span><span class="kr">satisfies</span><span class="w"> </span><span class="nv">$var</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="nt">VariableAccess</span><span class="err">/</span><span class="na">@Name</span><span class="w">
</span><span class="p">]</span><span class="w">
</span><span class="o">/</span><span class="nt">VariableAccess</span><span class="w">
</span><span class="p">|</span><span class="w">
</span><span class="o">//</span><span class="nt">InfixExpression</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="w">
</span><span class="o">/</span><span class="nt">InfixExpression</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="w">
</span><span class="c">(: one side is null :)</span><span class="w">
</span><span class="p">[</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="nt">VariableAccess</span><span class="p">]</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="err">*</span><span class="o">//</span><span class="nt">VariableAccess</span><span class="w">
</span><span class="p">[</span><span class="kn">parent::</span><span class="nt">MethodCall</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="kn">parent::</span><span class="nt">FieldAccess</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">InfixExpression</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">'&amp;&amp;'</span><span class="p">])]</span><span class="w">
</span><span class="err">/</span><span class="na">@Name</span><span class="w">
</span><span class="kr">satisfies</span><span class="w"> </span><span class="nv">$var</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="nt">VariableAccess</span><span class="err">/</span><span class="na">@Name</span><span class="w">
</span><span class="p">]</span><span class="w">
</span><span class="o">/</span><span class="nt">VariableAccess</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="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">ClassDeclaration</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">ClassBody</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">VariableId</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">ClassType</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="ow">le</span><span class="nt">t</span><span class="w"> </span><span class="nv">$topLevelClass</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="err">/*</span><span class="o">/</span><span class="nt">ClassDeclaration</span><span class="w"> </span><span class="kr">return</span><span class="w">
</span><span class="ow">le</span><span class="nt">t</span><span class="w"> </span><span class="nv">$isLombokUtility</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nf">exists</span><span class="p">(</span><span class="nv">$topLevelClass</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">hasAnnotation</span><span class="p">(</span><span class="s1">'lombok.experimental.UtilityClass'</span><span class="p">)])</span><span class="w"> </span><span class="kr">return</span><span class="w">
</span><span class="nv">$topLevelClass</span><span class="p">[</span><span class="w">
</span><span class="c">(: non-instantiable :)</span><span class="w">
</span><span class="nv">$isLombokUtility</span><span class="w"> </span><span class="ow">or</span><span class="w">
</span><span class="p">(</span><span class="w">
</span><span class="c">(: no lombok produced constructors :)</span><span class="w">
</span><span class="ow">not</span><span class="p">(</span><span class="nn">pmd-java:</span><span class="nf">hasAnnotation</span><span class="p">(</span><span class="s1">'lombok.NoArgsConstructor'</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">hasAnnotation</span><span class="p">(</span><span class="s1">'lombok.RequiredArgsConstructor'</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">hasAnnotation</span><span class="p">(</span><span class="s1">'lombok.AllArgsConstructor'</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">hasAnnotation</span><span class="p">(</span><span class="s1">'lombok.Builder'</span><span class="p">))</span><span class="w"> </span><span class="ow">and</span><span class="w">
</span><span class="c">(: or has non-default constructors … :)</span><span class="w">
</span><span class="nt">ClassBody</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">(: … but only private … :)</span><span class="w">
</span><span class="ow">not</span><span class="p">(</span><span class="nt">ClassBody</span><span class="o">/</span><span class="nt">ConstructorDeclaration</span><span class="p">[</span><span class="na">@Visibility</span><span class="w"> </span><span class="p">!</span><span class="err">=</span><span class="w"> </span><span class="s2">"private"</span><span class="p">])</span><span class="w"> </span><span class="ow">and</span><span class="w">
</span><span class="c">(: … and none 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="nt">ClassBody</span><span class="o">/</span><span class="nt">ConstructorDeclaration</span><span class="o">/</span><span class="nt">ModifierList</span><span class="o">/</span><span class="nt">Annotation</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="p">]</span><span class="w">
</span><span class="p">[</span><span class="w">
</span><span class="c">(: With no visible static methods … :)</span><span class="w">
</span><span class="ow">not</span><span class="p">(</span><span class="nt">ClassBody</span><span class="o">/</span><span class="nt">MethodDeclaration</span><span class="p">[(</span><span class="nv">$isLombokUtility</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="ow">mod</span><span class="kr">if</span><span class="nf">iers</span><span class="p">()</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s2">"static"</span><span class="p">)</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="na">@Visibility</span><span class="w"> </span><span class="p">!</span><span class="err">=</span><span class="w"> </span><span class="s2">"private"</span><span class="p">])</span><span class="w"> </span><span class="ow">and</span><span class="w">
</span><span class="c">(: … nor fields … :)</span><span class="w">
</span><span class="ow">not</span><span class="p">(</span><span class="nt">ClassBody</span><span class="o">/</span><span class="nt">FieldDeclaration</span><span class="p">[(</span><span class="nv">$isLombokUtility</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="ow">mod</span><span class="kr">if</span><span class="nf">iers</span><span class="p">()</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s2">"static"</span><span class="p">)</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="na">@Visibility</span><span class="w"> </span><span class="p">!</span><span class="err">=</span><span class="w"> </span><span class="s2">"private"</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 non-private and static … :)</span><span class="w">
</span><span class="ow">not</span><span class="p">(</span><span class="nt">ClassBody</span><span class="o">/</span><span class="nt">ClassDeclaration</span><span class="w">
</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="ow">mod</span><span class="kr">if</span><span class="nf">iers</span><span class="p">()</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s2">"static"</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="na">@Visibility</span><span class="w"> </span><span class="p">!</span><span class="err">=</span><span class="w"> </span><span class="s2">"private"</span><span class="p">]</span><span class="w">
</span><span class="c">(: … with a default or non-private constructor … :)</span><span class="w">
</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="nt">ClassBody</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="nt">ClassBody</span><span class="o">/</span><span class="nt">ConstructorDeclaration</span><span class="p">[</span><span class="na">@Visibility</span><span class="w"> </span><span class="p">!</span><span class="err">=</span><span class="w"> </span><span class="s2">"private"</span><span class="p">]]</span><span class="w">
</span><span class="c">(: … and a non-private method returning the outer class type … :)</span><span class="w">
</span><span class="p">[(</span><span class="nt">ClassBody</span><span class="o">/</span><span class="nt">MethodDeclaration</span><span class="w">
</span><span class="p">[</span><span class="na">@Visibility</span><span class="w"> </span><span class="p">!</span><span class="err">=</span><span class="w"> </span><span class="s2">"private"</span><span class="p">]</span><span class="w">
</span><span class="p">[</span><span class="kn">descendant::</span><span class="nt">ReturnStatement</span><span class="err">/</span><span class="o">*</span><span class="p">[</span><span class="m">1</span><span class="p">][</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="kn">ancestor::</span><span class="nt">ClassDeclaration</span><span class="p">[</span><span class="na">@Nested</span><span class="w"> </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">@BinaryName</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="c">(: … or the inner class extends the outer class :)</span><span class="w">
</span><span class="nt">ExtendsList</span><span class="o">/</span><span class="nt">ClassType</span><span class="p">[</span><span class="na">@SimpleName</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="kn">ancestor::</span><span class="nt">ClassDeclaration</span><span class="p">[</span><span class="na">@Nested</span><span class="w"> </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></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>
</tr>
</thead>
<tbody>
<tr>
<td>annotations</td>
<td>org.springframework.beans.factory.annotation.Autowired , javax.inject.Inject , com.google.inject.Inject , lombok.Builder</td>
<td>If a constructor is annotated with one of these annotations, then the class is ignored.</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,com.google.inject.Inject,lombok.Builder"</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 XPath expression:</strong></p>
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">ClassDeclaration</span><span class="p">[</span><span class="w">
</span><span class="nf">count</span><span class="p">(</span><span class="w">
</span><span class="nt">ClassBody</span><span class="o">/</span><span class="nt">FieldDeclaration</span><span class="o">/</span><span class="nt">ClassType</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="s2">"org.apache.log4j.Logger"</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="s2">"org.apache.logging.log4j.Logger"</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="s2">"java.util.logging.Logger"</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="s2">"org.slf4j.Logger"</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">&gt;</span><span class="w"> </span><span class="m">1</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">// 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 is 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">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="nonserializableclass">NonSerializableClass</h2>
<p><strong>Since:</strong> PMD 1.1</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>If a class is marked as <code class="language-plaintext highlighter-rouge">Serializable</code>, then all fields need to be serializable as well. In order to exclude
a field, it can be marked as transient. Static fields are not considered.</p>
<p>This rule reports all fields, that are not serializable.</p>
<p>If a class implements the methods to perform manual serialization (<code class="language-plaintext highlighter-rouge">writeObject</code>, <code class="language-plaintext highlighter-rouge">readObject</code>) or uses
a replacement object (<code class="language-plaintext highlighter-rouge">writeReplace</code>, <code class="language-plaintext highlighter-rouge">readResolve</code>) then this class is ignored.</p>
<p>Note: This rule has been revamped with PMD 6.52.0. It was previously called "BeanMembersShouldSerialize".
The property <code class="language-plaintext highlighter-rouge">prefix</code> has been deprecated, since in a serializable class all fields have to be
serializable regardless of the name.</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/NonSerializableClassRule.java">net.sourceforge.pmd.lang.java.rule.errorprone.NonSerializableClassRule</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">Buzz</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="kd">private</span> <span class="kd">static</span> <span class="kd">final</span> <span class="kt">long</span> <span class="n">serialVersionUID</span> <span class="o">=</span> <span class="mi">1L</span><span class="o">;</span>
<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, it's static</span>
<span class="kd">private</span> <span class="n">java</span><span class="o">.</span><span class="na">io</span><span class="o">.</span><span class="na">FileInputStream</span> <span class="n">stream</span><span class="o">;</span> <span class="c1">// bad - FileInputStream is not serializable</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">setStream</span><span class="o">(</span><span class="nc">FileInputStream</span> <span class="n">stream</span><span class="o">)</span> <span class="o">{</span>
<span class="k">this</span><span class="o">.</span><span class="na">stream</span> <span class="o">=</span> <span class="n">stream</span><span class="o">;</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kt">int</span> <span class="nf">getSomeFoo</span><span class="o">()</span> <span class="o">{</span>
<span class="k">return</span> <span class="k">this</span><span class="o">.</span><span class="na">someFoo</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>
</tr>
</thead>
<tbody>
<tr>
<td>checkAbstractTypes</td>
<td>false</td>
<td>Enable to verify fields with abstract types like abstract classes, interfaces, generic types or java.lang.Object. Enabling this might lead to more false positives, since the concrete runtime type can actually be serializable.</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/NonSerializableClass"</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/NonSerializableClass"</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">"checkAbstractTypes"</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="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">ConstructorCall</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">ClassType</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="ow">not</span><span class="p">(</span><span class="nn">pmd-java:</span><span class="ow">mod</span><span class="kr">if</span><span class="nf">iers</span><span class="p">()</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'private'</span><span class="p">)</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="ow">not</span><span class="p">(</span><span class="nn">pmd-java:</span><span class="ow">mod</span><span class="kr">if</span><span class="nf">iers</span><span class="p">()</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'final'</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="nn">pmd-java:</span><span class="ow">mod</span><span class="kr">if</span><span class="nf">iers</span><span class="p">()</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'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">VariableId</span><span class="err">/</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="ow">not</span><span class="p">(</span><span class="nn">pmd-java:</span><span class="ow">mod</span><span class="kr">if</span><span class="nf">iers</span><span class="p">()</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'static'</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">VariableId</span><span class="err">/</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">ClassLiteral</span><span class="o">/</span><span class="nt">ClassType</span><span class="err">/</span><span class="na">@SimpleName</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">ClassDeclaration</span><span class="err">/</span><span class="na">@SimpleName</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">ClassLiteral</span><span class="o">/</span><span class="nt">ClassType</span><span class="err">/</span><span class="na">@SimpleName</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="w">
</span><span class="c">(: special case - final logger initialized inside constructor :)</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="p">(</span><span class="nt">VariableDeclarator</span><span class="err">/</span><span class="na">@Initializer</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="ow">not</span><span class="p">(</span><span class="nn">pmd-java:</span><span class="ow">mod</span><span class="kr">if</span><span class="nf">iers</span><span class="p">()</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'static'</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="kn">ancestor::</span><span class="nt">ClassBody</span><span class="o">/</span><span class="nt">ConstructorDeclaration</span><span class="w">
</span><span class="o">//</span><span class="nt">AssignmentExpression</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="w">
</span><span class="p">[</span><span class="nt">FieldAccess</span><span class="p">[</span><span class="m">1</span><span class="p">]</span><span class="err">/</span><span class="na">@Name</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="nv">$loggerName</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="nt">VariableAccess</span><span class="p">[</span><span class="m">1</span><span class="p">]</span><span class="err">/</span><span class="na">@Name</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="nv">$loggerName</span><span class="p">]</span><span class="w">
</span><span class="p">[</span><span class="o">*</span><span class="p">[</span><span class="m">2</span><span class="p">][</span><span class="na">@Name</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">VariableId</span><span class="err">/</span><span class="na">@Name</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>
</tr>
</thead>
<tbody>
<tr>
<td>staticLoggerName</td>
<td>LOG</td>
<td>Name of the static Logger variable</td>
</tr>
<tr>
<td>loggerName</td>
<td>log</td>
<td>Name of the Logger instance variable</td>
</tr>
<tr>
<td>loggerClass</td>
<td>org.apache.commons.logging.Log</td>
<td>Class name of the logger</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">"org.apache.commons.logging.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="returnemptycollectionratherthannull">ReturnEmptyCollectionRatherThanNull</h2>
<p><strong>Since:</strong> PMD 6.37.0</p>
<p><strong>Priority:</strong> High (1)</p>
<p>For any method that returns an collection (such as an array, Collection or Map), it is better to return
an empty one rather than a null reference. This removes the need for null checking all results and avoids
inadvertent NullPointerExceptions.</p>
<p>See Effective Java, 3rd Edition, Item 54: Return empty collections or arrays instead of null</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">ReturnStatement</span><span class="o">/</span><span class="nt">NullLiteral</span><span class="w">
</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="w">
</span><span class="p">[</span><span class="nt">ArrayType</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="nt">ClassType</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'java.util.Collection'</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.util.Map'</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="o">/</span><span class="kn">ancestor::</span><span class="nt">LambdaExpression</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/ReturnEmptyCollectionRatherThanNull"</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">FinallyClause</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">FinallyClause</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">ConstructorCall</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">'java.text.SimpleDateFormat'</span><span class="p">)]</span><span class="w">
</span><span class="p">[</span><span class="nt">ArgumentList</span><span class="err">/</span><span class="na">@Size</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="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>A singleton class should only ever have one instance. Failure to check
whether an instance has already been created may result in multiple
instances being created.</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="c1">// this should be assigned to the field</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">ClassDeclaration</span><span class="p">[</span><span class="nt">ImplementsList</span><span class="o">/</span><span class="nt">ClassType</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">'javax.ejb.SessionBean'</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">'javax.ejb.EJBHome'</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">'javax.ejb.EJBLocalObject'</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">'javax.ejb.EJBLocalHome'</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">'javax.ejb.EJBObject'</span><span class="p">)</span><span class="w">
</span><span class="p">]]</span><span class="w">
</span><span class="o">/</span><span class="nt">ClassBody</span><span class="o">/</span><span class="nt">FieldDeclaration</span><span class="w">
</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="ow">mod</span><span class="kr">if</span><span class="nf">iers</span><span class="p">()</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'static'</span><span class="p">]</span><span class="w">
</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="nn">pmd-java:</span><span class="ow">mod</span><span class="kr">if</span><span class="nf">iers</span><span class="p">()</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'final'</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">ConstructorCall</span><span class="p">[</span><span class="nt">ArgumentList</span><span class="err">/</span><span class="o">*</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">typeIsExactly</span><span class="p">(</span><span class="s1">'char'</span><span class="p">)]]</span><span class="w">
</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">matchesSig</span><span class="p">(</span><span class="s1">'java.lang.StringBuilder#new(int)'</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">matchesSig</span><span class="p">(</span><span class="s1">'java.lang.StringBuffer#new(int)'</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="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="p">(</span><span class="nt">FormalParameters</span><span class="o">/</span><span class="nt">FormalParameter</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">typeIsExactly</span><span class="p">(</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="p">(</span><span class="nt">PrimitiveType</span><span class="p">[</span><span class="na">@Kind</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">PrimitiveType</span><span class="p">[</span><span class="na">@Kind</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">FormalParameters</span><span class="o">/</span><span class="nt">FormalParameter</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">typeIsExactly</span><span class="p">(</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="nn">pmd-java:</span><span class="nf">hasAnnotation</span><span class="p">(</span><span class="s1">'java.lang.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="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">FormalParameters</span><span class="o">/</span><span class="nt">FormalParameter</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">typeIsExactly</span><span class="p">(</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 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="nf">lower-case</span><span class="p">(</span><span class="na">@Name</span><span class="p">)</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'hashcode'</span><span class="w">
</span><span class="ow">and</span><span class="w"> </span><span class="na">@Name</span><span class="w"> </span><span class="p">!</span><span class="err">=</span><span class="w"> </span><span class="s1">'hashCode'</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="nt">PrimitiveType</span><span class="p">[</span><span class="na">@Kind</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'int'</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">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 typically end with the suffix "Test", "Tests" or "TestCase". 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" (JUnit3) or use annotations (e.g. <code class="language-plaintext highlighter-rouge">@Test</code>).</p>
<p>The suffix can be configured using the property <code class="language-plaintext highlighter-rouge">testClassPattern</code>. To disable the detection of possible test classes
by name, set this property to an empty string.</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>This rule has the following properties:</strong></p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Default Value</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>testClassPattern</td>
<td>^(?:.*\.)?Test[^\.]*$|^(?:.*\.)?.*Tests?$|^(?:.*\.)?.*TestCase$</td>
<td>Test class name pattern to identify test classes by their fully qualified name. An empty pattern disables test class detection by name. Since PMD 6.51.0.</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/TestClassWithoutTestCases"</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/TestClassWithoutTestCases"</span><span class="nt">&gt;</span>
<span class="nt">&lt;properties&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"testClassPattern"</span> <span class="na">value=</span><span class="s">"^(?:.*\.)?Test[^\.]*$|^(?:.*\.)?.*Tests?$|^(?:.*\.)?.*TestCase$"</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="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="p">[</span><span class="nt">BooleanLiteral</span><span class="p">[</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="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 <code class="language-plaintext highlighter-rouge">assertTrue(false)</code> or similar) or simply removing
statements like <code class="language-plaintext highlighter-rouge">assertTrue(true)</code> and <code class="language-plaintext highlighter-rouge">assertFalse(false)</code>. If you just want a test to halt after finding
an error, use the <code class="language-plaintext highlighter-rouge">fail()</code> 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">ClassDeclaration</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">'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">Annotation</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="w">
</span><span class="o">//</span><span class="nt">MethodCall</span><span class="p">[</span><span class="na">@MethodName</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="p">(</span><span class="s1">'assertTrue'</span><span class="p">,</span><span class="w"> </span><span class="s1">'assertFalse'</span><span class="p">)]</span><span class="w">
</span><span class="p">[</span><span class="nt">ArgumentList</span><span class="w">
</span><span class="p">[</span><span class="w">
</span><span class="nt">BooleanLiteral</span><span class="w"> </span><span class="ow">or</span><span class="w">
</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="nt">BooleanLiteral</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 - remove it</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 XPath expression:</strong></p>
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">MethodCall</span><span class="p">[</span><span class="na">@MethodName</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'toString'</span><span class="p">]</span><span class="w">
</span><span class="p">[</span><span class="nt">ConstructorCall</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="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">'java.lang.Integer'</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.Long'</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">'java.lang.Byte'</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="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.Short'</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="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="o">//</span><span class="nt">InfixExpression</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">'&amp;&amp;'</span><span class="p">]</span><span class="w">
</span><span class="o">/</span><span class="nt">MethodCall</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">matchesSig</span><span class="p">(</span><span class="s2">"java.lang.Object#equals(java.lang.Object)"</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">StringLiteral</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">VariableAccess</span><span class="p">[</span><span class="na">@CompileTimeConstant</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="p">[</span><span class="nt">ArgumentList</span><span class="o">/</span><span class="nt">VariableAccess</span><span class="err">/</span><span class="na">@Name</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="p">..</span><span class="o">//</span><span class="nt">InfixExpression</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="nt">NullLiteral</span><span class="p">]</span><span class="o">/</span><span class="nt">VariableAccess</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">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>This rule only applies to <a href="https://commons.apache.org/proper/commons-logging/">Apache Commons Logging</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">CatchClause</span><span class="o">/</span><span class="nt">Block</span><span class="o">//</span><span class="nt">MethodCall</span><span class="w">
</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">matchesSig</span><span class="p">(</span><span class="s1">'org.apache.commons.logging.Log#_(java.lang.Object)'</span><span class="p">)]</span><span class="w">
</span><span class="p">[</span><span class="nt">ArgumentList</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="nt">MethodCall</span><span class="p">)]</span><span class="o">//</span><span class="nt">VariableAccess</span><span class="err">/</span><span class="na">@Name</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="kn">ancestor::</span><span class="nt">CatchClause</span><span class="o">/</span><span class="nt">CatchParameter</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 is only reliable if the interned string (<code class="language-plaintext highlighter-rouge">String#intern()</code>)
is used on both sides.</p>
<p>Use the <code class="language-plaintext highlighter-rouge">equals()</code> 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">InfixExpression</span><span class="p">[</span><span class="na">@Operator</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="p">(</span><span class="s1">'=='</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">count</span><span class="p">(</span><span class="o">*</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">typeIsExactly</span><span class="p">(</span><span class="s1">'java.lang.String'</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></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">MethodCall</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">matchesSig</span><span class="p">(</span><span class="s2">"java.lang.String#toLowerCase()"</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">matchesSig</span><span class="p">(</span><span class="s2">"java.lang.String#toUpperCase()"</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">MethodCall</span><span class="p">[</span><span class="na">@MethodName</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s2">"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">MethodCall</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">matchesSig</span><span class="p">(</span><span class="s2">"java.lang.Class#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>
<footer>
<hr />
<div>
This documentation is written in markdown. <br />
If there is something missing or can be improved, edit this page on
github and create a PR:
<a
target="_blank"
href="https://github.com/pmd/pmd/blob/master/docs/../pmd-java/src/main/resources/category/java/errorprone.xml"
role="button"
><i class="fab fa-github fa-lg"></i> Edit on GitHub</a
>
</div>
<hr />
<div class="row">
<div class="col-lg-12 footer">
&copy;2024 PMD Open Source Project. All rights
reserved. <br />
Site last generated: Jul 23, 2024 <br />
<p>
<img src="images/logo/pmd-logo-70px.png" alt="PMD
logo"/>
</p>
</div>
</div>
</footer>
</div>
<!-- /.row -->
</div>
<!-- /.container -->
</div>
<!-- Sticky TOC column -->
<div class="toc-col">
<div id="toc"></div>
</div>
<!-- /.toc-container-wrapper -->
</div>
</div>
<script type="application/javascript" src="assets/jquery-3.5.1/jquery-3.5.1.min.js"></script>
<script type="application/javascript" src="assets/anchorjs-4.2.2/anchor.min.js"></script>
<script type="application/javascript" src="assets/navgoco-0.2.1/src/jquery.navgoco.min.js"></script>
<script type="application/javascript" src="assets/bootstrap-4.5.2-dist/js/bootstrap.bundle.min.js"></script>
<script type="application/javascript" src="assets/Simple-Jekyll-Search-1.0.8/dest/jekyll-search.js"></script>
<script type="application/javascript" src="assets/jekyll-table-of-contents/toc.js"></script>
<script type="application/javascript" src="js/tabstate.js"></script>
<script type="application/javascript" src="js/customscripts.js"></script>
</body>
</html>