Search in sources :

Example 6 with SensorContext

use of org.sonar.api.batch.sensor.SensorContext in project sonarlint-core by SonarSource.

the class GlobalSensor method execute.

@Override
public void execute(final SensorContext context) {
    long timeBefore = clock.millis();
    RuleKey globalRuleKey = RuleKey.of(GlobalRulesDefinition.KEY, GlobalRulesDefinition.RULE_KEY);
    ActiveRule activeGlobalRule = context.activeRules().find(globalRuleKey);
    if (activeGlobalRule != null) {
        Stream.of("stringParam", "textParam", "intParam", "boolParam", "floatParam", "enumParam", "enumListParam", "multipleIntegersParam").map(k -> Arrays.asList(k, activeGlobalRule.param(k))).forEach(kv -> LOGGER.info("Param {} has value {}", kv.get(0), kv.get(1)));
    } else {
        LOGGER.error("Rule is not active");
    }
    for (InputFile f : context.fileSystem().inputFiles(context.fileSystem().predicates().all())) {
        NewIssue newIssue = context.newIssue();
        newIssue.forRule(globalRuleKey).at(newIssue.newLocation().on(f).message("Issue number " + GlobalExtension.getInstance().getAndInc())).save();
    }
    long timeAfter = clock.millis();
    LOGGER.info(String.format("Executed Global Sensor in %d ms", timeAfter - timeBefore));
}
Also used : InputFile(org.sonar.api.batch.fs.InputFile) ActiveRule(org.sonar.api.batch.rule.ActiveRule) Loggers(org.sonar.api.utils.log.Loggers) Arrays(java.util.Arrays) Stream(java.util.stream.Stream) RuleKey(org.sonar.api.rule.RuleKey) Clock(java.time.Clock) Sensor(org.sonar.api.batch.sensor.Sensor) SensorContext(org.sonar.api.batch.sensor.SensorContext) NewIssue(org.sonar.api.batch.sensor.issue.NewIssue) Logger(org.sonar.api.utils.log.Logger) SensorDescriptor(org.sonar.api.batch.sensor.SensorDescriptor) RuleKey(org.sonar.api.rule.RuleKey) NewIssue(org.sonar.api.batch.sensor.issue.NewIssue) ActiveRule(org.sonar.api.batch.rule.ActiveRule) InputFile(org.sonar.api.batch.fs.InputFile)

Aggregations

SensorContext (org.sonar.api.batch.sensor.SensorContext)6 Test (org.junit.Test)3 SensorDescriptor (org.sonar.api.batch.sensor.SensorDescriptor)3 StreamSupport (java.util.stream.StreamSupport)2 FilePredicates (org.sonar.api.batch.fs.FilePredicates)2 InputFile (org.sonar.api.batch.fs.InputFile)2 Sensor (org.sonar.api.batch.sensor.Sensor)2 NewIssue (org.sonar.api.batch.sensor.issue.NewIssue)2 RuleKey (org.sonar.api.rule.RuleKey)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 Clock (java.time.Clock)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Objects (java.util.Objects)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1