Interface FileSystem

  • @ScannerSide
    public interface FileSystem
    The FileSystem manages all the source files to be analyzed.

    This is not an extension point so it must not be implemented by plugins. It must be injected as a constructor parameter :

     public class MySensor implements Sensor {
       private final FileSystem fs;
       public MySensor(FileSystem fs) {
         this.fs = fs;

    How to use in unit tests

    The unit tests needing an instance of FileSystem can use the implementation, available in sonar-plugin-api-impl, org.sonar.api.batch.fs.internal.DefaultFileSystem and the related org.sonar.api.batch.fs.internal.DefaultInputFile, for example :
     DefaultFileSystem fs = new DefaultFileSystem();
     fs.add(new DefaultInputFile("myprojectKey", "src/foo/bar.php"));
    • Method Detail

      • baseDir baseDir()
        Absolute base directory of module.
      • encoding

        java.nio.charset.Charset encoding()
        Default encoding of files in this project. If it's not defined, then the platform default encoding is returned. When reading an InputFile it is preferable to use InputFile.charset()
      • workDir workDir()
        Absolute work directory. It can be used to store third-party analysis reports.
        The work directory can be located outside baseDir().
      • inputFile

        InputFile inputFile​(FilePredicate predicate)
        Returns the single element matching the predicate. If more than one elements match the predicate, then IllegalArgumentException is thrown. Returns null if no files match.

        How to use :

         InputFile file = fs.inputFile(fs.predicates().hasRelativePath("src/Foo.php"));
        See Also:
      • inputDir

        InputDir inputDir​( dir)
        since 6.6 Ability to report issues or measures on directories will soon be dropped. Report issues on project if needed.
        Returns InputDir matching the current File.
        null if directory is not indexed.
        java.lang.IllegalArgumentException - is File is null or not a directory.
      • inputFiles

        java.lang.Iterable<InputFile> inputFiles​(FilePredicate predicate)
        Input files matching the given attributes. Return all the files if the parameter attributes is empty.

        Important - result is an Iterable to benefit from streaming and decreasing memory consumption. It should be iterated only once, else copy it into a list :

        How to use :

         FilePredicates p = fs.predicates();
         Iterable<InputFile> files = fs.inputFiles(p.and(p.hasLanguage("java"), p.hasType(InputFile.Type.MAIN)));
        See Also:
      • files

        java.lang.Iterable<> files​(FilePredicate predicate)
        since 6.6 Plugins should avoid working with File and prefer working with InputFile
        Files matching the given predicate.
        See Also:
      • languages

        java.util.SortedSet<java.lang.String> languages()
        Languages detected in all files, whatever their type (main or test)
      • resolvePath resolvePath​(java.lang.String path)
        Utility method mainly used to resolve location of reports.
        file in canonical form from specified path. Path can be absolute or relative to project basedir. For example resolvePath("pom.xml") or resolvePath("src/main/java")