Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "jni_internal.h" |
| 18 | #include "class_linker.h" |
| 19 | #include "object.h" |
| 20 | |
| 21 | #include "JniConstants.h" // Last to avoid problems with LOG redefinition. |
| 22 | |
| 23 | namespace art { |
| 24 | |
| 25 | namespace { |
| 26 | |
| 27 | jint Field_getFieldModifiers(JNIEnv* env, jobject jfield, jclass javaDeclaringClass, jint slot) { |
| 28 | return Decode<Object*>(env, jfield)->AsField()->GetAccessFlags() & kAccFieldFlagsMask; |
| 29 | } |
| 30 | |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame^] | 31 | // TODO: we'll need this for Method too. |
| 32 | bool VerifyObjectInClass(JNIEnv* env, Object* o, Class* c) { |
| 33 | if (o == NULL) { |
| 34 | jniThrowNullPointerException(env, "receiver for non-static field access was null"); |
| 35 | return false; |
| 36 | } |
| 37 | if (!o->InstanceOf(c)) { |
| 38 | std::string expectedClassName(PrettyDescriptor(c->GetDescriptor())); |
| 39 | std::string actualClassName(PrettyTypeOf(o)); |
| 40 | jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException", |
| 41 | "expected receiver of type %s, but got %s", |
| 42 | expectedClassName.c_str(), actualClassName.c_str()); |
| 43 | return false; |
| 44 | } |
| 45 | return true; |
| 46 | } |
| 47 | |
| 48 | /* |
| 49 | * Convert primitive, boxed data from "srcPtr" to "dstPtr". |
| 50 | * |
| 51 | * Section v2 2.6 lists the various conversions and promotions. We |
| 52 | * allow the "widening" and "identity" conversions, but don't allow the |
| 53 | * "narrowing" conversions. |
| 54 | * |
| 55 | * Allowed: |
| 56 | * byte to short, int, long, float, double |
| 57 | * short to int, long, float double |
| 58 | * char to int, long, float, double |
| 59 | * int to long, float, double |
| 60 | * long to float, double |
| 61 | * float to double |
| 62 | * Values of types byte, char, and short are "internally" widened to int. |
| 63 | * |
| 64 | * Returns the width in 32-bit words of the destination primitive, or |
| 65 | * -1 if the conversion is not allowed. |
| 66 | */ |
| 67 | bool ConvertPrimitiveValue(Class* src_class, Class* dst_class, const JValue& src, JValue& dst) { |
| 68 | Class::PrimitiveType srcType = src_class->GetPrimitiveType(); |
| 69 | Class::PrimitiveType dstType = dst_class->GetPrimitiveType(); |
| 70 | switch (dstType) { |
| 71 | case Class::kPrimBoolean: |
| 72 | case Class::kPrimChar: |
| 73 | case Class::kPrimByte: |
| 74 | if (srcType == dstType) { |
| 75 | dst.i = src.i; |
| 76 | return true; |
| 77 | } |
| 78 | break; |
| 79 | case Class::kPrimShort: |
| 80 | if (srcType == Class::kPrimByte || srcType == Class::kPrimShort) { |
| 81 | dst.i = src.i; |
| 82 | return true; |
| 83 | } |
| 84 | break; |
| 85 | case Class::kPrimInt: |
| 86 | if (srcType == Class::kPrimByte || srcType == Class::kPrimChar || |
| 87 | srcType == Class::kPrimShort || srcType == Class::kPrimInt) { |
| 88 | dst.i = src.i; |
| 89 | return true; |
| 90 | } |
| 91 | break; |
| 92 | case Class::kPrimLong: |
| 93 | if (srcType == Class::kPrimByte || srcType == Class::kPrimChar || |
| 94 | srcType == Class::kPrimShort || srcType == Class::kPrimInt) { |
| 95 | dst.j = src.i; |
| 96 | return true; |
| 97 | } else if (srcType == Class::kPrimLong) { |
| 98 | dst.j = src.j; |
| 99 | return true; |
| 100 | } |
| 101 | break; |
| 102 | case Class::kPrimFloat: |
| 103 | if (srcType == Class::kPrimByte || srcType == Class::kPrimChar || |
| 104 | srcType == Class::kPrimShort || srcType == Class::kPrimInt) { |
| 105 | dst.f = src.i; |
| 106 | return true; |
| 107 | } else if (srcType == Class::kPrimLong) { |
| 108 | dst.f = src.j; |
| 109 | return true; |
| 110 | } else if (srcType == Class::kPrimFloat) { |
| 111 | dst.i = src.i; |
| 112 | return true; |
| 113 | } |
| 114 | break; |
| 115 | case Class::kPrimDouble: |
| 116 | if (srcType == Class::kPrimByte || srcType == Class::kPrimChar || |
| 117 | srcType == Class::kPrimShort || srcType == Class::kPrimInt) { |
| 118 | dst.d = src.i; |
| 119 | return true; |
| 120 | } else if (srcType == Class::kPrimLong) { |
| 121 | dst.d = src.j; |
| 122 | return true; |
| 123 | } else if (srcType == Class::kPrimFloat) { |
| 124 | dst.d = src.f; |
| 125 | return true; |
| 126 | } else if (srcType == Class::kPrimDouble) { |
| 127 | dst.j = src.j; |
| 128 | return true; |
| 129 | } |
| 130 | break; |
| 131 | default: |
| 132 | break; |
| 133 | } |
| 134 | Thread::Current()->ThrowNewException("Ljava/lang/IllegalArgumentException;", |
| 135 | "invalid primitive conversion from %s to %s", |
| 136 | PrettyDescriptor(src_class->GetDescriptor()).c_str(), |
| 137 | PrettyDescriptor(dst_class->GetDescriptor()).c_str()); |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | bool UnboxPrimitive(JNIEnv* env, Object* o, Class* dst_class, JValue& unboxed_value) { |
| 142 | if (dst_class->GetPrimitiveType() == Class::kPrimNot) { |
| 143 | if (o != NULL && !o->InstanceOf(dst_class)) { |
| 144 | jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException", |
| 145 | "expected object of type %s, but got %s", |
| 146 | PrettyDescriptor(dst_class->GetDescriptor()).c_str(), |
| 147 | PrettyTypeOf(o).c_str()); |
| 148 | return false; |
| 149 | } |
| 150 | unboxed_value.l = o; |
| 151 | return true; |
| 152 | } else if (dst_class->GetPrimitiveType() == Class::kPrimVoid) { |
| 153 | Thread::Current()->ThrowNewException("Ljava/lang/IllegalArgumentException;", |
| 154 | "can't unbox to void"); |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | if (o == NULL) { |
| 159 | Thread::Current()->ThrowNewException("Ljava/lang/IllegalArgumentException;", |
| 160 | "null passed for boxed primitive type"); |
| 161 | return false; |
| 162 | } |
| 163 | |
| 164 | JValue boxed_value = { 0 }; |
| 165 | const String* src_descriptor = o->GetClass()->GetDescriptor(); |
| 166 | Class* src_class = NULL; |
| 167 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 168 | Field* primitive_field = o->GetClass()->GetIFields()->Get(0); |
| 169 | if (src_descriptor->Equals("Ljava/lang/Boolean;")) { |
| 170 | src_class = class_linker->FindPrimitiveClass('Z'); |
| 171 | boxed_value.z = primitive_field->GetBoolean(o); |
| 172 | } else if (src_descriptor->Equals("Ljava/lang/Byte;")) { |
| 173 | src_class = class_linker->FindPrimitiveClass('B'); |
| 174 | boxed_value.b = primitive_field->GetByte(o); |
| 175 | } else if (src_descriptor->Equals("Ljava/lang/Character;")) { |
| 176 | src_class = class_linker->FindPrimitiveClass('C'); |
| 177 | boxed_value.c = primitive_field->GetChar(o); |
| 178 | } else if (src_descriptor->Equals("Ljava/lang/Float;")) { |
| 179 | src_class = class_linker->FindPrimitiveClass('F'); |
| 180 | boxed_value.f = primitive_field->GetFloat(o); |
| 181 | } else if (src_descriptor->Equals("Ljava/lang/Double;")) { |
| 182 | src_class = class_linker->FindPrimitiveClass('D'); |
| 183 | boxed_value.d = primitive_field->GetDouble(o); |
| 184 | } else if (src_descriptor->Equals("Ljava/lang/Integer;")) { |
| 185 | src_class = class_linker->FindPrimitiveClass('I'); |
| 186 | boxed_value.i = primitive_field->GetInt(o); |
| 187 | } else if (src_descriptor->Equals("Ljava/lang/Long;")) { |
| 188 | src_class = class_linker->FindPrimitiveClass('J'); |
| 189 | boxed_value.j = primitive_field->GetLong(o); |
| 190 | } else if (src_descriptor->Equals("Ljava/lang/Short;")) { |
| 191 | src_class = class_linker->FindPrimitiveClass('S'); |
| 192 | boxed_value.s = primitive_field->GetShort(o); |
| 193 | } else { |
| 194 | Thread::Current()->ThrowNewException("Ljava/lang/IllegalArgumentException;", |
| 195 | "%s is not a boxed primitive type", PrettyDescriptor(src_descriptor).c_str()); |
| 196 | return false; |
| 197 | } |
| 198 | |
| 199 | return ConvertPrimitiveValue(src_class, dst_class, boxed_value, unboxed_value); |
| 200 | } |
| 201 | |
| 202 | Method* gBoolean_valueOf; |
| 203 | Method* gByte_valueOf; |
| 204 | Method* gCharacter_valueOf; |
| 205 | Method* gDouble_valueOf; |
| 206 | Method* gFloat_valueOf; |
| 207 | Method* gInteger_valueOf; |
| 208 | Method* gLong_valueOf; |
| 209 | Method* gShort_valueOf; |
| 210 | |
| 211 | void InitBoxingMethod(Method*& m, const char* class_descriptor, const char* method_signature) { |
| 212 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 213 | Class* c = class_linker->FindSystemClass(class_descriptor); |
| 214 | CHECK(c != NULL) << "Couldn't find boxing class: " << class_descriptor; |
| 215 | m = c->FindDirectMethod("valueOf", method_signature); |
| 216 | CHECK(m != NULL) << "Couldn't find boxing method: " << method_signature; |
| 217 | } |
| 218 | |
| 219 | void BoxPrimitive(JNIEnv* env, Class* src_class, JValue& value) { |
| 220 | if (!src_class->IsPrimitive()) { |
| 221 | return; |
| 222 | } |
| 223 | |
| 224 | Method* m = NULL; |
| 225 | UniquePtr<byte[]> args(new byte[8]); |
| 226 | memset(&args[0], 0, 8); |
| 227 | switch (src_class->GetPrimitiveType()) { |
| 228 | case Class::kPrimBoolean: |
| 229 | m = gBoolean_valueOf; |
| 230 | *reinterpret_cast<uint32_t*>(&args[0]) = value.z; |
| 231 | break; |
| 232 | case Class::kPrimByte: |
| 233 | m = gByte_valueOf; |
| 234 | *reinterpret_cast<uint32_t*>(&args[0]) = value.b; |
| 235 | break; |
| 236 | case Class::kPrimChar: |
| 237 | m = gCharacter_valueOf; |
| 238 | *reinterpret_cast<uint32_t*>(&args[0]) = value.c; |
| 239 | break; |
| 240 | case Class::kPrimDouble: |
| 241 | m = gDouble_valueOf; |
| 242 | *reinterpret_cast<double*>(&args[0]) = value.d; |
| 243 | break; |
| 244 | case Class::kPrimFloat: |
| 245 | m = gFloat_valueOf; |
| 246 | *reinterpret_cast<float*>(&args[0]) = value.f; |
| 247 | break; |
| 248 | case Class::kPrimInt: |
| 249 | m = gInteger_valueOf; |
| 250 | *reinterpret_cast<uint32_t*>(&args[0]) = value.i; |
| 251 | break; |
| 252 | case Class::kPrimLong: |
| 253 | m = gLong_valueOf; |
| 254 | *reinterpret_cast<uint64_t*>(&args[0]) = value.j; |
| 255 | break; |
| 256 | case Class::kPrimShort: |
| 257 | m = gShort_valueOf; |
| 258 | *reinterpret_cast<uint32_t*>(&args[0]) = value.s; |
| 259 | break; |
| 260 | default: |
| 261 | LOG(FATAL) << PrettyClass(src_class); |
| 262 | } |
| 263 | |
| 264 | Thread* self = Thread::Current(); |
| 265 | ScopedThreadStateChange tsc(self, Thread::kRunnable); |
| 266 | m->Invoke(self, NULL, args.get(), &value); |
| 267 | } |
| 268 | |
| 269 | bool GetFieldValue(Object* o, Field* f, JValue& value, bool allow_references) { |
| 270 | switch (f->GetType()->GetPrimitiveType()) { |
| 271 | case Class::kPrimBoolean: |
| 272 | value.z = f->GetBoolean(o); |
| 273 | return true; |
| 274 | case Class::kPrimByte: |
| 275 | value.b = f->GetByte(o); |
| 276 | return true; |
| 277 | case Class::kPrimChar: |
| 278 | value.c = f->GetChar(o); |
| 279 | return true; |
| 280 | case Class::kPrimDouble: |
| 281 | value.d = f->GetDouble(o); |
| 282 | return true; |
| 283 | case Class::kPrimFloat: |
| 284 | value.f = f->GetFloat(o); |
| 285 | return true; |
| 286 | case Class::kPrimInt: |
| 287 | value.i = f->GetInt(o); |
| 288 | return true; |
| 289 | case Class::kPrimLong: |
| 290 | value.j = f->GetLong(o); |
| 291 | return true; |
| 292 | case Class::kPrimShort: |
| 293 | value.s = f->GetShort(o); |
| 294 | return true; |
| 295 | case Class::kPrimNot: |
| 296 | if (allow_references) { |
| 297 | value.l = f->GetObject(o); |
| 298 | return true; |
| 299 | } |
| 300 | // Else break to report an error. |
| 301 | break; |
| 302 | case Class::kPrimVoid: |
| 303 | // Never okay. |
| 304 | break; |
| 305 | } |
| 306 | Thread::Current()->ThrowNewException("Ljava/lang/IllegalArgumentException;", |
| 307 | "Not a primitive field: %s", PrettyField(f).c_str()); |
| 308 | return false; |
| 309 | } |
| 310 | |
| 311 | JValue GetPrimitiveField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jchar targetDescriptor) { |
| 312 | Field* f = reinterpret_cast<Field*>(env->FromReflectedField(javaField)); |
| 313 | |
| 314 | // Check that the receiver is non-null and an instance of the field's declaring class. |
| 315 | Object* o = Decode<Object*>(env, javaObj); |
| 316 | bool isStatic = (javaObj == NULL); |
| 317 | if (!isStatic) { |
| 318 | Class* declaringClass = Decode<Class*>(env, javaDeclaringClass); |
| 319 | if (!VerifyObjectInClass(env, o, declaringClass)) { |
| 320 | return JValue(); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | // Read the value. |
| 325 | JValue field_value; |
| 326 | if (!GetFieldValue(o, f, field_value, false)) { |
| 327 | return JValue(); |
| 328 | } |
| 329 | |
| 330 | // Widen it if necessary (and possible). |
| 331 | JValue wide_value; |
| 332 | Class* targetType = Runtime::Current()->GetClassLinker()->FindPrimitiveClass(targetDescriptor); |
| 333 | if (!ConvertPrimitiveValue(f->GetType(), targetType, field_value, wide_value)) { |
| 334 | return JValue(); |
| 335 | } |
| 336 | return wide_value; |
| 337 | } |
| 338 | |
| 339 | jbyte Field_getBField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar targetDescriptor) { |
| 340 | return GetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, targetDescriptor).b; |
| 341 | } |
| 342 | |
| 343 | jchar Field_getCField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar targetDescriptor) { |
| 344 | return GetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, targetDescriptor).c; |
| 345 | } |
| 346 | |
| 347 | jdouble Field_getDField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar targetDescriptor) { |
| 348 | return GetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, targetDescriptor).d; |
| 349 | } |
| 350 | |
| 351 | jfloat Field_getFField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar targetDescriptor) { |
| 352 | return GetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, targetDescriptor).f; |
| 353 | } |
| 354 | |
| 355 | jint Field_getIField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar targetDescriptor) { |
| 356 | return GetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, targetDescriptor).i; |
| 357 | } |
| 358 | |
| 359 | jlong Field_getJField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar targetDescriptor) { |
| 360 | return GetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, targetDescriptor).j; |
| 361 | } |
| 362 | |
| 363 | jshort Field_getSField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar targetDescriptor) { |
| 364 | return GetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, targetDescriptor).s; |
| 365 | } |
| 366 | |
| 367 | jboolean Field_getZField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar targetDescriptor) { |
| 368 | return GetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, targetDescriptor).z; |
| 369 | } |
| 370 | |
| 371 | void SetFieldValue(Object* o, Field* f, const JValue& new_value, bool allow_references) { |
| 372 | switch (f->GetType()->GetPrimitiveType()) { |
| 373 | case Class::kPrimBoolean: |
| 374 | f->SetBoolean(o, new_value.z); |
| 375 | return; |
| 376 | case Class::kPrimByte: |
| 377 | f->SetByte(o, new_value.b); |
| 378 | return; |
| 379 | case Class::kPrimChar: |
| 380 | f->SetChar(o, new_value.c); |
| 381 | return; |
| 382 | case Class::kPrimDouble: |
| 383 | f->SetDouble(o, new_value.d); |
| 384 | return; |
| 385 | case Class::kPrimFloat: |
| 386 | f->SetFloat(o, new_value.f); |
| 387 | return; |
| 388 | case Class::kPrimInt: |
| 389 | f->SetInt(o, new_value.i); |
| 390 | return; |
| 391 | case Class::kPrimLong: |
| 392 | f->SetLong(o, new_value.j); |
| 393 | return; |
| 394 | case Class::kPrimShort: |
| 395 | f->SetShort(o, new_value.s); |
| 396 | return; |
| 397 | case Class::kPrimNot: |
| 398 | if (allow_references) { |
| 399 | f->SetObject(o, new_value.l); |
| 400 | return; |
| 401 | } |
| 402 | // Else break to report an error. |
| 403 | break; |
| 404 | case Class::kPrimVoid: |
| 405 | // Never okay. |
| 406 | break; |
| 407 | } |
| 408 | Thread::Current()->ThrowNewException("Ljava/lang/IllegalArgumentException;", |
| 409 | "Not a primitive field: %s", PrettyField(f).c_str()); |
| 410 | } |
| 411 | |
| 412 | void SetPrimitiveField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jchar targetDescriptor, const JValue& new_value) { |
| 413 | Field* f = reinterpret_cast<Field*>(env->FromReflectedField(javaField)); |
| 414 | |
| 415 | // Check that the receiver is non-null and an instance of the field's declaring class. |
| 416 | Object* o = Decode<Object*>(env, javaObj); |
| 417 | bool isStatic = (javaObj == NULL); |
| 418 | if (!isStatic) { |
| 419 | Class* declaringClass = Decode<Class*>(env, javaDeclaringClass); |
| 420 | if (!VerifyObjectInClass(env, o, declaringClass)) { |
| 421 | return; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | // Widen the value if necessary (and possible). |
| 426 | JValue wide_value; |
| 427 | Class* targetType = Runtime::Current()->GetClassLinker()->FindPrimitiveClass(targetDescriptor); |
| 428 | if (!ConvertPrimitiveValue(f->GetType(), targetType, new_value, wide_value)) { |
| 429 | return; |
| 430 | } |
| 431 | |
| 432 | // Write the value. |
| 433 | SetFieldValue(o, f, wide_value, false); |
| 434 | } |
| 435 | |
| 436 | void Field_setBField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar targetDescriptor, jbyte value) { |
| 437 | JValue v = { 0 }; |
| 438 | v.b = value; |
| 439 | SetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, targetDescriptor, v); |
| 440 | } |
| 441 | |
| 442 | void Field_setCField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar targetDescriptor, jchar value) { |
| 443 | JValue v = { 0 }; |
| 444 | v.c = value; |
| 445 | SetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, targetDescriptor, v); |
| 446 | } |
| 447 | |
| 448 | void Field_setDField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar targetDescriptor, jdouble value) { |
| 449 | JValue v = { 0 }; |
| 450 | v.d = value; |
| 451 | SetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, targetDescriptor, v); |
| 452 | } |
| 453 | |
| 454 | void Field_setFField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar targetDescriptor, jfloat value) { |
| 455 | JValue v = { 0 }; |
| 456 | v.f = value; |
| 457 | SetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, targetDescriptor, v); |
| 458 | } |
| 459 | |
| 460 | void Field_setIField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar targetDescriptor, jint value) { |
| 461 | JValue v = { 0 }; |
| 462 | v.i = value; |
| 463 | SetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, targetDescriptor, v); |
| 464 | } |
| 465 | |
| 466 | void Field_setJField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar targetDescriptor, jlong value) { |
| 467 | JValue v = { 0 }; |
| 468 | v.j = value; |
| 469 | SetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, targetDescriptor, v); |
| 470 | } |
| 471 | |
| 472 | void Field_setSField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar targetDescriptor, jshort value) { |
| 473 | JValue v = { 0 }; |
| 474 | v.s = value; |
| 475 | SetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, targetDescriptor, v); |
| 476 | } |
| 477 | |
| 478 | void Field_setZField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar targetDescriptor, jboolean value) { |
| 479 | JValue v = { 0 }; |
| 480 | v.z = value; |
| 481 | SetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, targetDescriptor, v); |
| 482 | } |
| 483 | |
| 484 | void Field_setField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jobject javaValue) { |
| 485 | Field* f = reinterpret_cast<Field*>(env->FromReflectedField(javaField)); |
| 486 | |
| 487 | // Unbox the value, if necessary. |
| 488 | Object* boxed_value = Decode<Object*>(env, javaValue); |
| 489 | JValue unboxed_value; |
| 490 | if (!UnboxPrimitive(env, boxed_value, f->GetType(), unboxed_value)) { |
| 491 | return; |
| 492 | } |
| 493 | |
| 494 | // Check that the receiver is non-null and an instance of the field's declaring class. |
| 495 | Object* o = Decode<Object*>(env, javaObj); |
| 496 | bool isStatic = (javaObj == NULL); |
| 497 | if (!isStatic) { |
| 498 | Class* declaringClass = Decode<Class*>(env, javaDeclaringClass); |
| 499 | if (!VerifyObjectInClass(env, o, declaringClass)) { |
| 500 | return; |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | SetFieldValue(o, f, unboxed_value, true); |
| 505 | } |
| 506 | |
| 507 | jobject Field_getField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean) { |
| 508 | Field* f = reinterpret_cast<Field*>(env->FromReflectedField(javaField)); |
| 509 | |
| 510 | // Check that the receiver is non-null and an instance of the field's declaring class. |
| 511 | Object* o = Decode<Object*>(env, javaObj); |
| 512 | bool isStatic = (javaObj == NULL); |
| 513 | if (!isStatic) { |
| 514 | Class* declaringClass = Decode<Class*>(env, javaDeclaringClass); |
| 515 | if (!VerifyObjectInClass(env, o, declaringClass)) { |
| 516 | return NULL; |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | // Get the field's value, boxing if necessary. |
| 521 | JValue value; |
| 522 | if (!GetFieldValue(o, f, value, true)) { |
| 523 | return NULL; |
| 524 | } |
| 525 | BoxPrimitive(env, f->GetType(), value); |
| 526 | |
| 527 | return AddLocalReference<jobject>(env, value.l); |
| 528 | } |
| 529 | |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 530 | static JNINativeMethod gMethods[] = { |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 531 | //NATIVE_METHOD(Field, getSignatureAnnotation, "(Ljava/lang/Class;I)[Ljava/lang/Object;"), |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame^] | 532 | NATIVE_METHOD(Field, getFieldModifiers, "(Ljava/lang/Class;I)I"), |
| 533 | |
| 534 | NATIVE_METHOD(Field, getBField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZC)B"), |
| 535 | NATIVE_METHOD(Field, getCField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZC)C"), |
| 536 | NATIVE_METHOD(Field, getDField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZC)D"), |
| 537 | NATIVE_METHOD(Field, getFField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZC)F"), |
| 538 | NATIVE_METHOD(Field, getField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZ)Ljava/lang/Object;"), |
| 539 | NATIVE_METHOD(Field, getIField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZC)I"), |
| 540 | NATIVE_METHOD(Field, getJField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZC)J"), |
| 541 | NATIVE_METHOD(Field, getSField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZC)S"), |
| 542 | NATIVE_METHOD(Field, getZField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZC)Z"), |
| 543 | NATIVE_METHOD(Field, setBField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZCB)V"), |
| 544 | NATIVE_METHOD(Field, setCField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZCC)V"), |
| 545 | NATIVE_METHOD(Field, setDField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZCD)V"), |
| 546 | NATIVE_METHOD(Field, setFField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZCF)V"), |
| 547 | NATIVE_METHOD(Field, setField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZLjava/lang/Object;)V"), |
| 548 | NATIVE_METHOD(Field, setIField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZCI)V"), |
| 549 | NATIVE_METHOD(Field, setJField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZCJ)V"), |
| 550 | NATIVE_METHOD(Field, setSField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZCS)V"), |
| 551 | NATIVE_METHOD(Field, setZField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZCZ)V"), |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 552 | }; |
| 553 | |
| 554 | } // namespace |
| 555 | |
| 556 | void register_java_lang_reflect_Field(JNIEnv* env) { |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame^] | 557 | InitBoxingMethod(gBoolean_valueOf, "Ljava/lang/Boolean;", "(Z)Ljava/lang/Boolean;"); |
| 558 | InitBoxingMethod(gByte_valueOf, "Ljava/lang/Byte;", "(B)Ljava/lang/Byte;"); |
| 559 | InitBoxingMethod(gCharacter_valueOf, "Ljava/lang/Character;", "(C)Ljava/lang/Character;"); |
| 560 | InitBoxingMethod(gDouble_valueOf, "Ljava/lang/Double;", "(D)Ljava/lang/Double;"); |
| 561 | InitBoxingMethod(gFloat_valueOf, "Ljava/lang/Float;", "(F)Ljava/lang/Float;"); |
| 562 | InitBoxingMethod(gInteger_valueOf, "Ljava/lang/Integer;", "(I)Ljava/lang/Integer;"); |
| 563 | InitBoxingMethod(gLong_valueOf, "Ljava/lang/Long;", "(J)Ljava/lang/Long;"); |
| 564 | InitBoxingMethod(gShort_valueOf, "Ljava/lang/Short;", "(S)Ljava/lang/Short;"); |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 565 | jniRegisterNativeMethods(env, "java/lang/reflect/Field", gMethods, NELEM(gMethods)); |
| 566 | } |
| 567 | |
| 568 | } // namespace art |