private void processLine(String fileLine, InputFile inputFile, NewSignificantCode significantCode) { String[] textPointer = fileLine.split(","); if (textPointer.length != 3) { throw new IllegalStateException("Invalid format in error file"); } try { int line = Integer.parseInt(textPointer[0]); int startLineOffset = Integer.parseInt(textPointer[1]); int endLineOffset = Integer.parseInt(textPointer[2]); significantCode.addRange(inputFile.newRange(line, startLineOffset, line, endLineOffset)); } catch (NumberFormatException e) { throw new IllegalStateException("Invalid format in error file", e); } }
@Override public NewCpdTokens addToken(int startLine, int startLineOffset, int endLine, int endLineOffset, String image) { checkInputFileNotNull(); TextRange newRange; try { newRange = inputFile.newRange(startLine, startLineOffset, endLine, endLineOffset); } catch (Exception e) { throw new IllegalArgumentException("Unable to register token in file " + inputFile, e); } return addToken(newRange, image); }
NewIssueLocation primaryLocation = newIssue.newLocation() .on(file) .at(file.newRange(entry.getValue(), endPositions.get(issueId))); newIssue.at(primaryLocation.message("Primary location")); if (startFlowsPositions.containsKey(issueId)) { NewIssueLocation newLocation = newIssue.newLocation() .on(file) .at(file.newRange(start, end)) .message("Flow step #" + flowNum); flowLocations.add(newLocation);
@Override protected void processFile(InputFile inputFile, SensorContext context, RuleKey ruleKey, String languageKey) { try { int[] lineCounter = {1}; try (InputStreamReader isr = new InputStreamReader(inputFile.inputStream(), inputFile.charset()); BufferedReader reader = new BufferedReader(isr)) { reader.lines().forEachOrdered(lineStr -> { int startIndex = -1; while ((startIndex = lineStr.indexOf(TAG, startIndex + 1)) != -1) { NewIssue newIssue = context.newIssue(); newIssue .forRule(ruleKey) .at(newIssue.newLocation() .on(inputFile) .at(inputFile.newRange(lineCounter[0], startIndex, lineCounter[0], startIndex + TAG.length()))) .save(); } lineCounter[0]++; }); } } catch (IOException e) { throw new IllegalStateException("Fail to process " + inputFile, e); } } }
.at(newIssue.newLocation() .on(inputFile) .at(inputFile.newRange(lineCounter[0], startIndex, lineCounter[0], startIndex + tag.length()))) .save();
private static TextRange toRange(InputFile file, ScannerReport.TextRange reportRange) { return file.newRange(file.newPointer(reportRange.getStartLine(), reportRange.getStartOffset()), file.newPointer(reportRange.getEndLine(), reportRange.getEndOffset())); }
@CheckForNull private static NewIssueLocation fillLocation(SensorContext context, NewIssueLocation newLocation, Location location) { InputFile file = findFile(context, location.filePath); if (file != null) { newLocation .on(file); if (location.message != null) { newLocation.message(location.message); } if (location.textRange != null) { if (location.textRange.startColumn != null) { TextPointer start = file.newPointer(location.textRange.startLine, location.textRange.startColumn); TextPointer end = file.newPointer(location.textRange.endLine, location.textRange.endColumn); newLocation.at(file.newRange(start, end)); } else { newLocation.at(file.selectLine(location.textRange.startLine)); } } return newLocation; } return null; }
@Test public void fail_if_range_includes_many_lines() { underTest.onFile(inputFile); exception.expect(IllegalArgumentException.class); exception.expectMessage("Ranges of significant code must be located in a single line"); underTest.addRange(inputFile.newRange(1, 1, 2, 1)); }
@Override public NewCpdTokens addToken(int startLine, int startLineOffset, int endLine, int endLineOffset, String image) { checkInputFileNotNull(); TextRange newRange; try { newRange = inputFile.newRange(startLine, startLineOffset, endLine, endLineOffset); } catch (Exception e) { throw new IllegalArgumentException("Unable to register token in file " + inputFile, e); } return addToken(newRange, image); }
private static TextRange getLocation(TslintError tslintError, InputFile inputFile) { if (samePosition(tslintError.startPosition, tslintError.endPosition)) { // tslint allows issue location with 0 length, SonarQube doesn't allow that return inputFile.selectLine(tslintError.startPosition.line + 1); } else { return inputFile.newRange( tslintError.startPosition.line + 1, tslintError.startPosition.character, tslintError.endPosition.line + 1, tslintError.endPosition.character); } }
private static TextRange getLocation(EslintError eslintError, InputFile inputFile) { if (eslintError.endLine == 0 || eslintError.isZeroLengthRange()) { // eslint can have issues only with start or with zero length range return inputFile.selectLine(eslintError.line); } else { return inputFile.newRange( eslintError.line, eslintError.column - 1, eslintError.endLine, eslintError.endColumn - 1); } }
private static TextRange getLocation(EslintError eslintError, InputFile inputFile) { if (eslintError.endLine == 0 || eslintError.isZeroLengthRange()) { // eslint can have issues only with start or with zero length range return inputFile.selectLine(eslintError.line); } else { return inputFile.newRange( eslintError.line, eslintError.column - 1, eslintError.endLine, eslintError.endColumn - 1); } }
public JavaIssue addSecondaryLocation(InputFile file, int startLine, int startLineOffset, int endLine, int endLineOffset, String message) { newIssue.addLocation( newIssue.newLocation() .on(file) .at(file.newRange(startLine, startLineOffset, endLine, endLineOffset)) .message(message)); return this; }
private TextRange textRange(StartElement violationElement) { Integer beginLine = getAttributeAsInt(violationElement, "beginline"); try { Integer endLine = getAttributeAsInt(violationElement, "endline"); Integer beginColumn = getAttributeAsInt(violationElement, "begincolumn"); Integer endColumn = getAttributeAsInt(violationElement, "endcolumn"); return inputFile.newRange(beginLine, beginColumn - 1, endLine, endColumn); } catch (RuntimeException e) { // Some PMD rules seem to report invalid line offsets, e.g. TooManyStaticImports return inputFile.selectLine(beginLine); } }
public JavaIssue addSecondaryLocation(InputFile file, int startLine, int startLineOffset, int endLine, int endLineOffset, String message) { newIssue.addLocation( newIssue.newLocation() .on(file) .at(file.newRange(startLine, startLineOffset, endLine, endLineOffset)) .message(message)); return this; }
private TextRange textRange(StartElement violationElement) { Integer beginLine = getAttributeAsInt(violationElement, "beginline"); try { Integer endLine = getAttributeAsInt(violationElement, "endline"); Integer beginColumn = getAttributeAsInt(violationElement, "begincolumn"); Integer endColumn = getAttributeAsInt(violationElement, "endcolumn"); return inputFile.newRange(beginLine, beginColumn - 1, endLine, endColumn); } catch (RuntimeException e) { // Some PMD rules seem to report invalid line offsets, e.g. TooManyStaticImports return inputFile.selectLine(beginLine); } }
private static NewIssueLocation newLocation(InputFile inputFile, NewIssue issue, IssueLocation location) { TextRange range = inputFile.newRange( location.startLine(), location.startLineOffset(), location.endLine(), location.endLineOffset()); NewIssueLocation newLocation = issue.newLocation() .on(inputFile) .at(range); if (location.message() != null) { newLocation.message(location.message()); } return newLocation; }
private static NewIssueLocation newLocation(InputFile inputFile, NewIssue issue, IssueLocation location) { TextRange range = inputFile.newRange( location.startLine(), location.startLineOffset(), location.endLine(), location.endLineOffset()); NewIssueLocation newLocation = issue.newLocation() .on(inputFile) .at(range); if (location.message() != null) { newLocation.message(location.message()); } return newLocation; }
private static NewIssueLocation newLocation(InputFile inputFile, NewIssue issue, IssueLocation location) { TextRange range = inputFile.newRange(location.startLine(), location.startLineOffset(), location.endLine(), location.endLineOffset()); NewIssueLocation newLocation = issue.newLocation() .on(inputFile) .at(range); if (location.message() != null) { newLocation.message(location.message()); } return newLocation; }