Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-3725

Java 8's "Files.exists" should not be used

    XMLWordPrintable

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Replace this with a call to the "toFile().exists()" method
    • Highlighting:
      Hide

      Files.exists

      Show
      Files.exists
    • 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

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              nicolas.peru Nicolas Peru
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated: