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