public static String sanitize(String name, int length) {
    StringBuilder result = new StringBuilder();
    for (int i = 0; i < name.length(); i++) {
      final char ch = name.charAt(i);

      if (ch > 0 && ch < 255) {
        if (Character.isJavaIdentifierPart(ch) || ch == ' ' || ch == '@' || ch == '-') {
          result.append(ch);
        }
        else {
          result.append("_");
        }
      }

    }

    int methodNameLimit = 250 - length;
    if (result.length() >= methodNameLimit) {
      String hash = String.valueOf(result.toString().hashCode());
      return (methodNameLimit > hash.length() ? result.substring(0, methodNameLimit - hash.length()) : "") + hash;
    }

    return result.toString();
  }
public static String sanitize(String name, int length)