How to get the file path in SonarQube Java custom rule

  • which versions are you using (SonarQube, Scanner, Plugin, and any relevant extension): sonarqube-, sonar-scanner-

  • what are you trying to achieve:
    I’m creating a new java custom rule.
    How can I get the path of the file that is checked relative to the project path?
    Or maybe, How do I get the project path?
    Example of the code I’m trying to achieve:

public void visitNode(Tree tree) {
    String pathToFileInsideTheProjectIScan = tree.getPath(); // Couldn't find anything like 'getPath()'
  • what have you tried so far to achieve this
    Tried to search online but couldn’t find any solution.

Hello @alfonsodavies

You can get the path of the current file being analyzed by overriding SubscriptionVisitor.scanFile.

public void scanFile(JavaFileScannerContext context) {

Hope it helps,