* src/mm/memory.cpp,
[cacao.git] / src / vm / class.cpp
1 /* src/vm/class.cpp - class related functions
2
3    Copyright (C) 1996-2005, 2006, 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <assert.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include "vm/types.h"
34
35 #include "arch.h"
36
37 #include "mm/memory.hpp"
38
39 #include "native/llni.h"
40
41 #include "threads/lock.hpp"
42 #include "threads/mutex.hpp"
43
44 #include "toolbox/logging.hpp"
45
46 #include "vm/array.hpp"
47 #include "vm/jit/builtin.hpp"
48 #include "vm/class.hpp"
49 #include "vm/classcache.hpp"
50 #include "vm/exceptions.hpp"
51 #include "vm/global.h"
52 #include "vm/globals.hpp"
53 #include "vm/javaobjects.hpp"
54 #include "vm/linker.hpp"
55 #include "vm/loader.hpp"
56 #include "vm/options.h"
57 #include "vm/resolve.hpp"
58
59 #if defined(ENABLE_STATISTICS)
60 # include "vm/statistics.h"
61 #endif
62
63 #include "vm/suck.hpp"
64 #include "vm/utf8.h"
65
66 #include "vm/jit/asmpart.h"
67
68
69 /* class_set_packagename *******************************************************
70
71    Derive the package name from the class name and store it in the
72    struct.
73
74    An internal package name consists of the package name plus the
75    trailing '/', e.g. "java/lang/".
76
77    For classes in the unnamed package, the package name is set to
78    NULL.
79
80 *******************************************************************************/
81
82 void class_set_packagename(classinfo *c)
83 {
84         char *p;
85         char *start;
86
87         p     = UTF_END(c->name) - 1;
88         start = c->name->text;
89
90         if (c->name->text[0] == '[') {
91                 /* Set packagename of arrays to the element's package. */
92
93                 for (; *start == '['; start++);
94
95                 /* Skip the 'L' in arrays of references. */
96
97                 if (*start == 'L')
98                         start++;
99         }
100
101         /* Search for last '/'. */
102
103         for (; (p > start) && (*p != '/'); --p);
104
105         /* If we found a '/' we set the package name plus the trailing
106            '/'.  Otherwise we set the packagename to NULL. */
107
108         if (p > start)
109                 c->packagename = utf_new(start, p - start + 1);
110         else
111                 c->packagename = NULL;
112 }
113
114
115 /* class_create_classinfo ******************************************************
116
117    Create a new classinfo struct. The class name is set to the given utf *,
118    most other fields are initialized to zero.
119
120    Note: classname may be NULL. In this case a not-yet-named classinfo is
121          created. The name must be filled in later and class_set_packagename
122                  must be called after that.
123
124 *******************************************************************************/
125
126 classinfo *class_create_classinfo(utf *classname)
127 {
128         classinfo *c;
129
130 #if defined(ENABLE_STATISTICS)
131         if (opt_stat)
132                 size_classinfo += sizeof(classinfo);
133 #endif
134
135         /* we use a safe name for temporarily unnamed classes */
136
137         if (classname == NULL)
138                 classname = utf_not_named_yet;
139
140 #if !defined(NDEBUG)
141         if (initverbose)
142                 log_message_utf("Creating class: ", classname);
143 #endif
144
145 #if !defined(ENABLE_GC_BOEHM)
146         c = (classinfo *) heap_alloc_uncollectable(sizeof(classinfo));
147         /*c = NEW(classinfo);
148         MZERO(c, classinfo, 1);*/
149 #else
150         c = GCNEW_UNCOLLECTABLE(classinfo, 1);
151         /* GCNEW_UNCOLLECTABLE clears the allocated memory */
152 #endif
153
154         c->name = classname;
155
156         /* Set the header.vftbl of all loaded classes to the one of
157        java.lang.Class, so Java code can use a class as object. */
158
159         if (class_java_lang_Class != NULL)
160                 if (class_java_lang_Class->vftbl != NULL)
161                         c->object.header.vftbl = class_java_lang_Class->vftbl;
162
163 #if defined(ENABLE_JAVASE)
164         /* check if the class is a reference class and flag it */
165
166         if (classname == utf_java_lang_ref_SoftReference) {
167                 c->flags |= ACC_CLASS_REFERENCE_SOFT;
168         }
169         else if (classname == utf_java_lang_ref_WeakReference) {
170                 c->flags |= ACC_CLASS_REFERENCE_WEAK;
171         }
172         else if (classname == utf_java_lang_ref_PhantomReference) {
173                 c->flags |= ACC_CLASS_REFERENCE_PHANTOM;
174         }
175 #endif
176
177         if (classname != utf_not_named_yet)
178                 class_set_packagename(c);
179
180         c->object.header.lockword.init();
181
182         return c;
183 }
184
185
186 /* class_postset_header_vftbl **************************************************
187
188    Set the header.vftbl of all classes created before java.lang.Class
189    was linked.  This is necessary that Java code can use a class as
190    object.
191
192 *******************************************************************************/
193
194 void class_postset_header_vftbl(void)
195 {
196         classinfo *c;
197         u4 slot;
198         classcache_name_entry *nmen;
199         classcache_class_entry *clsen;
200
201         assert(class_java_lang_Class);
202
203         for (slot = 0; slot < hashtable_classcache.size; slot++) {
204                 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
205
206                 for (; nmen; nmen = nmen->hashlink) {
207                         /* iterate over all class entries */
208
209                         for (clsen = nmen->classes; clsen; clsen = clsen->next) {
210                                 c = clsen->classobj;
211
212                                 /* now set the the vftbl */
213
214                                 if (c->object.header.vftbl == NULL)
215                                         c->object.header.vftbl = class_java_lang_Class->vftbl;
216                         }
217                 }
218         }
219 }
220
221 /* class_define ****************************************************************
222
223    Calls the loader and defines a class in the VM.
224
225 *******************************************************************************/
226
227 classinfo *class_define(utf *name, classloader_t *cl, int32_t length, uint8_t *data, java_handle_t *pd)
228 {
229         classinfo   *c;
230         classinfo   *r;
231         classbuffer *cb;
232
233         if (name != NULL) {
234                 /* check if this class has already been defined */
235
236                 c = classcache_lookup_defined_or_initiated(cl, name);
237
238                 if (c != NULL) {
239                         exceptions_throw_linkageerror("duplicate class definition: ", c);
240                         return NULL;
241                 }
242         } 
243
244         /* create a new classinfo struct */
245
246         c = class_create_classinfo(name);
247
248 #if defined(ENABLE_STATISTICS)
249         /* measure time */
250
251         if (opt_getloadingtime)
252                 loadingtime_start();
253 #endif
254
255         /* build a classbuffer with the given data */
256
257         cb = NEW(classbuffer);
258
259         cb->clazz = c;
260         cb->size  = length;
261         cb->data  = data;
262         cb->pos   = cb->data;
263
264         /* preset the defining classloader */
265
266         c->classloader = cl;
267
268         /* load the class from this buffer */
269
270         r = load_class_from_classbuffer(cb);
271
272         /* free memory */
273
274         FREE(cb, classbuffer);
275
276 #if defined(ENABLE_STATISTICS)
277         /* measure time */
278
279         if (opt_getloadingtime)
280                 loadingtime_stop();
281 #endif
282
283         if (r == NULL) {
284                 /* If return value is NULL, we had a problem and the class is
285                    not loaded.  Now free the allocated memory, otherwise we
286                    could run into a DOS. */
287
288                 class_free(c);
289
290                 return NULL;
291         }
292
293 #if defined(ENABLE_JAVASE)
294 # if defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
295         /* Store the protection domain. */
296
297         c->protectiondomain = pd;
298 # endif
299 #endif
300
301         /* Store the newly defined class in the class cache. This call
302            also checks whether a class of the same name has already been
303            defined by the same defining loader, and if so, replaces the
304            newly created class by the one defined earlier. */
305
306         /* Important: The classinfo given to classcache_store must be
307                       fully prepared because another thread may return
308                       this pointer after the lookup at to top of this
309                       function directly after the class cache lock has
310                       been released. */
311
312         c = classcache_store(cl, c, true);
313
314         return c;
315 }
316
317
318 /* class_load_attribute_sourcefile *********************************************
319
320    SourceFile_attribute {
321        u2 attribute_name_index;
322        u4 attribute_length;
323            u2 sourcefile_index;
324    }
325
326 *******************************************************************************/
327
328 static bool class_load_attribute_sourcefile(classbuffer *cb)
329 {
330         classinfo *c;
331         u4         attribute_length;
332         u2         sourcefile_index;
333         utf       *sourcefile;
334
335         /* get classinfo */
336
337         c = cb->clazz;
338
339         /* check buffer size */
340
341         if (!suck_check_classbuffer_size(cb, 4 + 2))
342                 return false;
343
344         /* check attribute length */
345
346         attribute_length = suck_u4(cb);
347
348         if (attribute_length != 2) {
349                 exceptions_throw_classformaterror(c, "Wrong size for VALUE attribute");
350                 return false;
351         }
352
353         /* there can be no more than one SourceFile attribute */
354
355         if (c->sourcefile != NULL) {
356                 exceptions_throw_classformaterror(c, "Multiple SourceFile attributes");
357                 return false;
358         }
359
360         /* get sourcefile */
361
362         sourcefile_index = suck_u2(cb);
363         sourcefile = (utf*) class_getconstant(c, sourcefile_index, CONSTANT_Utf8);
364
365         if (sourcefile == NULL)
366                 return false;
367
368         /* store sourcefile */
369
370         c->sourcefile = sourcefile;
371
372         return true;
373 }
374
375
376 /* class_load_attribute_enclosingmethod ****************************************
377
378    EnclosingMethod_attribute {
379        u2 attribute_name_index;
380        u4 attribute_length;
381            u2 class_index;
382            u2 method_index;
383    }
384
385 *******************************************************************************/
386
387 #if defined(ENABLE_JAVASE)
388 static bool class_load_attribute_enclosingmethod(classbuffer *cb)
389 {
390         classinfo             *c;
391         u4                     attribute_length;
392         u2                     class_index;
393         u2                     method_index;
394         classref_or_classinfo  cr;
395         constant_nameandtype  *cn;
396
397         /* get classinfo */
398
399         c = cb->clazz;
400
401         /* check buffer size */
402
403         if (!suck_check_classbuffer_size(cb, 4 + 2 + 2))
404                 return false;
405
406         /* check attribute length */
407
408         attribute_length = suck_u4(cb);
409
410         if (attribute_length != 4) {
411                 exceptions_throw_classformaterror(c, "Wrong size for VALUE attribute");
412                 return false;
413         }
414
415         /* there can be no more than one EnclosingMethod attribute */
416
417         if (c->enclosingmethod != NULL) {
418                 exceptions_throw_classformaterror(c, "Multiple EnclosingMethod attributes");
419                 return false;
420         }
421
422         /* get class index */
423
424         class_index = suck_u2(cb);
425         cr.ref = (constant_classref*) innerclass_getconstant(c, class_index, CONSTANT_Class);
426
427         /* get method index */
428
429         method_index = suck_u2(cb);
430         cn = (constant_nameandtype*) innerclass_getconstant(c, method_index, CONSTANT_NameAndType);
431
432         /* store info in classinfo */
433
434         c->enclosingclass.any = cr.any;
435         c->enclosingmethod    = cn;
436
437         return true;
438 }
439 #endif /* defined(ENABLE_JAVASE) */
440
441
442 /* class_load_attributes *******************************************************
443
444    Read attributes from ClassFile.
445
446    attribute_info {
447        u2 attribute_name_index;
448        u4 attribute_length;
449        u1 info[attribute_length];
450    }
451
452    InnerClasses_attribute {
453        u2 attribute_name_index;
454        u4 attribute_length;
455    }
456
457 *******************************************************************************/
458
459 bool class_load_attributes(classbuffer *cb)
460 {
461         classinfo             *c;
462         uint16_t               attributes_count;
463         uint16_t               attribute_name_index;
464         utf                   *attribute_name;
465         innerclassinfo        *info;
466         classref_or_classinfo  inner;
467         classref_or_classinfo  outer;
468         utf                   *name;
469         uint16_t               flags;
470         int                    i, j;
471
472         c = cb->clazz;
473
474         /* get attributes count */
475
476         if (!suck_check_classbuffer_size(cb, 2))
477                 return false;
478
479         attributes_count = suck_u2(cb);
480
481         for (i = 0; i < attributes_count; i++) {
482                 /* get attribute name */
483
484                 if (!suck_check_classbuffer_size(cb, 2))
485                         return false;
486
487                 attribute_name_index = suck_u2(cb);
488                 attribute_name =
489                         (utf*) class_getconstant(c, attribute_name_index, CONSTANT_Utf8);
490
491                 if (attribute_name == NULL)
492                         return false;
493
494                 if (attribute_name == utf_InnerClasses) {
495                         /* InnerClasses */
496
497                         if (c->innerclass != NULL) {
498                                 exceptions_throw_classformaterror(c, "Multiple InnerClasses attributes");
499                                 return false;
500                         }
501                                 
502                         if (!suck_check_classbuffer_size(cb, 4 + 2))
503                                 return false;
504
505                         /* skip attribute length */
506                         suck_u4(cb);
507
508                         /* number of records */
509                         c->innerclasscount = suck_u2(cb);
510
511                         if (!suck_check_classbuffer_size(cb, (2 + 2 + 2 + 2) * c->innerclasscount))
512                                 return false;
513
514                         /* allocate memory for innerclass structure */
515                         c->innerclass = MNEW(innerclassinfo, c->innerclasscount);
516
517                         for (j = 0; j < c->innerclasscount; j++) {
518                                 /* The innerclass structure contains a class with an encoded
519                                    name, its defining scope, its simple name and a bitmask of
520                                    the access flags. */
521                                                                 
522                                 info = c->innerclass + j;
523
524                                 inner.ref = (constant_classref*) innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
525                                 outer.ref = (constant_classref*) innerclass_getconstant(c, suck_u2(cb), CONSTANT_Class);
526                                 name      = (utf*) innerclass_getconstant(c, suck_u2(cb), CONSTANT_Utf8);
527                                 flags     = suck_u2(cb);
528
529                                 /* If the current inner-class is the currently loaded
530                                    class check for some special flags. */
531
532                                 if (inner.ref->name == c->name) {
533                                         /* If an inner-class is not a member, its
534                                            outer-class is NULL. */
535
536                                         if (outer.ref != NULL) {
537                                                 c->flags |= ACC_CLASS_MEMBER;
538
539                                                 /* A member class doesn't have an
540                                                    EnclosingMethod attribute, so set the
541                                                    enclosing-class to be the same as the
542                                                    declaring-class. */
543
544                                                 c->declaringclass = outer;
545                                                 c->enclosingclass = outer;
546                                         }
547
548                                         /* If an inner-class is anonymous, its name is
549                                            NULL. */
550
551                                         if (name == NULL)
552                                                 c->flags |= ACC_CLASS_ANONYMOUS;
553                                 }
554
555                                 info->inner_class = inner;
556                                 info->outer_class = outer;
557                                 info->name        = name;
558                                 info->flags       = flags;
559                         }
560                 }
561                 else if (attribute_name == utf_SourceFile) {
562                         /* SourceFile */
563
564                         if (!class_load_attribute_sourcefile(cb))
565                                 return false;
566                 }
567 #if defined(ENABLE_JAVASE)
568                 else if (attribute_name == utf_EnclosingMethod) {
569                         /* EnclosingMethod */
570
571                         if (!class_load_attribute_enclosingmethod(cb))
572                                 return false;
573                 }
574                 else if (attribute_name == utf_Signature) {
575                         /* Signature */
576
577                         if (!loader_load_attribute_signature(cb, &(c->signature)))
578                                 return false;
579                 }
580 #endif
581
582 #if defined(ENABLE_ANNOTATIONS)
583                 else if (attribute_name == utf_RuntimeVisibleAnnotations) {
584                         /* RuntimeVisibleAnnotations */
585                         if (!annotation_load_class_attribute_runtimevisibleannotations(cb))
586                                 return false;
587                 }
588                 else if (attribute_name == utf_RuntimeInvisibleAnnotations) {
589                         /* RuntimeInvisibleAnnotations */
590                         if (!annotation_load_class_attribute_runtimeinvisibleannotations(cb))
591                                 return false;
592                 }
593 #endif
594
595                 else {
596                         /* unknown attribute */
597
598                         if (!loader_skip_attribute_body(cb))
599                                 return false;
600                 }
601         }
602
603         return true;
604 }
605
606
607 /* class_freepool **************************************************************
608
609         Frees all resources used by this classes Constant Pool.
610
611 *******************************************************************************/
612
613 static void class_freecpool(classinfo *c)
614 {
615         u4 idx;
616         u4 tag;
617         void* info;
618         
619         if (c->cptags && c->cpinfos) {
620                 for (idx = 0; idx < c->cpcount; idx++) {
621                         tag = c->cptags[idx];
622                         info = c->cpinfos[idx];
623                 
624                         if (info != NULL) {
625                                 switch (tag) {
626                                 case CONSTANT_Fieldref:
627                                 case CONSTANT_Methodref:
628                                 case CONSTANT_InterfaceMethodref:
629                                         FREE(info, constant_FMIref);
630                                         break;
631                                 case CONSTANT_Integer:
632                                         FREE(info, constant_integer);
633                                         break;
634                                 case CONSTANT_Float:
635                                         FREE(info, constant_float);
636                                         break;
637                                 case CONSTANT_Long:
638                                         FREE(info, constant_long);
639                                         break;
640                                 case CONSTANT_Double:
641                                         FREE(info, constant_double);
642                                         break;
643                                 case CONSTANT_NameAndType:
644                                         FREE(info, constant_nameandtype);
645                                         break;
646                                 }
647                         }
648                 }
649         }
650
651         if (c->cptags)
652                 MFREE(c->cptags, u1, c->cpcount);
653
654         if (c->cpinfos)
655                 MFREE(c->cpinfos, void*, c->cpcount);
656 }
657
658
659 /* class_getconstant ***********************************************************
660
661    Retrieves the value at position 'pos' of the constantpool of a
662    class. If the type of the value is other than 'ctype', an error is
663    thrown.
664
665 *******************************************************************************/
666
667 void* class_getconstant(classinfo *c, u4 pos, u4 ctype)
668 {
669         /* check index and type of constantpool entry */
670         /* (pos == 0 is caught by type comparison) */
671
672         if ((pos >= c->cpcount) || (c->cptags[pos] != ctype)) {
673                 exceptions_throw_classformaterror(c, "Illegal constant pool index");
674                 return NULL;
675         }
676
677         return c->cpinfos[pos];
678 }
679
680
681 /* innerclass_getconstant ******************************************************
682
683    Like class_getconstant, but if cptags is ZERO, null is returned.
684         
685 *******************************************************************************/
686
687 void* innerclass_getconstant(classinfo *c, u4 pos, u4 ctype)
688 {
689         /* invalid position in constantpool */
690
691         if (pos >= c->cpcount) {
692                 exceptions_throw_classformaterror(c, "Illegal constant pool index");
693                 return NULL;
694         }
695
696         /* constantpool entry of type 0 */      
697
698         if (c->cptags[pos] == 0)
699                 return NULL;
700
701         /* check type of constantpool entry */
702
703         if (c->cptags[pos] != ctype) {
704                 exceptions_throw_classformaterror(c, "Illegal constant pool index");
705                 return NULL;
706         }
707                 
708         return c->cpinfos[pos];
709 }
710
711
712 /* class_free ******************************************************************
713
714    Frees all resources used by the class.
715
716 *******************************************************************************/
717
718 void class_free(classinfo *c)
719 {
720         s4 i;
721         vftbl_t *v;
722
723         class_freecpool(c);
724
725         if (c->interfaces != NULL)
726                 MFREE(c->interfaces, classinfo*, c->interfacescount);
727
728         if (c->fields) {
729                 for (i = 0; i < c->fieldscount; i++)
730                         field_free(&(c->fields[i]));
731                 MFREE(c->fields, fieldinfo, c->fieldscount);
732         }
733         
734         if (c->methods) {
735                 for (i = 0; i < c->methodscount; i++)
736                         method_free(&(c->methods[i]));
737                 MFREE(c->methods, methodinfo, c->methodscount);
738         }
739
740         if ((v = c->vftbl) != NULL) {
741                 if (v->arraydesc)
742                         mem_free(v->arraydesc,sizeof(arraydescriptor));
743                 
744                 for (i = 0; i < v->interfacetablelength; i++) {
745                         MFREE(v->interfacetable[-i], methodptr, v->interfacevftbllength[i]);
746                 }
747                 MFREE(v->interfacevftbllength, s4, v->interfacetablelength);
748
749                 i = sizeof(vftbl_t) + sizeof(methodptr) * (v->vftbllength - 1) +
750                     sizeof(methodptr*) * (v->interfacetablelength -
751                                          (v->interfacetablelength > 0));
752                 v = (vftbl_t*) (((methodptr*) v) -
753                                                 (v->interfacetablelength - 1) * (v->interfacetablelength > 1));
754                 mem_free(v, i);
755         }
756
757         if (c->innerclass)
758                 MFREE(c->innerclass, innerclassinfo, c->innerclasscount);
759
760         /*      if (c->classvftbl)
761                 mem_free(c->header.vftbl, sizeof(vftbl) + sizeof(methodptr)*(c->vftbl->vftbllength-1)); */
762         
763 /*      GCFREE(c); */
764 }
765
766
767 /* get_array_class *************************************************************
768
769    Returns the array class with the given name for the given
770    classloader, or NULL if an exception occurred.
771
772    Note: This function does eager loading. 
773
774 *******************************************************************************/
775
776 static classinfo *get_array_class(utf *name,classloader_t *initloader,
777                                                                                         classloader_t *defloader,bool link)
778 {
779         classinfo *c;
780         
781         /* lookup this class in the classcache */
782         c = classcache_lookup(initloader,name);
783         if (!c)
784                 c = classcache_lookup_defined(defloader,name);
785
786         if (!c) {
787                 /* we have to create it */
788                 c = class_create_classinfo(name);
789                 c = load_newly_created_array(c,initloader);
790                 if (c == NULL)
791                         return NULL;
792         }
793
794         assert(c);
795         assert(c->state & CLASS_LOADED);
796         assert(c->classloader == defloader);
797
798         if (link && !(c->state & CLASS_LINKED))
799                 if (!link_class(c))
800                         return NULL;
801
802         assert(!link || (c->state & CLASS_LINKED));
803
804         return c;
805 }
806
807
808 /* class_array_of **************************************************************
809
810    Returns an array class with the given component class. The array
811    class is dynamically created if neccessary.
812
813 *******************************************************************************/
814
815 classinfo *class_array_of(classinfo *component, bool link)
816 {
817         classloader_t     *cl;
818     s4                 namelen;
819     char              *namebuf;
820         utf               *u;
821         classinfo         *c;
822
823         cl = component->classloader;
824
825     /* Assemble the array class name */
826     namelen = component->name->blength;
827     
828     if (component->name->text[0] == '[') {
829         /* the component is itself an array */
830         namebuf = MNEW(char, namelen + 1);
831         namebuf[0] = '[';
832         MCOPY(namebuf + 1, component->name->text, char, namelen);
833         namelen++;
834     }
835         else {
836         /* the component is a non-array class */
837         namebuf = MNEW(char, namelen + 3);
838         namebuf[0] = '[';
839         namebuf[1] = 'L';
840         MCOPY(namebuf + 2, component->name->text, char, namelen);
841         namebuf[2 + namelen] = ';';
842         namelen += 3;
843     }
844
845         u = utf_new(namebuf, namelen);
846
847         MFREE(namebuf, char, namelen);
848
849         c = get_array_class(u, cl, cl, link);
850
851         return c;
852 }
853
854
855 /* class_multiarray_of *********************************************************
856
857    Returns an array class with the given dimension and element class.
858    The array class is dynamically created if neccessary.
859
860 *******************************************************************************/
861
862 classinfo *class_multiarray_of(s4 dim, classinfo *element, bool link)
863 {
864     s4 namelen;
865     char *namebuf;
866         classinfo *c;
867
868         if (dim < 1) {
869                 log_text("Invalid array dimension requested");
870                 assert(0);
871         }
872
873     /* Assemble the array class name */
874     namelen = element->name->blength;
875     
876     if (element->name->text[0] == '[') {
877         /* the element is itself an array */
878         namebuf = MNEW(char, namelen + dim);
879         memcpy(namebuf + dim, element->name->text, namelen);
880         namelen += dim;
881     }
882     else {
883         /* the element is a non-array class */
884         namebuf = MNEW(char, namelen + 2 + dim);
885         namebuf[dim] = 'L';
886         memcpy(namebuf + dim + 1, element->name->text, namelen);
887         namelen += (2 + dim);
888         namebuf[namelen - 1] = ';';
889     }
890         memset(namebuf, '[', dim);
891
892         utf* u = utf_new(namebuf, namelen);
893
894         MFREE(namebuf, char, namelen);
895
896         c = get_array_class(u,
897                                                 element->classloader,
898                                                 element->classloader,
899                                                 link);
900
901         return c;
902 }
903
904
905 /* class_lookup_classref *******************************************************
906
907    Looks up the constant_classref for a given classname in the classref
908    tables of a class.
909
910    IN:
911        cls..............the class containing the reference
912            name.............the name of the class refered to
913
914     RETURN VALUE:
915            a pointer to a constant_classref, or 
916            NULL if the reference was not found
917    
918 *******************************************************************************/
919
920 constant_classref *class_lookup_classref(classinfo *cls, utf *name)
921 {
922         constant_classref *ref;
923         extra_classref *xref;
924         int count;
925
926         assert(cls);
927         assert(name);
928         assert(!cls->classrefcount || cls->classrefs);
929         
930         /* first search the main classref table */
931         count = cls->classrefcount;
932         ref = cls->classrefs;
933         for (; count; --count, ++ref)
934                 if (ref->name == name)
935                         return ref;
936
937         /* next try the list of extra classrefs */
938         for (xref = cls->extclassrefs; xref; xref = xref->next) {
939                 if (xref->classref.name == name)
940                         return &(xref->classref);
941         }
942
943         /* not found */
944         return NULL;
945 }
946
947
948 /* class_get_classref **********************************************************
949
950    Returns the constant_classref for a given classname.
951
952    IN:
953        cls..............the class containing the reference
954            name.............the name of the class refered to
955
956    RETURN VALUE:
957        a pointer to a constant_classref (never NULL)
958
959    NOTE:
960        The given name is not checked for validity!
961    
962 *******************************************************************************/
963
964 constant_classref *class_get_classref(classinfo *cls, utf *name)
965 {
966         constant_classref *ref;
967         extra_classref *xref;
968
969         assert(cls);
970         assert(name);
971
972         ref = class_lookup_classref(cls,name);
973         if (ref)
974                 return ref;
975
976         xref = NEW(extra_classref);
977         CLASSREF_INIT(xref->classref,cls,name);
978
979         xref->next = cls->extclassrefs;
980         cls->extclassrefs = xref;
981
982         return &(xref->classref);
983 }
984
985
986 /* class_get_self_classref *****************************************************
987
988    Returns the constant_classref to the class itself.
989
990    IN:
991        cls..............the class containing the reference
992
993    RETURN VALUE:
994        a pointer to a constant_classref (never NULL)
995
996 *******************************************************************************/
997
998 constant_classref *class_get_self_classref(classinfo *cls)
999 {
1000         /* XXX this should be done in a faster way. Maybe always make */
1001         /* the classref of index 0 a self reference.                  */
1002         return class_get_classref(cls,cls->name);
1003 }
1004
1005 /* class_get_classref_multiarray_of ********************************************
1006
1007    Returns an array type reference with the given dimension and element class
1008    reference.
1009
1010    IN:
1011        dim..............the requested dimension
1012                             dim must be in [1;255]. This is NOT checked!
1013            ref..............the component class reference
1014
1015    RETURN VALUE:
1016        a pointer to the class reference for the array type
1017
1018    NOTE:
1019        The referer of `ref` is used as the referer for the new classref.
1020
1021 *******************************************************************************/
1022
1023 constant_classref *class_get_classref_multiarray_of(s4 dim, constant_classref *ref)
1024 {
1025     s4 namelen;
1026     char *namebuf;
1027         constant_classref *cr;
1028
1029         assert(ref);
1030         assert(dim >= 1 && dim <= 255);
1031
1032     /* Assemble the array class name */
1033     namelen = ref->name->blength;
1034     
1035     if (ref->name->text[0] == '[') {
1036         /* the element is itself an array */
1037         namebuf = MNEW(char, namelen + dim);
1038         memcpy(namebuf + dim, ref->name->text, namelen);
1039         namelen += dim;
1040     }
1041     else {
1042         /* the element is a non-array class */
1043         namebuf = MNEW(char, namelen + 2 + dim);
1044         namebuf[dim] = 'L';
1045         memcpy(namebuf + dim + 1, ref->name->text, namelen);
1046         namelen += (2 + dim);
1047         namebuf[namelen - 1] = ';';
1048     }
1049         memset(namebuf, '[', dim);
1050
1051         utf* u = utf_new(namebuf, namelen);
1052
1053         MFREE(namebuf, char, namelen);
1054
1055     cr = class_get_classref(ref->referer, u);
1056
1057         return cr;
1058 }
1059
1060
1061 /* class_get_classref_component_of *********************************************
1062
1063    Returns the component classref of a given array type reference
1064
1065    IN:
1066        ref..............the array type reference
1067
1068    RETURN VALUE:
1069        a reference to the component class, or
1070            NULL if `ref` is not an object array type reference
1071
1072    NOTE:
1073        The referer of `ref` is used as the referer for the new classref.
1074
1075 *******************************************************************************/
1076
1077 constant_classref *class_get_classref_component_of(constant_classref *ref)
1078 {
1079         s4 namelen;
1080         char *name;
1081         
1082         assert(ref);
1083
1084         name = ref->name->text;
1085         if (*name++ != '[')
1086                 return NULL;
1087         
1088         namelen = ref->name->blength - 1;
1089         if (*name == 'L') {
1090                 name++;
1091                 namelen -= 2;
1092         }
1093         else if (*name != '[') {
1094                 return NULL;
1095         }
1096
1097     return class_get_classref(ref->referer, utf_new(name, namelen));
1098 }
1099
1100
1101 /* class_findmethod ************************************************************
1102         
1103    Searches a 'classinfo' structure for a method having the given name
1104    and descriptor. If descriptor is NULL, it is ignored.
1105
1106 *******************************************************************************/
1107
1108 methodinfo *class_findmethod(classinfo *c, utf *name, utf *desc)
1109 {
1110         methodinfo *m;
1111         s4          i;
1112
1113         for (i = 0; i < c->methodscount; i++) {
1114                 m = &(c->methods[i]);
1115
1116                 if ((m->name == name) && ((desc == NULL) || (m->descriptor == desc)))
1117                         return m;
1118         }
1119
1120         return NULL;
1121 }
1122
1123
1124 /* class_resolvemethod *********************************************************
1125         
1126    Searches a class and it's super classes for a method.
1127
1128    Superinterfaces are *not* searched.
1129
1130 *******************************************************************************/
1131
1132 methodinfo *class_resolvemethod(classinfo *c, utf *name, utf *desc)
1133 {
1134         methodinfo *m;
1135
1136         while (c) {
1137                 m = class_findmethod(c, name, desc);
1138
1139                 if (m)
1140                         return m;
1141
1142                 /* JVM Specification bug: 
1143
1144                    It is important NOT to resolve special <init> and <clinit>
1145                    methods to super classes or interfaces; yet, this is not
1146                    explicited in the specification.  Section 5.4.3.3 should be
1147                    updated appropriately.  */
1148
1149                 if (name == utf_init || name == utf_clinit)
1150                         return NULL;
1151
1152                 c = c->super;
1153         }
1154
1155         return NULL;
1156 }
1157
1158
1159 /* class_resolveinterfacemethod_intern *****************************************
1160
1161    Internally used helper function. Do not use this directly.
1162
1163 *******************************************************************************/
1164
1165 static methodinfo *class_resolveinterfacemethod_intern(classinfo *c,
1166                                                                                                            utf *name, utf *desc)
1167 {
1168         methodinfo *m;
1169         s4          i;
1170
1171         /* try to find the method in the class */
1172
1173         m = class_findmethod(c, name, desc);
1174
1175         if (m != NULL)
1176                 return m;
1177
1178         /* No method found?  Try the super interfaces. */
1179
1180         for (i = 0; i < c->interfacescount; i++) {
1181                 m = class_resolveinterfacemethod_intern(c->interfaces[i], name, desc);
1182
1183                 if (m != NULL)
1184                         return m;
1185         }
1186
1187         /* no method found */
1188
1189         return NULL;
1190 }
1191
1192
1193 /* class_resolveclassmethod ****************************************************
1194         
1195    Resolves a reference from REFERER to a method with NAME and DESC in
1196    class C.
1197
1198    If the method cannot be resolved the return value is NULL. If
1199    EXCEPT is true *exceptionptr is set, too.
1200
1201 *******************************************************************************/
1202
1203 methodinfo *class_resolveclassmethod(classinfo *c, utf *name, utf *desc,
1204                                                                          classinfo *referer, bool throwexception)
1205 {
1206         classinfo  *cls;
1207         methodinfo *m;
1208         s4          i;
1209
1210 /*      if (c->flags & ACC_INTERFACE) { */
1211 /*              if (throwexception) */
1212 /*                      *exceptionptr = */
1213 /*                              new_exception(string_java_lang_IncompatibleClassChangeError); */
1214 /*              return NULL; */
1215 /*      } */
1216
1217         /* try class c and its superclasses */
1218
1219         cls = c;
1220
1221         m = class_resolvemethod(cls, name, desc);
1222
1223         if (m != NULL)
1224                 goto found;
1225
1226         /* Try the super interfaces. */
1227
1228         for (i = 0; i < c->interfacescount; i++) {
1229                 m = class_resolveinterfacemethod_intern(c->interfaces[i], name, desc);
1230
1231                 if (m != NULL)
1232                         goto found;
1233         }
1234         
1235         if (throwexception)
1236                 exceptions_throw_nosuchmethoderror(c, name, desc);
1237
1238         return NULL;
1239
1240  found:
1241         if ((m->flags & ACC_ABSTRACT) && !(c->flags & ACC_ABSTRACT)) {
1242                 if (throwexception)
1243                         exceptions_throw_abstractmethoderror();
1244
1245                 return NULL;
1246         }
1247
1248         /* XXX check access rights */
1249
1250         return m;
1251 }
1252
1253
1254 /* class_resolveinterfacemethod ************************************************
1255
1256    Resolves a reference from REFERER to a method with NAME and DESC in
1257    interface C.
1258
1259    If the method cannot be resolved the return value is NULL. If
1260    EXCEPT is true *exceptionptr is set, too.
1261
1262 *******************************************************************************/
1263
1264 methodinfo *class_resolveinterfacemethod(classinfo *c, utf *name, utf *desc,
1265                                                                                  classinfo *referer, bool throwexception)
1266 {
1267         methodinfo *mi;
1268
1269         if (!(c->flags & ACC_INTERFACE)) {
1270                 if (throwexception)
1271                         exceptions_throw_incompatibleclasschangeerror(c, "Not an interface");
1272
1273                 return NULL;
1274         }
1275
1276         mi = class_resolveinterfacemethod_intern(c, name, desc);
1277
1278         if (mi != NULL)
1279                 return mi;
1280
1281         /* try class java.lang.Object */
1282
1283         mi = class_findmethod(class_java_lang_Object, name, desc);
1284
1285         if (mi != NULL)
1286                 return mi;
1287
1288         if (throwexception)
1289                 exceptions_throw_nosuchmethoderror(c, name, desc);
1290
1291         return NULL;
1292 }
1293
1294
1295 /* class_findfield *************************************************************
1296         
1297    Searches for field with specified name and type in a classinfo
1298    structure. If no such field is found NULL is returned.
1299
1300 *******************************************************************************/
1301
1302 fieldinfo *class_findfield(classinfo *c, utf *name, utf *desc)
1303 {
1304         s4 i;
1305
1306         for (i = 0; i < c->fieldscount; i++)
1307                 if ((c->fields[i].name == name) && (c->fields[i].descriptor == desc))
1308                         return &(c->fields[i]);
1309
1310         if (c->super != NULL)
1311                 return class_findfield(c->super, name, desc);
1312
1313         return NULL;
1314 }
1315
1316
1317 /* class_findfield_approx ******************************************************
1318         
1319    Searches in 'classinfo'-structure for a field with the specified
1320    name.
1321
1322 *******************************************************************************/
1323  
1324 fieldinfo *class_findfield_by_name(classinfo* c, utf* name)
1325 {
1326         for (int32_t i = 0; i < c->fieldscount; i++) {
1327                 fieldinfo* f = &(c->fields[i]);
1328
1329                 if (f->name == name)
1330                         return f;
1331         }
1332
1333         // Field not found.
1334         exceptions_throw_nosuchfielderror(c, name);
1335         return NULL;
1336 }
1337
1338
1339 /****************** Function: class_resolvefield_int ***************************
1340
1341     This is an internally used helper function. Do not use this directly.
1342
1343         Tries to resolve a field having the given name and type.
1344     If the field cannot be resolved, NULL is returned.
1345
1346 *******************************************************************************/
1347
1348 static fieldinfo *class_resolvefield_int(classinfo *c, utf *name, utf *desc)
1349 {
1350         fieldinfo *fi;
1351         s4         i;
1352
1353         /* search for field in class c */
1354
1355         for (i = 0; i < c->fieldscount; i++) { 
1356                 if ((c->fields[i].name == name) && (c->fields[i].descriptor == desc)) {
1357                         return &(c->fields[i]);
1358                 }
1359     }
1360
1361         /* Try super interfaces recursively. */
1362
1363         for (i = 0; i < c->interfacescount; i++) {
1364                 fi = class_resolvefield_int(c->interfaces[i], name, desc);
1365
1366                 if (fi != NULL)
1367                         return fi;
1368         }
1369
1370         /* Try super class. */
1371
1372         if (c->super != NULL)
1373                 return class_resolvefield_int(c->super, name, desc);
1374
1375         /* not found */
1376
1377         return NULL;
1378 }
1379
1380
1381 /********************* Function: class_resolvefield ***************************
1382         
1383         Resolves a reference from REFERER to a field with NAME and DESC in class C.
1384
1385     If the field cannot be resolved, an exception is thrown and the
1386     return value is NULL.
1387
1388 *******************************************************************************/
1389
1390 fieldinfo *class_resolvefield(classinfo *c, utf *name, utf *desc, classinfo *referer)
1391 {
1392         fieldinfo *fi;
1393
1394         fi = class_resolvefield_int(c, name, desc);
1395
1396         if (!fi) {
1397                 exceptions_throw_nosuchfielderror(c, name);
1398                 return NULL;
1399         }
1400
1401         /* XXX check access rights */
1402
1403         return fi;
1404 }
1405
1406
1407 /* class_issubclass ************************************************************
1408
1409    Checks if sub is a descendant of super.
1410         
1411 *******************************************************************************/
1412
1413 bool class_issubclass(classinfo *sub, classinfo *super)
1414 {
1415         classinfo *c;
1416
1417         c = sub;
1418
1419         for (;;) {
1420                 /* We reached java/lang/Object and did not find the requested
1421                    super class. */
1422
1423                 if (c == NULL)
1424                         return false;
1425
1426                 /* We found the requested super class. */
1427
1428                 if (c == super)
1429                         return true;
1430
1431                 c = c->super;
1432         }
1433 }
1434
1435
1436 /* class_isanysubclass *********************************************************
1437
1438    Checks a subclass relation between two classes. Implemented
1439    interfaces are interpreted as super classes.
1440
1441    Return value: 1 ... sub is subclass of super
1442                  0 ... otherwise
1443
1444 *******************************************************************************/
1445
1446 bool class_isanysubclass(classinfo *sub, classinfo *super)
1447 {
1448         uint32_t diffval;
1449         bool     result;
1450
1451         /* This is the trivial case. */
1452
1453         if (sub == super)
1454                 return true;
1455
1456         /* Primitive classes are only subclasses of themselves. */
1457
1458         if (class_is_primitive(sub) || class_is_primitive(super))
1459                 return false;
1460
1461         /* Check for interfaces. */
1462
1463         if (super->flags & ACC_INTERFACE) {
1464                 result = (sub->vftbl->interfacetablelength > super->index) &&
1465                         (sub->vftbl->interfacetable[-super->index] != NULL);
1466         }
1467         else {
1468                 /* java.lang.Object is the only super class of any
1469                    interface. */
1470
1471                 if (sub->flags & ACC_INTERFACE)
1472                         return (super == class_java_lang_Object);
1473
1474                 linker_classrenumber_mutex->lock();
1475
1476                 diffval = sub->vftbl->baseval - super->vftbl->baseval;
1477                 result  = diffval <= (uint32_t) super->vftbl->diffval;
1478
1479                 linker_classrenumber_mutex->unlock();
1480         }
1481
1482         return result;
1483 }
1484
1485
1486 /* class_is_assignable_from ****************************************************
1487
1488    Return whether an instance of the "from" class parameter would be
1489    an instance of this class "to" as well.
1490
1491    ARGUMENTS:
1492        to ..... class
1493            from ... class
1494
1495    RETURN:
1496        true .... is assignable
1497            false ... is not assignable
1498
1499 *******************************************************************************/
1500
1501 bool class_is_assignable_from(classinfo *to, classinfo *from)
1502 {
1503         if (!(to->state & CLASS_LINKED))
1504                 if (!link_class(to))
1505                         return false;
1506
1507         if (!(from->state & CLASS_LINKED))
1508                 if (!link_class(from))
1509                         return false;
1510
1511         return class_isanysubclass(from, to);
1512 }
1513
1514
1515 /* class_is_instance ***********************************************************
1516
1517    Return if the given Java object is an instance of the given class.
1518
1519    ARGUMENTS:
1520        c ... class
1521            h ... Java object
1522
1523    RETURN:
1524        true .... is instance
1525            false ... is not instance
1526
1527 *******************************************************************************/
1528
1529 bool class_is_instance(classinfo *c, java_handle_t *h)
1530 {
1531         if (!(c->state & CLASS_LINKED))
1532                 if (!link_class(c))
1533                         return false;
1534
1535         return builtin_instanceof(h, c);
1536 }
1537
1538
1539 /* class_get_componenttype *****************************************************
1540
1541    Return the component class of the given class.  If the given class
1542    is not an array, return NULL.
1543
1544 *******************************************************************************/
1545
1546 classinfo *class_get_componenttype(classinfo *c)
1547 {
1548         classinfo       *component;
1549         arraydescriptor *ad;
1550         
1551         /* XXX maybe we could find a way to do this without linking. */
1552         /* This way should be safe and easy, however.                */
1553
1554         if (!(c->state & CLASS_LINKED))
1555                 if (!link_class(c))
1556                         return NULL;
1557
1558         ad = c->vftbl->arraydesc;
1559         
1560         if (ad == NULL)
1561                 return NULL;
1562         
1563         if (ad->arraytype == ARRAYTYPE_OBJECT)
1564                 component = ad->componentvftbl->clazz;
1565         else
1566                 component = Primitive::get_class_by_type(ad->arraytype);
1567                 
1568         return component;
1569 }
1570
1571
1572 /* class_get_declaredclasses ***************************************************
1573
1574    Return an array of declared classes of the given class.
1575
1576 *******************************************************************************/
1577
1578 java_handle_objectarray_t *class_get_declaredclasses(classinfo *c, bool publicOnly)
1579 {
1580         classref_or_classinfo  inner;
1581         classref_or_classinfo  outer;
1582         utf                   *outername;
1583         int                    declaredclasscount;  /* number of declared classes */
1584         int                    pos;                     /* current declared class */
1585         java_handle_objectarray_t *oa;               /* array of declared classes */
1586         int                    i;
1587         classinfo             *ic;
1588
1589         declaredclasscount = 0;
1590
1591         if (!class_is_primitive(c) && !class_is_array(c)) {
1592                 /* Determine number of declared classes. */
1593
1594                 for (i = 0; i < c->innerclasscount; i++) {
1595                         /* Get outer-class.  If the inner-class is not a member
1596                            class, the outer-class is NULL. */
1597
1598                         outer = c->innerclass[i].outer_class;
1599
1600                         if (outer.any == NULL)
1601                                 continue;
1602
1603                         /* Check if outer-class is a classref or a real class and
1604                get the class name from the structure. */
1605
1606                         outername = IS_CLASSREF(outer) ? outer.ref->name : outer.cls->name;
1607
1608                         /* Outer class is this class. */
1609
1610                         if ((outername == c->name) &&
1611                                 ((publicOnly == 0) || (c->innerclass[i].flags & ACC_PUBLIC)))
1612                                 declaredclasscount++;
1613                 }
1614         }
1615
1616         /* Allocate Class[] and check for OOM. */
1617
1618         oa = builtin_anewarray(declaredclasscount, class_java_lang_Class);
1619
1620         if (oa == NULL)
1621                 return NULL;
1622
1623         for (i = 0, pos = 0; i < c->innerclasscount; i++) {
1624                 inner = c->innerclass[i].inner_class;
1625                 outer = c->innerclass[i].outer_class;
1626
1627                 /* Get outer-class.  If the inner-class is not a member class,
1628                    the outer-class is NULL. */
1629
1630                 if (outer.any == NULL)
1631                         continue;
1632
1633                 /* Check if outer_class is a classref or a real class and get
1634                    the class name from the structure. */
1635
1636                 outername = IS_CLASSREF(outer) ? outer.ref->name : outer.cls->name;
1637
1638                 /* Outer class is this class. */
1639
1640                 if ((outername == c->name) &&
1641                         ((publicOnly == 0) || (c->innerclass[i].flags & ACC_PUBLIC))) {
1642
1643                         ic = resolve_classref_or_classinfo_eager(inner, false);
1644
1645                         if (ic == NULL)
1646                                 return NULL;
1647
1648                         if (!(ic->state & CLASS_LINKED))
1649                                 if (!link_class(ic))
1650                                         return NULL;
1651
1652                         LLNI_array_direct(oa, pos++) = (java_object_t *) ic;
1653                 }
1654         }
1655
1656         return oa;
1657 }
1658
1659
1660 /**
1661  * Return an array of declared constructors of the given class.
1662  *
1663  * @param c          class to get the constructors of
1664  * @param publicOnly show only public fields
1665  *
1666  * @return array of java.lang.reflect.Constructor
1667  */
1668 #if defined(ENABLE_JAVASE)
1669 java_handle_objectarray_t *class_get_declaredconstructors(classinfo *c, bool publicOnly)
1670 {
1671         methodinfo*                m;
1672         java_handle_objectarray_t* oa;
1673         int                        count;
1674         int                        index;
1675         int                        i;
1676
1677         /* Determine number of constructors. */
1678
1679         count = 0;
1680
1681         for (i = 0; i < c->methodscount; i++) {
1682                 m = &(c->methods[i]);
1683
1684                 if (((m->flags & ACC_PUBLIC) || (publicOnly == 0)) &&
1685                         (m->name == utf_init))
1686                         count++;
1687         }
1688
1689         /* Create array of constructors. */
1690
1691         oa = builtin_anewarray(count, class_java_lang_reflect_Constructor);
1692
1693         if (oa == NULL)
1694                 return NULL;
1695
1696         /* Get the constructors and store them in the array. */
1697
1698         for (i = 0, index = 0; i < c->methodscount; i++) {
1699                 m = &(c->methods[i]);
1700
1701                 if (((m->flags & ACC_PUBLIC) || (publicOnly == 0)) &&
1702                         (m->name == utf_init)) {
1703                         // Create a java.lang.reflect.Constructor object.
1704
1705                         java_lang_reflect_Constructor rc(m);
1706
1707                         /* Store object into array. */
1708
1709                         array_objectarray_element_set(oa, index, rc.get_handle());
1710                         index++;
1711                 }
1712         }
1713
1714         return oa;
1715 }
1716 #endif
1717
1718
1719 /* class_get_declaredfields ****************************************************
1720
1721    Return an array of declared fields of the given class.
1722
1723    ARGUMENTS:
1724        c ............ class to get the fields of
1725            publicOnly ... show only public fields
1726
1727    RETURN:
1728        array of java.lang.reflect.Field
1729
1730 *******************************************************************************/
1731
1732 #if defined(ENABLE_JAVASE)
1733 java_handle_objectarray_t *class_get_declaredfields(classinfo *c, bool publicOnly)
1734 {
1735         java_handle_objectarray_t *oa;
1736         fieldinfo                 *f;
1737         int                        count;
1738         int                        index;
1739         int                        i;
1740
1741         /* Determine number of fields. */
1742
1743         count = 0;
1744
1745         for (i = 0; i < c->fieldscount; i++)
1746                 if ((c->fields[i].flags & ACC_PUBLIC) || (publicOnly == 0))
1747                         count++;
1748
1749         /* Create array of fields. */
1750
1751         oa = builtin_anewarray(count, class_java_lang_reflect_Field);
1752
1753         if (oa == NULL)
1754                 return NULL;
1755
1756         /* Get the fields and store them in the array. */
1757
1758         for (i = 0, index = 0; i < c->fieldscount; i++) {
1759                 f = &(c->fields[i]);
1760
1761                 if ((f->flags & ACC_PUBLIC) || (publicOnly == 0)) {
1762                         // Create a java.lang.reflect.Field object.
1763
1764                         java_lang_reflect_Field rf(f);
1765
1766                         /* Store object into array. */
1767
1768                         array_objectarray_element_set(oa, index, rf.get_handle());
1769                         index++;
1770                 }
1771         }
1772
1773         return oa;
1774 }
1775 #endif
1776
1777
1778 /* class_get_declaredmethods ***************************************************
1779
1780    Return an array of declared methods of the given class.
1781
1782    ARGUMENTS:
1783        c ............ class to get the methods of
1784            publicOnly ... show only public methods
1785
1786    RETURN:
1787        array of java.lang.reflect.Method
1788
1789 *******************************************************************************/
1790
1791 #if defined(ENABLE_JAVASE)
1792 java_handle_objectarray_t *class_get_declaredmethods(classinfo *c, bool publicOnly)
1793 {
1794         java_handle_objectarray_t *oa;         /* result: array of Method-objects */
1795         methodinfo                *m;     /* the current method to be represented */
1796         int                        count;
1797         int                        index;
1798         int                        i;
1799
1800         /* JOWENN: array classes do not declare methods according to mauve
1801            test.  It should be considered, if we should return to my old
1802            clone method overriding instead of declaring it as a member
1803            function. */
1804
1805         if (class_is_array(c))
1806                 return builtin_anewarray(0, class_java_lang_reflect_Method);
1807
1808         /* Determine number of methods. */
1809
1810         count = 0;
1811
1812         for (i = 0; i < c->methodscount; i++) {
1813                 m = &(c->methods[i]);
1814
1815                 if (((m->flags & ACC_PUBLIC) || (publicOnly == false)) &&
1816                         ((m->name != utf_init) && (m->name != utf_clinit)) &&
1817                         !(m->flags & ACC_MIRANDA))
1818                         count++;
1819         }
1820
1821         /* Create array of methods. */
1822
1823         oa = builtin_anewarray(count, class_java_lang_reflect_Method);
1824
1825         if (oa == NULL)
1826                 return NULL;
1827
1828         /* Get the methods and store them in the array. */
1829
1830         for (i = 0, index = 0; i < c->methodscount; i++) {
1831                 m = &(c->methods[i]);
1832
1833                 if (((m->flags & ACC_PUBLIC) || (publicOnly == false)) && 
1834                         ((m->name != utf_init) && (m->name != utf_clinit)) &&
1835                         !(m->flags & ACC_MIRANDA)) {
1836                         // Create java.lang.reflect.Method object.
1837
1838                         java_lang_reflect_Method rm(m);
1839
1840                         /* Store object into array. */
1841
1842                         array_objectarray_element_set(oa, index, rm.get_handle());
1843                         index++;
1844                 }
1845         }
1846
1847         return oa;
1848 }
1849 #endif
1850
1851
1852 /* class_get_declaringclass ****************************************************
1853
1854    If the class or interface given is a member of another class,
1855    return the declaring class.  For array and primitive classes return
1856    NULL.
1857
1858 *******************************************************************************/
1859
1860 classinfo *class_get_declaringclass(classinfo *c)
1861 {
1862         classref_or_classinfo  cr;
1863         classinfo             *dc;
1864
1865         /* Get declaring class. */
1866
1867         cr = c->declaringclass;
1868
1869         if (cr.any == NULL)
1870                 return NULL;
1871
1872         /* Resolve the class if necessary. */
1873
1874         if (IS_CLASSREF(cr)) {
1875 /*              dc = resolve_classref_eager(cr.ref); */
1876                 dc = resolve_classref_or_classinfo_eager(cr, true);
1877
1878                 if (dc == NULL)
1879                         return NULL;
1880
1881                 /* Store the resolved class in the class structure. */
1882
1883                 cr.cls = dc;
1884         }
1885
1886         dc = cr.cls;
1887
1888         return dc;
1889 }
1890
1891
1892 /* class_get_enclosingclass ****************************************************
1893
1894    Return the enclosing class for the given class.
1895
1896 *******************************************************************************/
1897
1898 classinfo *class_get_enclosingclass(classinfo *c)
1899 {
1900         classref_or_classinfo  cr;
1901         classinfo             *ec;
1902
1903         /* Get enclosing class. */
1904
1905         cr = c->enclosingclass;
1906
1907         if (cr.any == NULL)
1908                 return NULL;
1909
1910         /* Resolve the class if necessary. */
1911
1912         if (IS_CLASSREF(cr)) {
1913 /*              ec = resolve_classref_eager(cr.ref); */
1914                 ec = resolve_classref_or_classinfo_eager(cr, true);
1915
1916                 if (ec == NULL)
1917                         return NULL;
1918
1919                 /* Store the resolved class in the class structure. */
1920
1921                 cr.cls = ec;
1922         }
1923
1924         ec = cr.cls;
1925
1926         return ec;
1927 }
1928
1929
1930 /**
1931  * Return the enclosing constructor as java.lang.reflect.Constructor
1932  * object for the given class.
1933  *
1934  * @param c class to return the enclosing constructor for
1935  *
1936  * @return java.lang.reflect.Constructor object of the enclosing
1937  * constructor
1938  */
1939 #if defined(ENABLE_JAVASE)
1940 java_handle_t* class_get_enclosingconstructor(classinfo *c)
1941 {
1942         methodinfo*    m;
1943
1944         m = class_get_enclosingmethod_raw(c);
1945
1946         if (m == NULL)
1947                 return NULL;
1948
1949         /* Check for <init>. */
1950
1951         if (m->name != utf_init)
1952                 return NULL;
1953
1954         // Create a java.lang.reflect.Constructor object.
1955
1956         java_lang_reflect_Constructor rc(m);
1957
1958         return rc.get_handle();
1959 }
1960 #endif
1961
1962
1963 /* class_get_enclosingmethod ***************************************************
1964
1965    Return the enclosing method for the given class.
1966
1967    IN:
1968        c ... class to return the enclosing method for
1969
1970    RETURN:
1971        methodinfo of the enclosing method
1972
1973 *******************************************************************************/
1974
1975 methodinfo *class_get_enclosingmethod_raw(classinfo *c)
1976 {
1977         constant_nameandtype *cn;
1978         classinfo            *ec;
1979         methodinfo           *m;
1980
1981         /* get enclosing class and method */
1982
1983         ec = class_get_enclosingclass(c);
1984         cn = c->enclosingmethod;
1985
1986         /* check for enclosing class and method */
1987
1988         if (ec == NULL)
1989                 return NULL;
1990
1991         if (cn == NULL)
1992                 return NULL;
1993
1994         /* find method in enclosing class */
1995
1996         m = class_findmethod(ec, cn->name, cn->descriptor);
1997
1998         if (m == NULL) {
1999                 exceptions_throw_internalerror("Enclosing method doesn't exist");
2000                 return NULL;
2001         }
2002
2003         return m;
2004 }
2005
2006
2007 /**
2008  * Return the enclosing method as java.lang.reflect.Method object for
2009  * the given class.
2010  *
2011  * @param c class to return the enclosing method for
2012  *
2013  * @return java.lang.reflect.Method object of the enclosing method
2014  */
2015 #if defined(ENABLE_JAVASE)
2016 java_handle_t* class_get_enclosingmethod(classinfo *c)
2017 {
2018         methodinfo*    m;
2019
2020         m = class_get_enclosingmethod_raw(c);
2021
2022         if (m == NULL)
2023                 return NULL;
2024
2025         /* check for <init> */
2026
2027         if (m->name == utf_init)
2028                 return NULL;
2029
2030         // Create a java.lang.reflect.Method object.
2031
2032         java_lang_reflect_Method rm(m);
2033
2034         return rm.get_handle();
2035 }
2036 #endif
2037
2038
2039 /* class_get_interfaces ********************************************************
2040
2041    Return an array of interfaces of the given class.
2042
2043 *******************************************************************************/
2044
2045 java_handle_objectarray_t *class_get_interfaces(classinfo *c)
2046 {
2047         classinfo                 *ic;
2048         java_handle_objectarray_t *oa;
2049         u4                         i;
2050
2051         if (!(c->state & CLASS_LINKED))
2052                 if (!link_class(c))
2053                         return NULL;
2054
2055         oa = builtin_anewarray(c->interfacescount, class_java_lang_Class);
2056
2057         if (oa == NULL)
2058                 return NULL;
2059
2060         for (i = 0; i < c->interfacescount; i++) {
2061                 ic = c->interfaces[i];
2062
2063                 LLNI_array_direct(oa, i) = (java_object_t *) ic;
2064         }
2065
2066         return oa;
2067 }
2068
2069
2070 /* class_get_annotations *******************************************************
2071
2072    Get the unparsed declared annotations in a byte array
2073    of the given class.
2074
2075    IN:
2076        c........the class of which the annotations should be returned
2077
2078    RETURN VALUE:
2079        The unparsed declared annotations in a byte array
2080        (or NULL if there aren't any).
2081
2082 *******************************************************************************/
2083
2084 java_handle_bytearray_t *class_get_annotations(classinfo *c)
2085 {
2086 #if defined(ENABLE_ANNOTATIONS)
2087         java_handle_t *annotations; /* unparsed annotations */
2088
2089         LLNI_classinfo_field_get(c, annotations, annotations);
2090
2091         return (java_handle_bytearray_t*)annotations;
2092 #else
2093         return NULL;
2094 #endif
2095 }
2096
2097
2098 /* class_get_modifiers *********************************************************
2099
2100    Get the modifier flags of the given class.
2101
2102    IN:
2103        c....the class of which the modifier flags should be returned
2104            ignoreInnerClassesAttrib
2105    RETURN VALUE:
2106        modifier flags
2107
2108 *******************************************************************************/
2109
2110 int32_t class_get_modifiers(classinfo *c, bool ignoreInnerClassesAttrib)
2111 {
2112         classref_or_classinfo  inner;
2113         classref_or_classinfo  outer;
2114         utf                   *innername;
2115         int                    i;
2116         int32_t                flags;
2117
2118         /* default to flags of passed class */
2119
2120         flags = c->flags;
2121
2122         /* if requested we check if passed class is inner class */
2123
2124         if (!ignoreInnerClassesAttrib && (c->innerclasscount != 0)) {
2125                 /* search for passed class as inner class */
2126
2127                 for (i = 0; i < c->innerclasscount; i++) {
2128                         inner = c->innerclass[i].inner_class;
2129                         outer = c->innerclass[i].outer_class;
2130
2131                         /* Check if inner is a classref or a real class and get
2132                the name of the structure */
2133
2134                         innername = IS_CLASSREF(inner) ? inner.ref->name : inner.cls->name;
2135
2136                         /* innerclass is this class */
2137
2138                         if (innername == c->name) {
2139                                 /* has the class actually an outer class? */
2140
2141                                 if (outer.any)
2142                                         /* return flags got from the outer class file */
2143                                         flags = c->innerclass[i].flags;
2144
2145                                 break;
2146                         }
2147                 }
2148         }
2149
2150         /* remove ACC_SUPER bit from flags */
2151
2152         return flags & ~ACC_SUPER & ACC_CLASS_REFLECT_MASK;
2153 }
2154
2155
2156 /* class_get_signature *********************************************************
2157
2158    Return the signature of the given class.  For array and primitive
2159    classes return NULL.
2160
2161 *******************************************************************************/
2162
2163 #if defined(ENABLE_JAVASE)
2164 utf *class_get_signature(classinfo *c)
2165 {
2166         /* For array and primitive classes return NULL. */
2167
2168         if (class_is_array(c) || class_is_primitive(c))
2169                 return NULL;
2170
2171         return c->signature;
2172 }
2173 #endif
2174
2175
2176 /* class_printflags ************************************************************
2177
2178    Prints flags of a class.
2179
2180 *******************************************************************************/
2181
2182 #if !defined(NDEBUG)
2183 void class_printflags(classinfo *c)
2184 {
2185         if (c == NULL) {
2186                 printf("NULL");
2187                 return;
2188         }
2189
2190         if (c->flags & ACC_PUBLIC)       printf(" PUBLIC");
2191         if (c->flags & ACC_PRIVATE)      printf(" PRIVATE");
2192         if (c->flags & ACC_PROTECTED)    printf(" PROTECTED");
2193         if (c->flags & ACC_STATIC)       printf(" STATIC");
2194         if (c->flags & ACC_FINAL)        printf(" FINAL");
2195         if (c->flags & ACC_SYNCHRONIZED) printf(" SYNCHRONIZED");
2196         if (c->flags & ACC_VOLATILE)     printf(" VOLATILE");
2197         if (c->flags & ACC_TRANSIENT)    printf(" TRANSIENT");
2198         if (c->flags & ACC_NATIVE)       printf(" NATIVE");
2199         if (c->flags & ACC_INTERFACE)    printf(" INTERFACE");
2200         if (c->flags & ACC_ABSTRACT)     printf(" ABSTRACT");
2201 }
2202 #endif
2203
2204
2205 /* class_print *****************************************************************
2206
2207    Prints classname plus flags.
2208
2209 *******************************************************************************/
2210
2211 #if !defined(NDEBUG)
2212 void class_print(classinfo *c)
2213 {
2214         if (c == NULL) {
2215                 printf("NULL");
2216                 return;
2217         }
2218
2219         utf_display_printable_ascii(c->name);
2220         class_printflags(c);
2221 }
2222 #endif
2223
2224
2225 /* class_classref_print ********************************************************
2226
2227    Prints classname plus referer class.
2228
2229 *******************************************************************************/
2230
2231 #if !defined(NDEBUG)
2232 void class_classref_print(constant_classref *cr)
2233 {
2234         if (cr == NULL) {
2235                 printf("NULL");
2236                 return;
2237         }
2238
2239         utf_display_printable_ascii(cr->name);
2240         printf("(ref.by ");
2241         if (cr->referer)
2242                 class_print(cr->referer);
2243         else
2244                 printf("NULL");
2245         printf(")");
2246 }
2247 #endif
2248
2249
2250 /* class_println ***************************************************************
2251
2252    Prints classname plus flags and new line.
2253
2254 *******************************************************************************/
2255
2256 #if !defined(NDEBUG)
2257 void class_println(classinfo *c)
2258 {
2259         class_print(c);
2260         printf("\n");
2261 }
2262 #endif
2263
2264
2265 /* class_classref_println ******************************************************
2266
2267    Prints classname plus referer class and new line.
2268
2269 *******************************************************************************/
2270
2271 #if !defined(NDEBUG)
2272 void class_classref_println(constant_classref *cr)
2273 {
2274         class_classref_print(cr);
2275         printf("\n");
2276 }
2277 #endif
2278
2279
2280 /* class_classref_or_classinfo_print *******************************************
2281
2282    Prints classname plus referer class.
2283
2284 *******************************************************************************/
2285
2286 #if !defined(NDEBUG)
2287 void class_classref_or_classinfo_print(classref_or_classinfo c)
2288 {
2289         if (c.any == NULL) {
2290                 printf("(classref_or_classinfo) NULL");
2291                 return;
2292         }
2293         if (IS_CLASSREF(c))
2294                 class_classref_print(c.ref);
2295         else
2296                 class_print(c.cls);
2297 }
2298 #endif
2299
2300
2301 /* class_classref_or_classinfo_println *****************************************
2302
2303    Prints classname plus referer class and a newline.
2304
2305 *******************************************************************************/
2306
2307 #if !defined(NDEBUG)
2308 void class_classref_or_classinfo_println(classref_or_classinfo c)
2309 {
2310         class_classref_or_classinfo_print(c);
2311         printf("\n");
2312 }
2313 #endif
2314
2315
2316 /* class_showconstantpool ******************************************************
2317
2318    Dump the constant pool of the given class to stdout.
2319
2320 *******************************************************************************/
2321
2322 #if !defined(NDEBUG)
2323 void class_showconstantpool (classinfo *c) 
2324 {
2325         u4 i;
2326         void* e;
2327
2328         printf ("---- dump of constant pool ----\n");
2329
2330         for (i=0; i<c->cpcount; i++) {
2331                 printf ("#%d:  ", (int) i);
2332                 
2333                 e = c -> cpinfos [i];
2334                 if (e) {
2335                         
2336                         switch (c -> cptags [i]) {
2337                         case CONSTANT_Class:
2338                                 printf ("Classreference -> ");
2339                                 utf_display_printable_ascii ( ((constant_classref*)e) -> name );
2340                                 break;
2341                         case CONSTANT_Fieldref:
2342                                 printf ("Fieldref -> ");
2343                                 field_fieldref_print((constant_FMIref *) e);
2344                                 break;
2345                         case CONSTANT_Methodref:
2346                                 printf ("Methodref -> ");
2347                                 method_methodref_print((constant_FMIref *) e);
2348                                 break;
2349                         case CONSTANT_InterfaceMethodref:
2350                                 printf ("InterfaceMethod -> ");
2351                                 method_methodref_print((constant_FMIref *) e);
2352                                 break;
2353                         case CONSTANT_String:
2354                                 printf ("String -> ");
2355                                 utf_display_printable_ascii ((utf*) e);
2356                                 break;
2357                         case CONSTANT_Integer:
2358                                 printf ("Integer -> %d", (int) ( ((constant_integer*)e) -> value) );
2359                                 break;
2360                         case CONSTANT_Float:
2361                                 printf ("Float -> %f", ((constant_float*)e) -> value);
2362                                 break;
2363                         case CONSTANT_Double:
2364                                 printf ("Double -> %f", ((constant_double*)e) -> value);
2365                                 break;
2366                         case CONSTANT_Long:
2367                                 printf ("Long -> %ld", (long int) ((constant_long*)e) -> value);
2368                                 break;
2369                         case CONSTANT_NameAndType:
2370                                 {
2371                                         constant_nameandtype *cnt = (constant_nameandtype *) e;
2372                                         printf ("NameAndType: ");
2373                                         utf_display_printable_ascii (cnt->name);
2374                                         printf (" ");
2375                                         utf_display_printable_ascii (cnt->descriptor);
2376                                 }
2377                                 break;
2378                         case CONSTANT_Utf8:
2379                                 printf ("Utf8 -> ");
2380                                 utf_display_printable_ascii ((utf*) e);
2381                                 break;
2382                         default: 
2383                                 log_text("Invalid type of ConstantPool-Entry");
2384                                 assert(0);
2385                         }
2386                 }
2387
2388                 printf ("\n");
2389         }
2390 }
2391 #endif /* !defined(NDEBUG) */
2392
2393
2394 /* class_showmethods ***********************************************************
2395
2396    Dump info about the fields and methods of the given class to stdout.
2397
2398 *******************************************************************************/
2399
2400 #if !defined(NDEBUG)
2401 void class_showmethods (classinfo *c)
2402 {
2403         s4 i;
2404         
2405         printf("--------- Fields and Methods ----------------\n");
2406         printf("Flags: ");
2407         class_printflags(c);
2408         printf("\n");
2409
2410         printf("This: ");
2411         utf_display_printable_ascii(c->name);
2412         printf("\n");
2413
2414         if (c->super) {
2415                 printf("Super: ");
2416                 utf_display_printable_ascii(c->super->name);
2417                 printf ("\n");
2418         }
2419
2420         printf("Index: %d\n", c->index);
2421         
2422         printf("Interfaces:\n");        
2423         for (i = 0; i < c->interfacescount; i++) {
2424                 printf("   ");
2425                 utf_display_printable_ascii(c->interfaces[i]->name);
2426                 printf (" (%d)\n", c->interfaces[i]->index);
2427         }
2428
2429         printf("Fields:\n");
2430         for (i = 0; i < c->fieldscount; i++)
2431                 field_println(&(c->fields[i]));
2432
2433         printf("Methods:\n");
2434         for (i = 0; i < c->methodscount; i++) {
2435                 methodinfo *m = &(c->methods[i]);
2436
2437                 if (!(m->flags & ACC_STATIC))
2438                         printf("vftblindex: %d   ", m->vftblindex);
2439
2440                 method_println(m);
2441         }
2442
2443         printf ("Virtual function table:\n");
2444         for (i = 0; i < c->vftbl->vftbllength; i++)
2445                 printf ("entry: %d,  %ld\n", i, (long int) (c->vftbl->table[i]));
2446 }
2447 #endif /* !defined(NDEBUG) */
2448
2449
2450 /*
2451  * These are local overrides for various environment variables in Emacs.
2452  * Please do not remove this and leave it at the end of the file, where
2453  * Emacs will automagically detect them.
2454  * ---------------------------------------------------------------------
2455  * Local variables:
2456  * mode: c
2457  * indent-tabs-mode: t
2458  * c-basic-offset: 4
2459  * tab-width: 4
2460  * End:
2461  * vim:noexpandtab:sw=4:ts=4:
2462  */