blob: ac8f5ef6c4ffd7898b5cd95ba7411e33d649f0de [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();
Mathieu Chartierc528dba2013-11-26 12:00:11 -080042 if (UNLIKELY(!declaring_class->IsInitialized())) {
43 SirtRef<mirror::Class> sirt_c(soa.Self(), declaring_class);
44 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(sirt_c, true, true)) {
45 return nullptr;
46 }
47 declaring_class = sirt_c.get();
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070048 }
49
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080050 mirror::Object* receiver = NULL;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070051 if (!m->IsStatic()) {
52 // Check that the receiver is non-null and an instance of the field's declaring class.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080053 receiver = soa.Decode<mirror::Object*>(javaReceiver);
Elliott Hugheseac76672012-05-24 21:56:51 -070054 if (!VerifyObjectInClass(receiver, declaring_class)) {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070055 return NULL;
56 }
57
58 // Find the actual implementation of the virtual method.
59 m = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(m);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070060 mid = soa.EncodeMethod(m);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070061 }
62
63 // Get our arrays of arguments and their types, and check they're the same size.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080064 mirror::ObjectArray<mirror::Object>* objects =
65 soa.Decode<mirror::ObjectArray<mirror::Object>*>(javaArgs);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080066 MethodHelper mh(m);
67 const DexFile::TypeList* classes = mh.GetParameterTypeList();
68 uint32_t classes_size = classes == NULL ? 0 : classes->Size();
69 uint32_t arg_count = (objects != NULL) ? objects->GetLength() : 0;
70 if (arg_count != classes_size) {
Ian Rogers62d6c772013-02-27 08:32:07 -080071 ThrowIllegalArgumentException(NULL,
72 StringPrintf("Wrong number of arguments; expected %d, got %d",
73 classes_size, arg_count).c_str());
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070074 return NULL;
75 }
76
77 // Translate javaArgs to a jvalue[].
78 UniquePtr<jvalue[]> args(new jvalue[arg_count]);
79 JValue* decoded_args = reinterpret_cast<JValue*>(args.get());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080080 for (uint32_t i = 0; i < arg_count; ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080081 mirror::Object* arg = objects->Get(i);
82 mirror::Class* dst_class = mh.GetClassFromTypeIdx(classes->GetTypeItem(i).type_idx_);
Elliott Hughes37f77752012-05-21 15:12:47 -070083 if (!UnboxPrimitiveForArgument(arg, dst_class, decoded_args[i], m, i)) {
84 return NULL;
85 }
86 if (!dst_class->IsPrimitive()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070087 args[i].l = soa.AddLocalReference<jobject>(arg);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070088 }
89 }
90
91 // Invoke the method.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070092 JValue value(InvokeWithJValues(soa, javaReceiver, mid, args.get()));
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070093
94 // Wrap any exception with "Ljava/lang/reflect/InvocationTargetException;" and return early.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070095 if (soa.Self()->IsExceptionPending()) {
96 jthrowable th = soa.Env()->ExceptionOccurred();
97 soa.Env()->ExceptionClear();
98 jclass exception_class = soa.Env()->FindClass("java/lang/reflect/InvocationTargetException");
99 jmethodID mid = soa.Env()->GetMethodID(exception_class, "<init>", "(Ljava/lang/Throwable;)V");
100 jobject exception_instance = soa.Env()->NewObject(exception_class, mid, th);
101 soa.Env()->Throw(reinterpret_cast<jthrowable>(exception_instance));
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700102 return NULL;
103 }
104
105 // Box if necessary and return.
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800106 return soa.AddLocalReference<jobject>(BoxPrimitive(mh.GetReturnType()->GetPrimitiveType(), value));
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700107}
108
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800109bool VerifyObjectInClass(mirror::Object* o, mirror::Class* c) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700110 if (o == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800111 ThrowNullPointerException(NULL, "null receiver");
112 return false;
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700113 } else if (!o->InstanceOf(c)) {
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700114 std::string expected_class_name(PrettyDescriptor(c));
115 std::string actual_class_name(PrettyTypeOf(o));
Ian Rogers62d6c772013-02-27 08:32:07 -0800116 ThrowIllegalArgumentException(NULL,
117 StringPrintf("Expected receiver of type %s, but got %s",
118 expected_class_name.c_str(),
119 actual_class_name.c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700120 return false;
121 }
122 return true;
123}
124
Ian Rogers62d6c772013-02-27 08:32:07 -0800125bool ConvertPrimitiveValue(const ThrowLocation* throw_location, bool unbox_for_result,
126 Primitive::Type srcType, Primitive::Type dstType,
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700127 const JValue& src, JValue& dst) {
Jesse Wilsonc129a6b2011-11-24 14:47:46 -0500128 CHECK(srcType != Primitive::kPrimNot && dstType != Primitive::kPrimNot);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700129 switch (dstType) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700130 case Primitive::kPrimBoolean:
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700131 if (srcType == Primitive::kPrimBoolean) {
132 dst.SetZ(src.GetZ());
133 return true;
134 }
135 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700136 case Primitive::kPrimChar:
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700137 if (srcType == Primitive::kPrimChar) {
138 dst.SetC(src.GetC());
139 return true;
140 }
141 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700142 case Primitive::kPrimByte:
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700143 if (srcType == Primitive::kPrimByte) {
144 dst.SetB(src.GetB());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700145 return true;
146 }
147 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700148 case Primitive::kPrimShort:
149 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimShort) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700150 dst.SetS(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700151 return true;
152 }
153 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700154 case Primitive::kPrimInt:
155 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
156 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700157 dst.SetI(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700158 return true;
159 }
160 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700161 case Primitive::kPrimLong:
162 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
163 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700164 dst.SetJ(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700165 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700166 } else if (srcType == Primitive::kPrimLong) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700167 dst.SetJ(src.GetJ());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700168 return true;
169 }
170 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700171 case Primitive::kPrimFloat:
172 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
173 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700174 dst.SetF(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700175 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700176 } else if (srcType == Primitive::kPrimLong) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700177 dst.SetF(src.GetJ());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700178 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700179 } else if (srcType == Primitive::kPrimFloat) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700180 dst.SetF(src.GetF());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700181 return true;
182 }
183 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700184 case Primitive::kPrimDouble:
185 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
186 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700187 dst.SetD(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700188 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700189 } else if (srcType == Primitive::kPrimLong) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700190 dst.SetD(src.GetJ());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700191 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700192 } else if (srcType == Primitive::kPrimFloat) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700193 dst.SetD(src.GetF());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700194 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700195 } else if (srcType == Primitive::kPrimDouble) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700196 dst.SetJ(src.GetJ());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700197 return true;
198 }
199 break;
200 default:
201 break;
202 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800203 if (!unbox_for_result) {
204 ThrowIllegalArgumentException(throw_location,
205 StringPrintf("Invalid primitive conversion from %s to %s",
206 PrettyDescriptor(srcType).c_str(),
207 PrettyDescriptor(dstType).c_str()).c_str());
208 } else {
209 ThrowClassCastException(throw_location,
210 StringPrintf("Couldn't convert result of type %s to %s",
211 PrettyDescriptor(srcType).c_str(),
Brian Carlstromdf629502013-07-17 22:39:56 -0700212 PrettyDescriptor(dstType).c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800213 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700214 return false;
215}
216
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800217mirror::Object* BoxPrimitive(Primitive::Type src_class, const JValue& value) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700218 if (src_class == Primitive::kPrimNot) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800219 return value.GetL();
Elliott Hughes418d20f2011-09-22 14:00:39 -0700220 }
221
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700222 jmethodID m = NULL;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700223 switch (src_class) {
224 case Primitive::kPrimBoolean:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700225 m = WellKnownClasses::java_lang_Boolean_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700226 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700227 case Primitive::kPrimByte:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700228 m = WellKnownClasses::java_lang_Byte_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700229 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700230 case Primitive::kPrimChar:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700231 m = WellKnownClasses::java_lang_Character_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700232 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700233 case Primitive::kPrimDouble:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700234 m = WellKnownClasses::java_lang_Double_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700235 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700236 case Primitive::kPrimFloat:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700237 m = WellKnownClasses::java_lang_Float_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700238 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700239 case Primitive::kPrimInt:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700240 m = WellKnownClasses::java_lang_Integer_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700241 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700242 case Primitive::kPrimLong:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700243 m = WellKnownClasses::java_lang_Long_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700244 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700245 case Primitive::kPrimShort:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700246 m = WellKnownClasses::java_lang_Short_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700247 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700248 case Primitive::kPrimVoid:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700249 // There's no such thing as a void field, and void methods invoked via reflection return null.
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800250 return NULL;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700251 default:
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700252 LOG(FATAL) << static_cast<int>(src_class);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700253 }
254
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700255 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogers50b35e22012-10-04 10:09:15 -0700256 if (kIsDebugBuild) {
257 CHECK_EQ(soa.Self()->GetState(), kRunnable);
258 }
Jeff Hao5d917302013-02-27 17:57:33 -0800259
260 ArgArray arg_array(NULL, 0);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800261 JValue result;
Jeff Hao5d917302013-02-27 17:57:33 -0800262 if (src_class == Primitive::kPrimDouble || src_class == Primitive::kPrimLong) {
263 arg_array.AppendWide(value.GetJ());
264 } else {
265 arg_array.Append(value.GetI());
266 }
267
268 soa.DecodeMethod(m)->Invoke(soa.Self(), arg_array.GetArray(), arg_array.GetNumBytes(),
Jeff Hao6474d192013-03-26 14:08:09 -0700269 &result, 'L');
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800270 return result.GetL();
Elliott Hughes418d20f2011-09-22 14:00:39 -0700271}
272
Brian Carlstromea46f952013-07-30 01:26:50 -0700273static std::string UnboxingFailureKind(mirror::ArtMethod* m, int index, mirror::ArtField* f)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700274 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700275 if (m != NULL && index != -1) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700276 ++index; // Humans count from 1.
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700277 return StringPrintf("method %s argument %d", PrettyMethod(m, false).c_str(), index);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700278 }
279 if (f != NULL) {
280 return "field " + PrettyField(f, false);
281 }
282 return "result";
283}
284
Ian Rogers62d6c772013-02-27 08:32:07 -0800285static bool UnboxPrimitive(const ThrowLocation* throw_location, mirror::Object* o,
286 mirror::Class* dst_class, JValue& unboxed_value,
Brian Carlstromea46f952013-07-30 01:26:50 -0700287 mirror::ArtMethod* m, int index, mirror::ArtField* f)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700288 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800289 bool unbox_for_result = (f == NULL) && (index == -1);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700290 if (!dst_class->IsPrimitive()) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800291 if (UNLIKELY(o != NULL && !o->InstanceOf(dst_class))) {
292 if (!unbox_for_result) {
293 ThrowIllegalArgumentException(throw_location,
294 StringPrintf("%s has type %s, got %s",
295 UnboxingFailureKind(m, index, f).c_str(),
296 PrettyDescriptor(dst_class).c_str(),
297 PrettyTypeOf(o).c_str()).c_str());
298 } else {
299 ThrowClassCastException(throw_location,
300 StringPrintf("Couldn't convert result of type %s to %s",
301 PrettyTypeOf(o).c_str(),
Brian Carlstromdf629502013-07-17 22:39:56 -0700302 PrettyDescriptor(dst_class).c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800303 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700304 return false;
305 }
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700306 unboxed_value.SetL(o);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700307 return true;
Ian Rogers62d6c772013-02-27 08:32:07 -0800308 }
309 if (UNLIKELY(dst_class->GetPrimitiveType() == Primitive::kPrimVoid)) {
310 ThrowIllegalArgumentException(throw_location,
311 StringPrintf("Can't unbox %s to void",
312 UnboxingFailureKind(m, index, f).c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700313 return false;
314 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800315 if (UNLIKELY(o == NULL)) {
316 if (!unbox_for_result) {
317 ThrowIllegalArgumentException(throw_location,
318 StringPrintf("%s has type %s, got null",
319 UnboxingFailureKind(m, index, f).c_str(),
320 PrettyDescriptor(dst_class).c_str()).c_str());
321 } else {
322 ThrowNullPointerException(throw_location,
323 StringPrintf("Expected to unbox a '%s' primitive type but was returned null",
324 PrettyDescriptor(dst_class).c_str()).c_str());
325 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700326 return false;
327 }
328
Elliott Hughes1d878f32012-04-11 15:17:54 -0700329 JValue boxed_value;
Ian Rogersdfb325e2013-10-30 01:00:44 -0700330 const StringPiece src_descriptor(ClassHelper(o->GetClass()).GetDescriptor());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800331 mirror::Class* src_class = NULL;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700332 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromea46f952013-07-30 01:26:50 -0700333 mirror::ArtField* primitive_field = o->GetClass()->GetIFields()->Get(0);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800334 if (src_descriptor == "Ljava/lang/Boolean;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700335 src_class = class_linker->FindPrimitiveClass('Z');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700336 boxed_value.SetZ(primitive_field->GetBoolean(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800337 } else if (src_descriptor == "Ljava/lang/Byte;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700338 src_class = class_linker->FindPrimitiveClass('B');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700339 boxed_value.SetB(primitive_field->GetByte(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800340 } else if (src_descriptor == "Ljava/lang/Character;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700341 src_class = class_linker->FindPrimitiveClass('C');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700342 boxed_value.SetC(primitive_field->GetChar(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800343 } else if (src_descriptor == "Ljava/lang/Float;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700344 src_class = class_linker->FindPrimitiveClass('F');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700345 boxed_value.SetF(primitive_field->GetFloat(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800346 } else if (src_descriptor == "Ljava/lang/Double;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700347 src_class = class_linker->FindPrimitiveClass('D');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700348 boxed_value.SetD(primitive_field->GetDouble(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800349 } else if (src_descriptor == "Ljava/lang/Integer;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700350 src_class = class_linker->FindPrimitiveClass('I');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700351 boxed_value.SetI(primitive_field->GetInt(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800352 } else if (src_descriptor == "Ljava/lang/Long;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700353 src_class = class_linker->FindPrimitiveClass('J');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700354 boxed_value.SetJ(primitive_field->GetLong(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800355 } else if (src_descriptor == "Ljava/lang/Short;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700356 src_class = class_linker->FindPrimitiveClass('S');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700357 boxed_value.SetS(primitive_field->GetShort(o));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700358 } else {
Ian Rogers62d6c772013-02-27 08:32:07 -0800359 ThrowIllegalArgumentException(throw_location,
360 StringPrintf("%s has type %s, got %s",
361 UnboxingFailureKind(m, index, f).c_str(),
362 PrettyDescriptor(dst_class).c_str(),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700363 PrettyDescriptor(src_descriptor.data()).c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700364 return false;
365 }
366
Ian Rogers62d6c772013-02-27 08:32:07 -0800367 return ConvertPrimitiveValue(throw_location, unbox_for_result,
368 src_class->GetPrimitiveType(), dst_class->GetPrimitiveType(),
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700369 boxed_value, unboxed_value);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700370}
371
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800372bool UnboxPrimitiveForArgument(mirror::Object* o, mirror::Class* dst_class, JValue& unboxed_value,
Brian Carlstromea46f952013-07-30 01:26:50 -0700373 mirror::ArtMethod* m, size_t index) {
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700374 CHECK(m != NULL);
Ian Rogers62d6c772013-02-27 08:32:07 -0800375 return UnboxPrimitive(NULL, o, dst_class, unboxed_value, m, index, NULL);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700376}
377
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800378bool UnboxPrimitiveForField(mirror::Object* o, mirror::Class* dst_class, JValue& unboxed_value,
Brian Carlstromea46f952013-07-30 01:26:50 -0700379 mirror::ArtField* f) {
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700380 CHECK(f != NULL);
Ian Rogers62d6c772013-02-27 08:32:07 -0800381 return UnboxPrimitive(NULL, o, dst_class, unboxed_value, NULL, -1, f);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700382}
383
Ian Rogers62d6c772013-02-27 08:32:07 -0800384bool UnboxPrimitiveForResult(const ThrowLocation& throw_location, mirror::Object* o,
385 mirror::Class* dst_class, JValue& unboxed_value) {
386 return UnboxPrimitive(&throw_location, o, dst_class, unboxed_value, NULL, -1, NULL);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700387}
388
Elliott Hughes418d20f2011-09-22 14:00:39 -0700389} // namespace art