summaryrefslogtreecommitdiff
path: root/Examples/java/native
diff options
context:
space:
mode:
authorLorry Tar Creator <lorry-tar-importer@baserock.org>2009-08-18 20:56:02 +0000
committerLorry <lorry@roadtrain.codethink.co.uk>2012-09-25 16:59:08 +0000
commit9f8a09ed743cedd9547bf0661d518647966ab114 (patch)
tree9c7803d3b27a8ec22e91792ac7f7932efa128b20 /Examples/java/native
downloadswig-tarball-master.tar.gz
Imported from /srv/lorry/lorry-area/swig-tarball/swig-1.3.40.tar.gz.HEADswig-1.3.40master
Diffstat (limited to 'Examples/java/native')
-rw-r--r--Examples/java/native/Makefile18
-rw-r--r--Examples/java/native/example.i56
-rw-r--r--Examples/java/native/index.html33
-rw-r--r--Examples/java/native/runme.java19
4 files changed, 126 insertions, 0 deletions
diff --git a/Examples/java/native/Makefile b/Examples/java/native/Makefile
new file mode 100644
index 0000000..92afbd4
--- /dev/null
+++ b/Examples/java/native/Makefile
@@ -0,0 +1,18 @@
+TOP = ../..
+SWIG = $(TOP)/../preinst-swig
+SRCS =
+TARGET = example
+INTERFACE = example.i
+SWIGOPT =
+
+all:: java
+
+java::
+ $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
+ SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' java
+ javac *.java
+
+clean::
+ $(MAKE) -f $(TOP)/Makefile java_clean
+
+check: all
diff --git a/Examples/java/native/example.i b/Examples/java/native/example.i
new file mode 100644
index 0000000..851b6fd
--- /dev/null
+++ b/Examples/java/native/example.i
@@ -0,0 +1,56 @@
+/* File : example.i */
+%module example
+
+%{
+#include <string.h>
+
+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;
+}
+
+static char *point_toString(char *format, Point *p) {
+ static char buf[80];
+
+ sprintf(buf, format, p->x, p->y);
+
+ return buf;
+}
+
+/* this function will be wrapped by SWIG */
+char *point_toString1(Point *p) {
+ return point_toString("(%d,%d)", p);
+}
+
+/* this one we wrapped manually*/
+JNIEXPORT jstring JNICALL Java_exampleJNI_point_1toString2(JNIEnv *jenv, jclass jcls, jlong jpoint) {
+ Point * p;
+ jstring result;
+
+ (void)jcls;
+
+ p = *(Point **)&jpoint;
+
+ result = (*jenv)->NewStringUTF(jenv, point_toString("[%d,%d]", p));
+
+ return result;
+}
+%}
+
+
+Point *point_create(int x, int y);
+char *point_toString1(Point *p);
+
+/* give access to free() for memory cleanup of the malloc'd Point */
+extern void free(void *memblock);
+
+%native(point_toString2) char *point_toString2(Point *p);
diff --git a/Examples/java/native/index.html b/Examples/java/native/index.html
new file mode 100644
index 0000000..1ca51c1
--- /dev/null
+++ b/Examples/java/native/index.html
@@ -0,0 +1,33 @@
+<html>
+<head>
+<title>SWIG:Examples:java:native</title>
+</head>
+
+<body bgcolor="#ffffff">
+
+
+<tt>SWIG/Examples/java/native/</tt>
+<hr>
+
+<H2>SWIG wrapped and manually wrapped functions in Java</H2>
+
+Click <a href="../../../Doc/Manual/Java.html#using_own_jni_functions">here</a> for the relevant section in the SWIG and Java documentation.
+<p>
+This example compares wrapping a c global function using the manual way and the SWIG way.
+</p>
+
+<ul>
+<li><a href="example.i">example.i</a>. Interface file comparing code wrapped by SWIG and wrapped manually.
+<li><a href="runme.java">runme.java</a>. Sample Java program showing calls to both manually wrapped and SWIG wrapped c functions.
+</ul>
+
+<h2>Notes</h2>
+
+<ul>
+<li>SWIG writes all the awkward JNI code for you. You just have to tell SWIG which functions to wrap.
+<li>If memory is allocated in c it needs to be free'd. A function, such as free(), can be provided with access from Java to free the memory.
+</ul>
+
+<hr>
+</body>
+</html>
diff --git a/Examples/java/native/runme.java b/Examples/java/native/runme.java
new file mode 100644
index 0000000..e9a18b2
--- /dev/null
+++ b/Examples/java/native/runme.java
@@ -0,0 +1,19 @@
+
+public class runme {
+
+ static {
+ try {
+ System.loadLibrary("example");
+ } catch (UnsatisfiedLinkError e) {
+ System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e);
+ System.exit(1);
+ }
+ }
+
+ public static void main(String argv[]) {
+ SWIGTYPE_p_Point p = example.point_create(1, 2);
+ System.out.println("auto wrapped : " + example.point_toString1(p));
+ System.out.println("manual wrapped: " + example.point_toString2(p));
+ example.free(new SWIGTYPE_p_void(SWIGTYPE_p_Point.getCPtr(p), false)); //clean up c allocated memory
+ }
+}