From f6f291ac352445261fc0e15d45d7d841a246fb23 Mon Sep 17 00:00:00 2001 From: Harco de Hilster Date: Sun, 5 Mar 2000 18:03:27 +0000 Subject: [PATCH] added example of mixing hand written jni code with swig git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@330 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- SWIG/Examples/java/native/Makefile | 17 +++++++++ SWIG/Examples/java/native/README | 6 ++++ SWIG/Examples/java/native/example.i | 54 +++++++++++++++++++++++++++++ SWIG/Examples/java/native/main.java | 19 ++++++++++ SWIG/Examples/java/simple/Makefile | 7 ++-- SWIG/Examples/java/simple/README | 7 ++++ 6 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 SWIG/Examples/java/native/Makefile create mode 100644 SWIG/Examples/java/native/README create mode 100644 SWIG/Examples/java/native/example.i create mode 100644 SWIG/Examples/java/native/main.java diff --git a/SWIG/Examples/java/native/Makefile b/SWIG/Examples/java/native/Makefile new file mode 100644 index 000000000..dfe05eb94 --- /dev/null +++ b/SWIG/Examples/java/native/Makefile @@ -0,0 +1,17 @@ +TOP = ../.. +SWIG = $(TOP)/../swig +SWIGOPT = +SRCS = +TARGET = libexample +INTERFACE = example.i + +all:: java + +java:: + $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \ + SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' java + +clean:: + rm -f *_wrap* example.java *.class *.o *~ .~* core *.so *.sl so_locations + + diff --git a/SWIG/Examples/java/native/README b/SWIG/Examples/java/native/README new file mode 100644 index 000000000..2b5bbcdb4 --- /dev/null +++ b/SWIG/Examples/java/native/README @@ -0,0 +1,6 @@ +type: +make +javac *.java +export LD_LIBRARY_PATH=. # sh +setenv LD_LIBRARY_PATH . # csh +java main diff --git a/SWIG/Examples/java/native/example.i b/SWIG/Examples/java/native/example.i new file mode 100644 index 000000000..331a2265b --- /dev/null +++ b/SWIG/Examples/java/native/example.i @@ -0,0 +1,54 @@ +/* File : example.i */ +%module example + +%{ +#include + +typedef struct point { + int x; + int y; +} Point; + + +Point *point_create(int x, int y) { + Point *p = (Point *) malloc(sizeof(Point)); + p->x = x; + p->y = y; + + return p; +} + +/* this function will be wrapped by jswig */ +char *point_toString1(Point *p) { + char buf[80]; + + sprintf(buf, "(%d,%d)", p->x, p->y); + + return strdup(buf); /* memory leak */ +} + +/* this one we wrapped manually*/ +JNIEXPORT jstring JNICALL Java_example_point_1toString2(JNIEnv *jenv, jclass jcls, jlong jpoint) { + Point * p; + char buf[80]; + jstring result; + + p = *(Point **)&jpoint; + sprintf(buf, "[%d,%d]", p->x, p->y); + + result = (*jenv)->NewStringUTF(jenv, buf); + + return result; +} +%} + + +Point *point_create(int x, int y); +char *point_toString1(Point *p); +/* + Use %new to free the memory returned by point_toString1 + + %new char *point_toString1(Point *p); +*/ + +%native(point_toString2) char *point_toString2(Point *p); diff --git a/SWIG/Examples/java/native/main.java b/SWIG/Examples/java/native/main.java new file mode 100644 index 000000000..444d53b58 --- /dev/null +++ b/SWIG/Examples/java/native/main.java @@ -0,0 +1,19 @@ +import example; + +public class main { + + static { + try { + System.loadLibrary("example"); + } catch (UnsatisfiedLinkError e) { + System.err.println("Cannot load the native code.\nMake sure your LD_LIBRARY_PATH contains \'.\'\n" + e); + System.exit(1); + } + } + + public static void main(String argv[]) { + long p = example.point_create(1, 2); + System.out.println("auto wrapped : " + example.point_toString1(p)); + System.out.println("manual wrapped: " + example.point_toString2(p)); + } +} diff --git a/SWIG/Examples/java/simple/Makefile b/SWIG/Examples/java/simple/Makefile index d62e5bfeb..87406e20e 100644 --- a/SWIG/Examples/java/simple/Makefile +++ b/SWIG/Examples/java/simple/Makefile @@ -4,10 +4,13 @@ SRCS = example.c TARGET = libexample INTERFACE = example.i SWIGOPT = -all:: + +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 *.pm myperl + rm -f *_wrap* *.o core *~ *.so example.java diff --git a/SWIG/Examples/java/simple/README b/SWIG/Examples/java/simple/README index 07e8da069..b52b7d7b0 100644 --- a/SWIG/Examples/java/simple/README +++ b/SWIG/Examples/java/simple/README @@ -1 +1,8 @@ Simple example from users manual. + +type: +make +javac *.java +export LD_LIBRARY_PATH=. # sh +setenv LD_LIBRARY_PATH . # csh +java main