1 /* //device/libs/android_runtime/BindTest.cpp
3 ** Copyright 2006, The Android Open Source Project
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
9 ** http://www.apache.org/licenses/LICENSE-2.0
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
20 #include <jam-public.h>
22 static u4 offset_instanceString;
23 static FieldBlock *fb_classString = NULL;
24 static Class *class_ReturnedObject = NULL;
25 static MethodBlock *mb_ReturnedObject_setReturnedString = NULL;
26 static MethodBlock *mb_Java_Lang_Object_Equals = NULL;
28 static u4 offset_mObj;
29 static u4 offset_mBool;
30 static u4 offset_mInt;
31 static u4 offset_mString;
32 static u4 offset_mDouble;
33 static u4 offset_mLong;
36 /* native String getString(); */
38 getString(Class *clazz, MethodBlock *mb, uintptr_t *ostack)
40 RETURN_OBJ (createString ("String"));
43 /* native String getNullString(); */
45 getNullString(Class *clazz, MethodBlock *mb, uintptr_t *ostack)
47 RETURN_OBJ (createString (NULL));
50 /* native String getBooleanTrue(); */
52 getBooleanTrue(Class *clazz, MethodBlock *mb, uintptr_t *ostack)
54 RETURN_BOOLEAN (TRUE);
57 /* native String getBooleanFalse(); */
59 getBooleanFalse(Class *clazz, MethodBlock *mb, uintptr_t *ostack)
61 RETURN_BOOLEAN (FALSE);
64 /* native Object nonvoidThrowsException() */
66 nonvoidThrowsException (Class *clazz, MethodBlock *mb, uintptr_t *ostack)
69 signalException("java/lang/NullPointerException", NULL);
78 /* native void setInstanceString(String s); */
80 setInstanceString (Class *clazz, MethodBlock *mb, uintptr_t *ostack)
82 Object *jthis = (Object *) ostack[0];
84 JOBJ_set_obj(jthis, offset_instanceString, ostack[1]);
89 /* native void setClassString(String s) */
91 setClassString (Class *clazz, MethodBlock *mb, uintptr_t *ostack)
93 // Object *jthis = (Object *) ostack[0];
95 fb_classString->static_value = ostack[1];
100 /* native String makeStringFromThreeChars(char a, char b, char c); */
102 makeStringFromThreeChars (Class *clazz, MethodBlock *mb, uintptr_t *ostack)
104 // Object *jthis = ostack[0];
105 char a = (char) ostack[1];
106 char b = (char) ostack[2];
107 char c = (char) ostack[3];
116 RETURN_OBJ(createString(str));
119 /* native ReturnedObject makeReturnedObject(String a); */
121 makeReturnedObject (Class *clazz, MethodBlock *mb, uintptr_t *ostack)
123 //Object *jthis = (Object*)ostack[0];
124 Object *a = (Object*)ostack[1];
128 ret = allocObject(class_ReturnedObject);
130 executeMethod(ret, mb_ReturnedObject_setReturnedString, a);
135 /* native double addDoubles(double a, double b); */
137 addDoubles (Class *clazz, MethodBlock *mb, uintptr_t *ostack)
139 //Object *jthis = (Object*)ostack[0];
140 double a = JARG_get_double(1);
141 double b = JARG_get_double(3);
146 /* native void setAll (Object obj, boolean bool, int i, String str, double d, long l) */
148 setAll (Class *clazz, MethodBlock *mb, uintptr_t *ostack)
150 Object *jthis = JARG_get_obj(0);
152 Object *obj = JARG_get_obj(1);
153 bool b = JARG_get_bool(2);
154 int i = JARG_get_int(3);
155 char *str = JARG_get_cstr_strdup(4);
156 double d = JARG_get_double(5);
157 long long ll = JARG_get_long_long(5+2);
159 JOBJ_set_obj(jthis, offset_mObj, obj);
160 JOBJ_set_bool(jthis, offset_mBool, b);
161 JOBJ_set_int(jthis, offset_mInt, i);
162 JOBJ_set_cstr(jthis, offset_mString, str);
165 JOBJ_set_double(jthis, offset_mDouble, d);
166 JOBJ_set_long_long(jthis, offset_mLong, ll);
171 /* native void compareAll (Object obj, boolean bool, int i, String str, double d, long l) */
173 compareAll (Class *clazz, MethodBlock *mb, uintptr_t *ostack)
175 Object *jthis = JARG_get_obj(0);
177 Object *obj = JARG_get_obj(1);
178 bool b = JARG_get_bool(2);
179 int i = JARG_get_int(3);
180 Object *strObj = JARG_get_obj(4);
181 double d = JARG_get_double(5);
182 long long ll = JARG_get_long_long(5+2);
188 Object *mStringObj = JOBJ_get_obj(jthis, offset_mString);
190 char *s = JARG_get_cstr_strdup(4);
192 result = executeMethod (strObj, lookupVirtualMethod(strObj,mb_Java_Lang_Object_Equals),
193 JOBJ_get_obj(jthis, offset_mString));
195 if (exceptionOccurred()) {
199 ret = (*(uintptr_t *)result != 0)
200 && (obj == JOBJ_get_obj(jthis, offset_mObj))
201 && (b == JOBJ_get_bool(jthis, offset_mBool))
202 && (i == JOBJ_get_int(jthis, offset_mInt))
203 && (d == JOBJ_get_double(jthis, offset_mDouble))
204 && (ll == JOBJ_get_long_long(jthis, offset_mLong));
210 static VMMethod methods[] = {
211 {"getString", getString},
212 {"getNullString", getNullString},
213 {"getBooleanTrue", getBooleanTrue},
214 {"getBooleanFalse", getBooleanFalse},
215 {"nonvoidThrowsException", nonvoidThrowsException},
216 {"setInstanceString", setInstanceString},
217 {"setClassString", setClassString},
218 {"makeStringFromThreeChars", makeStringFromThreeChars},
219 {"makeReturnedObject", makeReturnedObject},
220 {"addDoubles", addDoubles},
222 {"compareAll", compareAll},
227 void register_BindTest()
229 jamvm_registerClass("BindTest", methods);
233 clazz = findClassFromClassLoader("BindTest", getSystemClassLoader());
236 fprintf(stderr, "Error: BindTest not found\n");
243 fb = findField(clazz, "instanceString", "Ljava/lang/String;");
245 if (fb == NULL || ((fb->access_flags & ACC_STATIC) == ACC_STATIC)) {
246 fprintf(stderr, "Error: BindTest.instanceString not found or error\n");
250 offset_instanceString = fb->offset;
252 fb_classString = findField(clazz, "classString", "Ljava/lang/String;");
254 if (fb_classString == NULL || ((fb_classString->access_flags & ACC_STATIC) != ACC_STATIC)) {
255 fprintf(stderr, "Error: BindTest.classString not found or error\n");
260 class_ReturnedObject = findClassFromClassLoader("ReturnedObject", getSystemClassLoader());
262 if (class_ReturnedObject == NULL) {
263 fprintf(stderr, "Error: ReturnedObject class not found or error\n");
267 mb_ReturnedObject_setReturnedString=
268 findMethod (class_ReturnedObject, "setReturnedString", "(Ljava/lang/String;)V");
270 if (mb_ReturnedObject_setReturnedString == NULL) {
271 fprintf(stderr, "Error: ReturnedObject.setReturnedString class not found or error\n");
275 offset_mObj = findField(clazz, "mObj", "Ljava/lang/Object;")->offset;
276 offset_mBool = findField(clazz, "mBool", "Z" )->offset;
277 offset_mInt = findField(clazz, "mInt", "I")->offset;
278 offset_mString = findField(clazz, "mString", "Ljava/lang/String;")->offset;
279 offset_mDouble = findField(clazz, "mDouble", "D")->offset;
280 offset_mLong = findField(clazz, "mLong", "J")->offset;
283 mb_Java_Lang_Object_Equals = findMethod (
284 findClassFromClassLoader("java/lang/Object", getSystemClassLoader()),
285 "equals", "(Ljava/lang/Object;)Z");