121aa1e8e2
TRAVIS_JOB_NUMBER=3045.2 TRAVIS_COMMIT_RANGE=b0518c902fd0...1cfc477e4877
2910 lines
168 KiB
HTML
2910 lines
168 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, AvoidPrintStackTrace, AvoidReassigningParameters, AvoidStringBufferField, AvoidUsingHardCodedIP, CheckResultSet, ConstantsInInterface, DefaultLabelNotLastInSwitchStmt, ForLoopCanBeForeach, GuardLogStatement, JUnit4SuitesShouldUseSuiteAnnotation, JUnit4TestShouldUseAfterAnnotation, JUnit4TestShouldUseBeforeAnnotation, JUnit4TestShouldUseTestAnnotation, JUnitAssertionsShouldIncludeMessage, JUnitTestContainsTooManyAsserts, JUnitTestsShouldIncludeAssert, JUnitUseExpected, LooseCoupling, MethodReturnsInternalArray, MissingOverride, OneDeclarationPerLine, PositionLiteralsFirstInCaseInsensitiveComparisons, PositionLiteralsFirstInComparisons, PreserveStackTrace, ReplaceEnumerationWithIterator, ReplaceHashtableWithMap, ReplaceVectorWithList, SwitchStmtsShouldHaveDefault, SystemPrintln, UnusedFormalParameter, UnusedImports, UnusedLocalVariable, UnusedPrivateField, UnusedPrivateMethod, UseAssertEqualsInsteadOfAssertTrue, UseAssertNullInsteadOfAssertTrue, UseAssertSameInsteadOfAssertTrue, UseAssertTrueInsteadOfAssertEquals, UseCollectionIsEmpty, UseVarargs">
|
||
<title>Best Practices | PMD Source Code Analyzer</title>
|
||
<link rel="stylesheet" href="css/syntax.css">
|
||
|
||
|
||
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
|
||
<!--<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">-->
|
||
<link rel="stylesheet" href="css/modern-business.css">
|
||
<link rel="stylesheet" href="css/lavish-bootstrap.css">
|
||
<link rel="stylesheet" href="css/customstyles.css">
|
||
<link rel="stylesheet" href="css/theme-blue.css">
|
||
<link rel="stylesheet" href="css/pmd-customstyles.css">
|
||
|
||
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
|
||
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
|
||
<script src="js/jquery.navgoco.min.js"></script>
|
||
|
||
|
||
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
|
||
<script src="https://cdnjs.cloudflare.com/ajax/libs/anchor-js/2.0.0/anchor.min.js"></script>
|
||
<script src="js/toc.js"></script>
|
||
<script src="js/customscripts.js"></script>
|
||
|
||
<link rel="shortcut icon" href="images/favicon.ico" type="image/x-icon">
|
||
<link rel="icon" href="images/favicon.ico" type="image/x-icon">
|
||
|
||
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
|
||
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
|
||
<!--[if lt IE 9]>
|
||
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
|
||
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
|
||
<![endif]-->
|
||
|
||
<link rel="alternate" type="application/rss+xml" title="" href="https://pmd.github.io/pmd/feed.xml">
|
||
|
||
<script>
|
||
$(document).ready(function() {
|
||
// Initialize navgoco with default options
|
||
$("#mysidebar").navgoco({
|
||
caretHtml: '',
|
||
accordion: true,
|
||
openClass: 'active', // open
|
||
save: false, // leave false or nav highlighting doesn't work right
|
||
cookie: {
|
||
name: 'navgoco',
|
||
expires: false,
|
||
path: '/'
|
||
},
|
||
slide: {
|
||
duration: 400,
|
||
easing: 'swing'
|
||
}
|
||
});
|
||
|
||
$("#collapseAll").click(function(e) {
|
||
e.preventDefault();
|
||
$("#mysidebar").navgoco('toggle', false);
|
||
});
|
||
|
||
$("#expandAll").click(function(e) {
|
||
e.preventDefault();
|
||
$("#mysidebar").navgoco('toggle', true);
|
||
});
|
||
|
||
});
|
||
|
||
</script>
|
||
<script>
|
||
$(function () {
|
||
$('[data-toggle="tooltip"]').tooltip()
|
||
})
|
||
</script>
|
||
<script>
|
||
$(document).ready(function() {
|
||
$("#tg-sb-link").click(function() {
|
||
$("#tg-sb-sidebar").toggle();
|
||
$("#tg-sb-content").toggleClass('col-md-9');
|
||
$("#tg-sb-content").toggleClass('col-md-12');
|
||
$("#tg-sb-icon").toggleClass('fa-toggle-on');
|
||
$("#tg-sb-icon").toggleClass('fa-toggle-off');
|
||
});
|
||
});
|
||
</script>
|
||
|
||
|
||
</head>
|
||
<body>
|
||
<!-- Content is offset by the height of the topnav bar. -->
|
||
<!-- There's already a padding-top rule in modern-business.css, but it apparently doesn't work on Firefox 60 and Chrome 67 -->
|
||
<div id="topbar-content-offset">
|
||
<!-- Navigation -->
|
||
<nav class="navbar navbar-inverse navbar-fixed-top">
|
||
<div class="container topnavlinks">
|
||
<div class="navbar-header">
|
||
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
|
||
<span class="sr-only">Toggle navigation</span>
|
||
<span class="icon-bar"></span>
|
||
<span class="icon-bar"></span>
|
||
<span class="icon-bar"></span>
|
||
</button>
|
||
<a class="fa fa-home fa-lg navbar-brand" href="index.html"> <span class="projectTitle"> PMD Source Code Analyzer Project</span></a>
|
||
</div>
|
||
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
|
||
<ul class="nav navbar-nav navbar-right">
|
||
<!-- toggle sidebar button -->
|
||
<li><a id="tg-sb-link" href="#"><i id="tg-sb-icon" class="fa fa-toggle-on"></i> Nav</a></li>
|
||
<!-- entries without drop-downs appear here -->
|
||
|
||
|
||
|
||
<li><a href="https://github.com/pmd/pmd/releases/latest" target="_blank">Download</a></li>
|
||
|
||
|
||
|
||
<li><a href="https://github.com/pmd/pmd" target="_blank">Fork us on github</a></li>
|
||
|
||
|
||
|
||
<!-- entries with drop-downs appear here -->
|
||
<!-- conditional logic to control which topnav appears for the audience defined in the configuration file.-->
|
||
|
||
|
||
<!--comment out this block if you want to hide search-->
|
||
<li>
|
||
<!--start search-->
|
||
<div id="search-demo-container">
|
||
<input type="text" id="search-input" placeholder="search...">
|
||
<ul id="results-container"></ul>
|
||
</div>
|
||
<script src="js/jekyll-search.js" type="text/javascript"></script>
|
||
<script type="text/javascript">
|
||
SimpleJekyllSearch.init({
|
||
searchInput: document.getElementById('search-input'),
|
||
resultsContainer: document.getElementById('results-container'),
|
||
dataSource: 'search.json',
|
||
searchResultTemplate: '<li><a href="{url}" title="Best Practices">{title}</a></li>',
|
||
noResultsText: 'No results found.',
|
||
limit: 10,
|
||
fuzzy: true,
|
||
})
|
||
</script>
|
||
<!--end search-->
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
</div>
|
||
<!-- /.container -->
|
||
</nav>
|
||
|
||
<!-- Page Content -->
|
||
<div class="container">
|
||
<div class="col-lg-12"> </div>
|
||
<!-- Content Row -->
|
||
<div class="row">
|
||
|
||
|
||
<!-- Sidebar Column -->
|
||
<div class="col-md-3" id="tg-sb-sidebar">
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<ul id="mysidebar" class="nav">
|
||
<li class="sidebarTitle">PMD 6.10.0</li>
|
||
|
||
|
||
|
||
|
||
|
||
<li>
|
||
|
||
<a href="#">About</a>
|
||
|
||
<ul>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="index.html">Home</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_release_notes.html">Release notes</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_about_help.html">Getting help</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
<li>
|
||
|
||
<a href="#">User Documentation</a>
|
||
|
||
<ul>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_installation.html">Installation and basic CLI usage</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_making_rulesets.html">Making rulesets</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_configuring_rules.html">Configuring rules</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_best_practices.html">Best practices</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_suppressing_warnings.html">Suppressing warnings</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_incremental_analysis.html">Incremental analysis</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_cli_reference.html">PMD CLI reference</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">Extending PMD</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_extending_writing_pmd_rules.html">Writing a rule</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_extending_writing_xpath_rules.html">Writing XPath rules</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_extending_defining_properties.html">Defining rule properties</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_extending_metrics_howto.html">Using and defining code metrics</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_extending_rule_guidelines.html">Rule guidelines</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_extending_testing.html">Testing your rules</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_cpd.html">Copy-paste detection</a></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_ant.html">Ant</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_errorprone.html">Error Prone</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_apex_performance.html">Performance</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_apex_security.html">Security</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">Ecmascript Rules</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_ecmascript.html">Index</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_ecmascript_bestpractices.html">Best Practices</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_ecmascript_codestyle.html">Code Style</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_ecmascript_errorprone.html">Error Prone</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">Java Rules</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_java.html">Index</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li class="active"><a href="pmd_rules_java_bestpractices.html">Best Practices</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_java_codestyle.html">Code Style</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_java_design.html">Design</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_java_documentation.html">Documentation</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_java_errorprone.html">Error Prone</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_java_multithreading.html">Multithreading</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_java_performance.html">Performance</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_java_security.html">Security</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">Java Server Pages Rules</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_jsp.html">Index</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_jsp_bestpractices.html">Best Practices</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_jsp_codestyle.html">Code Style</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_jsp_design.html">Design</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_jsp_errorprone.html">Error Prone</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_jsp_security.html">Security</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">Maven POM Rules</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_pom.html">Index</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_pom_errorprone.html">Error Prone</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">PLSQL Rules</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_plsql.html">Index</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_plsql_bestpractices.html">Best Practices</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_plsql_codestyle.html">Code Style</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_plsql_design.html">Design</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_plsql_errorprone.html">Error Prone</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">Salesforce VisualForce Rules</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_vf.html">Index</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_vf_security.html">Security</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">VM Rules</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_vm.html">Index</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_vm_bestpractices.html">Best Practices</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_vm_design.html">Design</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_vm_errorprone.html">Error Prone</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">XML Rules</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_xml.html">Index</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_xml_errorprone.html">Error Prone</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">XSL Rules</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_xsl.html">Index</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_xsl_codestyle.html">Code Style</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_xsl_performance.html">Performance</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
<li>
|
||
|
||
<a href="#">Language Specific Documentation</a>
|
||
|
||
<ul>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_jsp.html">JSP Support</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_java_metrics_index.html">Java code metrics</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_apex_metrics_index.html">Apex code metrics</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
<li>
|
||
|
||
<a href="#">Developer Documentation</a>
|
||
|
||
<ul>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_devdocs_development.html">Developer resources</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_devdocs_building.html">Building PMD from source</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="https://github.com/pmd/pmd/blob/master/CONTRIBUTING.md" target="_blank">Contributing</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_devdocs_writing_documentation.html">Writing documentation</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_devdocs_roadmap.html">Roadmap</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_devdocs_how_pmd_works.html">How PMD works</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_devdocs_pmdtester.html">Pmdtester</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">Major contributions</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_devdocs_major_adding_new_language.html">Adding a new language</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_devdocs_major_adding_new_cpd_language.html">Adding a new CPD language</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_devdocs_major_adding_new_metrics_framework.html">Adding metrics support to a language</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
<li>
|
||
|
||
<a href="#">Project documentation</a>
|
||
|
||
<ul>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">Trivia about PMD</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_projectdocs_trivia_news.html">PMD in the press</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_projectdocs_trivia_products.html">Products & books related to PMD</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_projectdocs_trivia_similarprojects.html">Similar projects</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_projectdocs_trivia_meaning.html">What does 'PMD' mean?</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_projectdocs_faq.html">FAQ</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="license.html">License</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_projectdocs_credits.html">Credits</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_release_notes_old.html">Old release notes</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">Project management</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_projectdocs_committers_releasing.html">Release process</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_projectdocs_committers_merging_pull_requests.html">Merging pull requests</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
<!-- if you aren't using the accordion, uncomment this block:
|
||
<p class="external">
|
||
<a href="#" id="collapseAll">Collapse All</a> | <a href="#" id="expandAll">Expand All</a>
|
||
</p>
|
||
-->
|
||
</ul>
|
||
|
||
<!-- this highlights the active parent class in the navgoco sidebar. this is critical so that the parent expands when you're viewing a page. This must appear below the sidebar code above. Otherwise, if placed inside customscripts.js, the script runs before the sidebar code runs and the class never gets inserted.-->
|
||
<script>$("li.active").parents('li').toggleClass("active");</script>
|
||
|
||
</div>
|
||
|
||
|
||
|
||
<!-- Content Column -->
|
||
<div class="col-md-9" id="tg-sb-content">
|
||
<div class="post-header">
|
||
<h1 class="post-title-main">Best Practices</h1>
|
||
</div>
|
||
|
||
|
||
|
||
<div class="post-content">
|
||
|
||
|
||
<div class="summary">Rules which enforce generally accepted best practices.</div>
|
||
|
||
|
||
|
||
|
||
<!-- this handles the automatic toc. use ## for subheads to auto-generate the on-page minitoc. if you use html tags, you must supply an ID for the heading element in order for it to appear in the minitoc. -->
|
||
<script>
|
||
$( document ).ready(function() {
|
||
// Handler for .ready() called.
|
||
|
||
$('#toc').toc({ minimumHeaders: 0, listType: 'ul', showSpeed: 0, headers: 'h2,h3,h4' });
|
||
|
||
});
|
||
</script>
|
||
|
||
<div id="toc"></div>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<a target="_blank" href="https://github.com/pmd/pmd/blob/master/docs/../pmd-java/src/main/resources/category/java/bestpractices.xml" class="btn btn-default githubEditButton" role="button"><i class="fa fa-github fa-lg"></i> Edit me</a>
|
||
|
||
|
||
|
||
<h2 id="abstractclasswithoutabstractmethod">AbstractClassWithoutAbstractMethod</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 3.0</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>The abstract class does not contain any abstract methods. An abstract class suggests
|
||
an incomplete implementation, which is to be completed by subclasses implementing the
|
||
abstract methods. If the class is intended to be used as a base class only (not to be instantiated
|
||
directly) a protected constructor can be provided prevent direct instantiation.</p>
|
||
|
||
<p><strong>This rule is defined by the following XPath expression:</strong></p>
|
||
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">ClassOrInterfaceDeclaration</span><span class="w">
|
||
</span><span class="p">[</span><span class="na">@Abstract</span><span class="err">=</span><span class="s1">'true'</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="w"> </span><span class="p">.</span><span class="o">//</span><span class="nt">MethodDeclaration</span><span class="p">[</span><span class="na">@Abstract</span><span class="err">=</span><span class="s1">'true'</span><span class="p">]</span><span class="w"> </span><span class="p">)</span><span class="err">=</span><span class="m">0</span><span class="w"> </span><span class="p">]</span><span class="w">
|
||
</span><span class="p">[</span><span class="nf">count</span><span class="p">(</span><span class="nt">ImplementsList</span><span class="p">)</span><span class="err">=</span><span class="m">0</span><span class="p">]</span><span class="w">
|
||
</span><span class="p">[</span><span class="nf">count</span><span class="p">(.</span><span class="o">//</span><span class="nt">ExtendsList</span><span class="p">)</span><span class="err">=</span><span class="m">0</span><span class="p">]</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">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>Instantiation by way of private constructors from outside of 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><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="n">Inner</span> <span class="n">ic</span> <span class="o">=</span> <span class="k">new</span> <span class="n">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>When accessing a private field / method from another class, the Java compiler will generate a accessor methods
|
||
with package-private visibility. This adds overhead, and to the dex method count on Android. This situation can
|
||
be avoided by changing the visibility of the field / method from private to package-private.</p>
|
||
|
||
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/AccessorMethodGenerationRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.AccessorMethodGenerationRule</a></p>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">OuterClass</span> <span class="o">{</span>
|
||
<span class="kd">private</span> <span class="kt">int</span> <span class="n">counter</span><span class="o">;</span>
|
||
<span class="cm">/* package */</span> <span class="kt">int</span> <span class="n">id</span><span class="o">;</span>
|
||
|
||
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">InnerClass</span> <span class="o">{</span>
|
||
<span class="n">InnerClass</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="n">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="n">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="n">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="n">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>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/ArrayIsStoredDirectly"</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">PrimaryExpression</span><span class="w">
|
||
</span><span class="p">[</span><span class="nt">PrimaryPrefix</span><span class="o">/</span><span class="nt">Name</span><span class="p">[</span><span class="nf">contains</span><span class="p">(</span><span class="na">@Image</span><span class="p">,</span><span class="s1">'printStackTrace'</span><span class="p">)]]</span><span class="w">
|
||
</span><span class="p">[</span><span class="nt">PrimarySuffix</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="nf">boolean</span><span class="p">(</span><span class="nt">Arguments</span><span class="o">/</span><span class="nt">ArgumentList</span><span class="o">/</span><span class="nt">Expression</span><span class="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="n">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="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 is not recommended. Use temporary local variables instead.</p>
|
||
|
||
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/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">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="n">String</span> <span class="n">bar</span><span class="o">)</span> <span class="o">{</span>
|
||
<span class="n">bar</span> <span class="o">=</span> <span class="s">"something else"</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">Type</span><span class="o">/</span><span class="nt">ReferenceType</span><span class="o">/</span><span class="nt">ClassOrInterfaceType</span><span class="p">[</span><span class="na">@Image</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'StringBuffer'</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="na">@Image</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'StringBuilder'</span><span class="p">]</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
|
||
<span class="kd">private</span> <span class="n">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="n">String</span> <span class="n">ip</span> <span class="o">=</span> <span class="s">"127.0.0.1"</span><span class="o">;</span> <span class="c1">// not recommended</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>This rule has the following properties:</strong></p>
|
||
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th>Name</th>
|
||
<th>Default Value</th>
|
||
<th>Description</th>
|
||
<th>Multivalued</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td>checkAddressTypes</td>
|
||
<td>IPv4 | IPv6 | IPv4 mapped IPv6</td>
|
||
<td>Check for IP address types.</td>
|
||
<td>yes. Delimiter is ‘|’.</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
|
||
<p><strong>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/AvoidUsingHardCodedIP"</span> <span class="nt">/></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="n">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="n">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="n">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="n">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="n">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="n">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>Avoid constants in interfaces. Interfaces should define types, constants are implementation details
|
||
better placed in classes or enums. See Effective Java, item 19.</p>
|
||
|
||
<p><strong>This rule is defined by the following XPath expression:</strong></p>
|
||
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">ClassOrInterfaceDeclaration</span><span class="p">[</span><span class="na">@Interface</span><span class="err">=</span><span class="s1">'true'</span><span class="p">][</span><span class="nv">$ignoreIfHasMethods</span><span class="err">=</span><span class="s1">'false'</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="ow">not</span><span class="p">(.</span><span class="o">//</span><span class="nt">MethodDeclaration</span><span class="p">)]</span><span class="o">//</span><span class="nt">FieldDeclaration</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">interface</span> <span class="nc">ConstantInterface</span> <span class="o">{</span>
|
||
<span class="kd">public</span> <span class="kd">static</span> <span class="kd">final</span> <span class="kt">int</span> <span class="n">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="n">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="n">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="n">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="n">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="n">CONST1</span> <span class="o">=</span> <span class="mi">1</span><span class="o">;</span> <span class="c1">// no violation</span>
|
||
|
||
<span class="kt">int</span> <span class="nf">anyMethod</span><span class="o">();</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>This rule has the following properties:</strong></p>
|
||
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th>Name</th>
|
||
<th>Default Value</th>
|
||
<th>Description</th>
|
||
<th>Multivalued</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td>ignoreIfHasMethods</td>
|
||
<td>true</td>
|
||
<td>Whether to ignore constants in interfaces if the interface defines any methods</td>
|
||
<td>no</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
|
||
<p><strong>Use this rule 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/ConstantsInInterface"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="defaultlabelnotlastinswitchstmt">DefaultLabelNotLastInSwitchStmt</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 1.5</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>By convention, the default label should be the last label in a switch statement.</p>
|
||
|
||
<p><strong>This rule is defined by the following XPath expression:</strong></p>
|
||
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">SwitchStatement</span><span class="w">
|
||
</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="nt">SwitchLabel</span><span class="p">[</span><span class="nf">position</span><span class="p">()</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="nf">last</span><span class="p">()][</span><span class="na">@Default</span><span class="err">=</span><span class="s1">'true'</span><span class="p">])]</span><span class="w">
|
||
</span><span class="p">[</span><span class="nt">SwitchLabel</span><span class="p">[</span><span class="na">@Default</span><span class="err">=</span><span class="s1">'true'</span><span class="p">]]</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
|
||
<span class="kt">void</span> <span class="nf">bar</span><span class="o">(</span><span class="kt">int</span> <span class="n">a</span><span class="o">)</span> <span class="o">{</span>
|
||
<span class="k">switch</span> <span class="o">(</span><span class="n">a</span><span class="o">)</span> <span class="o">{</span>
|
||
<span class="k">case</span> <span class="mi">1</span><span class="o">:</span> <span class="c1">// do something</span>
|
||
<span class="k">break</span><span class="o">;</span>
|
||
<span class="k">default</span><span class="o">:</span> <span class="c1">// the default case should be last, by convention</span>
|
||
<span class="k">break</span><span class="o">;</span>
|
||
<span class="k">case</span> <span class="mi">2</span><span class="o">:</span>
|
||
<span class="k">break</span><span class="o">;</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule by referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><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="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="n">List</span><span class="o"><</span><span class="n">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="n">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="n">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="n">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="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><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="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="s">" and "</span> <span class="o">+</span> <span class="s">"concat strings"</span><span class="o">);</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>This rule has the following properties:</strong></p>
|
||
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th>Name</th>
|
||
<th>Default Value</th>
|
||
<th>Description</th>
|
||
<th>Multivalued</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td>guardsMethods</td>
|
||
<td>isTraceEnabled , isDebugEnabled , isInfoEnabled , isWarnEnabled , isErrorEnabled , isLoggable</td>
|
||
<td>method use to guard the log statement</td>
|
||
<td>yes. Delimiter is ‘,’.</td>
|
||
</tr>
|
||
<tr>
|
||
<td>logLevels</td>
|
||
<td>trace , debug , info , warn , error , log , finest , finer , fine , info , warning , severe</td>
|
||
<td>LogLevels to guard</td>
|
||
<td>yes. Delimiter is ‘,’.</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
|
||
<p><strong>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/GuardLogStatement"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="junit4suitesshouldusesuiteannotation">JUnit4SuitesShouldUseSuiteAnnotation</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 4.0</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>In JUnit 3, test suites are indicated by the suite() method. In JUnit 4, suites are indicated
|
||
through the @RunWith(Suite.class) annotation.</p>
|
||
|
||
<p><strong>This rule is defined by the following XPath expression:</strong></p>
|
||
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">ClassOrInterfaceBodyDeclaration</span><span class="p">[</span><span class="nt">MethodDeclaration</span><span class="o">/</span><span class="nt">MethodDeclarator</span><span class="p">[</span><span class="na">@Image</span><span class="err">=</span><span class="s1">'suite'</span><span class="p">]]</span><span class="w">
|
||
</span><span class="p">[</span><span class="nt">MethodDeclaration</span><span class="o">/</span><span class="nt">ResultType</span><span class="o">/</span><span class="nt">Type</span><span class="o">/</span><span class="nt">ReferenceType</span><span class="o">/</span><span class="nt">ClassOrInterfaceType</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'junit.framework.Test'</span><span class="p">)]]</span><span class="w">
|
||
</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="nt">MethodDeclaration</span><span class="o">/</span><span class="nt">Block</span><span class="o">//</span><span class="nt">ClassOrInterfaceType</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'junit.framework.JUnit4TestAdapter'</span><span class="p">)])]</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">BadExample</span> <span class="kd">extends</span> <span class="n">TestCase</span><span class="o">{</span>
|
||
|
||
<span class="kd">public</span> <span class="kd">static</span> <span class="n">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="n">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="n">TestOne</span><span class="o">.</span><span class="na">class</span><span class="o">,</span> <span class="n">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">ClassOrInterfaceBodyDeclaration</span><span class="w">
|
||
</span><span class="p">[</span><span class="nt">MethodDeclaration</span><span class="o">/</span><span class="nt">MethodDeclarator</span><span class="p">[</span><span class="na">@Image</span><span class="err">=</span><span class="s1">'tearDown'</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">Annotation</span><span class="o">//</span><span class="nt">Name</span><span class="p">[</span><span class="w">
|
||
</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.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="err">=</span><span class="m">0</span><span class="p">]</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">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">ClassOrInterfaceBodyDeclaration</span><span class="w">
|
||
</span><span class="p">[</span><span class="nt">MethodDeclaration</span><span class="o">/</span><span class="nt">MethodDeclarator</span><span class="p">[</span><span class="na">@Image</span><span class="err">=</span><span class="s1">'setUp'</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">Annotation</span><span class="o">//</span><span class="nt">Name</span><span class="p">[</span><span class="w">
|
||
</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.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="err">=</span><span class="m">0</span><span class="p">]</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">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.</p>
|
||
|
||
<p><strong>This rule is defined by the following XPath expression:</strong></p>
|
||
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">ClassOrInterfaceDeclaration</span><span class="p">[</span><span class="w">
|
||
</span><span class="nf">matches</span><span class="p">(</span><span class="na">@Image</span><span class="p">,</span><span class="w"> </span><span class="nv">$testClassPattern</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nt">ExtendsList</span><span class="o">/</span><span class="nt">ClassOrInterfaceType</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'junit.framework.TestCase'</span><span class="p">)]]</span><span class="w">
|
||
|
||
</span><span class="o">/</span><span class="nt">ClassOrInterfaceBody</span><span class="o">/</span><span class="nt">ClassOrInterfaceBodyDeclaration</span><span class="p">[</span><span class="nt">MethodDeclaration</span><span class="p">[</span><span class="na">@Public</span><span class="err">=</span><span class="nf">true</span><span class="p">()]</span><span class="o">/</span><span class="nt">MethodDeclarator</span><span class="p">[</span><span class="nf">starts-with</span><span class="p">(</span><span class="na">@Image</span><span class="p">,</span><span class="w"> </span><span class="s1">'test'</span><span class="p">)]]</span><span class="w">
|
||
</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="nt">Annotation</span><span class="o">//</span><span class="nt">Name</span><span class="p">[</span><span class="w">
|
||
</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.Test'</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.Test'</span><span class="p">)</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.RepeatedTest'</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.TestFactory'</span><span class="p">)</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.TestTemplate'</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.params.ParameterizedTest'</span><span class="p">)</span><span class="w">
|
||
</span><span class="p">])]</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">MyTest</span> <span class="o">{</span>
|
||
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">testBad</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="n">doSomething</span><span class="o">();</span>
|
||
<span class="o">}</span>
|
||
|
||
<span class="nd">@Test</span>
|
||
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">testGood</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="n">doSomething</span><span class="o">();</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>This rule has the following properties:</strong></p>
|
||
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th>Name</th>
|
||
<th>Default Value</th>
|
||
<th>Description</th>
|
||
<th>Multivalued</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td>testClassPattern</td>
|
||
<td>Test</td>
|
||
<td>The regex pattern used to identify test classes</td>
|
||
<td>no</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
|
||
<p><strong>Use this rule 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/JUnit4TestShouldUseTestAnnotation"</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="n">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 XPath expression:</strong></p>
|
||
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">MethodDeclarator</span><span class="p">[</span><span class="na">@Image</span><span class="p">[</span><span class="nn">fn:</span><span class="nf">matches</span><span class="p">(.,</span><span class="s1">'^test'</span><span class="p">)]</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="o">/</span><span class="nt">Annotation</span><span class="o">/</span><span class="nt">MarkerAnnotation</span><span class="o">/</span><span class="nt">Name</span><span class="p">[</span><span class="w">
|
||
</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.Test'</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.Test'</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.RepeatedTest'</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.TestFactory'</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.TestTemplate'</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.params.ParameterizedTest'</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.testng.annotations.Test'</span><span class="p">)</span><span class="w">
|
||
</span><span class="p">]]</span><span class="w">
|
||
</span><span class="p">[</span><span class="nf">count</span><span class="p">(..</span><span class="o">//</span><span class="nt">PrimaryPrefix</span><span class="o">/</span><span class="nt">Name</span><span class="p">[</span><span class="na">@Image</span><span class="p">[</span><span class="nn">fn:</span><span class="nf">matches</span><span class="p">(.,</span><span class="s1">'^assert'</span><span class="p">)]])</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="nv">$maximumAsserts</span><span class="p">]</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">MyTestCase</span> <span class="kd">extends</span> <span class="n">TestCase</span> <span class="o">{</span>
|
||
<span class="c1">// Ok</span>
|
||
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">testMyCaseWithOneAssert</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="kt">boolean</span> <span class="n">myVar</span> <span class="o">=</span> <span class="kc">false</span><span class="o">;</span>
|
||
<span class="n">assertFalse</span><span class="o">(</span><span class="s">"should be false"</span><span class="o">,</span> <span class="n">myVar</span><span class="o">);</span>
|
||
<span class="o">}</span>
|
||
|
||
<span class="c1">// Bad, too many asserts (assuming max=1)</span>
|
||
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">testMyCaseWithMoreAsserts</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="kt">boolean</span> <span class="n">myVar</span> <span class="o">=</span> <span class="kc">false</span><span class="o">;</span>
|
||
<span class="n">assertFalse</span><span class="o">(</span><span class="s">"myVar should be false"</span><span class="o">,</span> <span class="n">myVar</span><span class="o">);</span>
|
||
<span class="n">assertEquals</span><span class="o">(</span><span class="s">"should equals false"</span><span class="o">,</span> <span class="kc">false</span><span class="o">,</span> <span class="n">myVar</span><span class="o">);</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>This rule has the following properties:</strong></p>
|
||
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th>Name</th>
|
||
<th>Default Value</th>
|
||
<th>Description</th>
|
||
<th>Multivalued</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td>maximumAsserts</td>
|
||
<td>1</td>
|
||
<td>Maximum number of Asserts in a test method</td>
|
||
<td>no</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
|
||
<p><strong>Use this rule 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/JUnitTestContainsTooManyAsserts"</span> <span class="nt">/></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="n">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">Bar</span> <span class="n">b</span> <span class="o">=</span> <span class="n">findBar</span><span class="o">();</span>
|
||
<span class="c1">// This is better than having a NullPointerException</span>
|
||
<span class="c1">// assertNotNull("bar not found", b);</span>
|
||
<span class="n">b</span><span class="o">.</span><span class="na">work</span><span class="o">();</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule by referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/JUnitTestsShouldIncludeAssert"</span> <span class="nt">/></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="n">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="n">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="loosecoupling">LooseCoupling</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 0.7</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>The use of implementation types (i.e., HashSet) as object references limits your ability to use alternate
|
||
implementations in the future as requirements change. Whenever available, referencing objects
|
||
by their interface types (i.e, Set) provides much more flexibility.</p>
|
||
|
||
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/LooseCouplingRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.LooseCouplingRule</a></p>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">import</span> <span class="nn">java.util.ArrayList</span><span class="o">;</span>
|
||
<span class="kn">import</span> <span class="nn">java.util.HashSet</span><span class="o">;</span>
|
||
|
||
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">Bar</span> <span class="o">{</span>
|
||
<span class="c1">// sub-optimal approach</span>
|
||
<span class="kd">private</span> <span class="n">ArrayList</span><span class="o"><</span><span class="n">SomeType</span><span class="o">></span> <span class="n">list</span> <span class="o">=</span> <span class="k">new</span> <span class="n">ArrayList</span><span class="o"><>();</span>
|
||
|
||
<span class="kd">public</span> <span class="n">HashSet</span><span class="o"><</span><span class="n">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="n">HashSet</span><span class="o"><</span><span class="n">SomeType</span><span class="o">>();</span>
|
||
<span class="o">}</span>
|
||
|
||
<span class="c1">// preferred approach</span>
|
||
<span class="kd">private</span> <span class="n">List</span><span class="o"><</span><span class="n">SomeType</span><span class="o">></span> <span class="n">list</span> <span class="o">=</span> <span class="k">new</span> <span class="n">ArrayList</span><span class="o"><>();</span>
|
||
|
||
<span class="kd">public</span> <span class="n">Set</span><span class="o"><</span><span class="n">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="n">HashSet</span><span class="o"><</span><span class="n">SomeType</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/LooseCoupling"</span> <span class="nt">/></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="n">UserData</span> <span class="o">[]</span> <span class="n">ud</span><span class="o">;</span>
|
||
<span class="kd">public</span> <span class="n">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="n">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="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="n">String</span> <span class="n">name</span><span class="o">;</span> <span class="c1">// separate declarations</span>
|
||
<span class="n">String</span> <span class="n">lastname</span><span class="o">;</span>
|
||
|
||
<span class="n">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="n">String</span> <span class="n">name</span><span class="o">,</span>
|
||
<span class="n">lastname</span><span class="o">;</span> <span class="c1">// combined declaration on multiple lines, no violation by default.</span>
|
||
<span class="c1">// Set property strictMode to true to mark this as violation.</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>This rule has the following properties:</strong></p>
|
||
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th>Name</th>
|
||
<th>Default Value</th>
|
||
<th>Description</th>
|
||
<th>Multivalued</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td>strictMode</td>
|
||
<td>false</td>
|
||
<td>If true, mark combined declaration even if the declarations are on separate lines.</td>
|
||
<td>no</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
|
||
<p><strong>Use this rule 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/OneDeclarationPerLine"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="positionliteralsfirstincaseinsensitivecomparisons">PositionLiteralsFirstInCaseInsensitiveComparisons</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 5.1</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Position literals first in comparisons, if the second argument is null then NullPointerExceptions
|
||
can be avoided, they will just return false.</p>
|
||
|
||
<p><strong>This rule is defined by the following XPath expression:</strong></p>
|
||
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">PrimaryExpression</span><span class="p">[</span><span class="w">
|
||
</span><span class="nt">PrimaryPrefix</span><span class="p">[</span><span class="nt">Name</span><span class="w">
|
||
</span><span class="p">[</span><span class="w">
|
||
</span><span class="p">(</span><span class="nf">ends-with</span><span class="p">(</span><span class="na">@Image</span><span class="p">,</span><span class="w"> </span><span class="s1">'.equalsIgnoreCase'</span><span class="p">))</span><span class="w">
|
||
</span><span class="p">]</span><span class="w">
|
||
</span><span class="p">]</span><span class="w">
|
||
</span><span class="p">[</span><span class="w">
|
||
</span><span class="p">(..</span><span class="o">/</span><span class="nt">PrimarySuffix</span><span class="o">/</span><span class="nt">Arguments</span><span class="o">/</span><span class="nt">ArgumentList</span><span class="o">/</span><span class="nt">Expression</span><span class="o">/</span><span class="nt">PrimaryExpression</span><span class="o">/</span><span class="nt">PrimaryPrefix</span><span class="o">/</span><span class="nt">Literal</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">and</span><span class="w">
|
||
</span><span class="p">(</span><span class="w"> </span><span class="nf">count</span><span class="p">(..</span><span class="o">/</span><span class="nt">PrimarySuffix</span><span class="o">/</span><span class="nt">Arguments</span><span class="o">/</span><span class="nt">ArgumentList</span><span class="o">/</span><span class="nt">Expression</span><span class="p">)</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="m">1</span><span class="w"> </span><span class="p">)</span><span class="w">
|
||
</span><span class="p">]</span><span class="w">
|
||
</span><span class="p">]</span><span class="w">
|
||
</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="kn">ancestor::</span><span class="nt">Expression</span><span class="o">/</span><span class="nt">ConditionalAndExpression</span><span class="o">//</span><span class="nt">EqualityExpression</span><span class="p">[</span><span class="na">@Image</span><span class="err">=</span><span class="s1">'!='</span><span class="p">]</span><span class="o">//</span><span class="nt">NullLiteral</span><span class="p">)]</span><span class="w">
|
||
</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="kn">ancestor::</span><span class="nt">Expression</span><span class="o">/</span><span class="nt">ConditionalOrExpression</span><span class="o">//</span><span class="nt">EqualityExpression</span><span class="p">[</span><span class="na">@Image</span><span class="err">=</span><span class="s1">'=='</span><span class="p">]</span><span class="o">//</span><span class="nt">NullLiteral</span><span class="p">)]</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="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="n">String</span> <span class="n">x</span><span class="o">)</span> <span class="o">{</span>
|
||
<span class="k">return</span> <span class="n">x</span><span class="o">.</span><span class="na">equalsIgnoreCase</span><span class="o">(</span><span class="s">"2"</span><span class="o">);</span> <span class="c1">// should be "2".equalsIgnoreCase(x)</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule by referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/PositionLiteralsFirstInCaseInsensitiveComparisons"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="positionliteralsfirstincomparisons">PositionLiteralsFirstInComparisons</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 3.3</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Position literals first in comparisons, if the second argument is null then NullPointerExceptions
|
||
can be avoided, they will just return false.</p>
|
||
|
||
<p><strong>This rule is defined by the following XPath expression:</strong></p>
|
||
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">PrimaryExpression</span><span class="p">[</span><span class="w">
|
||
</span><span class="nt">PrimaryPrefix</span><span class="p">[</span><span class="nt">Name</span><span class="p">[(</span><span class="nf">ends-with</span><span class="p">(</span><span class="na">@Image</span><span class="p">,</span><span class="w"> </span><span class="s1">'.equals'</span><span class="p">))]]</span><span class="w">
|
||
</span><span class="p">[</span><span class="w">
|
||
</span><span class="p">(..</span><span class="o">/</span><span class="nt">PrimarySuffix</span><span class="o">/</span><span class="nt">Arguments</span><span class="o">/</span><span class="nt">ArgumentList</span><span class="o">/</span><span class="nt">Expression</span><span class="o">/</span><span class="nt">PrimaryExpression</span><span class="o">/</span><span class="nt">PrimaryPrefix</span><span class="o">/</span><span class="nt">Literal</span><span class="p">[</span><span class="na">@StringLiteral</span><span class="err">=</span><span class="s1">'true'</span><span class="p">])</span><span class="w">
|
||
</span><span class="ow">and</span><span class="w">
|
||
</span><span class="p">(</span><span class="w"> </span><span class="nf">count</span><span class="p">(..</span><span class="o">/</span><span class="nt">PrimarySuffix</span><span class="o">/</span><span class="nt">Arguments</span><span class="o">/</span><span class="nt">ArgumentList</span><span class="o">/</span><span class="nt">Expression</span><span class="p">)</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="m">1</span><span class="w"> </span><span class="p">)</span><span class="w">
|
||
</span><span class="p">]</span><span class="w">
|
||
</span><span class="p">]</span><span class="w">
|
||
</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="kn">ancestor::</span><span class="nt">Expression</span><span class="o">/</span><span class="nt">ConditionalAndExpression</span><span class="o">//</span><span class="nt">EqualityExpression</span><span class="p">[</span><span class="na">@Image</span><span class="err">=</span><span class="s1">'!='</span><span class="p">]</span><span class="o">//</span><span class="nt">NullLiteral</span><span class="p">)]</span><span class="w">
|
||
</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="kn">ancestor::</span><span class="nt">Expression</span><span class="o">/</span><span class="nt">ConditionalOrExpression</span><span class="o">//</span><span class="nt">EqualityExpression</span><span class="p">[</span><span class="na">@Image</span><span class="err">=</span><span class="s1">'=='</span><span class="p">]</span><span class="o">//</span><span class="nt">NullLiteral</span><span class="p">)]</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="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="n">String</span> <span class="n">x</span><span class="o">)</span> <span class="o">{</span>
|
||
<span class="k">return</span> <span class="n">x</span><span class="o">.</span><span class="na">equals</span><span class="o">(</span><span class="s">"2"</span><span class="o">);</span> <span class="c1">// should be "2".equals(x)</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule by referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/PositionLiteralsFirstInComparisons"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="preservestacktrace">PreserveStackTrace</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 3.7</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Throwing a new exception from a catch block without passing the original exception into the
|
||
new exception will cause the original stack trace to be lost making it difficult to debug
|
||
effectively.</p>
|
||
|
||
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/PreserveStackTraceRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.PreserveStackTraceRule</a></p>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
|
||
<span class="kt">void</span> <span class="nf">good</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="k">try</span><span class="o">{</span>
|
||
<span class="n">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="n">Exception</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
|
||
<span class="k">throw</span> <span class="k">new</span> <span class="nf">Exception</span><span class="o">(</span><span class="n">e</span><span class="o">);</span> <span class="c1">// first possibility to create exception chain</span>
|
||
<span class="o">}</span>
|
||
<span class="k">try</span> <span class="o">{</span>
|
||
<span class="n">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="n">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="n">IllegalStateException</span><span class="o">)</span><span class="k">new</span> <span class="n">IllegalStateException</span><span class="o">().</span><span class="na">initCause</span><span class="o">(</span><span class="n">e</span><span class="o">);</span> <span class="c1">// second possibility to create exception chain.</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
<span class="kt">void</span> <span class="nf">bad</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="k">try</span><span class="o">{</span>
|
||
<span class="n">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="n">Exception</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
|
||
<span class="k">throw</span> <span class="k">new</span> <span class="nf">Exception</span><span class="o">(</span><span class="n">e</span><span class="o">.</span><span class="na">getMessage</span><span class="o">());</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule by referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><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="replaceenumerationwithiterator">ReplaceEnumerationWithIterator</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 3.4</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Consider replacing Enumeration usages with the newer java.util.Iterator</p>
|
||
|
||
<p><strong>This rule is defined by the following XPath expression:</strong></p>
|
||
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">ImplementsList</span><span class="o">/</span><span class="nt">ClassOrInterfaceType</span><span class="p">[</span><span class="na">@Image</span><span class="err">=</span><span class="s1">'Enumeration'</span><span class="p">]</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="kd">implements</span> <span class="n">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="n">Object</span> <span class="nf">nextElement</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="k">return</span> <span class="n">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">Type</span><span class="o">/</span><span class="nt">ReferenceType</span><span class="o">/</span><span class="nt">ClassOrInterfaceType</span><span class="p">[</span><span class="na">@Image</span><span class="err">=</span><span class="s1">'Hashtable'</span><span class="p">]</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
|
||
<span class="kt">void</span> <span class="nf">bar</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="n">Hashtable</span> <span class="n">h</span> <span class="o">=</span> <span class="k">new</span> <span class="n">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">Type</span><span class="o">/</span><span class="nt">ReferenceType</span><span class="o">/</span><span class="nt">ClassOrInterfaceType</span><span class="p">[</span><span class="na">@Image</span><span class="err">=</span><span class="s1">'Vector'</span><span class="p">]</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
|
||
<span class="kt">void</span> <span class="nf">bar</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="n">Vector</span> <span class="n">v</span> <span class="o">=</span> <span class="k">new</span> <span class="n">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="switchstmtsshouldhavedefault">SwitchStmtsShouldHaveDefault</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 1.0</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>All switch statements should include a default option to catch any unspecified values.</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">public</span> <span class="kt">void</span> <span class="nf">bar</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="kt">int</span> <span class="n">x</span> <span class="o">=</span> <span class="mi">2</span><span class="o">;</span>
|
||
<span class="k">switch</span> <span class="o">(</span><span class="n">x</span><span class="o">)</span> <span class="o">{</span>
|
||
<span class="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">Name</span><span class="p">[</span><span class="w">
|
||
</span><span class="nf">starts-with</span><span class="p">(</span><span class="na">@Image</span><span class="p">,</span><span class="w"> </span><span class="s1">'System.out.print'</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w">
|
||
</span><span class="nf">starts-with</span><span class="p">(</span><span class="na">@Image</span><span class="p">,</span><span class="w"> </span><span class="s1">'System.err.print'</span><span class="p">)</span><span class="w">
|
||
</span><span class="p">]</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">class</span> <span class="nc">Foo</span><span class="o">{</span>
|
||
<span class="n">Logger</span> <span class="n">log</span> <span class="o">=</span> <span class="n">Logger</span><span class="o">.</span><span class="na">getLogger</span><span class="o">(</span><span class="n">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="n">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="unusedformalparameter">UnusedFormalParameter</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 0.8</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Avoid passing parameters to methods or constructors without actually referencing them in the method body.</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="n">String</span> <span class="n">howdy</span><span class="o">)</span> <span class="o">{</span>
|
||
<span class="c1">// howdy is not used</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>This rule has the following properties:</strong></p>
|
||
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th>Name</th>
|
||
<th>Default Value</th>
|
||
<th>Description</th>
|
||
<th>Multivalued</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td>checkAll</td>
|
||
<td>false</td>
|
||
<td>Check all methods, including non-private ones</td>
|
||
<td>no</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
|
||
<p><strong>Use this rule 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/UnusedFormalParameter"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="unusedimports">UnusedImports</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 1.0</p>
|
||
|
||
<p><strong>Priority:</strong> Medium Low (4)</p>
|
||
|
||
<p>Avoid unused import statements to prevent unwanted dependencies.
|
||
This rule will also find unused on demand imports, i.e. import com.foo.*.</p>
|
||
|
||
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UnusedImportsRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.UnusedImportsRule</a></p>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">import</span> <span class="nn">java.io.File</span><span class="o">;</span> <span class="c1">// not referenced or required</span>
|
||
<span class="kn">import</span> <span class="nn">java.util.*</span><span class="o">;</span> <span class="c1">// not referenced or required</span>
|
||
|
||
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule by referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/UnusedImports"</span> <span class="nt">/></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.</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><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="n">FOO</span> <span class="o">=</span> <span class="mi">2</span><span class="o">;</span> <span class="c1">// Unused</span>
|
||
<span class="kd">private</span> <span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">5</span><span class="o">;</span> <span class="c1">// Unused</span>
|
||
<span class="kd">private</span> <span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">6</span><span class="o">;</span>
|
||
<span class="kd">public</span> <span class="kt">int</span> <span class="nf">addOne</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="k">return</span> <span class="n">j</span><span class="o">++;</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>This rule has the following properties:</strong></p>
|
||
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th>Name</th>
|
||
<th>Default Value</th>
|
||
<th>Description</th>
|
||
<th>Multivalued</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td>ignoredAnnotations</td>
|
||
<td>lombok.Setter | lombok.Getter | lombok.Builder | lombok.Data | lombok.RequiredArgsConstructor | lombok.AllArgsConstructor | lombok.Value | lombok.NoArgsConstructor | java.lang.Deprecated | javafx.fxml.FXML</td>
|
||
<td>Fully qualified names of the annotation types that should be ignored by this rule</td>
|
||
<td>yes. Delimiter is ‘|’.</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
|
||
<p><strong>Use this rule 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/UnusedPrivateField"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="unusedprivatemethod">UnusedPrivateMethod</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 0.7</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Unused Private Method detects when a private method is declared but is unused.</p>
|
||
|
||
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UnusedPrivateMethodRule.java">net.sourceforge.pmd.lang.java.rule.bestpractices.UnusedPrivateMethodRule</a></p>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Something</span> <span class="o">{</span>
|
||
<span class="kd">private</span> <span class="kt">void</span> <span class="nf">foo</span><span class="o">()</span> <span class="o">{}</span> <span class="c1">// unused</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>This rule has the following properties:</strong></p>
|
||
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th>Name</th>
|
||
<th>Default Value</th>
|
||
<th>Description</th>
|
||
<th>Multivalued</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td>ignoredAnnotations</td>
|
||
<td>java.lang.Deprecated</td>
|
||
<td>Fully qualified names of the annotation types that should be ignored by this rule</td>
|
||
<td>yes. Delimiter is ‘|’.</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
|
||
<p><strong>Use this rule 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/UnusedPrivateMethod"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="useassertequalsinsteadofasserttrue">UseAssertEqualsInsteadOfAssertTrue</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 3.1</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>This rule detects JUnit assertions in object equality. These assertions should be made by more specific methods, like assertEquals.</p>
|
||
|
||
<p><strong>This rule is defined by the following XPath expression:</strong></p>
|
||
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">PrimaryExpression</span><span class="p">[</span><span class="w">
|
||
</span><span class="nt">PrimaryPrefix</span><span class="o">/</span><span class="nt">Name</span><span class="p">[</span><span class="na">@Image</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'assertTrue'</span><span class="p">]</span><span class="w">
|
||
</span><span class="p">][</span><span class="w">
|
||
</span><span class="nt">PrimarySuffix</span><span class="o">/</span><span class="nt">Arguments</span><span class="o">/</span><span class="nt">ArgumentList</span><span class="o">/</span><span class="nt">Expression</span><span class="o">/</span><span class="nt">PrimaryExpression</span><span class="o">/</span><span class="nt">PrimaryPrefix</span><span class="o">/</span><span class="nt">Name</span><span class="w">
|
||
</span><span class="p">[</span><span class="nf">ends-with</span><span class="p">(</span><span class="na">@Image</span><span class="p">,</span><span class="w"> </span><span class="s1">'.equals'</span><span class="p">)]</span><span class="w">
|
||
</span><span class="p">]</span><span class="w">
|
||
</span><span class="p">[</span><span class="kn">ancestor::</span><span class="nt">ClassOrInterfaceDeclaration</span><span class="p">[</span><span class="o">//</span><span class="nt">ClassOrInterfaceType</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'junit.framework.TestCase'</span><span class="p">)]</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="o">//</span><span class="nt">MarkerAnnotation</span><span class="o">/</span><span class="nt">Name</span><span class="p">[</span><span class="w">
|
||
</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.Test'</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.Test'</span><span class="p">)</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.RepeatedTest'</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.TestFactory'</span><span class="p">)</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.TestTemplate'</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.params.ParameterizedTest'</span><span class="p">)</span><span class="w">
|
||
</span><span class="p">]</span><span class="w">
|
||
</span><span class="p">]]</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">FooTest</span> <span class="kd">extends</span> <span class="n">TestCase</span> <span class="o">{</span>
|
||
<span class="kt">void</span> <span class="nf">testCode</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="n">Object</span> <span class="n">a</span><span class="o">,</span> <span class="n">b</span><span class="o">;</span>
|
||
<span class="n">assertTrue</span><span class="o">(</span><span class="n">a</span><span class="o">.</span><span class="na">equals</span><span class="o">(</span><span class="n">b</span><span class="o">));</span> <span class="c1">// bad usage</span>
|
||
<span class="n">assertEquals</span><span class="o">(?</span><span class="n">a</span> <span class="n">should</span> <span class="n">equals</span> <span class="n">b</span><span class="o">?,</span> <span class="n">a</span><span class="o">,</span> <span class="n">b</span><span class="o">);</span> <span class="c1">// good usage</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule by referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/UseAssertEqualsInsteadOfAssertTrue"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="useassertnullinsteadofasserttrue">UseAssertNullInsteadOfAssertTrue</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 3.5</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>This rule detects JUnit assertions in object references equality. These assertions should be made by
|
||
more specific methods, like assertNull, assertNotNull.</p>
|
||
|
||
<p><strong>This rule is defined by the following XPath expression:</strong></p>
|
||
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">PrimaryExpression</span><span class="p">[</span><span class="w">
|
||
</span><span class="nt">PrimaryPrefix</span><span class="o">/</span><span class="nt">Name</span><span class="p">[</span><span class="na">@Image</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'assertTrue'</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="na">@Image</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'assertFalse'</span><span class="p">]</span><span class="w">
|
||
</span><span class="p">][</span><span class="w">
|
||
</span><span class="nt">PrimarySuffix</span><span class="o">/</span><span class="nt">Arguments</span><span class="o">/</span><span class="nt">ArgumentList</span><span class="p">[</span><span class="w">
|
||
</span><span class="nt">Expression</span><span class="o">/</span><span class="nt">EqualityExpression</span><span class="o">/</span><span class="nt">PrimaryExpression</span><span class="o">/</span><span class="nt">PrimaryPrefix</span><span class="o">/</span><span class="nt">Literal</span><span class="o">/</span><span class="nt">NullLiteral</span><span class="w">
|
||
</span><span class="p">]</span><span class="w">
|
||
</span><span class="p">]</span><span class="w">
|
||
</span><span class="p">[</span><span class="kn">ancestor::</span><span class="nt">ClassOrInterfaceDeclaration</span><span class="p">[</span><span class="o">//</span><span class="nt">ClassOrInterfaceType</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'junit.framework.TestCase'</span><span class="p">)]</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="o">//</span><span class="nt">MarkerAnnotation</span><span class="o">/</span><span class="nt">Name</span><span class="p">[</span><span class="w">
|
||
</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.Test'</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.Test'</span><span class="p">)</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.RepeatedTest'</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.TestFactory'</span><span class="p">)</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.TestTemplate'</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.params.ParameterizedTest'</span><span class="p">)</span><span class="w">
|
||
</span><span class="p">]</span><span class="w">
|
||
</span><span class="p">]]</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">FooTest</span> <span class="kd">extends</span> <span class="n">TestCase</span> <span class="o">{</span>
|
||
<span class="kt">void</span> <span class="nf">testCode</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="n">Object</span> <span class="n">a</span> <span class="o">=</span> <span class="n">doSomething</span><span class="o">();</span>
|
||
<span class="n">assertTrue</span><span class="o">(</span><span class="n">a</span><span class="o">==</span><span class="kc">null</span><span class="o">);</span> <span class="c1">// bad usage</span>
|
||
<span class="n">assertNull</span><span class="o">(</span><span class="n">a</span><span class="o">);</span> <span class="c1">// good usage</span>
|
||
<span class="n">assertTrue</span><span class="o">(</span><span class="n">a</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">);</span> <span class="c1">// bad usage</span>
|
||
<span class="n">assertNotNull</span><span class="o">(</span><span class="n">a</span><span class="o">);</span> <span class="c1">// good usage</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule by referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/UseAssertNullInsteadOfAssertTrue"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="useassertsameinsteadofasserttrue">UseAssertSameInsteadOfAssertTrue</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 3.1</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>This rule detects JUnit assertions in object references equality. These assertions should be made
|
||
by more specific methods, like assertSame, assertNotSame.</p>
|
||
|
||
<p><strong>This rule is defined by the following XPath expression:</strong></p>
|
||
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">PrimaryExpression</span><span class="p">[</span><span class="w">
|
||
</span><span class="nt">PrimaryPrefix</span><span class="o">/</span><span class="nt">Name</span><span class="w">
|
||
</span><span class="p">[</span><span class="na">@Image</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'assertTrue'</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="na">@Image</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'assertFalse'</span><span class="p">]</span><span class="w">
|
||
</span><span class="p">]</span><span class="w">
|
||
</span><span class="p">[</span><span class="nt">PrimarySuffix</span><span class="o">/</span><span class="nt">Arguments</span><span class="w">
|
||
</span><span class="o">/</span><span class="nt">ArgumentList</span><span class="o">/</span><span class="nt">Expression</span><span class="w">
|
||
</span><span class="o">/</span><span class="nt">EqualityExpression</span><span class="p">[</span><span class="nf">count</span><span class="p">(.</span><span class="o">//</span><span class="nt">NullLiteral</span><span class="p">)</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="m">0</span><span class="p">]]</span><span class="w">
|
||
</span><span class="p">[</span><span class="kn">ancestor::</span><span class="nt">ClassOrInterfaceDeclaration</span><span class="p">[</span><span class="o">//</span><span class="nt">ClassOrInterfaceType</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'junit.framework.TestCase'</span><span class="p">)]</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="o">//</span><span class="nt">MarkerAnnotation</span><span class="o">/</span><span class="nt">Name</span><span class="p">[</span><span class="w">
|
||
</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.Test'</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.Test'</span><span class="p">)</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.RepeatedTest'</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.TestFactory'</span><span class="p">)</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.api.TestTemplate'</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.junit.jupiter.params.ParameterizedTest'</span><span class="p">)</span><span class="w">
|
||
</span><span class="p">]</span><span class="w">
|
||
</span><span class="p">]]</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">FooTest</span> <span class="kd">extends</span> <span class="n">TestCase</span> <span class="o">{</span>
|
||
<span class="kt">void</span> <span class="nf">testCode</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="n">Object</span> <span class="n">a</span><span class="o">,</span> <span class="n">b</span><span class="o">;</span>
|
||
<span class="n">assertTrue</span><span class="o">(</span><span class="n">a</span> <span class="o">==</span> <span class="n">b</span><span class="o">);</span> <span class="c1">// bad usage</span>
|
||
<span class="n">assertSame</span><span class="o">(</span><span class="n">a</span><span class="o">,</span> <span class="n">b</span><span class="o">);</span> <span class="c1">// good usage</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule by referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/UseAssertSameInsteadOfAssertTrue"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="useasserttrueinsteadofassertequals">UseAssertTrueInsteadOfAssertEquals</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 5.0</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>When asserting a value is the same as a literal or Boxed boolean, use assertTrue/assertFalse, instead of assertEquals.</p>
|
||
|
||
<p><strong>This rule is defined by the following XPath expression:</strong></p>
|
||
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">PrimaryExpression</span><span class="p">[</span><span class="nt">PrimaryPrefix</span><span class="o">/</span><span class="nt">Name</span><span class="p">[</span><span class="na">@Image</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'assertEquals'</span><span class="p">]]</span><span class="w">
|
||
</span><span class="p">[</span><span class="w">
|
||
</span><span class="nt">PrimarySuffix</span><span class="o">/</span><span class="nt">Arguments</span><span class="o">/</span><span class="nt">ArgumentList</span><span class="o">/</span><span class="nt">Expression</span><span class="o">/</span><span class="nt">PrimaryExpression</span><span class="o">/</span><span class="nt">PrimaryPrefix</span><span class="o">/</span><span class="nt">Literal</span><span class="o">/</span><span class="nt">BooleanLiteral</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w">
|
||
</span><span class="nt">PrimarySuffix</span><span class="o">/</span><span class="nt">Arguments</span><span class="o">/</span><span class="nt">ArgumentList</span><span class="o">/</span><span class="nt">Expression</span><span class="o">/</span><span class="nt">PrimaryExpression</span><span class="o">/</span><span class="nt">PrimaryPrefix</span><span class="w">
|
||
</span><span class="o">/</span><span class="nt">Name</span><span class="p">[(</span><span class="na">@Image</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'Boolean.TRUE'</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="na">@Image</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'Boolean.FALSE'</span><span class="p">)]</span><span class="w">
|
||
</span><span class="p">]</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">MyTestCase</span> <span class="kd">extends</span> <span class="n">TestCase</span> <span class="o">{</span>
|
||
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">testMyCase</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="kt">boolean</span> <span class="n">myVar</span> <span class="o">=</span> <span class="kc">true</span><span class="o">;</span>
|
||
<span class="c1">// Ok</span>
|
||
<span class="n">assertTrue</span><span class="o">(</span><span class="s">"myVar is true"</span><span class="o">,</span> <span class="n">myVar</span><span class="o">);</span>
|
||
<span class="c1">// Bad</span>
|
||
<span class="n">assertEquals</span><span class="o">(</span><span class="s">"myVar is true"</span><span class="o">,</span> <span class="kc">true</span><span class="o">,</span> <span class="n">myVar</span><span class="o">);</span>
|
||
<span class="c1">// Bad</span>
|
||
<span class="n">assertEquals</span><span class="o">(</span><span class="s">"myVar is false"</span><span class="o">,</span> <span class="kc">false</span><span class="o">,</span> <span class="n">myVar</span><span class="o">);</span>
|
||
<span class="c1">// Bad</span>
|
||
<span class="n">assertEquals</span><span class="o">(</span><span class="s">"myVar is true"</span><span class="o">,</span> <span class="n">Boolean</span><span class="o">.</span><span class="na">TRUE</span><span class="o">,</span> <span class="n">myVar</span><span class="o">);</span>
|
||
<span class="c1">// Bad</span>
|
||
<span class="n">assertEquals</span><span class="o">(</span><span class="s">"myVar is false"</span><span class="o">,</span> <span class="n">Boolean</span><span class="o">.</span><span class="na">FALSE</span><span class="o">,</span> <span class="n">myVar</span><span class="o">);</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule by referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/bestpractices.xml/UseAssertTrueInsteadOfAssertEquals"</span> <span class="nt">/></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="n">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="n">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="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><strong>This rule is defined by the following XPath expression:</strong></p>
|
||
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">FormalParameters</span><span class="o">/</span><span class="nt">FormalParameter</span><span class="w">
|
||
</span><span class="p">[</span><span class="nf">position</span><span class="p">()</span><span class="err">=</span><span class="nf">last</span><span class="p">()]</span><span class="w">
|
||
</span><span class="p">[</span><span class="na">@Array</span><span class="err">=</span><span class="s1">'true'</span><span class="p">]</span><span class="w">
|
||
</span><span class="p">[</span><span class="na">@Varargs</span><span class="err">=</span><span class="s1">'false'</span><span class="p">]</span><span class="w">
|
||
</span><span class="p">[</span><span class="ow">not</span><span class="w"> </span><span class="p">(.</span><span class="o">/</span><span class="nt">Type</span><span class="o">/</span><span class="nt">ReferenceType</span><span class="p">[</span><span class="na">@Array</span><span class="err">=</span><span class="s1">'true'</span><span class="p">][</span><span class="nt">PrimitiveType</span><span class="p">[</span><span class="na">@Image</span><span class="err">=</span><span class="s1">'byte'</span><span class="p">]])]</span><span class="w">
|
||
</span><span class="p">[</span><span class="ow">not</span><span class="w"> </span><span class="p">(.</span><span class="o">/</span><span class="nt">Type</span><span class="o">/</span><span class="nt">ReferenceType</span><span class="p">[</span><span class="nt">ClassOrInterfaceType</span><span class="p">[</span><span class="na">@Image</span><span class="err">=</span><span class="s1">'Byte'</span><span class="p">]])]</span><span class="w">
|
||
</span><span class="p">[</span><span class="ow">not</span><span class="w"> </span><span class="p">(.</span><span class="o">/</span><span class="nt">Type</span><span class="o">/</span><span class="nt">PrimitiveType</span><span class="p">[</span><span class="na">@Image</span><span class="err">=</span><span class="s1">'byte'</span><span class="p">])]</span><span class="w">
|
||
</span><span class="p">[</span><span class="ow">not</span><span class="w"> </span><span class="p">(</span><span class="kn">ancestor::</span><span class="nt">MethodDeclaration</span><span class="o">/</span><span class="kn">preceding-sibling::</span><span class="nt">Annotation</span><span class="err">/*</span><span class="o">/</span><span class="nt">Name</span><span class="p">[</span><span class="na">@Image</span><span class="err">=</span><span class="s1">'Override'</span><span class="p">])]</span><span class="w">
|
||
</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="w">
|
||
</span><span class="kn">ancestor::</span><span class="nt">MethodDeclaration</span><span class="w">
|
||
</span><span class="p">[</span><span class="na">@Public</span><span class="err">=</span><span class="s1">'true'</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="na">@Static</span><span class="err">=</span><span class="s1">'true'</span><span class="p">]</span><span class="w">
|
||
</span><span class="p">[</span><span class="kn">child::</span><span class="nt">ResultType</span><span class="p">[</span><span class="na">@Void</span><span class="err">=</span><span class="s1">'true'</span><span class="p">]]</span><span class="w"> </span><span class="ow">and</span><span class="w">
|
||
</span><span class="kn">ancestor::</span><span class="nt">MethodDeclarator</span><span class="p">[</span><span class="na">@Image</span><span class="err">=</span><span class="s1">'main'</span><span class="p">]</span><span class="w"> </span><span class="ow">and</span><span class="w">
|
||
</span><span class="p">..[</span><span class="na">@ParameterCount</span><span class="err">=</span><span class="s1">'1'</span><span class="p">]</span><span class="w"> </span><span class="ow">and</span><span class="w">
|
||
</span><span class="p">.</span><span class="o">/</span><span class="nt">Type</span><span class="o">/</span><span class="nt">ReferenceType</span><span class="p">[</span><span class="nt">ClassOrInterfaceType</span><span class="p">[</span><span class="na">@Image</span><span class="err">=</span><span class="s1">'String'</span><span class="p">]]</span><span class="w">
|
||
</span><span class="p">)]</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
|
||
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">foo</span><span class="o">(</span><span class="n">String</span> <span class="n">s</span><span class="o">,</span> <span class="n">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="n">String</span> <span class="n">s</span><span class="o">,</span> <span class="n">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>
|
||
|
||
|
||
|
||
<div class="tags">
|
||
|
||
</div>
|
||
|
||
|
||
|
||
</div>
|
||
|
||
<hr class="shaded"/>
|
||
|
||
<footer>
|
||
<div class="row">
|
||
<div class="col-lg-12 footer">
|
||
©2018 PMD Open Source Project. All rights reserved. <br />
|
||
Site last generated: Nov 11, 2018 <br />
|
||
<p><img src="images/pmd-logo-small.png" alt="Company logo"/></p>
|
||
</div>
|
||
</div>
|
||
</footer>
|
||
|
||
|
||
</div>
|
||
<!-- /.row -->
|
||
</div>
|
||
<!-- /.container -->
|
||
</div>
|
||
</div>
|
||
</body>
|
||
|
||
</html>
|