Browse Prior Art Database

Method to hide JNI plumbing details from programmer

IP.com Disclosure Number: IPCOM000015369D
Original Publication Date: 2001-Dec-19
Included in the Prior Art Database: 2003-Jun-20
Document File: 6 page(s) / 57K

Publishing Venue

IBM

Abstract

1. Concept

This text was extracted from a PDF file.
At least one non-text object (such as an image or picture) has been suppressed.
This is the abbreviated version, containing approximately 44% of the total text.

Page 1 of 6

Method to hide JNI plumbing details from programmer

1. Concept

    A programming guideline is disclosed that establishes a lightweight framework designed to hide Java Native Interface (JNI) plumbing from the programmer who wants to use Java methods out of an existing C++ environment. This allows to develop or reuse components written in Java and leveraging them from within C++ code without the traditional need to write and test a lot of error-prone and lengthy (1) JNI wrappering code around the Java object, thereby reducing code complexity and improving code maintainability.

    There exist implementations which solve this problem in a generic way, usually in combination with a code generator or with a metadata repository that mimics some of the capabilities of the Java class reflection interfaces. An example of the first apporach are CORBA implementations that allow to generate Java, C++ or other stubs from an interface file (IDL). If the runtime infrastructure is developed in C++, the Java stubs use JNI functionality "under the covers" but do not allow the end user to use that functionality directly.

    The lightweight infrastructure presented here hides the details of JNI programming from its user (i.e., the programmer) for many simple and recurrent cases while at the same time allowing to use the full width of JNI functionality for special cases. To achieve this, we provide a C++ base class "JniBaseWrapper" that the programmer needs to subclass. It includes a set of methods each of which has the signature of one frequently used JNI function with an immutable parameter list, e.g., void JniBaseWrapper::callVoidMethod(
const char* aMethodName,
const char* aMethodParameter)

    In addition, the class contains all the logic that is required to set up and shut down the Java virtual machine (JVM) and to maintain access to the JNI environment structures. Optionally, the functionality related to the management of the JVM might be delegated to a helper class, named "JvmWrapper" in the figure below.

    By inheriting from the JniBaseWrapper base class, the new C++ class " <YourClass>Wrapper" that is designed to wrapper the target Java class "<YourClass>" has automatically access to the full wealth of JNI calls provided by the JniBaseWrapper without writing a single line of JNI code while at the same time being able to access JNI functions and attributes directly for specific tasks that are not considered worth to be put into the JniBaseWrapper interface for general use.

The following figure depicts the structure of this approach:

1

Page 2 of 6

JNI wrapper structure and use

<YourClass>.java

<YourBaseCode>.cpp

JvmWrapper.cpp

JniBaseWrapper.cpp

UT environment: create and manage JVM, otherwise: if possible, attach to existing JVM

extremely thin, just a few lines of code to be written

Objective

The main purpose is to call and use the functionality of <YourClass>.java from

within <YourBaseCode>.cpp. <YourClass>Wrapper is an extremely thin wrapper, just a few lines...