Classes and objects


Data Structure

/* defined in classMethod.h */
/* to represent a class loader */

typedef struct Hjava_lang_ClassLoader {

        int     dummy;

} Hjava_lang_ClassLoader;


/* defined in classMethod.h */
/* to represent a class */

struct Hjava_lang_Class {

        Hjava_lang_Object       head;           /* A class is an object too */

        /* Link to class entry */
        struct _classEntry*     centry;

        Utf8Const*              name;
        accessFlags             accflags;

        /* If non-NULL, a pointer to the superclass.
         * However, if state < CSTATE_DOING_PREPARE, then
         * (int) superclass is a constant pool index. */
        struct Hjava_lang_Class* superclass;

        struct _constants       constants;    /* setup while read in constant pool from opened file, 'constants.c'->readConstantPool ()*/

        /* For regular classes, an array of the methods defined in this class.
           For array types, used for CLASS_ELEMENT_TYPE.
           For primitive types, used by CLASS_ARRAY_CACHE. */
        Method*                 methods;
        short                   nmethods;

        /* Number of methods in the dtable. */
        /* If CLASS_IS_PRIMITIVE, then the CLASS_PRIM_SIG. */
        short                   msize;

        /* Pointer to array of Fields, on for each field.
           Static fields come first. */
        Field*                  fields;

        /* The size of the non-static fields, in bytes.
           For a primitive type, the length in bytes.
           Also used temporarily while reading fields.  */
        int                     bfsize;

        /* Number of fields, including static fields. */
        short                   nfields;
        /* Number of static fields. */
        short                   nsfields;

        struct _dispatchTable*  dtable;

        struct Hjava_lang_Class** interfaces;
        short                   interface_len;
        short                   total_interface_len;
        Hjava_lang_ClassLoader* loader;

        /* A bitmap describing the layout of instances of that class.
           It contains CLASS_FSIZE/ALIGNMENTVOIDP bits.
           The MSB corresponds to the dtable field.
         */
        int*                    gc_layout;
        char                    state;
        void*                   processingThread;

};


/* defined in classMethod.h */
 /* Class hash entry. */

typedef struct _classEntry {

        Utf8Const*              name;
        Hjava_lang_ClassLoader* loader;
        Hjava_lang_Class*       class;
        struct _classEntry*     next;

} classEntry;


/* defined in classMethod.h */
/* to represent a method */

typedef struct _methods {

        Utf8Const*              name;
        Utf8Const*              signature;
        accessFlags             accflags;
        short                   idx;    /* Index into class->dtable */
        u2                      stacksz;
        u2                      localsz;
        nativecode*             ncode;  /* Must be here for trampolines */
        union {
          struct {
                nativecode*     ncode_start;
                nativecode*     ncode_end;
          } ncode;
          struct {
                unsigned char*  code;
                int             codelen;
          } bcode;
        } c;
        Hjava_lang_Class*       class;
        struct _lineNumbers*    lines;
        struct _jexception*     exception_table;
        u2                      ndeclared_exceptions;
        constIndex*             declared_exceptions;

} methods;


/* defined in classMethod.h */
/* dispatch table */

typedef struct _dispatchTable {

        Hjava_lang_Class*       class;
        void*                   method[1];

} dispatchTable;


/* defined in classMethod.h */
/* to represent a field */

typedef struct _fields {

        Utf8Const*              name;
        /* The type of the field, if FIELD_RESOLVED.
           If !FIELD_RESOLVED:  The fields's signature as a (Utf8Const*). */
        Hjava_lang_Class*       type;
        accessFlags             accflags;
        u2                      bsize;          /* in bytes */
        union {
                int             boffset;        /* offset in bytes (object) */
                void*           addr;           /* address (static) */
                u2              idx;            /* constant value index */
        } info;

} fields;


/* defined in include/java_lang_Object.h */
/* to represent an object */

typedef struct Hjava_lang_Object {

        struct _dispatchTable*  dtable;
#if defined(USE_LOCK_CACHE)
        struct _iLock*          lock;
#endif
        /* Data follows on immediately */

} Hjava_lang_Object;

 

 


Process

Class loading: (done in 'classMethod.c'->loadClass())

JVM contains two knids of class loaders: a primordial class loader and class loader objects which simply invoke a java method written by programmer. The primordial class loader is a part of the virtual machine implementation which in turn calls 'findInJar.c'->findClass().

Linking: performing verification, preparation, and (optionally) resolution, (done in 'classMethod.c'->processClass())

           #Verification: ensuring the correctness of the imported type which is done in lookupClass->processClass->verify2, verify3.
           #Preparation: allocating memory for class variables and initializing the memory to default values
              lookupClass->processClass->allocStaticFields.
           #Resolution: transforming symbolic references from the type into direct references which is done in
              lookupClass->processClass->resolveObjectFields, resolveStaticFields, resolveConstants.

Initialization: invoking Java code that initializes class variables to their proper starting values. which is done by calling the class

       initialization method i.e. <clinit>, lookupClass->processClass->callMethodA