@Override public void visitToken(DetailAST detailAST) { if (!ScopeUtils.isLocalVariableDef(detailAST)) { return; } List<DetailAST> methodCallASTList = DetailASTUtil.getMethodCalls(detailAST, "Pattern", "compile"); if (methodCallASTList.isEmpty()) { return; } DetailAST methodCallAST = methodCallASTList.get(0); DetailAST elistAST = methodCallAST.findFirstToken(TokenTypes.ELIST); DetailAST expressionAST = elistAST.findFirstToken(TokenTypes.EXPR); List<DetailAST> childASTList = DetailASTUtil.getAllChildTokens(expressionAST, DetailASTUtil.ALL_TYPES, true); for (DetailAST childAST : childASTList) { if ((childAST.getType() != TokenTypes.PLUS) && (childAST.getType() != TokenTypes.STRING_LITERAL)) { return; } } DetailAST nameAST = detailAST.findFirstToken(TokenTypes.IDENT); log(detailAST.getLineNo(), MSG_LOCAL_PATTERN, nameAST.getText()); }
@Override public void visitToken(DetailAST detailAST) { DetailAST firstChildAST = detailAST.getFirstChild(); if ((firstChildAST == null) || (firstChildAST.getType() == TokenTypes.DOT)) { return; } DetailAST parentAST = detailAST.getParent(); DetailAST nameAST = null; if (parentAST.getType() == TokenTypes.EXPR) { nameAST = detailAST.findFirstToken(TokenTypes.IDENT); } else if (parentAST.getType() == TokenTypes.VARIABLE_DEF) { nameAST = parentAST.findFirstToken(TokenTypes.IDENT); } if (nameAST == null) { return; } _checkMissingEmptyLineAfterReferencingVariable( parentAST, nameAST.getText(), DetailASTUtil.getEndLine(detailAST)); _checkMissingEmptyLineBetweenAssigningAndUsingVariable( parentAST, nameAST.getText(), DetailASTUtil.getEndLine(detailAST)); }
private void _checkMissingEmptyLineAfterReferencingVariable( DetailAST detailAST, String name, int endLine) { boolean isReferenced = false; DetailAST nextSibling = detailAST.getNextSibling(); while (true) { if ((nextSibling == null) || (nextSibling.getType() != TokenTypes.SEMI)) { return; } nextSibling = nextSibling.getNextSibling(); if ((nextSibling == null) || ((nextSibling.getType() != TokenTypes.EXPR) && (nextSibling.getType() != TokenTypes.VARIABLE_DEF))) { return; } boolean expressionReferencesVariable = false; List<DetailAST> identASTList = DetailASTUtil.getAllChildTokens(nextSibling, true, TokenTypes.IDENT); for (DetailAST identAST : identASTList) { String identName = identAST.getText(); if (identName.equals(name)) { expressionReferencesVariable = true; } } if (!expressionReferencesVariable) { if (isReferenced) { int startLineNextExpression = DetailASTUtil.getStartLine(nextSibling); if ((endLine + 1) == startLineNextExpression) { log( startLineNextExpression, MSG_MISSING_EMPTY_LINE_AFTER_VARIABLE_REFERENCE, startLineNextExpression, name); } } return; } isReferenced = true; endLine = DetailASTUtil.getEndLine(nextSibling); nextSibling = nextSibling.getNextSibling(); } }
@Override public void visitToken(DetailAST detailAST) { List<DetailAST> methodCallASTList = DetailASTUtil.getAllChildTokens(detailAST, TokenTypes.METHOD_CALL, true); for (DetailAST methodCallAST : methodCallASTList) { List<DetailAST> childMethodCallASTList = DetailASTUtil.getAllChildTokens(methodCallAST, TokenTypes.METHOD_CALL, true); // Only check the method that is first in the chain if (!childMethodCallASTList.isEmpty()) { continue; } String chainedMethodNames = _getChainedMethodNames(methodCallAST); if (!chainedMethodNames.contains(StringPool.PERIOD)) { continue; } _checkMethodName(chainedMethodNames, "getClass", methodCallAST, detailAST); if (StringUtil.count(chainedMethodNames, StringPool.PERIOD) == 1) { continue; } if (chainedMethodNames.contains("concat.concat.concat")) { log(methodCallAST.getLineNo(), MSG_AVOID_TOO_MANY_CONCAT); continue; } if (!chainedMethodNames.contains("concat.concat") && !chainedMethodNames.matches(_chainingAllowedFormat)) { log( methodCallAST.getLineNo(), MSG_AVOID_CHAINING_MULTIPLE, DetailASTUtil.getMethodName(methodCallAST)); } } }
private String _getChainedMethodNames(DetailAST methodCallAST) { StringBundler sb = new StringBundler(); sb.append(DetailASTUtil.getMethodName(methodCallAST)); while (true) { DetailAST parentAST = methodCallAST.getParent(); if (parentAST.getType() != TokenTypes.DOT) { return sb.toString(); } methodCallAST = parentAST.getParent(); if (methodCallAST.getType() != TokenTypes.METHOD_CALL) { return sb.toString(); } sb.append(StringPool.PERIOD); sb.append(DetailASTUtil.getMethodName(methodCallAST)); } }
private void _checkMissingEmptyLineBetweenAssigningAndUsingVariable( DetailAST detailAST, String name, int endLine) { DetailAST nextSibling = detailAST.getNextSibling(); if ((nextSibling == null) || (nextSibling.getType() != TokenTypes.SEMI)) { return; } nextSibling = nextSibling.getNextSibling(); if (nextSibling == null) { return; } int startLineNextExpression = DetailASTUtil.getStartLine(nextSibling); if ((endLine + 1) != startLineNextExpression) { return; } if (_isExpressionAssignsVariable(nextSibling, name)) { return; } List<DetailAST> identASTList = DetailASTUtil.getAllChildTokens(nextSibling, true, TokenTypes.IDENT); for (DetailAST identAST : identASTList) { String identName = identAST.getText(); if (identName.equals(name)) { log(startLineNextExpression, MSG_MISSING_EMPTY_LINE_BEFORE_VARIABLE_USE, name); } } }