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 GetIntField(Object obj);
43 public static native Object GetObjectField(Object obj);
44 public static native int GetStaticIntField();
45 public static native Object GetStaticObjectField();
46 public static native void SetStaticIntField(int val);
47 public static native int GetIntField();
48 public static native int[] NewIntArray(int length);
49 public static native long[] NewLongArray(int length);
51 public static int jsfI = 0x123456;
52 public static Object jsfL = new Object();
55 public int jfI = 0x123456;
56 public Object jfL = new Object();
59 public static void main(String[] argv) {
60 System.loadLibrary("checkjni");
66 checkIsAssignableFrom();
69 checkPushLocalFrame();
71 checkGetObjectClass();
77 void checkIsAssignableFrom() {
78 p("IsAssignableFrom:");
80 Class sub = Integer.class;
81 Class sup = Object.class;
83 equal(IsAssignableFrom(sup, sup), true);
84 equal(IsAssignableFrom(sub, sup), true);
85 equal(IsAssignableFrom(sup, sub), false);
88 void checkIsInstanceOf() {
91 Object obj = new Object();
92 Object obj2 = new Integer(1);
93 Class clazz = Object.class;
94 Class clazz2 = Integer.class;
96 equal(IsInstanceOf(obj, clazz), true);
97 equal(IsInstanceOf(obj2, clazz), true);
98 equal(IsInstanceOf(obj, clazz2), false);
101 void checkIsSameObject() {
104 Object obj1 = new Object();
105 Object obj2 = new Integer(1);
106 Class clazz = Object.class;
108 equal(IsSameObject(obj1, obj1), true);
109 equal(IsSameObject(clazz, clazz), true);
110 equal(IsSameObject(null, null), true);
111 equal(IsSameObject(obj1, obj2), false);
112 equal(IsSameObject(obj1, clazz), false);
113 equal(IsSameObject(obj1, null), false);
116 void checkPushLocalFrame() {
117 p("PushLocalFrame:");
119 equal(PushLocalFrame(100), 0);
127 p("FAILED, no exception thrown");
128 } catch (Exception e) {
133 void checkGetObjectClass() {
134 p("GetObjectClass:");
136 Object obj1 = new Object();
137 Object obj2 = new Integer(1);
138 Class clazz1 = Object.class;
139 Class clazz2 = Integer.class;
141 equal(GetObjectClass(obj1), clazz1);
142 equal(GetObjectClass(obj2), clazz2);
143 equal(GetObjectClass(null), null);
146 void checkNewString() {
149 equal(NewString(2), "Test String from JNI with UTF");
155 equal(GetStaticIntField(), jsfI);
156 equal(GetStaticObjectField(), jsfL);
158 checkjniobj o = new checkjniobj();
160 equal(GetIntField(o), o.jfI);
161 equal(GetObjectField(o), o.jfL);
163 SetStaticIntField(0xABCDEF); equal(jsfI, 0xABCDEF);
172 int[] aI = NewIntArray(10);
173 for (i = 0, result = true; i < aI.length; i++) result &= (aI[i] == i);
175 p("PASS, size=" + aI.length);
179 long[] aL = NewLongArray(20);
180 for (i = 0, result = true; i < aL.length; i++) result &= (aL[i] == i);
182 p("PASS, size=" + aL.length);
187 void equal(boolean a, boolean b) {
194 void equal(int a, int b) {
198 p("FAILED ("+a+"!="+b+")");
201 void equal(Object a, Object b) {
208 void equal(String a, String b) {
212 p("FAILED ("+a+"!="+b+")");
216 System.out.println(s);