added a typemap example for java
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@334 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
94119a92bb
commit
75f7e7b2ce
4 changed files with 135 additions and 0 deletions
16
Examples/java/typemap/Makefile
Normal file
16
Examples/java/typemap/Makefile
Normal file
|
|
@ -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
|
||||
|
||||
8
Examples/java/typemap/README
Normal file
8
Examples/java/typemap/README
Normal file
|
|
@ -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
|
||||
84
Examples/java/typemap/example.i
Normal file
84
Examples/java/typemap/example.i
Normal file
|
|
@ -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);
|
||||
27
Examples/java/typemap/main.java
Normal file
27
Examples/java/typemap/main.java
Normal file
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue