Package org.sonar.api.batch.sensor
Interface SensorContext
-
public interface SensorContextSeeSensor.execute(SensorContext)In order to write unit tests you can use SensorContextTester, available in sonar-plugin-api-impl- Since:
- 5.1
-
-
Method Summary
All Methods Instance Methods Abstract Methods Deprecated Methods Modifier and Type Method Description ActiveRulesactiveRules()Get list of active rules.voidaddAnalysisData(java.lang.String key, java.lang.String mimeType, java.io.InputStream data)Internal service to send data for reporting.voidaddContextProperty(java.lang.String key, java.lang.String value)Add a property to the scanner context.voidaddTelemetryProperty(java.lang.String key, java.lang.String value)Add a property to the Sensor telemetry.booleancanSkipUnchangedFiles()In some situations, there is no need to analyze unchanged files, as reported byInputFile.status()asInputFile.Status.SAME, and analyzers can skip reporting any data for those files.Configurationconfig()Get settings of the project.FileSystemfileSystem()Get filesystem of the project.VersiongetSonarQubeVersion()Deprecated.since 10.0.booleanisCacheEnabled()Returns true if caching is enabled.booleanisCancelled()Test if a cancellation of the analysis was requested.voidmarkAsUnchanged(InputFile inputFile)Marks a file as not having any data changed since the last analysis, including issues.voidmarkForPublishing(InputFile inputFile)Indicate that a file should be published in the report sent to SonarQube.InputModulemodule()Deprecated.since 7.6 modules are deprecated.NewAdHocRulenewAdHocRule()Fluent builder to create a newAdHocRule.NewAnalysisErrornewAnalysisError()Builder to declare errors that happened while processing a source file.NewCoveragenewCoverage()Builder to define coverage in a file.NewCpdTokensnewCpdTokens()Builder to define CPD tokens in a file.NewExternalIssuenewExternalIssue()Fluent builder to create a newExternalIssue.NewHighlightingnewHighlighting()Builder to define highlighting of a file.NewIssuenewIssue()Fluent builder to create a newIssue.<G extends java.io.Serializable>
NewMeasure<G>newMeasure()Fluent builder to create a newMeasure.NewSignificantCodenewSignificantCode()Builder to declare which parts of the code is significant code.NewSymbolTablenewSymbolTable()Builder to define symbol table of a file.WriteCachenextCache()Access object to write cache that will be stored and made available in a future analysis.ReadCachepreviousCache()Access object to read cached data.InputProjectproject()The current project.SonarRuntimeruntime()Runtime information, mainly: to be able to have different behaviours between SonarQube and SonarLint to enable new features depending on version of API available at runtimeSettingssettings()Deprecated.since 6.5 useconfig()
-
-
-
Method Detail
-
config
Configuration config()
Get settings of the project.- Since:
- 6.5
-
canSkipUnchangedFiles
boolean canSkipUnchangedFiles()
In some situations, there is no need to analyze unchanged files, as reported byInputFile.status()asInputFile.Status.SAME, and analyzers can skip reporting any data for those files. Plugins should be prepared for the flag to be always false in the future.- Since:
- 9.4
-
fileSystem
FileSystem fileSystem()
Get filesystem of the project.
-
activeRules
ActiveRules activeRules()
Get list of active rules.
-
module
@Deprecated InputModule module()
Deprecated.since 7.6 modules are deprecated. Useproject()instead.- Throws:
java.lang.UnsupportedOperationException- for globalProjectSensors- Since:
- 5.5
-
project
InputProject project()
The current project.- Since:
- 7.6
-
getSonarQubeVersion
@Deprecated(since="10.0") Version getSonarQubeVersion()
Deprecated.since 10.0. The name is confusing: the API version is different from the SonarQube version since the extraction. Use the advised replacementVersion of API at runtime, not at compilation time. It's a shortcut onruntime().getApiVersion()since 6.0.- Since:
- 5.5
- See Also:
since version 6.0.
-
runtime
SonarRuntime runtime()
Runtime information, mainly:- to be able to have different behaviours between SonarQube and SonarLint
- to enable new features depending on version of API available at runtime
- Since:
- 6.0
-
isCancelled
boolean isCancelled()
Test if a cancellation of the analysis was requested. Sensors should periodically test this flag and gracefully stop if value istrue. For example it could be tested between each processed file.- Since:
- 6.0
-
newMeasure
<G extends java.io.Serializable> NewMeasure<G> newMeasure()
Fluent builder to create a newMeasure. Don't forget to callNewMeasure.save()once all parameters are provided.
-
newIssue
NewIssue newIssue()
Fluent builder to create a newIssue. Don't forget to callNewIssue.save()once all parameters are provided.
-
newExternalIssue
NewExternalIssue newExternalIssue()
Fluent builder to create a newExternalIssue. Don't forget to callNewExternalIssue.save()once all parameters are provided.- Since:
- 7.2
-
newAdHocRule
NewAdHocRule newAdHocRule()
Fluent builder to create a newAdHocRule. Don't forget to callNewAdHocRule.save()once all parameters are provided.- Since:
- 7.4
-
newHighlighting
NewHighlighting newHighlighting()
Builder to define highlighting of a file. Don't forget to callNewHighlighting.save()once all elements are provided.
-
newSymbolTable
NewSymbolTable newSymbolTable()
Builder to define symbol table of a file. Don't forget to callNewSymbolTable.save()once all symbols are provided.- Since:
- 5.6
-
newCoverage
NewCoverage newCoverage()
Builder to define coverage in a file. Don't forget to callNewCoverage.save().
-
newCpdTokens
NewCpdTokens newCpdTokens()
Builder to define CPD tokens in a file. Don't forget to callNewCpdTokens.save().- Since:
- 5.5
-
newAnalysisError
NewAnalysisError newAnalysisError()
Builder to declare errors that happened while processing a source file. Don't forget to callNewAnalysisError.save().- Since:
- 6.0
-
newSignificantCode
NewSignificantCode newSignificantCode()
Builder to declare which parts of the code is significant code. Ranges that are not reported as significant code will be ignored and won't be considered when calculating which lines were modified. If the significant code is not reported for a file, it is assumed that the entire file is significant code.- Since:
- 7.2
-
addContextProperty
void addContextProperty(java.lang.String key, java.lang.String value)Add a property to the scanner context. This context is available in Compute Engine when processing the report.
The properties starting with"sonar.analysis."are included to the payload of webhooks.- Throws:
java.lang.IllegalArgumentException- if key or value parameter is null- Since:
- 6.1
- See Also:
PostProjectAnalysisTask.ProjectAnalysis.getScannerContext()
-
markForPublishing
void markForPublishing(InputFile inputFile)
Indicate that a file should be published in the report sent to SonarQube. Files are automatically marked if any data is created for it (issues, highlighting, coverage, etc.).- Since:
- 6.3
-
markAsUnchanged
@Beta void markAsUnchanged(InputFile inputFile)
Marks a file as not having any data changed since the last analysis, including issues. The platform might perform optimizations based on this flag. The sensor is still expected to report all data for the file.- Since:
- 9.9
-
nextCache
@Beta WriteCache nextCache()
Access object to write cache that will be stored and made available in a future analysis. If cache is disabled, the methods in the returned object will have no effect. This API is experimental and can be changed or dropped at any time.- Since:
- 9.4
- See Also:
isCacheEnabled()
-
previousCache
@Beta ReadCache previousCache()
Access object to read cached data. The origin of the cached data is not specified and could come from a different branch. If cache is disabled, the methods in the returned object will have no effect. This API is experimental and can be changed or dropped at any time.- Since:
- 9.4
- See Also:
isCacheEnabled()
-
isCacheEnabled
@Beta boolean isCacheEnabled()
Returns true if caching is enabled. This API is experimental and can be changed or dropped at any time.- Since:
- 9.4
- See Also:
nextCache(),previousCache()
-
addTelemetryProperty
void addTelemetryProperty(java.lang.String key, java.lang.String value)Add a property to the Sensor telemetry. Telemetry stats are collected by Sonar products for each sensors (like Sensor duration).
Sensors can attach additional properties to the telemetry to provide more context to the stats. For performance reason, the telemetry properties should be limited to a small set of key-value pairs. For GDPR compliance, the telemetry properties should not contain any personal or identifying data.
This method can not be used by plugins not developed by SonarSource SA.- Parameters:
key- The key must follow this convention: <pluginKey>.<entryKey>. Example: cfamily.qualityIndex- Throws:
java.lang.IllegalArgumentException- if key or value parameter is nulljava.lang.IllegalStateException- if the method is called by a plugin not developed by SonarSource SA- Since:
- 10.9
-
addAnalysisData
@Beta void addAnalysisData(java.lang.String key, java.lang.String mimeType, java.io.InputStream data)
Internal service to send data for reporting.- Parameters:
key- The key must follow this convention: <plugin_key>.<entry_key>. where the plugin_key is your plugin key. Example: architecture.file_graph.jvmmimeType- A valid MIME type, usage of custom type and additional parameters is encouraged.data- The binary data to be added. The InputStream will be consumed once then closed during the method execution.- Throws:
java.lang.IllegalArgumentException- if key, mimeType, or data parameter is null or invalidjava.lang.IllegalStateException- if the method is called by a plugin not developed by SonarSource SA- Since:
- 11.3
-
-