blob: 3e58b4bd94de17746db51ec02177662d81c5b31a [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"
Ian Rogers62d6c772013-02-27 08:32:07 -080020#include "common_throws.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070021#include "dex_file-inl.h"
Jeff Hao5d917302013-02-27 17:57:33 -080022#include "invoke_arg_array_builder.h"
Elliott Hughes418d20f2011-09-22 14:00:39 -070023#include "jni_internal.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070024#include "mirror/art_field-inl.h"
25#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026#include "mirror/class.h"
27#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "mirror/object_array.h"
29#include "mirror/object_array-inl.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080030#include "object_utils.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070031#include "scoped_thread_state_change.h"
32#include "well_known_classes.h"
Elliott Hughes418d20f2011-09-22 14:00:39 -070033
Elliott Hughes418d20f2011-09-22 14:00:39 -070034namespace art {
35
Ian Rogers00f7d0e2012-07-19 15:28:27 -070036jobject InvokeMethod(const ScopedObjectAccess& soa, jobject javaMethod, jobject javaReceiver,
Ian Rogers365c1022012-06-22 15:05:28 -070037 jobject javaArgs) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070038 jmethodID mid = soa.Env()->FromReflectedMethod(javaMethod);
Brian Carlstromea46f952013-07-30 01:26:50 -070039 mirror::ArtMethod* m = soa.DecodeMethod(mid);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070040
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041 mirror::Class* declaring_class = m->GetDeclaringClass();
Ian Rogers0045a292012-03-31 21:08:41 -070042 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(declaring_class, true, true)) {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070043 return NULL;
44 }
45
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080046 mirror::Object* receiver = NULL;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070047 if (!m->IsStatic()) {
48 // Check that the receiver is non-null and an instance of the field's declaring class.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080049 receiver = soa.Decode<mirror::Object*>(javaReceiver);
Elliott Hugheseac76672012-05-24 21:56:51 -070050 if (!VerifyObjectInClass(receiver, declaring_class)) {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070051 return NULL;
52 }
53
54 // Find the actual implementation of the virtual method.
55 m = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(m);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070056 mid = soa.EncodeMethod(m);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070057 }
58
59 // Get our arrays of arguments and their types, and check they're the same size.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080060 mirror::ObjectArray<mirror::Object>* objects =
61 soa.Decode<mirror::ObjectArray<mirror::Object>*>(javaArgs);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080062 MethodHelper mh(m);
63 const DexFile::TypeList* classes = mh.GetParameterTypeList();
64 uint32_t classes_size = classes == NULL ? 0 : classes->Size();
65 uint32_t arg_count = (objects != NULL) ? objects->GetLength() : 0;
66 if (arg_count != classes_size) {
Ian Rogers62d6c772013-02-27 08:32:07 -080067 ThrowIllegalArgumentException(NULL,
68 StringPrintf("Wrong number of arguments; expected %d, got %d",
69 classes_size, arg_count).c_str());
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070070 return NULL;
71 }
72
73 // Translate javaArgs to a jvalue[].
74 UniquePtr<jvalue[]> args(new jvalue[arg_count]);
75 JValue* decoded_args = reinterpret_cast<JValue*>(args.get());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080076 for (uint32_t i = 0; i < arg_count; ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080077 mirror::Object* arg = objects->Get(i);
78 mirror::Class* dst_class = mh.GetClassFromTypeIdx(classes->GetTypeItem(i).type_idx_);
Elliott Hughes37f77752012-05-21 15:12:47 -070079 if (!UnboxPrimitiveForArgument(arg, dst_class, decoded_args[i], m, i)) {
80 return NULL;
81 }
82 if (!dst_class->IsPrimitive()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070083 args[i].l = soa.AddLocalReference<jobject>(arg);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070084 }
85 }
86
87 // Invoke the method.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070088 JValue value(InvokeWithJValues(soa, javaReceiver, mid, args.get()));
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070089
90 // Wrap any exception with "Ljava/lang/reflect/InvocationTargetException;" and return early.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070091 if (soa.Self()->IsExceptionPending()) {
92 jthrowable th = soa.Env()->ExceptionOccurred();
93 soa.Env()->ExceptionClear();
94 jclass exception_class = soa.Env()->FindClass("java/lang/reflect/InvocationTargetException");
95 jmethodID mid = soa.Env()->GetMethodID(exception_class, "<init>", "(Ljava/lang/Throwable;)V");
96 jobject exception_instance = soa.Env()->NewObject(exception_class, mid, th);
97 soa.Env()->Throw(reinterpret_cast<jthrowable>(exception_instance));
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070098 return NULL;
99 }
100
101 // Box if necessary and return.
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800102 return soa.AddLocalReference<jobject>(BoxPrimitive(mh.GetReturnType()->GetPrimitiveType(), value));
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700103}
104
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800105bool VerifyObjectInClass(mirror::Object* o, mirror::Class* c) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700106 if (o == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800107 ThrowNullPointerException(NULL, "null receiver");
108 return false;
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700109 } else if (!o->InstanceOf(c)) {
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700110 std::string expected_class_name(PrettyDescriptor(c));
111 std::string actual_class_name(PrettyTypeOf(o));
Ian Rogers62d6c772013-02-27 08:32:07 -0800112 ThrowIllegalArgumentException(NULL,
113 StringPrintf("Expected receiver of type %s, but got %s",
114 expected_class_name.c_str(),
115 actual_class_name.c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700116 return false;
117 }
118 return true;
119}
120
Ian Rogers62d6c772013-02-27 08:32:07 -0800121bool ConvertPrimitiveValue(const ThrowLocation* throw_location, bool unbox_for_result,
122 Primitive::Type srcType, Primitive::Type dstType,
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700123 const JValue& src, JValue& dst) {
Jesse Wilsonc129a6b2011-11-24 14:47:46 -0500124 CHECK(srcType != Primitive::kPrimNot && dstType != Primitive::kPrimNot);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700125 switch (dstType) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700126 case Primitive::kPrimBoolean:
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700127 if (srcType == Primitive::kPrimBoolean) {
128 dst.SetZ(src.GetZ());
129 return true;
130 }
131 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700132 case Primitive::kPrimChar:
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700133 if (srcType == Primitive::kPrimChar) {
134 dst.SetC(src.GetC());
135 return true;
136 }
137 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700138 case Primitive::kPrimByte:
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700139 if (srcType == Primitive::kPrimByte) {
140 dst.SetB(src.GetB());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700141 return true;
142 }
143 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700144 case Primitive::kPrimShort:
145 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimShort) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700146 dst.SetS(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700147 return true;
148 }
149 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700150 case Primitive::kPrimInt:
151 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
152 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700153 dst.SetI(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700154 return true;
155 }
156 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700157 case Primitive::kPrimLong:
158 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
159 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700160 dst.SetJ(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700161 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700162 } else if (srcType == Primitive::kPrimLong) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700163 dst.SetJ(src.GetJ());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700164 return true;
165 }
166 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700167 case Primitive::kPrimFloat:
168 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
169 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700170 dst.SetF(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700171 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700172 } else if (srcType == Primitive::kPrimLong) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700173 dst.SetF(src.GetJ());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700174 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700175 } else if (srcType == Primitive::kPrimFloat) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700176 dst.SetF(src.GetF());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700177 return true;
178 }
179 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700180 case Primitive::kPrimDouble:
181 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
182 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700183 dst.SetD(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.SetD(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.SetD(src.GetF());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700190 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700191 } else if (srcType == Primitive::kPrimDouble) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700192 dst.SetJ(src.GetJ());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700193 return true;
194 }
195 break;
196 default:
197 break;
198 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800199 if (!unbox_for_result) {
200 ThrowIllegalArgumentException(throw_location,
201 StringPrintf("Invalid primitive conversion from %s to %s",
202 PrettyDescriptor(srcType).c_str(),
203 PrettyDescriptor(dstType).c_str()).c_str());
204 } else {
205 ThrowClassCastException(throw_location,
206 StringPrintf("Couldn't convert result of type %s to %s",
207 PrettyDescriptor(srcType).c_str(),
Brian Carlstromdf629502013-07-17 22:39:56 -0700208 PrettyDescriptor(dstType).c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800209 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700210 return false;
211}
212
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800213mirror::Object* BoxPrimitive(Primitive::Type src_class, const JValue& value) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700214 if (src_class == Primitive::kPrimNot) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800215 return value.GetL();
Elliott Hughes418d20f2011-09-22 14:00:39 -0700216 }
217
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700218 jmethodID m = NULL;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700219 switch (src_class) {
220 case Primitive::kPrimBoolean:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700221 m = WellKnownClasses::java_lang_Boolean_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700222 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700223 case Primitive::kPrimByte:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700224 m = WellKnownClasses::java_lang_Byte_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700225 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700226 case Primitive::kPrimChar:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700227 m = WellKnownClasses::java_lang_Character_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700228 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700229 case Primitive::kPrimDouble:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700230 m = WellKnownClasses::java_lang_Double_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700231 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700232 case Primitive::kPrimFloat:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700233 m = WellKnownClasses::java_lang_Float_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700234 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700235 case Primitive::kPrimInt:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700236 m = WellKnownClasses::java_lang_Integer_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700237 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700238 case Primitive::kPrimLong:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700239 m = WellKnownClasses::java_lang_Long_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700240 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700241 case Primitive::kPrimShort:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700242 m = WellKnownClasses::java_lang_Short_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700243 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700244 case Primitive::kPrimVoid:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700245 // There's no such thing as a void field, and void methods invoked via reflection return null.
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800246 return NULL;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700247 default:
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700248 LOG(FATAL) << static_cast<int>(src_class);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700249 }
250
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700251 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogers50b35e22012-10-04 10:09:15 -0700252 if (kIsDebugBuild) {
253 CHECK_EQ(soa.Self()->GetState(), kRunnable);
254 }
Jeff Hao5d917302013-02-27 17:57:33 -0800255
256 ArgArray arg_array(NULL, 0);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800257 JValue result;
Jeff Hao5d917302013-02-27 17:57:33 -0800258 if (src_class == Primitive::kPrimDouble || src_class == Primitive::kPrimLong) {
259 arg_array.AppendWide(value.GetJ());
260 } else {
261 arg_array.Append(value.GetI());
262 }
263
264 soa.DecodeMethod(m)->Invoke(soa.Self(), arg_array.GetArray(), arg_array.GetNumBytes(),
Jeff Hao6474d192013-03-26 14:08:09 -0700265 &result, 'L');
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800266 return result.GetL();
Elliott Hughes418d20f2011-09-22 14:00:39 -0700267}
268
Brian Carlstromea46f952013-07-30 01:26:50 -0700269static std::string UnboxingFailureKind(mirror::ArtMethod* m, int index, mirror::ArtField* f)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700270 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700271 if (m != NULL && index != -1) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700272 ++index; // Humans count from 1.
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700273 return StringPrintf("method %s argument %d", PrettyMethod(m, false).c_str(), index);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700274 }
275 if (f != NULL) {
276 return "field " + PrettyField(f, false);
277 }
278 return "result";
279}
280
Ian Rogers62d6c772013-02-27 08:32:07 -0800281static bool UnboxPrimitive(const ThrowLocation* throw_location, mirror::Object* o,
282 mirror::Class* dst_class, JValue& unboxed_value,
Brian Carlstromea46f952013-07-30 01:26:50 -0700283 mirror::ArtMethod* m, int index, mirror::ArtField* f)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700284 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800285 bool unbox_for_result = (f == NULL) && (index == -1);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700286 if (!dst_class->IsPrimitive()) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800287 if (UNLIKELY(o != NULL && !o->InstanceOf(dst_class))) {
288 if (!unbox_for_result) {
289 ThrowIllegalArgumentException(throw_location,
290 StringPrintf("%s has type %s, got %s",
291 UnboxingFailureKind(m, index, f).c_str(),
292 PrettyDescriptor(dst_class).c_str(),
293 PrettyTypeOf(o).c_str()).c_str());
294 } else {
295 ThrowClassCastException(throw_location,
296 StringPrintf("Couldn't convert result of type %s to %s",
297 PrettyTypeOf(o).c_str(),
Brian Carlstromdf629502013-07-17 22:39:56 -0700298 PrettyDescriptor(dst_class).c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800299 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700300 return false;
301 }
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700302 unboxed_value.SetL(o);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700303 return true;
Ian Rogers62d6c772013-02-27 08:32:07 -0800304 }
305 if (UNLIKELY(dst_class->GetPrimitiveType() == Primitive::kPrimVoid)) {
306 ThrowIllegalArgumentException(throw_location,
307 StringPrintf("Can't unbox %s to void",
308 UnboxingFailureKind(m, index, f).c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700309 return false;
310 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800311 if (UNLIKELY(o == NULL)) {
312 if (!unbox_for_result) {
313 ThrowIllegalArgumentException(throw_location,
314 StringPrintf("%s has type %s, got null",
315 UnboxingFailureKind(m, index, f).c_str(),
316 PrettyDescriptor(dst_class).c_str()).c_str());
317 } else {
318 ThrowNullPointerException(throw_location,
319 StringPrintf("Expected to unbox a '%s' primitive type but was returned null",
320 PrettyDescriptor(dst_class).c_str()).c_str());
321 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700322 return false;
323 }
324
Elliott Hughes1d878f32012-04-11 15:17:54 -0700325 JValue boxed_value;
Elliott Hughes95572412011-12-13 18:14:20 -0800326 std::string src_descriptor(ClassHelper(o->GetClass()).GetDescriptor());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800327 mirror::Class* src_class = NULL;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700328 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromea46f952013-07-30 01:26:50 -0700329 mirror::ArtField* primitive_field = o->GetClass()->GetIFields()->Get(0);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800330 if (src_descriptor == "Ljava/lang/Boolean;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700331 src_class = class_linker->FindPrimitiveClass('Z');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700332 boxed_value.SetZ(primitive_field->GetBoolean(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800333 } else if (src_descriptor == "Ljava/lang/Byte;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700334 src_class = class_linker->FindPrimitiveClass('B');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700335 boxed_value.SetB(primitive_field->GetByte(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800336 } else if (src_descriptor == "Ljava/lang/Character;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700337 src_class = class_linker->FindPrimitiveClass('C');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700338 boxed_value.SetC(primitive_field->GetChar(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800339 } else if (src_descriptor == "Ljava/lang/Float;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700340 src_class = class_linker->FindPrimitiveClass('F');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700341 boxed_value.SetF(primitive_field->GetFloat(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800342 } else if (src_descriptor == "Ljava/lang/Double;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700343 src_class = class_linker->FindPrimitiveClass('D');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700344 boxed_value.SetD(primitive_field->GetDouble(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800345 } else if (src_descriptor == "Ljava/lang/Integer;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700346 src_class = class_linker->FindPrimitiveClass('I');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700347 boxed_value.SetI(primitive_field->GetInt(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800348 } else if (src_descriptor == "Ljava/lang/Long;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700349 src_class = class_linker->FindPrimitiveClass('J');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700350 boxed_value.SetJ(primitive_field->GetLong(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800351 } else if (src_descriptor == "Ljava/lang/Short;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700352 src_class = class_linker->FindPrimitiveClass('S');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700353 boxed_value.SetS(primitive_field->GetShort(o));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700354 } else {
Ian Rogers62d6c772013-02-27 08:32:07 -0800355 ThrowIllegalArgumentException(throw_location,
356 StringPrintf("%s has type %s, got %s",
357 UnboxingFailureKind(m, index, f).c_str(),
358 PrettyDescriptor(dst_class).c_str(),
Brian Carlstromdf629502013-07-17 22:39:56 -0700359 PrettyDescriptor(src_descriptor.c_str()).c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700360 return false;
361 }
362
Ian Rogers62d6c772013-02-27 08:32:07 -0800363 return ConvertPrimitiveValue(throw_location, unbox_for_result,
364 src_class->GetPrimitiveType(), dst_class->GetPrimitiveType(),
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700365 boxed_value, unboxed_value);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700366}
367
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800368bool UnboxPrimitiveForArgument(mirror::Object* o, mirror::Class* dst_class, JValue& unboxed_value,
Brian Carlstromea46f952013-07-30 01:26:50 -0700369 mirror::ArtMethod* m, size_t index) {
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700370 CHECK(m != NULL);
Ian Rogers62d6c772013-02-27 08:32:07 -0800371 return UnboxPrimitive(NULL, o, dst_class, unboxed_value, m, index, NULL);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700372}
373
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800374bool UnboxPrimitiveForField(mirror::Object* o, mirror::Class* dst_class, JValue& unboxed_value,
Brian Carlstromea46f952013-07-30 01:26:50 -0700375 mirror::ArtField* f) {
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700376 CHECK(f != NULL);
Ian Rogers62d6c772013-02-27 08:32:07 -0800377 return UnboxPrimitive(NULL, o, dst_class, unboxed_value, NULL, -1, f);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700378}
379
Ian Rogers62d6c772013-02-27 08:32:07 -0800380bool UnboxPrimitiveForResult(const ThrowLocation& throw_location, mirror::Object* o,
381 mirror::Class* dst_class, JValue& unboxed_value) {
382 return UnboxPrimitive(&throw_location, o, dst_class, unboxed_value, NULL, -1, NULL);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700383}
384
Elliott Hughes418d20f2011-09-22 14:00:39 -0700385} // namespace art