Custom duplication detection rule

I’m writing my own duplicate code detection where a certain amount of reused data members/class variables will be detected as a code smell (data clump). I’m using the visitNode function of the IssuableSubscriptionVisitor class to scan for duplicate codes and store it somewhere else. Then, when the scan is complete, I want to know when the last visitNode would finish so I can call my issue reporting process. I’m currently using some functions I wrote to count how many files and classes there are to scan, and decrease the number every time visitNode executes, but I know it’s not the best thing to do and could add potential bugs. Are there ways to add callbacks or are there better ways to do what I’m trying to do?

Thanks in advance.

No, there’s no better way to do it as far as I know.
The callback you would need doesn’t exist because SonarJava itself doesn’t need it: it doesn’t have any rule which works on more than one source file.

In this case, is there a way to know how many input files there are? The method I’m using traces back to the ‘src’ folder and counts the number of java files under the directory. This probably won’t work in a lot of cases.

Hey @Jcseeco,

Strictly using a “Custom Rules” approach, I’m not sure it would be possible to do, as it has been designed to only provide rules to be execute on any file, one after another, in any order.

However, if you just need to count java files, and do not rely too much on SonarJava AST and API, you could use a custom Sensor, which would directly query the FileSystem:

import org.sonar.api.batch.sensor.Sensor;

public class MySensor implements Sensor {
  public MySensor(FileSystem fs) {
    Iterable<InputFile> javaFiles = fs.inputFiles(
      fs.predicates().and(
        fs.predicates().hasLanguage("java"), 
        fs.predicates().hasType(InputFile.Type.Main)
      )
    );
  }
}

However, we are now entering the world of custom plugins. About how to interface with the custom rule(s), this is another story on which I don’t really know what would be the best approach.

Hope this helps,
Michael

I’ve been trying this out by implementing Sensor class and extending BaseTreeVisitor or the IssualeSubscriptionVisitor at the same time.

Still can’t make my rule to work properly, that could be me not understanding custom rule enough, or maybe it’s just not possible.

Maybe I should change my approach towards data clump detection?

Thanks for your help, really appreciate it