private static void validateMaxLine(Map<Integer, Integer> m, InputFile inputFile) { int maxLine = inputFile.lines(); for (int line : m.keySet()) { if (line > maxLine) { throw new IllegalStateException(String.format("Can't create measure for line %d for file '%s' with %d lines", line, inputFile, maxLine)); } } }
public static FileHashes create(InputFile f) { final byte[][] hashes = new byte[f.lines()][]; FileMetadata.computeLineHashesForIssueTracking(f, (lineIdx, hash) -> hashes[lineIdx - 1] = hash); int size = hashes.length; Multimap<String, Integer> linesByHash = LinkedHashMultimap.create(); String[] hexHashes = new String[size]; for (int i = 0; i < size; i++) { String hash = hashes[i] != null ? Hex.encodeHexString(hashes[i]) : ""; hexHashes[i] = hash; // indices in array are shifted one line before linesByHash.put(hash, i + 1); } return new FileHashes(hexHashes, linesByHash); }
private static void storeZeroCoverageForEachExecutableLine(final SensorContext context, InputFile f, DefaultMeasure<String> execLines) { NewCoverage newCoverage = context.newCoverage().onFile(f); Map<Integer, Integer> lineMeasures = KeyValueFormat.parseIntInt((String) execLines.value()); for (Map.Entry<Integer, Integer> lineMeasure : lineMeasures.entrySet()) { int lineIdx = lineMeasure.getKey(); if (lineIdx <= f.lines() && lineMeasure.getValue() > 0) { newCoverage.lineHits(lineIdx, 0); } } newCoverage.save(); }
@Override public synchronized void blameResult(InputFile file, List<BlameLine> lines) { Preconditions.checkNotNull(file); Preconditions.checkNotNull(lines); Preconditions.checkArgument(allFilesToBlame.contains(file), "It was not expected to blame file %s", file); if (lines.size() != file.lines()) { LOG.debug("Ignoring blame result since provider returned {} blame lines but file {} has {} lines", lines.size(), file, file.lines()); return; } Builder scmBuilder = ScannerReport.Changesets.newBuilder(); DefaultInputFile inputFile = (DefaultInputFile) file; scmBuilder.setComponentRef(inputFile.scannerId()); Map<String, Integer> changesetsIdByRevision = new HashMap<>(); int lineId = 1; for (BlameLine line : lines) { validateLine(line, lineId, file); Integer changesetId = changesetsIdByRevision.get(line.revision()); if (changesetId == null) { addChangeset(scmBuilder, line); changesetId = scmBuilder.getChangesetCount() - 1; changesetsIdByRevision.put(line.revision(), changesetId); } scmBuilder.addChangesetIndexByLine(changesetId); lineId++; } writer.writeComponentChangesets(scmBuilder.build()); allFilesToBlame.remove(file); count++; progressReport.message(count + "/" + total + " files analyzed"); }
@Override public void execute(SensorContext context) { Iterator<InputFile> inputFiles = context.fileSystem().inputFiles(context.fileSystem().predicates().all()).iterator(); if (!inputFiles.hasNext()) { throw new IllegalStateException("No files indexed"); } InputFile file = inputFiles.next(); context.newHighlighting() .onFile(file) .highlight(file.selectLine(1), TypeOfText.CONSTANT) .save(); context.newHighlighting() .onFile(file) .highlight(file.selectLine(file.lines()), TypeOfText.COMMENT) .save(); } }
private static void createIssues(InputFile file, SensorContext context, String repo) { RuleKey ruleKey = RuleKey.of(repo, RULE_KEY); for (int line = 1; line <= file.lines(); line++) { TextRange text = file.selectLine(line); // do not count empty lines, which can be a pain with end-of-file return if (text.end().lineOffset() == 0) { continue; } NewIssue newIssue = context.newIssue(); newIssue .forRule(ruleKey) .at(newIssue.newLocation() .on(file) .at(text) .message("This bug issue is generated on each line")) .save(); } }
private static void createIssues(InputFile file, SensorContext context) { for (int line = 1; line <= file.lines(); line++) { NewExternalIssue newIssue = context.newExternalIssue(); newIssue .engineId(ENGINE_ID) .ruleId(RULE_ID) .at(newIssue.newLocation() .on(file) .at(file.selectLine(line)) .message("This issue is generated on each line and the rule is predefined")) .severity(Severity.valueOf(SEVERITY)) .remediationEffortMinutes(EFFORT) .type(TYPE) .save(); } } }
private static void createIssues(InputFile file, SensorContext context) { for (int line = 1; line <= file.lines(); line++) { NewExternalIssue newIssue = context.newExternalIssue(); newIssue .engineId(ENGINE_ID) .ruleId(RULE_ID) .at(newIssue.newLocation() .on(file) .at(file.selectLine(line)) .message("This issue is generated on each line")) .severity(Severity.valueOf(SEVERITY)) .remediationEffortMinutes(EFFORT) .type(TYPE) .save(); } } }
private void createIssues(InputFile file, SensorContext context, String repo) { RuleKey ruleKey = RuleKey.of(repo, RULE_KEY); String severity = context.settings().getString(FORCE_SEVERITY_PROPERTY); for (int line = 1; line <= file.lines(); line++) { NewIssue newIssue = context.newIssue(); newIssue .forRule(ruleKey) .at(newIssue.newLocation() .on(file) .at(file.selectLine(line)) .message("This issue is generated on each line")) .overrideSeverity(severity != null ? Severity.valueOf(severity) : null); if (context.getSonarQubeVersion().isGreaterThanOrEqual(Version.create(5, 5))) { newIssue.gap(context.settings().getDouble(EFFORT_TO_FIX_PROPERTY)); } else { newIssue.gap(context.settings().getDouble(EFFORT_TO_FIX_PROPERTY)); } newIssue.save(); } }
private static void createIssues(InputFile file, SensorContext context) { for (int line = 1; line <= file.lines(); line++) { NewExternalIssue newIssue = context.newExternalIssue(); newIssue .engineId(OnePredefinedRuleExternalIssuePerLineSensor.ENGINE_ID) .ruleId(OnePredefinedRuleExternalIssuePerLineSensor.RULE_ID) .at(newIssue.newLocation() .on(file) .at(file.selectLine(line)) .message("This issue is generated on each line and the rule is predefined")) .severity(Severity.valueOf(OnePredefinedRuleExternalIssuePerLineSensor.SEVERITY)) .remediationEffortMinutes(OnePredefinedRuleExternalIssuePerLineSensor.EFFORT) .type(OnePredefinedRuleExternalIssuePerLineSensor.TYPE) .save(); // Even if the issue is on a predefined rule, the sensor is declaring an adHoc rule => this info should be ignored context.newAdHocRule() .engineId(OnePredefinedRuleExternalIssuePerLineSensor.ENGINE_ID) .ruleId(OnePredefinedRuleExternalIssuePerLineSensor.RULE_ID) .name("An ad hoc rule") .description("blah blah") .severity(Severity.BLOCKER) .type(RuleType.BUG) .save(); } } }
@Override public DefaultIssue atLine(int line) { Preconditions.checkState(this.path != null && this.path instanceof InputFile, "atLine should be called after onFile."); Preconditions.checkArgument(line > 0, "line starts at 1, invalid value " + line + "."); int lines = ((InputFile) path).lines(); Preconditions.checkArgument(line <= lines, "File " + path + " has " + lines + " lines. Unable to create issue at line " + line + "."); this.line = line; return this; }
private static void validateMaxLine(Map<Integer, Integer> m, InputFile inputFile) { int maxLine = inputFile.lines(); for (int line : m.keySet()) { if (line > maxLine) { throw new IllegalStateException(String.format("Can't create measure for line %d for file '%s' with %d lines", line, inputFile, maxLine)); } } }
public int fileLength(File file) { return inputFromIOFile(file).lines(); }
private static void validateMaxLine(Map<Integer, Integer> m, InputFile inputFile) { int maxLine = inputFile.lines(); for (int l : m.keySet()) { if (l > maxLine) { throw new IllegalStateException(String.format("Can't create measure for line %d for file '%s' with %d lines", l, inputFile.absolutePath(), maxLine)); } } }
FileData(InputFile inputFile) { linesInFile = inputFile.lines(); filename = inputFile.relativePath(); }
FileData(InputFile inputFile) { linesInFile = inputFile.lines(); filename = inputFile.relativePath(); }
private static void storeZeroCoverageForEachExecutableLine(final SensorContext context, InputFile f, DefaultMeasure<String> execLines) { NewCoverage newCoverage = context.newCoverage().onFile(f); Map<Integer, Integer> lineMeasures = KeyValueFormat.parseIntInt((String) execLines.value()); for (Map.Entry<Integer, Integer> lineMeasure : lineMeasures.entrySet()) { int lineIdx = lineMeasure.getKey(); if (lineIdx <= f.lines() && lineMeasure.getValue() > 0) { newCoverage.lineHits(lineIdx, 0); } } newCoverage.save(); }