Discussion:
including custom Java code
Bob Marinier
2004-12-22 18:18:30 UTC
Permalink
I'm trying to get callbacks working through Java. Ultimately I'd like
to do this properly with typemaps, but due to time constraints I've
opted to write the code myself. I have a .h file which contains all of
the necessary C and JNI code. I can include this .h via my .i file.
However, I also have some custom Java code which needs to be appended to
various java files that SWIG generates. Is there any way of doing this
via SWIG, or must I do it by hand?

I'm also exploring the possibility of only providing the custom JNI
functions and letting SWIG generate the necessary Java code (i.e. via
%native). However, I'm not sure this is possible without writing some
typemaps. One of my JNI functions looks like this:

JNIEXPORT jint JNICALL
Java_sml_smlJNI_Agent_1RegisterForRunEventByHand(JNIEnv *jenv, jclass
jcls, jlong jarg1, jint jarg2, jobject jarg3, jobject jarg4, jstring
jarg5, jobject jarg6)

To use %native I need to provide the equivalent C types, but I don't
know how to do that for jobjects. Any ideas?

Thanks!
Bob Marinier
AI Laboratory
University of Michigan

_______________________________________________
Swig maillist - ***@cs.uchicago.edu
http://mailman.cs.uchicago.edu/mailman/listinfo/swig
Antti Karanta
2004-12-23 06:39:45 UTC
Permalink
-----Original Message-----
I'm trying to get callbacks working through Java. Ultimately
I'd like
to do this properly with typemaps, but due to time constraints I've
opted to write the code myself. I have a .h file which
contains all of
the necessary C and JNI code. I can include this .h via my .i file.
However, I also have some custom Java code which needs to be
appended to
various java files that SWIG generates. Is there any way of
doing this
via SWIG, or must I do it by hand?
It's pretty simple, see section 19.4. in the Swig manual. Here's a
small sample:

%pragma(java) modulecode=%{

// utility class - not to be instantiated
private $module() {}

/** javadoc comment */
static void Asgnumpar(int nInd, double fValue) {
$module.Asgnumpar(nInd, fValue, SFConstants.DEFLT_SFARG_DESCR);
}

%}

You can affect everything: the imports, the class modifiers, code
inside the class etc. in the JNI intermediary class, module class and I
also think this applies for the proxy classes (I've been wrapping
vanilla c so I haven't dug into that).



-A-



_______________________________________________
Swig maillist - ***@cs.uchicago.edu
http://mailman.cs.uchicago.edu/mailman/listinfo/swig
William S Fulton
2004-12-23 22:04:25 UTC
Permalink
Post by Bob Marinier
I'm trying to get callbacks working through Java. Ultimately I'd like
to do this properly with typemaps, but due to time constraints I've
opted to write the code myself. I have a .h file which contains all of
the necessary C and JNI code. I can include this .h via my .i file.
However, I also have some custom Java code which needs to be appended to
various java files that SWIG generates. Is there any way of doing this
via SWIG, or must I do it by hand?
You can prepend code to the Java generated code by abusing the imports
typemap or pragma.

%typemap(javaimports) FooBar %{
// Code for the FooBar proxy class
%}

%pragma(java) moduleimports=%{
// Code for the module class
%}

%pragma(java) jniclassimports=%{
// Code for the JNI intermediary class
%}

Otherwise use the Unix cat command in your makefiles to append code.
Post by Bob Marinier
I'm also exploring the possibility of only providing the custom JNI
functions and letting SWIG generate the necessary Java code (i.e. via
%native). However, I'm not sure this is possible without writing some
JNIEXPORT jint JNICALL
Java_sml_smlJNI_Agent_1RegisterForRunEventByHand(JNIEnv *jenv, jclass
jcls, jlong jarg1, jint jarg2, jobject jarg3, jobject jarg4, jstring
jarg5, jobject jarg6)
To use %native I need to provide the equivalent C types, but I don't
know how to do that for jobjects. Any ideas?
The Examples/java/native example might help. Maybe something like:

%{
JNIEXPORT jint JNICALL
Java_sml_smlJNI_Agent_1RegisterForRunEventByHand(JNIEnv *jenv, jclass
jcls, jobject jarg3);
%}
class Klass {};
%native(RegisterForRunEventByHand) int RegisterForRunEventByHand(Klass
arg3);


%native is rather fiddly because you might need to write the javain and
javaout typemaps for the types in the function if you are doing things
differently to the way SWIG handles the types. Also, the JNI method must
be in the JNI intermediary class and not anywhere else, so is a bit
restricting. I'm not even sure typemaps will work for %native methods.

Personally, I'd use %ignore on the JNI method being wrapped and then use
one of the 'modulecode' or 'jniclasscode' pragmas to add Java code to
the module class / JNI intermediary class and/or 'javacode' typemap to
add code to the appropriate proxy class.

William
_______________________________________________
Swig maillist - ***@cs.uchicago.edu
http://mailman.cs.uchicago.edu/mailman/listinfo/swig
Bob Marinier
2004-12-23 22:17:06 UTC
Permalink
Thanks, I got it working!

Bob
Post by William S Fulton
Post by Bob Marinier
I'm trying to get callbacks working through Java. Ultimately I'd
like to do this properly with typemaps, but due to time constraints
I've opted to write the code myself. I have a .h file which contains
all of the necessary C and JNI code. I can include this .h via my .i
file. However, I also have some custom Java code which needs to be
appended to various java files that SWIG generates. Is there any way
of doing this via SWIG, or must I do it by hand?
You can prepend code to the Java generated code by abusing the imports
typemap or pragma.
%typemap(javaimports) FooBar %{
// Code for the FooBar proxy class
%}
%pragma(java) moduleimports=%{
// Code for the module class
%}
%pragma(java) jniclassimports=%{
// Code for the JNI intermediary class
%}
Otherwise use the Unix cat command in your makefiles to append code.
Post by Bob Marinier
I'm also exploring the possibility of only providing the custom JNI
functions and letting SWIG generate the necessary Java code (i.e. via
%native). However, I'm not sure this is possible without writing
JNIEXPORT jint JNICALL
Java_sml_smlJNI_Agent_1RegisterForRunEventByHand(JNIEnv *jenv, jclass
jcls, jlong jarg1, jint jarg2, jobject jarg3, jobject jarg4, jstring
jarg5, jobject jarg6)
To use %native I need to provide the equivalent C types, but I don't
know how to do that for jobjects. Any ideas?
%{
JNIEXPORT jint JNICALL
Java_sml_smlJNI_Agent_1RegisterForRunEventByHand(JNIEnv *jenv, jclass
jcls, jobject jarg3);
%}
class Klass {};
%native(RegisterForRunEventByHand) int RegisterForRunEventByHand(Klass
arg3);
%native is rather fiddly because you might need to write the javain
and javaout typemaps for the types in the function if you are doing
things differently to the way SWIG handles the types. Also, the JNI
method must be in the JNI intermediary class and not anywhere else, so
is a bit restricting. I'm not even sure typemaps will work for %native
methods.
Personally, I'd use %ignore on the JNI method being wrapped and then
use one of the 'modulecode' or 'jniclasscode' pragmas to add Java code
to the module class / JNI intermediary class and/or 'javacode' typemap
to add code to the appropriate proxy class.
William
_______________________________________________
http://mailman.cs.uchicago.edu/mailman/listinfo/swig
_______________________________________________
Swig maillist - ***@cs.uchicago.edu
http://mailman.cs.uchicago.edu/mailman/listinfo/swig

Loading...