Details
-
Type:
Code Smell Detection
-
Status: Active
-
Resolution: Unresolved
-
Labels:
-
Message:Replace this with a call to the "toFile().exists()" method
-
Highlighting:
-
Default Severity:Major
-
Impact:Low
-
Likelihood:High
-
Covered Languages:Java
-
Remediation Function:Constant/Issue
-
Constant Cost:5min
-
Analysis Scope:Main Sources, Test Sources
Description
The Files.exists method has noticeably poor performance in JDK 8, and can slow an application significantly when used to check files that don't actually exist.
The same goes for Files.notExists, Files.isDirectory and Files.isRegularFile from java.nio.file package.
Note that this rule is automatically disabled when the project's sonar.java.source is not 8.
Noncompliant Code Example
Path myPath; if(java.nio.file.Files.exists(myPath)) { // Noncompliant // do something }
Compliant Solution
Path myPath; if(myPath.toFile().exists())) { // do something }
See
Attachments
Issue Links
- is implemented by
-
SONARJAVA-1854 S3725 : Don't use Files.exists from JDK 8
-
- Closed
-