How to get the file path in SonarQube Java custom rule

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

  • 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:

@Override
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.

@Override
public void scanFile(JavaFileScannerContext context) {
   context.getInputFile();
   super.scanFile(context);
}

Hope it helps,
Quentin