forked from phoedos/pmd
2876 lines
145 KiB
HTML
2876 lines
145 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 that flag suboptimal code.">
|
||
<meta name="keywords" content=" Performance, AddEmptyString, AppendCharacterWithChar, AvoidArrayLoops, AvoidCalendarDateCreation, AvoidFileStream, AvoidInstantiatingObjectsInLoops, BigIntegerInstantiation, ConsecutiveAppendsShouldReuse, ConsecutiveLiteralAppends, InefficientEmptyStringCheck, InefficientStringBuffering, InsufficientStringBufferDeclaration, OptimizableToArrayCall, RedundantFieldInitializer, StringInstantiation, StringToString, TooFewBranchesForASwitchStatement, UseArrayListInsteadOfVector, UseArraysAsList, UseIndexOfChar, UseIOStreamsWithApacheCommonsFileItem, UselessStringValueOf, UseStringBufferForStringAppends, UseStringBufferLength">
|
||
<title>Performance | PMD Source Code Analyzer</title>
|
||
|
||
|
||
<link rel="stylesheet" type="text/css" href="assets/fontawesome-free-5.15.4-web/css/all.min.css">
|
||
<link rel="stylesheet" type="text/css" href="assets/bootstrap-4.5.2-dist/css/bootstrap.min.css">
|
||
|
||
<link rel="stylesheet" type="text/css" href="css/syntax.css">
|
||
<link rel="stylesheet" type="text/css" href="css/modern-business.css">
|
||
<link rel="stylesheet" type="text/css" href="css/customstyles.css">
|
||
<link rel="stylesheet" type="text/css" href="css/theme-green.css">
|
||
<link rel="stylesheet" type="text/css" href="css/pmd-customstyles.css">
|
||
|
||
<link rel="shortcut icon" href="images/logo/favicon.ico" type="image/x-icon">
|
||
<link rel="icon" href="images/logo/favicon.ico" type="image/x-icon">
|
||
|
||
<link rel="alternate" type="application/rss+xml" title="" href="feed.xml">
|
||
|
||
</head>
|
||
<body>
|
||
<!-- Content is offset by the height of the topnav bar. -->
|
||
<!-- There's already a padding-top rule in modern-business.css, but it apparently doesn't work on Firefox 60 and Chrome 67 -->
|
||
<div id="topbar-content-offset">
|
||
<!-- Navigation -->
|
||
<nav class="navbar navbar-expand-lg fixed-top navbar-dark">
|
||
<div class="container topnavlinks">
|
||
<a class="navbar-brand fas fa-home fa-lg" href="index.html"> <span class="projectTitle"> PMD Source Code Analyzer Project</span></a>
|
||
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
|
||
<span class="navbar-toggler-icon"></span>
|
||
</button>
|
||
|
||
<div class="collapse navbar-collapse" id="navbarSupportedContent">
|
||
<ul class="navbar-nav mr-auto mt-2 mt-lg-0"></ul>
|
||
<ul class="navbar-nav">
|
||
<!-- toggle sidebar button -->
|
||
<li class="nav-item"><a id="tg-sb-link" class="nav-link" href="#"><i id="tg-sb-icon" class="fas fa-toggle-on"></i> Nav</a></li>
|
||
<!-- entries without drop-downs appear here -->
|
||
|
||
|
||
|
||
<li class="nav-item"><a class="nav-link" href="https://github.com/pmd/pmd/releases/latest" target="_blank">Download</a></li>
|
||
|
||
|
||
|
||
<li class="nav-item"><a class="nav-link" href="https://github.com/pmd/pmd" target="_blank">Fork us on github</a></li>
|
||
|
||
|
||
|
||
<!-- entries with drop-downs appear here -->
|
||
<!-- conditional logic to control which topnav appears for the audience defined in the configuration file.-->
|
||
|
||
</ul>
|
||
<form class="form-inline my-2 my-lg-0">
|
||
<input class="form-control mr-sm-2" type="search" placeholder="search..." id="search-input">
|
||
<ul id="results-container"></ul>
|
||
</form>
|
||
</div>
|
||
</div>
|
||
</nav>
|
||
|
||
<!-- Page Content -->
|
||
<div class="container-toc-wrapper">
|
||
<div class="container">
|
||
<div class="col-lg-12"> </div>
|
||
<!-- Content Row -->
|
||
<div class="row">
|
||
|
||
|
||
<!-- Sidebar Column -->
|
||
<div class="col-md-3" id="tg-sb-sidebar">
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<ul id="mysidebar" class="nav">
|
||
<li class="sidebarTitle">PMD 7.0.0-SNAPSHOT</li>
|
||
<div class="sidebarTitleDate">Release date: ??-?????-2023</div>
|
||
|
||
|
||
|
||
|
||
|
||
<li>
|
||
|
||
<a href="#">About</a>
|
||
|
||
<ul>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="index.html">Home</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_release_notes.html">Release notes</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_release_notes_pmd7.html">Release notes (PMD 7)</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_about_help.html">Getting help</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
<li>
|
||
|
||
<a href="#">User Documentation</a>
|
||
|
||
<ul>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_migrating_to_pmd7.html">Migration Guide for PMD 7</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_installation.html">Installation and basic CLI usage</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_making_rulesets.html">Making rulesets</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_configuring_rules.html">Configuring rules</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_best_practices.html">Best practices</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_suppressing_warnings.html">Suppressing warnings</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_incremental_analysis.html">Incremental analysis</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_cli_reference.html">PMD CLI reference</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_report_formats.html">PMD Report formats</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_3rdpartyrulesets.html">3rd party rulesets</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">CPD reference</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_cpd.html">Copy-paste detection</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_cpd_report_formats.html">CPD Report formats</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">Extending PMD</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_extending_writing_rules_intro.html">Introduction to writing rules</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_extending_your_first_rule.html">Your first rule</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_extending_writing_xpath_rules.html">XPath rules</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_extending_writing_java_rules.html">Java rules</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_extending_designer_reference.html">Rule designer reference</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_extending_defining_properties.html">Defining rule properties</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_extending_rule_guidelines.html">Rule guidelines</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_extending_testing.html">Testing your rules</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_extending_ast_dump.html">Creating (XML) dump of the AST</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">Tools / Integrations</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_tools_maven.html">Maven PMD Plugin</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_tools_gradle.html">Gradle</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_tools_ant.html">Ant</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_tools_java_api.html">PMD Java API</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_tools_bld.html">bld PMD Extension</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_tools_ci.html">CI integrations</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_userdocs_tools.html">Other Tools / Integrations</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
<li>
|
||
|
||
<a href="#">Rule Reference</a>
|
||
|
||
<ul>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">Apex Rules</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_apex.html">Index</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_apex_bestpractices.html">Best Practices</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_apex_codestyle.html">Code Style</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_apex_design.html">Design</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_apex_documentation.html">Documentation</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_apex_errorprone.html">Error Prone</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_apex_performance.html">Performance</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_apex_security.html">Security</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">HTML Rules</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_html.html">Index</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_html_bestpractices.html">Best Practices</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">Java Rules</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_java.html">Index</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_java_bestpractices.html">Best Practices</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><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 class="active"><a href="pmd_rules_java_performance.html">Performance</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_java_security.html">Security</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">Java Server Pages Rules</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_jsp.html">Index</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_jsp_bestpractices.html">Best Practices</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_jsp_codestyle.html">Code Style</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_jsp_design.html">Design</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_jsp_errorprone.html">Error Prone</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_jsp_security.html">Security</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">JavaScript Rules</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_ecmascript.html">Index</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_ecmascript_bestpractices.html">Best Practices</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_ecmascript_codestyle.html">Code Style</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_ecmascript_errorprone.html">Error Prone</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">Kotlin Rules</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_kotlin.html">Index</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_kotlin_bestpractices.html">Best Practices</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_kotlin_errorprone.html">Error Prone</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">Maven POM Rules</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_pom.html">Index</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_pom_errorprone.html">Error Prone</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">Modelica Rules</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_modelica.html">Index</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_modelica_bestpractices.html">Best Practices</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">PLSQL Rules</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_plsql.html">Index</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_plsql_bestpractices.html">Best Practices</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_plsql_codestyle.html">Code Style</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_plsql_design.html">Design</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_plsql_errorprone.html">Error Prone</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">Salesforce Visualforce Rules</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_vf.html">Index</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_vf_security.html">Security</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">Scala Rules</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_scala.html">Index</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">Swift Rules</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_swift.html">Index</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_swift_bestpractices.html">Best Practices</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_swift_errorprone.html">Error Prone</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">Velocity Template Language (VTL) Rules</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_vm.html">Index</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_vm_bestpractices.html">Best Practices</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_vm_design.html">Design</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_vm_errorprone.html">Error Prone</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">WSDL Rules</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_wsdl.html">Index</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">XML Rules</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_xml.html">Index</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_xml_bestpractices.html">Best Practices</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_xml_errorprone.html">Error Prone</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">XSL Rules</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_xsl.html">Index</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_xsl_codestyle.html">Code Style</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_rules_xsl_performance.html">Performance</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
<li>
|
||
|
||
<a href="#">Language-Specific Documentation</a>
|
||
|
||
<ul>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_index.html">Overview</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_configuration.html">Language configuration</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_apex.html">Apex</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_cpp.html">C/C++</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_cs.html">C#</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_coco.html">Coco</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_dart.html">Dart</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_fortran.html">Fortran</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_gherkin.html">Gherkin</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_go.html">Go</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_html.html">HTML</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_java.html">Java</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_js_ts.html">JavaScript / TypeScript</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_jsp.html">JSP</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_julia.html">Julia</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_kotlin.html">Kotlin</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_lua.html">Lua</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_matlab.html">Matlab</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_modelica.html">Modelica</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_objectivec.html">Objective-C</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_perl.html">Perl</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_php.html">PHP</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_plsql.html">PLSQL</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_python.html">Python</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_ruby.html">Ruby</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_scala.html">Scala</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_swift.html">Swift</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_tsql.html">T-SQL</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_visualforce.html">Visualforce</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_vm.html">Velocity Template Language (VTL)</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_languages_xml.html">XML and XML dialects</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
<li>
|
||
|
||
<a href="#">Developer Documentation</a>
|
||
|
||
<ul>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_devdocs_development.html">Developer resources</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_devdocs_building.html">Building PMD from source</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="https://github.com/pmd/pmd/blob/master/CONTRIBUTING.md" target="_blank">Contributing</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_devdocs_writing_documentation.html">Writing documentation</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_devdocs_roadmap.html">Roadmap</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_devdocs_how_pmd_works.html">How PMD works</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_devdocs_pmdtester.html">Pmdtester</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_devdocs_rule_deprecation_policy.html">Rule Deprecation Policy</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">Major contributions</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_devdocs_major_rule_guidelines.html">Rule Guidelines</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_devdocs_major_adding_new_language_javacc.html">Adding a new language (JavaCC)</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_devdocs_major_adding_new_language_antlr.html">Adding a new language (ANTLR)</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_devdocs_major_adding_new_cpd_language.html">Adding a new CPD language</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">Experimental features</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="tag_experimental.html">List of experimental Features</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
<li>
|
||
|
||
<a href="#">Project documentation</a>
|
||
|
||
<ul>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">Trivia about PMD</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_projectdocs_trivia_news.html">PMD in the press</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_projectdocs_trivia_products.html">Products & books related to PMD</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_projectdocs_trivia_similarprojects.html">Similar projects</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_projectdocs_trivia_meaning.html">What does 'PMD' mean?</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_projectdocs_logo.html">Logo</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_projectdocs_faq.html">FAQ</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="license.html">License</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_projectdocs_credits.html">Credits</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_release_notes_old.html">Old release notes</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_projectdocs_decisions.html">Decisions</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="subfolders">
|
||
<a href="#">Project management</a>
|
||
<ul>
|
||
|
||
|
||
|
||
<li><a href="pmd_projectdocs_committers_infrastructure.html">Infrastructure</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_projectdocs_committers_releasing.html">Release process</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_projectdocs_committers_merging_pull_requests.html">Merging pull requests</a></li>
|
||
|
||
|
||
|
||
|
||
|
||
<li><a href="pmd_projectdocs_committers_main_landing_page.html">Main Landing page</a></li>
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
</ul>
|
||
</li>
|
||
|
||
|
||
|
||
</ul>
|
||
|
||
</div>
|
||
|
||
|
||
|
||
<!-- Content Column -->
|
||
<div class="col-md-9" id="tg-sb-content">
|
||
<header>
|
||
<div class="row">
|
||
<div class="col-lg-12">
|
||
<a href="./" role="button"
|
||
><i class="fa fa-home fa-lg"></i
|
||
></a>
|
||
» Performance
|
||
<a
|
||
target="_blank"
|
||
href="https://github.com/pmd/pmd/blob/master/docs/../pmd-java/src/main/resources/category/java/performance.xml"
|
||
class="float-right"
|
||
role="button"
|
||
><i class="fab fa-github fa-lg"></i> Edit on GitHub</a
|
||
>
|
||
|
||
</div>
|
||
</div>
|
||
|
||
<hr />
|
||
</header>
|
||
|
||
|
||
<div class="post-header">
|
||
<h1 class="post-title-main">Performance</h1>
|
||
</div>
|
||
|
||
<div class="post-content" data-github-edit-url="https://github.com/pmd/pmd/blob/master/docs/../pmd-java/src/main/resources/category/java/performance.xml">
|
||
|
||
|
||
<div class="summary">Rules that flag suboptimal code.</div>
|
||
|
||
<details id="inline-toc-details">
|
||
<summary>Table of Contents</summary>
|
||
<div id="inline-toc"><!-- empty, move TOC here when screen size too small --></div>
|
||
</details>
|
||
|
||
<!-- DO NOT EDIT THIS FILE. This file is generated from file ../pmd-java/src/main/resources/category/java/performance.xml. -->
|
||
<h2 id="addemptystring">AddEmptyString</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 4.0</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>The conversion of literals to strings by concatenating them with empty strings is inefficient.
|
||
It is much better to use one of the type-specific <code class="language-plaintext highlighter-rouge">toString()</code> methods instead or <code class="language-plaintext highlighter-rouge">String.valueOf()</code>.</p>
|
||
|
||
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/performance/AddEmptyStringRule.java">net.sourceforge.pmd.lang.java.rule.performance.AddEmptyStringRule</a></p>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nc">String</span> <span class="n">s</span> <span class="o">=</span> <span class="s">""</span> <span class="o">+</span> <span class="mi">123</span><span class="o">;</span> <span class="c1">// inefficient</span>
|
||
<span class="nc">String</span> <span class="n">t</span> <span class="o">=</span> <span class="nc">Integer</span><span class="o">.</span><span class="na">toString</span><span class="o">(</span><span class="mi">456</span><span class="o">);</span> <span class="c1">// preferred approach</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>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/performance.xml/AddEmptyString"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="appendcharacterwithchar">AppendCharacterWithChar</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 3.5</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Avoid concatenating characters as strings in StringBuffer/StringBuilder.append methods.</p>
|
||
|
||
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/performance/AppendCharacterWithCharRule.java">net.sourceforge.pmd.lang.java.rule.performance.AppendCharacterWithCharRule</a></p>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nc">StringBuffer</span> <span class="n">sb</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">StringBuffer</span><span class="o">();</span>
|
||
<span class="n">sb</span><span class="o">.</span><span class="na">append</span><span class="o">(</span><span class="s">"a"</span><span class="o">);</span> <span class="c1">// avoid this</span>
|
||
|
||
<span class="nc">StringBuffer</span> <span class="n">sb</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">StringBuffer</span><span class="o">();</span>
|
||
<span class="n">sb</span><span class="o">.</span><span class="na">append</span><span class="o">(</span><span class="sc">'a'</span><span class="o">);</span> <span class="c1">// use this instead</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>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/performance.xml/AppendCharacterWithChar"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="avoidarrayloops">AvoidArrayLoops</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 3.5</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Instead of manually copying data between two arrays, use the more efficient <code class="language-plaintext highlighter-rouge">Arrays.copyOf</code>
|
||
or <code class="language-plaintext highlighter-rouge">System.arraycopy</code> method instead.</p>
|
||
|
||
<p>To copy only part of the array, use <code class="language-plaintext highlighter-rouge">Arrays.copyOfRange</code> or <code class="language-plaintext highlighter-rouge">System.arraycopy</code>.</p>
|
||
|
||
<p>If you want to copy/move elements inside the <em>same</em> array (e.g. shift the elements), use <code class="language-plaintext highlighter-rouge">System.arraycopy</code>.</p>
|
||
|
||
<p><strong>This rule is defined by the following XPath expression:</strong></p>
|
||
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="err">//</span><span class="p">(</span><span class="nt">ForStatement</span><span class="p">[</span><span class="nt">ForUpdate</span><span class="err">//</span><span class="p">(</span><span class="nt">UnaryExpression</span><span class="p">[</span><span class="na">@Operator</span><span class="err">=</span><span class="p">(</span><span class="s1">'++'</span><span class="p">,</span><span class="s1">'--'</span><span class="p">)]</span><span class="w"> </span><span class="p">|</span><span class="w"> </span><span class="nt">AssignmentExpression</span><span class="p">[</span><span class="na">@Operator</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="p">(</span><span class="s1">'+='</span><span class="p">,</span><span class="w"> </span><span class="s1">'-='</span><span class="p">)][</span><span class="nt">NumericLiteral</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">'1'</span><span class="p">]])]</span><span class="w">
|
||
</span><span class="p">|</span><span class="w"> </span><span class="nt">WhileStatement</span><span class="w"> </span><span class="p">|</span><span class="w"> </span><span class="nt">DoStatement</span><span class="p">)</span><span class="w">
|
||
</span><span class="p">[</span><span class="ow">not</span><span class="p">(.</span><span class="o">//</span><span class="nt">ContinueStatement</span><span class="p">)]</span><span class="w">
|
||
</span><span class="p">[</span><span class="ow">not</span><span class="p">(.</span><span class="o">//</span><span class="nt">BreakStatement</span><span class="p">)]</span><span class="w">
|
||
</span><span class="p">[</span><span class="ow">not</span><span class="p">(.</span><span class="o">//</span><span class="nt">ThrowStatement</span><span class="p">)]</span><span class="w">
|
||
</span><span class="p">[</span><span class="ow">not</span><span class="p">(.</span><span class="o">//</span><span class="nt">ReturnStatement</span><span class="p">)]</span><span class="w">
|
||
</span><span class="p">[</span><span class="nf">count</span><span class="p">(</span><span class="nt">Block</span><span class="o">//</span><span class="nt">AssignmentExpression</span><span class="p">[</span><span class="na">@Operator</span><span class="err">=</span><span class="s1">'='</span><span class="p">]</span><span class="w">
|
||
</span><span class="c">(: no nested arrays or method calls as array index :)</span><span class="w">
|
||
</span><span class="p">[</span><span class="nf">count</span><span class="p">(</span><span class="nt">ArrayAccess</span><span class="p">[</span><span class="ow">not</span><span class="p">(.</span><span class="o">//</span><span class="nt">ArrayAccess</span><span class="p">)]</span><span class="w">
|
||
</span><span class="p">[</span><span class="ow">not</span><span class="p">(.</span><span class="o">//</span><span class="nt">MethodCall</span><span class="p">)])</span><span class="err">=</span><span class="m">2</span><span class="p">]</span><span class="w">
|
||
</span><span class="c">(: array access indexes must be same (excluding constants) :)</span><span class="w">
|
||
</span><span class="p">[</span><span class="nf">deep-equal</span><span class="p">(</span><span class="w">
|
||
</span><span class="nf">sort</span><span class="p">(</span><span class="nf">distinct-values</span><span class="p">(</span><span class="nt">ArrayAccess</span><span class="p">[</span><span class="m">1</span><span class="p">]</span><span class="err">/</span><span class="p">(</span><span class="nt">VariableAccess</span><span class="p">[</span><span class="m">2</span><span class="p">]|</span><span class="nt">InfixExpression</span><span class="o">//</span><span class="nt">VariableAccess</span><span class="p">)</span><span class="w">
|
||
</span><span class="c">(: don't consider array length accesses :)</span><span class="w">
|
||
</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="kn">parent::</span><span class="nt">FieldAccess</span><span class="p">[</span><span class="na">@Name</span><span class="err">=</span><span class="s1">'length'</span><span class="p">])]</span><span class="w">
|
||
</span><span class="p">[</span><span class="w">
|
||
</span><span class="c">(: exclude referenced constants :)</span><span class="w">
|
||
</span><span class="ow">not</span><span class="p">(</span><span class="na">@Name</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="p">(</span><span class="kn">ancestor::</span><span class="nt">MethodDeclaration</span><span class="p">|</span><span class="o">//</span><span class="nt">FieldDeclaration</span><span class="p">)</span><span class="o">//</span><span class="nt">VariableDeclarator</span><span class="p">[</span><span class="nt">NumericLiteral</span><span class="p">][</span><span class="ow">not</span><span class="p">(..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="o">/</span><span class="kn">parent::</span><span class="nt">ForInit</span><span class="p">)]</span><span class="o">/</span><span class="nt">VariableId</span><span class="err">/</span><span class="na">@Name</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w">
|
||
</span><span class="c">(: include loop variable :)</span><span class="w">
|
||
</span><span class="na">@Name</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="kn">ancestor::</span><span class="nt">ForStatement</span><span class="o">/</span><span class="nt">ForInit</span><span class="o">/</span><span class="nt">LocalVariableDeclaration</span><span class="o">/</span><span class="nt">VariableDeclarator</span><span class="o">/</span><span class="nt">VariableId</span><span class="err">/</span><span class="na">@Name</span><span class="w">
|
||
</span><span class="p">]</span><span class="w">
|
||
</span><span class="err">/</span><span class="na">@Name</span><span class="p">)),</span><span class="w">
|
||
</span><span class="nf">sort</span><span class="p">(</span><span class="nf">distinct-values</span><span class="p">(</span><span class="nt">ArrayAccess</span><span class="p">[</span><span class="m">2</span><span class="p">]</span><span class="err">/</span><span class="p">(</span><span class="nt">VariableAccess</span><span class="p">[</span><span class="m">2</span><span class="p">]|</span><span class="nt">InfixExpression</span><span class="o">//</span><span class="nt">VariableAccess</span><span class="p">)</span><span class="w">
|
||
</span><span class="c">(: don't consider array length accesses :)</span><span class="w">
|
||
</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="kn">parent::</span><span class="nt">FieldAccess</span><span class="p">[</span><span class="na">@Name</span><span class="err">=</span><span class="s1">'length'</span><span class="p">])]</span><span class="w">
|
||
</span><span class="p">[</span><span class="w">
|
||
</span><span class="c">(: exclude referenced constants :)</span><span class="w">
|
||
</span><span class="ow">not</span><span class="p">(</span><span class="na">@Name</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="p">(</span><span class="kn">ancestor::</span><span class="nt">MethodDeclaration</span><span class="p">|</span><span class="o">//</span><span class="nt">FieldDeclaration</span><span class="p">)</span><span class="o">//</span><span class="nt">VariableDeclarator</span><span class="p">[</span><span class="nt">NumericLiteral</span><span class="p">][</span><span class="ow">not</span><span class="p">(..</span><span class="err">/</span><span class="p">..</span><span class="err">/</span><span class="p">..</span><span class="o">/</span><span class="kn">parent::</span><span class="nt">ForInit</span><span class="p">)]</span><span class="o">/</span><span class="nt">VariableId</span><span class="err">/</span><span class="na">@Name</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w">
|
||
</span><span class="c">(: include loop variable :)</span><span class="w">
|
||
</span><span class="na">@Name</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="kn">ancestor::</span><span class="nt">ForStatement</span><span class="o">/</span><span class="nt">ForInit</span><span class="o">/</span><span class="nt">LocalVariableDeclaration</span><span class="o">/</span><span class="nt">VariableDeclarator</span><span class="o">/</span><span class="nt">VariableId</span><span class="err">/</span><span class="na">@Name</span><span class="w">
|
||
</span><span class="p">]</span><span class="w">
|
||
</span><span class="err">/</span><span class="na">@Name</span><span class="p">))</span><span class="w">
|
||
</span><span class="p">)]</span><span class="w">
|
||
</span><span class="p">)</span><span class="err">=</span><span class="m">1</span><span class="p">]</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">class</span> <span class="nc">Scratch</span> <span class="o">{</span>
|
||
<span class="kt">void</span> <span class="nf">copy_a_to_b</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="kt">int</span><span class="o">[]</span> <span class="n">a</span> <span class="o">=</span> <span class="k">new</span> <span class="kt">int</span><span class="o">[</span><span class="mi">10</span><span class="o">];</span>
|
||
<span class="kt">int</span><span class="o">[]</span> <span class="n">b</span> <span class="o">=</span> <span class="k">new</span> <span class="kt">int</span><span class="o">[</span><span class="mi">10</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">a</span><span class="o">.</span><span class="na">length</span><span class="o">;</span> <span class="n">i</span><span class="o">++)</span> <span class="o">{</span>
|
||
<span class="n">b</span><span class="o">[</span><span class="n">i</span><span class="o">]</span> <span class="o">=</span> <span class="n">a</span><span class="o">[</span><span class="n">i</span><span class="o">];</span>
|
||
<span class="o">}</span>
|
||
<span class="c1">// equivalent</span>
|
||
<span class="n">b</span> <span class="o">=</span> <span class="nc">Arrays</span><span class="o">.</span><span class="na">copyOf</span><span class="o">(</span><span class="n">a</span><span class="o">,</span> <span class="n">a</span><span class="o">.</span><span class="na">length</span><span class="o">);</span>
|
||
<span class="c1">// equivalent</span>
|
||
<span class="nc">System</span><span class="o">.</span><span class="na">arraycopy</span><span class="o">(</span><span class="n">a</span><span class="o">,</span> <span class="mi">0</span><span class="o">,</span> <span class="n">b</span><span class="o">,</span> <span class="mi">0</span><span class="o">,</span> <span class="n">a</span><span class="o">.</span><span class="na">length</span><span class="o">);</span>
|
||
|
||
<span class="kt">int</span><span class="o">[]</span> <span class="n">c</span> <span class="o">=</span> <span class="k">new</span> <span class="kt">int</span><span class="o">[</span><span class="mi">10</span><span class="o">];</span>
|
||
<span class="c1">// this will not trigger the rule</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">c</span><span class="o">.</span><span class="na">length</span><span class="o">;</span> <span class="n">i</span><span class="o">++)</span> <span class="o">{</span>
|
||
<span class="n">b</span><span class="o">[</span><span class="n">i</span><span class="o">]</span> <span class="o">=</span> <span class="n">a</span><span class="o">[</span><span class="n">c</span><span class="o">[</span><span class="n">i</span><span class="o">]];</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">class</span> <span class="nc">Scratch</span> <span class="o">{</span>
|
||
<span class="kt">void</span> <span class="nf">shift_left</span><span class="o">(</span><span class="kt">int</span><span class="o">[]</span> <span class="n">a</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">a</span><span class="o">.</span><span class="na">length</span> <span class="o">-</span> <span class="mi">1</span><span class="o">;</span> <span class="n">i</span><span class="o">++)</span> <span class="o">{</span>
|
||
<span class="n">a</span><span class="o">[</span><span class="n">i</span><span class="o">]</span> <span class="o">=</span> <span class="n">a</span><span class="o">[</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="o">];</span>
|
||
<span class="o">}</span>
|
||
<span class="c1">// equivalent</span>
|
||
<span class="nc">System</span><span class="o">.</span><span class="na">arraycopy</span><span class="o">(</span><span class="n">a</span><span class="o">,</span> <span class="mi">1</span><span class="o">,</span> <span class="n">a</span><span class="o">,</span> <span class="mi">0</span><span class="o">,</span> <span class="n">a</span><span class="o">.</span><span class="na">length</span> <span class="o">-</span> <span class="mi">1</span><span class="o">);</span>
|
||
<span class="o">}</span>
|
||
<span class="kt">void</span> <span class="nf">shift_right</span><span class="o">(</span><span class="kt">int</span><span class="o">[]</span> <span class="n">a</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="n">a</span><span class="o">.</span><span class="na">length</span> <span class="o">-</span> <span class="mi">1</span><span class="o">;</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="o">{</span>
|
||
<span class="n">a</span><span class="o">[</span><span class="n">i</span><span class="o">]</span> <span class="o">=</span> <span class="n">a</span><span class="o">[</span><span class="n">i</span> <span class="o">-</span> <span class="mi">1</span><span class="o">];</span>
|
||
<span class="o">}</span>
|
||
<span class="c1">// equivalent</span>
|
||
<span class="nc">System</span><span class="o">.</span><span class="na">arraycopy</span><span class="o">(</span><span class="n">a</span><span class="o">,</span> <span class="mi">0</span><span class="o">,</span> <span class="n">a</span><span class="o">,</span> <span class="mi">1</span><span class="o">,</span> <span class="n">a</span><span class="o">.</span><span class="na">length</span> <span class="o">-</span> <span class="mi">1</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/performance.xml/AvoidArrayLoops"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="avoidcalendardatecreation">AvoidCalendarDateCreation</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 6.25.0</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Problem: <code class="language-plaintext highlighter-rouge">java.util.Calendar</code> is a heavyweight object and expensive to create. It should only be used, if
|
||
calendar calculations are needed.</p>
|
||
|
||
<p>Solution: Use <code class="language-plaintext highlighter-rouge">new Date()</code>, Java 8+ <code class="language-plaintext highlighter-rouge">java.time.LocalDateTime.now()</code> or <code class="language-plaintext highlighter-rouge">ZonedDateTime.now()</code>.</p>
|
||
|
||
<p><strong>This rule is defined by the following XPath expression:</strong></p>
|
||
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">MethodCall</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">matchesSig</span><span class="p">(</span><span class="s2">"java.util.Calendar#getTime()"</span><span class="p">)</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">matchesSig</span><span class="p">(</span><span class="s2">"java.util.Calendar#getTimeInMillis()"</span><span class="p">)]</span><span class="w">
|
||
</span><span class="p">[</span><span class="o">*</span><span class="p">[</span><span class="m">1</span><span class="p">][</span><span class="nf">local-name</span><span class="p">()</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="p">(</span><span class="s1">'MethodCall'</span><span class="p">,</span><span class="w"> </span><span class="s1">'ConstructorCall'</span><span class="p">)]</span><span class="w">
|
||
</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">matchesSig</span><span class="p">(</span><span class="s2">"java.util.Calendar#getInstance()"</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">matchesSig</span><span class="p">(</span><span class="s2">"java.util.GregorianCalendar#getInstance()"</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">matchesSig</span><span class="p">(</span><span class="s2">"java.util.GregorianCalendar#new()"</span><span class="p">)]</span><span class="w">
|
||
</span><span class="p">]</span><span class="w">
|
||
</span><span class="p">|</span><span class="w">
|
||
</span><span class="o">//</span><span class="nt">MethodCall</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">matchesSig</span><span class="p">(</span><span class="s2">"java.util.Calendar#getTime()"</span><span class="p">)</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">matchesSig</span><span class="p">(</span><span class="s2">"java.util.Calendar#getTimeInMillis()"</span><span class="p">)]</span><span class="w">
|
||
</span><span class="p">[</span><span class="o">*</span><span class="p">[</span><span class="m">1</span><span class="p">][</span><span class="nf">local-name</span><span class="p">()</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'VariableAccess'</span><span class="p">]]</span><span class="w">
|
||
</span><span class="c">(: ignore if .set* or .add or .clear or .roll is called on the variable :)</span><span class="w">
|
||
</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="nt">VariableAccess</span><span class="err">/</span><span class="na">@Name</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="kn">ancestor::</span><span class="nt">Block</span><span class="o">//</span><span class="nt">MethodCall</span><span class="p">[</span><span class="nf">starts-with</span><span class="p">(</span><span class="na">@MethodName</span><span class="p">,</span><span class="w"> </span><span class="s2">"set"</span><span class="p">)</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="na">@MethodName</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="p">(</span><span class="s2">"add"</span><span class="p">,</span><span class="w"> </span><span class="s2">"clear"</span><span class="p">,</span><span class="w"> </span><span class="s2">"roll"</span><span class="p">)]</span><span class="o">/</span><span class="nt">VariableAccess</span><span class="err">/</span><span class="na">@Name</span><span class="p">)]</span><span class="w">
|
||
</span><span class="c">(: variable must be initialized with getInstance :)</span><span class="w">
|
||
</span><span class="p">[</span><span class="nt">VariableAccess</span><span class="err">/</span><span class="na">@Name</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="kn">ancestor::</span><span class="nt">Block</span><span class="o">//</span><span class="nt">LocalVariableDeclaration</span><span class="o">/</span><span class="nt">VariableDeclarator</span><span class="p">[</span><span class="w">
|
||
</span><span class="p">(</span><span class="nt">MethodCall</span><span class="w"> </span><span class="p">|</span><span class="w"> </span><span class="nt">ConstructorCall</span><span class="p">)</span><span class="w">
|
||
</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">matchesSig</span><span class="p">(</span><span class="s2">"java.util.Calendar#getInstance()"</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">matchesSig</span><span class="p">(</span><span class="s2">"java.util.GregorianCalendar#getInstance()"</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">matchesSig</span><span class="p">(</span><span class="s2">"java.util.GregorianCalendar#new()"</span><span class="p">)]</span><span class="w">
|
||
</span><span class="p">]</span><span class="o">/</span><span class="nt">VariableId</span><span class="err">/</span><span class="na">@Name</span><span class="p">]</span><span class="w">
|
||
</span><span class="p">|</span><span class="w">
|
||
</span><span class="o">//</span><span class="nt">ConstructorCall</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s2">"org.joda.time.DateTime"</span><span class="p">)</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s2">"org.joda.time.LocalDateTime"</span><span class="p">)]</span><span class="w">
|
||
</span><span class="p">[</span><span class="nt">ArgumentList</span><span class="p">[(</span><span class="nt">MethodCall</span><span class="w"> </span><span class="p">|</span><span class="w"> </span><span class="nt">ConstructorCall</span><span class="p">)</span><span class="w">
|
||
</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">matchesSig</span><span class="p">(</span><span class="s2">"java.util.Calendar#getInstance()"</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">matchesSig</span><span class="p">(</span><span class="s2">"java.util.GregorianCalendar#getInstance()"</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">matchesSig</span><span class="p">(</span><span class="s2">"java.util.GregorianCalendar#new()"</span><span class="p">)]]</span><span class="w">
|
||
</span><span class="p">]</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">import</span> <span class="nn">java.time.LocalDateTime</span><span class="o">;</span>
|
||
<span class="kn">import</span> <span class="nn">java.util.Calendar</span><span class="o">;</span>
|
||
<span class="kn">import</span> <span class="nn">java.util.Date</span><span class="o">;</span>
|
||
|
||
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">DateStuff</span> <span class="o">{</span>
|
||
<span class="kd">private</span> <span class="nc">Date</span> <span class="nf">bad1</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="k">return</span> <span class="nc">Calendar</span><span class="o">.</span><span class="na">getInstance</span><span class="o">().</span><span class="na">getTime</span><span class="o">();</span> <span class="c1">// now</span>
|
||
<span class="o">}</span>
|
||
<span class="kd">private</span> <span class="nc">Date</span> <span class="nf">good1a</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="k">return</span> <span class="k">new</span> <span class="nf">Date</span><span class="o">();</span> <span class="c1">// now</span>
|
||
<span class="o">}</span>
|
||
<span class="kd">private</span> <span class="nc">LocalDateTime</span> <span class="nf">good1b</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="k">return</span> <span class="nc">LocalDateTime</span><span class="o">.</span><span class="na">now</span><span class="o">();</span>
|
||
<span class="o">}</span>
|
||
<span class="kd">private</span> <span class="kt">long</span> <span class="nf">bad2</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="k">return</span> <span class="nc">Calendar</span><span class="o">.</span><span class="na">getInstance</span><span class="o">().</span><span class="na">getTimeInMillis</span><span class="o">();</span>
|
||
<span class="o">}</span>
|
||
<span class="kd">private</span> <span class="kt">long</span> <span class="nf">good2</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="k">return</span> <span class="nc">System</span><span class="o">.</span><span class="na">currentTimeMillis</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/performance.xml/AvoidCalendarDateCreation"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="avoidfilestream">AvoidFileStream</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 6.0.0</p>
|
||
|
||
<p><strong>Priority:</strong> High (1)</p>
|
||
|
||
<p><strong>Minimum Language Version:</strong> Java 1.7</p>
|
||
|
||
<p>The FileInputStream and FileOutputStream classes contains a finalizer method which will cause garbage
|
||
collection pauses.
|
||
See <a href="https://bugs.openjdk.java.net/browse/JDK-8080225">JDK-8080225</a> for details.</p>
|
||
|
||
<p>The FileReader and FileWriter constructors instantiate FileInputStream and FileOutputStream,
|
||
again causing garbage collection issues while finalizer methods are called.</p>
|
||
|
||
<ul>
|
||
<li>Use <code class="language-plaintext highlighter-rouge">Files.newInputStream(Paths.get(fileName))</code> instead of <code class="language-plaintext highlighter-rouge">new FileInputStream(fileName)</code>.</li>
|
||
<li>Use <code class="language-plaintext highlighter-rouge">Files.newOutputStream(Paths.get(fileName))</code> instead of <code class="language-plaintext highlighter-rouge">new FileOutputStream(fileName)</code>.</li>
|
||
<li>Use <code class="language-plaintext highlighter-rouge">Files.newBufferedReader(Paths.get(fileName))</code> instead of <code class="language-plaintext highlighter-rouge">new FileReader(fileName)</code>.</li>
|
||
<li>Use <code class="language-plaintext highlighter-rouge">Files.newBufferedWriter(Paths.get(fileName))</code> instead of <code class="language-plaintext highlighter-rouge">new FileWriter(fileName)</code>.</li>
|
||
</ul>
|
||
|
||
<p>Please note, that the <code class="language-plaintext highlighter-rouge">java.nio</code> API does not throw a <code class="language-plaintext highlighter-rouge">FileNotFoundException</code> anymore, instead
|
||
it throws a <code class="language-plaintext highlighter-rouge">NoSuchFileException</code>. If your code dealt explicitly with a <code class="language-plaintext highlighter-rouge">FileNotFoundException</code>,
|
||
then this needs to be adjusted. Both exceptions are subclasses of <code class="language-plaintext highlighter-rouge">IOException</code>, so catching
|
||
that one covers both.</p>
|
||
|
||
<p><strong>This rule is defined by the following XPath expression:</strong></p>
|
||
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">ConstructorCall</span><span class="o">/</span><span class="nt">ClassType</span><span class="p">[</span><span class="w">
|
||
</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'java.io.FileInputStream'</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'java.io.FileOutputStream'</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'java.io.FileReader'</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">or</span><span class="w"> </span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'java.io.FileWriter'</span><span class="p">)</span><span class="w">
|
||
</span><span class="p">]</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">// these instantiations cause garbage collection pauses, even if properly closed</span>
|
||
|
||
<span class="nc">FileInputStream</span> <span class="n">fis</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">FileInputStream</span><span class="o">(</span><span class="n">fileName</span><span class="o">);</span>
|
||
<span class="nc">FileOutputStream</span> <span class="n">fos</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">FileOutputStream</span><span class="o">(</span><span class="n">fileName</span><span class="o">);</span>
|
||
<span class="nc">FileReader</span> <span class="n">fr</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">FileReader</span><span class="o">(</span><span class="n">fileName</span><span class="o">);</span>
|
||
<span class="nc">FileWriter</span> <span class="n">fw</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">FileWriter</span><span class="o">(</span><span class="n">fileName</span><span class="o">);</span>
|
||
|
||
<span class="c1">// the following instantiations help prevent Garbage Collection pauses, no finalization</span>
|
||
|
||
<span class="k">try</span><span class="o">(</span><span class="nc">InputStream</span> <span class="n">is</span> <span class="o">=</span> <span class="nc">Files</span><span class="o">.</span><span class="na">newInputStream</span><span class="o">(</span><span class="nc">Paths</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">fileName</span><span class="o">)))</span> <span class="o">{</span>
|
||
<span class="o">}</span>
|
||
<span class="k">try</span><span class="o">(</span><span class="nc">OutputStream</span> <span class="n">os</span> <span class="o">=</span> <span class="nc">Files</span><span class="o">.</span><span class="na">newOutputStream</span><span class="o">(</span><span class="nc">Paths</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">fileName</span><span class="o">)))</span> <span class="o">{</span>
|
||
<span class="o">}</span>
|
||
<span class="k">try</span><span class="o">(</span><span class="nc">BufferedReader</span> <span class="n">br</span> <span class="o">=</span> <span class="nc">Files</span><span class="o">.</span><span class="na">newBufferedReader</span><span class="o">(</span><span class="nc">Paths</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">fileName</span><span class="o">),</span> <span class="nc">StandardCharsets</span><span class="o">.</span><span class="na">UTF_8</span><span class="o">))</span> <span class="o">{</span>
|
||
<span class="o">}</span>
|
||
<span class="k">try</span><span class="o">(</span><span class="nc">BufferedWriter</span> <span class="n">wr</span> <span class="o">=</span> <span class="nc">Files</span><span class="o">.</span><span class="na">newBufferedWriter</span><span class="o">(</span><span class="nc">Paths</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">fileName</span><span class="o">),</span> <span class="nc">StandardCharsets</span><span class="o">.</span><span class="na">UTF_8</span><span class="o">))</span> <span class="o">{</span>
|
||
<span class="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/performance.xml/AvoidFileStream"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="avoidinstantiatingobjectsinloops">AvoidInstantiatingObjectsInLoops</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 2.2</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>New objects created within loops should be checked to see if they can created outside them and reused.</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/performance/AvoidInstantiatingObjectsInLoopsRule.java">net.sourceforge.pmd.lang.java.rule.performance.AvoidInstantiatingObjectsInLoopsRule</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">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="o">(</span> <span class="nc">String</span> <span class="n">as</span><span class="o">[]</span> <span class="o">)</span> <span class="o">{</span>
|
||
<span class="k">for</span> <span class="o">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span> <span class="n">i</span> <span class="o"><</span> <span class="mi">10</span><span class="o">;</span> <span class="n">i</span><span class="o">++)</span> <span class="o">{</span>
|
||
<span class="nc">Foo</span> <span class="n">f</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">Foo</span><span class="o">();</span> <span class="c1">// Avoid this whenever you can it's really expensive</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/performance.xml/AvoidInstantiatingObjectsInLoops"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="bigintegerinstantiation">BigIntegerInstantiation</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 3.9</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Don’t create instances of already existing BigInteger (<code class="language-plaintext highlighter-rouge">BigInteger.ZERO</code>, <code class="language-plaintext highlighter-rouge">BigInteger.ONE</code>),
|
||
for Java 1.5 onwards, BigInteger.TEN and BigDecimal (<code class="language-plaintext highlighter-rouge">BigDecimal.ZERO</code>, <code class="language-plaintext highlighter-rouge">BigDecimal.ONE</code>, <code class="language-plaintext highlighter-rouge">BigDecimal.TEN</code>) and
|
||
for Java 9 onwards <code class="language-plaintext highlighter-rouge">BigInteger.TWO</code>.</p>
|
||
|
||
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/performance/BigIntegerInstantiationRule.java">net.sourceforge.pmd.lang.java.rule.performance.BigIntegerInstantiationRule</a></p>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nc">BigInteger</span> <span class="n">bi1</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">BigInteger</span><span class="o">(</span><span class="s">"1"</span><span class="o">);</span> <span class="c1">// reference BigInteger.ONE instead</span>
|
||
<span class="nc">BigInteger</span> <span class="n">bi2</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">BigInteger</span><span class="o">(</span><span class="s">"0"</span><span class="o">);</span> <span class="c1">// reference BigInteger.ZERO instead</span>
|
||
<span class="nc">BigInteger</span> <span class="n">bi3</span><span class="o">;</span>
|
||
<span class="n">bi3</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">BigInteger</span><span class="o">(</span><span class="s">"0"</span><span class="o">);</span> <span class="c1">// reference BigInteger.ZERO instead</span>
|
||
|
||
<span class="nc">BigDecimal</span> <span class="n">bd1</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">BigDecimal</span><span class="o">(</span><span class="mi">0</span><span class="o">);</span> <span class="c1">// reference BigDecimal.ZERO instead</span>
|
||
<span class="nc">BigDecimal</span> <span class="n">bd2</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">BigDecimal</span><span class="o">(</span><span class="s">"0."</span><span class="o">)</span> <span class="o">;</span> <span class="c1">// reference BigDecimal.ZERO instead</span>
|
||
<span class="nc">BigDecimal</span> <span class="n">bd3</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">BigDecimal</span><span class="o">(</span><span class="mi">10</span><span class="o">);</span> <span class="c1">// reference BigDecimal.TEN instead</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>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/performance.xml/BigIntegerInstantiation"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="consecutiveappendsshouldreuse">ConsecutiveAppendsShouldReuse</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 5.1</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Consecutive calls to StringBuffer/StringBuilder .append should be chained, reusing the target object. This can improve the performance
|
||
by producing a smaller bytecode, reducing overhead and improving inlining. A complete analysis can be found <a href="https://github.com/pmd/pmd/issues/202#issuecomment-274349067">here</a></p>
|
||
|
||
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/performance/ConsecutiveAppendsShouldReuseRule.java">net.sourceforge.pmd.lang.java.rule.performance.ConsecutiveAppendsShouldReuseRule</a></p>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nc">String</span> <span class="n">foo</span> <span class="o">=</span> <span class="s">" "</span><span class="o">;</span>
|
||
|
||
<span class="nc">StringBuffer</span> <span class="n">buf</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">StringBuffer</span><span class="o">();</span>
|
||
<span class="n">buf</span><span class="o">.</span><span class="na">append</span><span class="o">(</span><span class="s">"Hello"</span><span class="o">);</span> <span class="c1">// poor</span>
|
||
<span class="n">buf</span><span class="o">.</span><span class="na">append</span><span class="o">(</span><span class="n">foo</span><span class="o">);</span>
|
||
<span class="n">buf</span><span class="o">.</span><span class="na">append</span><span class="o">(</span><span class="s">"World"</span><span class="o">);</span>
|
||
|
||
<span class="nc">StringBuffer</span> <span class="n">buf</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">StringBuffer</span><span class="o">();</span>
|
||
<span class="n">buf</span><span class="o">.</span><span class="na">append</span><span class="o">(</span><span class="s">"Hello"</span><span class="o">).</span><span class="na">append</span><span class="o">(</span><span class="n">foo</span><span class="o">).</span><span class="na">append</span><span class="o">(</span><span class="s">"World"</span><span class="o">);</span> <span class="c1">// good</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>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/performance.xml/ConsecutiveAppendsShouldReuse"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="consecutiveliteralappends">ConsecutiveLiteralAppends</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 3.5</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Consecutively calling StringBuffer/StringBuilder.append(…) with literals should be avoided.
|
||
Since the literals are constants, they can already be combined into a single String literal and this String
|
||
can be appended in a single method call.</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/performance/ConsecutiveLiteralAppendsRule.java">net.sourceforge.pmd.lang.java.rule.performance.ConsecutiveLiteralAppendsRule</a></p>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nc">StringBuilder</span> <span class="n">buf</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">StringBuilder</span><span class="o">();</span>
|
||
<span class="n">buf</span><span class="o">.</span><span class="na">append</span><span class="o">(</span><span class="s">"Hello"</span><span class="o">).</span><span class="na">append</span><span class="o">(</span><span class="s">" "</span><span class="o">).</span><span class="na">append</span><span class="o">(</span><span class="s">"World"</span><span class="o">);</span> <span class="c1">// poor</span>
|
||
<span class="n">buf</span><span class="o">.</span><span class="na">append</span><span class="o">(</span><span class="s">"Hello World"</span><span class="o">);</span> <span class="c1">// good</span>
|
||
|
||
<span class="n">buf</span><span class="o">.</span><span class="na">append</span><span class="o">(</span><span class="sc">'h'</span><span class="o">).</span><span class="na">append</span><span class="o">(</span><span class="sc">'e'</span><span class="o">).</span><span class="na">append</span><span class="o">(</span><span class="sc">'l'</span><span class="o">).</span><span class="na">append</span><span class="o">(</span><span class="sc">'l'</span><span class="o">).</span><span class="na">append</span><span class="o">(</span><span class="sc">'o'</span><span class="o">);</span> <span class="c1">// poor</span>
|
||
<span class="n">buf</span><span class="o">.</span><span class="na">append</span><span class="o">(</span><span class="s">"hello"</span><span class="o">);</span> <span class="c1">// good</span>
|
||
|
||
<span class="n">buf</span><span class="o">.</span><span class="na">append</span><span class="o">(</span><span class="mi">1</span><span class="o">).</span><span class="na">append</span><span class="o">(</span><span class="sc">'m'</span><span class="o">);</span> <span class="c1">// poor</span>
|
||
<span class="n">buf</span><span class="o">.</span><span class="na">append</span><span class="o">(</span><span class="s">"1m"</span><span class="o">);</span> <span class="c1">// good</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>This rule has the following properties:</strong></p>
|
||
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th>Name</th>
|
||
<th>Default Value</th>
|
||
<th>Description</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td>threshold</td>
|
||
<td>1</td>
|
||
<td>Max consecutive appends</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
|
||
<p><strong>Use this rule with the default properties by just referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/performance.xml/ConsecutiveLiteralAppends"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule and customize it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/performance.xml/ConsecutiveLiteralAppends"</span><span class="nt">></span>
|
||
<span class="nt"><properties></span>
|
||
<span class="nt"><property</span> <span class="na">name=</span><span class="s">"threshold"</span> <span class="na">value=</span><span class="s">"1"</span> <span class="nt">/></span>
|
||
<span class="nt"></properties></span>
|
||
<span class="nt"></rule></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="inefficientemptystringcheck">InefficientEmptyStringCheck</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 3.6</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>String.trim().length() == 0 (or String.trim().isEmpty() for the same reason) is an inefficient
|
||
way to check if a String is really blank, as it creates a new String object just to check its size.
|
||
Consider creating a static function that loops through a string, checking Character.isWhitespace()
|
||
on each character and returning false if a non-whitespace character is found. A Smarter code to
|
||
check for an empty string would be:</p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">private</span> <span class="kt">boolean</span> <span class="nf">checkTrimEmpty</span><span class="o">(</span><span class="nc">String</span> <span class="n">str</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">str</span><span class="o">.</span><span class="na">length</span><span class="o">();</span> <span class="n">i</span><span class="o">++)</span> <span class="o">{</span>
|
||
<span class="k">if</span><span class="o">(!</span><span class="nc">Character</span><span class="o">.</span><span class="na">isWhitespace</span><span class="o">(</span><span class="n">str</span><span class="o">.</span><span class="na">charAt</span><span class="o">(</span><span class="n">i</span><span class="o">)))</span> <span class="o">{</span>
|
||
<span class="k">return</span> <span class="kc">false</span><span class="o">;</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>
|
||
</code></pre></div></div>
|
||
|
||
<p>You can refer to Apache’s StringUtils#isBlank (in commons-lang),
|
||
Spring’s StringUtils#hasText (in the Spring framework) or Google’s
|
||
CharMatcher#whitespace (in Guava) for existing implementations (some might
|
||
include the check for != null).</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/performance/InefficientEmptyStringCheckRule.java">net.sourceforge.pmd.lang.java.rule.performance.InefficientEmptyStringCheckRule</a></p>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kt">void</span> <span class="nf">bar</span><span class="o">(</span><span class="nc">String</span> <span class="n">string</span><span class="o">)</span> <span class="o">{</span>
|
||
<span class="k">if</span> <span class="o">(</span><span class="n">string</span> <span class="o">!=</span> <span class="kc">null</span> <span class="o">&&</span> <span class="n">string</span><span class="o">.</span><span class="na">trim</span><span class="o">().</span><span class="na">length</span><span class="o">()</span> <span class="o">></span> <span class="mi">0</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/performance.xml/InefficientEmptyStringCheck"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="inefficientstringbuffering">InefficientStringBuffering</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 3.4</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Avoid concatenating non-literals in a StringBuffer constructor or append() since intermediate buffers will
|
||
need to be be created and destroyed by the JVM.</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/performance/InefficientStringBufferingRule.java">net.sourceforge.pmd.lang.java.rule.performance.InefficientStringBufferingRule</a></p>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">// Avoid this, two buffers are actually being created here</span>
|
||
<span class="nc">StringBuffer</span> <span class="n">sb</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">StringBuffer</span><span class="o">(</span><span class="s">"tmp = "</span><span class="o">+</span><span class="nc">System</span><span class="o">.</span><span class="na">getProperty</span><span class="o">(</span><span class="s">"java.io.tmpdir"</span><span class="o">));</span>
|
||
|
||
<span class="c1">// do this instead</span>
|
||
<span class="nc">StringBuffer</span> <span class="n">sb</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">StringBuffer</span><span class="o">(</span><span class="s">"tmp = "</span><span class="o">);</span>
|
||
<span class="n">sb</span><span class="o">.</span><span class="na">append</span><span class="o">(</span><span class="nc">System</span><span class="o">.</span><span class="na">getProperty</span><span class="o">(</span><span class="s">"java.io.tmpdir"</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/performance.xml/InefficientStringBuffering"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="insufficientstringbufferdeclaration">InsufficientStringBufferDeclaration</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 3.6</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Failing to pre-size a StringBuffer or StringBuilder properly could cause it to re-size many times
|
||
during runtime. This rule attempts to determine the total number the characters that are actually
|
||
passed into StringBuffer.append(), but represents a best guess "worst case" scenario. An empty
|
||
StringBuffer/StringBuilder constructor initializes the object to 16 characters. This default
|
||
is assumed if the length of the constructor can not be determined.</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/performance/InsufficientStringBufferDeclarationRule.java">net.sourceforge.pmd.lang.java.rule.performance.InsufficientStringBufferDeclarationRule</a></p>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nc">StringBuilder</span> <span class="n">bad</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">StringBuilder</span><span class="o">();</span>
|
||
<span class="n">bad</span><span class="o">.</span><span class="na">append</span><span class="o">(</span><span class="s">"This is a long string that will exceed the default 16 characters"</span><span class="o">);</span>
|
||
|
||
<span class="nc">StringBuilder</span> <span class="n">good</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">StringBuilder</span><span class="o">(</span><span class="mi">41</span><span class="o">);</span>
|
||
<span class="n">good</span><span class="o">.</span><span class="na">append</span><span class="o">(</span><span class="s">"This is a long string, which is pre-sized"</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/performance.xml/InsufficientStringBufferDeclaration"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="optimizabletoarraycall">OptimizableToArrayCall</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 1.8</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p><strong>Minimum Language Version:</strong> Java 1.6</p>
|
||
|
||
<p>Calls to a collection’s <code class="language-plaintext highlighter-rouge">toArray(E[])</code> method should specify a target array of zero size. This allows the JVM
|
||
to optimize the memory allocation and copying as much as possible.</p>
|
||
|
||
<p>Previous versions of this rule (pre PMD 6.0.0) suggested the opposite, but current JVM implementations
|
||
perform always better, when they have full control over the target array. And allocation an array via
|
||
reflection is nowadays as fast as the direct allocation.</p>
|
||
|
||
<p>See also <a href="https://shipilev.net/blog/2016/arrays-wisdom-ancients/">Arrays of Wisdom of the Ancients</a></p>
|
||
|
||
<p>Note: If you don’t need an array of the correct type, then the simple <code class="language-plaintext highlighter-rouge">toArray()</code> method without an array
|
||
is faster, but returns only an array of type <code class="language-plaintext highlighter-rouge">Object[]</code>.</p>
|
||
|
||
<p><strong>This rule is defined by the following XPath expression:</strong></p>
|
||
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">MethodCall</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">matchesSig</span><span class="p">(</span><span class="s2">"java.util.Collection#toArray(_)"</span><span class="p">)]</span><span class="w">
|
||
</span><span class="p">[</span><span class="nt">ArgumentList</span><span class="o">/</span><span class="nt">ArrayAllocation</span><span class="o">/</span><span class="nt">ArrayType</span><span class="o">/</span><span class="nt">ArrayDimensions</span><span class="o">/</span><span class="nt">ArrayDimExpr</span><span class="p">[</span><span class="ow">not</span><span class="p">(</span><span class="nt">NumericLiteral</span><span class="p">[</span><span class="na">@Image</span><span class="err">=</span><span class="s2">"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="nc">List</span><span class="o"><</span><span class="nc">Foo</span><span class="o">></span> <span class="n">foos</span> <span class="o">=</span> <span class="n">getFoos</span><span class="o">();</span>
|
||
|
||
<span class="c1">// much better; this one allows the jvm to allocate an array of the correct size and effectively skip</span>
|
||
<span class="c1">// the zeroing, since each array element will be overridden anyways</span>
|
||
<span class="nc">Foo</span><span class="o">[]</span> <span class="n">fooArray</span> <span class="o">=</span> <span class="n">foos</span><span class="o">.</span><span class="na">toArray</span><span class="o">(</span><span class="k">new</span> <span class="nc">Foo</span><span class="o">[</span><span class="mi">0</span><span class="o">]);</span>
|
||
|
||
<span class="c1">// inefficient, the array needs to be zeroed out by the jvm before it is handed over to the toArray method</span>
|
||
<span class="nc">Foo</span><span class="o">[]</span> <span class="n">fooArray</span> <span class="o">=</span> <span class="n">foos</span><span class="o">.</span><span class="na">toArray</span><span class="o">(</span><span class="k">new</span> <span class="nc">Foo</span><span class="o">[</span><span class="n">foos</span><span class="o">.</span><span class="na">size</span><span class="o">()]);</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule by referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/performance.xml/OptimizableToArrayCall"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="redundantfieldinitializer">RedundantFieldInitializer</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 5.0</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Java will initialize fields with known default values so any explicit initialization of those same defaults
|
||
is redundant and results in a larger class file (approximately three additional bytecode instructions per field).</p>
|
||
|
||
<p><strong>This rule is defined by the following Java class:</strong> <a href="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/performance/RedundantFieldInitializerRule.java">net.sourceforge.pmd.lang.java.rule.performance.RedundantFieldInitializerRule</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">C</span> <span class="o">{</span>
|
||
<span class="kt">boolean</span> <span class="n">b</span> <span class="o">=</span> <span class="kc">false</span><span class="o">;</span> <span class="c1">// examples of redundant initializers</span>
|
||
<span class="kt">byte</span> <span class="n">by</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span>
|
||
<span class="kt">short</span> <span class="n">s</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span>
|
||
<span class="kt">char</span> <span class="n">c</span> <span class="o">=</span> <span class="mi">0</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="kt">long</span> <span class="n">l</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span>
|
||
|
||
<span class="kt">float</span> <span class="n">f</span> <span class="o">=</span> <span class="o">.</span><span class="mi">0</span><span class="n">f</span><span class="o">;</span> <span class="c1">// all possible float literals</span>
|
||
<span class="n">doable</span> <span class="n">d</span> <span class="o">=</span> <span class="mi">0</span><span class="n">d</span><span class="o">;</span> <span class="c1">// all possible double literals</span>
|
||
<span class="nc">Object</span> <span class="n">o</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span>
|
||
|
||
<span class="nc">MyClass</span> <span class="n">mca</span><span class="o">[]</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span>
|
||
<span class="kt">int</span> <span class="n">i1</span> <span class="o">=</span> <span class="mi">0</span><span class="o">,</span> <span class="n">ia1</span><span class="o">[]</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span>
|
||
|
||
<span class="kd">class</span> <span class="nc">Nested</span> <span class="o">{</span>
|
||
<span class="kt">boolean</span> <span class="n">b</span> <span class="o">=</span> <span class="kc">false</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/performance.xml/RedundantFieldInitializer"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="stringinstantiation">StringInstantiation</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 1.0</p>
|
||
|
||
<p><strong>Priority:</strong> Medium High (2)</p>
|
||
|
||
<p>Avoid instantiating String objects; this is usually unnecessary since they are immutable and can be safely shared.</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/performance/StringInstantiationRule.java">net.sourceforge.pmd.lang.java.rule.performance.StringInstantiationRule</a></p>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">private</span> <span class="nc">String</span> <span class="n">bar</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">String</span><span class="o">(</span><span class="s">"bar"</span><span class="o">);</span> <span class="c1">// just do a String bar = "bar";</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>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/performance.xml/StringInstantiation"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="stringtostring">StringToString</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 1.0</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Avoid calling toString() on objects already known to be string instances; this is unnecessary.</p>
|
||
|
||
<p><strong>This rule is defined by the following XPath expression:</strong></p>
|
||
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">MethodCall</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">matchesSig</span><span class="p">(</span><span class="s2">"java.lang.String#toString()"</span><span class="p">)]</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">private</span> <span class="nc">String</span> <span class="nf">baz</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="nc">String</span> <span class="n">bar</span> <span class="o">=</span> <span class="s">"howdy"</span><span class="o">;</span>
|
||
<span class="k">return</span> <span class="n">bar</span><span class="o">.</span><span class="na">toString</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/performance.xml/StringToString"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="toofewbranchesforaswitchstatement">TooFewBranchesForASwitchStatement</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 4.2</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Switch statements are intended to be used to support complex branching behaviour. Using a switch for only a few
|
||
cases is ill-advised, since switches are not as easy to understand as if-else statements. In these cases use the
|
||
if-else statement to increase code readability.</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="w"> </span><span class="p">(</span><span class="nf">count</span><span class="p">(</span><span class="err">*</span><span class="p">)</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="m">1</span><span class="p">)</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="nv">$minimumNumberCaseForASwitch</span><span class="w"> </span><span class="p">]</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">// With a minimumNumberCaseForASwitch of 3</span>
|
||
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
|
||
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">bar</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="k">switch</span> <span class="o">(</span><span class="n">condition</span><span class="o">)</span> <span class="o">{</span>
|
||
<span class="k">case</span> <span class="nl">ONE:</span>
|
||
<span class="n">instruction</span><span class="o">;</span>
|
||
<span class="k">break</span><span class="o">;</span>
|
||
<span class="k">default</span><span class="o">:</span>
|
||
<span class="k">break</span><span class="o">;</span> <span class="c1">// not enough for a 'switch' stmt, a simple 'if' stmt would have been more appropriate</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>This rule has the following properties:</strong></p>
|
||
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th>Name</th>
|
||
<th>Default Value</th>
|
||
<th>Description</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td>minimumNumberCaseForASwitch</td>
|
||
<td>3</td>
|
||
<td>Minimum number of branches for a switch</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
|
||
<p><strong>Use this rule with the default properties by just referencing it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/performance.xml/TooFewBranchesForASwitchStatement"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>Use this rule and customize it:</strong></p>
|
||
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><rule</span> <span class="na">ref=</span><span class="s">"category/java/performance.xml/TooFewBranchesForASwitchStatement"</span><span class="nt">></span>
|
||
<span class="nt"><properties></span>
|
||
<span class="nt"><property</span> <span class="na">name=</span><span class="s">"minimumNumberCaseForASwitch"</span> <span class="na">value=</span><span class="s">"3"</span> <span class="nt">/></span>
|
||
<span class="nt"></properties></span>
|
||
<span class="nt"></rule></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="usearraylistinsteadofvector">UseArrayListInsteadOfVector</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 3.0</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>ArrayList is a much better Collection implementation than Vector if thread-safe operation 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">ConstructorCall</span><span class="o">/</span><span class="nt">ClassType</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">typeIsExactly</span><span class="p">(</span><span class="s1">'java.util.Vector'</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">typeIsExactly</span><span class="p">(</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="kn">import</span> <span class="nn">java.util.*</span><span class="o">;</span>
|
||
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">SimpleTest</span> <span class="kd">extends</span> <span class="nc">TestCase</span> <span class="o">{</span>
|
||
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">testX</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="nc">Collection</span> <span class="n">c1</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">Vector</span><span class="o">();</span>
|
||
<span class="nc">Collection</span> <span class="n">c2</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">ArrayList</span><span class="o">();</span> <span class="c1">// achieves the same with much better performance</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/performance.xml/UseArrayListInsteadOfVector"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="usearraysaslist">UseArraysAsList</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 3.5</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>The <code class="language-plaintext highlighter-rouge">java.util.Arrays</code> class has a <code class="language-plaintext highlighter-rouge">asList()</code> method that should be used when you want to create a new List from
|
||
an array of objects. It is faster than executing a loop to copy all the elements of the array one by one.</p>
|
||
|
||
<p>Note that the result of <code class="language-plaintext highlighter-rouge">Arrays.asList()</code> is backed by the specified array,
|
||
changes in the returned list will result in the array to be modified.
|
||
For that reason, it is not possible to add new elements to the returned list of <code class="language-plaintext highlighter-rouge">Arrays.asList()</code>
|
||
(UnsupportedOperationException).
|
||
You must use <code class="language-plaintext highlighter-rouge">new ArrayList<>(Arrays.asList(...))</code> if that is inconvenient for you (e.g. because of concurrent access).</p>
|
||
|
||
<p><strong>This rule is defined by the following XPath expression:</strong></p>
|
||
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">ForStatement</span><span class="w">
|
||
</span><span class="p">[</span><span class="nt">ForInit</span><span class="w">
|
||
</span><span class="p">[</span><span class="nt">LocalVariableDeclaration</span><span class="w">
|
||
</span><span class="p">[</span><span class="nt">PrimitiveType</span><span class="p">[</span><span class="na">@Kind</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'int'</span><span class="p">]]</span><span class="w">
|
||
</span><span class="p">[</span><span class="nt">VariableDeclarator</span><span class="p">[</span><span class="nt">NumericLiteral</span><span class="p">[</span><span class="na">@IntLiteral</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">'0'</span><span class="p">]]]</span><span class="w">
|
||
</span><span class="p">]</span><span class="w">
|
||
</span><span class="p">]</span><span class="w">
|
||
</span><span class="p">[</span><span class="o">*</span><span class="p">[</span><span class="m">2</span><span class="p">]</span><span class="o">//</span><span class="nt">FieldAccess</span><span class="p">[</span><span class="na">@Name</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'length'</span><span class="p">]</span><span class="o">/</span><span class="nt">VariableAccess</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s2">"java.lang.Object[]"</span><span class="p">)]]</span><span class="w">
|
||
</span><span class="err">/</span><span class="o">*</span><span class="p">[</span><span class="nf">last</span><span class="p">()]</span><span class="o">/</span><span class="nt">ExpressionStatement</span><span class="o">/</span><span class="w">
|
||
</span><span class="nt">MethodCall</span><span class="w">
|
||
</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">matchesSig</span><span class="p">(</span><span class="s1">'java.util.Collection#add(_)'</span><span class="p">)]</span><span class="w">
|
||
</span><span class="p">[</span><span class="nt">ArgumentList</span><span class="o">/</span><span class="nt">ArrayAccess</span><span class="w">
|
||
</span><span class="p">[</span><span class="nt">VariableAccess</span><span class="p">[</span><span class="na">@Name</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="kn">ancestor::</span><span class="nt">ForStatement</span><span class="o">/</span><span class="nt">ForInit</span><span class="o">/</span><span class="nt">LocalVariableDeclaration</span><span class="o">/</span><span class="nt">VariableDeclarator</span><span class="o">/</span><span class="nt">VariableId</span><span class="err">/</span><span class="na">@Name</span><span class="p">]]</span><span class="w">
|
||
</span><span class="p">]</span><span class="w">
|
||
</span><span class="p">|</span><span class="w">
|
||
</span><span class="o">//</span><span class="nt">ForeachStatement</span><span class="w">
|
||
</span><span class="p">[</span><span class="nt">VariableAccess</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s2">"java.lang.Object[]"</span><span class="p">)]]</span><span class="w">
|
||
</span><span class="err">/</span><span class="o">*</span><span class="p">[</span><span class="nf">last</span><span class="p">()]</span><span class="o">/</span><span class="nt">ExpressionStatement</span><span class="o">/</span><span class="nt">MethodCall</span><span class="w">
|
||
</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">matchesSig</span><span class="p">(</span><span class="s1">'java.util.Collection#add(_)'</span><span class="p">)]</span><span class="w">
|
||
</span><span class="p">[</span><span class="nt">ArgumentList</span><span class="w">
|
||
</span><span class="p">[</span><span class="nt">VariableAccess</span><span class="p">[</span><span class="na">@Name</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="kn">ancestor::</span><span class="nt">ForeachStatement</span><span class="o">/</span><span class="nt">LocalVariableDeclaration</span><span class="o">/</span><span class="nt">VariableDeclarator</span><span class="o">/</span><span class="nt">VariableId</span><span class="err">/</span><span class="na">@Name</span><span class="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">Test</span> <span class="o">{</span>
|
||
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">foo</span><span class="o">(</span><span class="nc">Integer</span><span class="o">[]</span> <span class="n">ints</span><span class="o">)</span> <span class="o">{</span>
|
||
<span class="c1">// could just use Arrays.asList(ints)</span>
|
||
<span class="nc">List</span><span class="o"><</span><span class="nc">Integer</span><span class="o">></span> <span class="n">l</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">ArrayList</span><span class="o"><>(</span><span class="mi">100</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">ints</span><span class="o">.</span><span class="na">length</span><span class="o">;</span> <span class="n">i</span><span class="o">++)</span> <span class="o">{</span>
|
||
<span class="n">l</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="n">ints</span><span class="o">[</span><span class="n">i</span><span class="o">]);</span>
|
||
<span class="o">}</span>
|
||
|
||
<span class="nc">List</span><span class="o"><</span><span class="nc">Integer</span><span class="o">></span> <span class="n">anotherList</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">ArrayList</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">ints</span><span class="o">.</span><span class="na">length</span><span class="o">;</span> <span class="n">i</span><span class="o">++)</span> <span class="o">{</span>
|
||
<span class="n">anotherList</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="n">ints</span><span class="o">[</span><span class="n">i</span><span class="o">].</span><span class="na">toString</span><span class="o">());</span> <span class="c1">// won't trigger the rule</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/performance.xml/UseArraysAsList"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="useindexofchar">UseIndexOfChar</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 3.5</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Use String.indexOf(char) when checking for the index of a single character; it executes faster.</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/performance/UseIndexOfCharRule.java">net.sourceforge.pmd.lang.java.rule.performance.UseIndexOfCharRule</a></p>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nc">String</span> <span class="n">s</span> <span class="o">=</span> <span class="s">"hello world"</span><span class="o">;</span>
|
||
<span class="c1">// avoid this</span>
|
||
<span class="k">if</span> <span class="o">(</span><span class="n">s</span><span class="o">.</span><span class="na">indexOf</span><span class="o">(</span><span class="s">"d"</span><span class="o">)</span> <span class="o">{}</span>
|
||
<span class="c1">// instead do this</span>
|
||
<span class="k">if</span> <span class="o">(</span><span class="n">s</span><span class="o">.</span><span class="na">indexOf</span><span class="o">(</span><span class="sc">'d'</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/performance.xml/UseIndexOfChar"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="useiostreamswithapachecommonsfileitem">UseIOStreamsWithApacheCommonsFileItem</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 6.25.0</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Problem: Use of <a href="https://commons.apache.org/proper/commons-fileupload/apidocs/org/apache/commons/fileupload/FileItem.html#get--">FileItem.get()</a>
|
||
and <a href="https://commons.apache.org/proper/commons-fileupload/apidocs/org/apache/commons/fileupload/FileItem.html#getString--">FileItem.getString()</a>
|
||
could exhaust memory since they load the entire file into memory.</p>
|
||
|
||
<p>Solution: Use <a href="https://commons.apache.org/proper/commons-fileupload/apidocs/org/apache/commons/fileupload/FileItem.html#getInputStream--">FileItem.getInputStream()</a>
|
||
and buffering.</p>
|
||
|
||
<p><strong>This rule is defined by the following XPath expression:</strong></p>
|
||
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">MethodCall</span><span class="w">
|
||
</span><span class="p">[</span><span class="na">@MethodName</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'get'</span><span class="w"> </span><span class="ow">or</span><span class="w"> </span><span class="na">@MethodName</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'getString'</span><span class="p">]</span><span class="w">
|
||
</span><span class="p">[</span><span class="o">*</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">typeIs</span><span class="p">(</span><span class="s1">'org.apache.commons.fileupload.FileItem'</span><span class="p">)]]</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">import</span> <span class="nn">org.apache.commons.fileupload.FileItem</span><span class="o">;</span>
|
||
|
||
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">FileStuff</span> <span class="o">{</span>
|
||
<span class="kd">private</span> <span class="nc">String</span> <span class="nf">bad</span><span class="o">(</span><span class="nc">FileItem</span> <span class="n">fileItem</span><span class="o">)</span> <span class="o">{</span>
|
||
<span class="k">return</span> <span class="n">fileItem</span><span class="o">.</span><span class="na">getString</span><span class="o">();</span>
|
||
<span class="o">}</span>
|
||
|
||
<span class="kd">private</span> <span class="nc">InputStream</span> <span class="nf">good</span><span class="o">(</span><span class="nc">FileItem</span> <span class="n">fileItem</span><span class="o">)</span> <span class="o">{</span>
|
||
<span class="k">return</span> <span class="n">fileItem</span><span class="o">.</span><span class="na">getInputStream</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/performance.xml/UseIOStreamsWithApacheCommonsFileItem"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="uselessstringvalueof">UselessStringValueOf</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 3.8</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>No need to call String.valueOf to append to a string; just use the valueOf() argument directly.</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/performance/UselessStringValueOfRule.java">net.sourceforge.pmd.lang.java.rule.performance.UselessStringValueOfRule</a></p>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="nc">String</span> <span class="nf">convert</span><span class="o">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">)</span> <span class="o">{</span>
|
||
<span class="nc">String</span> <span class="n">s</span><span class="o">;</span>
|
||
<span class="n">s</span> <span class="o">=</span> <span class="s">"a"</span> <span class="o">+</span> <span class="nc">String</span><span class="o">.</span><span class="na">valueOf</span><span class="o">(</span><span class="n">i</span><span class="o">);</span> <span class="c1">// not required</span>
|
||
<span class="n">s</span> <span class="o">=</span> <span class="s">"a"</span> <span class="o">+</span> <span class="n">i</span><span class="o">;</span> <span class="c1">// preferred approach</span>
|
||
<span class="k">return</span> <span class="n">s</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/performance.xml/UselessStringValueOf"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="usestringbufferforstringappends">UseStringBufferForStringAppends</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 3.1</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>The use of the ‘+=’ operator for appending strings causes the JVM to create and use an internal StringBuffer.
|
||
If a non-trivial number of these concatenations are being used then the explicit use of a StringBuilder or
|
||
threadsafe StringBuffer is recommended to avoid this.</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/performance/UseStringBufferForStringAppendsRule.java">net.sourceforge.pmd.lang.java.rule.performance.UseStringBufferForStringAppendsRule</a></p>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
|
||
<span class="nc">String</span> <span class="nf">inefficientConcatenation</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="nc">String</span> <span class="n">result</span> <span class="o">=</span> <span class="s">""</span><span class="o">;</span>
|
||
<span class="k">for</span> <span class="o">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span> <span class="n">i</span> <span class="o"><</span> <span class="mi">10</span><span class="o">;</span> <span class="n">i</span><span class="o">++)</span> <span class="o">{</span>
|
||
<span class="c1">// warning: this concatenation will create one new StringBuilder per iteration</span>
|
||
<span class="n">result</span> <span class="o">+=</span> <span class="n">getStringFromSomeWhere</span><span class="o">(</span><span class="n">i</span><span class="o">);</span>
|
||
<span class="o">}</span>
|
||
<span class="k">return</span> <span class="n">result</span><span class="o">;</span>
|
||
<span class="o">}</span>
|
||
|
||
<span class="nc">String</span> <span class="nf">efficientConcatenation</span><span class="o">()</span> <span class="o">{</span>
|
||
<span class="c1">// better would be to use one StringBuilder for the entire loop</span>
|
||
<span class="nc">StringBuilder</span> <span class="n">result</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">StringBuilder</span><span class="o">();</span>
|
||
<span class="k">for</span> <span class="o">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span> <span class="n">i</span> <span class="o"><</span> <span class="mi">10</span><span class="o">;</span> <span class="n">i</span><span class="o">++)</span> <span class="o">{</span>
|
||
<span class="n">result</span><span class="o">.</span><span class="na">append</span><span class="o">(</span><span class="n">getStringFromSomeWhere</span><span class="o">(</span><span class="n">i</span><span class="o">));</span>
|
||
<span class="o">}</span>
|
||
<span class="k">return</span> <span class="n">result</span><span class="o">.</span><span class="na">toString</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/performance.xml/UseStringBufferForStringAppends"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
<h2 id="usestringbufferlength">UseStringBufferLength</h2>
|
||
|
||
<p><strong>Since:</strong> PMD 3.4</p>
|
||
|
||
<p><strong>Priority:</strong> Medium (3)</p>
|
||
|
||
<p>Use StringBuffer.length() to determine StringBuffer length rather than using StringBuffer.toString().equals("")
|
||
or StringBuffer.toString().length() == …</p>
|
||
|
||
<p><strong>This rule is defined by the following XPath expression:</strong></p>
|
||
<div class="language-xpath highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">//</span><span class="nt">MethodCall</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">matchesSig</span><span class="p">(</span><span class="s1">'_#length()'</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">and</span><span class="w"> </span><span class="nt">MethodCall</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">matchesSig</span><span class="p">(</span><span class="s1">'java.lang.CharSequence#toString()'</span><span class="p">)]]</span><span class="w">
|
||
</span><span class="p">|</span><span class="w">
|
||
</span><span class="c">(: finds sb.toString().equals(someVar) where var is a final variable initialized with literal "" :)</span><span class="w">
|
||
</span><span class="o">//</span><span class="nt">MethodCall</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">matchesSig</span><span class="p">(</span><span class="s1">'_#equals(_)'</span><span class="p">)</span><span class="w">
|
||
</span><span class="ow">and</span><span class="w"> </span><span class="nt">MethodCall</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="nf">matchesSig</span><span class="p">(</span><span class="s1">'java.lang.AbstractStringBuilder#toString()'</span><span class="p">)]</span><span class="w">
|
||
</span><span class="ow">and</span><span class="w"> </span><span class="nt">ArgumentList</span><span class="o">/</span><span class="nt">VariableAccess</span><span class="p">[</span><span class="na">@Name</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="o">//</span><span class="nt">VariableDeclarator</span><span class="p">[</span><span class="nt">StringLiteral</span><span class="p">[</span><span class="na">@Image</span><span class="err">=</span><span class="s1">'""'</span><span class="p">]]</span><span class="w">
|
||
</span><span class="o">/</span><span class="nt">VariableId</span><span class="p">[</span><span class="nn">pmd-java:</span><span class="ow">mod</span><span class="kr">if</span><span class="nf">iers</span><span class="p">()</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="s1">'final'</span><span class="p">]</span><span class="err">/</span><span class="na">@Name</span><span class="p">]]</span><span class="w">
|
||
</span></code></pre></div></div>
|
||
|
||
<p><strong>Example(s):</strong></p>
|
||
|
||
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nc">StringBuffer</span> <span class="n">sb</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">StringBuffer</span><span class="o">();</span>
|
||
|
||
<span class="k">if</span> <span class="o">(</span><span class="n">sb</span><span class="o">.</span><span class="na">toString</span><span class="o">().</span><span class="na">equals</span><span class="o">(</span><span class="s">""</span><span class="o">))</span> <span class="o">{}</span> <span class="c1">// inefficient</span>
|
||
|
||
<span class="k">if</span> <span class="o">(</span><span class="n">sb</span><span class="o">.</span><span class="na">length</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">// preferred</span>
|
||
</code></pre></div></div>
|
||
|
||
<p><strong>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/performance.xml/UseStringBufferLength"</span> <span class="nt">/></span>
|
||
</code></pre></div></div>
|
||
|
||
|
||
|
||
<div class="tags">
|
||
|
||
</div>
|
||
|
||
</div>
|
||
|
||
|
||
<footer>
|
||
|
||
<hr />
|
||
<div>
|
||
This documentation is written in markdown. <br />
|
||
If there is something missing or can be improved, edit this page on
|
||
github and create a PR:
|
||
<a
|
||
target="_blank"
|
||
href="https://github.com/pmd/pmd/blob/master/docs/../pmd-java/src/main/resources/category/java/performance.xml"
|
||
role="button"
|
||
><i class="fab fa-github fa-lg"></i> Edit on GitHub</a
|
||
>
|
||
</div>
|
||
|
||
<hr />
|
||
<div class="row">
|
||
<div class="col-lg-12 footer">
|
||
©2024 PMD Open Source Project. All rights
|
||
reserved. <br />
|
||
Site last generated: Feb 29, 2024 <br />
|
||
<p>
|
||
<img src="images/logo/pmd-logo-70px.png" alt="PMD
|
||
logo"/>
|
||
</p>
|
||
</div>
|
||
</div>
|
||
</footer>
|
||
|
||
|
||
</div>
|
||
|
||
<!-- /.row -->
|
||
</div>
|
||
<!-- /.container -->
|
||
</div>
|
||
|
||
|
||
<!-- Sticky TOC column -->
|
||
<div class="toc-col">
|
||
<div id="toc"></div>
|
||
</div>
|
||
|
||
<!-- /.toc-container-wrapper -->
|
||
</div>
|
||
</div>
|
||
|
||
<script type="application/javascript" src="assets/jquery-3.5.1/jquery-3.5.1.min.js"></script>
|
||
<script type="application/javascript" src="assets/anchorjs-4.2.2/anchor.min.js"></script>
|
||
<script type="application/javascript" src="assets/navgoco-0.2.1/src/jquery.navgoco.min.js"></script>
|
||
<script type="application/javascript" src="assets/bootstrap-4.5.2-dist/js/bootstrap.bundle.min.js"></script>
|
||
<script type="application/javascript" src="assets/Simple-Jekyll-Search-1.0.8/dest/jekyll-search.js"></script>
|
||
<script type="application/javascript" src="assets/jekyll-table-of-contents/toc.js"></script>
|
||
<script type="application/javascript" src="js/tabstate.js"></script>
|
||
<script type="application/javascript" src="js/customscripts.js"></script>
|
||
|
||
|
||
|
||
</body>
|
||
</html>
|