SensorContext saveMeasure and AbstractCpdMapping alternatives

Hi All,
We are using below features of sonarqube 6.7.1:

  1. AbstractCpdMapping interface
  2. saveMeasure method in SensorContext interface
    Now we are moving to sonarqube 7.9.1 LTS and both these things are not available.
    Going through documentation, but having hard time figuring out how to replace these.
    With new version - Calculating CPD for 0 files. With old version - Calculating CPD for 9 files.
    The Dashboard is empty for new version.
    Any help, advice?

Hi.

As replacement for AbstractCpdMapping you should use NewCpdTokens. Example:

sensorContext.newCpdTokens().onFile(inputFile)
    .addToken(1, 10, 1, 15, "class")
    .addToken(1, 16, 1, 18, "IDENTIFIER")
    .save(); 

And the replacement for SensorContext.saveMeasure is SensorContext.newMeasure. Example:

sensorContext.<Integer>newMeasure()
    .on(inputFile)
    .forMetric(CoreMetrics.STATEMENTS)
    .withValue(10)
    .save()
4 Likes

Hi All,
On the similar lines, below line doesn’t work in sonarqube 7.9.1:
org.sonar.api.resources.File resource1 = org.sonar.api.resources.File.create(rv1.getFilename());
We need to create a resource from Filename, just the way we create file in Java.
Any idea about how can we achieve this in sonarqube 7.9.1?

Hi.

According to the docs: http://javadocs.sonarsource.org/6.7/apidocs/org/sonar/api/resources/File.html#create-java.lang.String-

@Deprecated public static File create(String relativePathFromBasedir)

Deprecated. since 5.1 use FileSystem.inputFile(org.sonar.api.batch.fs.FilePredicate)

And here is the example: http://javadocs.sonarsource.org/6.7/apidocs/org/sonar/api/batch/fs/FileSystem.html#inputFile-org.sonar.api.batch.fs.FilePredicate-

@CheckForNull 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"));

The fs variable in this case is the result of sensorContext.fileSystem().

1 Like

Hi Felipe,
Thanks a lot. Your replies turned out to be really helpful.
I might bug you for some more time, hope you don’t mind.
What is happening now is, we are able to load our plugin and scan projects for Sonarqube 7.9.1 installation.
However, we don’t see any issues in the Dashboard and Issues tab shows 0 for everything.
In output, I see one line as ‘Calculating CPD for 0 files’.
In our case, AbstractCpdMapping was being extended and as it is not available, I removed this parent relationship. Any idea about this?