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