1 /* src/tests/native/checkjni.java - for testing JNI related stuff
3 Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 Contact: cacao@cacaojvm.org
27 Authors: Christian Thalinger
34 public class checkjni {
35 public static native boolean IsAssignableFrom(Class sub, Class sup);
36 public static native boolean IsInstanceOf(Object obj, Class clazz);
37 public static native boolean IsSameObject(Object obj1, Object obj2);
38 public static native int PushLocalFrame(int capacity);
39 public static native void Throw() throws Exception;
40 public static native Class GetObjectClass(Object obj);
41 public static native String NewString(int type);
42 public static native int GetStaticIntField();
43 public static native Object GetStaticObjectField();
44 public static native void SetStaticIntField(int val);
45 public static native int GetIntField();
46 public static native int[] NewIntArray(int length);
47 public static native long[] NewLongArray(int length);
49 public static int jsfI = 0x123456;
50 public static Object jsfL = new Object();
52 public static void main(String[] argv) {
53 System.loadLibrary("checkjni");
59 checkIsAssignableFrom();
62 checkPushLocalFrame();
64 checkGetObjectClass();
70 void checkIsAssignableFrom() {
71 p("IsAssignableFrom:");
73 Class sub = Integer.class;
74 Class sup = Object.class;
76 equal(IsAssignableFrom(sup, sup), true);
77 equal(IsAssignableFrom(sub, sup), true);
78 equal(IsAssignableFrom(sup, sub), false);
81 void checkIsInstanceOf() {
84 Object obj = new Object();
85 Object obj2 = new Integer(1);
86 Class clazz = Object.class;
87 Class clazz2 = Integer.class;
89 equal(IsInstanceOf(obj, clazz), true);
90 equal(IsInstanceOf(obj2, clazz), true);
91 equal(IsInstanceOf(obj, clazz2), false);
94 void checkIsSameObject() {
97 Object obj1 = new Object();
98 Object obj2 = new Integer(1);
99 Class clazz = Object.class;
101 equal(IsSameObject(obj1, obj1), true);
102 equal(IsSameObject(clazz, clazz), true);
103 equal(IsSameObject(null, null), true);
104 equal(IsSameObject(obj1, obj2), false);
105 equal(IsSameObject(obj1, clazz), false);
106 equal(IsSameObject(obj1, null), false);
109 void checkPushLocalFrame() {
110 p("PushLocalFrame:");
112 equal(PushLocalFrame(100), 0);
120 p("FAILED, no exception thrown");
121 } catch (Exception e) {
126 void checkGetObjectClass() {
127 p("GetObjectClass:");
129 Object obj1 = new Object();
130 Object obj2 = new Integer(1);
131 Class clazz1 = Object.class;
132 Class clazz2 = Integer.class;
134 equal(GetObjectClass(obj1), clazz1);
135 equal(GetObjectClass(obj2), clazz2);
136 equal(GetObjectClass(null), null);
139 void checkNewString() {
142 equal(NewString(2), "Test String from JNI with UTF");
148 equal(GetStaticIntField(), jsfI);
149 equal(GetStaticObjectField(), jsfL);
151 SetStaticIntField(0xABCDEF); equal(jsfI, 0xABCDEF);
160 int[] aI = NewIntArray(10);
161 for (i = 0, result = true; i < aI.length; i++) result &= (aI[i] == i);
163 p("PASS, size=" + aI.length);
167 long[] aL = NewLongArray(20);
168 for (i = 0, result = true; i < aL.length; i++) result &= (aL[i] == i);
170 p("PASS, size=" + aL.length);
175 void equal(boolean a, boolean b) {
182 void equal(int a, int b) {
186 p("FAILED ("+a+"!="+b+")");
189 void equal(Object a, Object b) {
196 void equal(String a, String b) {
200 p("FAILED ("+a+"!="+b+")");
204 System.out.println(s);