Patch: yet another JNI test
Tom Tromey
tromey@cygnus.com
Wed Feb 16 16:49:00 GMT 2000
Yet another JNI test case.
With this one I discovered that writing code to use JNI is far more
painful than actually implementing JNI.
2000-02-16 Tom Tromey <tromey@cygnus.com>
* libjava.jni/calls.c: New file.
* libjava.jni/calls.out: New file.
* libjava.jni/calls.java: New file.
Tom
Index: libjava.jni/calls.out
===================================================================
RCS file: calls.out
diff -N calls.out
--- /dev/null Tue May 5 13:32:27 1998
+++ calls.out Wed Feb 16 16:48:10 2000
@@ -0,0 +1 @@
+void
Index: libjava.jni/calls.c
===================================================================
RCS file: calls.c
diff -N calls.c
--- /dev/null Tue May 5 13:32:27 1998
+++ calls.c Wed Feb 16 16:48:10 2000
@@ -0,0 +1,69 @@
+#include <stdio.h>
+#include <calls.h>
+
+jint
+Java_calls_docall (JNIEnv *env, jobject _this)
+{
+ jmethodID method;
+ jclass klass, super;
+
+ jbyte b;
+ jshort s;
+ jchar c;
+ jint i;
+ jlong l;
+ jfloat f;
+ jdouble d;
+
+ jvalue val;
+
+ jint fails = 0;
+
+ klass = (*env)->GetObjectClass (env, _this);
+ super = (*env)->GetSuperclass (env, klass);
+
+ method = (*env)->GetMethodID (env, klass, "byte_f", "()B");
+ b = (*env)->CallByteMethod (env, _this, method);
+ if (b != 23)
+ ++fails;
+
+ method = (*env)->GetMethodID (env, klass, "char_f", "(I)C");
+ val.i = 10;
+ c = (*env)->CallCharMethodV (env, _this, method, &val);
+ if (c != ('a' + 10))
+ ++fails;
+
+ method = (*env)->GetMethodID (env, super, "int_f", "()I");
+ i = (*env)->CallNonvirtualIntMethod (env, _this, super, method);
+ if (i != 27)
+ ++fails;
+
+ i = (*env)->CallIntMethod (env, _this, method);
+ if (i != 1023)
+ ++fails;
+
+ method = (*env)->GetStaticMethodID (env, klass, "long_f", "(J)J");
+ l = (*env)->CallStaticLongMethod (env, klass, method, (jlong) 10);
+ if (l != 2033)
+ ++fails;
+
+ method = (*env)->GetMethodID (env, klass, "void_f", "()V");
+ (*env)->CallVoidMethod (env, _this, method);
+
+ method = (*env)->GetStaticMethodID (env, klass, "short_f", "()S");
+ s = (*env)->CallStaticShortMethod (env, klass, method);
+ if (s != 2)
+ ++fails;
+
+ method = (*env)->GetMethodID (env, klass, "double_f", "()D");
+ d = (*env)->CallDoubleMethod (env, _this, method);
+ if (d != -1.0)
+ ++fails;
+
+ method = (*env)->GetMethodID (env, klass, "float_f", "()F");
+ f = (*env)->CallFloatMethod (env, _this, method);
+ if (f != 1.0)
+ ++fails;
+
+ return fails;
+}
Index: libjava.jni/calls.java
===================================================================
RCS file: calls.java
diff -N calls.java
--- /dev/null Tue May 5 13:32:27 1998
+++ calls.java Wed Feb 16 16:48:10 2000
@@ -0,0 +1,66 @@
+// Test a bunch of different calls.
+
+class base
+{
+ public int int_f ()
+ {
+ return 27;
+ }
+}
+
+public class calls extends base
+{
+ static
+ {
+ System.loadLibrary ("calls");
+ }
+
+ public native int docall ();
+
+ public byte byte_f ()
+ {
+ return 23;
+ }
+
+ public char char_f (int z)
+ {
+ return (char) ('a' + z);
+ }
+
+ public int int_f ()
+ {
+ return 1023;
+ }
+
+ public static long long_f (long q)
+ {
+ return q + 2023;
+ }
+
+ public void void_f ()
+ {
+ System.out.println ("void");
+ }
+
+ public static short short_f ()
+ {
+ return 2;
+ }
+
+ public double double_f ()
+ {
+ return -1.0;
+ }
+
+ public float float_f ()
+ {
+ return (float) 1.0;
+ }
+
+ public static void main (String[] args)
+ {
+ calls c = new calls ();
+ if (c.docall () != 0)
+ System.out.println ("fail");
+ }
+}
More information about the Java-patches
mailing list