invoking package-private constructor from native code

Tom Tromey tromey@cygnus.com
Sat Sep 16 09:49:00 GMT 2000


Oskar> From a jclass reference I wish to be able to create an instance
Oskar> using a package-private constructor. So I figured I can use
Oskar> _Jv_CallAnyMethodA for this. But I need a jmethodID of the
Oskar> constructor. How do I get that?

I recommend against using _Jv_CallAnyMethodA.  This is an ugly
internal function whose name and/or calling convention might change.

Instead you could just use reflection directly.  Use
Class.getDeclaredConstructor to get the constructor, and then use
Constructor.newInstance to make a new instance.

This is less efficient than the approaches that rely on internal
knowledge of libgcj, but more likely to continue working.  The
efficiency loss comes because this approach does some allocation.

If you really want to use the internals, use JvGetFirstMethod to get
the first jmethodID for your class, and then iterate over the method
ids (looking at the names and signatures) until you find the one you
want.  Look in natClass.cc for an example.

Tom


More information about the Java mailing list