blob: 6763100b54acc5fde183a781ced04825c58c55cc [file] [log] [blame]
Elliott Hughes418d20f2011-09-22 14:00:39 -07001/*
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 Rogers6d4d9fc2011-11-30 16:24:48 -080022#include "object_utils.h"
Elliott Hughes418d20f2011-09-22 14:00:39 -070023
24#include "JniConstants.h" // Last to avoid problems with LOG redefinition.
25
26namespace art {
27
28Method* gBoolean_valueOf;
29Method* gByte_valueOf;
30Method* gCharacter_valueOf;
31Method* gDouble_valueOf;
32Method* gFloat_valueOf;
33Method* gInteger_valueOf;
34Method* gLong_valueOf;
35Method* gShort_valueOf;
36
Jesse Wilson9a6bae82011-11-14 14:57:30 -050037void 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 Hughes418d20f2011-09-22 14:00:39 -070047}
48
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080049jobject InvokeMethod(JNIEnv* env, jobject javaMethod, jobject javaReceiver, jobject javaArgs) {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070050 Thread* self = Thread::Current();
Elliott Hughes34e06962012-04-09 13:55:55 -070051 ScopedThreadStateChange tsc(self, kRunnable);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070052
53 jmethodID mid = env->FromReflectedMethod(javaMethod);
54 Method* m = reinterpret_cast<Method*>(mid);
55
56 Class* declaring_class = m->GetDeclaringClass();
Ian Rogers0045a292012-03-31 21:08:41 -070057 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(declaring_class, true, true)) {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070058 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 Rogers466bb252011-10-14 03:29:56 -070071 mid = reinterpret_cast<jmethodID>(m);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070072 }
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 Rogers6d4d9fc2011-11-30 16:24:48 -080076 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 Hughes5cb5ad22011-10-02 12:13:39 -070081 self->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070082 "wrong number of arguments; expected %d, got %d",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080083 classes_size, arg_count);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070084 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 Rogers6d4d9fc2011-11-30 16:24:48 -080090 for (uint32_t i = 0; i < arg_count; ++i) {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070091 Object* arg = objects->Get(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080092 Class* dst_class = mh.GetClassFromTypeIdx(classes->GetTypeItem(i).type_idx_);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070093 if (dst_class->IsPrimitive()) {
Elliott Hughesaaa5edc2012-05-16 15:54:30 -070094 if (!UnboxPrimitiveForArgument(arg, dst_class, decoded_args[i], i)) {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070095 return NULL;
96 }
Elliott Hughesaaa5edc2012-05-16 15:54:30 -070097 } else if (arg != NULL && !arg->InstanceOf(dst_class)) {
98 self->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
99 "argument %d has type %s, got %s",
100 i + 1,
101 PrettyDescriptor(dst_class).c_str(),
102 PrettyTypeOf(arg).c_str());
103 return NULL;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700104 } else {
105 args[i].l = AddLocalReference<jobject>(env, arg);
106 }
107 }
108
109 // Invoke the method.
Elliott Hughes1d878f32012-04-11 15:17:54 -0700110 JValue value(InvokeWithJValues(env, javaReceiver, mid, args.get()));
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700111
112 // Wrap any exception with "Ljava/lang/reflect/InvocationTargetException;" and return early.
113 if (self->IsExceptionPending()) {
114 jthrowable th = env->ExceptionOccurred();
115 env->ExceptionClear();
116 jclass exception_class = env->FindClass("java/lang/reflect/InvocationTargetException");
117 jmethodID mid = env->GetMethodID(exception_class, "<init>", "(Ljava/lang/Throwable;)V");
118 jobject exception_instance = env->NewObject(exception_class, mid, th);
119 env->Throw(reinterpret_cast<jthrowable>(exception_instance));
120 return NULL;
121 }
122
123 // Box if necessary and return.
Elliott Hughesdbac3092012-03-16 18:00:30 -0700124 BoxPrimitive(mh.GetReturnType()->GetPrimitiveType(), value);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700125 return AddLocalReference<jobject>(env, value.GetL());
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700126}
127
Elliott Hughes418d20f2011-09-22 14:00:39 -0700128bool VerifyObjectInClass(JNIEnv* env, Object* o, Class* c) {
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700129 const char* exception = NULL;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700130 if (o == NULL) {
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700131 exception = "java/lang/NullPointerException";
132 } else if (!o->InstanceOf(c)) {
133 exception = "java/lang/IllegalArgumentException";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700134 }
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700135 if (exception != NULL) {
136 std::string expected_class_name(PrettyDescriptor(c));
137 std::string actual_class_name(PrettyTypeOf(o));
138 jniThrowExceptionFmt(env, exception, "expected receiver of type %s, but got %s",
139 expected_class_name.c_str(), actual_class_name.c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700140 return false;
141 }
142 return true;
143}
144
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700145bool ConvertPrimitiveValue(Primitive::Type srcType, Primitive::Type dstType,
146 const JValue& src, JValue& dst) {
Jesse Wilsonc129a6b2011-11-24 14:47:46 -0500147 CHECK(srcType != Primitive::kPrimNot && dstType != Primitive::kPrimNot);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700148 switch (dstType) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700149 case Primitive::kPrimBoolean:
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700150 if (srcType == Primitive::kPrimBoolean) {
151 dst.SetZ(src.GetZ());
152 return true;
153 }
154 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700155 case Primitive::kPrimChar:
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700156 if (srcType == Primitive::kPrimChar) {
157 dst.SetC(src.GetC());
158 return true;
159 }
160 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700161 case Primitive::kPrimByte:
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700162 if (srcType == Primitive::kPrimByte) {
163 dst.SetB(src.GetB());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700164 return true;
165 }
166 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700167 case Primitive::kPrimShort:
168 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimShort) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700169 dst.SetS(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700170 return true;
171 }
172 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700173 case Primitive::kPrimInt:
174 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
175 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700176 dst.SetI(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700177 return true;
178 }
179 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700180 case Primitive::kPrimLong:
181 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
182 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700183 dst.SetJ(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700184 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700185 } else if (srcType == Primitive::kPrimLong) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700186 dst.SetJ(src.GetJ());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700187 return true;
188 }
189 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700190 case Primitive::kPrimFloat:
191 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
192 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700193 dst.SetF(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700194 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700195 } else if (srcType == Primitive::kPrimLong) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700196 dst.SetF(src.GetJ());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700197 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700198 } else if (srcType == Primitive::kPrimFloat) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700199 dst.SetF(src.GetF());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700200 return true;
201 }
202 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700203 case Primitive::kPrimDouble:
204 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
205 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700206 dst.SetD(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700207 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700208 } else if (srcType == Primitive::kPrimLong) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700209 dst.SetD(src.GetJ());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700210 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700211 } else if (srcType == Primitive::kPrimFloat) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700212 dst.SetD(src.GetF());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700213 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700214 } else if (srcType == Primitive::kPrimDouble) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700215 dst.SetJ(src.GetJ());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700216 return true;
217 }
218 break;
219 default:
220 break;
221 }
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700222 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700223 "invalid primitive conversion from %s to %s",
224 PrettyDescriptor(srcType).c_str(),
225 PrettyDescriptor(dstType).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700226 return false;
227}
228
Elliott Hughesdbac3092012-03-16 18:00:30 -0700229void BoxPrimitive(Primitive::Type src_class, JValue& value) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700230 if (src_class == Primitive::kPrimNot) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700231 return;
232 }
233
234 Method* m = NULL;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700235 switch (src_class) {
236 case Primitive::kPrimBoolean:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700237 m = gBoolean_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700238 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700239 case Primitive::kPrimByte:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700240 m = gByte_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700241 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700242 case Primitive::kPrimChar:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700243 m = gCharacter_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700244 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700245 case Primitive::kPrimDouble:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700246 m = gDouble_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700247 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700248 case Primitive::kPrimFloat:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700249 m = gFloat_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700250 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700251 case Primitive::kPrimInt:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700252 m = gInteger_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700253 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700254 case Primitive::kPrimLong:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700255 m = gLong_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700256 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700257 case Primitive::kPrimShort:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700258 m = gShort_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700259 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700260 case Primitive::kPrimVoid:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700261 // There's no such thing as a void field, and void methods invoked via reflection return null.
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700262 value.SetL(NULL);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700263 return;
264 default:
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700265 LOG(FATAL) << static_cast<int>(src_class);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700266 }
267
268 Thread* self = Thread::Current();
Elliott Hughes34e06962012-04-09 13:55:55 -0700269 ScopedThreadStateChange tsc(self, kRunnable);
Elliott Hughesdbac3092012-03-16 18:00:30 -0700270 JValue args[1] = { value };
Elliott Hughes77405792012-03-15 15:22:12 -0700271 m->Invoke(self, NULL, args, &value);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700272}
273
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700274static std::string UnboxingFailureKind(int index, Field* f) {
275 if (index != -1) {
276 return StringPrintf("argument %d", index + 1); // Humans count from 1.
277 }
278 if (f != NULL) {
279 return "field " + PrettyField(f, false);
280 }
281 return "result";
282}
283
284static bool UnboxPrimitive(Object* o, Class* dst_class, JValue& unboxed_value, int index, Field* f) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700285 if (!dst_class->IsPrimitive()) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700286 if (o != NULL && !o->InstanceOf(dst_class)) {
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700287 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700288 "%s has type %s, got %s",
289 UnboxingFailureKind(index, f).c_str(),
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700290 PrettyDescriptor(dst_class).c_str(),
291 PrettyTypeOf(o).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700292 return false;
293 }
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700294 unboxed_value.SetL(o);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700295 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700296 } else if (dst_class->GetPrimitiveType() == Primitive::kPrimVoid) {
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700297 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
298 "can't unbox %s to void",
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700299 UnboxingFailureKind(index, f).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700300 return false;
301 }
302
303 if (o == NULL) {
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700304 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700305 "%s has type %s, got null",
306 UnboxingFailureKind(index, f).c_str(),
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700307 PrettyDescriptor(dst_class).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700308 return false;
309 }
310
Elliott Hughes1d878f32012-04-11 15:17:54 -0700311 JValue boxed_value;
Elliott Hughes95572412011-12-13 18:14:20 -0800312 std::string src_descriptor(ClassHelper(o->GetClass()).GetDescriptor());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700313 Class* src_class = NULL;
314 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
315 Field* primitive_field = o->GetClass()->GetIFields()->Get(0);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800316 if (src_descriptor == "Ljava/lang/Boolean;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700317 src_class = class_linker->FindPrimitiveClass('Z');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700318 boxed_value.SetZ(primitive_field->GetBoolean(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800319 } else if (src_descriptor == "Ljava/lang/Byte;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700320 src_class = class_linker->FindPrimitiveClass('B');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700321 boxed_value.SetB(primitive_field->GetByte(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800322 } else if (src_descriptor == "Ljava/lang/Character;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700323 src_class = class_linker->FindPrimitiveClass('C');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700324 boxed_value.SetC(primitive_field->GetChar(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800325 } else if (src_descriptor == "Ljava/lang/Float;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700326 src_class = class_linker->FindPrimitiveClass('F');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700327 boxed_value.SetF(primitive_field->GetFloat(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800328 } else if (src_descriptor == "Ljava/lang/Double;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700329 src_class = class_linker->FindPrimitiveClass('D');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700330 boxed_value.SetD(primitive_field->GetDouble(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800331 } else if (src_descriptor == "Ljava/lang/Integer;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700332 src_class = class_linker->FindPrimitiveClass('I');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700333 boxed_value.SetI(primitive_field->GetInt(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800334 } else if (src_descriptor == "Ljava/lang/Long;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700335 src_class = class_linker->FindPrimitiveClass('J');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700336 boxed_value.SetJ(primitive_field->GetLong(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800337 } else if (src_descriptor == "Ljava/lang/Short;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700338 src_class = class_linker->FindPrimitiveClass('S');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700339 boxed_value.SetS(primitive_field->GetShort(o));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700340 } else {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700341 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700342 "%s has type %s, got %s",
343 UnboxingFailureKind(index, f).c_str(),
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700344 PrettyDescriptor(dst_class).c_str(),
345 PrettyDescriptor(src_descriptor.c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700346 return false;
347 }
348
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700349 return ConvertPrimitiveValue(src_class->GetPrimitiveType(), dst_class->GetPrimitiveType(),
350 boxed_value, unboxed_value);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700351}
352
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700353bool UnboxPrimitiveForArgument(Object* o, Class* dst_class, JValue& unboxed_value, size_t index) {
354 return UnboxPrimitive(o, dst_class, unboxed_value, index, NULL);
355}
356
357bool UnboxPrimitiveForField(Object* o, Class* dst_class, JValue& unboxed_value, Field* f) {
358 CHECK(f != NULL);
359 return UnboxPrimitive(o, dst_class, unboxed_value, -1, f);
360}
361
362bool UnboxPrimitiveForResult(Object* o, Class* dst_class, JValue& unboxed_value) {
363 return UnboxPrimitive(o, dst_class, unboxed_value, -1, NULL);
364}
365
Elliott Hughes418d20f2011-09-22 14:00:39 -0700366} // namespace art