OSDN Git Service

cec519fd5023c34310d0ea3b2dd4819c1ed3045e
[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, 2001, 2002, 2003, 2004, 2005, 2006, 2007
4    Free Software Foundation
5
6    This file is part of libgcj.
7
8 This software is copyrighted work licensed under the terms of the
9 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
10 details.  */
11
12 #include <config.h>
13
14 #include <limits.h>
15 #include <string.h>
16 #include <stddef.h>
17 #include <stdio.h>
18
19 #pragma implementation "Class.h"
20
21 #include <gcj/cni.h>
22 #include <jvm.h>
23 #include <java-threads.h>
24
25 #include <java/lang/Class.h>
26 #include <java/lang/ClassLoader.h>
27 #include <java/lang/String.h>
28 #include <java/lang/reflect/Modifier.h>
29 #include <java/lang/reflect/Member.h>
30 #include <java/lang/reflect/Method.h>
31 #include <java/lang/reflect/Field.h>
32 #include <java/lang/reflect/Constructor.h>
33 #include <java/lang/AbstractMethodError.h>
34 #include <java/lang/ArrayStoreException.h>
35 #include <java/lang/ClassCastException.h>
36 #include <java/lang/ClassNotFoundException.h>
37 #include <java/lang/ExceptionInInitializerError.h>
38 #include <java/lang/IllegalAccessException.h>
39 #include <java/lang/IllegalAccessError.h>
40 #include <java/lang/IllegalArgumentException.h>
41 #include <java/lang/IncompatibleClassChangeError.h>
42 #include <java/lang/NoSuchFieldError.h>
43 #include <java/lang/ArrayIndexOutOfBoundsException.h>
44 #include <java/lang/InstantiationException.h>
45 #include <java/lang/NoClassDefFoundError.h>
46 #include <java/lang/NoSuchFieldException.h>
47 #include <java/lang/NoSuchMethodError.h>
48 #include <java/lang/NoSuchMethodException.h>
49 #include <java/lang/Thread.h>
50 #include <java/lang/NullPointerException.h>
51 #include <java/lang/RuntimePermission.h>
52 #include <java/lang/System.h>
53 #include <java/lang/SecurityException.h>
54 #include <java/lang/SecurityManager.h>
55 #include <java/lang/StringBuffer.h>
56 #include <java/lang/VMClassLoader.h>
57 #include <gcj/method.h>
58 #include <gnu/gcj/RawData.h>
59 #include <java/lang/VerifyError.h>
60 #include <java/lang/InternalError.h>
61 #include <java/lang/TypeNotPresentException.h>
62 #include <java/lang/Byte.h>
63 #include <java/lang/Short.h>
64 #include <java/lang/Integer.h>
65 #include <java/lang/Float.h>
66 #include <java/lang/Double.h>
67 #include <java/lang/Long.h>
68 #include <java/lang/Character.h>
69 #include <java/lang/Boolean.h>
70 #include <java/lang/annotation/Annotation.h>
71 #include <java/util/HashMap.h>
72 #include <java/util/Map.h>
73 #include <sun/reflect/annotation/AnnotationInvocationHandler.h>
74 #include <java/lang/Enum.h>
75
76 #include <java-cpool.h>
77 #include <java-interp.h>
78 #include <java-assert.h>
79 #include <java-stack.h>
80 #include <execution.h>
81
82 \f
83
84 using namespace gcj;
85
86 jclass
87 java::lang::Class::forName (jstring className, jboolean initialize,
88                             java::lang::ClassLoader *loader)
89 {
90   if (! className)
91     throw new java::lang::NullPointerException;
92
93   jsize length = _Jv_GetStringUTFLength (className);
94   char buffer[length];
95   _Jv_GetStringUTFRegion (className, 0, className->length(), buffer);
96
97   _Jv_Utf8Const *name = _Jv_makeUtf8Const (buffer, length);
98
99   if (! _Jv_VerifyClassName (name))
100     throw new java::lang::ClassNotFoundException (className);
101
102   jclass klass = (buffer[0] == '[' 
103                   ? _Jv_FindClassFromSignature (name->chars(), loader)
104                   : _Jv_FindClass (name, loader));
105
106   if (klass == NULL)
107     throw new java::lang::ClassNotFoundException (className);
108
109   if (initialize)
110     _Jv_InitClass (klass);
111
112   return klass;
113 }
114
115 jclass
116 java::lang::Class::forName (jstring className)
117 {
118   java::lang::ClassLoader *loader = NULL;
119
120   jclass caller = _Jv_StackTrace::GetCallingClass (&Class::class$);
121   if (caller)
122     loader = caller->getClassLoaderInternal();
123
124   return forName (className, true, loader);
125 }
126
127 java::lang::ClassLoader *
128 java::lang::Class::getClassLoader (void)
129 {
130   java::lang::SecurityManager *s = java::lang::System::getSecurityManager();
131   if (s != NULL)
132     {
133       jclass caller = _Jv_StackTrace::GetCallingClass (&Class::class$);
134       return getClassLoader (caller);
135    }
136
137   return loader;
138 }
139
140 java::lang::ClassLoader *
141 java::lang::Class::getClassLoader (jclass caller)
142 {
143   java::lang::SecurityManager *s = java::lang::System::getSecurityManager();
144   if (s != NULL)
145     {
146       ClassLoader *caller_loader = caller->getClassLoaderInternal();
147
148       // If the caller has a non-null class loader, and that loader
149       // is not this class' loader or an ancestor thereof, then do a
150       // security check.
151       if (caller_loader != NULL && ! caller_loader->isAncestorOf(loader))
152         s->checkPermission (new RuntimePermission (JvNewStringLatin1 ("getClassLoader")));
153     }
154
155   return loader;
156 }
157
158 java::lang::reflect::Constructor *
159 java::lang::Class::getConstructor (JArray<jclass> *param_types)
160 {
161   memberAccessCheck(java::lang::reflect::Member::PUBLIC);
162
163   jstring partial_sig = getSignature (param_types, true);
164   jint hash = partial_sig->hashCode ();
165
166   int i = isPrimitive () ? 0 : method_count;
167   while (--i >= 0)
168     {
169       if (_Jv_equalUtf8Consts (methods[i].name, init_name)
170           && _Jv_equal (methods[i].signature, partial_sig, hash))
171         {
172           // Found it.  For getConstructor, the constructor must be
173           // public.
174           using namespace java::lang::reflect;
175           if (! Modifier::isPublic(methods[i].accflags))
176             break;
177           Constructor *cons = new Constructor ();
178           cons->offset = (char *) (&methods[i]) - (char *) methods;
179           cons->declaringClass = this;
180           return cons;
181         }
182     }
183   throw new java::lang::NoSuchMethodException (_Jv_NewStringUtf8Const (init_name));
184 }
185
186 JArray<java::lang::reflect::Constructor *> *
187 java::lang::Class::getDeclaredConstructors (jboolean publicOnly)
188 {
189   int numConstructors = 0;
190   int max = isPrimitive () ? 0 : method_count;
191   int i;
192   for (i = max; --i >= 0; )
193     {
194       _Jv_Method *method = &methods[i];
195       if (method->name == NULL
196           || ! _Jv_equalUtf8Consts (method->name, init_name))
197         continue;
198       if (publicOnly
199           && ! java::lang::reflect::Modifier::isPublic(method->accflags))
200         continue;
201       numConstructors++;
202     }
203   JArray<java::lang::reflect::Constructor *> *result
204     = (JArray<java::lang::reflect::Constructor *> *)
205     JvNewObjectArray (numConstructors,
206                       &java::lang::reflect::Constructor::class$,
207                       NULL);
208   java::lang::reflect::Constructor** cptr = elements (result);
209   for (i = 0;  i < max;  i++)
210     {
211       _Jv_Method *method = &methods[i];
212       if (method->name == NULL
213           || ! _Jv_equalUtf8Consts (method->name, init_name))
214         continue;
215       if (publicOnly
216           && ! java::lang::reflect::Modifier::isPublic(method->accflags))
217         continue;
218       java::lang::reflect::Constructor *cons
219         = new java::lang::reflect::Constructor ();
220       cons->offset = (char *) method - (char *) methods;
221       cons->declaringClass = this;
222       *cptr++ = cons;
223     }
224   return result;
225 }
226
227 java::lang::reflect::Constructor *
228 java::lang::Class::getDeclaredConstructor (JArray<jclass> *param_types)
229 {
230   memberAccessCheck(java::lang::reflect::Member::DECLARED);
231
232   jstring partial_sig = getSignature (param_types, true);
233   jint hash = partial_sig->hashCode ();
234
235   int i = isPrimitive () ? 0 : method_count;
236   while (--i >= 0)
237     {
238       if (_Jv_equalUtf8Consts (methods[i].name, init_name)
239           && _Jv_equal (methods[i].signature, partial_sig, hash))
240         {
241           // Found it.
242           using namespace java::lang::reflect;
243           Constructor *cons = new Constructor ();
244           cons->offset = (char *) (&methods[i]) - (char *) methods;
245           cons->declaringClass = this;
246           return cons;
247         }
248     }
249   throw new java::lang::NoSuchMethodException (_Jv_NewStringUtf8Const (init_name));
250 }
251
252 java::lang::reflect::Field *
253 java::lang::Class::getField (jstring name, jint hash)
254 {
255   java::lang::reflect::Field* rfield;
256   for (int i = 0;  i < field_count;  i++)
257     {
258       _Jv_Field *field = &fields[i];
259       if (! _Jv_equal (field->name, name, hash))
260         continue;
261       if (! (field->getModifiers() & java::lang::reflect::Modifier::PUBLIC))
262         continue;
263       rfield = new java::lang::reflect::Field ();
264       rfield->offset = (char*) field - (char*) fields;
265       rfield->declaringClass = this;
266       rfield->name = name;
267       return rfield;
268     }
269   jclass superclass = getSuperclass();
270   if (superclass == NULL)
271     return NULL;
272   rfield = superclass->getField(name, hash);
273   for (int i = 0; i < interface_count && rfield == NULL; ++i)
274     rfield = interfaces[i]->getField (name, hash);
275   return rfield;
276 }
277
278 java::lang::reflect::Field *
279 java::lang::Class::getDeclaredField (jstring name)
280 {
281   memberAccessCheck(java::lang::reflect::Member::DECLARED);
282   int hash = name->hashCode();
283   for (int i = 0;  i < field_count;  i++)
284     {
285       _Jv_Field *field = &fields[i];
286       if (! _Jv_equal (field->name, name, hash))
287         continue;
288       java::lang::reflect::Field* rfield = new java::lang::reflect::Field ();
289       rfield->offset = (char*) field - (char*) fields;
290       rfield->declaringClass = this;
291       rfield->name = name;
292       return rfield;
293     }
294   throw new java::lang::NoSuchFieldException (name);
295 }
296
297 JArray<java::lang::reflect::Field *> *
298 java::lang::Class::getDeclaredFields (jboolean public_only)
299 {
300   int size;
301   if (public_only)
302     {
303       size = 0;
304       for (int i = 0; i < field_count; ++i)
305         {
306           _Jv_Field *field = &fields[i];
307           if ((field->flags & java::lang::reflect::Modifier::PUBLIC))
308             ++size;
309         }
310     }
311   else
312     size = field_count;
313
314   JArray<java::lang::reflect::Field *> *result
315     = (JArray<java::lang::reflect::Field *> *)
316     JvNewObjectArray (size, &java::lang::reflect::Field::class$, NULL);
317   java::lang::reflect::Field** fptr = elements (result);
318   for (int i = 0;  i < field_count;  i++)
319     {
320       _Jv_Field *field = &fields[i];
321       if (public_only
322           && ! (field->flags & java::lang::reflect::Modifier::PUBLIC))
323         continue;
324       java::lang::reflect::Field* rfield = new java::lang::reflect::Field ();
325       rfield->offset = (char*) field - (char*) fields;
326       rfield->declaringClass = this;
327       *fptr++ = rfield;
328     }
329   return result;
330 }
331
332 void
333 java::lang::Class::getSignature (java::lang::StringBuffer *buffer)
334 {
335   if (isPrimitive())
336     buffer->append((jchar) method_count);
337   else
338     {
339       jstring name = getName();
340       if (name->charAt(0) != '[')
341         buffer->append((jchar) 'L');
342       buffer->append(name);
343       if (name->charAt(0) != '[')
344         buffer->append((jchar) ';');
345     }
346 }
347
348 // This doesn't have to be native.  It is an implementation detail
349 // only called from the C++ code, though, so maybe this is clearer.
350 jstring
351 java::lang::Class::getSignature (JArray<jclass> *param_types,
352                                  jboolean is_constructor)
353 {
354   java::lang::StringBuffer *buf = new java::lang::StringBuffer ();
355   buf->append((jchar) '(');
356   // A NULL param_types means "no parameters".
357   if (param_types != NULL)
358     {
359       jclass *v = elements (param_types);
360       for (int i = 0; i < param_types->length; ++i)
361         v[i]->getSignature(buf);
362     }
363   buf->append((jchar) ')');
364   if (is_constructor)
365     buf->append((jchar) 'V');
366   return buf->toString();
367 }
368
369 java::lang::reflect::Method *
370 java::lang::Class::_getDeclaredMethod (jstring name,
371                                        JArray<jclass> *param_types)
372 {
373   jstring partial_sig = getSignature (param_types, false);
374   jint p_len = partial_sig->length();
375   _Jv_Utf8Const *utf_name = _Jv_makeUtf8Const (name);
376   int i = isPrimitive () ? 0 : method_count;
377   while (--i >= 0)
378     {
379       if (_Jv_equalUtf8Consts (methods[i].name, utf_name)
380           && _Jv_equaln (methods[i].signature, partial_sig, p_len)
381           && (methods[i].accflags
382               & java::lang::reflect::Modifier::INVISIBLE) == 0)
383         {
384           // Found it.
385           using namespace java::lang::reflect;
386           Method *rmethod = new Method ();
387           rmethod->offset = (char*) (&methods[i]) - (char*) methods;
388           rmethod->declaringClass = this;
389           return rmethod;
390         }
391     }
392   return NULL;
393 }
394
395 JArray<java::lang::reflect::Method *> *
396 java::lang::Class::getDeclaredMethods (void)
397 {
398   memberAccessCheck(java::lang::reflect::Member::DECLARED);
399
400   int numMethods = 0;
401   int max = isPrimitive () ? 0 : method_count;
402   int i;
403   for (i = max; --i >= 0; )
404     {
405       _Jv_Method *method = &methods[i];
406       if (method->name == NULL
407           || _Jv_equalUtf8Consts (method->name, clinit_name)
408           || _Jv_equalUtf8Consts (method->name, init_name)
409           || _Jv_equalUtf8Consts (method->name, finit_name)
410           || (methods[i].accflags
411               & java::lang::reflect::Modifier::INVISIBLE) != 0)
412         continue;
413       numMethods++;
414     }
415   JArray<java::lang::reflect::Method *> *result
416     = (JArray<java::lang::reflect::Method *> *)
417     JvNewObjectArray (numMethods, &java::lang::reflect::Method::class$, NULL);
418   java::lang::reflect::Method** mptr = elements (result);
419   for (i = 0;  i < max;  i++)
420     {
421       _Jv_Method *method = &methods[i];
422       if (method->name == NULL
423           || _Jv_equalUtf8Consts (method->name, clinit_name)
424           || _Jv_equalUtf8Consts (method->name, init_name)
425           || _Jv_equalUtf8Consts (method->name, finit_name)
426           || (methods[i].accflags
427               & java::lang::reflect::Modifier::INVISIBLE) != 0)
428         continue;
429       java::lang::reflect::Method* rmethod
430         = new java::lang::reflect::Method ();
431       rmethod->offset = (char*) method - (char*) methods;
432       rmethod->declaringClass = this;
433       *mptr++ = rmethod;
434     }
435   return result;
436 }
437
438 jstring
439 java::lang::Class::getName (void)
440 {
441   return name->toString();
442 }
443
444 JArray<jclass> *
445 java::lang::Class::getInterfaces (void)
446 {
447   jobjectArray r = JvNewObjectArray (interface_count, getClass (), NULL);
448   jobject *data = elements (r);
449   for (int i = 0; i < interface_count; ++i)
450     {
451       typedef unsigned int uaddr __attribute__ ((mode (pointer)));
452       data[i] = interfaces[i];
453       if ((uaddr)data[i] < (uaddr)constants.size)
454         fprintf (stderr, "ERROR !!!\n");
455     }
456   return reinterpret_cast<JArray<jclass> *> (r);
457 }
458
459 java::lang::reflect::Method *
460 java::lang::Class::_getMethod (jstring name, JArray<jclass> *param_types)
461 {
462   jstring partial_sig = getSignature (param_types, false);
463   jint p_len = partial_sig->length();
464   _Jv_Utf8Const *utf_name = _Jv_makeUtf8Const (name);
465
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           if (_Jv_equalUtf8Consts (klass->methods[i].name, utf_name)
472               && _Jv_equaln (klass->methods[i].signature, partial_sig, p_len)
473               && (klass->methods[i].accflags
474                   & java::lang::reflect::Modifier::INVISIBLE) == 0)
475             {
476               // Found it.
477               using namespace java::lang::reflect;
478
479               // Method must be public.
480               if (! Modifier::isPublic (klass->methods[i].accflags))
481                 break;
482
483               Method *rmethod = new Method ();
484               rmethod->offset = ((char *) (&klass->methods[i])
485                                  - (char *) klass->methods);
486               rmethod->declaringClass = klass;
487               return rmethod;
488             }
489         }
490     }
491
492   // If we haven't found a match, and this class is an interface, then
493   // check all the superinterfaces.
494   if (isInterface())
495     {
496       for (int i = 0; i < interface_count; ++i)
497         {
498           using namespace java::lang::reflect;
499           Method *rmethod = interfaces[i]->_getMethod (name, param_types);
500           if (rmethod != NULL)
501             return rmethod;
502         }
503     }
504
505   return NULL;
506 }
507
508 // This is a very slow implementation, since it re-scans all the
509 // methods we've already listed to make sure we haven't duplicated a
510 // method.  It also over-estimates the required size, so we have to
511 // shrink the result array later.
512 jint
513 java::lang::Class::_getMethods (JArray<java::lang::reflect::Method *> *result,
514                                 jint offset)
515 {
516   jint count = 0;
517
518   // First examine all local methods
519   for (int i = isPrimitive () ? 0 : method_count; --i >= 0; )
520     {
521       _Jv_Method *method = &methods[i];
522       if (method->name == NULL
523           || _Jv_equalUtf8Consts (method->name, clinit_name)
524           || _Jv_equalUtf8Consts (method->name, init_name)
525           || _Jv_equalUtf8Consts (method->name, finit_name)
526           || (method->accflags
527               & java::lang::reflect::Modifier::INVISIBLE) != 0)
528         continue;
529       // Only want public methods.
530       if (! java::lang::reflect::Modifier::isPublic (method->accflags))
531         continue;
532
533       // This is where we over-count the slots required if we aren't
534       // filling the result for real.
535       if (result != NULL)
536         {
537           jboolean add = true;
538           java::lang::reflect::Method **mp = elements (result);
539           // If we already have a method with this name and signature,
540           // then ignore this one.  This can happen with virtual
541           // methods.
542           for (int j = 0; j < offset; ++j)
543             {
544               _Jv_Method *meth_2 = _Jv_FromReflectedMethod (mp[j]);
545               if (_Jv_equalUtf8Consts (method->name, meth_2->name)
546                   && _Jv_equalUtf8Consts (method->signature,
547                                           meth_2->signature))
548                 {
549                   add = false;
550                   break;
551                 }
552             }
553           if (! add)
554             continue;
555         }
556
557       if (result != NULL)
558         {
559           using namespace java::lang::reflect;
560           Method *rmethod = new Method ();
561           rmethod->offset = (char *) method - (char *) methods;
562           rmethod->declaringClass = this;
563           Method **mp = elements (result);
564           mp[offset + count] = rmethod;
565         }
566       ++count;
567     }
568   offset += count;
569
570   // Now examine superclasses.
571   if (getSuperclass () != NULL)
572     {
573       jint s_count = getSuperclass()->_getMethods (result, offset);
574       offset += s_count;
575       count += s_count;
576     }
577
578   // Finally, examine interfaces.
579   for (int i = 0; i < interface_count; ++i)
580     {
581       int f_count = interfaces[i]->_getMethods (result, offset);
582       count += f_count;
583       offset += f_count;
584     }
585
586   return count;
587 }
588
589 JArray<java::lang::reflect::Method *> *
590 java::lang::Class::getMethods (void)
591 {
592   using namespace java::lang::reflect;
593
594   memberAccessCheck(Member::PUBLIC);
595
596   // This will overestimate the size we need.
597   jint count = _getMethods (NULL, 0);
598
599   JArray<Method *> *result
600     = ((JArray<Method *> *) JvNewObjectArray (count,
601                                               &Method::class$,
602                                               NULL));
603
604   // When filling the array for real, we get the actual count.  Then
605   // we resize the array.
606   jint real_count = _getMethods (result, 0);
607
608   if (real_count != count)
609     {
610       JArray<Method *> *r2
611         = ((JArray<Method *> *) JvNewObjectArray (real_count,
612                                                   &Method::class$,
613                                                   NULL));
614       
615       Method **destp = elements (r2);
616       Method **srcp = elements (result);
617
618       for (int i = 0; i < real_count; ++i)
619         *destp++ = *srcp++;
620
621       result = r2;
622     }
623
624   return result;
625 }
626
627 jboolean
628 java::lang::Class::isAssignableFrom (jclass klass)
629 {
630   // Arguments may not have been initialized, given ".class" syntax.
631   // This ensures we can at least look at their superclasses.
632   _Jv_Linker::wait_for_state (this, JV_STATE_LOADING);
633   _Jv_Linker::wait_for_state (klass, JV_STATE_LOADING);
634   return _Jv_IsAssignableFrom (klass, this);
635 }
636
637 jboolean
638 java::lang::Class::isInstance (jobject obj)
639 {
640   if (! obj)
641     return false;
642   return _Jv_IsAssignableFrom (JV_CLASS (obj), this);
643 }
644
645 jobject
646 java::lang::Class::newInstance (void)
647 {
648   memberAccessCheck(java::lang::reflect::Member::PUBLIC);
649
650   if (isPrimitive ()
651       || isInterface ()
652       || isArray ()
653       || java::lang::reflect::Modifier::isAbstract(accflags))
654     throw new java::lang::InstantiationException (getName ());
655
656   _Jv_InitClass (this);
657
658   _Jv_Method *meth = _Jv_GetMethodLocal (this, init_name, void_signature);
659   if (! meth)
660     throw new java::lang::InstantiationException (getName());
661
662   jobject r = _Jv_AllocObject (this);
663   ((void (*) (jobject)) meth->ncode) (r);
664   return r;
665 }
666
667 void
668 java::lang::Class::finalize (void)
669 {
670   engine->unregister(this);
671 }
672
673 // This implements the initialization process for a class.  From Spec
674 // section 12.4.2.
675 void
676 java::lang::Class::initializeClass (void)
677 {
678   // Short-circuit to avoid needless locking (expression includes
679   // JV_STATE_PHANTOM and JV_STATE_DONE).
680   if (state >= JV_STATE_PHANTOM)
681     return;
682
683   // Step 1.  We introduce a new scope so we can synchronize more
684   // easily.
685   {
686     JvSynchronize sync (this);
687
688     if (state < JV_STATE_LINKED)
689       {
690         try
691           {
692             _Jv_Linker::wait_for_state(this, JV_STATE_LINKED);
693           }
694         catch (java::lang::SecurityException *x)
695           {
696             throw x;
697           }
698         catch (java::lang::Throwable *x)
699           {
700             // Turn into a NoClassDefFoundError.
701             java::lang::NoClassDefFoundError *result
702               = new java::lang::NoClassDefFoundError(getName());
703             result->initCause(x);
704             throw result;
705           }
706       }
707
708     // Step 2.
709     java::lang::Thread *self = java::lang::Thread::currentThread();
710     self = (java::lang::Thread *) ((long) self | 1);
711     while (state == JV_STATE_IN_PROGRESS && thread && thread != self)
712       wait ();
713
714     // Steps 3 &  4.
715     if (state == JV_STATE_DONE || state == JV_STATE_IN_PROGRESS)
716       return;
717
718     // Step 5.
719     if (state == JV_STATE_ERROR)
720       throw new java::lang::NoClassDefFoundError (getName());
721
722     // Step 6.
723     thread = self;
724     _Jv_Linker::wait_for_state (this, JV_STATE_LINKED);
725     state = JV_STATE_IN_PROGRESS;
726   }
727
728   // Step 7.
729   if (! isInterface () && superclass)
730     {
731       try
732         {
733           _Jv_InitClass (superclass);
734         }
735       catch (java::lang::SecurityException *x)
736         {
737           throw x;
738         }
739       catch (java::lang::Throwable *except)
740         {
741           // Caught an exception.
742           JvSynchronize sync (this);
743           state = JV_STATE_ERROR;
744           notifyAll ();
745           throw except;
746         }
747     }
748
749   // Steps 8, 9, 10, 11.
750   try
751     {
752       _Jv_Method *meth = _Jv_GetMethodLocal (this, clinit_name,
753                                              void_signature);
754       if (meth)
755         ((void (*) (void)) meth->ncode) ();
756     }
757   catch (java::lang::SecurityException *x)
758     {
759       throw x;
760     }
761   catch (java::lang::Throwable *except)
762     {
763       if (! java::lang::Error::class$.isInstance(except))
764         {
765           try
766             {
767               except = new ExceptionInInitializerError (except);
768             }
769           catch (java::lang::Throwable *t)
770             {
771               except = t;
772             }
773         }
774
775       JvSynchronize sync (this);
776       state = JV_STATE_ERROR;
777       notifyAll ();
778       throw except;
779     }
780
781   JvSynchronize sync (this);
782   state = JV_STATE_DONE;
783   notifyAll ();
784 }
785
786 // Only used by serialization
787 java::lang::reflect::Field *
788 java::lang::Class::getPrivateField (jstring name)
789 {
790   int hash = name->hashCode ();
791
792   java::lang::reflect::Field* rfield;
793   for (int i = 0;  i < field_count;  i++)
794     {
795       _Jv_Field *field = &fields[i];
796       if (! _Jv_equal (field->name, name, hash))
797         continue;
798       rfield = new java::lang::reflect::Field ();
799       rfield->offset = (char*) field - (char*) fields;
800       rfield->declaringClass = this;
801       rfield->name = name;
802       return rfield;
803     }
804   jclass superclass = getSuperclass();
805   if (superclass == NULL)
806     return NULL;
807   rfield = superclass->getPrivateField(name);
808   for (int i = 0; i < interface_count && rfield == NULL; ++i)
809     rfield = interfaces[i]->getPrivateField (name);
810   return rfield;
811 }
812
813 // Only used by serialization
814 java::lang::reflect::Method *
815 java::lang::Class::getPrivateMethod (jstring name, JArray<jclass> *param_types)
816 {
817   jstring partial_sig = getSignature (param_types, false);
818   jint p_len = partial_sig->length();
819   _Jv_Utf8Const *utf_name = _Jv_makeUtf8Const (name);
820   for (Class *klass = this; klass; klass = klass->getSuperclass())
821     {
822       int i = klass->isPrimitive () ? 0 : klass->method_count;
823       while (--i >= 0)
824         {
825           if (_Jv_equalUtf8Consts (klass->methods[i].name, utf_name)
826               && _Jv_equaln (klass->methods[i].signature, partial_sig, p_len))
827             {
828               // Found it.
829               using namespace java::lang::reflect;
830
831               Method *rmethod = new Method ();
832               rmethod->offset = ((char *) (&klass->methods[i])
833                                  - (char *) klass->methods);
834               rmethod->declaringClass = klass;
835               return rmethod;
836             }
837         }
838     }
839   throw new java::lang::NoSuchMethodException (name);
840 }
841
842 // Private accessor method for Java code to retrieve the protection domain.
843 java::security::ProtectionDomain *
844 java::lang::Class::getProtectionDomain0 ()
845 {
846   return protectionDomain;
847 }
848
849 JArray<jobject> *
850 java::lang::Class::getSigners()
851 {
852   return hack_signers;
853 }
854
855 void
856 java::lang::Class::setSigners(JArray<jobject> *s)
857 {
858   hack_signers = s;
859 }
860
861 \f
862
863 static unsigned char
864 read_u1 (unsigned char *&p)
865 {
866   return *p++;
867 }
868
869 static unsigned char
870 read_u1 (unsigned char *&p, unsigned char *next)
871 {
872   if (next - p < 1)
873     throw new java::lang::InternalError();
874   return *p++;
875 }
876
877 static unsigned int
878 read_u2 (unsigned char *&p)
879 {
880   unsigned int b1 = *p++;
881   unsigned int b2 = *p++;
882   return (b1 << 8) | b2;
883 }
884
885 static unsigned int
886 read_u2 (unsigned char *&p, unsigned char *next)
887 {
888   if (next - p < 2)
889     throw new java::lang::InternalError();
890   return read_u2 (p);
891 }
892
893 static int
894 read_4 (unsigned char *&p)
895 {
896   int b1 = *p++;
897   int b2 = *p++;
898   int b3 = *p++;
899   int b4 = *p++;
900   return (b1 << 24) | (b2 << 16) | (b3 << 8) | b4;
901 }
902
903 jstring
904 java::lang::Class::getReflectionSignature (jint /*jv_attr_type*/ type,
905                                            jint obj_index)
906 {
907   // We just re-parse the bytecode for this data each time.  If
908   // necessary we can cache results, but I suspect this is not
909   // performance sensitive.
910   unsigned char *bytes = reflection_data;
911   if (bytes == NULL)
912     return NULL;
913   while (true)
914     {
915       int kind = read_u1 (bytes);
916       if (kind == JV_DONE_ATTR)
917         return NULL;
918       int len = read_4 (bytes);
919       unsigned char *next = bytes + len;
920       if (kind != type)
921         {
922           bytes = next;
923           continue;
924         }
925       if (type != JV_CLASS_ATTR)
926         {
927           unsigned short index = read_u2 (bytes, next);
928           if (index != obj_index)
929             {
930               bytes = next;
931               continue;
932             }
933         }
934       int nt = read_u1 (bytes, next);
935       if (nt != JV_SIGNATURE_KIND)
936         {
937           bytes = next;
938           continue;
939         }
940       unsigned int cpool_idx = read_u2 (bytes, next);
941       if (cpool_idx >= (unsigned int) constants.size
942           || constants.tags[cpool_idx] != JV_CONSTANT_Utf8)
943         {
944           // We just ignore errors for now.  It isn't clear what is
945           // best to do here, as an encoding error here means a bug
946           // either in the compiler or in defineclass.cc.
947           return NULL;
948         }
949       return _Jv_NewStringUtf8Const (constants.data[cpool_idx].utf8);
950     }
951 }
952
953 jstring
954 java::lang::Class::getReflectionSignature (::java::lang::reflect::Constructor *c)
955 {
956   _Jv_Method *meth = _Jv_FromReflectedConstructor (c);
957   unsigned short meth_index = meth - methods;
958   return getReflectionSignature (JV_METHOD_ATTR, meth_index);
959 }
960
961 jstring
962 java::lang::Class::getReflectionSignature (::java::lang::reflect::Method *m)
963 {
964   _Jv_Method *meth = _Jv_FromReflectedMethod (m);
965   unsigned short meth_index = meth - methods;
966   return getReflectionSignature (JV_METHOD_ATTR, meth_index);
967 }
968
969 jstring
970 java::lang::Class::getReflectionSignature (::java::lang::reflect::Field *f)
971 {
972   _Jv_Field *fld = _Jv_FromReflectedField (f);
973   unsigned short fld_index = fld - fields;
974   return getReflectionSignature (JV_FIELD_ATTR, fld_index);
975 }
976
977 jstring
978 java::lang::Class::getClassSignature()
979 {
980   return getReflectionSignature (JV_CLASS_ATTR, 0);
981 }
982
983 jint
984 java::lang::Class::getEnclosingMethodData()
985 {
986   unsigned char *bytes = reflection_data;
987   if (bytes == NULL)
988     return 0;
989   while (true)
990     {
991       int kind = read_u1 (bytes);
992       if (kind == JV_DONE_ATTR)
993         return 0;
994       int len = read_4 (bytes);
995       unsigned char *next = bytes + len;
996       if (kind != JV_CLASS_ATTR)
997         {
998           bytes = next;
999           continue;
1000         }
1001       int type = read_u1 (bytes, next);
1002       if (type != JV_ENCLOSING_METHOD_KIND)
1003         {
1004           bytes = next;
1005           continue;
1006         }
1007       int class_index = read_u2 (bytes, next);
1008       int method_index = read_u2 (bytes, next);
1009       _Jv_word result;
1010       _Jv_storeIndexes (&result, class_index, method_index);
1011       return result.i;
1012     }
1013 }
1014
1015 jclass
1016 java::lang::Class::getEnclosingClass()
1017 {
1018   _Jv_word indexes;
1019   indexes.i = getEnclosingMethodData();
1020   if (indexes.i == 0)
1021     return NULL;
1022   _Jv_ushort class_index, method_index;
1023   _Jv_loadIndexes (&indexes, class_index, method_index);
1024   return _Jv_Linker::resolve_pool_entry (this, class_index).clazz;
1025 }
1026
1027 ::java::lang::reflect::Method *
1028 java::lang::Class::getEnclosingMethod()
1029 {
1030   _Jv_word indexes;
1031   indexes.i = getEnclosingMethodData();
1032   if (indexes.i == 0)
1033     return NULL;
1034   _Jv_ushort class_index, method_index;
1035   _Jv_loadIndexes (&indexes, class_index, method_index);
1036   jclass found_class;
1037   _Jv_Method *method = _Jv_Linker::resolve_method_entry (this, found_class,
1038                                                          class_index,
1039                                                          method_index,
1040                                                          false, false);
1041   using namespace java::lang::reflect;
1042   Method *rmethod = new Method ();
1043   rmethod->offset = (char *) method - (char *) found_class->methods;
1044   rmethod->declaringClass = found_class;
1045   return rmethod;
1046 }
1047
1048 ::java::lang::reflect::Constructor *
1049 java::lang::Class::getEnclosingConstructor()
1050 {
1051   _Jv_word indexes;
1052   indexes.i = getEnclosingMethodData();
1053   if (indexes.i == 0)
1054     return NULL;
1055   _Jv_ushort class_index, method_index;
1056   _Jv_loadIndexes (&indexes, class_index, method_index);
1057   jclass found_class;
1058   _Jv_Method *method = _Jv_Linker::resolve_method_entry (this, found_class,
1059                                                          class_index,
1060                                                          method_index,
1061                                                          false, false);
1062   using namespace java::lang::reflect;
1063   Constructor *cons = new Constructor ();
1064   cons->offset = (char *) method - (char *) found_class->methods;
1065   cons->declaringClass = this;
1066   return cons;
1067 }
1068
1069 static void
1070 check_constant (_Jv_Constants *pool, jint cpool_index, jint type)
1071 {
1072   if (cpool_index <= 0 || cpool_index >= pool->size)
1073     throw new InternalError(JvNewStringLatin1("invalid constant pool index"));
1074   if ((pool->tags[cpool_index] & 
1075         ~(JV_CONSTANT_ResolvedFlag|JV_CONSTANT_LazyFlag)) != type)
1076     {
1077       ::java::lang::StringBuffer *sb = new ::java::lang::StringBuffer();
1078       sb->append(JvNewStringLatin1("expected pool constant "));
1079       sb->append(type);
1080       sb->append(JvNewStringLatin1(" but got "));
1081       sb->append(jint (pool->tags[cpool_index]));
1082       throw new InternalError(sb->toString());
1083     }
1084 }
1085
1086 // Forward declaration
1087 static ::java::lang::annotation::Annotation *
1088 parseAnnotation(jclass klass, _Jv_Constants *pool,
1089                 unsigned char *&bytes, unsigned char *last);
1090
1091 static jobject
1092 parseAnnotationElement(jclass klass, _Jv_Constants *pool,
1093                        unsigned char *&bytes, unsigned char *last)
1094 {
1095   int tag = read_u1 (bytes, last);
1096   jobject result;
1097   switch (tag)
1098     {
1099     case 'B':
1100       {
1101         int cindex = read_u2 (bytes, last);
1102         check_constant (pool, cindex, JV_CONSTANT_Integer);
1103         result = Byte::valueOf (pool->data[cindex].i);
1104       }
1105       break;
1106     case 'C':
1107       {
1108         int cindex = read_u2 (bytes, last);
1109         check_constant (pool, cindex, JV_CONSTANT_Integer);
1110         result = Character::valueOf (pool->data[cindex].i);
1111       }
1112       break;
1113     case 'S':
1114       {
1115         int cindex = read_u2 (bytes, last);
1116         check_constant (pool, cindex, JV_CONSTANT_Integer);
1117         result = Short::valueOf (pool->data[cindex].i);
1118       }
1119       break;
1120     case 'Z':
1121       {
1122         int cindex = read_u2 (bytes, last);
1123         check_constant (pool, cindex, JV_CONSTANT_Integer);
1124         result = Boolean::valueOf (jboolean (pool->data[cindex].i));
1125       }
1126       break;
1127     case 'I':
1128       {
1129         int cindex = read_u2 (bytes, last);
1130         check_constant (pool, cindex, JV_CONSTANT_Integer);
1131         result = Integer::valueOf (pool->data[cindex].i);
1132       }
1133       break;
1134     case 'D':
1135       {
1136         int cindex = read_u2 (bytes, last);
1137         check_constant (pool, cindex, JV_CONSTANT_Double);
1138         _Jv_word2 word;
1139         memcpy (&word, &pool->data[cindex], 2 * sizeof (_Jv_word));
1140         result = Double::valueOf (word.d);
1141       }
1142       break;
1143     case 'F':
1144       {
1145         int cindex = read_u2 (bytes, last);
1146         check_constant (pool, cindex, JV_CONSTANT_Float);
1147         result = Float::valueOf (pool->data[cindex].f);
1148       }
1149       break;
1150     case 'J':
1151       {
1152         int cindex = read_u2 (bytes, last);
1153         check_constant (pool, cindex, JV_CONSTANT_Long);
1154         _Jv_word2 word;
1155         memcpy (&word, &pool->data[cindex], 2 * sizeof (_Jv_word));
1156         result = Long::valueOf (word.l);
1157       }
1158       break;
1159     case 's':
1160       {
1161         int cindex = read_u2 (bytes, last);
1162         // Despite what the JVM spec says, compilers generate a Utf8
1163         // constant here, not a String.
1164         check_constant (pool, cindex, JV_CONSTANT_Utf8);
1165         result = pool->data[cindex].utf8->toString();
1166       }
1167       break;
1168     case 'e':
1169       {
1170         int type_name_index = read_u2 (bytes, last);
1171         check_constant (pool, type_name_index, JV_CONSTANT_Utf8);
1172         int const_name_index = read_u2 (bytes, last);
1173         check_constant (pool, const_name_index, JV_CONSTANT_Utf8);
1174
1175         _Jv_Utf8Const *u_name = pool->data[type_name_index].utf8;
1176         _Jv_Utf8Const *e_name = pool->data[const_name_index].utf8;
1177
1178         // FIXME: throw correct exceptions at the correct times.
1179         jclass e_class = _Jv_FindClassFromSignature(u_name->chars(),
1180                                                     klass->getClassLoaderInternal());
1181         result = ::java::lang::Enum::valueOf(e_class, e_name->toString());
1182       }
1183       break;
1184     case 'c':
1185       {
1186         int cindex = read_u2 (bytes, last);
1187         check_constant (pool, cindex, JV_CONSTANT_Utf8);
1188         _Jv_Utf8Const *u_name = pool->data[cindex].utf8;
1189         jclass anno_class
1190           = _Jv_FindClassFromSignatureNoException(u_name->chars(),
1191                                                   klass->getClassLoaderInternal());
1192         // FIXME: not correct: we should lazily do this when trying to
1193         // read the element.  This means that
1194         // AnnotationInvocationHandler needs to have a special case.
1195         if (! anno_class)
1196           // FIXME: original exception...
1197           throw new TypeNotPresentException(u_name->toString(), NULL);
1198         result = anno_class;
1199       }
1200       break;
1201     case '@':
1202       result = parseAnnotation (klass, pool, bytes, last);
1203       break;
1204     case '[':
1205       {
1206         int n_array_elts = read_u2 (bytes, last);
1207         jobjectArray aresult = _Jv_NewObjectArray (n_array_elts,
1208                                                    &Object::class$, NULL);
1209         jobject *elts = elements (aresult);
1210         for (int i = 0; i < n_array_elts; ++i)
1211           elts[i] = parseAnnotationElement(klass, pool, bytes, last);
1212         result = aresult;
1213       }
1214       break;
1215     default:
1216       throw new java::lang::InternalError();
1217     }
1218   return result;
1219 }
1220
1221 static ::java::lang::annotation::Annotation *
1222 parseAnnotation(jclass klass, _Jv_Constants *pool,
1223                 unsigned char *&bytes, unsigned char *last)
1224 {
1225   int type_index = read_u2 (bytes, last);
1226   check_constant (pool, type_index, JV_CONSTANT_Utf8);
1227
1228   _Jv_Utf8Const *u_name = pool->data[type_index].utf8;
1229   jclass anno_class = _Jv_FindClassFromSignatureNoException(u_name->chars(),
1230                                                             klass->getClassLoaderInternal());
1231   // FIXME: what to do if anno_class==NULL?
1232
1233   ::java::util::HashMap *hmap = new ::java::util::HashMap();
1234   int npairs = read_u2 (bytes, last);
1235   for (int i = 0; i < npairs; ++i)
1236     {
1237       int name_index = read_u2 (bytes, last);
1238       check_constant (pool, name_index, JV_CONSTANT_Utf8);
1239       jstring name = _Jv_NewStringUtf8Const (pool->data[name_index].utf8);
1240       jobject value = parseAnnotationElement (klass, pool, bytes, last);
1241       // FIXME: any checks needed for name?
1242       hmap->put(name, value);
1243     }
1244   using namespace ::sun::reflect::annotation;
1245   return AnnotationInvocationHandler::create (anno_class,
1246                                               (::java::util::Map *) hmap);
1247 }
1248
1249 static jobjectArray
1250 parseAnnotations(jclass klass, _Jv_Constants *pool,
1251                  unsigned char *&bytes, unsigned char *last)
1252 {
1253   int num = read_u2 (bytes, last);
1254   jobjectArray result = _Jv_NewObjectArray (num,
1255                                             &::java::lang::annotation::Annotation::class$,
1256                                             NULL);
1257   jobject *elts = elements (result);
1258   for (int i = 0; i < num; ++i)
1259     elts[i] = parseAnnotation(klass, pool, bytes, last);
1260   return result;
1261 }
1262
1263 static jobjectArray
1264 parseParameterAnnotations(jclass klass, _Jv_Constants *pool,
1265                           unsigned char *&bytes, unsigned char *last)
1266 {
1267   jclass anno = &::java::lang::annotation::Annotation::class$;
1268   jclass annoary = _Jv_GetArrayClass (anno, anno->getClassLoaderInternal());
1269
1270   // FIXME: something should check the number of params versus the
1271   // method
1272   int n_params = read_u1 (bytes, last);
1273   jobjectArray result = _Jv_NewObjectArray (n_params, annoary, NULL);
1274   jobject *elts = elements (result);
1275   for (int i = 0; i < n_params; ++i)
1276     elts[i] = parseAnnotations(klass, pool, bytes, last);
1277   return result;
1278 }
1279
1280 jobject
1281 java::lang::Class::getMethodDefaultValue(::java::lang::reflect::Method *meth)
1282 {
1283   // FIXME: could cache the value here...
1284
1285   unsigned char *bytes = reflection_data;
1286   if (bytes == NULL)
1287     return 0;
1288
1289   unsigned short meth_index = _Jv_FromReflectedMethod (meth) - methods;
1290
1291   while (true)
1292     {
1293       int type = read_u1 (bytes);
1294       if (type == JV_DONE_ATTR)
1295         return NULL;
1296       int len = read_4 (bytes);
1297       unsigned char *next = bytes + len;
1298       if (type != JV_METHOD_ATTR)
1299         {
1300           bytes = next;
1301           continue;
1302         }
1303       int kind = read_u1 (bytes, next);
1304       if (kind != JV_ANNOTATION_DEFAULT_KIND)
1305         {
1306           bytes = next;
1307           continue;
1308         }
1309       int index = read_u2 (bytes, next);
1310       if (meth_index != index)
1311         {
1312           bytes = next;
1313           continue;
1314         }
1315
1316       // FIXME: could cache here.  If we do then we have to clone any
1317       // array result.
1318       return parseAnnotationElement(this, &this->constants, bytes, next);
1319     }
1320 }
1321
1322 jobjectArray
1323 java::lang::Class::getDeclaredAnnotations(jint /* jv_attr_type */ member_type,
1324                                           jint member_index,
1325                                           jint /* jv_attr_kind */ kind_req)
1326 {
1327   using namespace java::lang::annotation;
1328   jobjectArray result;
1329
1330   unsigned char *bytes = reflection_data;
1331   if (bytes == NULL)
1332     return 0;
1333
1334   if (loader == NULL)
1335     loader = (ClassLoader *)VMClassLoader::bootLoader;
1336
1337   result = (loader->getDeclaredAnnotations
1338             (this, member_type, member_index, kind_req));
1339   if (result)
1340     return result;
1341
1342   for (;;)
1343     {
1344       int type = read_u1 (bytes);
1345       if (type == JV_DONE_ATTR)
1346         return NULL;
1347       int len = read_4 (bytes);
1348       unsigned char *next = bytes + len;
1349       if (type != member_type)
1350         {
1351           bytes = next;
1352           continue;
1353         }
1354       int kind = read_u1 (bytes, next);
1355       if (kind != kind_req)
1356         {
1357           bytes = next;
1358           continue;
1359         }
1360       if (member_type != JV_CLASS_ATTR)
1361         {
1362           int index = read_u2 (bytes, next);
1363           if (member_index != index)
1364             {
1365               bytes = next;
1366               continue;
1367             }
1368         }
1369
1370       if (kind_req == JV_PARAMETER_ANNOTATIONS_KIND)
1371         result = ((parseParameterAnnotations 
1372                    (this, &this->constants, bytes, next)));
1373       else
1374         result = ((parseAnnotations (this, &this->constants, bytes, next)));
1375       break;
1376     }
1377
1378   return (loader->putDeclaredAnnotations
1379           (this, member_type, member_index, kind_req, result));
1380 }
1381
1382 jobjectArray
1383 java::lang::Class::getDeclaredAnnotations(::java::lang::reflect::Method *meth,
1384                                           jboolean is_param)
1385 {
1386   unsigned short meth_index = _Jv_FromReflectedMethod (meth) - methods;
1387   return getDeclaredAnnotations(JV_METHOD_ATTR, meth_index,
1388                                 (is_param
1389                                  ? JV_PARAMETER_ANNOTATIONS_KIND
1390                                  : JV_ANNOTATIONS_KIND));
1391 }
1392
1393 jobjectArray
1394 java::lang::Class::getDeclaredAnnotations(::java::lang::reflect::Constructor *cons,
1395                                           jboolean is_param)
1396 {
1397   unsigned short meth_index = _Jv_FromReflectedConstructor (cons) - methods;
1398   return getDeclaredAnnotations(JV_METHOD_ATTR, meth_index,
1399                                 (is_param
1400                                  ? JV_PARAMETER_ANNOTATIONS_KIND
1401                                  : JV_ANNOTATIONS_KIND));
1402 }
1403
1404 jobjectArray
1405 java::lang::Class::getDeclaredAnnotations(::java::lang::reflect::Field *fld)
1406 {
1407   unsigned short field_index = _Jv_FromReflectedField (fld) - fields;
1408   return getDeclaredAnnotations(JV_FIELD_ATTR, field_index,
1409                                 JV_ANNOTATIONS_KIND);
1410 }
1411
1412 JArray< ::java::lang::annotation::Annotation *> *
1413 java::lang::Class::getDeclaredAnnotationsInternal()
1414 {
1415   return (JArray< ::java::lang::annotation::Annotation *> *) getDeclaredAnnotations(JV_CLASS_ATTR, 0, JV_ANNOTATIONS_KIND);
1416 }
1417
1418 static jclass
1419 resolve_class_constant (jclass klass, _Jv_Constants *pool, int cpool_index)
1420 {
1421   check_constant (pool, cpool_index, JV_CONSTANT_Class);
1422   // FIXME: what is the correct thing to do with an exception here?
1423   return _Jv_Linker::resolve_pool_entry (klass, cpool_index, false).clazz;
1424 }
1425
1426 jint
1427 java::lang::Class::findInnerClassAttribute()
1428 {
1429   unsigned char *bytes = reflection_data;
1430   if (bytes == NULL)
1431     return -1;
1432   while (true)
1433     {
1434       int type = read_u1 (bytes);
1435       if (type == JV_DONE_ATTR)
1436         break;
1437       // After the type but before the length.
1438       unsigned char *save = bytes;
1439       int len = read_4 (bytes);
1440       unsigned char *next = bytes + len;
1441       if (type != JV_CLASS_ATTR)
1442         {
1443           bytes = next;
1444           continue;
1445         }
1446       int kind = read_u1 (bytes, next);
1447       if (kind != JV_INNER_CLASSES_KIND)
1448         {
1449           bytes = next;
1450           continue;
1451         }
1452       return save - reflection_data;
1453     }
1454   return -1;
1455 }
1456
1457 jint
1458 java::lang::Class::findDeclaredClasses(JArray<jclass> *result,
1459                                        jboolean publicOnly,
1460                                        jint offset)
1461 {
1462   unsigned char *bytes = reflection_data + offset;
1463   int len = read_4 (bytes);
1464   unsigned char *next = bytes + len;
1465   // Skip a byte.
1466   read_u1 (bytes, next);
1467   int n_classes = read_u2 (bytes, next);
1468   int count = 0;
1469   for (int i = 0; i < n_classes; ++i)
1470     {
1471       int inner_class_index = read_u2 (bytes, next);
1472       int outer_class_index = read_u2 (bytes, next);
1473       /*int inner_name_index = */ read_u2 (bytes, next);
1474       int inner_flags = read_u2 (bytes, next);
1475
1476       if (inner_class_index == 0 || outer_class_index == 0)
1477         continue;
1478       if (resolve_class_constant (this, &constants, outer_class_index) == this)
1479         {
1480           jclass inner = resolve_class_constant (this, &constants,
1481                                                  inner_class_index);
1482           if (! publicOnly
1483               || ((inner_flags
1484                    & java::lang::reflect::Modifier::PUBLIC) != 0))
1485             {
1486               if (result)
1487                 {
1488                   jclass *elts = elements (result);
1489                   elts[count] = inner;
1490                 }
1491               ++count;
1492             }
1493         }
1494     }
1495
1496   return count;
1497 }
1498
1499 JArray<jclass> *
1500 java::lang::Class::getDeclaredClasses (jboolean publicOnly)
1501 {
1502   int offset = findInnerClassAttribute();
1503   int count;
1504   if (offset == -1)
1505     {
1506       // No InnerClasses attribute, so no declared classes.
1507       count = 0;
1508     }
1509   else
1510     count = findDeclaredClasses(NULL, publicOnly, offset);
1511   JArray<jclass> *result
1512     = (JArray<jclass> *) JvNewObjectArray (count, &java::lang::Class::class$,
1513                                            NULL);
1514   if (count > 0)
1515     findDeclaredClasses(result, publicOnly, offset);
1516   return result;
1517 }
1518
1519 jclass
1520 java::lang::Class::getDeclaringClass (void)
1521 {
1522   int offset = findInnerClassAttribute();
1523   if (offset == -1)
1524     return NULL;
1525
1526   unsigned char *bytes = reflection_data + offset;
1527   int len = read_4 (bytes);
1528   unsigned char *next = bytes + len;
1529   // Skip a byte.
1530   read_u1 (bytes, next);
1531   int n_classes = read_u2 (bytes, next);
1532   for (int i = 0; i < n_classes; ++i)
1533     {
1534       int inner_class_index = read_u2 (bytes, next);
1535       int outer_class_index = read_u2 (bytes, next);
1536       /*int inner_name_index = */read_u2 (bytes, next);
1537       /*int inner_flags = */read_u2 (bytes, next);
1538
1539       if (inner_class_index == 0 || outer_class_index == 0)
1540         continue;
1541       if (resolve_class_constant (this, &constants, inner_class_index) == this)
1542         return resolve_class_constant (this, &constants, outer_class_index);
1543     }
1544
1545   return NULL;
1546 }
1547
1548 jboolean
1549 java::lang::Class::isAnonymousClass()
1550 {
1551   int offset = findInnerClassAttribute();
1552   if (offset == -1)
1553     return false;
1554
1555   unsigned char *bytes = reflection_data + offset;
1556   int len = read_4 (bytes);
1557   unsigned char *next = bytes + len;
1558   // Skip a byte.
1559   read_u1 (bytes, next);
1560   int n_classes = read_u2 (bytes, next);
1561   for (int i = 0; i < n_classes; ++i)
1562     {
1563       int inner_class_index = read_u2 (bytes, next);
1564       /*int outer_class_index = */read_u2 (bytes, next);
1565       int inner_name_index = read_u2 (bytes, next);
1566       /*int inner_flags = */read_u2 (bytes, next);
1567
1568       if (inner_class_index == 0)
1569         continue;
1570       if (resolve_class_constant (this, &constants, inner_class_index) == this)
1571         return inner_name_index == 0;
1572     }
1573
1574   return false;
1575 }
1576
1577 jboolean
1578 java::lang::Class::isLocalClass()
1579 {
1580   _Jv_word indexes;
1581   indexes.i = getEnclosingMethodData();
1582   return indexes.i != 0;
1583 }
1584
1585 jboolean
1586 java::lang::Class::isMemberClass()
1587 {
1588   // FIXME: is this correct?
1589   return !isLocalClass() && getDeclaringClass() != NULL;
1590 }
1591
1592 \f
1593
1594 //
1595 // Some class-related convenience functions.
1596 //
1597
1598 // Find a method declared in the class.  If it is not declared locally
1599 // (or if it is inherited), return NULL.
1600 _Jv_Method *
1601 _Jv_GetMethodLocal (jclass klass, _Jv_Utf8Const *name,
1602                     _Jv_Utf8Const *signature)
1603 {
1604   for (int i = 0; i < klass->method_count; ++i)
1605     {
1606       if (_Jv_equalUtf8Consts (name, klass->methods[i].name)
1607           && _Jv_equalUtf8Consts (signature, klass->methods[i].signature))
1608         return &klass->methods[i];
1609     }
1610   return NULL;
1611 }
1612
1613 _Jv_Method *
1614 _Jv_LookupDeclaredMethod (jclass klass, _Jv_Utf8Const *name,
1615                           _Jv_Utf8Const *signature,
1616                           jclass *declarer_result)
1617 {
1618   for (; klass; klass = klass->getSuperclass())
1619     {
1620       _Jv_Method *meth = _Jv_GetMethodLocal (klass, name, signature);
1621
1622       if (meth)
1623         {
1624           if (declarer_result)
1625             *declarer_result = klass;
1626           return meth;
1627         }
1628     }
1629
1630   return NULL;
1631 }
1632
1633 java::lang::reflect::Method *
1634 _Jv_GetReflectedMethod (jclass klass, _Jv_Utf8Const *name,
1635                        _Jv_Utf8Const *signature)
1636 {
1637   for (; klass; klass = klass->getSuperclass())
1638     {
1639       _Jv_Method *meth = _Jv_GetMethodLocal (klass, name, signature);
1640       if (meth)
1641         {
1642           using namespace java::lang::reflect;
1643           Method *rmethod = new Method ();
1644           rmethod->offset = (char*) meth - (char*) klass->methods;
1645           rmethod->declaringClass = klass;
1646           return rmethod;
1647         }
1648     }
1649   
1650   return NULL;
1651 }
1652
1653 #ifdef HAVE_TLS
1654
1655 // NOTE: MCACHE_SIZE should be a power of 2 minus one.
1656 #define MCACHE_SIZE 31
1657
1658 struct _Jv_mcache
1659 {
1660   jclass klass;
1661   _Jv_Method *method;
1662 };
1663
1664 static __thread _Jv_mcache *method_cache;
1665 #endif // HAVE_TLS
1666
1667 static void *
1668 _Jv_FindMethodInCache (jclass klass MAYBE_UNUSED,
1669                        _Jv_Utf8Const *name MAYBE_UNUSED,
1670                        _Jv_Utf8Const *signature MAYBE_UNUSED)
1671 {
1672 #ifdef HAVE_TLS
1673   _Jv_mcache *cache = method_cache;
1674   if (cache)
1675     {
1676       int index = name->hash16 () & MCACHE_SIZE;
1677       _Jv_mcache *mc = &cache[index];
1678       _Jv_Method *m = mc->method;
1679
1680       if (mc->klass == klass
1681           && _Jv_equalUtf8Consts (m->name, name)
1682           && _Jv_equalUtf8Consts (m->signature, signature))
1683         return mc->method->ncode;
1684     }
1685 #endif // HAVE_TLS
1686   return NULL;
1687 }
1688
1689 static void
1690 _Jv_AddMethodToCache (jclass klass MAYBE_UNUSED,
1691                       _Jv_Method *method MAYBE_UNUSED)
1692 {
1693 #ifdef HAVE_TLS
1694   if (method_cache == NULL)
1695     method_cache = (_Jv_mcache *) _Jv_MallocUnchecked((MCACHE_SIZE + 1)
1696                                                       * sizeof (_Jv_mcache));
1697   // If the allocation failed, just keep going.
1698   if (method_cache != NULL)
1699     {
1700       int index = method->name->hash16 () & MCACHE_SIZE;
1701       method_cache[index].method = method;
1702       method_cache[index].klass = klass;
1703     }
1704 #endif // HAVE_TLS
1705 }
1706
1707 // Free this thread's method cache.  We explicitly manage this memory
1708 // as the GC does not yet know how to scan TLS on all platforms.
1709 void
1710 _Jv_FreeMethodCache ()
1711 {
1712 #ifdef HAVE_TLS
1713   if (method_cache != NULL)
1714     {
1715       _Jv_Free(method_cache);
1716       method_cache = NULL;
1717     }
1718 #endif // HAVE_TLS
1719 }
1720
1721 void *
1722 _Jv_LookupInterfaceMethod (jclass klass, _Jv_Utf8Const *name,
1723                            _Jv_Utf8Const *signature)
1724 {
1725   using namespace java::lang::reflect;
1726
1727   void *ncode = _Jv_FindMethodInCache (klass, name, signature);
1728   if (ncode != 0)
1729     return ncode;
1730
1731   for (; klass; klass = klass->getSuperclass())
1732     {
1733       _Jv_Method *meth = _Jv_GetMethodLocal (klass, name, signature);
1734       if (! meth)
1735         continue;
1736
1737       if (Modifier::isStatic(meth->accflags))
1738         throw new java::lang::IncompatibleClassChangeError
1739           (_Jv_GetMethodString (klass, meth));
1740       if (Modifier::isAbstract(meth->accflags))
1741         throw new java::lang::AbstractMethodError
1742           (_Jv_GetMethodString (klass, meth));
1743       if (! Modifier::isPublic(meth->accflags))
1744         throw new java::lang::IllegalAccessError
1745           (_Jv_GetMethodString (klass, meth));
1746
1747       _Jv_AddMethodToCache (klass, meth);
1748
1749       return meth->ncode;
1750     }
1751   throw new java::lang::IncompatibleClassChangeError;
1752 }
1753
1754 // Fast interface method lookup by index.
1755 void *
1756 _Jv_LookupInterfaceMethodIdx (jclass klass, jclass iface, int method_idx)
1757 {
1758   _Jv_IDispatchTable *cldt = klass->idt;
1759   int idx = iface->ioffsets[cldt->iindex] + method_idx;
1760   return cldt->itable[idx];
1761 }
1762
1763 jboolean
1764 _Jv_IsAssignableFrom (jclass source, jclass target)
1765 {
1766   if (source == target)
1767     return true;
1768
1769   // If target is array, so must source be.  
1770   while (target->isArray ())
1771     {
1772       if (! source->isArray())
1773         return false;
1774       target = target->getComponentType();
1775       source = source->getComponentType();
1776     }
1777
1778   if (target->isInterface())
1779     {
1780       // Abstract classes have no IDT, and IDTs provide no way to check
1781       // two interfaces for assignability.
1782       if (__builtin_expect 
1783           (source->idt == NULL || source->isInterface(), false))
1784         return _Jv_InterfaceAssignableFrom (source, target);
1785
1786       _Jv_IDispatchTable *cl_idt = source->idt;
1787
1788       if (__builtin_expect ((target->ioffsets == NULL), false))
1789         return false; // No class implementing TARGET has been loaded.    
1790       jshort cl_iindex = cl_idt->iindex;
1791       if (cl_iindex < target->ioffsets[0])
1792         {
1793           jshort offset = target->ioffsets[cl_iindex];
1794           if (offset != -1 && offset < cl_idt->itable_length
1795               && cl_idt->itable[offset] == target)
1796             return true;
1797         }
1798       return false;
1799     }
1800
1801   // Primitive TYPE classes are only assignable to themselves.
1802   if (__builtin_expect (target->isPrimitive() || source->isPrimitive(), false))
1803     return false;
1804
1805   if (target == &java::lang::Object::class$)
1806     return true;
1807   else if (source->ancestors == NULL || target->ancestors == NULL)
1808     {
1809       // We need this case when either SOURCE or TARGET has not has
1810       // its constant-time tables prepared.
1811
1812       // At this point we know that TARGET can't be Object, so it is
1813       // safe to use that as the termination point.
1814       while (source && source != &java::lang::Object::class$)
1815         {
1816           if (source == target)
1817             return true;
1818           source = source->getSuperclass();
1819         }
1820     }
1821   else if (source->depth >= target->depth
1822            && source->ancestors[source->depth - target->depth] == target)
1823     return true;
1824
1825   return false;
1826 }
1827
1828 // Interface type checking, the slow way. Returns TRUE if IFACE is a 
1829 // superinterface of SOURCE. This is used when SOURCE is also an interface,
1830 // or a class with no interface dispatch table.
1831 jboolean
1832 _Jv_InterfaceAssignableFrom (jclass source, jclass iface)
1833 {
1834   for (int i = 0; i < source->interface_count; i++)
1835     {
1836       jclass interface = source->interfaces[i];
1837       if (iface == interface
1838           || _Jv_InterfaceAssignableFrom (interface, iface))
1839         return true;      
1840     }
1841     
1842   if (!source->isInterface()
1843       && source->superclass 
1844       && _Jv_InterfaceAssignableFrom (source->superclass, iface))
1845     return true;
1846         
1847   return false;
1848 }
1849
1850 jboolean
1851 _Jv_IsInstanceOf(jobject obj, jclass cl)
1852 {
1853   if (__builtin_expect (!obj, false))
1854     return false;
1855   return _Jv_IsAssignableFrom (JV_CLASS (obj), cl);
1856 }
1857
1858 void *
1859 _Jv_CheckCast (jclass c, jobject obj)
1860 {
1861   if (__builtin_expect 
1862       (obj != NULL && ! _Jv_IsAssignableFrom(JV_CLASS (obj), c), false))
1863     throw new java::lang::ClassCastException
1864       ((new java::lang::StringBuffer
1865         (obj->getClass()->getName()))->append
1866        (JvNewStringUTF(" cannot be cast to "))->append
1867        (c->getName())->toString());
1868
1869   return obj;
1870 }
1871
1872 void
1873 _Jv_CheckArrayStore (jobject arr, jobject obj)
1874 {
1875   if (obj)
1876     {
1877       JvAssert (arr != NULL);
1878       jclass elt_class = (JV_CLASS (arr))->getComponentType();
1879       if (elt_class == &java::lang::Object::class$)
1880         return;
1881       jclass obj_class = JV_CLASS (obj);
1882       if (__builtin_expect 
1883           (! _Jv_IsAssignableFrom (obj_class, elt_class), false))
1884         throw new java::lang::ArrayStoreException
1885                 ((new java::lang::StringBuffer
1886                  (JvNewStringUTF("Cannot store ")))->append
1887                  (obj_class->getName())->append
1888                  (JvNewStringUTF(" in array of type "))->append
1889                  (elt_class->getName())->toString());
1890     }
1891 }
1892
1893 jboolean
1894 _Jv_IsAssignableFromSlow (jclass source, jclass target)
1895 {
1896   // First, strip arrays.
1897   while (target->isArray ())
1898     {
1899       // If target is array, source must be as well.
1900       if (! source->isArray ())
1901        return false;
1902       target = target->getComponentType ();
1903       source = source->getComponentType ();
1904     }
1905
1906   // Quick success.
1907   if (target == &java::lang::Object::class$)
1908     return true;
1909
1910   // Ensure that the classes have their supers installed.
1911   _Jv_Linker::wait_for_state (source, JV_STATE_LOADING);
1912   _Jv_Linker::wait_for_state (target, JV_STATE_LOADING);
1913
1914   do
1915     {
1916       if (source == target)
1917        return true;
1918
1919       if (target->isPrimitive () || source->isPrimitive ())
1920        return false;
1921
1922       if (target->isInterface ())
1923        {
1924          for (int i = 0; i < source->interface_count; ++i)
1925            {
1926              // We use a recursive call because we also need to
1927              // check superinterfaces.
1928              if (_Jv_IsAssignableFromSlow (source->getInterface (i), target))
1929                return true;
1930            }
1931        }
1932       source = source->getSuperclass ();
1933     }
1934   while (source != NULL);
1935
1936   return false;
1937 }
1938
1939 // Lookup an interface method by name.  This is very similar to
1940 // purpose to _getMethod, but the interfaces are quite different.  It
1941 // might be a good idea for _getMethod to call this function.
1942 //
1943 // Return true of the method is found, with the class in FOUND_CLASS
1944 // and the index in INDEX.
1945 bool
1946 _Jv_getInterfaceMethod (jclass search_class, jclass &found_class, int &index,
1947                         const _Jv_Utf8Const *utf_name,  
1948                         const _Jv_Utf8Const *utf_sig)
1949 {
1950    for (jclass klass = search_class; klass; klass = klass->getSuperclass())
1951     {
1952       // FIXME: Throw an exception?
1953       if (!klass->isInterface ())
1954         return false;
1955       
1956       int max = klass->method_count;
1957       int offset = 0;
1958       for (int i = 0; i < max; ++i)
1959         {
1960           // Skip <clinit> here, as it will not be in the IDT.
1961           if (klass->methods[i].name->first() == '<')
1962             continue;
1963
1964           if (_Jv_equalUtf8Consts (klass->methods[i].name, utf_name)
1965               && _Jv_equalUtf8Consts (klass->methods[i].signature, utf_sig))
1966             {
1967               // Found it.
1968               using namespace java::lang::reflect;
1969
1970               // FIXME: Method must be public.  Throw an exception?
1971               if (! Modifier::isPublic (klass->methods[i].accflags))
1972                 break;
1973
1974               found_class = klass;
1975               // Interface method indexes count from 1.
1976               index = offset + 1;
1977               return true;
1978             }
1979
1980           ++offset;
1981         }
1982     }
1983
1984   // If we haven't found a match, and this class is an interface, then
1985   // check all the superinterfaces.
1986   if (search_class->isInterface())
1987     {
1988       for (int i = 0; i < search_class->interface_count; ++i)
1989         {
1990           using namespace java::lang::reflect;
1991           bool found = _Jv_getInterfaceMethod (search_class->interfaces[i], 
1992                                                found_class, index,
1993                                                utf_name, utf_sig);
1994           if (found)
1995             return true;
1996         }
1997     }
1998
1999   return false;
2000 }
2001
2002 #ifdef INTERPRETER
2003 _Jv_MethodBase *
2004 _Jv_FindInterpreterMethod (jclass klass, jmethodID desired_method)
2005 {
2006   using namespace java::lang::reflect;
2007
2008   _Jv_InterpClass *iclass
2009     = reinterpret_cast<_Jv_InterpClass *> (klass->aux_info);
2010   _Jv_MethodBase **imethods = _Jv_GetFirstMethod (iclass);
2011
2012   for (int i = 0; i < JvNumMethods (klass); ++i)
2013     {
2014       _Jv_MethodBase *imeth = imethods[i];
2015       if (imeth->get_method () == desired_method)
2016         return imeth;
2017     }
2018
2019   return NULL;
2020 }
2021 #endif
2022
2023 // Return Utf8 name of a class. This function is here for code that
2024 // can't access klass->name directly.
2025 _Jv_Utf8Const*
2026 _Jv_GetClassNameUtf8 (jclass klass)
2027 {
2028   return klass->name;
2029 }
2030
2031 jclass
2032 _Jv_GetMethodDeclaringClass (jmethodID method)
2033 {
2034   _Jv_StackTrace::UpdateNCodeMap ();
2035   jobject obj = reinterpret_cast<jobject> (method->ncode);
2036   return reinterpret_cast<jclass> (_Jv_StackTrace::ncodeMap->get (obj));
2037 }
2038
2039 jbyte
2040 _Jv_GetClassState (jclass klass)
2041 {
2042   return klass->state;
2043 }
2044