[开发] Android上Method Sign的Descriptor转PrettyName算法

void AppendPrettyDescriptor(const char* descriptor, std::string* result) {
  // Count the number of '['s to get the dimensionality.
  const char* c = descriptor;
  size_t dim = 0;
  while (*c == '[') {
    dim++;
    c++;
  }

  // Reference or primitive?
  if (*c == 'L') {
    // "[[La/b/C;" -> "a.b.C[][]".
    c++;  // Skip the 'L'.
  } else {
    // "[[B" -> "byte[][]".
    // To make life easier, we make primitives look like unqualified
    // reference types.
    switch (*c) {
      case 'B': c = "byte;"; break;
      case 'C': c = "char;"; break;
      case 'D': c = "double;"; break;
      case 'F': c = "float;"; break;
      case 'I': c = "int;"; break;
      case 'J': c = "long;"; break;
      case 'S': c = "short;"; break;
      case 'Z': c = "boolean;"; break;
      case 'V': c = "void;"; break;  // Used when decoding return types.
      default: result->append(descriptor); return;
    }
  }

  // At this point, 'c' is a string of the form "fully/qualified/Type;"
  // or "primitive;". Rewrite the type with '.' instead of '/':
  const char* p = c;
  while (*p != ';') {
    char ch = *p++;
    if (ch == '/') {
      ch = '.';
    }
    result->push_back(ch);
  }
  // ...and replace the semicolon with 'dim' "[]" pairs:
  for (size_t i = 0; i < dim; ++i) {
    result->append("[]");
  }
}
posted @ 2022-07-14 19:09  qwertzxc  阅读(32)  评论(0)    收藏  举报