From 75fd6feb633d930e88d9cd2b562da96d48b0ea65 Mon Sep 17 00:00:00 2001 From: Harco de Hilster Date: Mon, 6 Mar 2000 11:56:10 +0000 Subject: [PATCH] added a typemap example for java git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@334 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- SWIG/Examples/java/typemap/Makefile | 16 ++++++ SWIG/Examples/java/typemap/README | 8 +++ SWIG/Examples/java/typemap/example.i | 84 ++++++++++++++++++++++++++++ SWIG/Examples/java/typemap/main.java | 27 +++++++++ 4 files changed, 135 insertions(+) create mode 100644 SWIG/Examples/java/typemap/Makefile create mode 100644 SWIG/Examples/java/typemap/README create mode 100644 SWIG/Examples/java/typemap/example.i create mode 100644 SWIG/Examples/java/typemap/main.java diff --git a/SWIG/Examples/java/typemap/Makefile b/SWIG/Examples/java/typemap/Makefile new file mode 100644 index 000000000..0826bbd7f --- /dev/null +++ b/SWIG/Examples/java/typemap/Makefile @@ -0,0 +1,16 @@ +TOP = ../.. +SWIG = $(TOP)/../swig +SRCS = +TARGET = libexample +INTERFACE = example.i +SWIGOPT = + +all:: java + +java:: + $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \ + SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' java + +clean:: + rm -f *_wrap* *.o core *~ *.so *.class example.java + diff --git a/SWIG/Examples/java/typemap/README b/SWIG/Examples/java/typemap/README new file mode 100644 index 000000000..67dabbfdc --- /dev/null +++ b/SWIG/Examples/java/typemap/README @@ -0,0 +1,8 @@ +Example of a typemap to handle return values in char * arguments. + +type: +make +javac *.java +export LD_LIBRARY_PATH=. # sh +setenv LD_LIBRARY_PATH . # csh +java main diff --git a/SWIG/Examples/java/typemap/example.i b/SWIG/Examples/java/typemap/example.i new file mode 100644 index 000000000..46c137431 --- /dev/null +++ b/SWIG/Examples/java/typemap/example.i @@ -0,0 +1,84 @@ +/* File : example.i */ +%module example +%{ +/* Put headers and other declarations here */ + +/* + example of a function that returns a value in the char * argument + normally used like: + + char buf[bigenough]; + f1(buf); +*/ + +void f1(char *s) { + if(s != NULL) { + sprintf(s, "hello world"); + } +} + +void f2(char *s) { + return f1(s); +} + +void f3(char *s) { + return f1(s); +} + +%} + +/* default behaviour is that of input arg, besides java cannot return + a value in a string argument +*/ +void f1(char *s); + +%include typemaps.i + +/* use the BYTE typemap to get around this, but the resulting code is ugly */ +void f2(char *BYTE); + +/* make a StringBuffer typemap to handle this case */ + +/* what type to use in java source code */ +%typemap(java,jtype) char *SBUF {StringBuffer} + +/* what is the corresponding jni type */ +%typemap(java,jni) char *SBUF {jobject} + +/* how to convert java type to requested c type */ +%typemap(java,in) char *SBUF { + jclass sbufClass; + jmethodID toStringID; + jmethodID setLengthID; + jstring js; + + $target = NULL; + if($source != NULL) { + /* get the String from the StringBuffer */ + sbufClass = (*jenv)->GetObjectClass(jenv, $source); + toStringID = (*jenv)->GetMethodID(jenv, sbufClass, "toString", "()Ljava/lang/String;"); + js = (jstring) (*jenv)->CallObjectMethod(jenv, $source, toStringID); + /* convert the String to a char * */ + $target = (char *)(*jenv)->GetStringUTFChars(jenv, js, 0); + /* zero the original StringBuffer, so we can replace it with the result */ + setLengthID = (*jenv)->GetMethodID(jenv, sbufClass, "setLength", "(I)V"); + (*jenv)->CallVoidMethod(jenv, $source, setLengthID, (jint) 0); + } +} + +/* how to convert the c type to the java type */ +%typemap(java,argout) char *SBUF { + jclass sbufClass; + jmethodID appendStringID; + + if($target != NULL) { + /* append the result to the empty StringBuffer */ + sbufClass = (*jenv)->GetObjectClass(jenv, $source); + appendStringID = (*jenv)->GetMethodID(jenv, sbufClass, "append", "(Ljava/lang/String;)Ljava/lang/StringBuffer;"); + (*jenv)->CallObjectMethod(jenv, $source, appendStringID, (*jenv)->NewStringUTF(jenv, $target)); + if($source != NULL) (*jenv)->ReleaseStringUTFChars(jenv, $source, $target); + } +} + +/* apply the new typemap to our function */ +void f3(char *SBUF); diff --git a/SWIG/Examples/java/typemap/main.java b/SWIG/Examples/java/typemap/main.java new file mode 100644 index 000000000..f7da20cdc --- /dev/null +++ b/SWIG/Examples/java/typemap/main.java @@ -0,0 +1,27 @@ +import example; + +public class main { + + static { + try { + System.loadLibrary("example"); + } catch (UnsatisfiedLinkError e) { + System.err.println("Cannot load the example native code.\nMake sure your LD_LIBRARY_PATH contains \'.\'\n" + e); + System.exit(1); + } + } + + public static void main(String argv[]) { + String s = "brave new world"; + example.f1(s); + System.out.println("f1(String): " + s); + + byte b[] = new byte[25]; + example.f2(b); + System.out.println("f2(byte[]): " + new String(b)); + + StringBuffer sb = new StringBuffer(20); + example.f3(sb); + System.out.println("f3(StringBuffer): " + sb); + } +}