contrib/jwrapper: intro a practical example use of jwrapper
authorAlexis Laferrière <alexis.laf@xymus.net>
Thu, 23 Jul 2015 16:52:52 +0000 (12:52 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Fri, 24 Jul 2015 19:45:56 +0000 (15:45 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

contrib/jwrapper/Makefile
contrib/jwrapper/examples/queue/.gitignore [new file with mode: 0644]
contrib/jwrapper/examples/queue/Makefile [new file with mode: 0644]
contrib/jwrapper/examples/queue/Queue.java [new file with mode: 0644]
contrib/jwrapper/examples/queue/user_test.nit [new file with mode: 0644]
contrib/jwrapper/examples/queue/user_test.sav [new file with mode: 0644]

index a6aa956..0822be5 100644 (file)
@@ -30,6 +30,7 @@ check: bin/jwrapper tests/wildcards.javap
        ../../bin/nitpick -q tests/many.nit
        bin/jwrapper -v -u comment -o tests/wildcards.nit tests/wildcards.javap
        ../../bin/nitpick -q tests/wildcards.nit
+       make -C examples/queue/ check
 
 check-libs: bin/jwrapper
        # This config dependent rule must be tweaked according to each system
diff --git a/contrib/jwrapper/examples/queue/.gitignore b/contrib/jwrapper/examples/queue/.gitignore
new file mode 100644 (file)
index 0000000..5343079
--- /dev/null
@@ -0,0 +1,5 @@
+Queue.class
+queue.nit
+user_test
+user_test.res
+user_test.jar
diff --git a/contrib/jwrapper/examples/queue/Makefile b/contrib/jwrapper/examples/queue/Makefile
new file mode 100644 (file)
index 0000000..a07438c
--- /dev/null
@@ -0,0 +1,22 @@
+# Nit test program
+user_test: queue.nit $(shell ../../../../bin/nitls -M user_test.nit) ../../../../bin/nitc
+       CLASSPATH=`pwd` ../../../../bin/nitc user_test.nit
+
+       # Manually add our class file to the Jar for easy access
+       jar -uf user_test.jar Queue.class
+
+# Compiled Java class
+Queue.class: Queue.java
+       javac Queue.java
+
+# The Nit wrapper to the Java class
+queue.nit: Queue.class
+       ../../bin/jwrapper Queue.class -o queue.nit -p "Java" -i auto
+
+# Test
+check: user_test
+       # Execute test
+       ./user_test > user_test.res
+
+       # Compare the result with the expected
+       diff user_test.sav user_test.res
diff --git a/contrib/jwrapper/examples/queue/Queue.java b/contrib/jwrapper/examples/queue/Queue.java
new file mode 100644 (file)
index 0000000..daf0709
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+* This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+*/
+
+import java.util.*;
+
+public class Queue
+{
+    // function pointer
+    public native void printError( String errorMsg );
+
+    // internal list
+    private LinkedList<String> list;
+
+    public Queue()
+    {
+        list = new LinkedList<String>();
+    }
+
+    public void push( String element )
+    {
+        System.out.print( "From java, pushing " );
+        System.out.print( element );
+        System.out.print( "\n" );
+        list.addLast( element );
+    }
+
+    public String pop() // knows where is native printError
+    {
+        String element;
+
+        try
+        {
+            element = list.removeFirst();
+        }
+        catch ( NoSuchElementException e )
+        {
+            printError( "From java, empty queue." );
+            element = null;
+            throw e;
+        }
+
+        System.out.print( "From java, popping " );
+        System.out.print( element );
+        System.out.print( "\n" );
+
+        return element;
+    }
+}
diff --git a/contrib/jwrapper/examples/queue/user_test.nit b/contrib/jwrapper/examples/queue/user_test.nit
new file mode 100644 (file)
index 0000000..882a4d4
--- /dev/null
@@ -0,0 +1,23 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import queue
+
+var queue = new JavaQueue
+queue.push "one".to_java_string
+queue.push "two".to_java_string
+queue.push "tree".to_java_string
+print queue.pop
+print queue.pop
+print queue.pop
diff --git a/contrib/jwrapper/examples/queue/user_test.sav b/contrib/jwrapper/examples/queue/user_test.sav
new file mode 100644 (file)
index 0000000..3db08d4
--- /dev/null
@@ -0,0 +1,9 @@
+From java, pushing one
+From java, pushing two
+From java, pushing tree
+From java, popping one
+one
+From java, popping two
+two
+From java, popping tree
+tree