@Override public Object visit(ASTCompilationUnit cUnit, Object data) { for (Comment comment : cUnit.getComments()) { if (hasTooManyLines(comment)) { addViolationWithMessage( data, cUnit, this.getMessage() + ": Too many lines", comment.getBeginLine(), comment.getEndLine()); } List<Integer> lineNumbers = overLengthLineIndicesIn(comment); if (lineNumbers.isEmpty()) { continue; } for (Integer lineNum : lineNumbers) { addViolationWithMessage( data, cUnit, this.getMessage() + ": Line too long", lineNum, lineNum); } } return super.visit(cUnit, data); }
private List<Integer> overLengthLineIndicesIn(Comment comment) { int maxLength = getProperty(MAX_LINE_LENGTH); List<Integer> indicies = new ArrayList<>(); String[] lines = comment.getImage().split(CR); int offset = comment.getBeginLine(); for (int i = 0; i < lines.length; i++) { String cleaned = withoutCommentMarkup(lines[i]); if (cleaned.length() > maxLength) { indicies.add(i + offset); } } return indicies; }