From 6efcd02ea9c677536c9c9eb852f06f4874176870 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexis=20Laferri=C3=A8re?= Date: Thu, 23 Jul 2015 12:52:52 -0400 Subject: [PATCH] contrib/jwrapper: intro a practical example use of jwrapper MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Alexis Laferrière --- contrib/jwrapper/Makefile | 1 + contrib/jwrapper/examples/queue/.gitignore | 5 ++ contrib/jwrapper/examples/queue/Makefile | 22 +++++++++ contrib/jwrapper/examples/queue/Queue.java | 61 +++++++++++++++++++++++++ contrib/jwrapper/examples/queue/user_test.nit | 23 ++++++++++ contrib/jwrapper/examples/queue/user_test.sav | 9 ++++ 6 files changed, 121 insertions(+) create mode 100644 contrib/jwrapper/examples/queue/.gitignore create mode 100644 contrib/jwrapper/examples/queue/Makefile create mode 100644 contrib/jwrapper/examples/queue/Queue.java create mode 100644 contrib/jwrapper/examples/queue/user_test.nit create mode 100644 contrib/jwrapper/examples/queue/user_test.sav diff --git a/contrib/jwrapper/Makefile b/contrib/jwrapper/Makefile index a6aa956..0822be5 100644 --- a/contrib/jwrapper/Makefile +++ b/contrib/jwrapper/Makefile @@ -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 index 0000000..5343079 --- /dev/null +++ b/contrib/jwrapper/examples/queue/.gitignore @@ -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 index 0000000..a07438c --- /dev/null +++ b/contrib/jwrapper/examples/queue/Makefile @@ -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 index 0000000..daf0709 --- /dev/null +++ b/contrib/jwrapper/examples/queue/Queue.java @@ -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 list; + + public Queue() + { + list = new LinkedList(); + } + + 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 index 0000000..882a4d4 --- /dev/null +++ b/contrib/jwrapper/examples/queue/user_test.nit @@ -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 index 0000000..3db08d4 --- /dev/null +++ b/contrib/jwrapper/examples/queue/user_test.sav @@ -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 -- 1.7.9.5