pmd/pmd_rules_java_codestyle.html

4836 lines
254 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

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

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Rules which enforce a specific coding style.">
<meta name="keywords" content=" Code Style, AtLeastOneConstructor, AvoidDollarSigns, AvoidProtectedFieldInFinalClass, AvoidProtectedMethodInFinalClassNotExtending, AvoidUsingNativeCode, BooleanGetMethodName, CallSuperInConstructor, ClassNamingConventions, CommentDefaultAccessModifier, ConfusingTernary, ControlStatementBraces, EmptyMethodInAbstractClassShouldBeAbstract, EmptyControlStatement, ExtendsObject, FieldDeclarationsShouldBeAtStartOfClass, FieldNamingConventions, FinalParameterInAbstractMethod, ForLoopShouldBeWhileLoop, FormalParameterNamingConventions, GenericsNaming, IdenticalCatchBranches, LinguisticNaming, LocalHomeNamingConvention, LocalInterfaceSessionNamingConvention, LocalVariableCouldBeFinal, LocalVariableNamingConventions, LongVariable, MDBAndSessionBeanNamingConvention, MethodArgumentCouldBeFinal, MethodNamingConventions, NoPackage, UseUnderscoresInNumericLiterals, OnlyOneReturn, PackageCase, PrematureDeclaration, RemoteInterfaceNamingConvention, RemoteSessionInterfaceNamingConvention, ShortClassName, ShortMethodName, ShortVariable, TooManyStaticImports, UnnecessaryAnnotationValueElement, UnnecessaryBoxing, UnnecessaryCast, UnnecessaryConstructor, UnnecessaryFullyQualifiedName, UnnecessaryImport, UnnecessaryLocalBeforeReturn, UnnecessaryModifier, UnnecessaryReturn, UnnecessarySemicolon, UseDiamondOperator, UseExplicitTypes, UselessParentheses, UselessQualifiedThis, UseShortArrayInitializer">
<title>Code Style | 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.0.0-SNAPSHOT</li>
<div class="sidebarTitleDate">Release date: ??-?????-2023</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>
</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 class="active"><a href="pmd_rules_java_codestyle.html">Code Style</a></li>
<li><a href="pmd_rules_java_design.html">Design</a></li>
<li><a href="pmd_rules_java_documentation.html">Documentation</a></li>
<li><a href="pmd_rules_java_errorprone.html">Error Prone</a></li>
<li><a href="pmd_rules_java_multithreading.html">Multithreading</a></li>
<li><a href="pmd_rules_java_performance.html">Performance</a></li>
<li><a href="pmd_rules_java_security.html">Security</a></li>
</ul>
</li>
<li class="subfolders">
<a href="#">Java Server Pages Rules</a>
<ul>
<li><a href="pmd_rules_jsp.html">Index</a></li>
<li><a href="pmd_rules_jsp_bestpractices.html">Best Practices</a></li>
<li><a href="pmd_rules_jsp_codestyle.html">Code Style</a></li>
<li><a href="pmd_rules_jsp_design.html">Design</a></li>
<li><a href="pmd_rules_jsp_errorprone.html">Error Prone</a></li>
<li><a href="pmd_rules_jsp_security.html">Security</a></li>
</ul>
</li>
<li class="subfolders">
<a href="#">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_vf.html">Index</a></li>
<li><a href="pmd_rules_vf_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_vm.html">Index</a></li>
<li><a href="pmd_rules_vm_bestpractices.html">Best Practices</a></li>
<li><a href="pmd_rules_vm_design.html">Design</a></li>
<li><a href="pmd_rules_vm_errorprone.html">Error Prone</a></li>
</ul>
</li>
<li class="subfolders">
<a href="#">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_vm.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>
» Code Style
<a
target="_blank"
href="https://github.com/pmd/pmd/blob/master/docs/../pmd-java/src/main/resources/category/java/codestyle.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">Code Style</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/codestyle.xml">
<div class="summary">Rules which enforce a specific coding style.</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/codestyle.xml. -->
<h2 id="atleastoneconstructor">AtLeastOneConstructor</h2>
<p><strong>Since:</strong> PMD 1.04</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Each non-static class should declare at least one constructor.
Classes with solely static members are ignored, refer to <a href="pmd_rules_java_design.html#useutilityclass">UseUtilityClassRule</a> to detect those.</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/codestyle/AtLeastOneConstructorRule.java">net.sourceforge.pmd.lang.java.rule.codestyle.AtLeastOneConstructorRule</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="c1">// missing constructor</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="o">...</span> <span class="o">}</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="n">doOtherThing</span> <span class="o">{</span> <span class="o">...</span> <span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>This rule has the following properties:</strong></p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Default Value</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>ignoredAnnotations</td>
<td>lombok.Data , lombok.Value , lombok.Builder , lombok.NoArgsConstructor , lombok.RequiredArgsConstructor , lombok.AllArgsConstructor</td>
<td>Fully qualified names of the annotation types that should be ignored by this rule</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/codestyle.xml/AtLeastOneConstructor"</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/codestyle.xml/AtLeastOneConstructor"</span><span class="nt">&gt;</span>
<span class="nt">&lt;properties&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"ignoredAnnotations"</span> <span class="na">value=</span><span class="s">"lombok.Data,lombok.Value,lombok.Builder,lombok.NoArgsConstructor,lombok.RequiredArgsConstructor,lombok.AllArgsConstructor"</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="avoiddollarsigns">AvoidDollarSigns</h2>
<p><strong>Since:</strong> PMD 1.5</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Avoid using dollar signs in variable/method/class/interface names.</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="nf">contains</span><span class="p">(</span><span class="na">@SimpleName</span><span class="p">,</span><span class="w"> </span><span class="s1">'$'</span><span class="p">)]</span><span class="w">
</span><span class="p">|</span><span class="w"> </span><span class="o">//</span><span class="nt">EnumDeclaration</span><span class="w"> </span><span class="p">[</span><span class="nf">contains</span><span class="p">(</span><span class="na">@SimpleName</span><span class="p">,</span><span class="w"> </span><span class="s1">'$'</span><span class="p">)]</span><span class="w">
</span><span class="p">|</span><span class="w"> </span><span class="o">//</span><span class="nt">AnnotationTypeDeclaration</span><span class="w"> </span><span class="p">[</span><span class="nf">contains</span><span class="p">(</span><span class="na">@SimpleName</span><span class="p">,</span><span class="w"> </span><span class="s1">'$'</span><span class="p">)]</span><span class="w">
</span><span class="p">|</span><span class="w"> </span><span class="o">//</span><span class="nt">RecordDeclaration</span><span class="w"> </span><span class="p">[</span><span class="nf">contains</span><span class="p">(</span><span class="na">@SimpleName</span><span class="p">,</span><span class="w"> </span><span class="s1">'$'</span><span class="p">)]</span><span class="w">
</span><span class="p">|</span><span class="w"> </span><span class="o">//</span><span class="nt">VariableId</span><span class="w"> </span><span class="p">[</span><span class="nf">contains</span><span class="p">(</span><span class="na">@Name</span><span class="p">,</span><span class="w"> </span><span class="s1">'$'</span><span class="p">)]</span><span class="w">
</span><span class="p">|</span><span class="w"> </span><span class="o">//</span><span class="nt">MethodDeclaration</span><span class="w"> </span><span class="p">[</span><span class="nf">contains</span><span class="p">(</span><span class="na">@Name</span><span class="p">,</span><span class="w"> </span><span class="s1">'$'</span><span class="p">)]</span><span class="w">
</span></code></pre></div></div>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Fo</span><span class="n">$o</span> <span class="o">{</span> <span class="c1">// not a recommended name</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/codestyle.xml/AvoidDollarSigns"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="avoidprotectedfieldinfinalclass">AvoidProtectedFieldInFinalClass</h2>
<p><strong>Since:</strong> PMD 2.1</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Do not use protected fields in final classes since they cannot be subclassed.
Clarify your intent by using private or package access modifiers 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">ClassDeclaration</span><span class="p">[</span><span class="na">@Final</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="nf">true</span><span class="p">()]</span><span class="w">
</span><span class="o">/</span><span class="nt">ClassBody</span><span class="w">
</span><span class="o">/</span><span class="nt">FieldDeclaration</span><span class="p">[</span><span class="na">@Visibility</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s2">"protected"</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">final</span> <span class="kd">class</span> <span class="nc">Bar</span> <span class="o">{</span>
<span class="kd">private</span> <span class="kt">int</span> <span class="n">x</span><span class="o">;</span>
<span class="kd">protected</span> <span class="kt">int</span> <span class="n">y</span><span class="o">;</span> <span class="c1">// bar cannot be subclassed, so is y really private or package visible?</span>
<span class="nc">Bar</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/codestyle.xml/AvoidProtectedFieldInFinalClass"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="avoidprotectedmethodinfinalclassnotextending">AvoidProtectedMethodInFinalClassNotExtending</h2>
<p><strong>Since:</strong> PMD 5.1</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Do not use protected methods in most final classes since they cannot be subclassed. This should
only be allowed in final classes that extend other classes with protected methods (whose
visibility cannot be reduced). Clarify your intent by using private or package access modifiers 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">ClassDeclaration</span><span class="p">[</span><span class="na">@Final</span><span class="err">=</span><span class="w"> </span><span class="nf">true</span><span class="p">()</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="ow">not</span><span class="p">(</span><span class="nt">ExtendsList</span><span class="p">)]</span><span class="w">
</span><span class="o">/</span><span class="nt">ClassBody</span><span class="w">
</span><span class="o">/</span><span class="nt">MethodDeclaration</span><span class="p">[</span><span class="na">@Visibility</span><span class="err">=</span><span class="s2">"protected"</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">'finalize'</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">final</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
<span class="kd">private</span> <span class="kt">int</span> <span class="nf">bar</span><span class="o">()</span> <span class="o">{}</span>
<span class="kd">protected</span> <span class="kt">int</span> <span class="nf">baz</span><span class="o">()</span> <span class="o">{}</span> <span class="c1">// Foo cannot be subclassed, and doesn't extend anything, so is baz() really private or package visible?</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/codestyle.xml/AvoidProtectedMethodInFinalClassNotExtending"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="avoidusingnativecode">AvoidUsingNativeCode</h2>
<p><strong>Since:</strong> PMD 4.1</p>
<p><strong>Priority:</strong> Medium High (2)</p>
<p>Unnecessary reliance on Java Native Interface (JNI) calls directly reduces application portability
and increases the maintenance burden.</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="nt">TypeExpression</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.lang.System'</span><span class="p">)]</span><span class="w">
</span><span class="ow">and</span><span class="w"> </span><span class="na">@MethodName</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'loadLibrary'</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">SomeJNIClass</span> <span class="o">{</span>
<span class="kd">public</span> <span class="nf">SomeJNIClass</span><span class="o">()</span> <span class="o">{</span>
<span class="nc">System</span><span class="o">.</span><span class="na">loadLibrary</span><span class="o">(</span><span class="s">"nativelib"</span><span class="o">);</span>
<span class="o">}</span>
<span class="kd">static</span> <span class="o">{</span>
<span class="nc">System</span><span class="o">.</span><span class="na">loadLibrary</span><span class="o">(</span><span class="s">"nativelib"</span><span class="o">);</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">invalidCallsInMethod</span><span class="o">()</span> <span class="kd">throws</span> <span class="nc">SecurityException</span><span class="o">,</span> <span class="nc">NoSuchMethodException</span> <span class="o">{</span>
<span class="nc">System</span><span class="o">.</span><span class="na">loadLibrary</span><span class="o">(</span><span class="s">"nativelib"</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/codestyle.xml/AvoidUsingNativeCode"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="booleangetmethodname">BooleanGetMethodName</h2>
<p><strong>Since:</strong> PMD 4.0</p>
<p><strong>Priority:</strong> Medium Low (4)</p>
<p>Methods that return boolean results should be named as predicate statements to denote this.
I.e, isReady(), hasValues(), canCommit(), willFail(), etc. Avoid the use of the get
prefix for these 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">MethodDeclaration</span><span class="w">
</span><span class="p">[</span><span class="nf">starts-with</span><span class="p">(</span><span class="na">@Name</span><span class="p">,</span><span class="w"> </span><span class="s1">'get'</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="w"> </span><span class="ow">or</span><span class="w"> </span><span class="nv">$checkParameterizedMethods</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="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="na">@Overridden</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="nf">false</span><span class="p">()</span><span class="w"> </span><span class="p">]</span><span class="w">
</span></code></pre></div></div>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kt">boolean</span> <span class="nf">getFoo</span><span class="o">();</span> <span class="c1">// bad</span>
<span class="kd">public</span> <span class="kt">boolean</span> <span class="nf">isFoo</span><span class="o">();</span> <span class="c1">// ok</span>
<span class="kd">public</span> <span class="kt">boolean</span> <span class="nf">getFoo</span><span class="o">(</span><span class="kt">boolean</span> <span class="n">bar</span><span class="o">);</span> <span class="c1">// ok, unless checkParameterizedMethods=true</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>checkParameterizedMethods</td>
<td>false</td>
<td>Check parameterized methods</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/codestyle.xml/BooleanGetMethodName"</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/codestyle.xml/BooleanGetMethodName"</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">"checkParameterizedMethods"</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="callsuperinconstructor">CallSuperInConstructor</h2>
<p><strong>Since:</strong> PMD 3.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>It is a good practice to call super() in a constructor. If super() is not called but
another constructor (such as an overloaded constructor) is called, this rule will not report it.</p>
<p><strong>This rule is defined by the following XPath expression:</strong></p>
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">ClassDeclaration</span><span class="p">[</span><span class="nt">ExtendsList</span><span class="err">/*</span><span class="p">]</span><span class="w">
</span><span class="o">/</span><span class="nt">ClassBody</span><span class="w">
</span><span class="o">/</span><span class="nt">ConstructorDeclaration</span><span class="p">[</span><span class="w"> </span><span class="ow">not</span><span class="p">(</span><span class="nt">Block</span><span class="o">/</span><span class="nt">ExplicitConstructorInvocation</span><span class="p">)</span><span class="w"> </span><span class="p">]</span><span class="w">
</span></code></pre></div></div>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="kd">extends</span> <span class="nc">Bar</span><span class="o">{</span>
<span class="kd">public</span> <span class="nf">Foo</span><span class="o">()</span> <span class="o">{</span>
<span class="c1">// call the constructor of Bar</span>
<span class="kd">super</span><span class="o">();</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="nf">Foo</span><span class="o">(</span><span class="kt">int</span> <span class="n">code</span><span class="o">)</span> <span class="o">{</span>
<span class="c1">// do something with code</span>
<span class="k">this</span><span class="o">();</span>
<span class="c1">// no problem with this</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>Use this rule by referencing it:</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;rule</span> <span class="na">ref=</span><span class="s">"category/java/codestyle.xml/CallSuperInConstructor"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="classnamingconventions">ClassNamingConventions</h2>
<p><strong>Since:</strong> PMD 1.2</p>
<p><strong>Priority:</strong> High (1)</p>
<p>Configurable naming conventions for type declarations. This rule reports
type declarations which do not match the regex that applies to their
specific kind (e.g. enum or interface). Each regex can be configured through
properties.</p>
<p>By default, this rule uses the standard Java naming convention (Pascal case).</p>
<p>The rule can detect utility classes and enforce a different naming convention
on those. E.g. setting the property <code class="language-plaintext highlighter-rouge">utilityClassPattern</code> to
<code class="language-plaintext highlighter-rouge">[A-Z][a-zA-Z0-9]+(Utils?|Helper|Constants)</code> reports any utility class, whose name
does not end in "Util(s)", "Helper" or "Constants".</p>
<p>For this rule, a utility class is defined as: a concrete class that does not
inherit from a super class or implement any interface and only has static fields
or methods.</p>
<p>This rule detects test classes using the following convention: Test classes are top-level classes, that
either inherit from JUnit 3 TestCase or have at least one method annotated with the Test annotations from
JUnit4/5 or TestNG.</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/codestyle/ClassNamingConventionsRule.java">net.sourceforge.pmd.lang.java.rule.codestyle.ClassNamingConventionsRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">// This is Pascal case, the recommended naming convention in Java</span>
<span class="c1">// Note that the default values of this rule don't allow underscores</span>
<span class="c1">// or accented characters in type names</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">FooBar</span> <span class="o">{}</span>
<span class="c1">// You may want abstract classes to be named 'AbstractXXX',</span>
<span class="c1">// in which case you can customize the regex for abstract</span>
<span class="c1">// classes to 'Abstract[A-Z]\w+'</span>
<span class="kd">public</span> <span class="kd">abstract</span> <span class="kd">class</span> <span class="nc">Thing</span> <span class="o">{}</span>
<span class="c1">// This class doesn't respect the convention, and will be flagged</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">Éléphant</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>classPattern</td>
<td>[A-Z][a-zA-Z0-9]*</td>
<td>Regex which applies to concrete class names</td>
</tr>
<tr>
<td>abstractClassPattern</td>
<td>[A-Z][a-zA-Z0-9]*</td>
<td>Regex which applies to abstract class names</td>
</tr>
<tr>
<td>interfacePattern</td>
<td>[A-Z][a-zA-Z0-9]*</td>
<td>Regex which applies to interface names</td>
</tr>
<tr>
<td>enumPattern</td>
<td>[A-Z][a-zA-Z0-9]*</td>
<td>Regex which applies to enum names</td>
</tr>
<tr>
<td>annotationPattern</td>
<td>[A-Z][a-zA-Z0-9]*</td>
<td>Regex which applies to annotation names</td>
</tr>
<tr>
<td>utilityClassPattern</td>
<td>[A-Z][a-zA-Z0-9]*</td>
<td>Regex which applies to utility class names</td>
</tr>
<tr>
<td>testClassPattern</td>
<td>^Test.*$|^[A-Z][a-zA-Z0-9]*Test(s|Case)?$</td>
<td>Regex which applies to test class names. Since PMD 6.52.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/codestyle.xml/ClassNamingConventions"</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/codestyle.xml/ClassNamingConventions"</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">"classPattern"</span> <span class="na">value=</span><span class="s">"[A-Z][a-zA-Z0-9]*"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"abstractClassPattern"</span> <span class="na">value=</span><span class="s">"[A-Z][a-zA-Z0-9]*"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"interfacePattern"</span> <span class="na">value=</span><span class="s">"[A-Z][a-zA-Z0-9]*"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"enumPattern"</span> <span class="na">value=</span><span class="s">"[A-Z][a-zA-Z0-9]*"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"annotationPattern"</span> <span class="na">value=</span><span class="s">"[A-Z][a-zA-Z0-9]*"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"utilityClassPattern"</span> <span class="na">value=</span><span class="s">"[A-Z][a-zA-Z0-9]*"</span> <span class="nt">/&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.*$|^[A-Z][a-zA-Z0-9]*Test(s|Case)?$"</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="commentdefaultaccessmodifier">CommentDefaultAccessModifier</h2>
<p><strong>Since:</strong> PMD 5.4.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>To avoid mistakes if we want that an Annotation, Class, Enum, Method, Constructor or Field have a default access modifier
we must add a comment at the beginning of its declaration.
By default, the comment must be <code class="language-plaintext highlighter-rouge">/* default */</code> or <code class="language-plaintext highlighter-rouge">/* package */</code>, if you want another, you have to provide a regular expression.</p>
<p>This rule ignores by default all cases that have a <code class="language-plaintext highlighter-rouge">@VisibleForTesting</code> annotation or any JUnit5/TestNG annotation. Use the
property "ignoredAnnotations" to customize the recognized annotations.</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/codestyle/CommentDefaultAccessModifierRule.java">net.sourceforge.pmd.lang.java.rule.codestyle.CommentDefaultAccessModifierRule</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">final</span> <span class="nc">String</span> <span class="n">stringValue</span> <span class="o">=</span> <span class="s">"some string"</span><span class="o">;</span>
<span class="nc">String</span> <span class="nf">getString</span><span class="o">()</span> <span class="o">{</span>
<span class="k">return</span> <span class="n">stringValue</span><span class="o">;</span>
<span class="o">}</span>
<span class="kd">class</span> <span class="nc">NestedFoo</span> <span class="o">{</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="c1">// should be</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
<span class="cm">/* default */</span> <span class="kd">final</span> <span class="nc">String</span> <span class="n">stringValue</span> <span class="o">=</span> <span class="s">"some string"</span><span class="o">;</span>
<span class="cm">/* default */</span> <span class="nc">String</span> <span class="nf">getString</span><span class="o">()</span> <span class="o">{</span>
<span class="k">return</span> <span class="n">stringValue</span><span class="o">;</span>
<span class="o">}</span>
<span class="cm">/* default */</span> <span class="kd">class</span> <span class="nc">NestedFoo</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>ignoredAnnotations</td>
<td>android.support.annotation.VisibleForTesting , co.elastic.clients.util.VisibleForTesting , com.google.common.annotations.VisibleForTesting , org.junit.jupiter.api.AfterAll , org.junit.jupiter.api.AfterEach , org.junit.jupiter.api.BeforeAll , org.junit.jupiter.api.BeforeEach , org.junit.jupiter.api.RepeatedTest , org.junit.jupiter.api.Test , org.junit.jupiter.api.TestFactory , org.junit.jupiter.api.TestTemplate , org.junit.jupiter.api.extension.RegisterExtension , org.junit.jupiter.params.ParameterizedTest , org.testng.annotations.AfterClass , org.testng.annotations.AfterGroups , org.testng.annotations.AfterMethod , org.testng.annotations.AfterSuite , org.testng.annotations.AfterTest , org.testng.annotations.BeforeClass , org.testng.annotations.BeforeGroups , org.testng.annotations.BeforeMethod , org.testng.annotations.BeforeSuite , org.testng.annotations.BeforeTest , org.testng.annotations.Test</td>
<td>Fully qualified names of the annotation types that should be ignored by this rule</td>
</tr>
<tr>
<td>regex</td>
<td>\/\*\s*(default|package)\s*\*\/</td>
<td>Regular expression</td>
</tr>
<tr>
<td>checkTopLevelTypes</td>
<td>false</td>
<td>Check for default access modifier in top-level classes, annotations, and enums</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/codestyle.xml/CommentDefaultAccessModifier"</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/codestyle.xml/CommentDefaultAccessModifier"</span><span class="nt">&gt;</span>
<span class="nt">&lt;properties&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"ignoredAnnotations"</span> <span class="na">value=</span><span class="s">"android.support.annotation.VisibleForTesting,co.elastic.clients.util.VisibleForTesting,com.google.common.annotations.VisibleForTesting,org.junit.jupiter.api.AfterAll,org.junit.jupiter.api.AfterEach,org.junit.jupiter.api.BeforeAll,org.junit.jupiter.api.BeforeEach,org.junit.jupiter.api.RepeatedTest,org.junit.jupiter.api.Test,org.junit.jupiter.api.TestFactory,org.junit.jupiter.api.TestTemplate,org.junit.jupiter.api.extension.RegisterExtension,org.junit.jupiter.params.ParameterizedTest,org.testng.annotations.AfterClass,org.testng.annotations.AfterGroups,org.testng.annotations.AfterMethod,org.testng.annotations.AfterSuite,org.testng.annotations.AfterTest,org.testng.annotations.BeforeClass,org.testng.annotations.BeforeGroups,org.testng.annotations.BeforeMethod,org.testng.annotations.BeforeSuite,org.testng.annotations.BeforeTest,org.testng.annotations.Test"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"regex"</span> <span class="na">value=</span><span class="s">"\/\*\s*(default|package)\s*\*\/"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"checkTopLevelTypes"</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="confusingternary">ConfusingTernary</h2>
<p><strong>Since:</strong> PMD 1.9</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Avoid negation within an "if" expression with an "else" clause. For example, rephrase:
<code class="language-plaintext highlighter-rouge">if (x != y) diff(); else same();</code> as: <code class="language-plaintext highlighter-rouge">if (x == y) same(); else diff();</code>.</p>
<p>Most "if (x != y)" cases without an "else" are often return cases, so consistent use of this
rule makes the code easier to read. Also, this resolves trivial ordering problems, such
as "does the error case go first?" or "does the common case go first?".</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/codestyle/ConfusingTernaryRule.java">net.sourceforge.pmd.lang.java.rule.codestyle.ConfusingTernaryRule</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="nf">bar</span><span class="o">(</span><span class="kt">int</span> <span class="n">x</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="k">return</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="o">?</span> <span class="n">diff</span> <span class="o">:</span> <span class="n">same</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>ignoreElseIf</td>
<td>false</td>
<td>Ignore conditions with an else-if case</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/codestyle.xml/ConfusingTernary"</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/codestyle.xml/ConfusingTernary"</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">"ignoreElseIf"</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="controlstatementbraces">ControlStatementBraces</h2>
<p><strong>Since:</strong> PMD 6.2.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Enforce a policy for braces on control statements. It is recommended to use braces on if … else
statements and loop statements, even if they are optional. This usually makes the code clearer, and
helps prepare the future when you need to add another statement. That said, this rule lets you control
which statements are required to have braces via properties.</p>
<p>From 6.2.0 on, this rule supersedes WhileLoopMustUseBraces, ForLoopMustUseBraces, IfStmtMustUseBraces,
and IfElseStmtMustUseBraces.</p>
<p><strong>This rule is defined by the following XPath expression:</strong></p>
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">WhileStatement</span><span class="p">[</span><span class="nv">$checkWhileStmt</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">Block</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="nv">$allowEmptyLoop</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="nt">EmptyStatement</span><span class="p">)]</span><span class="w">
</span><span class="p">|</span><span class="w">
</span><span class="o">//</span><span class="nt">ForStatement</span><span class="p">[</span><span class="nv">$checkForStmt</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">Block</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="nv">$allowEmptyLoop</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="nt">EmptyStatement</span><span class="p">)]</span><span class="w">
</span><span class="p">|</span><span class="w">
</span><span class="o">//</span><span class="nt">ForeachStatement</span><span class="p">[</span><span class="nv">$checkForStmt</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">Block</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="nv">$allowEmptyLoop</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="nt">EmptyStatement</span><span class="p">)]</span><span class="w">
</span><span class="p">|</span><span class="w">
</span><span class="o">//</span><span class="nt">DoStatement</span><span class="p">[</span><span class="nv">$checkDoWhileStmt</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">Block</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="nv">$allowEmptyLoop</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="nt">EmptyStatement</span><span class="p">)]</span><span class="w">
</span><span class="p">|</span><span class="w">
</span><span class="c">(: The violation is reported on the sub statement -- not the if statement :)</span><span class="w">
</span><span class="o">//</span><span class="nt">IfStatement</span><span class="p">[</span><span class="nv">$checkIfElseStmt</span><span class="p">]</span><span class="w">
</span><span class="err">/</span><span class="o">*</span><span class="p">[</span><span class="nf">position</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="ow">and</span><span class="w"> </span><span class="ow">not</span><span class="p">(</span><span class="kn">self::</span><span class="nt">Block</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="kn">self::</span><span class="nt">IfStatement</span><span class="p">)]</span><span class="w">
</span><span class="p">[</span><span class="w"> </span><span class="nv">$checkSingleIfStmt</span><span class="w">
</span><span class="c">(: Inside this (...) is the definition of a "single if statement" :)</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="kn">parent::</span><span class="err">*/</span><span class="na">@Else</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">(: No else stmt :)</span><span class="w">
</span><span class="c">(: Not the last branch of an 'if ... else if' chain :)</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">IfStatement</span><span class="p">[</span><span class="kn">parent::</span><span class="nt">IfStatement</span><span class="p">]))]</span><span class="w">
</span><span class="p">|</span><span class="w">
</span><span class="c">(: Reports case labels if one of their subordinate statements is not braced :)</span><span class="w">
</span><span class="o">//</span><span class="nt">SwitchFallthroughBranch</span><span class="p">[</span><span class="nv">$checkCaseStmt</span><span class="p">]</span><span class="w">
</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="o">&gt;</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="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="o">&gt;</span><span class="w"> </span><span class="m">2</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="kn">child::</span><span class="o">*</span><span class="p">[</span><span class="m">2</span><span class="p">]</span><span class="o">/</span><span class="kn">self::</span><span class="nt">Block</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">while</span> <span class="o">(</span><span class="kc">true</span><span class="o">)</span> <span class="c1">// not recommended</span>
<span class="n">x</span><span class="o">++;</span>
<span class="k">while</span> <span class="o">(</span><span class="kc">true</span><span class="o">)</span> <span class="o">{</span> <span class="c1">// preferred approach</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>checkIfElseStmt</td>
<td>true</td>
<td>Require that if … else statements use braces</td>
</tr>
<tr>
<td>checkSingleIfStmt</td>
<td>true</td>
<td>Require that if statements with a single branch use braces</td>
</tr>
<tr>
<td>checkWhileStmt</td>
<td>true</td>
<td>Require that while loops use braces</td>
</tr>
<tr>
<td>checkForStmt</td>
<td>true</td>
<td>Require that for loops should use braces</td>
</tr>
<tr>
<td>checkDoWhileStmt</td>
<td>true</td>
<td>Require that do … while loops use braces</td>
</tr>
<tr>
<td>checkCaseStmt</td>
<td>false</td>
<td>Require that cases of a switch have braces</td>
</tr>
<tr>
<td>allowEmptyLoop</td>
<td>false</td>
<td>Allow loops with an empty statement, e.g. while(true);</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/codestyle.xml/ControlStatementBraces"</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/codestyle.xml/ControlStatementBraces"</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">"checkIfElseStmt"</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">"checkSingleIfStmt"</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">"checkWhileStmt"</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">"checkForStmt"</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">"checkDoWhileStmt"</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">"checkCaseStmt"</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">"allowEmptyLoop"</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="emptycontrolstatement">EmptyControlStatement</h2>
<p><strong>Since:</strong> PMD 6.46.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Reports control statements whose body is empty, as well as empty initializers.</p>
<p>The checked code constructs are the following:</p>
<ul>
<li>bodies of <code class="language-plaintext highlighter-rouge">try</code> statements</li>
<li><code class="language-plaintext highlighter-rouge">finally</code> clauses of <code class="language-plaintext highlighter-rouge">try</code> statements</li>
<li><code class="language-plaintext highlighter-rouge">switch</code> statements</li>
<li><code class="language-plaintext highlighter-rouge">synchronized</code> statements</li>
<li><code class="language-plaintext highlighter-rouge">if</code> statements</li>
<li>loop statements: <code class="language-plaintext highlighter-rouge">while</code>, <code class="language-plaintext highlighter-rouge">for</code>, <code class="language-plaintext highlighter-rouge">do .. while</code></li>
<li>initializers</li>
<li>blocks used as statements (for scoping)</li>
</ul>
<p>This rule replaces the rules EmptyFinallyBlock,
EmptyIfStmt, EmptyInitializer, EmptyStatementBlock,
EmptySwitchStatements, EmptySynchronizedBlock, EmptyTryBlock, and EmptyWhileStmt.</p>
<p>Notice that <a href="pmd_rules_java_errorprone.html#emptycatchblock"><code class="language-plaintext highlighter-rouge">EmptyCatchBlock</code></a> is still an independent rule.</p>
<p>EmptyStatementNotInLoop is replaced by <a href="pmd_rules_java_codestyle.html#unnecessarysemicolon"><code class="language-plaintext highlighter-rouge">UnnecessarySemicolon</code></a>.</p>
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/EmptyControlStatementRule.java">net.sourceforge.pmd.lang.java.rule.codestyle.EmptyControlStatementRule</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="o">{</span>
<span class="k">if</span> <span class="o">(</span><span class="kc">true</span><span class="o">);</span> <span class="c1">// empty if statement</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">// empty as well</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="o">{}</span> <span class="c1">// empty initializer</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>Option for allowing empty but commented blocks. This is useful where a developer wants to have the code structure and explain why a condition does not require logic or to hold TODO comments for future work.</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/codestyle.xml/EmptyControlStatement"</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/codestyle.xml/EmptyControlStatement"</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;/properties&gt;</span>
<span class="nt">&lt;/rule&gt;</span>
</code></pre></div></div>
<h2 id="emptymethodinabstractclassshouldbeabstract">EmptyMethodInAbstractClassShouldBeAbstract</h2>
<p><strong>Since:</strong> PMD 4.1</p>
<p><strong>Priority:</strong> High (1)</p>
<p>Empty or auto-generated methods in an abstract class should be tagged as abstract. This helps to remove their inapproprate
usage by developers who should be implementing their own versions in the concrete subclasses.</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="na">@RegularClass</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="nf">true</span><span class="p">()</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="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">"abstract"</span><span class="p">]</span><span class="w">
</span><span class="o">/</span><span class="nt">ClassBody</span><span class="w">
</span><span class="o">/</span><span class="nt">MethodDeclaration</span><span class="w">
</span><span class="p">[</span><span class="nt">Block</span><span class="p">[</span><span class="w">
</span><span class="ow">le</span><span class="nt">t</span><span class="w"> </span><span class="nv">$size</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nf">count</span><span class="p">(</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">EmptyStatement</span><span class="p">)])</span><span class="w">
</span><span class="kr">return</span><span class="w"> </span><span class="nv">$size</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">or</span><span class="w"> </span><span class="nv">$size</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">ReturnStatement</span><span class="p">[</span><span class="w"> </span><span class="nt">NullLiteral</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="nt">NumericLiteral</span><span class="p">[</span><span class="na">@ValueAsInt</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="ow">or</span><span class="w"> </span><span class="nt">StringLiteral</span><span class="p">[</span><span class="na">@Empty</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="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">abstract</span> <span class="kd">class</span> <span class="nc">ShouldBeAbstract</span> <span class="o">{</span>
<span class="kd">public</span> <span class="nc">Object</span> <span class="nf">couldBeAbstract</span><span class="o">()</span> <span class="o">{</span>
<span class="c1">// Should be abstract method ?</span>
<span class="k">return</span> <span class="kc">null</span><span class="o">;</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">couldBeAbstract</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/codestyle.xml/EmptyMethodInAbstractClassShouldBeAbstract"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="extendsobject">ExtendsObject</h2>
<p><strong>Since:</strong> PMD 5.0</p>
<p><strong>Priority:</strong> Medium Low (4)</p>
<p>No need to explicitly extend Object.</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">ExtendsList</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.Object'</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">Object</span> <span class="o">{</span> <span class="c1">// not required</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/codestyle.xml/ExtendsObject"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="fielddeclarationsshouldbeatstartofclass">FieldDeclarationsShouldBeAtStartOfClass</h2>
<p><strong>Since:</strong> PMD 5.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Fields should be declared at the top of the class, before any method declarations, constructors, initializers or inner classes.</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/codestyle/FieldDeclarationsShouldBeAtStartOfClassRule.java">net.sourceforge.pmd.lang.java.rule.codestyle.FieldDeclarationsShouldBeAtStartOfClassRule</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">HelloWorldBean</span> <span class="o">{</span>
<span class="c1">// Field declared before methods / inner classes - OK</span>
<span class="kd">private</span> <span class="nc">String</span> <span class="n">_thing</span><span class="o">;</span>
<span class="kd">public</span> <span class="nc">String</span> <span class="nf">getMessage</span><span class="o">()</span> <span class="o">{</span>
<span class="k">return</span> <span class="s">"Hello World!"</span><span class="o">;</span>
<span class="o">}</span>
<span class="c1">// Field declared after methods / inner classes - avoid this</span>
<span class="kd">private</span> <span class="nc">String</span> <span class="n">_fieldInWrongLocation</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>ignoreAnonymousClassDeclarations</td>
<td>true</td>
<td>Ignore field declarations, that are initialized with an anonymous class creation expression</td>
</tr>
<tr>
<td>ignoreInterfaceDeclarations</td>
<td>false</td>
<td>Ignore interface declarations that precede fields</td>
</tr>
<tr>
<td>ignoreEnumDeclarations</td>
<td>true</td>
<td>Ignore enum declarations that precede fields</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/codestyle.xml/FieldDeclarationsShouldBeAtStartOfClass"</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/codestyle.xml/FieldDeclarationsShouldBeAtStartOfClass"</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">"ignoreAnonymousClassDeclarations"</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">"ignoreInterfaceDeclarations"</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">"ignoreEnumDeclarations"</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="fieldnamingconventions">FieldNamingConventions</h2>
<p><strong>Since:</strong> PMD 6.7.0</p>
<p><strong>Priority:</strong> High (1)</p>
<p>Configurable naming conventions for field declarations. This rule reports variable declarations
which do not match the regex that applies to their specific kind —e.g. constants (static final),
enum constant, final field. Each regex can be configured through properties.</p>
<p>By default this rule uses the standard Java naming convention (Camel case), and uses the ALL_UPPER
convention for constants and enum constants.</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/codestyle/FieldNamingConventionsRule.java">net.sourceforge.pmd.lang.java.rule.codestyle.FieldNamingConventionsRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
<span class="kt">int</span> <span class="n">myField</span> <span class="o">=</span> <span class="mi">1</span><span class="o">;</span> <span class="c1">// This is in camel case, so it's ok</span>
<span class="kt">int</span> <span class="n">my_Field</span> <span class="o">=</span> <span class="mi">1</span><span class="o">;</span> <span class="c1">// This contains an underscore, it's not ok by default</span>
<span class="c1">// but you may allow it, or even require the "my_" prefix</span>
<span class="kd">final</span> <span class="kt">int</span> <span class="nc">FinalField</span> <span class="o">=</span> <span class="mi">1</span><span class="o">;</span> <span class="c1">// you may configure a different convention for final fields,</span>
<span class="c1">// e.g. here PascalCase: [A-Z][a-zA-Z0-9]*</span>
<span class="kd">interface</span> <span class="nc">Interface</span> <span class="o">{</span>
<span class="kt">double</span> <span class="no">PI</span> <span class="o">=</span> <span class="mf">3.14</span><span class="o">;</span> <span class="c1">// interface "fields" use the constantPattern property</span>
<span class="o">}</span>
<span class="kd">enum</span> <span class="nc">AnEnum</span> <span class="o">{</span>
<span class="no">ORG</span><span class="o">,</span> <span class="no">NET</span><span class="o">,</span> <span class="no">COM</span><span class="o">;</span> <span class="c1">// These use a separate property but are set to ALL_UPPER by default</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>publicConstantPattern</td>
<td>[A-Z][A-Z_0-9]*</td>
<td>Regex which applies to public constant names</td>
</tr>
<tr>
<td>constantPattern</td>
<td>[A-Z][A-Z_0-9]*</td>
<td>Regex which applies to non-public static final field names</td>
</tr>
<tr>
<td>enumConstantPattern</td>
<td>[A-Z][A-Z_0-9]*</td>
<td>Regex which applies to enum constant names</td>
</tr>
<tr>
<td>finalFieldPattern</td>
<td>[a-z][a-zA-Z0-9]*</td>
<td>Regex which applies to final field names</td>
</tr>
<tr>
<td>staticFieldPattern</td>
<td>[a-z][a-zA-Z0-9]*</td>
<td>Regex which applies to static field names</td>
</tr>
<tr>
<td>defaultFieldPattern</td>
<td>[a-z][a-zA-Z0-9]*</td>
<td>Regex which applies to field names</td>
</tr>
<tr>
<td>exclusions</td>
<td>serialVersionUID , serialPersistentFields</td>
<td>Names of fields to whitelist.</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/codestyle.xml/FieldNamingConventions"</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/codestyle.xml/FieldNamingConventions"</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">"publicConstantPattern"</span> <span class="na">value=</span><span class="s">"[A-Z][A-Z_0-9]*"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"constantPattern"</span> <span class="na">value=</span><span class="s">"[A-Z][A-Z_0-9]*"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"enumConstantPattern"</span> <span class="na">value=</span><span class="s">"[A-Z][A-Z_0-9]*"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"finalFieldPattern"</span> <span class="na">value=</span><span class="s">"[a-z][a-zA-Z0-9]*"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"staticFieldPattern"</span> <span class="na">value=</span><span class="s">"[a-z][a-zA-Z0-9]*"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"defaultFieldPattern"</span> <span class="na">value=</span><span class="s">"[a-z][a-zA-Z0-9]*"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"exclusions"</span> <span class="na">value=</span><span class="s">"serialVersionUID,serialPersistentFields"</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="finalparameterinabstractmethod">FinalParameterInAbstractMethod</h2>
<p><strong>Since:</strong> PMD 6.42.0</p>
<p><strong>Priority:</strong> High (1)</p>
<p>Declaring a method parameter as final for an interface method is useless because the implementation may choose to not respect it.</p>
<p><strong>This rule is defined by the following XPath expression:</strong></p>
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">MethodDeclaration</span><span class="w">
</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="na">@Final</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="ow">not</span><span class="p">(</span><span class="nt">Block</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">interface</span> <span class="nc">MyInterface</span> <span class="o">{</span>
<span class="kt">void</span> <span class="nf">process</span><span class="o">(</span><span class="kd">final</span> <span class="nc">Object</span> <span class="n">arg</span><span class="o">);</span> <span class="c1">// Avoid using final 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/codestyle.xml/FinalParameterInAbstractMethod"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="forloopshouldbewhileloop">ForLoopShouldBeWhileLoop</h2>
<p><strong>Since:</strong> PMD 1.02</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Some for loops can be simplified to while loops, this makes them more concise.</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="p">[</span><span class="ow">not</span><span class="p">(</span><span class="nt">ForInit</span><span class="w"> </span><span class="p">|</span><span class="w"> </span><span class="nt">ForUpdate</span><span class="p">)</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="nf">count</span><span class="p">(</span><span class="err">*</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="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">for</span> <span class="o">(;</span><span class="kc">true</span><span class="o">;)</span> <span class="kc">true</span><span class="o">;</span> <span class="c1">// No Init or Update part, may as well be: while (true)</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/codestyle.xml/ForLoopShouldBeWhileLoop"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="formalparameternamingconventions">FormalParameterNamingConventions</h2>
<p><strong>Since:</strong> PMD 6.6.0</p>
<p><strong>Priority:</strong> High (1)</p>
<p>Configurable naming conventions for formal parameters of methods and lambdas.
This rule reports formal parameters which do not match the regex that applies to their
specific kind (e.g. lambda parameter, or final formal parameter). Each regex can be
configured through properties.</p>
<p>By default this rule uses the standard Java naming convention (Camel case).</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/codestyle/FormalParameterNamingConventionsRule.java">net.sourceforge.pmd.lang.java.rule.codestyle.FormalParameterNamingConventionsRule</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">abstract</span> <span class="kt">void</span> <span class="nf">bar</span><span class="o">(</span><span class="kt">int</span> <span class="n">myInt</span><span class="o">);</span> <span class="c1">// This is Camel case, so it's ok</span>
<span class="kt">void</span> <span class="nf">bar</span><span class="o">(</span><span class="kt">int</span> <span class="n">my_i</span><span class="o">)</span> <span class="o">{</span> <span class="c1">// this will be reported</span>
<span class="o">}</span>
<span class="kt">void</span> <span class="nf">lambdas</span><span class="o">()</span> <span class="o">{</span>
<span class="c1">// lambdas parameters can be configured separately</span>
<span class="nc">Consumer</span><span class="o">&lt;</span><span class="nc">String</span><span class="o">&gt;</span> <span class="n">lambda1</span> <span class="o">=</span> <span class="n">s_str</span> <span class="o">-&gt;</span> <span class="o">{</span> <span class="o">};</span>
<span class="c1">// lambda parameters with an explicit type can be configured separately</span>
<span class="nc">Consumer</span><span class="o">&lt;</span><span class="nc">String</span><span class="o">&gt;</span> <span class="n">lambda1</span> <span class="o">=</span> <span class="o">(</span><span class="nc">String</span> <span class="n">str</span><span class="o">)</span> <span class="o">-&gt;</span> <span class="o">{</span> <span class="o">};</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>This rule has the following properties:</strong></p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Default Value</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>methodParameterPattern</td>
<td>[a-z][a-zA-Z0-9]*</td>
<td>Regex which applies to formal parameter names</td>
</tr>
<tr>
<td>finalMethodParameterPattern</td>
<td>[a-z][a-zA-Z0-9]*</td>
<td>Regex which applies to final formal parameter names</td>
</tr>
<tr>
<td>lambdaParameterPattern</td>
<td>[a-z][a-zA-Z0-9]*</td>
<td>Regex which applies to inferred-type lambda parameter names</td>
</tr>
<tr>
<td>explicitLambdaParameterPattern</td>
<td>[a-z][a-zA-Z0-9]*</td>
<td>Regex which applies to explicitly-typed lambda parameter names</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/codestyle.xml/FormalParameterNamingConventions"</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/codestyle.xml/FormalParameterNamingConventions"</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">"methodParameterPattern"</span> <span class="na">value=</span><span class="s">"[a-z][a-zA-Z0-9]*"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"finalMethodParameterPattern"</span> <span class="na">value=</span><span class="s">"[a-z][a-zA-Z0-9]*"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"lambdaParameterPattern"</span> <span class="na">value=</span><span class="s">"[a-z][a-zA-Z0-9]*"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"explicitLambdaParameterPattern"</span> <span class="na">value=</span><span class="s">"[a-z][a-zA-Z0-9]*"</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="genericsnaming">GenericsNaming</h2>
<p><strong>Since:</strong> PMD 4.2.6</p>
<p><strong>Priority:</strong> Medium Low (4)</p>
<p>Names for references to generic values should be limited to a single uppercase letter.</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">TypeParameter</span><span class="p">[</span><span class="w">
</span><span class="nf">string-length</span><span class="p">(</span><span class="na">@Name</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="ow">or</span><span class="w">
</span><span class="nf">upper-case</span><span class="p">(</span><span class="na">@Name</span><span class="p">)</span><span class="w"> </span><span class="p">!</span><span class="err">=</span><span class="w"> </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">interface</span> <span class="nc">GenericDao</span><span class="o">&lt;</span><span class="no">E</span> <span class="kd">extends</span> <span class="nc">BaseModel</span><span class="o">,</span> <span class="no">K</span> <span class="kd">extends</span> <span class="nc">Serializable</span><span class="o">&gt;</span> <span class="kd">extends</span> <span class="nc">BaseDao</span> <span class="o">{</span>
<span class="c1">// This is ok...</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kd">interface</span> <span class="nc">GenericDao</span><span class="o">&lt;</span><span class="no">E</span> <span class="kd">extends</span> <span class="nc">BaseModel</span><span class="o">,</span> <span class="no">K</span> <span class="kd">extends</span> <span class="nc">Serializable</span><span class="o">&gt;</span> <span class="o">{</span>
<span class="c1">// Also this</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kd">interface</span> <span class="nc">GenericDao</span><span class="o">&lt;</span><span class="n">e</span> <span class="kd">extends</span> <span class="nc">BaseModel</span><span class="o">,</span> <span class="no">K</span> <span class="kd">extends</span> <span class="nc">Serializable</span><span class="o">&gt;</span> <span class="o">{</span>
<span class="c1">// 'e' should be an 'E'</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kd">interface</span> <span class="nc">GenericDao</span><span class="o">&lt;</span><span class="no">EF</span> <span class="kd">extends</span> <span class="nc">BaseModel</span><span class="o">,</span> <span class="no">K</span> <span class="kd">extends</span> <span class="nc">Serializable</span><span class="o">&gt;</span> <span class="o">{</span>
<span class="c1">// 'EF' is not 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/codestyle.xml/GenericsNaming"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="identicalcatchbranches">IdenticalCatchBranches</h2>
<p><strong>Since:</strong> PMD 6.4.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p><strong>Minimum Language Version:</strong> Java 1.7</p>
<p>Identical <code class="language-plaintext highlighter-rouge">catch</code> branches use up vertical space and increase the complexity of code without
adding functionality. Its better style to collapse identical branches into a single multi-catch
branch.</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/codestyle/IdenticalCatchBranchesRule.java">net.sourceforge.pmd.lang.java.rule.codestyle.IdenticalCatchBranchesRule</a></p>
<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">// do something</span>
<span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="nc">IllegalArgumentException</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">catch</span> <span class="o">(</span><span class="nc">IllegalStateException</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span> <span class="c1">// Can be collapsed into the previous block</span>
<span class="k">throw</span> <span class="n">e</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">IllegalArgumentException</span> <span class="o">|</span> <span class="nc">IllegalStateException</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span> <span class="c1">// This is better</span>
<span class="k">throw</span> <span class="n">e</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/codestyle.xml/IdenticalCatchBranches"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="linguisticnaming">LinguisticNaming</h2>
<p><strong>Since:</strong> PMD 6.7.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>This rule finds Linguistic Naming Antipatterns. It checks for fields, that are named, as if they should
be boolean but have a different type. It also checks for methods, that according to their name, should
return a boolean, but dont. Further, it checks, that getters return something and setters wont.
Finally, it checks that methods, that start with "to" - so called transform methods - actually return
something, since according to their name, they should convert or transform one object into another.
There is additionally an option, to check for methods that contain "To" in their name - which are
also transform methods. However, this is disabled by default, since this detection is prone to
false positives.</p>
<p>For more information, see <a href="https://doi.org/10.1007/s10664-014-9350-8">Linguistic Antipatterns - What They Are and How
Developers Perceive Them</a>.</p>
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/LinguisticNamingRule.java">net.sourceforge.pmd.lang.java.rule.codestyle.LinguisticNamingRule</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">LinguisticNaming</span> <span class="o">{</span>
<span class="kt">int</span> <span class="n">isValid</span><span class="o">;</span> <span class="c1">// the field name indicates a boolean, but it is an int.</span>
<span class="kt">boolean</span> <span class="n">isTrue</span><span class="o">;</span> <span class="c1">// correct type of the field</span>
<span class="kt">void</span> <span class="nf">myMethod</span><span class="o">()</span> <span class="o">{</span>
<span class="kt">int</span> <span class="n">hasMoneyLocal</span><span class="o">;</span> <span class="c1">// the local variable name indicates a boolean, but it is an int.</span>
<span class="kt">boolean</span> <span class="n">hasSalaryLocal</span><span class="o">;</span> <span class="c1">// correct naming and type</span>
<span class="o">}</span>
<span class="c1">// the name of the method indicates, it is a boolean, but the method returns an int.</span>
<span class="kt">int</span> <span class="nf">isValid</span><span class="o">()</span> <span class="o">{</span>
<span class="k">return</span> <span class="mi">1</span><span class="o">;</span>
<span class="o">}</span>
<span class="c1">// correct naming and return type</span>
<span class="kt">boolean</span> <span class="nf">isSmall</span><span class="o">()</span> <span class="o">{</span>
<span class="k">return</span> <span class="kc">true</span><span class="o">;</span>
<span class="o">}</span>
<span class="c1">// the name indicates, this is a setter, but it returns something</span>
<span class="kt">int</span> <span class="nf">setName</span><span class="o">()</span> <span class="o">{</span>
<span class="k">return</span> <span class="mi">1</span><span class="o">;</span>
<span class="o">}</span>
<span class="c1">// the name indicates, this is a getter, but it doesn't return anything</span>
<span class="kt">void</span> <span class="nf">getName</span><span class="o">()</span> <span class="o">{</span>
<span class="c1">// nothing to return?</span>
<span class="o">}</span>
<span class="c1">// the name indicates, it transforms an object and should return the result</span>
<span class="kt">void</span> <span class="nf">toDataType</span><span class="o">()</span> <span class="o">{</span>
<span class="c1">// nothing to return?</span>
<span class="o">}</span>
<span class="c1">// the name indicates, it transforms an object and should return the result</span>
<span class="kt">void</span> <span class="nf">grapeToWine</span><span class="o">()</span> <span class="o">{</span>
<span class="c1">// nothing to return?</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>ignoredAnnotations</td>
<td>java.lang.Override</td>
<td>Fully qualified names of the annotation types that should be ignored by this rule</td>
</tr>
<tr>
<td>checkBooleanMethod</td>
<td>true</td>
<td>Check method names and types for inconsistent naming.</td>
</tr>
<tr>
<td>checkGetters</td>
<td>true</td>
<td>Check return type of getters.</td>
</tr>
<tr>
<td>checkSetters</td>
<td>true</td>
<td>Check return type of setters.</td>
</tr>
<tr>
<td>checkPrefixedTransformMethods</td>
<td>true</td>
<td>Check return type of methods whose names start with the configured prefix (see transformMethodNames property).</td>
</tr>
<tr>
<td>checkTransformMethods</td>
<td>false</td>
<td>Check return type of methods which contain the configured infix in their name (see transformMethodNames property).</td>
</tr>
<tr>
<td>booleanMethodPrefixes</td>
<td>is , has , can , have , will , should</td>
<td>The prefixes of methods that return boolean.</td>
</tr>
<tr>
<td>transformMethodNames</td>
<td>to , as</td>
<td>The prefixes and infixes that indicate a transform method.</td>
</tr>
<tr>
<td>checkFields</td>
<td>true</td>
<td>Check field names and types for inconsistent naming.</td>
</tr>
<tr>
<td>checkVariables</td>
<td>true</td>
<td>Check local variable names and types for inconsistent naming.</td>
</tr>
<tr>
<td>booleanFieldPrefixes</td>
<td>is , has , can , have , will , should</td>
<td>The prefixes of fields and variables that indicate boolean.</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/codestyle.xml/LinguisticNaming"</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/codestyle.xml/LinguisticNaming"</span><span class="nt">&gt;</span>
<span class="nt">&lt;properties&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"ignoredAnnotations"</span> <span class="na">value=</span><span class="s">"java.lang.Override"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"checkBooleanMethod"</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">"checkGetters"</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">"checkSetters"</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">"checkPrefixedTransformMethods"</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">"checkTransformMethods"</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">"booleanMethodPrefixes"</span> <span class="na">value=</span><span class="s">"is,has,can,have,will,should"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"transformMethodNames"</span> <span class="na">value=</span><span class="s">"to,as"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"checkFields"</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">"checkVariables"</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">"booleanFieldPrefixes"</span> <span class="na">value=</span><span class="s">"is,has,can,have,will,should"</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="localhomenamingconvention">LocalHomeNamingConvention</h2>
<p><strong>Since:</strong> PMD 4.0</p>
<p><strong>Priority:</strong> Medium Low (4)</p>
<p>The Local Home interface of a Session EJB should be suffixed by LocalHome.</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">'javax.ejb.EJBLocalHome'</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="nf">ends-with</span><span class="p">(</span><span class="na">@SimpleName</span><span class="p">,</span><span class="w"> </span><span class="s1">'LocalHome'</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">interface</span> <span class="nc">MyBeautifulLocalHome</span> <span class="kd">extends</span> <span class="n">javax</span><span class="o">.</span><span class="na">ejb</span><span class="o">.</span><span class="na">EJBLocalHome</span> <span class="o">{}</span> <span class="c1">// proper name</span>
<span class="kd">public</span> <span class="kd">interface</span> <span class="nc">MissingProperSuffix</span> <span class="kd">extends</span> <span class="n">javax</span><span class="o">.</span><span class="na">ejb</span><span class="o">.</span><span class="na">EJBLocalHome</span> <span class="o">{}</span> <span class="c1">// non-standard name</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/codestyle.xml/LocalHomeNamingConvention"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="localinterfacesessionnamingconvention">LocalInterfaceSessionNamingConvention</h2>
<p><strong>Since:</strong> PMD 4.0</p>
<p><strong>Priority:</strong> Medium Low (4)</p>
<p>The Local Interface of a Session EJB should be suffixed by Local.</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">'javax.ejb.EJBLocalObject'</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="nf">ends-with</span><span class="p">(</span><span class="na">@SimpleName</span><span class="p">,</span><span class="w"> </span><span class="s1">'Local'</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">interface</span> <span class="nc">MyLocal</span> <span class="kd">extends</span> <span class="n">javax</span><span class="o">.</span><span class="na">ejb</span><span class="o">.</span><span class="na">EJBLocalObject</span> <span class="o">{}</span> <span class="c1">// proper name</span>
<span class="kd">public</span> <span class="kd">interface</span> <span class="nc">MissingProperSuffix</span> <span class="kd">extends</span> <span class="n">javax</span><span class="o">.</span><span class="na">ejb</span><span class="o">.</span><span class="na">EJBLocalObject</span> <span class="o">{}</span> <span class="c1">// non-standard name</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/codestyle.xml/LocalInterfaceSessionNamingConvention"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="localvariablecouldbefinal">LocalVariableCouldBeFinal</h2>
<p><strong>Since:</strong> PMD 2.2</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>A local variable assigned only once can be declared final.</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/codestyle/LocalVariableCouldBeFinalRule.java">net.sourceforge.pmd.lang.java.rule.codestyle.LocalVariableCouldBeFinalRule</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">foo</span> <span class="o">()</span> <span class="o">{</span>
<span class="nc">String</span> <span class="n">txtA</span> <span class="o">=</span> <span class="s">"a"</span><span class="o">;</span> <span class="c1">// if txtA will not be assigned again it is better to do this:</span>
<span class="kd">final</span> <span class="nc">String</span> <span class="n">txtB</span> <span class="o">=</span> <span class="s">"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>ignoreForEachDecl</td>
<td>false</td>
<td>Ignore non-final loop variables in a for-each 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/codestyle.xml/LocalVariableCouldBeFinal"</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/codestyle.xml/LocalVariableCouldBeFinal"</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">"ignoreForEachDecl"</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="localvariablenamingconventions">LocalVariableNamingConventions</h2>
<p><strong>Since:</strong> PMD 6.6.0</p>
<p><strong>Priority:</strong> High (1)</p>
<p>Configurable naming conventions for local variable declarations and other locally-scoped
variables. This rule reports variable declarations which do not match the regex that applies to their
specific kind (e.g. final variable, or catch-clause parameter). Each regex can be configured through
properties.</p>
<p>By default this rule uses the standard Java naming convention (Camel case).</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/codestyle/LocalVariableNamingConventionsRule.java">net.sourceforge.pmd.lang.java.rule.codestyle.LocalVariableNamingConventionsRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
<span class="kt">void</span> <span class="nf">bar</span><span class="o">()</span> <span class="o">{</span>
<span class="kt">int</span> <span class="n">localVariable</span> <span class="o">=</span> <span class="mi">1</span><span class="o">;</span> <span class="c1">// This is in camel case, so it's ok</span>
<span class="kt">int</span> <span class="n">local_variable</span> <span class="o">=</span> <span class="mi">1</span><span class="o">;</span> <span class="c1">// This will be reported unless you change the regex</span>
<span class="kd">final</span> <span class="kt">int</span> <span class="n">i_var</span> <span class="o">=</span> <span class="mi">1</span><span class="o">;</span> <span class="c1">// final local variables can be configured separately</span>
<span class="k">try</span> <span class="o">{</span>
<span class="n">foo</span><span class="o">();</span>
<span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="nc">IllegalArgumentException</span> <span class="n">e_illegal</span><span class="o">)</span> <span class="o">{</span>
<span class="c1">// exception block parameters can be configured separately</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>localVarPattern</td>
<td>[a-z][a-zA-Z0-9]*</td>
<td>Regex which applies to non-final local variable names</td>
</tr>
<tr>
<td>finalVarPattern</td>
<td>[a-z][a-zA-Z0-9]*</td>
<td>Regex which applies to final local variable names</td>
</tr>
<tr>
<td>catchParameterPattern</td>
<td>[a-z][a-zA-Z0-9]*</td>
<td>Regex which applies to exception block parameter names</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/codestyle.xml/LocalVariableNamingConventions"</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/codestyle.xml/LocalVariableNamingConventions"</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">"localVarPattern"</span> <span class="na">value=</span><span class="s">"[a-z][a-zA-Z0-9]*"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"finalVarPattern"</span> <span class="na">value=</span><span class="s">"[a-z][a-zA-Z0-9]*"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"catchParameterPattern"</span> <span class="na">value=</span><span class="s">"[a-z][a-zA-Z0-9]*"</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="longvariable">LongVariable</h2>
<p><strong>Since:</strong> PMD 0.3</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Fields, formal arguments, or local variable names that are too long can make the code difficult to follow.</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="nf">string-length</span><span class="p">(</span><span class="na">@Name</span><span class="p">)</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="nv">$minimum</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">Something</span> <span class="o">{</span>
<span class="kt">int</span> <span class="n">reallyLongIntName</span> <span class="o">=</span> <span class="o">-</span><span class="mi">3</span><span class="o">;</span> <span class="c1">// VIOLATION - Field</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="n">argumentsList</span><span class="o">[]</span> <span class="o">)</span> <span class="o">{</span> <span class="c1">// VIOLATION - Formal</span>
<span class="kt">int</span> <span class="n">otherReallyLongName</span> <span class="o">=</span> <span class="o">-</span><span class="mi">5</span><span class="o">;</span> <span class="c1">// VIOLATION - Local</span>
<span class="k">for</span> <span class="o">(</span><span class="kt">int</span> <span class="n">interestingIntIndex</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span> <span class="c1">// VIOLATION - For</span>
<span class="n">interestingIntIndex</span> <span class="o">&lt;</span> <span class="mi">10</span><span class="o">;</span>
<span class="n">interestingIntIndex</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>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>minimum</td>
<td>17</td>
<td>The variable length reporting threshold</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/codestyle.xml/LongVariable"</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/codestyle.xml/LongVariable"</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">"minimum"</span> <span class="na">value=</span><span class="s">"17"</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="mdbandsessionbeannamingconvention">MDBAndSessionBeanNamingConvention</h2>
<p><strong>Since:</strong> PMD 4.0</p>
<p><strong>Priority:</strong> Medium Low (4)</p>
<p>The EJB Specification states that any MessageDrivenBean or SessionBean should be suffixed by Bean.</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="p">(</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.MessageDrivenBean'</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="nf">ends-with</span><span class="p">(</span><span class="na">@SimpleName</span><span class="p">,</span><span class="w"> </span><span class="s1">'Bean'</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">SomeBean</span> <span class="kd">implements</span> <span class="nc">SessionBean</span><span class="o">{}</span> <span class="c1">// proper name</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">MissingTheProperSuffix</span> <span class="kd">implements</span> <span class="nc">SessionBean</span> <span class="o">{}</span> <span class="c1">// non-standard name</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/codestyle.xml/MDBAndSessionBeanNamingConvention"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="methodargumentcouldbefinal">MethodArgumentCouldBeFinal</h2>
<p><strong>Since:</strong> PMD 2.2</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>A method argument that is never re-assigned within the method can be declared final.</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/codestyle/MethodArgumentCouldBeFinalRule.java">net.sourceforge.pmd.lang.java.rule.codestyle.MethodArgumentCouldBeFinalRule</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">foo1</span> <span class="o">(</span><span class="nc">String</span> <span class="n">param</span><span class="o">)</span> <span class="o">{</span> <span class="c1">// do stuff with param never assigning it</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">foo2</span> <span class="o">(</span><span class="kd">final</span> <span class="nc">String</span> <span class="n">param</span><span class="o">)</span> <span class="o">{</span> <span class="c1">// better, do stuff with param never assigning it</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/codestyle.xml/MethodArgumentCouldBeFinal"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="methodnamingconventions">MethodNamingConventions</h2>
<p><strong>Since:</strong> PMD 1.2</p>
<p><strong>Priority:</strong> High (1)</p>
<p>Configurable naming conventions for method declarations. This rule reports
method declarations which do not match the regex that applies to their
specific kind (e.g. JUnit test or native method). Each regex can be
configured through properties.</p>
<p>By default this rule uses the standard Java naming convention (Camel case).</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/codestyle/MethodNamingConventionsRule.java">net.sourceforge.pmd.lang.java.rule.codestyle.MethodNamingConventionsRule</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">fooStuff</span><span class="o">()</span> <span class="o">{</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>This rule has the following properties:</strong></p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Default Value</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>methodPattern</td>
<td>[a-z][a-zA-Z0-9]*</td>
<td>Regex which applies to instance method names</td>
</tr>
<tr>
<td>staticPattern</td>
<td>[a-z][a-zA-Z0-9]*</td>
<td>Regex which applies to static method names</td>
</tr>
<tr>
<td>nativePattern</td>
<td>[a-z][a-zA-Z0-9]*</td>
<td>Regex which applies to native method names</td>
</tr>
<tr>
<td>junit3TestPattern</td>
<td>test[A-Z0-9][a-zA-Z0-9]*</td>
<td>Regex which applies to JUnit 3 test method names</td>
</tr>
<tr>
<td>junit4TestPattern</td>
<td>[a-z][a-zA-Z0-9]*</td>
<td>Regex which applies to JUnit 4 test method names</td>
</tr>
<tr>
<td>junit5TestPattern</td>
<td>[a-z][a-zA-Z0-9]*</td>
<td>Regex which applies to JUnit 5 test method names</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/codestyle.xml/MethodNamingConventions"</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/codestyle.xml/MethodNamingConventions"</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">"methodPattern"</span> <span class="na">value=</span><span class="s">"[a-z][a-zA-Z0-9]*"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"staticPattern"</span> <span class="na">value=</span><span class="s">"[a-z][a-zA-Z0-9]*"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"nativePattern"</span> <span class="na">value=</span><span class="s">"[a-z][a-zA-Z0-9]*"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"junit3TestPattern"</span> <span class="na">value=</span><span class="s">"test[A-Z0-9][a-zA-Z0-9]*"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"junit4TestPattern"</span> <span class="na">value=</span><span class="s">"[a-z][a-zA-Z0-9]*"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"junit5TestPattern"</span> <span class="na">value=</span><span class="s">"[a-z][a-zA-Z0-9]*"</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="nopackage">NoPackage</h2>
<p><strong>Since:</strong> PMD 3.3</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Detects when a class, interface, enum or annotation does not have a package definition.</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">CompilationUnit</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="nt">PackageDeclaration</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="s2">"TypeDeclaration"</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="c1">// no package declaration</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">ClassInDefaultPackage</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/codestyle.xml/NoPackage"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="onlyonereturn">OnlyOneReturn</h2>
<p><strong>Since:</strong> PMD 1.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>A method should have only one exit point, and that should be the last statement in the method.</p>
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/OnlyOneReturnRule.java">net.sourceforge.pmd.lang.java.rule.codestyle.OnlyOneReturnRule</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">OneReturnOnly1</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="kt">int</span> <span class="n">x</span><span class="o">)</span> <span class="o">{</span>
<span class="k">if</span> <span class="o">(</span><span class="n">x</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="o">)</span> <span class="o">{</span>
<span class="k">return</span> <span class="s">"hey"</span><span class="o">;</span> <span class="c1">// first exit</span>
<span class="o">}</span>
<span class="k">return</span> <span class="s">"hi"</span><span class="o">;</span> <span class="c1">// second exit</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/codestyle.xml/OnlyOneReturn"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="packagecase">PackageCase</h2>
<p><strong>Since:</strong> PMD 3.3</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Detects when a package definition contains uppercase characters.</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">PackageDeclaration</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="p">!</span><span class="err">=</span><span class="w"> </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">package</span> <span class="nn">com.MyCompany</span><span class="o">;</span> <span class="c1">// should be lowercase name</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">SomeClass</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/codestyle.xml/PackageCase"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="prematuredeclaration">PrematureDeclaration</h2>
<p><strong>Since:</strong> PMD 5.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Checks for variables that are defined before they might be used. A declaration is
deemed to be premature if there are some statements that may return or throw an
exception between the time the variable is declared and the time it is first read.</p>
<p>Some variables cannot be declared close to their first usage because of side-effects
occurring before theyre first used. We try to avoid reporting those by considering
most method and constructor invocations to be impure. See the second example.</p>
<p>Note that this rule is meant to improve code readability but is not an optimization.
A smart JIT will not care whether the variable is declared prematurely or not, as it
can reorder code.</p>
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/PrematureDeclarationRule.java">net.sourceforge.pmd.lang.java.rule.codestyle.PrematureDeclarationRule</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">int</span> <span class="nf">getLength</span><span class="o">(</span><span class="nc">String</span><span class="o">[]</span> <span class="n">strings</span><span class="o">)</span> <span class="o">{</span>
<span class="kt">int</span> <span class="n">length</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span> <span class="c1">// could be moved closer to the loop</span>
<span class="k">if</span> <span class="o">(</span><span class="n">strings</span> <span class="o">==</span> <span class="kc">null</span> <span class="o">||</span> <span class="n">strings</span><span class="o">.</span><span class="na">length</span> <span class="o">==</span> <span class="mi">0</span><span class="o">)</span> <span class="k">return</span> <span class="mi">0</span><span class="o">;</span>
<span class="k">for</span> <span class="o">(</span><span class="nc">String</span> <span class="n">str</span> <span class="o">:</span> <span class="n">strings</span><span class="o">)</span> <span class="o">{</span>
<span class="n">length</span> <span class="o">+=</span> <span class="n">str</span><span class="o">.</span><span class="na">length</span><span class="o">();</span>
<span class="o">}</span>
<span class="k">return</span> <span class="n">length</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="kt">int</span> <span class="nf">getLength</span><span class="o">(</span><span class="nc">String</span><span class="o">[]</span> <span class="n">strings</span><span class="o">)</span> <span class="o">{</span>
<span class="kt">int</span> <span class="n">startTime</span> <span class="o">=</span> <span class="nc">System</span><span class="o">.</span><span class="na">nanoTime</span><span class="o">();</span> <span class="c1">// cannot be moved because initializer is impure</span>
<span class="k">if</span> <span class="o">(</span><span class="n">strings</span> <span class="o">==</span> <span class="kc">null</span> <span class="o">||</span> <span class="n">strings</span><span class="o">.</span><span class="na">length</span> <span class="o">==</span> <span class="mi">0</span><span class="o">)</span> <span class="o">{</span>
<span class="c1">// some error logic</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nf">SomeException</span><span class="o">(...);</span>
<span class="o">}</span>
<span class="k">for</span> <span class="o">(</span><span class="nc">String</span> <span class="n">str</span> <span class="o">:</span> <span class="n">strings</span><span class="o">)</span> <span class="o">{</span>
<span class="n">length</span> <span class="o">+=</span> <span class="n">str</span><span class="o">.</span><span class="na">length</span><span class="o">();</span>
<span class="o">}</span>
<span class="k">return</span> <span class="nc">System</span><span class="o">.</span><span class="na">nanoTime</span><span class="o">()</span> <span class="o">-</span> <span class="n">startTime</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/codestyle.xml/PrematureDeclaration"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="remoteinterfacenamingconvention">RemoteInterfaceNamingConvention</h2>
<p><strong>Since:</strong> PMD 4.0</p>
<p><strong>Priority:</strong> Medium Low (4)</p>
<p>Remote Interface of a Session EJB should not have a suffix.</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">'javax.ejb.EJBObject'</span><span class="p">)</span><span class="w">
</span><span class="ow">and</span><span class="w"> </span><span class="nf">matches</span><span class="p">(</span><span class="na">@SimpleName</span><span class="p">,</span><span class="w"> </span><span class="s1">'.*(Session|EJB|Bean)$'</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="cm">/* Poor Session suffix */</span>
<span class="kd">public</span> <span class="kd">interface</span> <span class="nc">BadSuffixSession</span> <span class="kd">extends</span> <span class="n">javax</span><span class="o">.</span><span class="na">ejb</span><span class="o">.</span><span class="na">EJBObject</span> <span class="o">{}</span>
<span class="cm">/* Poor EJB suffix */</span>
<span class="kd">public</span> <span class="kd">interface</span> <span class="nc">BadSuffixEJB</span> <span class="kd">extends</span> <span class="n">javax</span><span class="o">.</span><span class="na">ejb</span><span class="o">.</span><span class="na">EJBObject</span> <span class="o">{}</span>
<span class="cm">/* Poor Bean suffix */</span>
<span class="kd">public</span> <span class="kd">interface</span> <span class="nc">BadSuffixBean</span> <span class="kd">extends</span> <span class="n">javax</span><span class="o">.</span><span class="na">ejb</span><span class="o">.</span><span class="na">EJBObject</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/codestyle.xml/RemoteInterfaceNamingConvention"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="remotesessioninterfacenamingconvention">RemoteSessionInterfaceNamingConvention</h2>
<p><strong>Since:</strong> PMD 4.0</p>
<p><strong>Priority:</strong> Medium Low (4)</p>
<p>A Remote Home interface type of a Session EJB should be suffixed by Home.</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">'javax.ejb.EJBHome'</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="nf">ends-with</span><span class="p">(</span><span class="na">@SimpleName</span><span class="p">,</span><span class="w"> </span><span class="s1">'Home'</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">interface</span> <span class="nc">MyBeautifulHome</span> <span class="kd">extends</span> <span class="n">javax</span><span class="o">.</span><span class="na">ejb</span><span class="o">.</span><span class="na">EJBHome</span> <span class="o">{}</span> <span class="c1">// proper name</span>
<span class="kd">public</span> <span class="kd">interface</span> <span class="nc">MissingProperSuffix</span> <span class="kd">extends</span> <span class="n">javax</span><span class="o">.</span><span class="na">ejb</span><span class="o">.</span><span class="na">EJBHome</span> <span class="o">{}</span> <span class="c1">// non-standard name</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/codestyle.xml/RemoteSessionInterfaceNamingConvention"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="shortclassname">ShortClassName</h2>
<p><strong>Since:</strong> PMD 5.0</p>
<p><strong>Priority:</strong> Medium Low (4)</p>
<p>Short Classnames with fewer than e.g. five characters are not recommended.</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="nf">string-length</span><span class="p">(</span><span class="na">@SimpleName</span><span class="p">)</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="nv">$minimum</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="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>minimum</td>
<td>5</td>
<td>Number of characters that are required as a minimum for a class name.</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/codestyle.xml/ShortClassName"</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/codestyle.xml/ShortClassName"</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">"minimum"</span> <span class="na">value=</span><span class="s">"5"</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="shortmethodname">ShortMethodName</h2>
<p><strong>Since:</strong> PMD 0.3</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Method names that are very short are not helpful to the reader.</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="nf">string-length</span><span class="p">(</span><span class="na">@Name</span><span class="p">)</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="nv">$minimum</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">ShortMethod</span> <span class="o">{</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">a</span><span class="o">(</span> <span class="kt">int</span> <span class="n">i</span> <span class="o">)</span> <span class="o">{</span> <span class="c1">// Violation</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>minimum</td>
<td>3</td>
<td>Number of characters that are required as a minimum for a method name.</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/codestyle.xml/ShortMethodName"</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/codestyle.xml/ShortMethodName"</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">"minimum"</span> <span class="na">value=</span><span class="s">"3"</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="shortvariable">ShortVariable</h2>
<p><strong>Since:</strong> PMD 0.3</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Fields, local variables, enum constant names or parameter names that are very short are not helpful to the reader.</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="nf">string-length</span><span class="p">(</span><span class="na">@Name</span><span class="p">)</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="nv">$minimum</span><span class="p">]</span><span class="w">
</span><span class="c">(: ForStatement :)</span><span class="w">
</span><span class="p">[</span><span class="ow">not</span><span class="p">(..</span><span class="err">/</span><span class="p">..</span><span class="o">/</span><span class="kn">parent::</span><span class="nt">ForInit</span><span class="p">)]</span><span class="w">
</span><span class="c">(: Foreach statement :)</span><span class="w">
</span><span class="p">[</span><span class="ow">not</span><span class="p">(..</span><span class="err">/</span><span class="p">..</span><span class="o">/</span><span class="kn">parent::</span><span class="nt">ForeachStatement</span><span class="p">)]</span><span class="w">
</span><span class="c">(: Catch statement parameter :)</span><span class="w">
</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="kn">parent::</span><span class="nt">CatchParameter</span><span class="p">)]</span><span class="w">
</span><span class="c">(: Lambda expression parameter :)</span><span class="w">
</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="kn">parent::</span><span class="nt">LambdaParameter</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">Something</span> <span class="o">{</span>
<span class="kd">private</span> <span class="kt">int</span> <span class="n">q</span> <span class="o">=</span> <span class="mi">15</span><span class="o">;</span> <span class="c1">// field - too short</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="n">as</span><span class="o">[]</span> <span class="o">)</span> <span class="o">{</span> <span class="c1">// formal arg - too short</span>
<span class="kt">int</span> <span class="n">r</span> <span class="o">=</span> <span class="mi">20</span> <span class="o">+</span> <span class="n">q</span><span class="o">;</span> <span class="c1">// local var - too short</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">// not a violation (inside 'for' loop)</span>
<span class="n">r</span> <span class="o">+=</span> <span class="n">q</span><span class="o">;</span>
<span class="o">}</span>
<span class="k">for</span> <span class="o">(</span><span class="nc">Integer</span> <span class="n">i</span> <span class="o">:</span> <span class="n">numbers</span><span class="o">)</span> <span class="o">{</span> <span class="c1">// not a violation (inside 'for-each' loop)</span>
<span class="n">r</span> <span class="o">+=</span> <span class="n">q</span><span class="o">;</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p><strong>This rule has the following properties:</strong></p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Default Value</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>minimum</td>
<td>3</td>
<td>Number of characters that are required as a minimum for a variable name.</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/codestyle.xml/ShortVariable"</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/codestyle.xml/ShortVariable"</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">"minimum"</span> <span class="na">value=</span><span class="s">"3"</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="toomanystaticimports">TooManyStaticImports</h2>
<p><strong>Since:</strong> PMD 4.1</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>If you overuse the static import feature, it can make your program unreadable and
unmaintainable, polluting its namespace with all the static members you import.
Readers of your code (including you, a few months after you wrote it) will not know
which class a static member comes from (Sun 1.5 Language Guide).</p>
<p><strong>This rule is defined by the following XPath expression:</strong></p>
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">.[</span><span class="nf">count</span><span class="p">(</span><span class="nt">ImportDeclaration</span><span class="p">[</span><span class="na">@Static</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="nf">true</span><span class="p">()])</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="nv">$maximumStaticImports</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">static</span> <span class="nc">Lennon</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">static</span> <span class="nc">Ringo</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">static</span> <span class="nc">George</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">static</span> <span class="nc">Paul</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">static</span> <span class="nc">Yoko</span><span class="o">;</span> <span class="c1">// Too much !</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>maximumStaticImports</td>
<td>4</td>
<td>All static imports can be disallowed by setting this to 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/codestyle.xml/TooManyStaticImports"</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/codestyle.xml/TooManyStaticImports"</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">"maximumStaticImports"</span> <span class="na">value=</span><span class="s">"4"</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="unnecessaryannotationvalueelement">UnnecessaryAnnotationValueElement</h2>
<p><strong>Since:</strong> PMD 6.2.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Avoid the use of value in annotations when its the only element.</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">Annotation</span><span class="o">/</span><span class="nt">AnnotationMemberList</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">MemberValuePair</span><span class="p">[</span><span class="na">@Shorthand</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="nf">false</span><span class="p">()</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="na">@Name</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'value'</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="nd">@TestClassAnnotation</span><span class="o">(</span><span class="n">value</span> <span class="o">=</span> <span class="s">"TEST"</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="nd">@TestMemberAnnotation</span><span class="o">(</span><span class="n">value</span> <span class="o">=</span> <span class="s">"TEST"</span><span class="o">)</span>
<span class="kd">private</span> <span class="nc">String</span> <span class="n">y</span><span class="o">;</span>
<span class="nd">@TestMethodAnnotation</span><span class="o">(</span><span class="n">value</span> <span class="o">=</span> <span class="s">"TEST"</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">42</span><span class="o">;</span>
<span class="k">return</span><span class="o">;</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="c1">// should be</span>
<span class="nd">@TestClassAnnotation</span><span class="o">(</span><span class="s">"TEST"</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="nd">@TestMemberAnnotation</span><span class="o">(</span><span class="s">"TEST"</span><span class="o">)</span>
<span class="kd">private</span> <span class="nc">String</span> <span class="n">y</span><span class="o">;</span>
<span class="nd">@TestMethodAnnotation</span><span class="o">(</span><span class="s">"TEST"</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">42</span><span class="o">;</span>
<span class="k">return</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>java7Compatibility</td>
<td>false</td>
<td>If disabled, the rule shows also violations that are applicable for java8+</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/codestyle.xml/UnnecessaryAnnotationValueElement"</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/codestyle.xml/UnnecessaryAnnotationValueElement"</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">"java7Compatibility"</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="unnecessaryboxing">UnnecessaryBoxing</h2>
<p><strong>Since:</strong> PMD 7.0.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p><strong>Minimum Language Version:</strong> Java 1.5</p>
<p>Reports explicit boxing and unboxing conversions that may safely be removed,
either because they would be inserted by the compiler automatically,
or because theyre semantically a noop (eg unboxing a value to rebox it immediately).</p>
<p>Note that this only handles boxing and unboxing conversions occurring through
calls to <code class="language-plaintext highlighter-rouge">valueOf</code> or one of the <code class="language-plaintext highlighter-rouge">intValue</code>, <code class="language-plaintext highlighter-rouge">byteValue</code>, etc. methods. Casts
that command a conversion are reported by <a href="pmd_rules_java_codestyle.html#unnecessarycast"><code class="language-plaintext highlighter-rouge">UnnecessaryCast</code></a> instead.</p>
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/UnnecessaryBoxingRule.java">net.sourceforge.pmd.lang.java.rule.codestyle.UnnecessaryBoxingRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">{</span>
<span class="c1">// Instead of</span>
<span class="nc">Integer</span> <span class="n">integer</span> <span class="o">=</span> <span class="nc">Integer</span><span class="o">.</span><span class="na">valueOf</span><span class="o">(</span><span class="mi">2</span><span class="o">);</span>
<span class="c1">// you may just write</span>
<span class="nc">Integer</span> <span class="n">integer</span> <span class="o">=</span> <span class="mi">2</span><span class="o">;</span>
<span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="n">integer</span><span class="o">.</span><span class="na">intValue</span><span class="o">();</span> <span class="c1">// similarly for unboxing</span>
<span class="c1">// Instead of</span>
<span class="kt">int</span> <span class="n">x</span> <span class="o">=</span> <span class="nc">Integer</span><span class="o">.</span><span class="na">valueOf</span><span class="o">(</span><span class="s">"42"</span><span class="o">);</span>
<span class="c1">// you may just write</span>
<span class="kt">int</span> <span class="n">x</span> <span class="o">=</span> <span class="nc">Integer</span><span class="o">.</span><span class="na">parseInt</span><span class="o">(</span><span class="s">"42"</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/codestyle.xml/UnnecessaryBoxing"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="unnecessarycast">UnnecessaryCast</h2>
<p><strong>Since:</strong> PMD 6.24.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p><strong>Minimum Language Version:</strong> Java 1.5</p>
<p>Detects casts which could be removed as the operand of the cast is already suitable
for the context type. For instance, in the following:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Object context = (Comparable) "o";
</code></pre></div></div>
<p>The cast is unnecessary. This is because <code class="language-plaintext highlighter-rouge">String</code> already is a subtype of both
<code class="language-plaintext highlighter-rouge">Comparable</code> and <code class="language-plaintext highlighter-rouge">Object</code>.</p>
<p>This will also flag casts that can be avoided because of the autoboxing feature of Java 5.</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Integer integer = (Integer) 1;
</code></pre></div></div>
<p>The literal would be autoboxed to <code class="language-plaintext highlighter-rouge">Integer</code> anyway.</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/codestyle/UnnecessaryCastRule.java">net.sourceforge.pmd.lang.java.rule.codestyle.UnnecessaryCastRule</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.function.Function</span><span class="o">;</span>
<span class="kd">class</span> <span class="nc">SomeClass</span> <span class="o">{</span>
<span class="kd">static</span> <span class="o">{</span>
<span class="nc">Object</span> <span class="n">o</span><span class="o">;</span> <span class="kt">long</span> <span class="n">l</span><span class="o">;</span> <span class="kt">int</span> <span class="n">i</span><span class="o">;</span> <span class="nc">Integer</span> <span class="n">boxedInt</span><span class="o">;</span>
<span class="c1">// reference conversions</span>
<span class="n">o</span> <span class="o">=</span> <span class="o">(</span><span class="nc">Object</span><span class="o">)</span> <span class="k">new</span> <span class="nc">SomeClass</span><span class="o">();</span> <span class="c1">// unnecessary</span>
<span class="n">o</span> <span class="o">=</span> <span class="o">(</span><span class="nc">SomeClass</span><span class="o">)</span> <span class="n">o</span><span class="o">;</span> <span class="c1">// necessary (narrowing cast)</span>
<span class="n">o</span> <span class="o">=</span> <span class="o">(</span><span class="nc">Comparable</span><span class="o">&lt;</span><span class="nc">String</span><span class="o">&gt;)</span> <span class="s">"string"</span><span class="o">;</span> <span class="c1">// unnecessary</span>
<span class="c1">// primitive conversions</span>
<span class="n">l</span> <span class="o">=</span> <span class="o">(</span><span class="kt">long</span><span class="o">)</span> <span class="mi">2</span><span class="o">;</span> <span class="c1">// unnecessary</span>
<span class="n">l</span> <span class="o">=</span> <span class="o">(</span><span class="kt">long</span><span class="o">)</span> <span class="mf">2.0</span><span class="o">;</span> <span class="c1">// necessary (narrowing cast)</span>
<span class="n">l</span> <span class="o">=</span> <span class="o">(</span><span class="kt">byte</span><span class="o">)</span> <span class="n">i</span><span class="o">;</span> <span class="c1">// necessary (narrowing cast)</span>
<span class="c1">// boxing/unboxing casts (since java 5)</span>
<span class="n">o</span> <span class="o">=</span> <span class="o">(</span><span class="nc">Integer</span><span class="o">)</span> <span class="mi">3</span><span class="o">;</span> <span class="c1">// unnecessary (autoboxing would apply)</span>
<span class="n">o</span> <span class="o">=</span> <span class="o">(</span><span class="kt">long</span><span class="o">)</span> <span class="mi">3</span><span class="o">;</span> <span class="c1">// necessary (would be boxed to Long)</span>
<span class="n">l</span> <span class="o">=</span> <span class="o">(</span><span class="kt">int</span><span class="o">)</span> <span class="n">boxedInt</span><span class="o">;</span> <span class="c1">// necessary (cannot cast Integer to long)</span>
<span class="c1">// casts that give a target type to a lambda/ method ref are necessary</span>
<span class="n">o</span> <span class="o">=</span> <span class="o">(</span><span class="nc">Function</span><span class="o">&lt;</span><span class="nc">Integer</span><span class="o">,</span> <span class="nc">String</span><span class="o">&gt;)</span> <span class="nl">Integer:</span><span class="o">:</span><span class="n">toString</span><span class="o">;</span> <span class="c1">// necessary (no target type)</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="kn">import</span> <span class="nn">java.util.*</span><span class="o">;</span>
<span class="kd">class</span> <span class="nc">SomeClass</span> <span class="o">{</span>
<span class="kd">static</span> <span class="o">{</span>
<span class="cm">/* Casts involving access to collections were common before Java 5, because collections
* were not generic. This rule may hence be useful when converting from using a raw
* type like `List` to a parameterized type like `List&lt;String&gt;`.
*/</span>
<span class="nc">List</span><span class="o">&lt;</span><span class="nc">String</span><span class="o">&gt;</span> <span class="n">stringList</span> <span class="o">=</span> <span class="nc">Arrays</span><span class="o">.</span><span class="na">asList</span><span class="o">(</span><span class="s">"a"</span><span class="o">,</span> <span class="s">"b"</span><span class="o">);</span>
<span class="nc">String</span> <span class="n">element</span> <span class="o">=</span> <span class="o">(</span><span class="nc">String</span><span class="o">)</span> <span class="n">stringList</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="mi">0</span><span class="o">);</span> <span class="c1">// this cast is unnecessary</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/codestyle.xml/UnnecessaryCast"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="unnecessaryconstructor">UnnecessaryConstructor</h2>
<p><strong>Since:</strong> PMD 1.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>This rule detects when a constructor is not necessary; i.e., when there is only one constructor and the
constructor is identical to the default constructor. The default constructor should has same access
modifier as the declaring class. In an enum type, the default constructor is implicitly private.</p>
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/UnnecessaryConstructorRule.java">net.sourceforge.pmd.lang.java.rule.codestyle.UnnecessaryConstructorRule</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="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>ignoredAnnotations</td>
<td>javax.inject.Inject , com.google.inject.Inject , org.springframework.beans.factory.annotation.Autowired</td>
<td>Fully qualified names of the annotation types that should be ignored by this rule</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/codestyle.xml/UnnecessaryConstructor"</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/codestyle.xml/UnnecessaryConstructor"</span><span class="nt">&gt;</span>
<span class="nt">&lt;properties&gt;</span>
<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">"ignoredAnnotations"</span> <span class="na">value=</span><span class="s">"javax.inject.Inject,com.google.inject.Inject,org.springframework.beans.factory.annotation.Autowired"</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="unnecessaryfullyqualifiedname">UnnecessaryFullyQualifiedName</h2>
<p><strong>Since:</strong> PMD 5.0</p>
<p><strong>Priority:</strong> Medium Low (4)</p>
<p>Import statements allow the use of non-fully qualified names. The use of a fully qualified name
which is covered by an import statement is redundant. Consider using the non-fully qualified 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/codestyle/UnnecessaryFullyQualifiedNameRule.java">net.sourceforge.pmd.lang.java.rule.codestyle.UnnecessaryFullyQualifiedNameRule</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.List</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="kd">private</span> <span class="n">java</span><span class="o">.</span><span class="na">util</span><span class="o">.</span><span class="na">List</span> <span class="n">list1</span><span class="o">;</span> <span class="c1">// Unnecessary FQN</span>
<span class="kd">private</span> <span class="nc">List</span> <span class="n">list2</span><span class="o">;</span> <span class="c1">// More appropriate given import of 'java.util.List'</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>reportStaticMethods</td>
<td>true</td>
<td>Report unnecessary static method qualifiers like in <code class="language-plaintext highlighter-rouge">Collections.emptyList()</code>, if the method is imported or inherited.</td>
</tr>
<tr>
<td>reportStaticFields</td>
<td>true</td>
<td>Report unnecessary static field qualifiers like in <code class="language-plaintext highlighter-rouge">Math.PI</code>, if the field is imported or inherited.</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/codestyle.xml/UnnecessaryFullyQualifiedName"</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/codestyle.xml/UnnecessaryFullyQualifiedName"</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">"reportStaticMethods"</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">"reportStaticFields"</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="unnecessaryimport">UnnecessaryImport</h2>
<p><strong>Since:</strong> PMD 6.34.0</p>
<p><strong>Priority:</strong> Medium Low (4)</p>
<p>Reports import statements that can be removed. They are either unused,
duplicated, or the members they import are already implicitly in scope,
because theyre in java.lang, or the current package.</p>
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/UnnecessaryImportRule.java">net.sourceforge.pmd.lang.java.rule.codestyle.UnnecessaryImportRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">import</span> <span class="nn">java.io.File</span><span class="o">;</span> <span class="c1">// not used, can be removed</span>
<span class="kn">import</span> <span class="nn">java.util.Collections</span><span class="o">;</span> <span class="c1">// used below</span>
<span class="kn">import</span> <span class="nn">java.util.*</span><span class="o">;</span> <span class="c1">// so this one is not used</span>
<span class="kn">import</span> <span class="nn">java.lang.Object</span><span class="o">;</span> <span class="c1">// imports from java.lang, unnecessary</span>
<span class="kn">import</span> <span class="nn">java.lang.Object</span><span class="o">;</span> <span class="c1">// duplicate, unnecessary</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
<span class="kd">static</span> <span class="nc">Object</span> <span class="nf">emptyList</span><span class="o">()</span> <span class="o">{</span>
<span class="k">return</span> <span class="nc">Collections</span><span class="o">.</span><span class="na">emptyList</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/codestyle.xml/UnnecessaryImport"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="unnecessarylocalbeforereturn">UnnecessaryLocalBeforeReturn</h2>
<p><strong>Since:</strong> PMD 3.3</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Avoid the creation of unnecessary local variables</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/codestyle/UnnecessaryLocalBeforeReturnRule.java">net.sourceforge.pmd.lang.java.rule.codestyle.UnnecessaryLocalBeforeReturnRule</a></p>
<p><strong>Example(s):</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
<span class="kd">public</span> <span class="kt">int</span> <span class="nf">foo</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="n">doSomething</span><span class="o">();</span>
<span class="k">return</span> <span class="n">x</span><span class="o">;</span> <span class="c1">// instead, just 'return doSomething();'</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>statementOrderMatters</td>
<td>true</td>
<td>If set to false this rule no longer requires the variable declaration and return statement to be on consecutive lines. Any variable that is used solely in a return statement will be reported.</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/codestyle.xml/UnnecessaryLocalBeforeReturn"</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/codestyle.xml/UnnecessaryLocalBeforeReturn"</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">"statementOrderMatters"</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="unnecessarymodifier">UnnecessaryModifier</h2>
<p><strong>Since:</strong> PMD 1.02</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Fields in interfaces and annotations are automatically <code class="language-plaintext highlighter-rouge">public static final</code>, and methods are <code class="language-plaintext highlighter-rouge">public abstract</code>.
Classes, interfaces or annotations nested in an interface or annotation are automatically <code class="language-plaintext highlighter-rouge">public static</code>
(all nested interfaces and annotations are automatically static).
Nested enums are automatically <code class="language-plaintext highlighter-rouge">static</code>.
For historical reasons, modifiers which are implied by the context are accepted by the compiler, but are superfluous.</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/codestyle/UnnecessaryModifierRule.java">net.sourceforge.pmd.lang.java.rule.codestyle.UnnecessaryModifierRule</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="nd">@interface</span> <span class="nc">Annotation</span> <span class="o">{</span>
<span class="kd">public</span> <span class="kd">abstract</span> <span class="kt">void</span> <span class="nf">bar</span><span class="o">();</span> <span class="c1">// both abstract and public are ignored by the compiler</span>
<span class="kd">public</span> <span class="kd">static</span> <span class="kd">final</span> <span class="kt">int</span> <span class="no">X</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span> <span class="c1">// public, static, and final all ignored</span>
<span class="kd">public</span> <span class="kd">static</span> <span class="kd">class</span> <span class="nc">Bar</span> <span class="o">{}</span> <span class="c1">// public, static ignored</span>
<span class="kd">public</span> <span class="kd">static</span> <span class="kd">interface</span> <span class="nc">Baz</span> <span class="o">{}</span> <span class="c1">// ditto</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kd">interface</span> <span class="nc">Foo</span> <span class="o">{</span>
<span class="kd">public</span> <span class="kd">abstract</span> <span class="kt">void</span> <span class="nf">bar</span><span class="o">();</span> <span class="c1">// both abstract and public are ignored by the compiler</span>
<span class="kd">public</span> <span class="kd">static</span> <span class="kd">final</span> <span class="kt">int</span> <span class="no">X</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span> <span class="c1">// public, static, and final all ignored</span>
<span class="kd">public</span> <span class="kd">static</span> <span class="kd">class</span> <span class="nc">Bar</span> <span class="o">{}</span> <span class="c1">// public, static ignored</span>
<span class="kd">public</span> <span class="kd">static</span> <span class="kd">interface</span> <span class="nc">Baz</span> <span class="o">{}</span> <span class="c1">// ditto</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">Bar</span> <span class="o">{</span>
<span class="kd">public</span> <span class="kd">static</span> <span class="kd">interface</span> <span class="nc">Baz</span> <span class="o">{}</span> <span class="c1">// static ignored</span>
<span class="kd">public</span> <span class="kd">static</span> <span class="kd">enum</span> <span class="nc">FoorBar</span> <span class="o">{</span> <span class="c1">// static ignored</span>
<span class="no">FOO</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">FooClass</span> <span class="o">{</span>
<span class="kd">static</span> <span class="n">record</span> <span class="nf">BarRecord</span><span class="o">()</span> <span class="o">{}</span> <span class="c1">// static ignored</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kd">interface</span> <span class="nc">FooInterface</span> <span class="o">{</span>
<span class="kd">static</span> <span class="n">record</span> <span class="nf">BarRecord</span><span class="o">()</span> <span class="o">{}</span> <span class="c1">// static ignored</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/codestyle.xml/UnnecessaryModifier"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="unnecessaryreturn">UnnecessaryReturn</h2>
<p><strong>Since:</strong> PMD 1.3</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Avoid the use of unnecessary return statements. A return is unnecessary when no
instructions follow anyway.</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/codestyle/UnnecessaryReturnRule.java">net.sourceforge.pmd.lang.java.rule.codestyle.UnnecessaryReturnRule</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">42</span><span class="o">;</span>
<span class="k">return</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/codestyle.xml/UnnecessaryReturn"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="unnecessarysemicolon">UnnecessarySemicolon</h2>
<p><strong>Since:</strong> PMD 6.46.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Reports unnecessary semicolons (so called "empty statements" and "empty declarations").
These can be removed without changing the program. The Java grammar
allows them for historical reasons, but they should be avoided.</p>
<p>This rule will not report empty statements that are syntactically
required, for instance, because they are the body of a control statement.</p>
<p>This rule replaces EmptyStatementNotInLoop.</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">(: empty declarations :)</span><span class="w">
</span><span class="o">//</span><span class="nf">EmptyDeclaration</span><span class="w">
</span><span class="c">(: empty statements :)</span><span class="w">
</span><span class="p">|</span><span class="w"> </span><span class="o">//</span><span class="nt">Block</span><span class="o">/</span><span class="nt">EmptyStatement</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="o">{</span>
<span class="n">toString</span><span class="o">();;</span> <span class="c1">// one of these semicolons is unnecessary</span>
<span class="k">if</span> <span class="o">(</span><span class="kc">true</span><span class="o">);</span> <span class="c1">// this semicolon is not unnecessary, but it could be an empty block instead (not reported)</span>
<span class="o">}</span>
<span class="o">};</span> <span class="c1">// this semicolon is unnecessary</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/codestyle.xml/UnnecessarySemicolon"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="usediamondoperator">UseDiamondOperator</h2>
<p><strong>Since:</strong> PMD 6.11.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p><strong>Minimum Language Version:</strong> Java 1.7</p>
<p>In some cases, explicit type arguments in a constructor call for a generic type
may be replaced by diamond type arguments (<code class="language-plaintext highlighter-rouge">&lt;&gt;</code>), and be inferred by the compiler.
This rule recommends that you use diamond type arguments anywhere possible, since
it avoids duplication of the type arguments, and makes the code more concise and readable.</p>
<p>This rule is useful when upgrading a codebase to Java 1.7, Java 1.8, or Java 9.
The diamond syntax was first introduced in Java 1.7. In Java 8, improvements in Javas
type inference made more type arguments redundant. In Java 9, type arguments inference
was made possible for anonymous class constructors.</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/codestyle/UseDiamondOperatorRule.java">net.sourceforge.pmd.lang.java.rule.codestyle.UseDiamondOperatorRule</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.*</span><span class="o">;</span>
<span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
<span class="kd">static</span> <span class="o">{</span>
<span class="nc">List</span><span class="o">&lt;</span><span class="nc">String</span><span class="o">&gt;</span> <span class="n">strings</span><span class="o">;</span>
<span class="n">strings</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">ArrayList</span><span class="o">&lt;</span><span class="nc">String</span><span class="o">&gt;();</span> <span class="c1">// unnecessary duplication of type parameters</span>
<span class="n">strings</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">ArrayList</span><span class="o">&lt;&gt;();</span> <span class="c1">// using diamond type arguments is more concise</span>
<span class="n">strings</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">ArrayList</span><span class="o">();</span> <span class="c1">// accidental use of a raw type, you can use ArrayList&lt;&gt; instead</span>
<span class="n">strings</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">ArrayList</span><span class="o">&lt;&gt;()</span> <span class="o">{</span>
<span class="c1">// for anonymous classes, this is possible since Java 9 only</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/codestyle.xml/UseDiamondOperator"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="useexplicittypes">UseExplicitTypes</h2>
<p><strong>Since:</strong> PMD 7.0.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p><strong>Minimum Language Version:</strong> Java 10</p>
<p>Java 10 introduced the <code class="language-plaintext highlighter-rouge">var</code> keyword. This reduces the amount of code written because java can infer the type
from the initializer of the variable declaration.</p>
<p>This is essentially a trade-off: On the one hand, it can make code more readable by eliminating redundant
information. On the other hand, it can make code less readable by eliding useful information. There is no
blanket rule for when <code class="language-plaintext highlighter-rouge">var</code> should be used or shouldnt.</p>
<p>It may make sense to use <code class="language-plaintext highlighter-rouge">var</code> when the type is inherently clear upon reading the statement
(ie: assignment to either a literal value or a constructor call). Those use cases
can be enabled through properties.</p>
<p>Notice that lambda parameters are allowed, as they are already inferred by default (the <code class="language-plaintext highlighter-rouge">var</code> keyword
is completely optional).</p>
<p>See also <a href="https://openjdk.org/projects/amber/guides/lvti-style-guide">Local Variable Type Inference Style Guidelines</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">LocalVariableDeclaration</span><span class="p">[</span><span class="na">@TypeInferred</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="ow">not</span><span class="p">(</span><span class="nt">VariableDeclarator</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="s2">"Literal"</span><span class="p">)]])</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="nv">$allowLiterals</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="ow">not</span><span class="p">(</span><span class="nt">VariableDeclarator</span><span class="p">[</span><span class="nt">ConstructorCall</span><span class="p">])</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="nv">$allowCtors</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="nf">false</span><span class="p">()]</span><span class="w">
</span></code></pre></div></div>
<p><strong>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>allowLiterals</td>
<td>false</td>
<td>Allow when variables are directly initialized with literals</td>
</tr>
<tr>
<td>allowCtors</td>
<td>false</td>
<td>Allow when variables are directly initialized with a constructor call</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/codestyle.xml/UseExplicitTypes"</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/codestyle.xml/UseExplicitTypes"</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">"allowLiterals"</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">"allowCtors"</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="uselessparentheses">UselessParentheses</h2>
<p><strong>Since:</strong> PMD 5.0</p>
<p><strong>Priority:</strong> Medium Low (4)</p>
<p>Parenthesized expressions are used to override the default operator precedence
rules. Parentheses whose removal would not change the relative nesting of operators
are unnecessary, because they dont change the semantics of the enclosing expression.</p>
<p>Some parentheses that strictly speaking are unnecessary, may still be considered useful
for readability. This rule allows to ignore violations on two kinds of unnecessary parentheses:</p>
<ul>
<li>"Clarifying" parentheses, which separate operators of difference precedence. While
unnecessary, they make precedence rules explicit, which may be useful for rarely used
operators. For example:
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code> <span class="o">(</span><span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="o">)</span> <span class="o">&amp;</span> <span class="n">c</span> <span class="c1">// is equivalent to `a + b &amp; c`, but probably clearer</span>
</code></pre></div> </div>
<p>Unset the property <code class="language-plaintext highlighter-rouge">ignoreClarifying</code> to report them.</p>
</li>
<li>"Balancing" parentheses, which are unnecessary but visually balance out another pair
of parentheses around an equality operator. For example, those two expressions are equivalent:
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code> <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="o">(</span><span class="n">b</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="kc">null</span> <span class="o">!=</span> <span class="o">(</span><span class="n">b</span> <span class="o">==</span> <span class="kc">null</span><span class="o">)</span>
</code></pre></div> </div>
<p>The parentheses on the right are required, and the parentheses on the left are
just more visually pleasing. Unset the property <code class="language-plaintext highlighter-rouge">ignoreBalancing</code> to report them.</p>
</li>
</ul>
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/UselessParenthesesRule.java">net.sourceforge.pmd.lang.java.rule.codestyle.UselessParenthesesRule</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="o">{</span>
<span class="kt">int</span> <span class="n">n</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span>
<span class="n">n</span> <span class="o">=</span> <span class="o">(</span><span class="n">n</span><span class="o">);</span> <span class="c1">// here</span>
<span class="n">n</span> <span class="o">=</span> <span class="o">(</span><span class="n">n</span> <span class="o">*</span> <span class="mi">2</span><span class="o">)</span> <span class="o">*</span> <span class="mi">3</span><span class="o">;</span> <span class="c1">// and here</span>
<span class="n">n</span> <span class="o">=</span> <span class="n">n</span> <span class="o">*</span> <span class="o">(</span><span class="mi">2</span> <span class="o">*</span> <span class="mi">3</span><span class="o">);</span> <span class="c1">// and here</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>ignoreClarifying</td>
<td>true</td>
<td>Ignore parentheses that separate expressions of difference precedence, like in <code class="language-plaintext highlighter-rouge">(a % 2 == 0) ? x : -x</code></td>
</tr>
<tr>
<td>ignoreBalancing</td>
<td>true</td>
<td>Ignore unnecessary parentheses that appear balanced around an equality operator, because the other operand requires parentheses.For example, in <code class="language-plaintext highlighter-rouge">(a == null) == (b == null)</code>, only the second pair of parentheses is necessary, but the expression is clearer that way.</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/codestyle.xml/UselessParentheses"</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/codestyle.xml/UselessParentheses"</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">"ignoreClarifying"</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">"ignoreBalancing"</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="uselessqualifiedthis">UselessQualifiedThis</h2>
<p><strong>Since:</strong> PMD 5.4.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>Reports qualified this usages in the same class.</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">ThisExpression</span><span class="o">/</span><span class="nt">ClassType</span><span class="w">
</span><span class="p">[</span><span class="w"> </span><span class="kn">ancestor::</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">'TypeDeclaration'</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="err">=</span><span class="w"> </span><span class="p">.</span><span class="err">/</span><span class="na">@SimpleName</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">final</span> <span class="nc">Foo</span> <span class="n">otherFoo</span> <span class="o">=</span> <span class="nc">Foo</span><span class="o">.</span><span class="na">this</span><span class="o">;</span> <span class="c1">// use "this" directly</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="kd">final</span> <span class="nc">Foo</span> <span class="n">anotherFoo</span> <span class="o">=</span> <span class="nc">Foo</span><span class="o">.</span><span class="na">this</span><span class="o">;</span> <span class="c1">// use "this" directly</span>
<span class="o">}</span>
<span class="kd">private</span> <span class="nc">ActionListener</span> <span class="nf">returnListener</span><span class="o">()</span> <span class="o">{</span>
<span class="k">return</span> <span class="k">new</span> <span class="nf">ActionListener</span><span class="o">()</span> <span class="o">{</span>
<span class="nd">@Override</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">actionPerformed</span><span class="o">(</span><span class="nc">ActionEvent</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
<span class="n">doSomethingWithQualifiedThis</span><span class="o">(</span><span class="nc">Foo</span><span class="o">.</span><span class="na">this</span><span class="o">);</span> <span class="c1">// This is fine</span>
<span class="o">}</span>
<span class="o">};</span>
<span class="o">}</span>
<span class="kd">private</span> <span class="kd">class</span> <span class="nc">Foo3</span> <span class="o">{</span>
<span class="kd">final</span> <span class="nc">Foo</span> <span class="n">myFoo</span> <span class="o">=</span> <span class="nc">Foo</span><span class="o">.</span><span class="na">this</span><span class="o">;</span> <span class="c1">// This is fine</span>
<span class="o">}</span>
<span class="kd">private</span> <span class="kd">class</span> <span class="nc">Foo2</span> <span class="o">{</span>
<span class="kd">final</span> <span class="nc">Foo2</span> <span class="n">myFoo2</span> <span class="o">=</span> <span class="nc">Foo2</span><span class="o">.</span><span class="na">this</span><span class="o">;</span> <span class="c1">// Use "this" direclty</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/codestyle.xml/UselessQualifiedThis"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="useshortarrayinitializer">UseShortArrayInitializer</h2>
<p><strong>Since:</strong> PMD 6.15.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p>When declaring and initializing array fields or variables, it is not necessary to explicitly create a new array
using <code class="language-plaintext highlighter-rouge">new</code>. Instead one can simply define the initial content of the array as a expression in curly braces.</p>
<p>E.g. <code class="language-plaintext highlighter-rouge">int[] x = new int[] { 1, 2, 3 };</code> can be written as <code class="language-plaintext highlighter-rouge">int[] x = { 1, 2, 3 };</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">VariableDeclarator</span><span class="w">
</span><span class="p">[</span><span class="nt">VariableId</span><span class="p">[</span><span class="na">@TypeInferred</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="nf">false</span><span class="p">()</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="na">@ArrayType</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">ArrayAllocation</span><span class="o">/</span><span class="nt">ArrayInitializer</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">Foo</span><span class="o">[]</span> <span class="n">x</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">Foo</span><span class="o">[]</span> <span class="o">{</span> <span class="o">...</span> <span class="o">};</span> <span class="c1">// Overly verbose</span>
<span class="nc">Foo</span><span class="o">[]</span> <span class="n">x</span> <span class="o">=</span> <span class="o">{</span> <span class="o">...</span> <span class="o">};</span> <span class="c1">//Equivalent to above line</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/codestyle.xml/UseShortArrayInitializer"</span> <span class="nt">/&gt;</span>
</code></pre></div></div>
<h2 id="useunderscoresinnumericliterals">UseUnderscoresInNumericLiterals</h2>
<p><strong>Since:</strong> PMD 6.10.0</p>
<p><strong>Priority:</strong> Medium (3)</p>
<p><strong>Minimum Language Version:</strong> Java 1.7</p>
<p>Since Java 1.7, numeric literals can use underscores to separate digits. This rule enforces that
numeric literals above a certain length use these underscores to increase readability.</p>
<p>The rule only supports decimal (base 10) literals for now. The acceptable length under which literals
are not required to have underscores is configurable via a property. Even under that length, underscores
that are misplaced (not making groups of 3 digits) are 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="nf">NumericLiteral</span><span class="w">
</span><span class="c">(: Filter out literals in base other than 10 :)</span><span class="w">
</span><span class="p">[</span><span class="na">@Base</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="m">10</span><span class="p">]</span><span class="w">
</span><span class="c">(: Filter out ignored field name :)</span><span class="w">
</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="kn">ancestor::</span><span class="nt">VariableDeclarator</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">'serialVersionUID'</span><span class="p">])]</span><span class="w">
</span><span class="p">[</span><span class="w">
</span><span class="kr">some</span><span class="w"> </span><span class="nv">$num</span><span class="w"> </span><span class="kr">in</span><span class="w"> </span><span class="kr">to</span><span class="nf">kenize</span><span class="p">(</span><span class="na">@Image</span><span class="p">,</span><span class="w"> </span><span class="s2">"[dDfFlLeE+\-]"</span><span class="p">)</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="w">
</span><span class="p">(</span><span class="w"> </span><span class="nf">contains</span><span class="p">(</span><span class="nv">$num</span><span class="p">,</span><span class="w"> </span><span class="s2">"."</span><span class="p">)</span><span class="w">
</span><span class="ow">and</span><span class="w"> </span><span class="nf">string-length</span><span class="p">(</span><span class="nf">substring-before</span><span class="p">(</span><span class="nv">$num</span><span class="p">,</span><span class="w"> </span><span class="s2">"."</span><span class="p">))</span><span class="w"> </span><span class="err">&lt;=</span><span class="w"> </span><span class="nv">$acceptableDecimalLength</span><span class="w">
</span><span class="ow">and</span><span class="w"> </span><span class="nf">string-length</span><span class="p">(</span><span class="nf">substring-after</span><span class="p">(</span><span class="nv">$num</span><span class="p">,</span><span class="w"> </span><span class="s2">"."</span><span class="p">))</span><span class="w"> </span><span class="err">&lt;=</span><span class="w"> </span><span class="nv">$acceptableDecimalLength</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="nf">string-length</span><span class="p">(</span><span class="nv">$num</span><span class="p">)</span><span class="w"> </span><span class="err">&lt;=</span><span class="w"> </span><span class="nv">$acceptableDecimalLength</span><span class="w">
</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="nf">contains</span><span class="p">(</span><span class="nv">$num</span><span class="p">,</span><span class="s2">"_"</span><span class="p">))</span><span class="w">
</span><span class="ow">or</span><span class="w"> </span><span class="nf">matches</span><span class="p">(</span><span class="nv">$num</span><span class="p">,</span><span class="w"> </span><span class="s2">"^[0-9]{1,3}(_[0-9]{3})*(\.([0-9]{3}_)*[0-9]{1,3})?$"</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="kt">int</span> <span class="n">num</span> <span class="o">=</span> <span class="mi">1000000</span><span class="o">;</span> <span class="c1">// should be 1_000_000</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>acceptableDecimalLength</td>
<td>4</td>
<td>Length under which literals in base 10 are not required to have underscores</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/codestyle.xml/UseUnderscoresInNumericLiterals"</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/codestyle.xml/UseUnderscoresInNumericLiterals"</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">"acceptableDecimalLength"</span> <span class="na">value=</span><span class="s">"4"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;/properties&gt;</span>
<span class="nt">&lt;/rule&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/codestyle.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: Feb 29, 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>