forked from phoedos/pmd
4354 lines
235 KiB
HTML
4354 lines
235 KiB
HTML
<!DOCTYPE html>
|
||
<html>
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||
<meta name="description" content="Rules which enforce generally accepted best practices.">
|
||
<meta name="keywords" content=" Best Practices, AbstractClassWithoutAbstractMethod, AccessorClassGeneration, AccessorMethodGeneration, ArrayIsStoredDirectly, AvoidMessageDigestField, AvoidPrintStackTrace, AvoidReassigningCatchVariables, AvoidReassigningLoopVariables, AvoidReassigningParameters, AvoidStringBufferField, AvoidUsingHardCodedIP, CheckResultSet, ConstantsInInterface, DefaultLabelNotLastInSwitchStmt, DoubleBraceInitialization, ForLoopCanBeForeach, ForLoopVariableCount, GuardLogStatement, JUnit4SuitesShouldUseSuiteAnnotation, JUnit4TestShouldUseAfterAnnotation, JUnit4TestShouldUseBeforeAnnotation, JUnit4TestShouldUseTestAnnotation, JUnit5TestShouldBePackagePrivate, JUnitAssertionsShouldIncludeMessage, JUnitTestContainsTooManyAsserts, JUnitTestsShouldIncludeAssert, JUnitUseExpected, LiteralsFirstInComparisons, LooseCoupling, MethodReturnsInternalArray, MissingOverride, OneDeclarationPerLine, PreserveStackTrace, PrimitiveWrapperInstantiation, ReplaceEnumerationWithIterator, ReplaceHashtableWithMap, ReplaceVectorWithList, SimplifiableTestAssertion, SwitchStmtsShouldHaveDefault, SystemPrintln, UnnecessaryVarargsArrayCreation, UnusedAssignment, UnusedFormalParameter, UnusedLocalVariable, UnusedPrivateField, UnusedPrivateMethod, UseCollectionIsEmpty, UseEnumCollections, UseStandardCharsets, UseTryWithResources, UseVarargs, WhileLoopWithLiteralBoolean">
|
||
<title>Best Practices | 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"> <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"> </div>
|
||
<!-- Content Row -->
|
||
<div class="row">
|
||
|
||
|
||
<!-- Sidebar Column -->
|
||
<div class="col-md-3" id="tg-sb-sidebar">
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<ul id="mysidebar" class="nav">
|
||
<li class="sidebarTitle">PMD 7.4.0-SNAPSHOT</li>
|
||
<div class="sidebarTitleDate">Release date: 26-July-2024</div>
|
||
|
||
|
||
|
||
|
||
|
||
<li>
|
||
|
||
<a href="#">About</a>
|
||
|
||
<ul>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="index.html">Home</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_release_notes.html">Release notes</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_release_notes_pmd7.html">Release notes (PMD 7)</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_about_help.html">Getting help</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_about_release_policies.html">Release policies</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_about_support_lifecycle.html">Support lifecycle</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
<li>
|
||
|
||
<a href="#">User Documentation</a>
|
||
|
||
<ul>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_migrating_to_pmd7.html">Migration Guide for PMD 7</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_installation.html">Installation and basic CLI usage</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_making_rulesets.html">Making rulesets</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_configuring_rules.html">Configuring rules</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_best_practices.html">Best practices</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_suppressing_warnings.html">Suppressing warnings</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_incremental_analysis.html">Incremental analysis</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_cli_reference.html">PMD CLI reference</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_report_formats.html">PMD Report formats</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_3rdpartyrulesets.html">3rd party rulesets</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">CPD reference</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_cpd.html">Copy-paste detection</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_cpd_report_formats.html">CPD Report formats</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">Extending PMD</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_extending_writing_rules_intro.html">Introduction to writing rules</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_extending_your_first_rule.html">Your first rule</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_extending_writing_xpath_rules.html">XPath rules</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_extending_writing_java_rules.html">Java rules</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_extending_designer_reference.html">Rule designer reference</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_extending_defining_properties.html">Defining rule properties</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_extending_rule_guidelines.html">Rule guidelines</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_extending_testing.html">Testing your rules</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_extending_ast_dump.html">Creating (XML) dump of the AST</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">Tools / Integrations</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_tools_maven.html">Maven PMD Plugin</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_tools_gradle.html">Gradle</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_tools_ant.html">Ant</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_tools_java_api.html">PMD Java API</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_tools_bld.html">bld PMD Extension</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_tools_ci.html">CI integrations</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_tools.html">Other Tools / Integrations</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
<li>
|
||
|
||
<a href="#">Rule Reference</a>
|
||
|
||
<ul>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">Apex Rules</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_apex.html">Index</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_apex_bestpractices.html">Best Practices</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_apex_codestyle.html">Code Style</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_apex_design.html">Design</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_apex_documentation.html">Documentation</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_apex_errorprone.html">Error Prone</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_apex_performance.html">Performance</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_apex_security.html">Security</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">HTML Rules</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_html.html">Index</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_html_bestpractices.html">Best Practices</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">Java Rules</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_java.html">Index</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li class="active"><a href="pmd_rules_java_bestpractices.html">Best Practices</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_java_codestyle.html">Code Style</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_java_design.html">Design</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_java_documentation.html">Documentation</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_java_errorprone.html">Error Prone</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_java_multithreading.html">Multithreading</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_java_performance.html">Performance</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_java_security.html">Security</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">Java Server Pages Rules</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_jsp.html">Index</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_jsp_bestpractices.html">Best Practices</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_jsp_codestyle.html">Code Style</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_jsp_design.html">Design</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_jsp_errorprone.html">Error Prone</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_jsp_security.html">Security</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">JavaScript Rules</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_ecmascript.html">Index</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_ecmascript_bestpractices.html">Best Practices</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_ecmascript_codestyle.html">Code Style</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_ecmascript_errorprone.html">Error Prone</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">Kotlin Rules</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_kotlin.html">Index</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_kotlin_bestpractices.html">Best Practices</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_kotlin_errorprone.html">Error Prone</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">Maven POM Rules</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_pom.html">Index</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_pom_errorprone.html">Error Prone</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">Modelica Rules</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_modelica.html">Index</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_modelica_bestpractices.html">Best Practices</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">PLSQL Rules</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_plsql.html">Index</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_plsql_bestpractices.html">Best Practices</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_plsql_codestyle.html">Code Style</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_plsql_design.html">Design</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_plsql_errorprone.html">Error Prone</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">Salesforce Visualforce Rules</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_visualforce.html">Index</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_visualforce_security.html">Security</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">Scala Rules</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_scala.html">Index</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">Swift Rules</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_swift.html">Index</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_swift_bestpractices.html">Best Practices</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_swift_errorprone.html">Error Prone</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">Velocity Template Language (VTL) Rules</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_velocity.html">Index</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_velocity_bestpractices.html">Best Practices</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_velocity_design.html">Design</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_velocity_errorprone.html">Error Prone</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">WSDL Rules</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_wsdl.html">Index</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">XML Rules</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_xml.html">Index</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_xml_bestpractices.html">Best Practices</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_xml_errorprone.html">Error Prone</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">XSL Rules</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_xsl.html">Index</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_xsl_codestyle.html">Code Style</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_xsl_performance.html">Performance</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
<li>
|
||
|
||
<a href="#">Language-Specific Documentation</a>
|
||
|
||
<ul>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_index.html">Overview</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_configuration.html">Language configuration</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_apex.html">Apex</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_cpp.html">C/C++</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_cs.html">C#</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_coco.html">Coco</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_dart.html">Dart</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_fortran.html">Fortran</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_gherkin.html">Gherkin</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_go.html">Go</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_html.html">HTML</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_java.html">Java</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_js_ts.html">JavaScript / TypeScript</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_jsp.html">JSP</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_julia.html">Julia</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_kotlin.html">Kotlin</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_lua.html">Lua</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_matlab.html">Matlab</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_modelica.html">Modelica</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_objectivec.html">Objective-C</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_perl.html">Perl</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_php.html">PHP</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_plsql.html">PLSQL</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_python.html">Python</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_ruby.html">Ruby</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_scala.html">Scala</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_swift.html">Swift</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_tsql.html">T-SQL</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_visualforce.html">Visualforce</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_velocity.html">Velocity Template Language (VTL)</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_xml.html">XML and XML dialects</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
<li>
|
||
|
||
<a href="#">Developer Documentation</a>
|
||
|
||
<ul>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_devdocs_development.html">Developer resources</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_devdocs_building.html">Building PMD from source</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="https://github.com/pmd/pmd/blob/master/CONTRIBUTING.md" target="_blank">Contributing</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_devdocs_writing_documentation.html">Writing documentation</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_devdocs_roadmap.html">Roadmap</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_devdocs_how_pmd_works.html">How PMD works</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_devdocs_pmdtester.html">Pmdtester</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_devdocs_rule_deprecation_policy.html">Rule Deprecation Policy</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">Major contributions</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_devdocs_major_rule_guidelines.html">Rule Guidelines</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_devdocs_major_adding_new_language_javacc.html">Adding a new language (JavaCC)</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_devdocs_major_adding_new_language_antlr.html">Adding a new language (ANTLR)</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_devdocs_major_adding_new_cpd_language.html">Adding a new CPD language</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">Experimental features</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="tag_experimental.html">List of experimental Features</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
<li>
|
||
|
||
<a href="#">Project documentation</a>
|
||
|
||
<ul>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">Trivia about PMD</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_projectdocs_trivia_news.html">PMD in the press</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_projectdocs_trivia_products.html">Products & books related to PMD</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_projectdocs_trivia_similarprojects.html">Similar projects</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_projectdocs_trivia_meaning.html">What does 'PMD' mean?</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_projectdocs_logo.html">Logo</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_projectdocs_faq.html">FAQ</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="license.html">License</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_projectdocs_credits.html">Credits</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_release_notes_old.html">Old release notes</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_projectdocs_decisions.html">Decisions</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">Project management</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_projectdocs_committers_infrastructure.html">Infrastructure</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_projectdocs_committers_releasing.html">Release process</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_projectdocs_committers_merging_pull_requests.html">Merging pull requests</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_projectdocs_committers_main_landing_page.html">Main Landing page</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
</ul>
|
||
|
||
</div>
|
||
|
||
|
||
|
||
<!-- Content Column -->
|
||
<div class="col-md-9" id="tg-sb-content">
|
||
<header>
|
||
<div class="row">
|
||
<div class="col-lg-12">
|
||
<a href="./" role="button"
|
||
><i class="fa fa-home fa-lg"></i
|
||
></a>
|
||
» Best Practices
|
||
<a
|
||
target="_blank"
|
||
href="https://github.com/pmd/pmd/blob/master/docs/../pmd-java/src/main/resources/category/java/bestpractices.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">Best Practices</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/bestpractices.xml">
|
||
|
||
|
||
<div class="summary">Rules which enforce generally accepted best practices.</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/bestpractices.xml. -->
|
||
<h2 id="abstractclasswithoutabstractmethod">AbstractClassWithoutAbstractMethod</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 3.0</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>The abstract class does not contain any abstract methods. An abstract class suggests
|
||
an incomplete implementation, which is to be completed by subclasses implementing the
|
||
abstract methods. If the class is intended to be used as a base class only (not to be instantiated
|
||
directly) a protected constructor can be provided to prevent direct instantiation.</p>
|
||
|
||
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/AbstractClassWithoutAbstractMethodRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.AbstractClassWithoutAbstractMethodRule</a></p>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">abstract</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
|
||
<span class="kt">void</span> <span class="kt">int</span> <span class="nf">method1</span><span class="o">()</span> <span class="o">{</span> <span class="o">...</span> <span class="o">}</span>
|
||
<span class="kt">void</span> <span class="kt">int</span> <span class="nf">method2</span><span class="o">()</span> <span class="o">{</span> <span class="o">...</span> <span class="o">}</span>
|
||
<span class="c1">// consider using abstract methods or removing</span>
|
||
<span class="c1">// the abstract modifier and adding protected constructors</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule by referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/AbstractClassWithoutAbstractMethod"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="accessorclassgeneration">AccessorClassGeneration</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 1.04</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p><strong>Maximum Language Version:</strong> Java 10</p>
|
||
|
||
<p>Instantiation by way of private constructors from outside the constructor’s class often causes the
|
||
generation of an accessor. A factory method, or non-privatization of the constructor can eliminate this
|
||
situation. The generated class file is actually an interface. It gives the accessing class the ability
|
||
to invoke a new hidden package scope constructor that takes the interface as a supplementary parameter.
|
||
This turns a private constructor effectively into one with package scope, and is challenging to discern.</p>
|
||
|
||
<p><em>Note:</em> This rule is only executed for Java 10 or lower.
|
||
Since Java 11, <a href="https://openjdk.org/jeps/181">JEP 181: Nest-Based Access Control</a> has been implemented. This
|
||
means that in Java 11 and above accessor classes are not generated anymore.</p>
|
||
|
||
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/AccessorClassGenerationRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.AccessorClassGenerationRule</a></p>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Outer</span> <span class="o">{</span>
|
||
<span class="kt">void</span> <span class="nf">method</span><span class="o">(){</span>
|
||
<span class="nc">Inner</span> <span class="n">ic</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">Inner</span><span class="o">();</span><span class="c1">//Causes generation of accessor class</span>
|
||
<span class="o">}</span>
|
||
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">Inner</span> <span class="o">{</span>
|
||
<span class="kd">private</span> <span class="nf">Inner</span><span class="o">(){}</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule by referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/AccessorClassGeneration"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="accessormethodgeneration">AccessorMethodGeneration</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 5.5.4</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p><strong>Maximum Language Version:</strong> Java 10</p>
|
||
|
||
<p>When accessing private fields / methods from another class, the Java compiler will generate accessor methods
|
||
with package-private visibility. This adds overhead, and to the dex method count on Android. This situation can
|
||
be avoided by changing the visibility of the field / method from private to package-private.</p>
|
||
|
||
<p><em>Note:</em> This rule is only executed for Java 10 or lower.
|
||
Since Java 11, <a href="https://openjdk.org/jeps/181">JEP 181: Nest-Based Access Control</a> has been implemented. This
|
||
means that in Java 11 and above accessor classes are not generated anymore.</p>
|
||
|
||
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/AccessorMethodGenerationRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.AccessorMethodGenerationRule</a></p>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">OuterClass</span> <span class="o">{</span>
|
||
<span class="kd">private</span> <span class="kt">int</span> <span class="n">counter</span><span class="o">;</span>
|
||
<span class="cm">/* package */</span> <span class="kt">int</span> <span class="n">id</span><span class="o">;</span>
|
||
|
||
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">InnerClass</span> <span class="o">{</span>
|
||
<span class="nc">InnerClass</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="nc">OuterClass</span><span class="o">.</span><span class="na">this</span><span class="o">.</span><span class="na">counter</span><span class="o">++;</span> <span class="c1">// wrong accessor method will be generated</span>
|
||
<span class="o">}</span>
|
||
|
||
<span class="kd">public</span> <span class="kt">int</span> <span class="nf">getOuterClassId</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="k">return</span> <span class="nc">OuterClass</span><span class="o">.</span><span class="na">this</span><span class="o">.</span><span class="na">id</span><span class="o">;</span> <span class="c1">// id is package-private, no accessor method needed</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule by referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/AccessorMethodGeneration"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="arrayisstoreddirectly">ArrayIsStoredDirectly</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 2.2</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Constructors and methods receiving arrays should clone objects and store the copy.
|
||
This prevents future changes from the user from affecting the original array.</p>
|
||
|
||
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/ArrayIsStoredDirectlyRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.ArrayIsStoredDirectlyRule</a></p>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
|
||
<span class="kd">private</span> <span class="nc">String</span> <span class="o">[]</span> <span class="n">x</span><span class="o">;</span>
|
||
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">foo</span> <span class="o">(</span><span class="nc">String</span> <span class="o">[]</span> <span class="n">param</span><span class="o">)</span> <span class="o">{</span>
|
||
<span class="c1">// Don't do this, make a copy of the array at least</span>
|
||
<span class="k">this</span><span class="o">.</span><span class="na">x</span><span class="o">=</span><span class="n">param</span><span class="o">;</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>This rule has the following properties:</strong></p>
|
||
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th>Name</th>
|
||
<th>Default Value</th>
|
||
<th>Description</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td>allowPrivate</td>
|
||
<td>true</td>
|
||
<td>If true, allow private methods/constructors to store arrays directly</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"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/ArrayIsStoredDirectly"</span> <span class="nt">/></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"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/ArrayIsStoredDirectly"</span><span class="nt">></span>
|
||
<span class="nt"><properties></span>
|
||
<span class="nt"><property</span> <span class="na">name=</span><span class="s">"allowPrivate"</span> <span class="na">value=</span><span class="s">"true"</span> <span class="nt">/></span>
|
||
<span class="nt"></properties></span>
|
||
<span class="nt"></rule></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="avoidmessagedigestfield">AvoidMessageDigestField</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 6.18.0</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Declaring a MessageDigest instance as a field make this instance directly available to multiple threads.
|
||
Such sharing of MessageDigest instances should be avoided if possible since it leads to wrong results
|
||
if the access is not synchronized correctly.
|
||
Just create a new instance and use it locally, where you need it.
|
||
Creating a new instance is easier than synchronizing access to a shared instance.</p>
|
||
|
||
<p><strong>This rule is defined by the following XPath expression:</strong></p>
|
||
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">FieldDeclaration</span><span class="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.security.MessageDigest'</span><span class="p">)]</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">import</span> <span class="nn">java.security.MessageDigest</span><span class="o">;</span>
|
||
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">AvoidMessageDigestFieldExample</span> <span class="o">{</span>
|
||
<span class="kd">private</span> <span class="kd">final</span> <span class="nc">MessageDigest</span> <span class="n">sharedMd</span><span class="o">;</span>
|
||
<span class="kd">public</span> <span class="nf">AvoidMessageDigestFieldExample</span><span class="o">()</span> <span class="kd">throws</span> <span class="nc">Exception</span> <span class="o">{</span>
|
||
<span class="n">sharedMd</span> <span class="o">=</span> <span class="nc">MessageDigest</span><span class="o">.</span><span class="na">getInstance</span><span class="o">(</span><span class="s">"SHA-256"</span><span class="o">);</span>
|
||
<span class="o">}</span>
|
||
<span class="kd">public</span> <span class="kt">byte</span><span class="o">[]</span> <span class="nf">calculateHashShared</span><span class="o">(</span><span class="kt">byte</span><span class="o">[]</span> <span class="n">data</span><span class="o">)</span> <span class="o">{</span>
|
||
<span class="c1">// sharing a MessageDigest like this without synchronizing access</span>
|
||
<span class="c1">// might lead to wrong results</span>
|
||
<span class="n">sharedMd</span><span class="o">.</span><span class="na">reset</span><span class="o">();</span>
|
||
<span class="n">sharedMd</span><span class="o">.</span><span class="na">update</span><span class="o">(</span><span class="n">data</span><span class="o">);</span>
|
||
<span class="k">return</span> <span class="n">sharedMd</span><span class="o">.</span><span class="na">digest</span><span class="o">();</span>
|
||
<span class="o">}</span>
|
||
|
||
<span class="c1">// better</span>
|
||
<span class="kd">public</span> <span class="kt">byte</span><span class="o">[]</span> <span class="nf">calculateHash</span><span class="o">(</span><span class="kt">byte</span><span class="o">[]</span> <span class="n">data</span><span class="o">)</span> <span class="kd">throws</span> <span class="nc">Exception</span> <span class="o">{</span>
|
||
<span class="nc">MessageDigest</span> <span class="n">md</span> <span class="o">=</span> <span class="nc">MessageDigest</span><span class="o">.</span><span class="na">getInstance</span><span class="o">(</span><span class="s">"SHA-256"</span><span class="o">);</span>
|
||
<span class="n">md</span><span class="o">.</span><span class="na">update</span><span class="o">(</span><span class="n">data</span><span class="o">);</span>
|
||
<span class="k">return</span> <span class="n">md</span><span class="o">.</span><span class="na">digest</span><span class="o">();</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule by referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/AvoidMessageDigestField"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="avoidprintstacktrace">AvoidPrintStackTrace</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 3.2</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Avoid printStackTrace(); use a logger call instead.</p>
|
||
|
||
<p><strong>This rule is defined by the following XPath expression:</strong></p>
|
||
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">MethodCall</span><span class="p">[</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">matchesSig</span><span class="p">(</span><span class="s2">"java.lang.Throwable#printStackTrace()"</span><span class="p">)</span><span class="w"> </span><span class="p">]</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
|
||
<span class="kt">void</span> <span class="nf">bar</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="k">try</span> <span class="o">{</span>
|
||
<span class="c1">// do something</span>
|
||
<span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="nc">Exception</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
|
||
<span class="n">e</span><span class="o">.</span><span class="na">printStackTrace</span><span class="o">();</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule by referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/AvoidPrintStackTrace"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="avoidreassigningcatchvariables">AvoidReassigningCatchVariables</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 6.27.0</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Reassigning exception variables caught in a catch statement should be avoided because of:</p>
|
||
|
||
<p>1) If it is needed, multi catch can be easily added and code will still compile.</p>
|
||
|
||
<p>2) Following the principle of least surprise we want to make sure that a variable caught in a catch statement
|
||
is always the one thrown in a try block.</p>
|
||
|
||
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/AvoidReassigningCatchVariablesRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.AvoidReassigningCatchVariablesRule</a></p>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
|
||
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">foo</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="k">try</span> <span class="o">{</span>
|
||
<span class="c1">// do something</span>
|
||
<span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="nc">Exception</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
|
||
<span class="n">e</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">NullPointerException</span><span class="o">();</span> <span class="c1">// not recommended</span>
|
||
<span class="o">}</span>
|
||
|
||
<span class="k">try</span> <span class="o">{</span>
|
||
<span class="c1">// do something</span>
|
||
<span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="nc">MyException</span> <span class="o">|</span> <span class="nc">ServerException</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
|
||
<span class="n">e</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">RuntimeException</span><span class="o">();</span> <span class="c1">// won't compile</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule by referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/AvoidReassigningCatchVariables"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="avoidreassigningloopvariables">AvoidReassigningLoopVariables</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 6.11.0</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Reassigning loop variables can lead to hard-to-find bugs. Prevent or limit how these variables can be changed.</p>
|
||
|
||
<p>In foreach-loops, configured by the <code class="language-plaintext highlighter-rouge">foreachReassign</code> property:</p>
|
||
<ul>
|
||
<li><code class="language-plaintext highlighter-rouge">deny</code>: Report any reassignment of the loop variable in the loop body. <em>This is the default.</em></li>
|
||
<li><code class="language-plaintext highlighter-rouge">allow</code>: Don’t check the loop variable.</li>
|
||
<li><code class="language-plaintext highlighter-rouge">firstOnly</code>: Report any reassignments of the loop variable, except as the first statement in the loop body.
|
||
<em>This is useful if some kind of normalization or clean-up of the value before using is permitted, but any other change of the variable is not.</em></li>
|
||
</ul>
|
||
|
||
<p>In for-loops, configured by the <code class="language-plaintext highlighter-rouge">forReassign</code> property:</p>
|
||
<ul>
|
||
<li><code class="language-plaintext highlighter-rouge">deny</code>: Report any reassignment of the control variable in the loop body. <em>This is the default.</em></li>
|
||
<li><code class="language-plaintext highlighter-rouge">allow</code>: Don’t check the control variable.</li>
|
||
<li><code class="language-plaintext highlighter-rouge">skip</code>: Report any reassignments of the control variable, except conditional increments/decrements (<code class="language-plaintext highlighter-rouge">++</code>, <code class="language-plaintext highlighter-rouge">--</code>, <code class="language-plaintext highlighter-rouge">+=</code>, <code class="language-plaintext highlighter-rouge">-=</code>).
|
||
<em>This prevents accidental reassignments or unconditional increments of the control variable.</em></li>
|
||
</ul>
|
||
|
||
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/AvoidReassigningLoopVariablesRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.AvoidReassigningLoopVariablesRule</a></p>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
|
||
<span class="kd">private</span> <span class="kt">void</span> <span class="nf">foo</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="k">for</span> <span class="o">(</span><span class="nc">String</span> <span class="n">s</span> <span class="o">:</span> <span class="n">listOfStrings</span><span class="o">())</span> <span class="o">{</span>
|
||
<span class="n">s</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="na">trim</span><span class="o">();</span> <span class="c1">// OK, when foreachReassign is "firstOnly" or "allow"</span>
|
||
<span class="n">doSomethingWith</span><span class="o">(</span><span class="n">s</span><span class="o">);</span>
|
||
|
||
<span class="n">s</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="na">toUpper</span><span class="o">();</span> <span class="c1">// OK, when foreachReassign is "allow"</span>
|
||
<span class="n">doSomethingElseWith</span><span class="o">(</span><span class="n">s</span><span class="o">);</span>
|
||
<span class="o">}</span>
|
||
|
||
<span class="k">for</span> <span class="o">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="o">;</span> <span class="n">i</span> <span class="o"><</span> <span class="mi">10</span><span class="o">;</span> <span class="n">i</span><span class="o">++)</span> <span class="o">{</span>
|
||
<span class="k">if</span> <span class="o">(</span><span class="n">check</span><span class="o">(</span><span class="n">i</span><span class="o">))</span> <span class="o">{</span>
|
||
<span class="n">i</span><span class="o">++;</span> <span class="c1">// OK, when forReassign is "skip" or "allow"</span>
|
||
<span class="o">}</span>
|
||
|
||
<span class="n">i</span> <span class="o">=</span> <span class="mi">5</span><span class="o">;</span> <span class="c1">// OK, when forReassign is "allow"</span>
|
||
|
||
<span class="n">doSomethingWith</span><span class="o">(</span><span class="n">i</span><span class="o">);</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>This rule has the following properties:</strong></p>
|
||
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th>Name</th>
|
||
<th>Default Value</th>
|
||
<th>Description</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td>foreachReassign</td>
|
||
<td>deny</td>
|
||
<td>how/if foreach control variables may be reassigned</td>
|
||
</tr>
|
||
<tr>
|
||
<td>forReassign</td>
|
||
<td>deny</td>
|
||
<td>how/if for control variables may be reassigned</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"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/AvoidReassigningLoopVariables"</span> <span class="nt">/></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"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/AvoidReassigningLoopVariables"</span><span class="nt">></span>
|
||
<span class="nt"><properties></span>
|
||
<span class="nt"><property</span> <span class="na">name=</span><span class="s">"foreachReassign"</span> <span class="na">value=</span><span class="s">"deny"</span> <span class="nt">/></span>
|
||
<span class="nt"><property</span> <span class="na">name=</span><span class="s">"forReassign"</span> <span class="na">value=</span><span class="s">"deny"</span> <span class="nt">/></span>
|
||
<span class="nt"></properties></span>
|
||
<span class="nt"></rule></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="avoidreassigningparameters">AvoidReassigningParameters</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 1.0</p>
|
||
|
||
<p><strong>Priority:</strong> Medium High (2)</p>
|
||
|
||
<p>Reassigning values to incoming parameters of a method or constructor is not recommended, as this can
|
||
make the code more difficult to understand. The code is often read with the assumption that parameter values
|
||
don’t change and an assignment violates therefore the principle of least astonishment. This is especially a
|
||
problem if the parameter is documented e.g. in the method’s javadoc and the new content differs from the original
|
||
documented content.</p>
|
||
|
||
<p>Use temporary local variables instead. This allows you to assign a new name, which makes the code better
|
||
understandable.</p>
|
||
|
||
<p>Note that this rule considers both methods and constructors. If there are multiple assignments for a formal
|
||
parameter, then only the first assignment is reported.</p>
|
||
|
||
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/AvoidReassigningParametersRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.AvoidReassigningParametersRule</a></p>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Hello</span> <span class="o">{</span>
|
||
<span class="kd">private</span> <span class="kt">void</span> <span class="nf">greet</span><span class="o">(</span><span class="nc">String</span> <span class="n">name</span><span class="o">)</span> <span class="o">{</span>
|
||
<span class="n">name</span> <span class="o">=</span> <span class="n">name</span><span class="o">.</span><span class="na">trim</span><span class="o">();</span>
|
||
<span class="nc">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">"Hello "</span> <span class="o">+</span> <span class="n">name</span><span class="o">);</span>
|
||
|
||
<span class="c1">// preferred</span>
|
||
<span class="nc">String</span> <span class="n">trimmedName</span> <span class="o">=</span> <span class="n">name</span><span class="o">.</span><span class="na">trim</span><span class="o">();</span>
|
||
<span class="nc">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">"Hello "</span> <span class="o">+</span> <span class="n">trimmedName</span><span class="o">);</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule by referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/AvoidReassigningParameters"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="avoidstringbufferfield">AvoidStringBufferField</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 4.2</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>StringBuffers/StringBuilders can grow considerably, and so may become a source of memory leaks
|
||
if held within objects with long lifetimes.</p>
|
||
|
||
<p><strong>This rule is defined by the following XPath expression:</strong></p>
|
||
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">FieldDeclaration</span><span class="o">/</span><span class="nt">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.StringBuffer'</span><span class="p">)</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'java.lang.StringBuilder'</span><span class="p">)]</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
|
||
<span class="kd">private</span> <span class="nc">StringBuffer</span> <span class="n">buffer</span><span class="o">;</span> <span class="c1">// potential memory leak as an instance variable;</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule by referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/AvoidStringBufferField"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="avoidusinghardcodedip">AvoidUsingHardCodedIP</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 4.1</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Application with hard-coded IP addresses can become impossible to deploy in some cases.
|
||
Externalizing IP adresses is preferable.</p>
|
||
|
||
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/AvoidUsingHardCodedIPRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.AvoidUsingHardCodedIPRule</a></p>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
|
||
<span class="kd">private</span> <span class="nc">String</span> <span class="n">ip</span> <span class="o">=</span> <span class="s">"127.0.0.1"</span><span class="o">;</span> <span class="c1">// not recommended</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>This rule has the following properties:</strong></p>
|
||
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th>Name</th>
|
||
<th>Default Value</th>
|
||
<th>Description</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td>checkAddressTypes</td>
|
||
<td>IPv4 , IPv6 , IPv4 mapped IPv6</td>
|
||
<td>Check for IP address types.</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"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/AvoidUsingHardCodedIP"</span> <span class="nt">/></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"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/AvoidUsingHardCodedIP"</span><span class="nt">></span>
|
||
<span class="nt"><properties></span>
|
||
<span class="nt"><property</span> <span class="na">name=</span><span class="s">"checkAddressTypes"</span> <span class="na">value=</span><span class="s">"IPv4,IPv6,IPv4 mapped IPv6"</span> <span class="nt">/></span>
|
||
<span class="nt"></properties></span>
|
||
<span class="nt"></rule></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="checkresultset">CheckResultSet</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 4.1</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Always check the return values of navigation methods (next, previous, first, last) of a ResultSet.
|
||
If the value return is ‘false’, it should be handled properly.</p>
|
||
|
||
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/CheckResultSetRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.CheckResultSetRule</a></p>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nc">Statement</span> <span class="n">stat</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="na">createStatement</span><span class="o">();</span>
|
||
<span class="nc">ResultSet</span> <span class="n">rst</span> <span class="o">=</span> <span class="n">stat</span><span class="o">.</span><span class="na">executeQuery</span><span class="o">(</span><span class="s">"SELECT name FROM person"</span><span class="o">);</span>
|
||
<span class="n">rst</span><span class="o">.</span><span class="na">next</span><span class="o">();</span> <span class="c1">// what if it returns false? bad form</span>
|
||
<span class="nc">String</span> <span class="n">firstName</span> <span class="o">=</span> <span class="n">rst</span><span class="o">.</span><span class="na">getString</span><span class="o">(</span><span class="mi">1</span><span class="o">);</span>
|
||
|
||
<span class="nc">Statement</span> <span class="n">stat</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="na">createStatement</span><span class="o">();</span>
|
||
<span class="nc">ResultSet</span> <span class="n">rst</span> <span class="o">=</span> <span class="n">stat</span><span class="o">.</span><span class="na">executeQuery</span><span class="o">(</span><span class="s">"SELECT name FROM person"</span><span class="o">);</span>
|
||
<span class="k">if</span> <span class="o">(</span><span class="n">rst</span><span class="o">.</span><span class="na">next</span><span class="o">())</span> <span class="o">{</span> <span class="c1">// result is properly examined and used</span>
|
||
<span class="nc">String</span> <span class="n">firstName</span> <span class="o">=</span> <span class="n">rst</span><span class="o">.</span><span class="na">getString</span><span class="o">(</span><span class="mi">1</span><span class="o">);</span>
|
||
<span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
|
||
<span class="c1">// handle missing data</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule by referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/CheckResultSet"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="constantsininterface">ConstantsInInterface</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 5.5</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Using constants in interfaces is a bad practice. Interfaces define types, constants are implementation details better placed in classes or enums. If the constants are best viewed as members of an enumerated type, you should export them with an enum type.
|
||
For other scenarios, consider using a utility class. See Effective Java’s ‘Use interfaces only to define types’.</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">@Interface</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="nf">true</span><span class="p">()][</span><span class="nv">$ignoreIfHasMethods</span><span class="err">=</span><span class="w"> </span><span class="nf">false</span><span class="p">()</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="ow">not</span><span class="p">(</span><span class="nt">ClassBody</span><span class="o">/</span><span class="nt">MethodDeclaration</span><span class="p">)]</span><span class="o">/</span><span class="nt">ClassBody</span><span class="o">/</span><span class="nt">FieldDeclaration</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">interface</span> <span class="nc">ConstantInterface</span> <span class="o">{</span>
|
||
<span class="kd">public</span> <span class="kd">static</span> <span class="kd">final</span> <span class="kt">int</span> <span class="no">CONST1</span> <span class="o">=</span> <span class="mi">1</span><span class="o">;</span> <span class="c1">// violation, no fields allowed in interface!</span>
|
||
<span class="kd">static</span> <span class="kd">final</span> <span class="kt">int</span> <span class="no">CONST2</span> <span class="o">=</span> <span class="mi">1</span><span class="o">;</span> <span class="c1">// violation, no fields allowed in interface!</span>
|
||
<span class="kd">final</span> <span class="kt">int</span> <span class="no">CONST3</span> <span class="o">=</span> <span class="mi">1</span><span class="o">;</span> <span class="c1">// violation, no fields allowed in interface!</span>
|
||
<span class="kt">int</span> <span class="no">CONST4</span> <span class="o">=</span> <span class="mi">1</span><span class="o">;</span> <span class="c1">// violation, no fields allowed in interface!</span>
|
||
<span class="o">}</span>
|
||
|
||
<span class="c1">// with ignoreIfHasMethods = false</span>
|
||
<span class="kd">public</span> <span class="kd">interface</span> <span class="nc">AnotherConstantInterface</span> <span class="o">{</span>
|
||
<span class="kd">public</span> <span class="kd">static</span> <span class="kd">final</span> <span class="kt">int</span> <span class="no">CONST1</span> <span class="o">=</span> <span class="mi">1</span><span class="o">;</span> <span class="c1">// violation, no fields allowed in interface!</span>
|
||
|
||
<span class="kt">int</span> <span class="nf">anyMethod</span><span class="o">();</span>
|
||
<span class="o">}</span>
|
||
|
||
<span class="c1">// with ignoreIfHasMethods = true</span>
|
||
<span class="kd">public</span> <span class="kd">interface</span> <span class="nc">YetAnotherConstantInterface</span> <span class="o">{</span>
|
||
<span class="kd">public</span> <span class="kd">static</span> <span class="kd">final</span> <span class="kt">int</span> <span class="no">CONST1</span> <span class="o">=</span> <span class="mi">1</span><span class="o">;</span> <span class="c1">// no violation</span>
|
||
|
||
<span class="kt">int</span> <span class="nf">anyMethod</span><span class="o">();</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>This rule has the following properties:</strong></p>
|
||
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th>Name</th>
|
||
<th>Default Value</th>
|
||
<th>Description</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td>ignoreIfHasMethods</td>
|
||
<td>true</td>
|
||
<td>Whether to ignore constants in interfaces if the interface defines any 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"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/ConstantsInInterface"</span> <span class="nt">/></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"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/ConstantsInInterface"</span><span class="nt">></span>
|
||
<span class="nt"><properties></span>
|
||
<span class="nt"><property</span> <span class="na">name=</span><span class="s">"ignoreIfHasMethods"</span> <span class="na">value=</span><span class="s">"true"</span> <span class="nt">/></span>
|
||
<span class="nt"></properties></span>
|
||
<span class="nt"></rule></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="defaultlabelnotlastinswitchstmt">DefaultLabelNotLastInSwitchStmt</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 1.5</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>By convention, the default label should be the last label in a switch statement.</p>
|
||
|
||
<p><strong>This rule is defined by the following XPath expression:</strong></p>
|
||
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">SwitchLabel</span><span class="p">[</span><span class="na">@Default</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="ow">not</span><span class="p">(..</span><span class="w"> </span><span class="kr">is</span><span class="w"> </span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="o">*</span><span class="p">[</span><span class="nf">last</span><span class="p">()])]</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
|
||
<span class="kt">void</span> <span class="nf">bar</span><span class="o">(</span><span class="kt">int</span> <span class="n">a</span><span class="o">)</span> <span class="o">{</span>
|
||
<span class="k">switch</span> <span class="o">(</span><span class="n">a</span><span class="o">)</span> <span class="o">{</span>
|
||
<span class="k">case</span> <span class="mi">1</span><span class="o">:</span> <span class="c1">// do something</span>
|
||
<span class="k">break</span><span class="o">;</span>
|
||
<span class="k">default</span><span class="o">:</span> <span class="c1">// the default case should be last, by convention</span>
|
||
<span class="k">break</span><span class="o">;</span>
|
||
<span class="k">case</span> <span class="mi">2</span><span class="o">:</span>
|
||
<span class="k">break</span><span class="o">;</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule by referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/DefaultLabelNotLastInSwitchStmt"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="doublebraceinitialization">DoubleBraceInitialization</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 6.16.0</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Double brace initialisation is a pattern to initialise eg collections concisely. But it implicitly
|
||
generates a new .class file, and the object holds a strong reference to the enclosing object. For those
|
||
reasons, it is preferable to initialize the object normally, even though it’s verbose.</p>
|
||
|
||
<p>This rule counts any anonymous class which only has a single initializer as an instance of double-brace
|
||
initialization. There is currently no way to find out whether a method called in the initializer is not
|
||
accessible from outside the anonymous class, and those legit cases should be suppressed for the time being.</p>
|
||
|
||
<p><strong>This rule is defined by the following XPath expression:</strong></p>
|
||
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">ConstructorCall</span><span class="o">/</span><span class="nt">AnonymousClassDeclaration</span><span class="o">/</span><span class="nt">ClassBody</span><span class="p">[</span><span class="nf">count</span><span class="p">(</span><span class="err">*</span><span class="p">)</span><span class="err">=</span><span class="m">1</span><span class="p">]</span><span class="o">/</span><span class="nt">Initializer</span><span class="p">[</span><span class="na">@Static</span><span class="err">=</span><span class="nf">false</span><span class="p">()]</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">// this is double-brace initialization</span>
|
||
<span class="k">return</span> <span class="k">new</span> <span class="nc">ArrayList</span><span class="o"><</span><span class="nc">String</span><span class="o">>(){{</span>
|
||
<span class="n">add</span><span class="o">(</span><span class="s">"a"</span><span class="o">);</span>
|
||
<span class="n">add</span><span class="o">(</span><span class="s">"b"</span><span class="o">);</span>
|
||
<span class="n">add</span><span class="o">(</span><span class="s">"c"</span><span class="o">);</span>
|
||
<span class="o">}};</span>
|
||
|
||
<span class="c1">// the better way is to not create an anonymous class:</span>
|
||
<span class="nc">List</span><span class="o"><</span><span class="nc">String</span><span class="o">></span> <span class="n">a</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">ArrayList</span><span class="o"><>();</span>
|
||
<span class="n">a</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="s">"a"</span><span class="o">);</span>
|
||
<span class="n">a</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="s">"b"</span><span class="o">);</span>
|
||
<span class="n">a</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="s">"c"</span><span class="o">);</span>
|
||
<span class="k">return</span> <span class="n">a</span><span class="o">;</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule by referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/DoubleBraceInitialization"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="forloopcanbeforeach">ForLoopCanBeForeach</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 6.0.0</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p><strong>Minimum Language Version:</strong> Java 1.5</p>
|
||
|
||
<p>Reports loops that can be safely replaced with the foreach syntax. The rule considers loops over
|
||
lists, arrays and iterators. A loop is safe to replace if it only uses the index variable to
|
||
access an element of the list or array, only has one update statement, and loops through <em>every</em>
|
||
element of the list or array left to right.</p>
|
||
|
||
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/ForLoopCanBeForeachRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.ForLoopCanBeForeachRule</a></p>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">MyClass</span> <span class="o">{</span>
|
||
<span class="kt">void</span> <span class="nf">loop</span><span class="o">(</span><span class="nc">List</span><span class="o"><</span><span class="nc">String</span><span class="o">></span> <span class="n">l</span><span class="o">)</span> <span class="o">{</span>
|
||
<span class="k">for</span> <span class="o">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span> <span class="n">i</span> <span class="o"><</span> <span class="n">l</span><span class="o">.</span><span class="na">size</span><span class="o">();</span> <span class="n">i</span><span class="o">++)</span> <span class="o">{</span> <span class="c1">// pre Java 1.5</span>
|
||
<span class="nc">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">l</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">i</span><span class="o">));</span>
|
||
<span class="o">}</span>
|
||
|
||
<span class="k">for</span> <span class="o">(</span><span class="nc">String</span> <span class="n">s</span> <span class="o">:</span> <span class="n">l</span><span class="o">)</span> <span class="o">{</span> <span class="c1">// post Java 1.5</span>
|
||
<span class="nc">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">s</span><span class="o">);</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule by referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/ForLoopCanBeForeach"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="forloopvariablecount">ForLoopVariableCount</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 6.11.0</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Having a lot of control variables in a ‘for’ loop makes it harder to see what range of values
|
||
the loop iterates over. By default this rule allows a regular ‘for’ loop with only one variable.</p>
|
||
|
||
<p><strong>This rule is defined by the following XPath expression:</strong></p>
|
||
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">ForInit</span><span class="o">/</span><span class="nt">LocalVariableDeclaration</span><span class="p">[</span><span class="nf">count</span><span class="p">(</span><span class="nt">VariableDeclarator</span><span class="p">)</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="nv">$maximumVariables</span><span class="p">]</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">// this will be reported with the default setting of at most one control variable in a for loop</span>
|
||
<span class="k">for</span> <span class="o">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="o">,</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span> <span class="n">i</span> <span class="o"><</span> <span class="mi">10</span><span class="o">;</span> <span class="n">i</span><span class="o">++,</span> <span class="n">j</span> <span class="o">+=</span> <span class="mi">2</span><span class="o">)</span> <span class="o">{</span>
|
||
<span class="n">foo</span><span class="o">();</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>This rule has the following properties:</strong></p>
|
||
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th>Name</th>
|
||
<th>Default Value</th>
|
||
<th>Description</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td>maximumVariables</td>
|
||
<td>1</td>
|
||
<td>A regular for statement will have 1 control variable</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"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/ForLoopVariableCount"</span> <span class="nt">/></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"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/ForLoopVariableCount"</span><span class="nt">></span>
|
||
<span class="nt"><properties></span>
|
||
<span class="nt"><property</span> <span class="na">name=</span><span class="s">"maximumVariables"</span> <span class="na">value=</span><span class="s">"1"</span> <span class="nt">/></span>
|
||
<span class="nt"></properties></span>
|
||
<span class="nt"></rule></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="guardlogstatement">GuardLogStatement</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 5.1.0</p>
|
||
|
||
<p><strong>Priority:</strong> Medium High (2)</p>
|
||
|
||
<p>Whenever using a log level, one should check if the loglevel is actually enabled, or
|
||
otherwise skip the associate String creation and manipulation.</p>
|
||
|
||
<p>An alternative to checking the log level are substituting parameters, formatters or lazy logging
|
||
with lambdas. The available alternatives depend on the actual logging framework.</p>
|
||
|
||
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/GuardLogStatementRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.GuardLogStatementRule</a></p>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">// Add this for performance</span>
|
||
<span class="k">if</span> <span class="o">(</span><span class="n">log</span><span class="o">.</span><span class="na">isDebugEnabled</span><span class="o">())</span> <span class="o">{</span>
|
||
<span class="n">log</span><span class="o">.</span><span class="na">debug</span><span class="o">(</span><span class="s">"log something"</span> <span class="o">+</span> <span class="n">param1</span> <span class="o">+</span> <span class="s">" and "</span> <span class="o">+</span> <span class="n">param2</span> <span class="o">+</span> <span class="s">"concat strings"</span><span class="o">);</span>
|
||
<span class="o">}</span>
|
||
|
||
<span class="c1">// Avoid the guarding if statement with substituting parameters</span>
|
||
<span class="n">log</span><span class="o">.</span><span class="na">debug</span><span class="o">(</span><span class="s">"log something {} and {}"</span><span class="o">,</span> <span class="n">param1</span><span class="o">,</span> <span class="n">param2</span><span class="o">);</span>
|
||
|
||
<span class="c1">// Avoid the guarding if statement with formatters</span>
|
||
<span class="n">log</span><span class="o">.</span><span class="na">debug</span><span class="o">(</span><span class="s">"log something %s and %s"</span><span class="o">,</span> <span class="n">param1</span><span class="o">,</span> <span class="n">param2</span><span class="o">);</span>
|
||
|
||
<span class="c1">// Avoid the guarding if statement with lazy logging and lambdas</span>
|
||
<span class="n">log</span><span class="o">.</span><span class="na">debug</span><span class="o">(</span><span class="s">"log something expensive: {}"</span><span class="o">,</span> <span class="o">()</span> <span class="o">-></span> <span class="n">calculateExpensiveLoggingText</span><span class="o">());</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>This rule has the following properties:</strong></p>
|
||
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th>Name</th>
|
||
<th>Default Value</th>
|
||
<th>Description</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td>logLevels</td>
|
||
<td>trace , debug , info , warn , error , log , finest , finer , fine , info , warning , severe</td>
|
||
<td>LogLevels to guard</td>
|
||
</tr>
|
||
<tr>
|
||
<td>guardsMethods</td>
|
||
<td>isTraceEnabled , isDebugEnabled , isInfoEnabled , isWarnEnabled , isErrorEnabled , isLoggable</td>
|
||
<td>Method use to guard the log 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"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/GuardLogStatement"</span> <span class="nt">/></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"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/GuardLogStatement"</span><span class="nt">></span>
|
||
<span class="nt"><properties></span>
|
||
<span class="nt"><property</span> <span class="na">name=</span><span class="s">"logLevels"</span> <span class="na">value=</span><span class="s">"trace,debug,info,warn,error,log,finest,finer,fine,info,warning,severe"</span> <span class="nt">/></span>
|
||
<span class="nt"><property</span> <span class="na">name=</span><span class="s">"guardsMethods"</span> <span class="na">value=</span><span class="s">"isTraceEnabled,isDebugEnabled,isInfoEnabled,isWarnEnabled,isErrorEnabled,isLoggable"</span> <span class="nt">/></span>
|
||
<span class="nt"></properties></span>
|
||
<span class="nt"></rule></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="junit4suitesshouldusesuiteannotation">JUnit4SuitesShouldUseSuiteAnnotation</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 4.0</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>In JUnit 3, test suites are indicated by the suite() method. In JUnit 4, suites are indicated
|
||
through the @RunWith(Suite.class) annotation.</p>
|
||
|
||
<p><strong>This rule is defined by the following XPath expression:</strong></p>
|
||
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">MethodDeclaration</span><span class="p">[</span><span class="na">@Name</span><span class="err">=</span><span class="s1">'suite'</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="nt">ClassType</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'junit.framework.Test'</span><span class="p">)]]</span><span class="w">
|
||
</span><span class="p">[</span><span class="ow">not</span><span class="p">(.</span><span class="o">//</span><span class="nt">ReturnStatement</span><span class="err">/</span><span class="o">*</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'junit.framework.JUnit4TestAdapter'</span><span class="p">)])]</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">BadExample</span> <span class="kd">extends</span> <span class="nc">TestCase</span><span class="o">{</span>
|
||
|
||
<span class="kd">public</span> <span class="kd">static</span> <span class="nc">Test</span> <span class="nf">suite</span><span class="o">(){</span>
|
||
<span class="k">return</span> <span class="k">new</span> <span class="nf">Suite</span><span class="o">();</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
|
||
<span class="nd">@RunWith</span><span class="o">(</span><span class="nc">Suite</span><span class="o">.</span><span class="na">class</span><span class="o">)</span>
|
||
<span class="nd">@SuiteClasses</span><span class="o">(</span> <span class="o">{</span> <span class="nc">TestOne</span><span class="o">.</span><span class="na">class</span><span class="o">,</span> <span class="nc">TestTwo</span><span class="o">.</span><span class="na">class</span> <span class="o">})</span>
|
||
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">GoodTest</span> <span class="o">{</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule by referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/JUnit4SuitesShouldUseSuiteAnnotation"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="junit4testshoulduseafterannotation">JUnit4TestShouldUseAfterAnnotation</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 4.0</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>In JUnit 3, the tearDown method was used to clean up all data entities required in running tests.
|
||
JUnit 4 skips the tearDown method and executes all methods annotated with @After after running each test.
|
||
JUnit 5 introduced @AfterEach and @AfterAll annotations to execute methods after each test or after all tests in the class, respectively.</p>
|
||
|
||
<p><strong>This rule is defined by the following XPath expression:</strong></p>
|
||
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">MethodDeclaration</span><span class="p">[</span><span class="na">@Name</span><span class="err">=</span><span class="s1">'tearDown'</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="na">@Arity</span><span class="err">=</span><span class="m">0</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">ModifierList</span><span class="o">/</span><span class="nt">Annotation</span><span class="p">[</span><span class="w">
|
||
</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.After'</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.AfterEach'</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.AfterAll'</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.testng.annotations.AfterMethod'</span><span class="p">)])]</span><span class="w">
|
||
</span><span class="c">(: Make sure this is a junit 4 class :)</span><span class="w">
|
||
</span><span class="p">[..</span><span class="o">/</span><span class="nt">MethodDeclaration</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">hasAnnotation</span><span class="p">(</span><span class="s1">'org.junit.Test'</span><span class="p">)]]</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">MyTest</span> <span class="o">{</span>
|
||
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">tearDown</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="n">bad</span><span class="o">();</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">MyTest2</span> <span class="o">{</span>
|
||
<span class="nd">@After</span> <span class="kd">public</span> <span class="kt">void</span> <span class="nf">tearDown</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="n">good</span><span class="o">();</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule by referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/JUnit4TestShouldUseAfterAnnotation"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="junit4testshouldusebeforeannotation">JUnit4TestShouldUseBeforeAnnotation</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 4.0</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>In JUnit 3, the setUp method was used to set up all data entities required in running tests.
|
||
JUnit 4 skips the setUp method and executes all methods annotated with @Before before all tests.
|
||
JUnit 5 introduced @BeforeEach and @BeforeAll annotations to execute methods before each test or before all tests in the class, respectively.</p>
|
||
|
||
<p><strong>This rule is defined by the following XPath expression:</strong></p>
|
||
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">MethodDeclaration</span><span class="p">[</span><span class="na">@Name</span><span class="err">=</span><span class="s1">'setUp'</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="na">@Arity</span><span class="err">=</span><span class="m">0</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">ModifierList</span><span class="o">/</span><span class="nt">Annotation</span><span class="p">[</span><span class="w">
|
||
</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.Before'</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.BeforeEach'</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.BeforeAll'</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.testng.annotations.BeforeMethod'</span><span class="p">)])]</span><span class="w">
|
||
</span><span class="c">(: Make sure this is a junit 4 class :)</span><span class="w">
|
||
</span><span class="p">[..</span><span class="o">/</span><span class="nt">MethodDeclaration</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">hasAnnotation</span><span class="p">(</span><span class="s1">'org.junit.Test'</span><span class="p">)]]</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">MyTest</span> <span class="o">{</span>
|
||
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">setUp</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="n">bad</span><span class="o">();</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">MyTest2</span> <span class="o">{</span>
|
||
<span class="nd">@Before</span> <span class="kd">public</span> <span class="kt">void</span> <span class="nf">setUp</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="n">good</span><span class="o">();</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule by referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/JUnit4TestShouldUseBeforeAnnotation"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="junit4testshouldusetestannotation">JUnit4TestShouldUseTestAnnotation</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 4.0</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>In JUnit 3, the framework executed all methods which started with the word test as a unit test.
|
||
In JUnit 4, only methods annotated with the @Test annotation are executed.
|
||
In JUnit 5, one of the following annotations should be used for tests: @Test, @RepeatedTest, @TestFactory, @TestTemplate or @ParameterizedTest.
|
||
In TestNG, only methods annotated with the @Test annotation are executed.</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">matches</span><span class="p">(</span><span class="na">@SimpleName</span><span class="p">,</span><span class="w"> </span><span class="nv">$testClassPattern</span><span class="p">)</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'junit.framework.TestCase'</span><span class="p">)]</span><span class="w">
|
||
</span><span class="c">(: a junit 3 method :)</span><span class="w">
|
||
</span><span class="o">/</span><span class="nt">ClassBody</span><span class="o">/</span><span class="nt">MethodDeclaration</span><span class="p">[</span><span class="w">
|
||
</span><span class="na">@Visibility</span><span class="err">=</span><span class="s2">"public"</span><span class="w">
|
||
</span><span class="ow">and</span><span class="w"> </span><span class="nf">starts-with</span><span class="p">(</span><span class="na">@Name</span><span class="p">,</span><span class="w"> </span><span class="s1">'test'</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">and</span><span class="w"> </span><span class="ow">not</span><span class="p">(</span><span class="nt">ModifierList</span><span class="o">/</span><span class="nt">Annotation</span><span class="p">[</span><span class="w">
|
||
</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.Test'</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.Test'</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.RepeatedTest'</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.TestFactory'</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.TestTemplate'</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.params.ParameterizedTest'</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.testng.annotations.Test'</span><span class="p">)</span><span class="w">
|
||
</span><span class="p">]</span><span class="w">
|
||
</span><span class="p">)</span><span class="w">
|
||
</span><span class="p">]</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">MyTest</span> <span class="o">{</span>
|
||
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">testBad</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="n">doSomething</span><span class="o">();</span>
|
||
<span class="o">}</span>
|
||
|
||
<span class="nd">@Test</span>
|
||
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">testGood</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="n">doSomething</span><span class="o">();</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>This rule has the following properties:</strong></p>
|
||
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th>Name</th>
|
||
<th>Default Value</th>
|
||
<th>Description</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td>testClassPattern</td>
|
||
<td>Test</td>
|
||
<td>The regex pattern used to identify test classes</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"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/JUnit4TestShouldUseTestAnnotation"</span> <span class="nt">/></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"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/JUnit4TestShouldUseTestAnnotation"</span><span class="nt">></span>
|
||
<span class="nt"><properties></span>
|
||
<span class="nt"><property</span> <span class="na">name=</span><span class="s">"testClassPattern"</span> <span class="na">value=</span><span class="s">"Test"</span> <span class="nt">/></span>
|
||
<span class="nt"></properties></span>
|
||
<span class="nt"></rule></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="junit5testshouldbepackageprivate">JUnit5TestShouldBePackagePrivate</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 6.35.0</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Reports JUnit 5 test classes and methods that are not package-private.
|
||
Contrary to JUnit 4 tests, which required public visibility to be run by the engine,
|
||
JUnit 5 tests can also be run if they’re package-private. Marking them as such
|
||
is a good practice to limit their visibility.</p>
|
||
|
||
<p>Test methods are identified as those which use <code class="language-plaintext highlighter-rouge">@Test</code>, <code class="language-plaintext highlighter-rouge">@RepeatedTest</code>,
|
||
<code class="language-plaintext highlighter-rouge">@TestFactory</code>, <code class="language-plaintext highlighter-rouge">@TestTemplate</code> or <code class="language-plaintext highlighter-rouge">@ParameterizedTest</code>.</p>
|
||
|
||
<p><strong>This rule is defined by the following XPath expression:</strong></p>
|
||
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">ClassDeclaration</span><span class="p">[</span><span class="w">
|
||
</span><span class="c">(: a Junit 5 test class, ie, it has methods with the annotation :)</span><span class="w">
|
||
</span><span class="na">@Interface</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="nf">false</span><span class="p">()</span><span class="w"> </span><span class="ow">and</span><span class="w">
|
||
</span><span class="nt">ClassBody</span><span class="o">/</span><span class="nt">MethodDeclaration</span><span class="w">
|
||
</span><span class="p">[</span><span class="nt">ModifierList</span><span class="o">/</span><span class="nt">Annotation</span><span class="p">[</span><span class="w">
|
||
</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.Test'</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.RepeatedTest'</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.TestFactory'</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.TestTemplate'</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.params.ParameterizedTest'</span><span class="p">)</span><span class="w">
|
||
</span><span class="p">]]</span><span class="w">
|
||
</span><span class="p">]</span><span class="err">/</span><span class="p">(</span><span class="w">
|
||
</span><span class="kn">self::</span><span class="o">*</span><span class="p">[</span><span class="na">@Abstract</span><span class="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">@Visibility</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="p">(</span><span class="s2">"public"</span><span class="p">,</span><span class="w"> </span><span class="s2">"protected"</span><span class="p">)]</span><span class="w">
|
||
</span><span class="p">|</span><span class="w"> </span><span class="nt">ClassBody</span><span class="o">/</span><span class="nt">MethodDeclaration</span><span class="w">
|
||
</span><span class="p">[</span><span class="na">@Visibility</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="p">(</span><span class="s2">"public"</span><span class="p">,</span><span class="w"> </span><span class="s2">"protected"</span><span class="p">)]</span><span class="w">
|
||
</span><span class="p">[</span><span class="nt">ModifierList</span><span class="o">/</span><span class="nt">Annotation</span><span class="p">[</span><span class="w">
|
||
</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.Test'</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.RepeatedTest'</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.TestFactory'</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.TestTemplate'</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.params.ParameterizedTest'</span><span class="p">)</span><span class="w">
|
||
</span><span class="p">]]</span><span class="w">
|
||
</span><span class="p">)</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">class</span> <span class="nc">MyTest</span> <span class="o">{</span> <span class="c1">// not public, that's fine</span>
|
||
<span class="nd">@Test</span>
|
||
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">testBad</span><span class="o">()</span> <span class="o">{</span> <span class="o">}</span> <span class="c1">// should not have a public modifier</span>
|
||
|
||
<span class="nd">@Test</span>
|
||
<span class="kd">protected</span> <span class="kt">void</span> <span class="nf">testAlsoBad</span><span class="o">()</span> <span class="o">{</span> <span class="o">}</span> <span class="c1">// should not have a protected modifier</span>
|
||
|
||
<span class="nd">@Test</span>
|
||
<span class="kd">private</span> <span class="kt">void</span> <span class="nf">testNoRun</span><span class="o">()</span> <span class="o">{</span> <span class="o">}</span> <span class="c1">// should not have a private modifier</span>
|
||
|
||
<span class="nd">@Test</span>
|
||
<span class="kt">void</span> <span class="nf">testGood</span><span class="o">()</span> <span class="o">{</span> <span class="o">}</span> <span class="c1">// package private as expected</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule by referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/JUnit5TestShouldBePackagePrivate"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="junitassertionsshouldincludemessage">JUnitAssertionsShouldIncludeMessage</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 1.04</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>JUnit assertions should include an informative message - i.e., use the three-argument version of
|
||
assertEquals(), not the two-argument version.</p>
|
||
|
||
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/JUnitAssertionsShouldIncludeMessageRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.JUnitAssertionsShouldIncludeMessageRule</a></p>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="kd">extends</span> <span class="nc">TestCase</span> <span class="o">{</span>
|
||
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">testSomething</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="n">assertEquals</span><span class="o">(</span><span class="s">"foo"</span><span class="o">,</span> <span class="s">"bar"</span><span class="o">);</span>
|
||
<span class="c1">// Use the form:</span>
|
||
<span class="c1">// assertEquals("Foo does not equals bar", "foo", "bar");</span>
|
||
<span class="c1">// instead</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule by referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/JUnitAssertionsShouldIncludeMessage"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="junittestcontainstoomanyasserts">JUnitTestContainsTooManyAsserts</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 5.0</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Unit tests should not contain too many asserts. Many asserts are indicative of a complex test, for which
|
||
it is harder to verify correctness. Consider breaking the test scenario into multiple, shorter test scenarios.
|
||
Customize the maximum number of assertions used by this Rule to suit your needs.</p>
|
||
|
||
<p>This rule checks for JUnit4, JUnit5 and TestNG Tests, as well as methods starting with "test".</p>
|
||
|
||
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/JUnitTestContainsTooManyAssertsRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.JUnitTestContainsTooManyAssertsRule</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">MyTestCase</span> <span class="kd">extends</span> <span class="nc">TestCase</span> <span class="o">{</span>
|
||
<span class="c1">// Ok</span>
|
||
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">testMyCaseWithOneAssert</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="kt">boolean</span> <span class="n">myVar</span> <span class="o">=</span> <span class="kc">false</span><span class="o">;</span>
|
||
<span class="n">assertFalse</span><span class="o">(</span><span class="s">"should be false"</span><span class="o">,</span> <span class="n">myVar</span><span class="o">);</span>
|
||
<span class="o">}</span>
|
||
|
||
<span class="c1">// Bad, too many asserts (assuming max=1)</span>
|
||
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">testMyCaseWithMoreAsserts</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="kt">boolean</span> <span class="n">myVar</span> <span class="o">=</span> <span class="kc">false</span><span class="o">;</span>
|
||
<span class="n">assertFalse</span><span class="o">(</span><span class="s">"myVar should be false"</span><span class="o">,</span> <span class="n">myVar</span><span class="o">);</span>
|
||
<span class="n">assertEquals</span><span class="o">(</span><span class="s">"should equals false"</span><span class="o">,</span> <span class="kc">false</span><span class="o">,</span> <span class="n">myVar</span><span class="o">);</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>This rule has the following properties:</strong></p>
|
||
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th>Name</th>
|
||
<th>Default Value</th>
|
||
<th>Description</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td>maximumAsserts</td>
|
||
<td>1</td>
|
||
<td>Maximum number of assert calls in a test method</td>
|
||
</tr>
|
||
<tr>
|
||
<td>extraAssertMethodNames</td>
|
||
<td> </td>
|
||
<td>Extra valid assertion methods 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"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/JUnitTestContainsTooManyAsserts"</span> <span class="nt">/></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"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/JUnitTestContainsTooManyAsserts"</span><span class="nt">></span>
|
||
<span class="nt"><properties></span>
|
||
<span class="nt"><property</span> <span class="na">name=</span><span class="s">"maximumAsserts"</span> <span class="na">value=</span><span class="s">"1"</span> <span class="nt">/></span>
|
||
<span class="nt"><property</span> <span class="na">name=</span><span class="s">"extraAssertMethodNames"</span> <span class="na">value=</span><span class="s">""</span> <span class="nt">/></span>
|
||
<span class="nt"></properties></span>
|
||
<span class="nt"></rule></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="junittestsshouldincludeassert">JUnitTestsShouldIncludeAssert</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 2.0</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>JUnit tests should include at least one assertion. This makes the tests more robust, and using assert
|
||
with messages provide the developer a clearer idea of what the test does.</p>
|
||
|
||
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/JUnitTestsShouldIncludeAssertRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.JUnitTestsShouldIncludeAssertRule</a></p>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="kd">extends</span> <span class="nc">TestCase</span> <span class="o">{</span>
|
||
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">testSomething</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="nc">Bar</span> <span class="n">b</span> <span class="o">=</span> <span class="n">findBar</span><span class="o">();</span>
|
||
<span class="c1">// This is better than having a NullPointerException</span>
|
||
<span class="c1">// assertNotNull("bar not found", b);</span>
|
||
<span class="n">b</span><span class="o">.</span><span class="na">work</span><span class="o">();</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>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>extraAssertMethodNames</td>
|
||
<td> </td>
|
||
<td>Extra valid assertion methods 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"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/JUnitTestsShouldIncludeAssert"</span> <span class="nt">/></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"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/JUnitTestsShouldIncludeAssert"</span><span class="nt">></span>
|
||
<span class="nt"><properties></span>
|
||
<span class="nt"><property</span> <span class="na">name=</span><span class="s">"extraAssertMethodNames"</span> <span class="na">value=</span><span class="s">""</span> <span class="nt">/></span>
|
||
<span class="nt"></properties></span>
|
||
<span class="nt"></rule></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="junituseexpected">JUnitUseExpected</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 4.0</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>In JUnit4, use the @Test(expected) annotation to denote tests that should throw exceptions.</p>
|
||
|
||
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/JUnitUseExpectedRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.JUnitUseExpectedRule</a></p>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">MyTest</span> <span class="o">{</span>
|
||
<span class="nd">@Test</span>
|
||
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">testBad</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="k">try</span> <span class="o">{</span>
|
||
<span class="n">doSomething</span><span class="o">();</span>
|
||
<span class="n">fail</span><span class="o">(</span><span class="s">"should have thrown an exception"</span><span class="o">);</span>
|
||
<span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="nc">Exception</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
|
||
<span class="nd">@Test</span><span class="o">(</span><span class="n">expected</span><span class="o">=</span><span class="nc">Exception</span><span class="o">.</span><span class="na">class</span><span class="o">)</span>
|
||
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">testGood</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="n">doSomething</span><span class="o">();</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule by referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/JUnitUseExpected"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="literalsfirstincomparisons">LiteralsFirstInComparisons</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 6.24.0</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Position literals first in all String comparisons, if the second argument is null then NullPointerExceptions
|
||
can be avoided, they will just return false. Note that switching literal positions for compareTo and
|
||
compareToIgnoreCase may change the result, see examples.</p>
|
||
|
||
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/LiteralsFirstInComparisonsRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.LiteralsFirstInComparisonsRule</a></p>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
|
||
<span class="kt">boolean</span> <span class="nf">bar</span><span class="o">(</span><span class="nc">String</span> <span class="n">x</span><span class="o">)</span> <span class="o">{</span>
|
||
<span class="k">return</span> <span class="n">x</span><span class="o">.</span><span class="na">equals</span><span class="o">(</span><span class="s">"2"</span><span class="o">);</span> <span class="c1">// should be "2".equals(x)</span>
|
||
<span class="o">}</span>
|
||
<span class="kt">boolean</span> <span class="nf">bar</span><span class="o">(</span><span class="nc">String</span> <span class="n">x</span><span class="o">)</span> <span class="o">{</span>
|
||
<span class="k">return</span> <span class="n">x</span><span class="o">.</span><span class="na">equalsIgnoreCase</span><span class="o">(</span><span class="s">"2"</span><span class="o">);</span> <span class="c1">// should be "2".equalsIgnoreCase(x)</span>
|
||
<span class="o">}</span>
|
||
<span class="kt">boolean</span> <span class="nf">bar</span><span class="o">(</span><span class="nc">String</span> <span class="n">x</span><span class="o">)</span> <span class="o">{</span>
|
||
<span class="k">return</span> <span class="o">(</span><span class="n">x</span><span class="o">.</span><span class="na">compareTo</span><span class="o">(</span><span class="s">"bar"</span><span class="o">)</span> <span class="o">></span> <span class="mi">0</span><span class="o">);</span> <span class="c1">// should be: "bar".compareTo(x) < 0</span>
|
||
<span class="o">}</span>
|
||
<span class="kt">boolean</span> <span class="nf">bar</span><span class="o">(</span><span class="nc">String</span> <span class="n">x</span><span class="o">)</span> <span class="o">{</span>
|
||
<span class="k">return</span> <span class="o">(</span><span class="n">x</span><span class="o">.</span><span class="na">compareToIgnoreCase</span><span class="o">(</span><span class="s">"bar"</span><span class="o">)</span> <span class="o">></span> <span class="mi">0</span><span class="o">);</span> <span class="c1">// should be: "bar".compareToIgnoreCase(x) < 0</span>
|
||
<span class="o">}</span>
|
||
<span class="kt">boolean</span> <span class="nf">bar</span><span class="o">(</span><span class="nc">String</span> <span class="n">x</span><span class="o">)</span> <span class="o">{</span>
|
||
<span class="k">return</span> <span class="n">x</span><span class="o">.</span><span class="na">contentEquals</span><span class="o">(</span><span class="s">"bar"</span><span class="o">);</span> <span class="c1">// should be "bar".contentEquals(x)</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule by referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/LiteralsFirstInComparisons"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="loosecoupling">LooseCoupling</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 0.7</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Excessive coupling to implementation types (e.g., <code class="language-plaintext highlighter-rouge">HashSet</code>) limits your ability to use alternate
|
||
implementations in the future as requirements change. Whenever available, declare variables
|
||
and parameters using a more general type (e.g, <code class="language-plaintext highlighter-rouge">Set</code>).</p>
|
||
|
||
<p>This rule reports uses of concrete collection types. User-defined types that should be treated
|
||
the same as interfaces can be configured with the property <code class="language-plaintext highlighter-rouge">allowedTypes</code>.</p>
|
||
|
||
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/LooseCouplingRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.LooseCouplingRule</a></p>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">import</span> <span class="nn">java.util.ArrayList</span><span class="o">;</span>
|
||
<span class="kn">import</span> <span class="nn">java.util.HashSet</span><span class="o">;</span>
|
||
|
||
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">Bar</span> <span class="o">{</span>
|
||
<span class="c1">// sub-optimal approach</span>
|
||
<span class="kd">private</span> <span class="nc">ArrayList</span><span class="o"><</span><span class="nc">SomeType</span><span class="o">></span> <span class="n">list</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">ArrayList</span><span class="o"><>();</span>
|
||
|
||
<span class="kd">public</span> <span class="nc">HashSet</span><span class="o"><</span><span class="nc">SomeType</span><span class="o">></span> <span class="nf">getFoo</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="k">return</span> <span class="k">new</span> <span class="nc">HashSet</span><span class="o"><</span><span class="nc">SomeType</span><span class="o">>();</span>
|
||
<span class="o">}</span>
|
||
|
||
<span class="c1">// preferred approach</span>
|
||
<span class="kd">private</span> <span class="nc">List</span><span class="o"><</span><span class="nc">SomeType</span><span class="o">></span> <span class="n">list</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">ArrayList</span><span class="o"><>();</span>
|
||
|
||
<span class="kd">public</span> <span class="nc">Set</span><span class="o"><</span><span class="nc">SomeType</span><span class="o">></span> <span class="nf">getFoo</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="k">return</span> <span class="k">new</span> <span class="nc">HashSet</span><span class="o"><</span><span class="nc">SomeType</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>allowedTypes</td>
|
||
<td>java.util.Properties</td>
|
||
<td>Exceptions to the 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"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/LooseCoupling"</span> <span class="nt">/></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"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/LooseCoupling"</span><span class="nt">></span>
|
||
<span class="nt"><properties></span>
|
||
<span class="nt"><property</span> <span class="na">name=</span><span class="s">"allowedTypes"</span> <span class="na">value=</span><span class="s">"java.util.Properties"</span> <span class="nt">/></span>
|
||
<span class="nt"></properties></span>
|
||
<span class="nt"></rule></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="methodreturnsinternalarray">MethodReturnsInternalArray</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 2.2</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Exposing internal arrays to the caller violates object encapsulation since elements can be
|
||
removed or replaced outside of the object that owns it. It is safer to return a copy of the array.</p>
|
||
|
||
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/MethodReturnsInternalArrayRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.MethodReturnsInternalArrayRule</a></p>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">SecureSystem</span> <span class="o">{</span>
|
||
<span class="nc">UserData</span> <span class="o">[]</span> <span class="n">ud</span><span class="o">;</span>
|
||
<span class="kd">public</span> <span class="nc">UserData</span> <span class="o">[]</span> <span class="n">getUserData</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="c1">// Don't return directly the internal array, return a copy</span>
|
||
<span class="k">return</span> <span class="n">ud</span><span class="o">;</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule by referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/MethodReturnsInternalArray"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="missingoverride">MissingOverride</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 6.2.0</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p><strong>Minimum Language Version:</strong> Java 1.5</p>
|
||
|
||
<p>Annotating overridden methods with @Override ensures at compile time that
|
||
the method really overrides one, which helps refactoring and clarifies intent.</p>
|
||
|
||
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/MissingOverrideRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.MissingOverrideRule</a></p>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="kd">implements</span> <span class="nc">Runnable</span> <span class="o">{</span>
|
||
<span class="c1">// This method is overridden, and should have an @Override annotation</span>
|
||
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">run</span><span class="o">()</span> <span class="o">{</span>
|
||
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule by referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/MissingOverride"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="onedeclarationperline">OneDeclarationPerLine</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 5.0</p>
|
||
|
||
<p><strong>Priority:</strong> Medium Low (4)</p>
|
||
|
||
<p>Java allows the use of several variables declaration of the same type on one line.
|
||
However, it can lead to quite messy code. This rule looks for several declarations on the same line.</p>
|
||
|
||
<p><strong>This rule is defined by the following XPath expression:</strong></p>
|
||
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">LocalVariableDeclaration</span><span class="w">
|
||
</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="kn">parent::</span><span class="nt">ForInit</span><span class="p">)]</span><span class="w">
|
||
</span><span class="p">[</span><span class="nf">count</span><span class="p">(</span><span class="nt">VariableDeclarator</span><span class="p">)</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="m">1</span><span class="p">]</span><span class="w">
|
||
</span><span class="p">[</span><span class="nv">$strictMode</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="nf">count</span><span class="p">(</span><span class="nf">distinct-values</span><span class="p">(</span><span class="nt">VariableDeclarator</span><span class="err">/</span><span class="na">@BeginLine</span><span class="p">))</span><span class="w"> </span><span class="p">!</span><span class="err">=</span><span class="w"> </span><span class="nf">count</span><span class="p">(</span><span class="nt">VariableDeclarator</span><span class="p">)]</span><span class="w">
|
||
</span><span class="p">|</span><span class="w">
|
||
</span><span class="o">//</span><span class="nt">FieldDeclaration</span><span class="w">
|
||
</span><span class="p">[</span><span class="nf">count</span><span class="p">(</span><span class="nt">VariableDeclarator</span><span class="p">)</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="m">1</span><span class="p">]</span><span class="w">
|
||
</span><span class="p">[</span><span class="nv">$strictMode</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="nf">count</span><span class="p">(</span><span class="nf">distinct-values</span><span class="p">(</span><span class="nt">VariableDeclarator</span><span class="err">/</span><span class="na">@BeginLine</span><span class="p">))</span><span class="w"> </span><span class="p">!</span><span class="err">=</span><span class="w"> </span><span class="nf">count</span><span class="p">(</span><span class="nt">VariableDeclarator</span><span class="p">)]</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nc">String</span> <span class="n">name</span><span class="o">;</span> <span class="c1">// separate declarations</span>
|
||
<span class="nc">String</span> <span class="n">lastname</span><span class="o">;</span>
|
||
|
||
<span class="nc">String</span> <span class="n">name</span><span class="o">,</span> <span class="n">lastname</span><span class="o">;</span> <span class="c1">// combined declaration, a violation</span>
|
||
|
||
<span class="nc">String</span> <span class="n">name</span><span class="o">,</span>
|
||
<span class="n">lastname</span><span class="o">;</span> <span class="c1">// combined declaration on multiple lines, no violation by default.</span>
|
||
<span class="c1">// Set property strictMode to true to mark this as violation.</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>This rule has the following properties:</strong></p>
|
||
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th>Name</th>
|
||
<th>Default Value</th>
|
||
<th>Description</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td>strictMode</td>
|
||
<td>false</td>
|
||
<td>If true, mark combined declaration even if the declarations are on separate lines.</td>
|
||
</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"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/OneDeclarationPerLine"</span> <span class="nt">/></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"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/OneDeclarationPerLine"</span><span class="nt">></span>
|
||
<span class="nt"><properties></span>
|
||
<span class="nt"><property</span> <span class="na">name=</span><span class="s">"strictMode"</span> <span class="na">value=</span><span class="s">"false"</span> <span class="nt">/></span>
|
||
<span class="nt"></properties></span>
|
||
<span class="nt"></rule></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="preservestacktrace">PreserveStackTrace</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 3.7</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Reports exceptions that are thrown from within a catch block, yet don’t refer to the
|
||
exception parameter declared by that catch block. The stack trace of the original
|
||
exception could be lost, which makes the thrown exception less informative.</p>
|
||
|
||
<p>To preserve the stack trace, the original exception may be used as the cause of
|
||
the new exception, using <code class="language-plaintext highlighter-rouge">Throwable#initCause</code>, or passed as a constructor argument
|
||
to the new exception. It may also be preserved using <code class="language-plaintext highlighter-rouge">Throwable#addSuppressed</code>.
|
||
The rule actually assumes that any method or constructor that takes the original
|
||
exception as argument preserves the original stack trace.</p>
|
||
|
||
<p>The rule allows <code class="language-plaintext highlighter-rouge">InvocationTargetException</code> and <code class="language-plaintext highlighter-rouge">PrivilegedActionException</code> to be
|
||
replaced by their cause exception. The discarded part of the stack trace is in those
|
||
cases only JDK-internal code, which is not very useful. The rule also ignores exceptions
|
||
whose name starts with <code class="language-plaintext highlighter-rouge">ignored</code>.</p>
|
||
|
||
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/PreserveStackTraceRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.PreserveStackTraceRule</a></p>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
|
||
<span class="kt">void</span> <span class="nf">good</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="k">try</span><span class="o">{</span>
|
||
<span class="nc">Integer</span><span class="o">.</span><span class="na">parseInt</span><span class="o">(</span><span class="s">"a"</span><span class="o">);</span>
|
||
<span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="nc">Exception</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
|
||
<span class="k">throw</span> <span class="k">new</span> <span class="nf">Exception</span><span class="o">(</span><span class="n">e</span><span class="o">);</span> <span class="c1">// Ok, this initializes the cause of the new exception</span>
|
||
<span class="o">}</span>
|
||
<span class="k">try</span> <span class="o">{</span>
|
||
<span class="nc">Integer</span><span class="o">.</span><span class="na">parseInt</span><span class="o">(</span><span class="s">"a"</span><span class="o">);</span>
|
||
<span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="nc">Exception</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
|
||
<span class="k">throw</span> <span class="o">(</span><span class="nc">IllegalStateException</span><span class="o">)</span><span class="k">new</span> <span class="nc">IllegalStateException</span><span class="o">().</span><span class="na">initCause</span><span class="o">(</span><span class="n">e</span><span class="o">);</span> <span class="c1">// second possibility to create exception chain.</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
<span class="kt">void</span> <span class="nf">wrong</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="k">try</span><span class="o">{</span>
|
||
<span class="nc">Integer</span><span class="o">.</span><span class="na">parseInt</span><span class="o">(</span><span class="s">"a"</span><span class="o">);</span>
|
||
<span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="nc">Exception</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
|
||
<span class="c1">// Violation: this only preserves the message and not the stack trace</span>
|
||
<span class="k">throw</span> <span class="k">new</span> <span class="nf">Exception</span><span class="o">(</span><span class="n">e</span><span class="o">.</span><span class="na">getMessage</span><span class="o">());</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule by referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/PreserveStackTrace"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="primitivewrapperinstantiation">PrimitiveWrapperInstantiation</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 6.37.0</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Reports usages of primitive wrapper constructors. They are deprecated
|
||
since Java 9 and should not be used. Even before Java 9, they can
|
||
be replaced with usage of the corresponding static <code class="language-plaintext highlighter-rouge">valueOf</code> factory method
|
||
(which may be automatically inserted by the compiler since Java 1.5).
|
||
This has the advantage that it may reuse common instances instead of creating
|
||
a new instance each time.</p>
|
||
|
||
<p>Note that for <code class="language-plaintext highlighter-rouge">Boolean</code>, the named constants <code class="language-plaintext highlighter-rouge">Boolean.TRUE</code> and <code class="language-plaintext highlighter-rouge">Boolean.FALSE</code>
|
||
are preferred instead of <code class="language-plaintext highlighter-rouge">Boolean.valueOf</code>.</p>
|
||
|
||
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/PrimitiveWrapperInstantiationRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.PrimitiveWrapperInstantiationRule</a></p>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
|
||
<span class="kd">private</span> <span class="nc">Integer</span> <span class="no">ZERO</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">Integer</span><span class="o">(</span><span class="mi">0</span><span class="o">);</span> <span class="c1">// violation</span>
|
||
<span class="kd">private</span> <span class="nc">Integer</span> <span class="no">ZERO1</span> <span class="o">=</span> <span class="nc">Integer</span><span class="o">.</span><span class="na">valueOf</span><span class="o">(</span><span class="mi">0</span><span class="o">);</span> <span class="c1">// better</span>
|
||
<span class="kd">private</span> <span class="nc">Integer</span> <span class="no">ZERO1</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span> <span class="c1">// even better</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule by referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/PrimitiveWrapperInstantiation"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="replaceenumerationwithiterator">ReplaceEnumerationWithIterator</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 3.4</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Consider replacing Enumeration usages with the newer java.util.Iterator</p>
|
||
|
||
<p><strong>This rule is defined by the following XPath expression:</strong></p>
|
||
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">ImplementsList</span><span class="o">/</span><span class="nt">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.util.Enumeration'</span><span class="p">)]</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="kd">implements</span> <span class="nc">Enumeration</span> <span class="o">{</span>
|
||
<span class="kd">private</span> <span class="kt">int</span> <span class="n">x</span> <span class="o">=</span> <span class="mi">42</span><span class="o">;</span>
|
||
<span class="kd">public</span> <span class="kt">boolean</span> <span class="nf">hasMoreElements</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="k">return</span> <span class="kc">true</span><span class="o">;</span>
|
||
<span class="o">}</span>
|
||
<span class="kd">public</span> <span class="nc">Object</span> <span class="nf">nextElement</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="k">return</span> <span class="nc">String</span><span class="o">.</span><span class="na">valueOf</span><span class="o">(</span><span class="n">i</span><span class="o">++);</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule by referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/ReplaceEnumerationWithIterator"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="replacehashtablewithmap">ReplaceHashtableWithMap</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 3.4</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Consider replacing Hashtable usage with the newer java.util.Map if thread safety is not required.</p>
|
||
|
||
<p><strong>This rule is defined by the following XPath expression:</strong></p>
|
||
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">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.util.Hashtable'</span><span class="p">)]</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
|
||
<span class="kt">void</span> <span class="nf">bar</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="nc">Hashtable</span> <span class="n">h</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">Hashtable</span><span class="o">();</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule by referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/ReplaceHashtableWithMap"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="replacevectorwithlist">ReplaceVectorWithList</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 3.4</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Consider replacing Vector usages with the newer java.util.ArrayList if expensive thread-safe operations are not required.</p>
|
||
|
||
<p><strong>This rule is defined by the following XPath expression:</strong></p>
|
||
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">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.util.Vector'</span><span class="p">)]</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">import</span> <span class="nn">java.util.Vector</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="kt">void</span> <span class="nf">bar</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="nc">Vector</span> <span class="n">v</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">Vector</span><span class="o">();</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule by referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/ReplaceVectorWithList"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="simplifiabletestassertion">SimplifiableTestAssertion</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 6.37.0</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Reports test assertions that may be simplified using a more specific
|
||
assertion method. This enables better error messages, and makes the
|
||
assertions more readable.</p>
|
||
|
||
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/SimplifiableTestAssertionRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.SimplifiableTestAssertionRule</a></p>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">import</span> <span class="nn">org.junit.Test</span><span class="o">;</span>
|
||
<span class="kn">import</span> <span class="nn">static</span> <span class="n">org</span><span class="o">.</span><span class="na">junit</span><span class="o">.</span><span class="na">Assert</span><span class="o">.*;</span>
|
||
|
||
<span class="kd">class</span> <span class="nc">SomeTestClass</span> <span class="o">{</span>
|
||
<span class="nc">Object</span> <span class="n">a</span><span class="o">,</span><span class="n">b</span><span class="o">;</span>
|
||
<span class="nd">@Test</span>
|
||
<span class="kt">void</span> <span class="nf">testMethod</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="n">assertTrue</span><span class="o">(</span><span class="n">a</span><span class="o">.</span><span class="na">equals</span><span class="o">(</span><span class="n">b</span><span class="o">));</span> <span class="c1">// could be assertEquals(a, b);</span>
|
||
<span class="n">assertTrue</span><span class="o">(!</span><span class="n">a</span><span class="o">.</span><span class="na">equals</span><span class="o">(</span><span class="n">b</span><span class="o">));</span> <span class="c1">// could be assertNotEquals(a, b);</span>
|
||
|
||
<span class="n">assertTrue</span><span class="o">(!</span><span class="n">something</span><span class="o">);</span> <span class="c1">// could be assertFalse(something);</span>
|
||
<span class="n">assertFalse</span><span class="o">(!</span><span class="n">something</span><span class="o">);</span> <span class="c1">// could be assertTrue(something);</span>
|
||
|
||
<span class="n">assertTrue</span><span class="o">(</span><span class="n">a</span> <span class="o">==</span> <span class="n">b</span><span class="o">);</span> <span class="c1">// could be assertSame(a, b);</span>
|
||
<span class="n">assertTrue</span><span class="o">(</span><span class="n">a</span> <span class="o">!=</span> <span class="n">b</span><span class="o">);</span> <span class="c1">// could be assertNotSame(a, b);</span>
|
||
|
||
<span class="n">assertTrue</span><span class="o">(</span><span class="n">a</span> <span class="o">==</span> <span class="kc">null</span><span class="o">);</span> <span class="c1">// could be assertNull(a);</span>
|
||
<span class="n">assertTrue</span><span class="o">(</span><span class="n">a</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">);</span> <span class="c1">// could be assertNotNull(a);</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule by referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/SimplifiableTestAssertion"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="switchstmtsshouldhavedefault">SwitchStmtsShouldHaveDefault</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 1.0</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Switch statements should be exhaustive, to make their control flow
|
||
easier to follow. This can be achieved by adding a <code class="language-plaintext highlighter-rouge">default</code> case, or,
|
||
if the switch is on an enum type, by ensuring there is one switch branch
|
||
for each enum constant.</p>
|
||
|
||
<p><strong>This rule is defined by the following XPath expression:</strong></p>
|
||
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">SwitchStatement</span><span class="p">[</span><span class="na">@DefaultCase</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="nf">false</span><span class="p">()</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="na">@ExhaustiveEnumSwitch</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="nf">false</span><span class="p">()]</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{{</span>
|
||
<span class="kt">int</span> <span class="n">x</span> <span class="o">=</span> <span class="mi">2</span><span class="o">;</span>
|
||
<span class="k">switch</span> <span class="o">(</span><span class="n">x</span><span class="o">)</span> <span class="o">{</span>
|
||
<span class="k">case</span> <span class="mi">1</span><span class="o">:</span> <span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">6</span><span class="o">;</span>
|
||
<span class="k">case</span> <span class="mi">2</span><span class="o">:</span> <span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">8</span><span class="o">;</span>
|
||
<span class="c1">// missing default: here</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule by referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/SwitchStmtsShouldHaveDefault"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="systemprintln">SystemPrintln</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 2.1</p>
|
||
|
||
<p><strong>Priority:</strong> Medium High (2)</p>
|
||
|
||
<p>References to System.(out|err).print are usually intended for debugging purposes and can remain in
|
||
the codebase even in production code. By using a logger one can enable/disable this behaviour at
|
||
will (and by priority) and avoid clogging the Standard out log.</p>
|
||
|
||
<p><strong>This rule is defined by the following XPath expression:</strong></p>
|
||
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">MethodCall</span><span class="p">[</span><span class="w"> </span><span class="nf">starts-with</span><span class="p">(</span><span class="na">@MethodName</span><span class="p">,</span><span class="w"> </span><span class="s1">'print'</span><span class="p">)</span><span class="w"> </span><span class="p">]</span><span class="w">
|
||
</span><span class="o">/</span><span class="nt">FieldAccess</span><span class="p">[</span><span class="w"> </span><span class="na">@Name</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="p">(</span><span class="s1">'err'</span><span class="p">,</span><span class="w"> </span><span class="s1">'out'</span><span class="p">)</span><span class="w"> </span><span class="p">]</span><span class="w">
|
||
</span><span class="o">/</span><span class="nt">TypeExpression</span><span class="p">[</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIsExactly</span><span class="p">(</span><span class="s1">'java.lang.System'</span><span class="p">)</span><span class="w"> </span><span class="p">]</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">class</span> <span class="nc">Foo</span><span class="o">{</span>
|
||
<span class="nc">Logger</span> <span class="n">log</span> <span class="o">=</span> <span class="nc">Logger</span><span class="o">.</span><span class="na">getLogger</span><span class="o">(</span><span class="nc">Foo</span><span class="o">.</span><span class="na">class</span><span class="o">.</span><span class="na">getName</span><span class="o">());</span>
|
||
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">testA</span> <span class="o">()</span> <span class="o">{</span>
|
||
<span class="nc">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">"Entering test"</span><span class="o">);</span>
|
||
<span class="c1">// Better use this</span>
|
||
<span class="n">log</span><span class="o">.</span><span class="na">fine</span><span class="o">(</span><span class="s">"Entering test"</span><span class="o">);</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule by referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/SystemPrintln"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="unnecessaryvarargsarraycreation">UnnecessaryVarargsArrayCreation</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 7.1.0</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Reports explicit array creation when a varargs is expected.
|
||
For instance:</p>
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nc">Arrays</span><span class="o">.</span><span class="na">asList</span><span class="o">(</span><span class="k">new</span> <span class="nc">String</span><span class="o">[]</span> <span class="o">{</span> <span class="s">"foo"</span><span class="o">,</span> <span class="s">"bar"</span><span class="o">,</span> <span class="o">});</span>
|
||
</code></pre></div></div>
|
||
<p>can be replaced by:</p>
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nc">Arrays</span><span class="o">.</span><span class="na">asList</span><span class="o">(</span><span class="s">"foo"</span><span class="o">,</span> <span class="s">"bar"</span><span class="o">);</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UnnecessaryVarargsArrayCreationRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.UnnecessaryVarargsArrayCreationRule</a></p>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">import</span> <span class="nn">java.util.Arrays</span><span class="o">;</span>
|
||
|
||
<span class="kd">class</span> <span class="nc">C</span> <span class="o">{</span>
|
||
<span class="kd">static</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="k">new</span> <span class="nc">String</span><span class="o">[]{</span><span class="s">"foo"</span><span class="o">,</span> <span class="s">"bar"</span><span class="o">,});</span>
|
||
<span class="c1">// should be</span>
|
||
<span class="nc">Arrays</span><span class="o">.</span><span class="na">asList</span><span class="o">(</span><span class="s">"foo"</span><span class="o">,</span> <span class="s">"bar"</span><span class="o">);</span>
|
||
<span class="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"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/UnnecessaryVarargsArrayCreation"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="unusedassignment">UnusedAssignment</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 6.26.0</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Reports assignments to variables that are never used before the variable is overwritten,
|
||
or goes out of scope. Unused assignments are those for which</p>
|
||
<ol>
|
||
<li>The variable is never read after the assignment, or</li>
|
||
<li>The assigned value is always overwritten by other assignments before the next read of
|
||
the variable.</li>
|
||
</ol>
|
||
|
||
<p>The rule tracks assignements to fields of <code class="language-plaintext highlighter-rouge">this</code>, and static fields of the current class.
|
||
This may cause some false positives in timing-sensitive concurrent code, which the rule cannot detect.</p>
|
||
|
||
<p>The rule may be suppressed with the standard <code class="language-plaintext highlighter-rouge">@SuppressWarnings("unused")</code> tag.</p>
|
||
|
||
<p>The rule subsumes <a href="pmd_rules_java_bestpractices.html#unusedlocalvariable"><code class="language-plaintext highlighter-rouge">UnusedLocalVariable</code></a>, and <a href="pmd_rules_java_bestpractices.html#unusedformalparameter"><code class="language-plaintext highlighter-rouge">UnusedFormalParameter</code></a>.
|
||
Those violations are filtered
|
||
out by default, in case you already have enabled those rules, but may be enabled with the property
|
||
<code class="language-plaintext highlighter-rouge">reportUnusedVariables</code>. Variables whose name starts with <code class="language-plaintext highlighter-rouge">ignored</code> or <code class="language-plaintext highlighter-rouge">unused</code> are filtered out, as
|
||
is standard practice for exceptions.</p>
|
||
|
||
<p>Limitations:</p>
|
||
<ul>
|
||
<li>The rule currently cannot know which method calls throw exceptions, or which exceptions they throw.
|
||
In the body of a try block, every method or constructor call is assumed to throw. This may cause false-negatives.
|
||
The only other language construct that is assumed to throw is the <code class="language-plaintext highlighter-rouge">throw</code> statement, in particular,
|
||
things like <code class="language-plaintext highlighter-rouge">assert</code> statements, or NullPointerExceptions on dereference are ignored.</li>
|
||
<li>The rule cannot resolve assignments across constructors, when they’re called with the special
|
||
<code class="language-plaintext highlighter-rouge">this(...)</code> syntax. This may cause false-negatives.</li>
|
||
</ul>
|
||
|
||
<p>Both of those limitations may be partly relaxed in PMD 7.</p>
|
||
|
||
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UnusedAssignmentRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.UnusedAssignmentRule</a></p>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">class</span> <span class="nc">A</span> <span class="o">{</span>
|
||
<span class="c1">// this field initializer is redundant,</span>
|
||
<span class="c1">// it is always overwritten in the constructor</span>
|
||
<span class="kt">int</span> <span class="n">f</span> <span class="o">=</span> <span class="mi">1</span><span class="o">;</span>
|
||
|
||
<span class="no">A</span><span class="o">(</span><span class="kt">int</span> <span class="n">f</span><span class="o">)</span> <span class="o">{</span>
|
||
<span class="k">this</span><span class="o">.</span><span class="na">f</span> <span class="o">=</span> <span class="n">f</span><span class="o">;</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">class</span> <span class="nc">B</span> <span class="o">{</span>
|
||
|
||
<span class="kt">int</span> <span class="nf">method</span><span class="o">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">,</span> <span class="kt">int</span> <span class="n">j</span><span class="o">)</span> <span class="o">{</span>
|
||
<span class="c1">// this initializer is redundant,</span>
|
||
<span class="c1">// it is overwritten in all branches of the `if`</span>
|
||
<span class="kt">int</span> <span class="n">k</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span>
|
||
|
||
<span class="c1">// Both the assignments to k are unused, because k is</span>
|
||
<span class="c1">// not read after the if/else</span>
|
||
<span class="c1">// This may hide a bug: the programmer probably wanted to return k</span>
|
||
<span class="k">if</span> <span class="o">(</span><span class="n">i</span> <span class="o"><</span> <span class="n">j</span><span class="o">)</span>
|
||
<span class="n">k</span> <span class="o">=</span> <span class="n">i</span><span class="o">;</span>
|
||
<span class="k">else</span>
|
||
<span class="n">k</span> <span class="o">=</span> <span class="n">j</span><span class="o">;</span>
|
||
|
||
<span class="k">return</span> <span class="n">j</span><span class="o">;</span>
|
||
<span class="o">}</span>
|
||
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">class</span> <span class="nc">C</span> <span class="o">{</span>
|
||
|
||
<span class="kt">int</span> <span class="nf">method</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span>
|
||
|
||
<span class="n">checkSomething</span><span class="o">(++</span><span class="n">i</span><span class="o">);</span>
|
||
<span class="n">checkSomething</span><span class="o">(++</span><span class="n">i</span><span class="o">);</span>
|
||
<span class="n">checkSomething</span><span class="o">(++</span><span class="n">i</span><span class="o">);</span>
|
||
<span class="n">checkSomething</span><span class="o">(++</span><span class="n">i</span><span class="o">);</span>
|
||
|
||
<span class="c1">// That last increment is not reported unless</span>
|
||
<span class="c1">// the property `checkUnusedPrefixIncrement` is</span>
|
||
<span class="c1">// set to `true`</span>
|
||
<span class="c1">// Technically it could be written (i+1), but it</span>
|
||
<span class="c1">// is not very important</span>
|
||
<span class="o">}</span>
|
||
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">class</span> <span class="nc">C</span> <span class="o">{</span>
|
||
|
||
<span class="c1">// variables that are truly unused (at most assigned to, but never accessed)</span>
|
||
<span class="c1">// are only reported if property `reportUnusedVariables` is true</span>
|
||
|
||
<span class="kt">void</span> <span class="nf">method</span><span class="o">(</span><span class="kt">int</span> <span class="n">param</span><span class="o">)</span> <span class="o">{</span> <span class="o">}</span> <span class="c1">// for example this method parameter</span>
|
||
|
||
<span class="c1">// even then, you can suppress the violation with an annotation:</span>
|
||
|
||
<span class="kt">void</span> <span class="nf">method</span><span class="o">(</span><span class="nd">@SuppressWarning</span><span class="o">(</span><span class="s">"unused"</span><span class="o">)</span> <span class="kt">int</span> <span class="n">param</span><span class="o">)</span> <span class="o">{</span> <span class="o">}</span> <span class="c1">// no violation, even if `reportUnusedVariables` is true</span>
|
||
|
||
<span class="c1">// For catch parameters, or for resources which don't need to be used explicitly,</span>
|
||
<span class="c1">// you can give a name that starts with "ignored" to ignore such warnings</span>
|
||
|
||
<span class="o">{</span>
|
||
<span class="k">try</span> <span class="o">(</span><span class="nc">Something</span> <span class="n">ignored</span> <span class="o">=</span> <span class="nc">Something</span><span class="o">.</span><span class="na">create</span><span class="o">())</span> <span class="o">{</span>
|
||
<span class="c1">// even if ignored is unused, it won't be flagged</span>
|
||
<span class="c1">// its purpose might be to side-effect in the create/close routines</span>
|
||
|
||
<span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="nc">Exception</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span> <span class="c1">// this is unused and will cause a warning if `reportUnusedVariables` is true</span>
|
||
<span class="c1">// you should choose a name that starts with "ignored"</span>
|
||
<span class="k">return</span><span class="o">;</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>This rule has the following properties:</strong></p>
|
||
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th>Name</th>
|
||
<th>Default Value</th>
|
||
<th>Description</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td>checkUnusedPrefixIncrement</td>
|
||
<td>false</td>
|
||
<td>Report expressions like ++i that may be replaced with (i + 1)</td>
|
||
</tr>
|
||
<tr>
|
||
<td>reportUnusedVariables</td>
|
||
<td>false</td>
|
||
<td>Report variables that are only initialized, and never read at all. The rule UnusedVariable already cares for that, but you can enable it if needed</td>
|
||
</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"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/UnusedAssignment"</span> <span class="nt">/></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"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/UnusedAssignment"</span><span class="nt">></span>
|
||
<span class="nt"><properties></span>
|
||
<span class="nt"><property</span> <span class="na">name=</span><span class="s">"checkUnusedPrefixIncrement"</span> <span class="na">value=</span><span class="s">"false"</span> <span class="nt">/></span>
|
||
<span class="nt"><property</span> <span class="na">name=</span><span class="s">"reportUnusedVariables"</span> <span class="na">value=</span><span class="s">"false"</span> <span class="nt">/></span>
|
||
<span class="nt"></properties></span>
|
||
<span class="nt"></rule></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="unusedformalparameter">UnusedFormalParameter</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 0.8</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Reports parameters of methods and constructors that are not referenced them in the method body.
|
||
Parameters whose name starts with <code class="language-plaintext highlighter-rouge">ignored</code> or <code class="language-plaintext highlighter-rouge">unused</code> are filtered out.</p>
|
||
|
||
<p>Removing unused formal parameters from public methods could cause a ripple effect through the code base.
|
||
Hence, by default, this rule only considers private methods. To include non-private methods, set the
|
||
<code class="language-plaintext highlighter-rouge">checkAll</code> property to <code class="language-plaintext highlighter-rouge">true</code>.</p>
|
||
|
||
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UnusedFormalParameterRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.UnusedFormalParameterRule</a></p>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
|
||
<span class="kd">private</span> <span class="kt">void</span> <span class="nf">bar</span><span class="o">(</span><span class="nc">String</span> <span class="n">howdy</span><span class="o">)</span> <span class="o">{</span>
|
||
<span class="c1">// howdy is not used</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>This rule has the following properties:</strong></p>
|
||
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th>Name</th>
|
||
<th>Default Value</th>
|
||
<th>Description</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td>checkAll</td>
|
||
<td>false</td>
|
||
<td>Check all methods, including non-private ones</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"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/UnusedFormalParameter"</span> <span class="nt">/></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"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/UnusedFormalParameter"</span><span class="nt">></span>
|
||
<span class="nt"><properties></span>
|
||
<span class="nt"><property</span> <span class="na">name=</span><span class="s">"checkAll"</span> <span class="na">value=</span><span class="s">"false"</span> <span class="nt">/></span>
|
||
<span class="nt"></properties></span>
|
||
<span class="nt"></rule></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="unusedlocalvariable">UnusedLocalVariable</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 0.1</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Detects when a local variable is declared and/or assigned, but not used.
|
||
Variables whose name starts with <code class="language-plaintext highlighter-rouge">ignored</code> or <code class="language-plaintext highlighter-rouge">unused</code> are filtered out.</p>
|
||
|
||
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UnusedLocalVariableRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.UnusedLocalVariableRule</a></p>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
|
||
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">doSomething</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">5</span><span class="o">;</span> <span class="c1">// Unused</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule by referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/UnusedLocalVariable"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="unusedprivatefield">UnusedPrivateField</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 0.1</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Detects when a private field is declared and/or assigned a value, but not used.</p>
|
||
|
||
<p>Since PMD 6.50.0 private fields are ignored, if the fields are annotated with any annotation or the
|
||
enclosing class has any annotation. Annotations often enable a framework (such as dependency injection, mocking
|
||
or e.g. Lombok) which use the fields by reflection or other means. This usage can’t be detected by static code analysis.
|
||
Previously these frameworks where explicitly allowed by listing their annotations in the property
|
||
"ignoredAnnotations", but that turned out to be prone of false positive for any not explicitly considered framework.</p>
|
||
|
||
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UnusedPrivateFieldRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.UnusedPrivateFieldRule</a></p>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Something</span> <span class="o">{</span>
|
||
<span class="kd">private</span> <span class="kd">static</span> <span class="kt">int</span> <span class="no">FOO</span> <span class="o">=</span> <span class="mi">2</span><span class="o">;</span> <span class="c1">// Unused</span>
|
||
<span class="kd">private</span> <span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">5</span><span class="o">;</span> <span class="c1">// Unused</span>
|
||
<span class="kd">private</span> <span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">6</span><span class="o">;</span>
|
||
<span class="kd">public</span> <span class="kt">int</span> <span class="nf">addOne</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="k">return</span> <span class="n">j</span><span class="o">++;</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>This rule has the following properties:</strong></p>
|
||
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th>Name</th>
|
||
<th>Default Value</th>
|
||
<th>Description</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td>ignoredFieldNames</td>
|
||
<td>serialVersionUID , serialPersistentFields</td>
|
||
<td>Field Names that are ignored from the unused check</td>
|
||
</tr>
|
||
<tr>
|
||
<td>reportForAnnotations</td>
|
||
<td> </td>
|
||
<td>Fully qualified names of the annotation types that should be reported anyway. If an unused field has any of these annotations, then it is reported. If it has any other annotation, then it is still considered to used and is not 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"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/UnusedPrivateField"</span> <span class="nt">/></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"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/UnusedPrivateField"</span><span class="nt">></span>
|
||
<span class="nt"><properties></span>
|
||
<span class="nt"><property</span> <span class="na">name=</span><span class="s">"ignoredFieldNames"</span> <span class="na">value=</span><span class="s">"serialVersionUID,serialPersistentFields"</span> <span class="nt">/></span>
|
||
<span class="nt"><property</span> <span class="na">name=</span><span class="s">"reportForAnnotations"</span> <span class="na">value=</span><span class="s">""</span> <span class="nt">/></span>
|
||
<span class="nt"></properties></span>
|
||
<span class="nt"></rule></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="unusedprivatemethod">UnusedPrivateMethod</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 0.7</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Unused Private Method detects when a private method is declared but is unused.</p>
|
||
|
||
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UnusedPrivateMethodRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.UnusedPrivateMethodRule</a></p>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Something</span> <span class="o">{</span>
|
||
<span class="kd">private</span> <span class="kt">void</span> <span class="nf">foo</span><span class="o">()</span> <span class="o">{}</span> <span class="c1">// unused</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>This rule has the following properties:</strong></p>
|
||
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th>Name</th>
|
||
<th>Default Value</th>
|
||
<th>Description</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td>ignoredAnnotations</td>
|
||
<td>java.lang.Deprecated , jakarta.annotation.PostConstruct , jakarta.annotation.PreDestroy</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"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/UnusedPrivateMethod"</span> <span class="nt">/></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"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/UnusedPrivateMethod"</span><span class="nt">></span>
|
||
<span class="nt"><properties></span>
|
||
<span class="nt"><property</span> <span class="na">name=</span><span class="s">"ignoredAnnotations"</span> <span class="na">value=</span><span class="s">"java.lang.Deprecated,jakarta.annotation.PostConstruct,jakarta.annotation.PreDestroy"</span> <span class="nt">/></span>
|
||
<span class="nt"></properties></span>
|
||
<span class="nt"></rule></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="usecollectionisempty">UseCollectionIsEmpty</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 3.9</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>The isEmpty() method on java.util.Collection is provided to determine if a collection has any elements.
|
||
Comparing the value of size() to 0 does not convey intent as well as the isEmpty() method.</p>
|
||
|
||
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UseCollectionIsEmptyRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.UseCollectionIsEmptyRule</a></p>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
|
||
<span class="kt">void</span> <span class="nf">good</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="nc">List</span> <span class="n">foo</span> <span class="o">=</span> <span class="n">getList</span><span class="o">();</span>
|
||
<span class="k">if</span> <span class="o">(</span><span class="n">foo</span><span class="o">.</span><span class="na">isEmpty</span><span class="o">())</span> <span class="o">{</span>
|
||
<span class="c1">// blah</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
|
||
<span class="kt">void</span> <span class="nf">bad</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="nc">List</span> <span class="n">foo</span> <span class="o">=</span> <span class="n">getList</span><span class="o">();</span>
|
||
<span class="k">if</span> <span class="o">(</span><span class="n">foo</span><span class="o">.</span><span class="na">size</span><span class="o">()</span> <span class="o">==</span> <span class="mi">0</span><span class="o">)</span> <span class="o">{</span>
|
||
<span class="c1">// blah</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule by referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/UseCollectionIsEmpty"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="useenumcollections">UseEnumCollections</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 7.3.0</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Wherever possible, use <code class="language-plaintext highlighter-rouge">EnumSet</code> or <code class="language-plaintext highlighter-rouge">EnumMap</code> instead of <code class="language-plaintext highlighter-rouge">HashSet</code> and <code class="language-plaintext highlighter-rouge">HashMap</code> when the keys
|
||
are of an enum type. The specialized enum collections are more space- and time-efficient.
|
||
This rule reports constructor expressions for hash sets or maps whose key
|
||
type is an enum type.</p>
|
||
|
||
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UseEnumCollectionsRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.UseEnumCollectionsRule</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.EnumMap</span><span class="o">;</span>
|
||
<span class="kn">import</span> <span class="nn">java.util.HashSet</span><span class="o">;</span>
|
||
|
||
<span class="kd">enum</span> <span class="nc">Example</span> <span class="o">{</span>
|
||
<span class="no">A</span><span class="o">,</span> <span class="no">B</span><span class="o">,</span> <span class="no">C</span><span class="o">;</span>
|
||
|
||
<span class="kd">public</span> <span class="kd">static</span> <span class="nc">Set</span><span class="o"><</span><span class="nc">Example</span><span class="o">></span> <span class="nf">newSet</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="k">return</span> <span class="k">new</span> <span class="nc">HashSet</span><span class="o"><>();</span> <span class="c1">// Could be EnumSet.noneOf(Example.class)</span>
|
||
<span class="o">}</span>
|
||
|
||
<span class="kd">public</span> <span class="kd">static</span> <span class="o"><</span><span class="no">V</span><span class="o">></span> <span class="nc">Map</span><span class="o"><</span><span class="nc">Example</span><span class="o">,</span> <span class="no">V</span><span class="o">></span> <span class="nf">newMap</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="k">return</span> <span class="k">new</span> <span class="nc">HashMap</span><span class="o"><>();</span> <span class="c1">// Could be new EnumMap<>(Example.class)</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"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/UseEnumCollections"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="usestandardcharsets">UseStandardCharsets</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 6.34.0</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p><strong>Minimum Language Version:</strong> Java 1.7</p>
|
||
|
||
<p>Starting with Java 7, StandardCharsets provides constants for common Charset objects, such as UTF-8.
|
||
Using the constants is less error prone, and can provide a small performance advantage compared to <code class="language-plaintext highlighter-rouge">Charset.forName(...)</code>
|
||
since no scan across the internal <code class="language-plaintext highlighter-rouge">Charset</code> caches is needed.</p>
|
||
|
||
<p><strong>This rule is defined by the following XPath expression:</strong></p>
|
||
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">MethodCall</span><span class="p">[</span><span class="na">@MethodName</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'forName'</span><span class="p">][</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'java.nio.charset.Charset'</span><span class="p">)]</span><span class="w">
|
||
</span><span class="p">[</span><span class="w">
|
||
</span><span class="nt">ArgumentList</span><span class="o">/</span><span class="nt">StringLiteral</span><span class="w">
|
||
</span><span class="p">[</span><span class="na">@Image</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="p">(</span><span class="s1">'"US-ASCII"'</span><span class="p">,</span><span class="w"> </span><span class="s1">'"ISO-8859-1"'</span><span class="p">,</span><span class="w"> </span><span class="s1">'"UTF-8"'</span><span class="p">,</span><span class="w"> </span><span class="s1">'"UTF-16BE"'</span><span class="p">,</span><span class="w"> </span><span class="s1">'"UTF-16LE"'</span><span class="p">,</span><span class="w"> </span><span class="s1">'"UTF-16"'</span><span class="p">)]</span><span class="w">
|
||
</span><span class="p">]</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">UseStandardCharsets</span> <span class="o">{</span>
|
||
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">run</span><span class="o">()</span> <span class="o">{</span>
|
||
|
||
<span class="c1">// looking up the charset dynamically</span>
|
||
<span class="k">try</span> <span class="o">(</span><span class="nc">OutputStreamWriter</span> <span class="n">osw</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">OutputStreamWriter</span><span class="o">(</span><span class="n">out</span><span class="o">,</span> <span class="nc">Charset</span><span class="o">.</span><span class="na">forName</span><span class="o">(</span><span class="s">"UTF-8"</span><span class="o">)))</span> <span class="o">{</span>
|
||
<span class="n">osw</span><span class="o">.</span><span class="na">write</span><span class="o">(</span><span class="s">"test"</span><span class="o">);</span>
|
||
<span class="o">}</span>
|
||
|
||
<span class="c1">// best to use StandardCharsets</span>
|
||
<span class="k">try</span> <span class="o">(</span><span class="nc">OutputStreamWriter</span> <span class="n">osw</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">OutputStreamWriter</span><span class="o">(</span><span class="n">out</span><span class="o">,</span> <span class="nc">StandardCharsets</span><span class="o">.</span><span class="na">UTF_8</span><span class="o">))</span> <span class="o">{</span>
|
||
<span class="n">osw</span><span class="o">.</span><span class="na">write</span><span class="o">(</span><span class="s">"test"</span><span class="o">);</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule by referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/UseStandardCharsets"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="usetrywithresources">UseTryWithResources</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 6.12.0</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p><strong>Minimum Language Version:</strong> Java 1.7</p>
|
||
|
||
<p>Java 7 introduced the try-with-resources statement. This statement ensures that each resource is closed at the end
|
||
of the statement. It avoids the need of explicitly closing the resources in a finally block. Additionally exceptions
|
||
are better handled: If an exception occurred both in the <code class="language-plaintext highlighter-rouge">try</code> block and <code class="language-plaintext highlighter-rouge">finally</code> block, then the exception from
|
||
the try block was suppressed. With the <code class="language-plaintext highlighter-rouge">try</code>-with-resources statement, the exception thrown from the try-block is
|
||
preserved.</p>
|
||
|
||
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UseTryWithResourcesRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.UseTryWithResourcesRule</a></p>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">TryWithResources</span> <span class="o">{</span>
|
||
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">run</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="nc">InputStream</span> <span class="n">in</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span>
|
||
<span class="k">try</span> <span class="o">{</span>
|
||
<span class="n">in</span> <span class="o">=</span> <span class="n">openInputStream</span><span class="o">();</span>
|
||
<span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="n">in</span><span class="o">.</span><span class="na">read</span><span class="o">();</span>
|
||
<span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="nc">IOException</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
|
||
<span class="n">e</span><span class="o">.</span><span class="na">printStackTrace</span><span class="o">();</span>
|
||
<span class="o">}</span> <span class="k">finally</span> <span class="o">{</span>
|
||
<span class="k">try</span> <span class="o">{</span>
|
||
<span class="k">if</span> <span class="o">(</span><span class="n">in</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">)</span> <span class="n">in</span><span class="o">.</span><span class="na">close</span><span class="o">();</span>
|
||
<span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="nc">IOException</span> <span class="n">ignored</span><span class="o">)</span> <span class="o">{</span>
|
||
<span class="c1">// ignored</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
|
||
<span class="c1">// better use try-with-resources</span>
|
||
<span class="k">try</span> <span class="o">(</span><span class="nc">InputStream</span> <span class="n">in2</span> <span class="o">=</span> <span class="n">openInputStream</span><span class="o">())</span> <span class="o">{</span>
|
||
<span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="n">in2</span><span class="o">.</span><span class="na">read</span><span class="o">();</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>This rule has the following properties:</strong></p>
|
||
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th>Name</th>
|
||
<th>Default Value</th>
|
||
<th>Description</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td>closeMethods</td>
|
||
<td>close , closeQuietly</td>
|
||
<td>Method names in finally block, which trigger 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"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/UseTryWithResources"</span> <span class="nt">/></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"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/UseTryWithResources"</span><span class="nt">></span>
|
||
<span class="nt"><properties></span>
|
||
<span class="nt"><property</span> <span class="na">name=</span><span class="s">"closeMethods"</span> <span class="na">value=</span><span class="s">"close,closeQuietly"</span> <span class="nt">/></span>
|
||
<span class="nt"></properties></span>
|
||
<span class="nt"></rule></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="usevarargs">UseVarargs</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 5.0</p>
|
||
|
||
<p><strong>Priority:</strong> Medium Low (4)</p>
|
||
|
||
<p><strong>Minimum Language Version:</strong> Java 1.5</p>
|
||
|
||
<p>Java 5 introduced the varargs parameter declaration for methods and constructors. This syntactic
|
||
sugar provides flexibility for users of these methods and constructors, allowing them to avoid
|
||
having to deal with the creation of an array.</p>
|
||
|
||
<p>Byte arrays in any method and String arrays in <code class="language-plaintext highlighter-rouge">public static void main(String[])</code> methods are ignored.</p>
|
||
|
||
<p><strong>This rule is defined by the following XPath expression:</strong></p>
|
||
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">FormalParameters</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="kn">parent::</span><span class="nt">MethodDeclaration</span><span class="p">[</span><span class="na">@Overridden</span><span class="err">=</span><span class="nf">true</span><span class="p">()</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="na">@MainMethod</span><span class="err">=</span><span class="nf">true</span><span class="p">()])]</span><span class="w">
|
||
</span><span class="o">/</span><span class="nt">FormalParameter</span><span class="p">[</span><span class="nf">position</span><span class="p">()</span><span class="err">=</span><span class="nf">last</span><span class="p">()]</span><span class="w">
|
||
</span><span class="p">[</span><span class="na">@Varargs</span><span class="err">=</span><span class="nf">false</span><span class="p">()]</span><span class="w">
|
||
</span><span class="p">[</span><span class="nt">ArrayType</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="nt">PrimitiveType</span><span class="p">[</span><span class="na">@Kind</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s2">"byte"</span><span class="p">]</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="nt">ClassType</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'java.lang.Byte'</span><span class="p">)])]</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nt">VariableId</span><span class="p">[</span><span class="nt">ArrayDimensions</span><span class="p">]</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="p">(</span><span class="nt">PrimitiveType</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="na">@Kind</span><span class="err">=</span><span class="s2">"byte"</span><span class="p">)]</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="nt">ClassType</span><span class="p">[</span><span class="ow">not</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.Byte'</span><span class="p">))])]</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
|
||
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">foo</span><span class="o">(</span><span class="nc">String</span> <span class="n">s</span><span class="o">,</span> <span class="nc">Object</span><span class="o">[]</span> <span class="n">args</span><span class="o">)</span> <span class="o">{</span>
|
||
<span class="c1">// Do something here...</span>
|
||
<span class="o">}</span>
|
||
|
||
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">bar</span><span class="o">(</span><span class="nc">String</span> <span class="n">s</span><span class="o">,</span> <span class="nc">Object</span><span class="o">...</span> <span class="n">args</span><span class="o">)</span> <span class="o">{</span>
|
||
<span class="c1">// Ahh, varargs tastes much better...</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule by referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/UseVarargs"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="whileloopwithliteralboolean">WhileLoopWithLiteralBoolean</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 6.13.0</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p><code class="language-plaintext highlighter-rouge">do {} while (true);</code> requires reading the end of the statement before it is
|
||
apparent that it loops forever, whereas <code class="language-plaintext highlighter-rouge">while (true) {}</code> is easier to understand.</p>
|
||
|
||
<p><code class="language-plaintext highlighter-rouge">do {} while (false);</code> is redundant, and if an inner variable scope is required,
|
||
a block <code class="language-plaintext highlighter-rouge">{}</code> is sufficient.</p>
|
||
|
||
<p><code class="language-plaintext highlighter-rouge">while (false) {}</code> will never execute the block and can be removed in its entirety.</p>
|
||
|
||
<p><strong>This rule is defined by the following XPath expression:</strong></p>
|
||
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c">(: while loops with single boolean literal 'false', maybe parenthesized :)</span><span class="w">
|
||
</span><span class="o">//</span><span class="nt">WhileStatement</span><span class="o">/</span><span class="nt">BooleanLiteral</span><span class="p">[</span><span class="na">@True</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="nf">false</span><span class="p">()]</span><span class="w">
|
||
</span><span class="p">|</span><span class="w">
|
||
</span><span class="c">(: do-while loops with single boolean literal ('false' or 'true'), maybe parenthesized :)</span><span class="w">
|
||
</span><span class="o">//</span><span class="nt">DoStatement</span><span class="o">/</span><span class="nt">BooleanLiteral</span><span class="w">
|
||
</span><span class="p">|</span><span class="w">
|
||
</span><span class="c">(: while loops with conditional or'ed boolean literals, maybe parenthesized :)</span><span class="w">
|
||
</span><span class="o">//</span><span class="nt">WhileStatement</span><span class="p">[(</span><span class="nt">InfixExpression</span><span class="p">[</span><span class="na">@Operator</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="p">(</span><span class="s1">'|'</span><span class="p">,</span><span class="w"> </span><span class="s1">'||'</span><span class="p">)])</span><span class="w">
|
||
</span><span class="c">(: no var access :)</span><span class="w">
|
||
</span><span class="p">[</span><span class="nf">count</span><span class="p">(</span><span class="nt">VariableAccess</span><span class="p">)</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="m">0</span><span class="p">]</span><span class="w">
|
||
</span><span class="c">(: at least one false literal :)</span><span class="w">
|
||
</span><span class="p">[</span><span class="nf">count</span><span class="p">(</span><span class="nt">BooleanLiteral</span><span class="p">[</span><span class="na">@True</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="nf">false</span><span class="p">()])</span><span class="w"> </span><span class="o">>=</span><span class="w"> </span><span class="m">1</span><span class="p">]]</span><span class="w">
|
||
</span><span class="p">|</span><span class="w">
|
||
</span><span class="c">(: while loops with conditional and'ed boolean literals, maybe parenthesized :)</span><span class="w">
|
||
</span><span class="o">//</span><span class="nt">WhileStatement</span><span class="p">[(</span><span class="nt">InfixExpression</span><span class="p">[</span><span class="na">@Operator</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="p">(</span><span class="s1">'&'</span><span class="p">,</span><span class="w"> </span><span class="s1">'&&'</span><span class="p">)])</span><span class="w">
|
||
</span><span class="c">(: at least one false literal :)</span><span class="w">
|
||
</span><span class="p">[</span><span class="nf">count</span><span class="p">(</span><span class="nt">BooleanLiteral</span><span class="p">[</span><span class="na">@True</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="nf">false</span><span class="p">()])</span><span class="w"> </span><span class="o">>=</span><span class="w"> </span><span class="m">1</span><span class="p">]]</span><span class="w">
|
||
</span><span class="p">|</span><span class="w">
|
||
</span><span class="c">(: do-while loops with conditional or'ed boolean literals, maybe parenthesized :)</span><span class="w">
|
||
</span><span class="o">//</span><span class="nt">DoStatement</span><span class="p">[(</span><span class="nt">InfixExpression</span><span class="p">[</span><span class="na">@Operator</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="p">(</span><span class="s1">'|'</span><span class="p">,</span><span class="w"> </span><span class="s1">'||'</span><span class="p">)])</span><span class="w">
|
||
</span><span class="c">(: at least one true literal :)</span><span class="w">
|
||
</span><span class="p">[</span><span class="nf">count</span><span class="p">(</span><span class="nt">BooleanLiteral</span><span class="p">[</span><span class="na">@True</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="nf">true</span><span class="p">()])</span><span class="w"> </span><span class="o">>=</span><span class="w"> </span><span class="m">1</span><span class="w">
|
||
</span><span class="c">(: or only boolean literal and no no var access :)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nf">count</span><span class="p">(</span><span class="nt">BooleanLiteral</span><span class="p">)</span><span class="w"> </span><span class="o">>=</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="nf">count</span><span class="p">(</span><span class="nt">VariableAccess</span><span class="p">)</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="m">0</span><span class="w">
|
||
</span><span class="p">]]</span><span class="w">
|
||
</span><span class="p">|</span><span class="w">
|
||
</span><span class="c">(: do-while loops with conditional and'ed boolean literals, maybe parenthesized :)</span><span class="w">
|
||
</span><span class="o">//</span><span class="nt">DoStatement</span><span class="p">[(</span><span class="nt">InfixExpression</span><span class="p">[</span><span class="na">@Operator</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="p">(</span><span class="s1">'&'</span><span class="p">,</span><span class="w"> </span><span class="s1">'&&'</span><span class="p">)])</span><span class="w">
|
||
</span><span class="c">(: at least one false literal :)</span><span class="w">
|
||
</span><span class="p">[</span><span class="nf">count</span><span class="p">(</span><span class="nt">BooleanLiteral</span><span class="p">[</span><span class="na">@True</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="nf">false</span><span class="p">()])</span><span class="w"> </span><span class="o">>=</span><span class="w"> </span><span class="m">1</span><span class="w">
|
||
</span><span class="c">(: or only boolean literal and no no var access :)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nf">count</span><span class="p">(</span><span class="nt">BooleanLiteral</span><span class="p">)</span><span class="w"> </span><span class="o">>=</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="nf">count</span><span class="p">(</span><span class="nt">VariableAccess</span><span class="p">)</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="m">0</span><span class="w">
|
||
</span><span class="p">]]</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Example</span> <span class="o">{</span>
|
||
<span class="o">{</span>
|
||
<span class="k">while</span> <span class="o">(</span><span class="kc">true</span><span class="o">)</span> <span class="o">{</span> <span class="o">}</span> <span class="c1">// allowed</span>
|
||
<span class="k">while</span> <span class="o">(</span><span class="kc">false</span><span class="o">)</span> <span class="o">{</span> <span class="o">}</span> <span class="c1">// disallowed</span>
|
||
<span class="k">do</span> <span class="o">{</span> <span class="o">}</span> <span class="k">while</span> <span class="o">(</span><span class="kc">true</span><span class="o">);</span> <span class="c1">// disallowed</span>
|
||
<span class="k">do</span> <span class="o">{</span> <span class="o">}</span> <span class="k">while</span> <span class="o">(</span><span class="kc">false</span><span class="o">);</span> <span class="c1">// disallowed</span>
|
||
<span class="k">do</span> <span class="o">{</span> <span class="o">}</span> <span class="k">while</span> <span class="o">(</span><span class="kc">false</span> <span class="o">|</span> <span class="kc">false</span><span class="o">);</span> <span class="c1">// disallowed</span>
|
||
<span class="k">do</span> <span class="o">{</span> <span class="o">}</span> <span class="k">while</span> <span class="o">(</span><span class="kc">false</span> <span class="o">||</span> <span class="kc">false</span><span class="o">);</span> <span class="c1">// disallowed</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule by referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/WhileLoopWithLiteralBoolean"</span> <span class="nt">/></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/bestpractices.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">
|
||
©2024 PMD Open Source Project. All rights
|
||
reserved. <br />
|
||
Site last generated: Jul 23, 2024 <br />
|
||
<p>
|
||
<img src="images/logo/pmd-logo-70px.png" alt="PMD
|
||
logo"/>
|
||
</p>
|
||
</div>
|
||
</div>
|
||
</footer>
|
||
|
||
|
||
</div>
|
||
|
||
<!-- /.row -->
|
||
</div>
|
||
<!-- /.container -->
|
||
</div>
|
||
|
||
|
||
<!-- Sticky TOC column -->
|
||
<div class="toc-col">
|
||
<div id="toc"></div>
|
||
</div>
|
||
|
||
<!-- /.toc-container-wrapper -->
|
||
</div>
|
||
</div>
|
||
|
||
<script type="application/javascript" src="assets/jquery-3.5.1/jquery-3.5.1.min.js"></script>
|
||
<script type="application/javascript" src="assets/anchorjs-4.2.2/anchor.min.js"></script>
|
||
<script type="application/javascript" src="assets/navgoco-0.2.1/src/jquery.navgoco.min.js"></script>
|
||
<script type="application/javascript" src="assets/bootstrap-4.5.2-dist/js/bootstrap.bundle.min.js"></script>
|
||
<script type="application/javascript" src="assets/Simple-Jekyll-Search-1.0.8/dest/jekyll-search.js"></script>
|
||
<script type="application/javascript" src="assets/jekyll-table-of-contents/toc.js"></script>
|
||
<script type="application/javascript" src="js/tabstate.js"></script>
|
||
<script type="application/javascript" src="js/customscripts.js"></script>
|
||
|
||
|
||
|
||
</body>
|
||
</html>
|