Will cni be the sane native interface to c++ in openjdk U gcj?

Glenn Chambers gchamber@bright.net
Wed Dec 26 00:47:00 GMT 2007


On Tue, 2007-12-25 at 16:03 -0800, ter wrote:
> On Tue, 2007-12-25 at 15:57 -0500, Glenn Chambers wrote:
> > On Tue, 2007-12-25 at 16:49 +0000, Andrew Haley wrote:
> > 
> > > Mmm, I think you are right.  C++ code either throws Java exceptions or
> > > C++ exceptions, but not both.  I failed proprtly to read what you
> > > wrote.
> > 
> > The most interesting question to me isn't what it throws, but what
> > it can catch.
> > 
> > If I can write this in a Java CNI implementation function, then I'm
> > happy:
> > 
> > void myJavaClass::NativeMethod(int parameter)
> > {
> > try {
> > nativeCPPLibrary::globalFunction();
> > } catch (nativeCPPLibrary::myException x) {
> > throw new myJavaException(x.failCode);
> > }
> > }
> > 
> > In the 3.0 version I was playing with, this code was illegal.  In fact,
> > referring to both C++ exceptions and Java exceptions in the same object
> > file failed.
> > 
> > 
> swig (using >= 1.3.31) provides this with a java proxy wrapping
> nativeCPPLibrary::myException as myJavaException subclassed from
> java.lang.Exception, and jni code to do just the above. This is very
> much one of the complex contortions in using jni.

The major advantage of CNI is that the C++ compiler and the Java
compiler agree on the physical layout of the objects, so that access
to member variables (I've forgotten the correct C++ terminology here)
is a simple "*(this+offset)" memory reference instead of a JNI function
call.

If Java allowed multiple inheritance, or allowed the throwing of things
other than those derived from Exception, you wouldn't need the exception
wrapping logic, and CNI would be a Major Win.  As it is, it's still
substantial, since if the library uses inlined accessors, you're still
at the direct memory reference even with the single indirection of a
wrapper object.

On the other hand, CNI comes with a cost: "Binary Compatibility" goes
out the window.  If you add *anything* to a class, everything using
that class needs to be recompiled.




More information about the Java mailing list