Refine search
@Override public String toString() { return String.join("", elements) + "[" + detailAst.getLineNo() + "x" + detailAst.getColumnNo() + "]"; }
/** * Checks if saved coordinates located after given token. * @param ast given token. * @return true, if saved coordinates located after given token. */ public boolean isAfter(DetailAST ast) { final int lineNo = ast.getLineNo(); final int columnNo = ast.getColumnNo(); boolean isAfter = true; if (lineNo > endLineNo || lineNo == endLineNo && columnNo > endColumnNo) { isAfter = false; } return isAfter; }
/** * Whether the declaration is located before the checked ast. * @param ast1 the IDENT ast of the declaration. * @param ast2 the IDENT ast to check. * @return true, if the declaration is located before the checked ast. */ private static boolean checkPosition(DetailAST ast1, DetailAST ast2) { boolean result = false; if (ast1.getLineNo() < ast2.getLineNo() || ast1.getLineNo() == ast2.getLineNo() && ast1.getColumnNo() < ast2.getColumnNo()) { result = true; } return result; }
/** * Sets end coordinates from given token. * @param endToken token. */ public void setToken(DetailAST endToken) { if (!isAfter(endToken)) { endLineNo = endToken.getLineNo(); endColumnNo = endToken.getColumnNo(); } }
/** * Check whether the field is declared before the method call in case of * methods and initialization blocks. * @param field field to check. * @param objCalledOn object equals method called on. * @return true if the field is declared before the method call. */ private static boolean checkLineNo(DetailAST field, DetailAST objCalledOn) { boolean result = false; // Required for pitest coverage. We should specify columnNo passing condition // in such a way, so that the minimal possible distance between field and // objCalledOn will be the maximal condition to pass this check. // The minimal distance between objCalledOn and field (of type String) initialization // is calculated as follows: // String(6) + space(1) + variableName(1) + assign(1) + // anotherStringVariableName(1) + semicolon(1) = 11 // Example: length of "String s=d;" is 11 symbols. final int minimumSymbolsBetween = 11; if (field.getLineNo() < objCalledOn.getLineNo() || field.getLineNo() == objCalledOn.getLineNo() && field.getColumnNo() + minimumSymbolsBetween <= objCalledOn.getColumnNo()) { result = true; } return result; }
@Override public String toString() { return super.toString() + "[" + getLineNo() + "x" + getColumnNo() + "]"; }
/** * Handles a comment which is placed within the empty code block. * Note, if comment is placed at the end of the empty code block, we have Checkstyle's * limitations to clearly detect user intention of explanation target - above or below. The * only case we can assume as a violation is when a single line comment within the empty * code block has indentation level that is lower than the indentation level of the closing * right curly brace. For example: * <p> * {@code * if (a == true) { * // violation * } * } * </p> * * @param comment comment to check. * @param nextStmt next statement. */ private void handleCommentInEmptyCodeBlock(DetailAST comment, DetailAST nextStmt) { if (comment.getColumnNo() < nextStmt.getColumnNo()) { log(comment.getLineNo(), getMessageKey(comment), nextStmt.getLineNo(), comment.getColumnNo(), nextStmt.getColumnNo()); } }
/** * Checks whether right curly is alone on a line. * @param details for validation. * @param targetSrcLine A string with contents of rcurly's line * @return true if right curly is alone on a line. */ private static boolean isAloneOnLine(Details details, String targetSrcLine) { final DetailAST rcurly = details.rcurly; final DetailAST nextToken = details.nextToken; return (rcurly.getLineNo() != nextToken.getLineNo() || skipDoubleBraceInstInit(details)) && CommonUtil.hasWhitespaceBefore(details.rcurly.getColumnNo(), targetSrcLine); }
/** * Validate token on new Line position. * @param brace brace AST * @param startToken start Token * @param braceLine content of line with Brace */ private void validateNewLinePosition(DetailAST brace, DetailAST startToken, String braceLine) { // not on the same line if (startToken.getLineNo() + 1 == brace.getLineNo()) { if (CommonUtil.hasWhitespaceBefore(brace.getColumnNo(), braceLine)) { log(brace, MSG_KEY_LINE_PREVIOUS, OPEN_CURLY_BRACE, brace.getColumnNo() + 1); } else { log(brace, MSG_KEY_LINE_NEW, OPEN_CURLY_BRACE, brace.getColumnNo() + 1); } } else if (!CommonUtil.hasWhitespaceBefore(brace.getColumnNo(), braceLine)) { log(brace, MSG_KEY_LINE_NEW, OPEN_CURLY_BRACE, brace.getColumnNo() + 1); } }
/** * Finds sub-node for given node minimal (line, column) pair. * @param node the root of tree for search. * @return sub-node with minimal (line, column) pair. */ public static DetailAST getFirstNode(final DetailAST node) { DetailAST currentNode = node; DetailAST child = node.getFirstChild(); while (child != null) { final DetailAST newNode = getFirstNode(child); if (newNode.getLineNo() < currentNode.getLineNo() || newNode.getLineNo() == currentNode.getLineNo() && newNode.getColumnNo() < currentNode.getColumnNo()) { currentNode = newNode; } child = child.getNextSibling(); } return currentNode; }
/** * Logs comment which can have the same indentation level as next or previous statement. * @param comment comment. * @param nextStmt next statement. * @param prevStmt previous statement. */ private void logMultilineIndentation(DetailAST prevStmt, DetailAST comment, DetailAST nextStmt) { final String multilineNoTemplate = "%d, %d"; log(comment.getLineNo(), getMessageKey(comment), String.format(Locale.getDefault(), multilineNoTemplate, prevStmt.getLineNo(), nextStmt.getLineNo()), comment.getColumnNo(), String.format(Locale.getDefault(), multilineNoTemplate, getLineStart(prevStmt.getLineNo()), getLineStart(nextStmt.getLineNo()))); }
/** * Finds sub-node for given node maximum (line, column) pair. * @param node the root of tree for search. * @return sub-node with maximum (line, column) pair. */ private static DetailAST getLastNode(final DetailAST node) { DetailAST currentNode = node; DetailAST child = node.getFirstChild(); while (child != null) { final DetailAST newNode = getLastNode(child); if (newNode.getLineNo() > currentNode.getLineNo() || newNode.getLineNo() == currentNode.getLineNo() && newNode.getColumnNo() > currentNode.getColumnNo()) { currentNode = newNode; } child = child.getNextSibling(); } return currentNode; }
/** * Checks if current single line comment is trailing comment, e.g.: * <p> * {@code * double d = 3.14; // some comment * } * </p> * @param singleLineComment {@link TokenTypes#SINGLE_LINE_COMMENT single line comment}. * @return true if current single line comment is trailing comment. */ private boolean isTrailingSingleLineComment(DetailAST singleLineComment) { final String targetSourceLine = getLine(singleLineComment.getLineNo() - 1); final int commentColumnNo = singleLineComment.getColumnNo(); return !CommonUtil.hasWhitespaceBefore(commentColumnNo, targetSourceLine); }
/** * Process a token representing a left parentheses. * @param ast the token representing a left parentheses */ protected void processLeft(DetailAST ast) { final String line = getLines()[ast.getLineNo() - 1]; final int after = ast.getColumnNo() + 1; if (after < line.length()) { if (option == PadOption.NOSPACE && Character.isWhitespace(line.charAt(after))) { log(ast, MSG_WS_FOLLOWED, OPEN_PARENTHESIS); } else if (option == PadOption.SPACE && !Character.isWhitespace(line.charAt(after)) && line.charAt(after) != CLOSE_PARENTHESIS) { log(ast, MSG_WS_NOT_FOLLOWED, OPEN_PARENTHESIS); } } }
/** * Checks whether {@link TokenTypes#RPAREN} is a closing paren * of a {@link TokenTypes#TYPECAST}. * @param ast of a {@link TokenTypes#RPAREN} to check. * @return true if ast is a closing paren of a {@link TokenTypes#TYPECAST}. */ private static boolean isInTypecast(DetailAST ast) { boolean result = false; if (ast.getParent().getType() == TokenTypes.TYPECAST) { final DetailAST firstRparen = ast.getParent().findFirstToken(TokenTypes.RPAREN); if (firstRparen.getLineNo() == ast.getLineNo() && firstRparen.getColumnNo() == ast.getColumnNo()) { result = true; } } return result; }
@Override public void visitToken(DetailAST ast) { final String text = ast.getText(); final int colNo = ast.getColumnNo(); final int lineNo = ast.getLineNo(); final String currentLine = getLines()[lineNo - 1]; final String substringAfterToken = currentLine.substring(colNo + text.length()).trim(); final String substringBeforeToken = currentLine.substring(0, colNo).trim(); if (option == WrapOption.EOL && substringBeforeToken.isEmpty()) { log(ast, MSG_LINE_PREVIOUS, text); } else if (option == WrapOption.NL && substringAfterToken.isEmpty()) { log(ast, MSG_LINE_NEW, text); } }
/** * Checks if current comment block is trailing comment, e.g.: * <p> * {@code * double d = 3.14; /* some comment */ * /* some comment */ double d = 18.5; * } * </p> * @param blockComment {@link TokenTypes#BLOCK_COMMENT_BEGIN block comment begin}. * @return true if current comment block is trailing comment. */ private boolean isTrailingBlockComment(DetailAST blockComment) { final String commentLine = getLine(blockComment.getLineNo() - 1); final int commentColumnNo = blockComment.getColumnNo(); final DetailAST nextSibling = blockComment.getNextSibling(); return !CommonUtil.hasWhitespaceBefore(commentColumnNo, commentLine) || nextSibling != null && nextSibling.getLineNo() == blockComment.getLineNo(); }
@Override public void visitToken(DetailAST ast) { final String line = getLine(ast.getLineNo() - 1); final int before = ast.getColumnNo() - 1; if ((before == -1 || Character.isWhitespace(line.charAt(before))) && !isInEmptyForInitializerOrCondition(ast)) { boolean flag = !allowLineBreaks; // verify all characters before '.' are whitespace for (int i = 0; i <= before - 1; i++) { if (!Character.isWhitespace(line.charAt(i))) { flag = true; break; } } if (flag) { log(ast, MSG_KEY, ast.getText()); } } }
/** * Process a token representing a right parentheses. * @param ast the token representing a right parentheses */ protected void processRight(DetailAST ast) { final int before = ast.getColumnNo() - 1; if (before >= 0) { final String line = getLines()[ast.getLineNo() - 1]; if (option == PadOption.NOSPACE && Character.isWhitespace(line.charAt(before)) && !CommonUtil.hasWhitespaceBefore(before, line)) { log(ast, MSG_WS_PRECEDED, CLOSE_PARENTHESIS); } else if (option == PadOption.SPACE && !Character.isWhitespace(line.charAt(before)) && line.charAt(before) != OPEN_PARENTHESIS) { log(ast, MSG_WS_NOT_PRECEDED, CLOSE_PARENTHESIS); } } }
/** * Checks the token for the end of Generics. * @param ast the token to check */ private void processEnd(DetailAST ast) { final String line = getLine(ast.getLineNo() - 1); final int before = ast.getColumnNo() - 1; final int after = ast.getColumnNo() + 1; if (before >= 0 && Character.isWhitespace(line.charAt(before)) && !containsWhitespaceBefore(before, line)) { log(ast, MSG_WS_PRECEDED, CLOSE_ANGLE_BRACKET); } if (after < line.length()) { // Check if the last Generic, in which case must be a whitespace // or a '(),[.'. if (depth == 1) { processSingleGeneric(ast, line, after); } else { processNestedGenerics(ast, line, after); } } }