Chapter 3 |
Proxy Classes |
Here a Proxy, There a Proxy, Everywhere a Proxy Proxy |
Jace C++ Proxies are C++ classes that wrap existing Java types. If you examine the JNI type system, you'll see that there are a total of 24 different types.
nine primitive types:
Figure 1
jint jniInt = 32;
JInt fromJniInt( jniInt );
JInt fromCppInt( 32 );
Also subclassing JValue is JObject, the root of all Java reference types. JObject is a wrapper around the JNI jobject type, and as such, provides access to its JNI jobject, through the getLocalObject()
method. Like JValues, JObjects can be constructed from jvalues, but they can also be constructed from jobjects. And also like JValue, JObject sets a rule for all of its base classes - only this time, the children must be constructible from a jobject as well as a jvalue. Differing from it's simpler ancestors and brethern, JObject has some added capabilities. First, when a JObject is constructed, it creates a new global reference to its jobject. You can access that global reference with a call to getJavaObject()
. Second, you can test a JObject to see if it is null by calling, isNull()
.
void printLength( jintArray array ) {
JArray<JInt> intArray( array );
cout << intArray.length() << endl;
}
JArray<URL> createNewURLArrayOfLength( int length ) {
JArray<URL> array( length );
return array;
}
Like its Java counterpart, you can access elements of a JArray by using operator[](). For example,
void printArray( JArray<String> array ) {
for ( int i = 0; i < array.length(); ++i ) {
String str = array[ i ];
cout << str << endl;
}
}
Previous Next |