Esempio n. 1
0
  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));
    }
  }
Esempio n. 2
0
  @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));
      }
    }
  }