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; }
@Override protected void checkDuplicatedBranches(List<Tree> branches) { Set<Tree> withIssue = new HashSet<>(); for (int i = 0; i < branches.size(); i++) { Tree currentBranch = branches.get(i); for (int j = i + 1; j < branches.size(); j++) { Tree comparedBranch = branches.get(j); if (!withIssue.contains(comparedBranch) && syntacticallyEqual(currentBranch, comparedBranch) && linesOfCodeForBranch(comparedBranch) > 1) { IssueLocation secondary = new IssueLocation(currentBranch, "Original"); String branchType = currentBranch instanceof SwitchClauseTree ? "case" : "branch"; String message = String.format(MESSAGE, branchType, secondary.startLine()); addIssue(comparedBranch, message).secondary(secondary); withIssue.add(comparedBranch); } } } }
private static void verifyIssue(TestIssue expected, Issue actual) { if (line(actual) > expected.line()) { fail("Missing issue at line " + expected.line()); } if (line(actual) < expected.line()) { fail("Unexpected issue at line " + line(actual) + ": \"" + message(actual) + "\""); } if (expected.message() != null) { assertThat(message(actual)).as("Bad message at line " + expected.line()).isEqualTo(expected.message()); } if (expected.effortToFix() != null) { assertThat(actual.cost()).as("Bad effortToFix at line " + expected.line()).isEqualTo((double) expected.effortToFix()); } if (expected.startColumn() != null) { assertThat(((PreciseIssue) actual).primaryLocation().startLineOffset() + 1).as("Bad start column at line " + expected.line()).isEqualTo(expected.startColumn()); } if (expected.endColumn() != null) { assertThat(((PreciseIssue) actual).primaryLocation().endLineOffset() + 1).as("Bad end column at line " + expected.line()).isEqualTo(expected.endColumn()); } if (expected.endLine() != null) { assertThat(((PreciseIssue) actual).primaryLocation().endLine()).as("Bad end line at line " + expected.line()).isEqualTo(expected.endLine()); } if (!expected.secondaryLocations().isEmpty()) { assertSecondary(actual, expected); } }
private static Collection<CheckMessage> getCheckMessages(List<Issue> issues) { List<CheckMessage> checkMessages = new ArrayList<>(); for (Issue issue : issues) { CheckMessage checkMessage; if (issue instanceof FileIssue) { FileIssue fileIssue = (FileIssue)issue; checkMessage = new CheckMessage(fileIssue.check(), fileIssue.message()); } else if (issue instanceof LineIssue) { LineIssue lineIssue = (LineIssue)issue; checkMessage = new CheckMessage(lineIssue.check(), lineIssue.message()); checkMessage.setLine(lineIssue.line()); } else { PreciseIssue preciseIssue = (PreciseIssue) issue; checkMessage = new CheckMessage(preciseIssue.check(), preciseIssue.primaryLocation().message()); checkMessage.setLine(preciseIssue.primaryLocation().startLine()); } if (issue.cost() != null) { checkMessage.setCost(issue.cost()); } checkMessages.add(checkMessage); } return checkMessages; }
private static IssueLocation secondary(int line, List<IssueLocation> allSecondaryLocations) { for (IssueLocation location : allSecondaryLocations) { if (location.startLine() == line) { return location; } } return null; }
private void addIssue(List<StatementTree> statementsAtLine) { PreciseIssue issue = addIssue(statementsAtLine.get(1), MESSAGE); for (int i = 2; i < statementsAtLine.size(); i++) { issue.secondary(new IssueLocation(statementsAtLine.get(i))); } } }
private static String message(Issue issue) { if (issue instanceof PreciseIssue) { return ((PreciseIssue) issue).primaryLocation().message(); } else { return ((LineIssue) issue).message(); } }
private static int line(Issue issue) { if (issue instanceof PreciseIssue) { return ((PreciseIssue) issue).primaryLocation().startLine(); } else { return ((LineIssue) issue).line(); } }
private void checkSuperInvokedOnlyOnce(MethodDeclarationTree tree, List<SuperTree> superTrees) { if (isConstructor(tree) && superTrees.size() > 1) { SuperTree firstSuper = superTrees.get(0); superTrees.stream() .skip(1) .forEach(s -> addIssue(s, MESSAGE_SUPER_INVOKED_ONCE).secondary(new IssueLocation(firstSuper))); } }
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 void raiseIssue(AccessorMethodDeclarationTree tree, String message) { addIssue(new PreciseIssue(this, new IssueLocation(tree, tree.name(), message))); }
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 void createIssue(Tree keyword, ExpressionTree condition) { LoopsShouldNotBeInfiniteCheck.this.addIssue(keyword, "Correct this loop's end condition as to not be invariant.") .secondary(new IssueLocation(condition, null)); }
public static IssueEntry from(Issue issue, File file) { if (issue instanceof PreciseIssue) { final PreciseIssue preciseIssue = (PreciseIssue) issue; final IssueLocation primaryLocation = preciseIssue.primaryLocation(); String locationBlurb = "P[" + primaryLocation.startLine() + "/" + primaryLocation.startLineOffset() + "," + primaryLocation.endLine() + "/" + primaryLocation.endLineOffset() + "]"; return new IssueEntry(primaryLocation.startLine(), primaryLocation.endLine(), primaryLocation.message(), locationBlurb, file); } if (issue instanceof LineIssue) { final LineIssue lineIssue = (LineIssue) issue; return new IssueEntry(lineIssue.line(), lineIssue.line(), lineIssue.message(), "L[" + lineIssue.line() + "]", file); } if (issue instanceof FileIssue) { final FileIssue fileIssue = (FileIssue) issue; return new IssueEntry(0, 0, fileIssue.message(), "F", file); } throw new IllegalArgumentException("Unsupported issue type : " + issue.getClass()); }
private void leaveScopeAndCheckNumberOfJump(SyntaxToken loopKeyword) { List<Tree> jumps = jumpTargets.pop().jumps; int jumpStatementNumber = jumps.size(); if (jumpStatementNumber > 1) { PreciseIssue issue = addIssue(loopKeyword, MESSAGE).cost((double) jumpStatementNumber - 1); for (Tree jump : jumps) { issue.secondary(new IssueLocation(jump)); } } }
private static void assertSecondary(Issue actualIssue, TestIssue expectedIssue) { List<Location> expectedLocations = expectedIssue.secondaryLocations(); List<IssueLocation> actualLocations = actualIssue instanceof PreciseIssue ? ((PreciseIssue) actualIssue).secondaryLocations() : new ArrayList<>(); String format = "Bad secondary location at line %s (issue at line %s): %s"; for (Location expected : expectedLocations) { IssueLocation actual = secondary(expected.line(), actualLocations); if (actual != null) { if (expected.message() != null) { assertThat(actual.message()).as(String.format(format, expected.line(), line(actualIssue), "bad message")).isEqualTo(expected.message()); } if (expected.startColumn() != null) { assertThat(actual.startLineOffset() + 1).as(String.format(format, expected.line(), line(actualIssue), "bad start column")).isEqualTo(expected.startColumn()); assertThat(actual.endLineOffset() + 1).as(String.format(format, expected.line(), line(actualIssue), "bad end column")).isEqualTo(expected.endColumn()); } actualLocations.remove(actual); } else { throw new AssertionError("Missing secondary location at line " + expected.line() + " for issue at line " + expectedIssue.line()); } } if (!actualLocations.isEmpty()) { IssueLocation location = actualLocations.get(0); throw new AssertionError("Unexpected secondary location at line " + location.startLine() + " for issue at line " + line(actualIssue)); } }
private static IssueLocation issueLocation(IfStatementTree tree, String message) { return new IssueLocation(tree.ifKeyword(), tree.closeParenthesisToken(), message); }
private static <T extends Tree> IssueLocation issueLocation(T tree, String message) { if (tree.is(Kind.NEW_EXPRESSION)) { NewExpressionTree newExpressionTree = (NewExpressionTree) tree; return new IssueLocation( newExpressionTree.newKeyword(), newExpressionTree.expression(), message ); } else { CallExpressionTree callExpressionTree = (CallExpressionTree) tree; return new IssueLocation( callExpressionTree.callee(), message ); } }
private void issueIfLineMismatch(SyntaxToken curlyBrace, SyntaxToken target) { CodeLine curlyBraceLine = new CodeLine(curlyBrace.line()); if (curlyBraceLine.isJustBelow(target.line())) { addIssue(new PreciseIssue(this, new IssueLocation(curlyBrace, "Move this open curly brace to the end of the previous line."))); } else if (curlyBraceLine.isFarBelow(target.line())) { addIssue(new PreciseIssue(this, new IssueLocation(curlyBrace, "Move this open curly brace to the end of line " + target.line() + "."))); } }
private void raiseIssue(FunctionTree tree, List<Tree> complexityTrees) { int complexity = complexityTrees.size(); String message = String.format(MESSAGE, complexity, maximumFunctionComplexityThreshold); IssueLocation primaryIssueLocation = new IssueLocation(tree.firstToken(), tree.parameterClause(), message); PreciseIssue issue = addIssue(new PreciseIssue(this, primaryIssueLocation)); for (Tree complexityTree : complexityTrees) { issue.secondary(complexityTree, "+1"); } issue.cost((double) complexity - maximumFunctionComplexityThreshold); }