/** * Extracts the name of an ast. * @param ast to extract the name * @return the text content of the inner {@code TokenTypes.IDENT} node */ private static String getName(DetailAST ast) { return ast.findFirstToken(TokenTypes.IDENT).getText(); }
@Override public boolean isValidOn(final DetailAST ast) { final int astType = ast.getType(); final DetailAST methodNameAst = ast.findFirstToken(TokenTypes.IDENT); final String methodName = methodNameAst.getText(); return astType == TokenTypes.METHOD_DEF && ("writeObject".equals(methodName) || "readObject".equals(methodName) || "writeExternal".equals(methodName) || "readExternal".equals(methodName) || "writeReplace".equals(methodName) || "readResolve".equals(methodName)); }
/** * Examine char literal for initializing to default value. * @param exprStart expression * @return true is literal is initialized by zero symbol */ private static boolean isZeroChar(DetailAST exprStart) { return isZero(exprStart) || exprStart.getType() == TokenTypes.CHAR_LITERAL && "'\\0'".equals(exprStart.getText()); }
@Override public void beginTree(DetailAST rootAST) { publicTypeFound = false; lineNumberTypeMap.clear(); DetailAST currentNode = rootAST; while (currentNode != null) { if (currentNode.getType() == TokenTypes.CLASS_DEF || currentNode.getType() == TokenTypes.ENUM_DEF || currentNode.getType() == TokenTypes.INTERFACE_DEF) { if (isPublic(currentNode)) { publicTypeFound = true; } else { final String typeName = currentNode .findFirstToken(TokenTypes.IDENT).getText(); lineNumberTypeMap.put(currentNode.getLineNo(), typeName); } } currentNode = currentNode.getNextSibling(); } }
/** * Process Import. * @param ast token * @param nextToken next token */ private void processImport(DetailAST ast, DetailAST nextToken) { if (nextToken.getType() != TokenTypes.IMPORT && nextToken.getType() != TokenTypes.STATIC_IMPORT && !hasEmptyLineAfter(ast)) { log(nextToken.getLineNo(), MSG_SHOULD_BE_SEPARATED, nextToken.getText()); } }
@Override public void visitToken(DetailAST aAST) { final DetailAST mid = aAST.findFirstToken(TokenTypes.IDENT); final String name = mid.getText(); if ("clone".equals(name)) { final DetailAST params = aAST.findFirstToken(TokenTypes.PARAMETERS); final boolean hasEmptyParamList = params.findFirstToken(TokenTypes.PARAMETER_DEF) == null; if (hasEmptyParamList) { log(aAST.getLineNo(), MSG_KEY); } } }
/** * Get the name of the field. * @param field to get the name from. * @return name of the field. */ private static String getFieldName(DetailAST field) { return field.findFirstToken(TokenTypes.IDENT).getText(); }
@Override public boolean isValidOn(final DetailAST ast) { final int astType = ast.getType(); final DetailAST varType = ast.findFirstToken(TokenTypes.TYPE); return astType == TokenTypes.VARIABLE_DEF && varType.getFirstChild().getType() == TokenTypes.ARRAY_DECLARATOR && "ObjectStreamField".equals(varType.getFirstChild().getText()); }
@Override public void visitToken(DetailAST ast) { final DetailAST slistAST = ast.findFirstToken(TokenTypes.SLIST); boolean isElseIf = false; if (ast.getType() == TokenTypes.LITERAL_ELSE && ast.findFirstToken(TokenTypes.LITERAL_IF) != null) { isElseIf = true; } final boolean isInAnnotationField = isInAnnotationField(ast); final boolean skipStatement = isSkipStatement(ast); final boolean skipEmptyLoopBody = allowEmptyLoopBody && isEmptyLoopBody(ast); if (slistAST == null && !isElseIf && !isInAnnotationField && !skipStatement && !skipEmptyLoopBody) { log(ast.getLineNo(), MSG_KEY_NEED_BRACES, ast.getText()); } }
/** * Get string representation of the node as token name, * node text, line number and column number. * @param node DetailAST * @return node info */ private static String getNodeInfo(DetailAST node) { return TokenUtil.getTokenName(node.getType()) + " -> " + escapeAllControlChars(node.getText()) + " [" + node.getLineNo() + ':' + node.getColumnNo() + ']'; }
/** * Converts given AST node to string representation. * @param ast node to be represented as string * @return string representation of AST node */ private static String convertToString(DetailAST ast) { final String tokenText; switch (ast.getType()) { case TokenTypes.LABELED_STAT: tokenText = ast.getFirstChild().getText() + ast.getText(); break; // multiline tokens need to become singlelined case TokenTypes.COMMENT_CONTENT: tokenText = JavadocUtil.escapeAllControlChars(ast.getText()); break; default: tokenText = ast.getText(); break; } return tokenText; }
@Override public void visitToken(DetailAST aAST) { final DetailAST mid = aAST.findFirstToken(TokenTypes.IDENT); final String name = mid.getText(); if ("finalize".equals(name)) { final DetailAST params = aAST.findFirstToken(TokenTypes.PARAMETERS); final boolean hasEmptyParamList = params.findFirstToken(TokenTypes.PARAMETER_DEF) == null; if (hasEmptyParamList) { log(aAST.getLineNo(), MSG_KEY); } } }
/** * Returns true if method has to be checked or false. * @param ast method def to check. * @return true if we should check this method. */ private boolean isCheckedMethod(DetailAST ast) { final String methodName = ast.findFirstToken(TokenTypes.IDENT).getText(); return !ignoredMethodNames.contains(methodName); }
/** * Get all variables initialized In init part of for loop. * @param ast for loop token * @return set of variables initialized in for loop */ private static Set<String> getForInitVariables(DetailAST ast) { final Set<String> initializedVariables = new HashSet<>(); final DetailAST forInitAST = ast.findFirstToken(TokenTypes.FOR_INIT); for (DetailAST parameterDefAST = forInitAST.findFirstToken(TokenTypes.VARIABLE_DEF); parameterDefAST != null; parameterDefAST = parameterDefAST.getNextSibling()) { if (parameterDefAST.getType() == TokenTypes.VARIABLE_DEF) { final DetailAST param = parameterDefAST.findFirstToken(TokenTypes.IDENT); initializedVariables.add(param.getText()); } } return initializedVariables; }
int currentIndex = 0; while (currentToken != null) { if (currentToken.getType() == TokenTypes.METHOD_DEF) { currentIndex++; final String methodName = currentToken.findFirstToken(TokenTypes.IDENT).getText(); if (methodIndexMap.containsKey(methodName)) { final int previousIndex = methodIndexMap.get(methodName); final int previousLineWithOverloadMethod = methodLineNumberMap.get(methodName); log(currentToken.getLineNo(), MSG_KEY, previousLineWithOverloadMethod); methodLineNumberMap.put(methodName, currentToken.getLineNo());
/** * Find start and end selection positions from AST line and Column. * @param ast DetailAST node for which selection finds */ private void findSelectionPositions(DetailAST ast) { selectionStart = lines2position.get(ast.getLineNo()) + ast.getColumnNo(); if (ast.getChildCount() == 0 && TokenUtil.getTokenName(ast.getType()).equals(ast.getText())) { selectionEnd = selectionStart; } else { selectionEnd = findLastPosition(ast); } }
@Override public void visitToken(DetailAST commentAst) { switch (commentAst.getType()) { case TokenTypes.SINGLE_LINE_COMMENT: case TokenTypes.BLOCK_COMMENT_BEGIN: visitComment(commentAst); break; default: final String exceptionMsg = "Unexpected token type: " + commentAst.getText(); throw new IllegalArgumentException(exceptionMsg); } }
@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); } }