blob: 2b72944297a2305f92ee7b0be34ce036296574b6 [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
Elliott Hughes418d20f2011-09-22 14:00:39 -070024namespace art {
25
26Method* gBoolean_valueOf;
27Method* gByte_valueOf;
28Method* gCharacter_valueOf;
29Method* gDouble_valueOf;
30Method* gFloat_valueOf;
31Method* gInteger_valueOf;
32Method* gLong_valueOf;
33Method* gShort_valueOf;
34
Jesse Wilson9a6bae82011-11-14 14:57:30 -050035void InitBoxingMethods() {
36 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
37 gBoolean_valueOf = class_linker->FindSystemClass("Ljava/lang/Boolean;")->FindDeclaredDirectMethod("valueOf", "(Z)Ljava/lang/Boolean;");
38 gByte_valueOf = class_linker->FindSystemClass("Ljava/lang/Byte;")->FindDeclaredDirectMethod("valueOf", "(B)Ljava/lang/Byte;");
39 gCharacter_valueOf = class_linker->FindSystemClass("Ljava/lang/Character;")->FindDeclaredDirectMethod("valueOf", "(C)Ljava/lang/Character;");
40 gDouble_valueOf = class_linker->FindSystemClass("Ljava/lang/Double;")->FindDeclaredDirectMethod("valueOf", "(D)Ljava/lang/Double;");
41 gFloat_valueOf = class_linker->FindSystemClass("Ljava/lang/Float;")->FindDeclaredDirectMethod("valueOf", "(F)Ljava/lang/Float;");
42 gInteger_valueOf = class_linker->FindSystemClass("Ljava/lang/Integer;")->FindDeclaredDirectMethod("valueOf", "(I)Ljava/lang/Integer;");
43 gLong_valueOf = class_linker->FindSystemClass("Ljava/lang/Long;")->FindDeclaredDirectMethod("valueOf", "(J)Ljava/lang/Long;");
44 gShort_valueOf = class_linker->FindSystemClass("Ljava/lang/Short;")->FindDeclaredDirectMethod("valueOf", "(S)Ljava/lang/Short;");
Elliott Hughes418d20f2011-09-22 14:00:39 -070045}
46
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080047jobject InvokeMethod(JNIEnv* env, jobject javaMethod, jobject javaReceiver, jobject javaArgs) {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070048 Thread* self = Thread::Current();
Elliott Hughes34e06962012-04-09 13:55:55 -070049 ScopedThreadStateChange tsc(self, kRunnable);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070050
51 jmethodID mid = env->FromReflectedMethod(javaMethod);
52 Method* m = reinterpret_cast<Method*>(mid);
53
54 Class* declaring_class = m->GetDeclaringClass();
Ian Rogers0045a292012-03-31 21:08:41 -070055 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(declaring_class, true, true)) {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070056 return NULL;
57 }
58
59 Object* receiver = NULL;
60 if (!m->IsStatic()) {
61 // Check that the receiver is non-null and an instance of the field's declaring class.
62 receiver = Decode<Object*>(env, javaReceiver);
Elliott Hugheseac76672012-05-24 21:56:51 -070063 if (!VerifyObjectInClass(receiver, declaring_class)) {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070064 return NULL;
65 }
66
67 // Find the actual implementation of the virtual method.
68 m = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(m);
Ian Rogers466bb252011-10-14 03:29:56 -070069 mid = reinterpret_cast<jmethodID>(m);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070070 }
71
72 // Get our arrays of arguments and their types, and check they're the same size.
73 ObjectArray<Object>* objects = Decode<ObjectArray<Object>*>(env, javaArgs);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080074 MethodHelper mh(m);
75 const DexFile::TypeList* classes = mh.GetParameterTypeList();
76 uint32_t classes_size = classes == NULL ? 0 : classes->Size();
77 uint32_t arg_count = (objects != NULL) ? objects->GetLength() : 0;
78 if (arg_count != classes_size) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -070079 self->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070080 "wrong number of arguments; expected %d, got %d",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080081 classes_size, arg_count);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070082 return NULL;
83 }
84
85 // Translate javaArgs to a jvalue[].
86 UniquePtr<jvalue[]> args(new jvalue[arg_count]);
87 JValue* decoded_args = reinterpret_cast<JValue*>(args.get());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080088 for (uint32_t i = 0; i < arg_count; ++i) {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070089 Object* arg = objects->Get(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080090 Class* dst_class = mh.GetClassFromTypeIdx(classes->GetTypeItem(i).type_idx_);
Elliott Hughes37f77752012-05-21 15:12:47 -070091 if (!UnboxPrimitiveForArgument(arg, dst_class, decoded_args[i], m, i)) {
92 return NULL;
93 }
94 if (!dst_class->IsPrimitive()) {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070095 args[i].l = AddLocalReference<jobject>(env, arg);
96 }
97 }
98
99 // Invoke the method.
Elliott Hughes1d878f32012-04-11 15:17:54 -0700100 JValue value(InvokeWithJValues(env, javaReceiver, mid, args.get()));
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700101
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.
Elliott Hughesdbac3092012-03-16 18:00:30 -0700114 BoxPrimitive(mh.GetReturnType()->GetPrimitiveType(), value);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700115 return AddLocalReference<jobject>(env, value.GetL());
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700116}
117
Elliott Hugheseac76672012-05-24 21:56:51 -0700118bool VerifyObjectInClass(Object* o, Class* c) {
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700119 const char* exception = NULL;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700120 if (o == NULL) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700121 exception = "Ljava/lang/NullPointerException;";
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700122 } else if (!o->InstanceOf(c)) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700123 exception = "Ljava/lang/IllegalArgumentException;";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700124 }
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700125 if (exception != NULL) {
126 std::string expected_class_name(PrettyDescriptor(c));
127 std::string actual_class_name(PrettyTypeOf(o));
Elliott Hugheseac76672012-05-24 21:56:51 -0700128 Thread::Current()->ThrowNewExceptionF(exception, "expected receiver of type %s, but got %s",
129 expected_class_name.c_str(), actual_class_name.c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700130 return false;
131 }
132 return true;
133}
134
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700135bool ConvertPrimitiveValue(Primitive::Type srcType, Primitive::Type dstType,
136 const JValue& src, JValue& dst) {
Jesse Wilsonc129a6b2011-11-24 14:47:46 -0500137 CHECK(srcType != Primitive::kPrimNot && dstType != Primitive::kPrimNot);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700138 switch (dstType) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700139 case Primitive::kPrimBoolean:
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700140 if (srcType == Primitive::kPrimBoolean) {
141 dst.SetZ(src.GetZ());
142 return true;
143 }
144 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700145 case Primitive::kPrimChar:
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700146 if (srcType == Primitive::kPrimChar) {
147 dst.SetC(src.GetC());
148 return true;
149 }
150 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700151 case Primitive::kPrimByte:
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700152 if (srcType == Primitive::kPrimByte) {
153 dst.SetB(src.GetB());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700154 return true;
155 }
156 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700157 case Primitive::kPrimShort:
158 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimShort) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700159 dst.SetS(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700160 return true;
161 }
162 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700163 case Primitive::kPrimInt:
164 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
165 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700166 dst.SetI(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700167 return true;
168 }
169 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700170 case Primitive::kPrimLong:
171 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
172 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700173 dst.SetJ(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700174 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700175 } else if (srcType == Primitive::kPrimLong) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700176 dst.SetJ(src.GetJ());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700177 return true;
178 }
179 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700180 case Primitive::kPrimFloat:
181 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
182 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700183 dst.SetF(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.SetF(src.GetJ());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700187 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700188 } else if (srcType == Primitive::kPrimFloat) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700189 dst.SetF(src.GetF());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700190 return true;
191 }
192 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700193 case Primitive::kPrimDouble:
194 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
195 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700196 dst.SetD(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700197 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700198 } else if (srcType == Primitive::kPrimLong) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700199 dst.SetD(src.GetJ());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700200 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700201 } else if (srcType == Primitive::kPrimFloat) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700202 dst.SetD(src.GetF());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700203 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700204 } else if (srcType == Primitive::kPrimDouble) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700205 dst.SetJ(src.GetJ());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700206 return true;
207 }
208 break;
209 default:
210 break;
211 }
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700212 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700213 "invalid primitive conversion from %s to %s",
214 PrettyDescriptor(srcType).c_str(),
215 PrettyDescriptor(dstType).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700216 return false;
217}
218
Elliott Hughesdbac3092012-03-16 18:00:30 -0700219void BoxPrimitive(Primitive::Type src_class, JValue& value) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700220 if (src_class == Primitive::kPrimNot) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700221 return;
222 }
223
224 Method* m = NULL;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700225 switch (src_class) {
226 case Primitive::kPrimBoolean:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700227 m = gBoolean_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700228 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700229 case Primitive::kPrimByte:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700230 m = gByte_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700231 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700232 case Primitive::kPrimChar:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700233 m = gCharacter_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700234 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700235 case Primitive::kPrimDouble:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700236 m = gDouble_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700237 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700238 case Primitive::kPrimFloat:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700239 m = gFloat_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700240 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700241 case Primitive::kPrimInt:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700242 m = gInteger_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700243 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700244 case Primitive::kPrimLong:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700245 m = gLong_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700246 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700247 case Primitive::kPrimShort:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700248 m = gShort_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700249 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700250 case Primitive::kPrimVoid:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700251 // There's no such thing as a void field, and void methods invoked via reflection return null.
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700252 value.SetL(NULL);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700253 return;
254 default:
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700255 LOG(FATAL) << static_cast<int>(src_class);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700256 }
257
258 Thread* self = Thread::Current();
Elliott Hughes34e06962012-04-09 13:55:55 -0700259 ScopedThreadStateChange tsc(self, kRunnable);
Elliott Hughesdbac3092012-03-16 18:00:30 -0700260 JValue args[1] = { value };
Elliott Hughes77405792012-03-15 15:22:12 -0700261 m->Invoke(self, NULL, args, &value);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700262}
263
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700264static std::string UnboxingFailureKind(Method* m, int index, Field* f) {
265 if (m != NULL && index != -1) {
266 ++index; // Humans count from 1.
267 return StringPrintf("method %s argument %d", PrettyMethod(m, false).c_str(), index);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700268 }
269 if (f != NULL) {
270 return "field " + PrettyField(f, false);
271 }
272 return "result";
273}
274
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700275static bool UnboxPrimitive(Object* o, Class* dst_class, JValue& unboxed_value, Method* m, int index, Field* f) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700276 if (!dst_class->IsPrimitive()) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700277 if (o != NULL && !o->InstanceOf(dst_class)) {
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700278 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700279 "%s has type %s, got %s",
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700280 UnboxingFailureKind(m, index, f).c_str(),
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700281 PrettyDescriptor(dst_class).c_str(),
282 PrettyTypeOf(o).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700283 return false;
284 }
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700285 unboxed_value.SetL(o);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700286 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700287 } else if (dst_class->GetPrimitiveType() == Primitive::kPrimVoid) {
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700288 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
289 "can't unbox %s to void",
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700290 UnboxingFailureKind(m, index, f).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700291 return false;
292 }
293
294 if (o == NULL) {
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700295 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700296 "%s has type %s, got null",
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700297 UnboxingFailureKind(m, index, f).c_str(),
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700298 PrettyDescriptor(dst_class).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700299 return false;
300 }
301
Elliott Hughes1d878f32012-04-11 15:17:54 -0700302 JValue boxed_value;
Elliott Hughes95572412011-12-13 18:14:20 -0800303 std::string src_descriptor(ClassHelper(o->GetClass()).GetDescriptor());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700304 Class* src_class = NULL;
305 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
306 Field* primitive_field = o->GetClass()->GetIFields()->Get(0);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800307 if (src_descriptor == "Ljava/lang/Boolean;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700308 src_class = class_linker->FindPrimitiveClass('Z');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700309 boxed_value.SetZ(primitive_field->GetBoolean(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800310 } else if (src_descriptor == "Ljava/lang/Byte;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700311 src_class = class_linker->FindPrimitiveClass('B');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700312 boxed_value.SetB(primitive_field->GetByte(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800313 } else if (src_descriptor == "Ljava/lang/Character;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700314 src_class = class_linker->FindPrimitiveClass('C');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700315 boxed_value.SetC(primitive_field->GetChar(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800316 } else if (src_descriptor == "Ljava/lang/Float;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700317 src_class = class_linker->FindPrimitiveClass('F');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700318 boxed_value.SetF(primitive_field->GetFloat(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800319 } else if (src_descriptor == "Ljava/lang/Double;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700320 src_class = class_linker->FindPrimitiveClass('D');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700321 boxed_value.SetD(primitive_field->GetDouble(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800322 } else if (src_descriptor == "Ljava/lang/Integer;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700323 src_class = class_linker->FindPrimitiveClass('I');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700324 boxed_value.SetI(primitive_field->GetInt(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800325 } else if (src_descriptor == "Ljava/lang/Long;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700326 src_class = class_linker->FindPrimitiveClass('J');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700327 boxed_value.SetJ(primitive_field->GetLong(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800328 } else if (src_descriptor == "Ljava/lang/Short;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700329 src_class = class_linker->FindPrimitiveClass('S');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700330 boxed_value.SetS(primitive_field->GetShort(o));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700331 } else {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700332 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700333 "%s has type %s, got %s",
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700334 UnboxingFailureKind(m, index, f).c_str(),
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700335 PrettyDescriptor(dst_class).c_str(),
336 PrettyDescriptor(src_descriptor.c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700337 return false;
338 }
339
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700340 return ConvertPrimitiveValue(src_class->GetPrimitiveType(), dst_class->GetPrimitiveType(),
341 boxed_value, unboxed_value);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700342}
343
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700344bool UnboxPrimitiveForArgument(Object* o, Class* dst_class, JValue& unboxed_value, Method* m, size_t index) {
345 CHECK(m != NULL);
346 return UnboxPrimitive(o, dst_class, unboxed_value, m, index, NULL);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700347}
348
349bool UnboxPrimitiveForField(Object* o, Class* dst_class, JValue& unboxed_value, Field* f) {
350 CHECK(f != NULL);
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700351 return UnboxPrimitive(o, dst_class, unboxed_value, NULL, -1, f);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700352}
353
354bool UnboxPrimitiveForResult(Object* o, Class* dst_class, JValue& unboxed_value) {
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700355 return UnboxPrimitive(o, dst_class, unboxed_value, NULL, -1, NULL);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700356}
357
Elliott Hughes418d20f2011-09-22 14:00:39 -0700358} // namespace art