Interface InputFile

  • All Superinterfaces:
    IndexedFile, InputComponent, InputPath

    public interface InputFile
    extends IndexedFile
    This layer over File adds information for code analyzers. For unit testing purpose, use TestInputFileBuilder available in sonar-plugin-api-impl and initialize the needed fields:
       new TestInputFileBuilder("moduleKey", "relative/path/from/module/baseDir.java")
         .setModuleBaseDir(path)
         .build();
     
    Since:
    4.2
    • Method Detail

      • relativePath

        @Deprecated
        java.lang.String relativePath()
        Deprecated.
        since 6.6 use inputStream() for file content, IndexedFile.filename() for file name, IndexedFile.uri() for an unique identifier, and toString() for logging
        Relative path to module (for normal Sensors) or project (for global Sensors) base directory. File separator is the forward slash ('/'), even on Microsoft Windows.
        Returns src/main/java/com/Foo.java if module base dir is /path/to/module and if file is /path/to/module/src/main/java/com/Foo.java.
        Relative path is not null and is normalized ('foo/../foo' is replaced by 'foo').
        Specified by:
        relativePath in interface IndexedFile
        Specified by:
        relativePath in interface InputPath
        See Also:
        relativePath(), InputDir.relativePath()
      • language

        @CheckForNull
        java.lang.String language()
        Language, for example "java" or "php". Can be null if indexation of all files is enabled and no language claims to support the file.
        Specified by:
        language in interface IndexedFile
      • inputStream

        java.io.InputStream inputStream()
                                 throws java.io.IOException
        Creates a stream of the file's contents. Depending on the runtime context, the source might be a file in a physical or virtual filesystem. Typically, it won't be buffered. The stream must be closed by the caller. Since 6.4 BOM is automatically filtered out.
        Specified by:
        inputStream in interface IndexedFile
        Throws:
        java.io.IOException
        Since:
        6.2
      • contents

        java.lang.String contents()
                           throws java.io.IOException
        Fetches the entire contents of the file, decoding with the charset(). Since 6.4 BOM is automatically filtered out.
        Throws:
        java.io.IOException
        Since:
        6.2
      • status

        @Deprecated
        InputFile.Status status()
        Deprecated.
        since 7.7 preview/issue mode was removed
      • lines

        int lines()
        Number of physical lines. This method supports all end-of-line characters. Formula is (number of line break + 1).

        Returns 1 if the file is empty.
        Returns 2 for foo\nbar.
        Returns 3 for foo\nbar\n.

      • isEmpty

        boolean isEmpty()
        Check if the file content is empty (ignore potential BOM).
        Since:
        5.2
      • newPointer

        TextPointer newPointer​(int line,
                               int lineOffset)
        Returns a TextPointer in the given file.
        Parameters:
        line - Line of the pointer. Start at 1.
        lineOffset - Offset in the line. Start at 0.
        Throws:
        java.lang.IllegalArgumentException - if line or offset is not valid for the given file.
        Since:
        5.2
      • newRange

        TextRange newRange​(TextPointer start,
                           TextPointer end)
        Returns a TextRange in the given file.
        Parameters:
        start - start pointer
        end - end pointer
        Throws:
        java.lang.IllegalArgumentException - if start or stop pointers are not valid for the given file.
        Since:
        5.2
      • newRange

        TextRange newRange​(int startLine,
                           int startLineOffset,
                           int endLine,
                           int endLineOffset)
        Returns a TextRange in the given file.
        • newRange(1, 0, 1, 1) selects the first character at line 1
        • newRange(1, 0, 1, 10) selects the 10 first characters at line 1
        Throws:
        java.lang.IllegalArgumentException - if start or stop positions are not valid for the given file.
        Since:
        5.2
      • selectLine

        TextRange selectLine​(int line)
        Returns a TextRange in the given file that select the full line.
        Parameters:
        line - Start at 1.
        Throws:
        java.lang.IllegalArgumentException - if line is not valid for the given file.
        Since:
        5.2
      • charset

        java.nio.charset.Charset charset()
        Charset to be used to decode this specific file.
        Since:
        6.0
      • toString

        java.lang.String toString()
        Return a string to identify this file (suitable for logs).
        Overrides:
        toString in class java.lang.Object