Contents Prev Next

# 2 Useful Routines in C

A number of Java interpreter internal macros and functions are available for accessing Java structures. Note that these functions and macros are internal and subject to change at any time. It's recommended that you actually verify the macro and function prototypes before using them in your C code by looking at the Java sources.

unhand

    Object *unhand(Handle *)

A macro to dereference an object handle. This returns a pointer to the data portion of an object.

obj_length

    int obj_length(HArray *)

Given a pointer to an array handle this macro returns the maximum length of the array.

bodyof

    Object *bodyof(HArray *)

Given a pointer to an array handle this macro returns a pointer to the data portion of the array. As with unhand(), the return type depends on the type of the array .

FindClass

    ClassClass* FindClass(struct execenv *ee, char *name, bool_t resolve)

Find the class with the given name. If "resolve" is true, then we completely resolve the class which entails loading all of the class's superclasses etc. If "resolve" is false and the requested class is not loaded the function returns null.

MakeString

    HArrayOfChar *MakeString(char *str, int len)

Creates a Java array of characters of length len and initializes it from the values pointed to by str. Note that Java strings are strings of 16-bit Unicode values. Only the low-order byte of the Java characters are initialized and the high-order bytes are set to `\0'.

ArrayAlloc

    Handle *ArrayAlloc(int type, int len)

Allocates an Java array of a given type and length len. The type field must be one of the following:

execute_method

    long execute_method(struct execenv *current, void *obj, char *method, ...)

This routine allows you to call an Java method from C. The first parameter is the current Java environment. If you pass in null or "0" the Java interpreter uses the current environment by default. The object instance on which you want to call the method is passed as a "void *" in the second parameter to the routine. The name of the method to call is passed as the third parameter. Finally, any subsequent arguments are passed to the method. The Java method may have any number of arguments and may return a single 4 byte value which may be an integer, a pointer to an object handle, or any other 4 byte C type. The method name must be the complete internal method signature name as described previously. See execute_static_method() if you need to call a static (or class) method; execute_method() only works on instance methods. Finally, note that this function may throw an exception which we cannot catch in our C code. Below is an example of calling an Java method from C.

    void
    DeliverData(HActivityRecord *arh, Ptype ptype, HArrayOfByte *buf, int len)
    {
        execute_method(0, arh, "DeliverData(I[BI)V", ptype, buf, len);
    }

exectute_static_method

    long execute_static_method(struct execenv *current, ClassClass *cb,
    char *method, ...)

This routine is used to call a static (class) method from C. It is exactly analogous to execute_method() above, except you provide a Class instead of an object.

execute_constructor

    HObject *execute_constructor(struct execenv *current, char *method, ...)

This routine allows you to easily create a new Java object from C.The arguments to the constructor are determined by looking at the signature of the constructor. The new object is returned if all is OK, 0 is returned in case of an error. Example:

        HObject * obj;
        int x, y;

        obj = execute_constructor(0, "<init>(II)Lfp/ade/gui/MouseEvent;", x, y);

Note that execute_constructor is a wrapper for the internal exec_constructor function. Also note that this function may throw an exception.

SignalError

    SignalError(0, JAVAPKG "Exception Name", "message");

Basically, SignalError() throws an Java exception on the thread that called into your C code. This routine does not return! It does a non-local goto and continues execution as if the Java code had done a

    throw new exception_name("details_message");

Valid values for the exception_name argument are any valid exception class name (see the Java class documentation for more information).

Contents Prev Next

Implementing Native Methods

Generated with CERN WebMaker