Пример #1
0
  /**
   * Determines whether or not the given AST is in a valid hash code method. A valid hash code
   * method is considered to be a method of the signature {@code public int hashCode()}.
   *
   * @param ast the AST from which to search for an enclosing hash code method definition
   * @return {@code true} if {@code ast} is in the scope of a valid hash code method
   */
  private boolean isInHashCodeMethod(DetailAST ast) {
    // if not in a code block, can't be in hashCode()
    if (!ScopeUtils.inCodeBlock(ast)) {
      return false;
    }

    // find the method definition AST
    DetailAST methodDefAST = ast.getParent();
    while (null != methodDefAST && TokenTypes.METHOD_DEF != methodDefAST.getType()) {
      methodDefAST = methodDefAST.getParent();
    }

    if (null == methodDefAST) {
      return false;
    }

    // Check for 'hashCode' name.
    final DetailAST identAST = methodDefAST.findFirstToken(TokenTypes.IDENT);
    if (!"hashCode".equals(identAST.getText())) {
      return false;
    }

    // Check for no arguments.
    final DetailAST paramAST = methodDefAST.findFirstToken(TokenTypes.PARAMETERS);
    // we are in a 'public int hashCode()' method! The compiler will ensure
    // the method returns an 'int' and is public.
    return 0 == paramAST.getChildCount();
  }