natgetmethodname.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include <gcj/cni.h>
  2. #include <jvm.h>
  3. #include <jvmti.h>
  4. #include <stdio.h>
  5. #include <java/lang/Object.h>
  6. #include "getmethodname.h"
  7. static void
  8. print_error (jvmtiEnv *env, const char *msg, jvmtiError err)
  9. {
  10. char *error_msg;
  11. env->GetErrorName (err, &error_msg);
  12. printf ("%s: %s\n", msg, error_msg);
  13. env->Deallocate (reinterpret_cast<unsigned char *> (error_msg));
  14. }
  15. #define NUM_METHODS 8
  16. static const char *function_names[] = { "clone",
  17. "equals",
  18. "finalize",
  19. "getClass",
  20. "hashCode",
  21. "notify",
  22. "notifyAll",
  23. "toString" };
  24. static int
  25. function_index (const char *name)
  26. {
  27. for (int i = 0; i < NUM_METHODS; ++i)
  28. {
  29. if (strcmp (function_names[i], name) == 0)
  30. return i;
  31. }
  32. return -1;
  33. }
  34. void
  35. getmethodname::do_getmethodname_tests ()
  36. {
  37. jvmtiEnv *env;
  38. JavaVM *vm = _Jv_GetJavaVM ();
  39. vm->GetEnv (reinterpret_cast<void **> (&env), JVMTI_VERSION_1_0);
  40. jvmtiError err;
  41. err = env->GetMethodName (reinterpret_cast<jmethodID> (NULL),
  42. reinterpret_cast<char **> (NULL),
  43. reinterpret_cast<char **> (NULL),
  44. reinterpret_cast<char **> (NULL));
  45. print_error (env, "null jmethodID", err);
  46. jint count;
  47. jmethodID *methods;
  48. err = env->GetClassMethods (&java::lang::Object::class$, &count, &methods);
  49. print_error (env, "GetClassMethods", err);
  50. char *names[NUM_METHODS], *solo_names[NUM_METHODS];
  51. char *signatures[NUM_METHODS], *solo_signatures[NUM_METHODS];
  52. char *generics[NUM_METHODS], *solo_generics[NUM_METHODS];
  53. for (jint i = 0; i < count; ++i)
  54. {
  55. char *name, *n;
  56. char *signature, *s;
  57. char *generic, *g;
  58. err = env->GetMethodName (methods[i], &name, &signature, &generic);
  59. int idx = -1;
  60. if (err != JVMTI_ERROR_NONE)
  61. {
  62. print_error (env, "GetMethodName - all fields", err);
  63. continue;
  64. }
  65. idx = function_index (name);
  66. if (idx == -1)
  67. continue;
  68. names[idx] = name;
  69. signatures[idx] = signature;
  70. generics[idx] = generic;
  71. err = env->GetMethodName (methods[i], &n, NULL, NULL);
  72. print_error (env, "GetMethodName - name", err);
  73. solo_names[idx] = n;
  74. err = env->GetMethodName (methods[i], NULL, &s, NULL);
  75. print_error (env, "GetMethodName - signature", err);
  76. solo_signatures[idx] = s;
  77. err = env->GetMethodName (methods[i], NULL, NULL, &g);
  78. print_error (env, "GetMethodName - generic", err);
  79. solo_generics[idx] = g;
  80. }
  81. #define WRAP(X) ((X) == NULL ? "null" : (X))
  82. #define MATCH(X,Y) (strcmp ((X),(Y)) == 0 ? "match" : "do not match")
  83. for (int i = 0; i < NUM_METHODS; ++i)
  84. {
  85. printf ("name=%s, signature=%s, generic=%s\n",
  86. WRAP (names[i]), WRAP (signatures[i]), WRAP (generics[i]));
  87. printf ("names %s\n", MATCH (solo_names[i], names[i]));
  88. printf ("signatures %s\n", MATCH (solo_signatures[i], signatures[i]));
  89. printf ("generic %s\n", "not yet");
  90. env->Deallocate (reinterpret_cast<unsigned char *> (names[i]));
  91. env->Deallocate (reinterpret_cast<unsigned char *> (solo_names[i]));
  92. env->Deallocate (reinterpret_cast<unsigned char *> (signatures[i]));
  93. env->Deallocate (reinterpret_cast<unsigned char *> (solo_signatures[i]));
  94. env->Deallocate (reinterpret_cast<unsigned char *> (generics[i]));
  95. env->Deallocate (reinterpret_cast<unsigned char *> (solo_generics[i]));
  96. }
  97. }