OSDN Git Service

3e410e6247163263c3bb85a203d22bf32d3909fc
[pf3gnuchains/gcc-fork.git] / libjava / java / lang / natClass.cc
1 // natClass.cc - Implementation of java.lang.Class native methods.
2
3 /* Copyright (C) 1998, 1999, 2000  Cygnus Solutions
4
5    This file is part of libgcj.
6
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
9 details.  */
10
11 #include <config.h>
12
13 #include <stdlib.h>
14 #include <string.h>
15
16 #pragma implementation "Class.h"
17
18 #include <gcj/cni.h>
19 #include <jvm.h>
20 #include <java/lang/Class.h>
21 #include <java/lang/ClassLoader.h>
22 #include <java/lang/String.h>
23 #include <java/lang/reflect/Modifier.h>
24 #include <java/lang/reflect/Member.h>
25 #include <java/lang/reflect/Method.h>
26 #include <java/lang/reflect/Field.h>
27 #include <java/lang/reflect/Constructor.h>
28 #include <java/lang/AbstractMethodError.h>
29 #include <java/lang/ClassNotFoundException.h>
30 #include <java/lang/IllegalAccessException.h>
31 #include <java/lang/IllegalAccessError.h>
32 #include <java/lang/IncompatibleClassChangeError.h>
33 #include <java/lang/InstantiationException.h>
34 #include <java/lang/NoClassDefFoundError.h>
35 #include <java/lang/NoSuchFieldException.h>
36 #include <java/lang/NoSuchMethodException.h>
37 #include <java/lang/Thread.h>
38 #include <java/lang/NullPointerException.h>
39 #include <java/lang/System.h>
40 #include <java/lang/SecurityManager.h>
41 #include <java/lang/StringBuffer.h>
42 #include <gcj/method.h>
43
44 #include <java-cpool.h>
45
46 \f
47
48 #define CloneableClass _CL_Q34java4lang9Cloneable
49 extern java::lang::Class CloneableClass;
50 #define ObjectClass _CL_Q34java4lang6Object
51 extern java::lang::Class ObjectClass;
52 #define ErrorClass _CL_Q34java4lang5Error
53 extern java::lang::Class ErrorClass;
54 #define ClassClass _CL_Q34java4lang5Class
55 extern java::lang::Class ClassClass;
56 #define MethodClass _CL_Q44java4lang7reflect6Method
57 extern java::lang::Class MethodClass;
58 #define FieldClass _CL_Q44java4lang7reflect5Field
59 extern java::lang::Class FieldClass;
60 #define ConstructorClass _CL_Q44java4lang7reflect11Constructor
61 extern java::lang::Class ConstructorClass;
62
63 // Some constants we use to look up the class initializer.
64 static _Jv_Utf8Const *void_signature = _Jv_makeUtf8Const ("()V", 3);
65 static _Jv_Utf8Const *clinit_name = _Jv_makeUtf8Const ("<clinit>", 8);
66 static _Jv_Utf8Const *init_name = _Jv_makeUtf8Const ("<init>", 6);
67 static _Jv_Utf8Const *finit_name = _Jv_makeUtf8Const ("$finit$", 7);
68
69 \f
70
71 jclass
72 java::lang::Class::forName (jstring className)
73 {
74   if (! className)
75     JvThrow (new java::lang::NullPointerException);
76
77 #if 0
78   // FIXME: should check syntax of CLASSNAME and throw
79   // IllegalArgumentException on failure.
80
81   // FIXME: should use class loader from calling method.
82   jclass klass = _Jv_FindClass (className, NULL);
83 #else
84   jsize length = _Jv_GetStringUTFLength (className);
85   char buffer[length];
86   _Jv_GetStringUTFRegion (className, 0, length, buffer);
87
88   // FIXME: should check syntax of CLASSNAME and throw
89   // IllegalArgumentException on failure.
90   _Jv_Utf8Const *name = _Jv_makeUtf8Const (buffer, length);
91
92   // FIXME: should use class loader from calling method.
93   jclass klass = (buffer[0] == '[' 
94                   ? _Jv_FindClassFromSignature (name->data, NULL)
95                   : _Jv_FindClass (name, NULL));
96 #endif
97   if (! klass)
98     JvThrow (new java::lang::ClassNotFoundException (className));
99
100   return klass;
101 }
102
103 java::lang::reflect::Constructor *
104 java::lang::Class::getConstructor (JArray<jclass> *param_types)
105 {
106   jstring partial_sig = getSignature (param_types, true);
107   jint hash = partial_sig->hashCode ();
108
109   int i = isPrimitive () ? 0 : method_count;
110   while (--i >= 0)
111     {
112       // FIXME: access checks.
113       if (_Jv_equalUtf8Consts (methods[i].name, init_name)
114           && _Jv_equal (methods[i].signature, partial_sig, hash))
115         {
116           // Found it.  For getConstructor, the constructor must be
117           // public.
118           using namespace java::lang::reflect;
119           if (! Modifier::isPublic(methods[i].accflags))
120             break;
121           Constructor *cons = new Constructor ();
122           cons->offset = (char *) (&methods[i]) - (char *) methods;
123           cons->declaringClass = this;
124           return cons;
125         }
126     }
127   JvThrow (new java::lang::NoSuchMethodException);
128 }
129
130 JArray<java::lang::reflect::Constructor *> *
131 java::lang::Class::_getConstructors (jboolean declared)
132 {
133   // FIXME: this method needs access checks.
134
135   int numConstructors = 0;
136   int max = isPrimitive () ? 0 : method_count;
137   int i;
138   for (i = max; --i >= 0; )
139     {
140       _Jv_Method *method = &methods[i];
141       if (method->name == NULL
142           || ! _Jv_equalUtf8Consts (method->name, init_name))
143         continue;
144       if (! declared
145           && ! java::lang::reflect::Modifier::isPublic(method->accflags))
146         continue;
147       numConstructors++;
148     }
149   JArray<java::lang::reflect::Constructor *> *result
150     = (JArray<java::lang::reflect::Constructor *> *)
151     JvNewObjectArray (numConstructors, &ConstructorClass, NULL);
152   java::lang::reflect::Constructor** cptr = elements (result);
153   for (i = 0;  i < max;  i++)
154     {
155       _Jv_Method *method = &methods[i];
156       if (method->name == NULL
157           || ! _Jv_equalUtf8Consts (method->name, init_name))
158         continue;
159       if (! declared
160           && ! java::lang::reflect::Modifier::isPublic(method->accflags))
161         continue;
162       java::lang::reflect::Constructor *cons
163         = new java::lang::reflect::Constructor ();
164       cons->offset = (char *) method - (char *) methods;
165       cons->declaringClass = this;
166       *cptr++ = cons;
167     }
168   return result;
169 }
170
171 java::lang::reflect::Constructor *
172 java::lang::Class::getDeclaredConstructor (JArray<jclass> *param_types)
173 {
174   jstring partial_sig = getSignature (param_types, true);
175   jint hash = partial_sig->hashCode ();
176
177   int i = isPrimitive () ? 0 : method_count;
178   while (--i >= 0)
179     {
180       // FIXME: access checks.
181       if (_Jv_equalUtf8Consts (methods[i].name, init_name)
182           && _Jv_equal (methods[i].signature, partial_sig, hash))
183         {
184           // Found it.
185           using namespace java::lang::reflect;
186           Constructor *cons = new Constructor ();
187           cons->offset = (char *) (&methods[i]) - (char *) methods;
188           cons->declaringClass = this;
189           return cons;
190         }
191     }
192   JvThrow (new java::lang::NoSuchMethodException);
193 }
194
195 java::lang::reflect::Field *
196 java::lang::Class::getField (jstring name, jint hash)
197 {
198   java::lang::reflect::Field* rfield;
199   for (int i = 0;  i < field_count;  i++)
200     {
201       _Jv_Field *field = &fields[i];
202       if (! _Jv_equal (field->name, name, hash))
203         continue;
204       if (! (field->getModifiers() & java::lang::reflect::Modifier::PUBLIC))
205         continue;
206       rfield = new java::lang::reflect::Field ();
207       rfield->offset = (char*) field - (char*) fields;
208       rfield->declaringClass = this;
209       rfield->name = name;
210       return rfield;
211     }
212   jclass superclass = getSuperclass();
213   if (superclass == NULL)
214     return NULL;
215   rfield = superclass->getField(name, hash);
216   for (int i = 0; i < interface_count && rfield == NULL; ++i)
217     rfield = interfaces[i]->getField (name, hash);
218   return rfield;
219 }
220
221 java::lang::reflect::Field *
222 java::lang::Class::getDeclaredField (jstring name)
223 {
224   java::lang::SecurityManager *s = java::lang::System::getSecurityManager();
225   if (s != NULL)
226     s->checkMemberAccess (this, java::lang::reflect::Member::DECLARED);
227   int hash = name->hashCode();
228   for (int i = 0;  i < field_count;  i++)
229     {
230       _Jv_Field *field = &fields[i];
231       if (! _Jv_equal (field->name, name, hash))
232         continue;
233       java::lang::reflect::Field* rfield = new java::lang::reflect::Field ();
234       rfield->offset = (char*) field - (char*) fields;
235       rfield->declaringClass = this;
236       rfield->name = name;
237       return rfield;
238     }
239   JvThrow (new java::lang::NoSuchFieldException (name));
240 }
241
242 JArray<java::lang::reflect::Field *> *
243 java::lang::Class::getDeclaredFields (void)
244 {
245   java::lang::SecurityManager *s = java::lang::System::getSecurityManager();
246   if (s != NULL)
247     s->checkMemberAccess (this, java::lang::reflect::Member::DECLARED);
248   JArray<java::lang::reflect::Field *> *result
249     = (JArray<java::lang::reflect::Field *> *)
250     JvNewObjectArray (field_count, &FieldClass, NULL);
251   java::lang::reflect::Field** fptr = elements (result);
252   for (int i = 0;  i < field_count;  i++)
253     {
254       _Jv_Field *field = &fields[i];
255       java::lang::reflect::Field* rfield = new java::lang::reflect::Field ();
256       rfield->offset = (char*) field - (char*) fields;
257       rfield->declaringClass = this;
258       *fptr++ = rfield;
259     }
260   return result;
261 }
262
263 void
264 java::lang::Class::getSignature (java::lang::StringBuffer *buffer)
265 {
266   if (isPrimitive())
267     buffer->append((jchar) method_count);
268   else
269     {
270       jstring name = getName();
271       if (name->charAt(0) != '[')
272         buffer->append((jchar) 'L');
273       buffer->append(name);
274       if (name->charAt(0) != '[')
275         buffer->append((jchar) ';');
276     }
277 }
278
279 // This doesn't have to be native.  It is an implementation detail
280 // only called from the C++ code, though, so maybe this is clearer.
281 jstring
282 java::lang::Class::getSignature (JArray<jclass> *param_types,
283                                  jboolean is_constructor)
284 {
285   java::lang::StringBuffer *buf = new java::lang::StringBuffer ();
286   buf->append((jchar) '(');
287   jclass *v = elements (param_types);
288   for (int i = 0; i < param_types->length; ++i)
289     v[i]->getSignature(buf);
290   buf->append((jchar) ')');
291   if (is_constructor)
292     buf->append((jchar) 'V');
293   return buf->toString();
294 }
295
296 java::lang::reflect::Method *
297 java::lang::Class::getDeclaredMethod (jstring name,
298                                       JArray<jclass> *param_types)
299 {
300   jstring partial_sig = getSignature (param_types, false);
301   jint p_len = partial_sig->length();
302   _Jv_Utf8Const *utf_name = _Jv_makeUtf8Const (name);
303   int i = isPrimitive () ? 0 : method_count;
304   while (--i >= 0)
305     {
306       // FIXME: access checks.
307       if (_Jv_equalUtf8Consts (methods[i].name, utf_name)
308           && _Jv_equaln (methods[i].signature, partial_sig, p_len))
309         {
310           // Found it.
311           using namespace java::lang::reflect;
312           Method *rmethod = new Method ();
313           rmethod->offset = (char*) (&methods[i]) - (char*) methods;
314           rmethod->declaringClass = this;
315           return rmethod;
316         }
317     }
318   JvThrow (new java::lang::NoSuchMethodException);
319 }
320
321 JArray<java::lang::reflect::Method *> *
322 java::lang::Class::getDeclaredMethods (void)
323 {
324   int numMethods = 0;
325   int max = isPrimitive () ? 0 : method_count;
326   int i;
327   for (i = max; --i >= 0; )
328     {
329       _Jv_Method *method = &methods[i];
330       if (method->name == NULL
331           || _Jv_equalUtf8Consts (method->name, clinit_name)
332           || _Jv_equalUtf8Consts (method->name, init_name)
333           || _Jv_equalUtf8Consts (method->name, finit_name))
334         continue;
335       numMethods++;
336     }
337   JArray<java::lang::reflect::Method *> *result
338     = (JArray<java::lang::reflect::Method *> *)
339     JvNewObjectArray (numMethods, &MethodClass, NULL);
340   java::lang::reflect::Method** mptr = elements (result);
341   for (i = 0;  i < max;  i++)
342     {
343       _Jv_Method *method = &methods[i];
344       if (method->name == NULL
345           || _Jv_equalUtf8Consts (method->name, clinit_name)
346           || _Jv_equalUtf8Consts (method->name, init_name)
347           || _Jv_equalUtf8Consts (method->name, finit_name))
348         continue;
349       java::lang::reflect::Method* rmethod
350         = new java::lang::reflect::Method ();
351       rmethod->offset = (char*) method - (char*) methods;
352       rmethod->declaringClass = this;
353       *mptr++ = rmethod;
354     }
355   return result;
356 }
357
358 jstring
359 java::lang::Class::getName (void)
360 {
361   char buffer[name->length + 1];  
362   memcpy (buffer, name->data, name->length); 
363   buffer[name->length] = '\0';
364   return _Jv_NewStringUTF (buffer);
365 }
366
367 JArray<jclass> *
368 java::lang::Class::getClasses (void)
369 {
370   // Until we have inner classes, it always makes sense to return an
371   // empty array.
372   JArray<jclass> *result
373     = (JArray<jclass> *) JvNewObjectArray (0, &ClassClass, NULL);
374   return result;
375 }
376
377 JArray<jclass> *
378 java::lang::Class::getDeclaredClasses (void)
379 {
380   checkMemberAccess (java::lang::reflect::Member::DECLARED);
381   // Until we have inner classes, it always makes sense to return an
382   // empty array.
383   JArray<jclass> *result
384     = (JArray<jclass> *) JvNewObjectArray (0, &ClassClass, NULL);
385   return result;
386 }
387
388 jclass
389 java::lang::Class::getDeclaringClass (void)
390 {
391   // Until we have inner classes, it makes sense to always return
392   // NULL.
393   return NULL;
394 }
395
396 jint
397 java::lang::Class::_getFields (JArray<java::lang::reflect::Field *> *result,
398                                jint offset)
399 {
400   int count = 0;
401   for (int i = 0;  i < field_count;  i++)
402     {
403       _Jv_Field *field = &fields[i];
404       if (! (field->getModifiers() & java::lang::reflect::Modifier::PUBLIC))
405         continue;
406       ++count;
407
408       if (result != NULL)
409         {
410           java::lang::reflect::Field *rfield
411             = new java::lang::reflect::Field ();
412           rfield->offset = (char *) field - (char *) fields;
413           rfield->declaringClass = this;
414           rfield->name = _Jv_NewStringUtf8Const (field->name);
415           (elements (result))[offset + i] = rfield;
416         }
417     }
418   jclass superclass = getSuperclass();
419   if (superclass != NULL)
420     {
421       int s_count = superclass->_getFields (result, offset);
422       count += s_count;
423       offset += s_count;
424     }
425   for (int i = 0; i < interface_count; ++i)
426     {
427       int f_count = interfaces[i]->_getFields (result, offset);
428       count += f_count;
429       offset += f_count;
430     }
431   return count;
432 }
433
434 JArray<java::lang::reflect::Field *> *
435 java::lang::Class::getFields (void)
436 {
437   using namespace java::lang::reflect;
438
439   int count = _getFields (NULL, 0);
440
441   JArray<java::lang::reflect::Field *> *result
442     = ((JArray<java::lang::reflect::Field *> *)
443        JvNewObjectArray (count, &FieldClass, NULL));
444
445   _getFields (result, 0);
446
447   return result;
448 }
449
450 JArray<jclass> *
451 java::lang::Class::getInterfaces (void)
452 {
453   jobjectArray r = JvNewObjectArray (interface_count, getClass (), NULL);
454   jobject *data = elements (r);
455   for (int i = 0; i < interface_count; ++i)
456     data[i] = interfaces[i];
457   return reinterpret_cast<JArray<jclass> *> (r);
458 }
459
460 java::lang::reflect::Method *
461 java::lang::Class::getMethod (jstring name, JArray<jclass> *param_types)
462 {
463   jstring partial_sig = getSignature (param_types, false);
464   jint p_len = partial_sig->length();
465   _Jv_Utf8Const *utf_name = _Jv_makeUtf8Const (name);
466   for (Class *klass = this; klass; klass = klass->getSuperclass())
467     {
468       int i = klass->isPrimitive () ? 0 : klass->method_count;
469       while (--i >= 0)
470         {
471           // FIXME: access checks.
472           if (_Jv_equalUtf8Consts (klass->methods[i].name, utf_name)
473               && _Jv_equaln (klass->methods[i].signature, partial_sig, p_len))
474             {
475               // Found it.
476               using namespace java::lang::reflect;
477
478               // Method must be public.
479               if (! Modifier::isPublic (klass->methods[i].accflags))
480                 break;
481
482               Method *rmethod = new Method ();
483               rmethod->offset = ((char *) (&klass->methods[i])
484                                  - (char *) klass->methods);
485               rmethod->declaringClass = klass;
486               return rmethod;
487             }
488         }
489     }
490   JvThrow (new java::lang::NoSuchMethodException);
491 }
492
493 // This is a very slow implementation, since it re-scans all the
494 // methods we've already listed to make sure we haven't duplicated a
495 // method.  It also over-estimates the required size, so we have to
496 // shrink the result array later.
497 jint
498 java::lang::Class::_getMethods (JArray<java::lang::reflect::Method *> *result,
499                                 jint offset)
500 {
501   jint count = 0;
502
503   // First examine all local methods
504   for (int i = isPrimitive () ? 0 : method_count; --i >= 0; )
505     {
506       _Jv_Method *method = &methods[i];
507       if (method->name == NULL
508           || _Jv_equalUtf8Consts (method->name, clinit_name)
509           || _Jv_equalUtf8Consts (method->name, init_name)
510           || _Jv_equalUtf8Consts (method->name, finit_name))
511         continue;
512       // Only want public methods.
513       if (! java::lang::reflect::Modifier::isPublic (method->accflags))
514         continue;
515
516       // This is where we over-count the slots required if we aren't
517       // filling the result for real.
518       if (result != NULL)
519         {
520           jboolean add = true;
521           java::lang::reflect::Method **mp = elements (result);
522           // If we already have a method with this name and signature,
523           // then ignore this one.  This can happen with virtual
524           // methods.
525           for (int j = 0; j < offset; ++j)
526             {
527               _Jv_Method *meth_2 = _Jv_FromReflectedMethod (mp[j]);
528               if (_Jv_equalUtf8Consts (method->name, meth_2->name)
529                   && _Jv_equalUtf8Consts (method->signature,
530                                           meth_2->signature))
531                 {
532                   add = false;
533                   break;
534                 }
535             }
536           if (! add)
537             continue;
538         }
539
540       if (result != NULL)
541         {
542           using namespace java::lang::reflect;
543           Method *rmethod = new Method ();
544           rmethod->offset = (char *) method - (char *) methods;
545           rmethod->declaringClass = this;
546           Method **mp = elements (result);
547           mp[offset + count] = rmethod;
548         }
549       ++count;
550     }
551   offset += count;
552
553   // Now examine superclasses.
554   if (getSuperclass () != NULL)
555     {
556       jint s_count = getSuperclass()->_getMethods (result, offset);
557       offset += s_count;
558       count += s_count;
559     }
560
561   // Finally, examine interfaces.
562   for (int i = 0; i < interface_count; ++i)
563     {
564       int f_count = interfaces[i]->_getMethods (result, offset);
565       count += f_count;
566       offset += f_count;
567     }
568
569   return count;
570 }
571
572 JArray<java::lang::reflect::Method *> *
573 java::lang::Class::getMethods (void)
574 {
575   using namespace java::lang::reflect;
576
577   // FIXME: security checks.
578
579   // This will overestimate the size we need.
580   jint count = _getMethods (NULL, 0);
581
582   JArray<Method *> *result
583     = ((JArray<Method *> *) JvNewObjectArray (count, &MethodClass, NULL));
584
585   // When filling the array for real, we get the actual count.  Then
586   // we resize the array.
587   jint real_count = _getMethods (result, 0);
588
589   if (real_count != count)
590     {
591       JArray<Method *> *r2
592         = ((JArray<Method *> *) JvNewObjectArray (real_count, &MethodClass,
593                                                   NULL));
594       
595       Method **destp = elements (r2);
596       Method **srcp = elements (result);
597
598       for (int i = 0; i < real_count; ++i)
599         *destp++ = *srcp++;
600
601       result = r2;
602     }
603
604   return result;
605 }
606
607 jboolean
608 java::lang::Class::isAssignableFrom (jclass klass)
609 {
610   if (this == klass)
611     return true;
612   // Primitive types must be equal, which we just tested for.
613   if (isPrimitive () || ! klass || klass->isPrimitive())
614     return false;
615
616   // If target is array, so must source be.
617   if (isArray ())
618     {
619       if (! klass->isArray())
620         return false;
621       return getComponentType()->isAssignableFrom(klass->getComponentType());
622     }
623
624   if (isAssignableFrom (klass->getSuperclass()))
625     return true;
626
627   if (isInterface())
628     {
629       // See if source implements this interface.
630       for (int i = 0; i < klass->interface_count; ++i)
631         {
632           jclass interface = klass->interfaces[i];
633           // FIXME: ensure that class is prepared here.
634           // See Spec 12.3.2.
635           if (isAssignableFrom (interface))
636             return true;
637         }
638     }
639
640   return false;
641 }
642
643 jboolean
644 java::lang::Class::isInstance (jobject obj)
645 {
646   if (! obj || isPrimitive ())
647     return false;
648   return isAssignableFrom (obj->getClass());
649 }
650
651 jboolean
652 java::lang::Class::isInterface (void)
653 {
654   return (accflags & java::lang::reflect::Modifier::INTERFACE) != 0;
655 }
656
657 jobject
658 java::lang::Class::newInstance (void)
659 {
660   // FIXME: do accessibility checks here.  There currently doesn't
661   // seem to be any way to do these.
662   // FIXME: we special-case one check here just to pass a Plum Hall
663   // test.  Once access checking is implemented, remove this.
664   if (this == &ClassClass)
665     JvThrow (new java::lang::IllegalAccessException);
666
667   if (isPrimitive ()
668       || isInterface ()
669       || isArray ()
670       || java::lang::reflect::Modifier::isAbstract(accflags))
671     JvThrow (new java::lang::InstantiationException);
672
673   _Jv_InitClass (this);
674
675   _Jv_Method *meth = _Jv_GetMethodLocal (this, init_name, void_signature);
676   if (! meth)
677     JvThrow (new java::lang::NoSuchMethodException);
678
679   jobject r = JvAllocObject (this);
680   ((void (*) (jobject)) meth->ncode) (r);
681   return r;
682 }
683
684 void
685 java::lang::Class::finalize (void)
686 {
687 #ifdef INTERPRETER
688   JvAssert (_Jv_IsInterpretedClass (this));
689   _Jv_UnregisterClass (this);
690 #endif
691 }
692
693 // FIXME.
694 void
695 java::lang::Class::hackRunInitializers (void)
696 {
697   _Jv_Method *meth = _Jv_GetMethodLocal (this, clinit_name, void_signature);
698   if (meth)
699     ((void (*) (void)) meth->ncode) ();
700 }
701
702 // This implements the initialization process for a class.  From Spec
703 // section 12.4.2.
704 void
705 java::lang::Class::initializeClass (void)
706 {
707   // Short-circuit to avoid needless locking.
708   if (state == JV_STATE_DONE)
709     return;
710
711   // do this before we enter the monitor below, since this can cause
712   // exceptions.  Here we assume, that reading "state" is an atomic
713   // operation, I pressume that is true? --Kresten
714   if (state < JV_STATE_LINKED)
715     {
716 #ifdef INTERPRETER
717       if (_Jv_IsInterpretedClass (this))
718         {
719           java::lang::ClassLoader::resolveClass0 (this);
720
721           // Step 1.
722           _Jv_MonitorEnter (this);
723         }
724       else
725 #endif
726         {
727           // Step 1.
728           _Jv_MonitorEnter (this);
729           _Jv_PrepareCompiledClass (this);
730         }
731     }
732   else
733     {
734       // Step 1.
735       _Jv_MonitorEnter (this);
736     }
737
738   // Step 2.
739   java::lang::Thread *self = java::lang::Thread::currentThread();
740   // FIXME: `self' can be null at startup.  Hence this nasty trick.
741   self = (java::lang::Thread *) ((long) self | 1);
742   while (state == JV_STATE_IN_PROGRESS && thread && thread != self)
743     wait ();
744
745   // Steps 3 &  4.
746   if (state == JV_STATE_DONE || state == JV_STATE_IN_PROGRESS || thread == self)
747     {
748       _Jv_MonitorExit (this);
749       return;
750     }
751
752   // Step 5.
753   if (state == JV_STATE_ERROR)
754     {
755       _Jv_MonitorExit (this);
756       JvThrow (new java::lang::NoClassDefFoundError);
757     }
758
759   // Step 6.
760   thread = self;
761   state = JV_STATE_IN_PROGRESS;
762   _Jv_MonitorExit (this);
763
764   // Step 7.
765   if (! isInterface () && superclass)
766     {
767       // FIXME: We can't currently catch a Java exception in C++ code.
768       // So instead we call a Java trampoline.  It returns an
769       // exception, or null.
770       jobject except = superclass->hackTrampoline(0, NULL);
771       if (except)
772         {
773           // Caught an exception.
774           _Jv_MonitorEnter (this);
775           state = JV_STATE_ERROR;
776           notifyAll ();
777           _Jv_MonitorExit (this);
778           JvThrow (except);
779         }
780     }
781
782   // Step 8.
783   // FIXME: once again we have to go through a trampoline.
784   java::lang::Throwable *except = hackTrampoline (1, NULL);
785
786   // Steps 9, 10, 11.
787   if (! except)
788     {
789       _Jv_MonitorEnter (this);
790       state = JV_STATE_DONE;
791     }
792   else
793     {
794       if (! ErrorClass.isInstance(except))
795         {
796           // Once again we must use the trampoline.  In this case we
797           // have to detect an OutOfMemoryError.
798           except = hackTrampoline(2, except);
799         }
800       _Jv_MonitorEnter (this);
801       state = JV_STATE_ERROR;
802     }
803   notifyAll ();
804   _Jv_MonitorExit (this);
805   if (except)
806     JvThrow (except);
807 }
808
809 \f
810
811 //
812 // Some class-related convenience functions.
813 //
814
815 // Find a method declared in the class.  If it is not declared locally
816 // (or if it is inherited), return NULL.
817 _Jv_Method *
818 _Jv_GetMethodLocal (jclass klass, _Jv_Utf8Const *name,
819                     _Jv_Utf8Const *signature)
820 {
821   for (int i = 0; i < klass->method_count; ++i)
822     {
823       if (_Jv_equalUtf8Consts (name, klass->methods[i].name)
824           && _Jv_equalUtf8Consts (signature, klass->methods[i].signature))
825         return &klass->methods[i];
826     }
827   return NULL;
828 }
829
830 _Jv_Method *
831 _Jv_LookupDeclaredMethod (jclass klass, _Jv_Utf8Const *name,
832                         _Jv_Utf8Const *signature)
833 {
834   for (; klass; klass = klass->getSuperclass())
835     {
836       _Jv_Method *meth = _Jv_GetMethodLocal (klass, name, signature);
837
838       if (meth)
839         return meth;
840     }
841
842   return NULL;
843 }
844
845 // NOTE: MCACHE_SIZE should be a power of 2 minus one.
846 #define MCACHE_SIZE 1023
847
848 struct _Jv_mcache
849 {
850   jclass klass;
851   _Jv_Method *method;
852 };
853
854 static _Jv_mcache method_cache[MCACHE_SIZE + 1];
855
856 static void *
857 _Jv_FindMethodInCache (jclass klass,
858                        _Jv_Utf8Const *name,
859                        _Jv_Utf8Const *signature)
860 {
861   int index = name->hash & MCACHE_SIZE;
862   _Jv_mcache *mc = method_cache + index;
863   _Jv_Method *m = mc->method;
864
865   if (mc->klass == klass
866       && m != NULL              // thread safe check
867       && _Jv_equalUtf8Consts (m->name, name)
868       && _Jv_equalUtf8Consts (m->signature, signature))
869     return mc->method->ncode;
870   return NULL;
871 }
872
873 static void
874 _Jv_AddMethodToCache (jclass klass,
875                         _Jv_Method *method)
876 {
877   _Jv_MonitorEnter (&ClassClass); 
878
879   int index = method->name->hash & MCACHE_SIZE;
880
881   method_cache[index].method = method;
882   method_cache[index].klass = klass;
883
884   _Jv_MonitorExit (&ClassClass);
885 }
886
887 void *
888 _Jv_LookupInterfaceMethod (jclass klass, _Jv_Utf8Const *name,
889                            _Jv_Utf8Const *signature)
890 {
891   void *ncode = _Jv_FindMethodInCache (klass, name, signature);
892   if (ncode != 0)
893     return ncode;
894
895   for (; klass; klass = klass->getSuperclass())
896     {
897       _Jv_Method *meth = _Jv_GetMethodLocal (klass, name, signature);
898       if (! meth)
899         continue;
900
901       if (java::lang::reflect::Modifier::isStatic(meth->accflags))
902         JvThrow (new java::lang::IncompatibleClassChangeError);
903       if (java::lang::reflect::Modifier::isAbstract(meth->accflags))
904         JvThrow (new java::lang::AbstractMethodError);
905       if (! java::lang::reflect::Modifier::isPublic(meth->accflags))
906         JvThrow (new java::lang::IllegalAccessError);
907
908       _Jv_AddMethodToCache (klass, meth);
909
910       return meth->ncode;
911     }
912   JvThrow (new java::lang::IncompatibleClassChangeError);
913   return NULL;                  // Placate compiler.
914 }
915
916 void
917 _Jv_InitClass (jclass klass)
918 {
919   klass->initializeClass();
920 }
921
922 jboolean
923 _Jv_IsInstanceOf(jobject obj, jclass cl)
924 {
925   return cl->isInstance(obj);
926 }