Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 "reflection.h" |
| 18 | |
| 19 | #include "class_linker.h" |
| 20 | #include "jni_internal.h" |
| 21 | #include "object.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame^] | 22 | #include "object_utils.h" |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 23 | |
| 24 | #include "JniConstants.h" // Last to avoid problems with LOG redefinition. |
| 25 | |
| 26 | namespace art { |
| 27 | |
| 28 | Method* gBoolean_valueOf; |
| 29 | Method* gByte_valueOf; |
| 30 | Method* gCharacter_valueOf; |
| 31 | Method* gDouble_valueOf; |
| 32 | Method* gFloat_valueOf; |
| 33 | Method* gInteger_valueOf; |
| 34 | Method* gLong_valueOf; |
| 35 | Method* gShort_valueOf; |
| 36 | |
Jesse Wilson | 9a6bae8 | 2011-11-14 14:57:30 -0500 | [diff] [blame] | 37 | void InitBoxingMethods() { |
| 38 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 39 | gBoolean_valueOf = class_linker->FindSystemClass("Ljava/lang/Boolean;")->FindDeclaredDirectMethod("valueOf", "(Z)Ljava/lang/Boolean;"); |
| 40 | gByte_valueOf = class_linker->FindSystemClass("Ljava/lang/Byte;")->FindDeclaredDirectMethod("valueOf", "(B)Ljava/lang/Byte;"); |
| 41 | gCharacter_valueOf = class_linker->FindSystemClass("Ljava/lang/Character;")->FindDeclaredDirectMethod("valueOf", "(C)Ljava/lang/Character;"); |
| 42 | gDouble_valueOf = class_linker->FindSystemClass("Ljava/lang/Double;")->FindDeclaredDirectMethod("valueOf", "(D)Ljava/lang/Double;"); |
| 43 | gFloat_valueOf = class_linker->FindSystemClass("Ljava/lang/Float;")->FindDeclaredDirectMethod("valueOf", "(F)Ljava/lang/Float;"); |
| 44 | gInteger_valueOf = class_linker->FindSystemClass("Ljava/lang/Integer;")->FindDeclaredDirectMethod("valueOf", "(I)Ljava/lang/Integer;"); |
| 45 | gLong_valueOf = class_linker->FindSystemClass("Ljava/lang/Long;")->FindDeclaredDirectMethod("valueOf", "(J)Ljava/lang/Long;"); |
| 46 | gShort_valueOf = class_linker->FindSystemClass("Ljava/lang/Short;")->FindDeclaredDirectMethod("valueOf", "(S)Ljava/lang/Short;"); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame^] | 49 | jobject InvokeMethod(JNIEnv* env, jobject javaMethod, jobject javaReceiver, jobject javaArgs) { |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 50 | Thread* self = Thread::Current(); |
| 51 | ScopedThreadStateChange tsc(self, Thread::kRunnable); |
| 52 | |
| 53 | jmethodID mid = env->FromReflectedMethod(javaMethod); |
| 54 | Method* m = reinterpret_cast<Method*>(mid); |
| 55 | |
| 56 | Class* declaring_class = m->GetDeclaringClass(); |
| 57 | if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(declaring_class, true)) { |
| 58 | return NULL; |
| 59 | } |
| 60 | |
| 61 | Object* receiver = NULL; |
| 62 | if (!m->IsStatic()) { |
| 63 | // Check that the receiver is non-null and an instance of the field's declaring class. |
| 64 | receiver = Decode<Object*>(env, javaReceiver); |
| 65 | if (!VerifyObjectInClass(env, receiver, declaring_class)) { |
| 66 | return NULL; |
| 67 | } |
| 68 | |
| 69 | // Find the actual implementation of the virtual method. |
| 70 | m = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(m); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 71 | mid = reinterpret_cast<jmethodID>(m); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | // Get our arrays of arguments and their types, and check they're the same size. |
| 75 | ObjectArray<Object>* objects = Decode<ObjectArray<Object>*>(env, javaArgs); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame^] | 76 | MethodHelper mh(m); |
| 77 | const DexFile::TypeList* classes = mh.GetParameterTypeList(); |
| 78 | uint32_t classes_size = classes == NULL ? 0 : classes->Size(); |
| 79 | uint32_t arg_count = (objects != NULL) ? objects->GetLength() : 0; |
| 80 | if (arg_count != classes_size) { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 81 | self->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;", |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 82 | "wrong number of arguments; expected %d, got %d", |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame^] | 83 | classes_size, arg_count); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 84 | return NULL; |
| 85 | } |
| 86 | |
| 87 | // Translate javaArgs to a jvalue[]. |
| 88 | UniquePtr<jvalue[]> args(new jvalue[arg_count]); |
| 89 | JValue* decoded_args = reinterpret_cast<JValue*>(args.get()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame^] | 90 | for (uint32_t i = 0; i < arg_count; ++i) { |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 91 | Object* arg = objects->Get(i); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame^] | 92 | Class* dst_class = mh.GetClassFromTypeIdx(classes->GetTypeItem(i).type_idx_); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 93 | if (dst_class->IsPrimitive()) { |
| 94 | if (!UnboxPrimitive(env, arg, dst_class, decoded_args[i])) { |
| 95 | return NULL; |
| 96 | } |
| 97 | } else { |
| 98 | args[i].l = AddLocalReference<jobject>(env, arg); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | // Invoke the method. |
| 103 | JValue value = InvokeWithJValues(env, javaReceiver, mid, args.get()); |
| 104 | |
| 105 | // Wrap any exception with "Ljava/lang/reflect/InvocationTargetException;" and return early. |
| 106 | if (self->IsExceptionPending()) { |
| 107 | jthrowable th = env->ExceptionOccurred(); |
| 108 | env->ExceptionClear(); |
| 109 | jclass exception_class = env->FindClass("java/lang/reflect/InvocationTargetException"); |
| 110 | jmethodID mid = env->GetMethodID(exception_class, "<init>", "(Ljava/lang/Throwable;)V"); |
| 111 | jobject exception_instance = env->NewObject(exception_class, mid, th); |
| 112 | env->Throw(reinterpret_cast<jthrowable>(exception_instance)); |
| 113 | return NULL; |
| 114 | } |
| 115 | |
| 116 | // Box if necessary and return. |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame^] | 117 | BoxPrimitive(env, mh.GetReturnType()->GetPrimitiveType(), value); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 118 | return AddLocalReference<jobject>(env, value.l); |
| 119 | } |
| 120 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 121 | bool VerifyObjectInClass(JNIEnv* env, Object* o, Class* c) { |
| 122 | if (o == NULL) { |
| 123 | jniThrowNullPointerException(env, "receiver for non-static field access was null"); |
| 124 | return false; |
| 125 | } |
| 126 | if (!o->InstanceOf(c)) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame^] | 127 | std::string expectedClassName(PrettyDescriptor(c)); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 128 | std::string actualClassName(PrettyTypeOf(o)); |
| 129 | jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException", |
| 130 | "expected receiver of type %s, but got %s", |
| 131 | expectedClassName.c_str(), actualClassName.c_str()); |
| 132 | return false; |
| 133 | } |
| 134 | return true; |
| 135 | } |
| 136 | |
| 137 | /* |
| 138 | * Convert primitive, boxed data from "srcPtr" to "dstPtr". |
| 139 | * |
| 140 | * Section v2 2.6 lists the various conversions and promotions. We |
| 141 | * allow the "widening" and "identity" conversions, but don't allow the |
| 142 | * "narrowing" conversions. |
| 143 | * |
| 144 | * Allowed: |
| 145 | * byte to short, int, long, float, double |
| 146 | * short to int, long, float double |
| 147 | * char to int, long, float, double |
| 148 | * int to long, float, double |
| 149 | * long to float, double |
| 150 | * float to double |
| 151 | * Values of types byte, char, and short are "internally" widened to int. |
| 152 | * |
| 153 | * Returns the width in 32-bit words of the destination primitive, or |
| 154 | * -1 if the conversion is not allowed. |
| 155 | */ |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 156 | bool ConvertPrimitiveValue(Primitive::Type srcType, Primitive::Type dstType, |
| 157 | const JValue& src, JValue& dst) { |
Jesse Wilson | c129a6b | 2011-11-24 14:47:46 -0500 | [diff] [blame] | 158 | CHECK(srcType != Primitive::kPrimNot && dstType != Primitive::kPrimNot); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 159 | switch (dstType) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 160 | case Primitive::kPrimBoolean: |
| 161 | case Primitive::kPrimChar: |
| 162 | case Primitive::kPrimByte: |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 163 | if (srcType == dstType) { |
| 164 | dst.i = src.i; |
| 165 | return true; |
| 166 | } |
| 167 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 168 | case Primitive::kPrimShort: |
| 169 | if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimShort) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 170 | dst.i = src.i; |
| 171 | return true; |
| 172 | } |
| 173 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 174 | case Primitive::kPrimInt: |
| 175 | if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar || |
| 176 | srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 177 | dst.i = src.i; |
| 178 | return true; |
| 179 | } |
| 180 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 181 | case Primitive::kPrimLong: |
| 182 | if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar || |
| 183 | srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 184 | dst.j = src.i; |
| 185 | return true; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 186 | } else if (srcType == Primitive::kPrimLong) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 187 | dst.j = src.j; |
| 188 | return true; |
| 189 | } |
| 190 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 191 | case Primitive::kPrimFloat: |
| 192 | if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar || |
| 193 | srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 194 | dst.f = src.i; |
| 195 | return true; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 196 | } else if (srcType == Primitive::kPrimLong) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 197 | dst.f = src.j; |
| 198 | return true; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 199 | } else if (srcType == Primitive::kPrimFloat) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 200 | dst.i = src.i; |
| 201 | return true; |
| 202 | } |
| 203 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 204 | case Primitive::kPrimDouble: |
| 205 | if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar || |
| 206 | srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 207 | dst.d = src.i; |
| 208 | return true; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 209 | } else if (srcType == Primitive::kPrimLong) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 210 | dst.d = src.j; |
| 211 | return true; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 212 | } else if (srcType == Primitive::kPrimFloat) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 213 | dst.d = src.f; |
| 214 | return true; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 215 | } else if (srcType == Primitive::kPrimDouble) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 216 | dst.j = src.j; |
| 217 | return true; |
| 218 | } |
| 219 | break; |
| 220 | default: |
| 221 | break; |
| 222 | } |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 223 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;", |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 224 | "invalid primitive conversion from %s to %s", |
| 225 | PrettyDescriptor(srcType).c_str(), |
| 226 | PrettyDescriptor(dstType).c_str()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 227 | return false; |
| 228 | } |
| 229 | |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 230 | void BoxPrimitive(JNIEnv* env, Primitive::Type src_class, JValue& value) { |
| 231 | if (src_class == Primitive::kPrimNot) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 232 | return; |
| 233 | } |
| 234 | |
| 235 | Method* m = NULL; |
| 236 | UniquePtr<byte[]> args(new byte[8]); |
| 237 | memset(&args[0], 0, 8); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 238 | switch (src_class) { |
| 239 | case Primitive::kPrimBoolean: |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 240 | m = gBoolean_valueOf; |
| 241 | *reinterpret_cast<uint32_t*>(&args[0]) = value.z; |
| 242 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 243 | case Primitive::kPrimByte: |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 244 | m = gByte_valueOf; |
| 245 | *reinterpret_cast<uint32_t*>(&args[0]) = value.b; |
| 246 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 247 | case Primitive::kPrimChar: |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 248 | m = gCharacter_valueOf; |
| 249 | *reinterpret_cast<uint32_t*>(&args[0]) = value.c; |
| 250 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 251 | case Primitive::kPrimDouble: |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 252 | m = gDouble_valueOf; |
| 253 | *reinterpret_cast<double*>(&args[0]) = value.d; |
| 254 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 255 | case Primitive::kPrimFloat: |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 256 | m = gFloat_valueOf; |
| 257 | *reinterpret_cast<float*>(&args[0]) = value.f; |
| 258 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 259 | case Primitive::kPrimInt: |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 260 | m = gInteger_valueOf; |
| 261 | *reinterpret_cast<uint32_t*>(&args[0]) = value.i; |
| 262 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 263 | case Primitive::kPrimLong: |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 264 | m = gLong_valueOf; |
| 265 | *reinterpret_cast<uint64_t*>(&args[0]) = value.j; |
| 266 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 267 | case Primitive::kPrimShort: |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 268 | m = gShort_valueOf; |
| 269 | *reinterpret_cast<uint32_t*>(&args[0]) = value.s; |
| 270 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 271 | case Primitive::kPrimVoid: |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 272 | // There's no such thing as a void field, and void methods invoked via reflection return null. |
| 273 | value.l = NULL; |
| 274 | return; |
| 275 | default: |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 276 | LOG(FATAL) << static_cast<int>(src_class); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | Thread* self = Thread::Current(); |
| 280 | ScopedThreadStateChange tsc(self, Thread::kRunnable); |
| 281 | m->Invoke(self, NULL, args.get(), &value); |
| 282 | } |
| 283 | |
| 284 | bool UnboxPrimitive(JNIEnv* env, Object* o, Class* dst_class, JValue& unboxed_value) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 285 | if (!dst_class->IsPrimitive()) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 286 | if (o != NULL && !o->InstanceOf(dst_class)) { |
| 287 | jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException", |
| 288 | "expected object of type %s, but got %s", |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame^] | 289 | PrettyDescriptor(dst_class).c_str(), |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 290 | PrettyTypeOf(o).c_str()); |
| 291 | return false; |
| 292 | } |
| 293 | unboxed_value.l = o; |
| 294 | return true; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 295 | } else if (dst_class->GetPrimitiveType() == Primitive::kPrimVoid) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 296 | Thread::Current()->ThrowNewException("Ljava/lang/IllegalArgumentException;", |
| 297 | "can't unbox to void"); |
| 298 | return false; |
| 299 | } |
| 300 | |
| 301 | if (o == NULL) { |
| 302 | Thread::Current()->ThrowNewException("Ljava/lang/IllegalArgumentException;", |
| 303 | "null passed for boxed primitive type"); |
| 304 | return false; |
| 305 | } |
| 306 | |
| 307 | JValue boxed_value = { 0 }; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame^] | 308 | std::string src_descriptor = ClassHelper(o->GetClass()).GetDescriptor(); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 309 | Class* src_class = NULL; |
| 310 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 311 | Field* primitive_field = o->GetClass()->GetIFields()->Get(0); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame^] | 312 | if (src_descriptor == "Ljava/lang/Boolean;") { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 313 | src_class = class_linker->FindPrimitiveClass('Z'); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 314 | boxed_value.i = primitive_field->GetBoolean(o); // and extend read value to 32bits |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame^] | 315 | } else if (src_descriptor == "Ljava/lang/Byte;") { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 316 | src_class = class_linker->FindPrimitiveClass('B'); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 317 | boxed_value.i = primitive_field->GetByte(o); // and extend read value to 32bits |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame^] | 318 | } else if (src_descriptor == "Ljava/lang/Character;") { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 319 | src_class = class_linker->FindPrimitiveClass('C'); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 320 | boxed_value.i = primitive_field->GetChar(o); // and extend read value to 32bits |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame^] | 321 | } else if (src_descriptor == "Ljava/lang/Float;") { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 322 | src_class = class_linker->FindPrimitiveClass('F'); |
| 323 | boxed_value.f = primitive_field->GetFloat(o); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame^] | 324 | } else if (src_descriptor == "Ljava/lang/Double;") { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 325 | src_class = class_linker->FindPrimitiveClass('D'); |
| 326 | boxed_value.d = primitive_field->GetDouble(o); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame^] | 327 | } else if (src_descriptor == "Ljava/lang/Integer;") { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 328 | src_class = class_linker->FindPrimitiveClass('I'); |
| 329 | boxed_value.i = primitive_field->GetInt(o); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame^] | 330 | } else if (src_descriptor == "Ljava/lang/Long;") { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 331 | src_class = class_linker->FindPrimitiveClass('J'); |
| 332 | boxed_value.j = primitive_field->GetLong(o); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame^] | 333 | } else if (src_descriptor == "Ljava/lang/Short;") { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 334 | src_class = class_linker->FindPrimitiveClass('S'); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 335 | boxed_value.i = primitive_field->GetShort(o); // and extend read value to 32bits |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 336 | } else { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 337 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;", |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame^] | 338 | "%s is not a boxed primitive type", PrettyDescriptor(src_descriptor.c_str()).c_str()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 339 | return false; |
| 340 | } |
| 341 | |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 342 | return ConvertPrimitiveValue(src_class->GetPrimitiveType(), dst_class->GetPrimitiveType(), |
| 343 | boxed_value, unboxed_value); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | } // namespace art |