blob: 8e478ff96910c7d5ff008966301c8944468f5e08 [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"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080024#include "mirror/abstract_method.h"
25#include "mirror/abstract_method-inl.h"
26#include "mirror/class.h"
27#include "mirror/class-inl.h"
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080028#include "mirror/field-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029#include "mirror/object_array.h"
30#include "mirror/object_array-inl.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080031#include "object_utils.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070032#include "scoped_thread_state_change.h"
33#include "well_known_classes.h"
Elliott Hughes418d20f2011-09-22 14:00:39 -070034
Elliott Hughes418d20f2011-09-22 14:00:39 -070035namespace art {
36
Ian Rogers00f7d0e2012-07-19 15:28:27 -070037jobject InvokeMethod(const ScopedObjectAccess& soa, jobject javaMethod, jobject javaReceiver,
Ian Rogers365c1022012-06-22 15:05:28 -070038 jobject javaArgs) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070039 jmethodID mid = soa.Env()->FromReflectedMethod(javaMethod);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040 mirror::AbstractMethod* m = soa.DecodeMethod(mid);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070041
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080042 mirror::Class* declaring_class = m->GetDeclaringClass();
Ian Rogers0045a292012-03-31 21:08:41 -070043 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(declaring_class, true, true)) {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070044 return NULL;
45 }
46
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080047 mirror::Object* receiver = NULL;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070048 if (!m->IsStatic()) {
49 // Check that the receiver is non-null and an instance of the field's declaring class.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080050 receiver = soa.Decode<mirror::Object*>(javaReceiver);
Elliott Hugheseac76672012-05-24 21:56:51 -070051 if (!VerifyObjectInClass(receiver, declaring_class)) {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070052 return NULL;
53 }
54
55 // Find the actual implementation of the virtual method.
56 m = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(m);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070057 mid = soa.EncodeMethod(m);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070058 }
59
60 // Get our arrays of arguments and their types, and check they're the same size.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080061 mirror::ObjectArray<mirror::Object>* objects =
62 soa.Decode<mirror::ObjectArray<mirror::Object>*>(javaArgs);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080063 MethodHelper mh(m);
64 const DexFile::TypeList* classes = mh.GetParameterTypeList();
65 uint32_t classes_size = classes == NULL ? 0 : classes->Size();
66 uint32_t arg_count = (objects != NULL) ? objects->GetLength() : 0;
67 if (arg_count != classes_size) {
Ian Rogers62d6c772013-02-27 08:32:07 -080068 ThrowIllegalArgumentException(NULL,
69 StringPrintf("Wrong number of arguments; expected %d, got %d",
70 classes_size, arg_count).c_str());
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070071 return NULL;
72 }
73
74 // Translate javaArgs to a jvalue[].
75 UniquePtr<jvalue[]> args(new jvalue[arg_count]);
76 JValue* decoded_args = reinterpret_cast<JValue*>(args.get());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080077 for (uint32_t i = 0; i < arg_count; ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080078 mirror::Object* arg = objects->Get(i);
79 mirror::Class* dst_class = mh.GetClassFromTypeIdx(classes->GetTypeItem(i).type_idx_);
Elliott Hughes37f77752012-05-21 15:12:47 -070080 if (!UnboxPrimitiveForArgument(arg, dst_class, decoded_args[i], m, i)) {
81 return NULL;
82 }
83 if (!dst_class->IsPrimitive()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070084 args[i].l = soa.AddLocalReference<jobject>(arg);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070085 }
86 }
87
88 // Invoke the method.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070089 JValue value(InvokeWithJValues(soa, javaReceiver, mid, args.get()));
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070090
91 // Wrap any exception with "Ljava/lang/reflect/InvocationTargetException;" and return early.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070092 if (soa.Self()->IsExceptionPending()) {
93 jthrowable th = soa.Env()->ExceptionOccurred();
94 soa.Env()->ExceptionClear();
95 jclass exception_class = soa.Env()->FindClass("java/lang/reflect/InvocationTargetException");
96 jmethodID mid = soa.Env()->GetMethodID(exception_class, "<init>", "(Ljava/lang/Throwable;)V");
97 jobject exception_instance = soa.Env()->NewObject(exception_class, mid, th);
98 soa.Env()->Throw(reinterpret_cast<jthrowable>(exception_instance));
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070099 return NULL;
100 }
101
102 // Box if necessary and return.
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800103 return soa.AddLocalReference<jobject>(BoxPrimitive(mh.GetReturnType()->GetPrimitiveType(), value));
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700104}
105
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800106bool VerifyObjectInClass(mirror::Object* o, mirror::Class* c) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700107 if (o == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800108 ThrowNullPointerException(NULL, "null receiver");
109 return false;
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700110 } else if (!o->InstanceOf(c)) {
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700111 std::string expected_class_name(PrettyDescriptor(c));
112 std::string actual_class_name(PrettyTypeOf(o));
Ian Rogers62d6c772013-02-27 08:32:07 -0800113 ThrowIllegalArgumentException(NULL,
114 StringPrintf("Expected receiver of type %s, but got %s",
115 expected_class_name.c_str(),
116 actual_class_name.c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700117 return false;
118 }
119 return true;
120}
121
Ian Rogers62d6c772013-02-27 08:32:07 -0800122bool ConvertPrimitiveValue(const ThrowLocation* throw_location, bool unbox_for_result,
123 Primitive::Type srcType, Primitive::Type dstType,
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700124 const JValue& src, JValue& dst) {
Jesse Wilsonc129a6b2011-11-24 14:47:46 -0500125 CHECK(srcType != Primitive::kPrimNot && dstType != Primitive::kPrimNot);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700126 switch (dstType) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700127 case Primitive::kPrimBoolean:
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700128 if (srcType == Primitive::kPrimBoolean) {
129 dst.SetZ(src.GetZ());
130 return true;
131 }
132 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700133 case Primitive::kPrimChar:
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700134 if (srcType == Primitive::kPrimChar) {
135 dst.SetC(src.GetC());
136 return true;
137 }
138 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700139 case Primitive::kPrimByte:
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700140 if (srcType == Primitive::kPrimByte) {
141 dst.SetB(src.GetB());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700142 return true;
143 }
144 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700145 case Primitive::kPrimShort:
146 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimShort) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700147 dst.SetS(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700148 return true;
149 }
150 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700151 case Primitive::kPrimInt:
152 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
153 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700154 dst.SetI(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700155 return true;
156 }
157 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700158 case Primitive::kPrimLong:
159 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
160 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700161 dst.SetJ(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700162 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700163 } else if (srcType == Primitive::kPrimLong) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700164 dst.SetJ(src.GetJ());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700165 return true;
166 }
167 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700168 case Primitive::kPrimFloat:
169 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
170 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700171 dst.SetF(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700172 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700173 } else if (srcType == Primitive::kPrimLong) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700174 dst.SetF(src.GetJ());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700175 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700176 } else if (srcType == Primitive::kPrimFloat) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700177 dst.SetF(src.GetF());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700178 return true;
179 }
180 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700181 case Primitive::kPrimDouble:
182 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
183 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700184 dst.SetD(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700185 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700186 } else if (srcType == Primitive::kPrimLong) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700187 dst.SetD(src.GetJ());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700188 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700189 } else if (srcType == Primitive::kPrimFloat) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700190 dst.SetD(src.GetF());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700191 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700192 } else if (srcType == Primitive::kPrimDouble) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700193 dst.SetJ(src.GetJ());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700194 return true;
195 }
196 break;
197 default:
198 break;
199 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800200 if (!unbox_for_result) {
201 ThrowIllegalArgumentException(throw_location,
202 StringPrintf("Invalid primitive conversion from %s to %s",
203 PrettyDescriptor(srcType).c_str(),
204 PrettyDescriptor(dstType).c_str()).c_str());
205 } else {
206 ThrowClassCastException(throw_location,
207 StringPrintf("Couldn't convert result of type %s to %s",
208 PrettyDescriptor(srcType).c_str(),
Brian Carlstromdf629502013-07-17 22:39:56 -0700209 PrettyDescriptor(dstType).c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800210 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700211 return false;
212}
213
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800214mirror::Object* BoxPrimitive(Primitive::Type src_class, const JValue& value) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700215 if (src_class == Primitive::kPrimNot) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800216 return value.GetL();
Elliott Hughes418d20f2011-09-22 14:00:39 -0700217 }
218
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700219 jmethodID m = NULL;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700220 switch (src_class) {
221 case Primitive::kPrimBoolean:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700222 m = WellKnownClasses::java_lang_Boolean_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700223 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700224 case Primitive::kPrimByte:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700225 m = WellKnownClasses::java_lang_Byte_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700226 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700227 case Primitive::kPrimChar:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700228 m = WellKnownClasses::java_lang_Character_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700229 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700230 case Primitive::kPrimDouble:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700231 m = WellKnownClasses::java_lang_Double_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700232 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700233 case Primitive::kPrimFloat:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700234 m = WellKnownClasses::java_lang_Float_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700235 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700236 case Primitive::kPrimInt:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700237 m = WellKnownClasses::java_lang_Integer_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700238 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700239 case Primitive::kPrimLong:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700240 m = WellKnownClasses::java_lang_Long_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700241 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700242 case Primitive::kPrimShort:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700243 m = WellKnownClasses::java_lang_Short_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700244 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700245 case Primitive::kPrimVoid:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700246 // There's no such thing as a void field, and void methods invoked via reflection return null.
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800247 return NULL;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700248 default:
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700249 LOG(FATAL) << static_cast<int>(src_class);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700250 }
251
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700252 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogers50b35e22012-10-04 10:09:15 -0700253 if (kIsDebugBuild) {
254 CHECK_EQ(soa.Self()->GetState(), kRunnable);
255 }
Jeff Hao5d917302013-02-27 17:57:33 -0800256
257 ArgArray arg_array(NULL, 0);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800258 JValue result;
Jeff Hao5d917302013-02-27 17:57:33 -0800259 if (src_class == Primitive::kPrimDouble || src_class == Primitive::kPrimLong) {
260 arg_array.AppendWide(value.GetJ());
261 } else {
262 arg_array.Append(value.GetI());
263 }
264
265 soa.DecodeMethod(m)->Invoke(soa.Self(), arg_array.GetArray(), arg_array.GetNumBytes(),
Jeff Hao6474d192013-03-26 14:08:09 -0700266 &result, 'L');
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800267 return result.GetL();
Elliott Hughes418d20f2011-09-22 14:00:39 -0700268}
269
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800270static std::string UnboxingFailureKind(mirror::AbstractMethod* m, int index, mirror::Field* f)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700271 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700272 if (m != NULL && index != -1) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700273 ++index; // Humans count from 1.
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700274 return StringPrintf("method %s argument %d", PrettyMethod(m, false).c_str(), index);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700275 }
276 if (f != NULL) {
277 return "field " + PrettyField(f, false);
278 }
279 return "result";
280}
281
Ian Rogers62d6c772013-02-27 08:32:07 -0800282static bool UnboxPrimitive(const ThrowLocation* throw_location, mirror::Object* o,
283 mirror::Class* dst_class, JValue& unboxed_value,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800284 mirror::AbstractMethod* m, int index, mirror::Field* f)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700285 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800286 bool unbox_for_result = (f == NULL) && (index == -1);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700287 if (!dst_class->IsPrimitive()) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800288 if (UNLIKELY(o != NULL && !o->InstanceOf(dst_class))) {
289 if (!unbox_for_result) {
290 ThrowIllegalArgumentException(throw_location,
291 StringPrintf("%s has type %s, got %s",
292 UnboxingFailureKind(m, index, f).c_str(),
293 PrettyDescriptor(dst_class).c_str(),
294 PrettyTypeOf(o).c_str()).c_str());
295 } else {
296 ThrowClassCastException(throw_location,
297 StringPrintf("Couldn't convert result of type %s to %s",
298 PrettyTypeOf(o).c_str(),
Brian Carlstromdf629502013-07-17 22:39:56 -0700299 PrettyDescriptor(dst_class).c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800300 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700301 return false;
302 }
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700303 unboxed_value.SetL(o);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700304 return true;
Ian Rogers62d6c772013-02-27 08:32:07 -0800305 }
306 if (UNLIKELY(dst_class->GetPrimitiveType() == Primitive::kPrimVoid)) {
307 ThrowIllegalArgumentException(throw_location,
308 StringPrintf("Can't unbox %s to void",
309 UnboxingFailureKind(m, index, f).c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700310 return false;
311 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800312 if (UNLIKELY(o == NULL)) {
313 if (!unbox_for_result) {
314 ThrowIllegalArgumentException(throw_location,
315 StringPrintf("%s has type %s, got null",
316 UnboxingFailureKind(m, index, f).c_str(),
317 PrettyDescriptor(dst_class).c_str()).c_str());
318 } else {
319 ThrowNullPointerException(throw_location,
320 StringPrintf("Expected to unbox a '%s' primitive type but was returned null",
321 PrettyDescriptor(dst_class).c_str()).c_str());
322 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700323 return false;
324 }
325
Elliott Hughes1d878f32012-04-11 15:17:54 -0700326 JValue boxed_value;
Elliott Hughes95572412011-12-13 18:14:20 -0800327 std::string src_descriptor(ClassHelper(o->GetClass()).GetDescriptor());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800328 mirror::Class* src_class = NULL;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700329 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800330 mirror::Field* primitive_field = o->GetClass()->GetIFields()->Get(0);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800331 if (src_descriptor == "Ljava/lang/Boolean;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700332 src_class = class_linker->FindPrimitiveClass('Z');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700333 boxed_value.SetZ(primitive_field->GetBoolean(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800334 } else if (src_descriptor == "Ljava/lang/Byte;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700335 src_class = class_linker->FindPrimitiveClass('B');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700336 boxed_value.SetB(primitive_field->GetByte(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800337 } else if (src_descriptor == "Ljava/lang/Character;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700338 src_class = class_linker->FindPrimitiveClass('C');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700339 boxed_value.SetC(primitive_field->GetChar(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800340 } else if (src_descriptor == "Ljava/lang/Float;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700341 src_class = class_linker->FindPrimitiveClass('F');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700342 boxed_value.SetF(primitive_field->GetFloat(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800343 } else if (src_descriptor == "Ljava/lang/Double;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700344 src_class = class_linker->FindPrimitiveClass('D');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700345 boxed_value.SetD(primitive_field->GetDouble(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800346 } else if (src_descriptor == "Ljava/lang/Integer;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700347 src_class = class_linker->FindPrimitiveClass('I');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700348 boxed_value.SetI(primitive_field->GetInt(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800349 } else if (src_descriptor == "Ljava/lang/Long;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700350 src_class = class_linker->FindPrimitiveClass('J');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700351 boxed_value.SetJ(primitive_field->GetLong(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800352 } else if (src_descriptor == "Ljava/lang/Short;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700353 src_class = class_linker->FindPrimitiveClass('S');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700354 boxed_value.SetS(primitive_field->GetShort(o));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700355 } else {
Ian Rogers62d6c772013-02-27 08:32:07 -0800356 ThrowIllegalArgumentException(throw_location,
357 StringPrintf("%s has type %s, got %s",
358 UnboxingFailureKind(m, index, f).c_str(),
359 PrettyDescriptor(dst_class).c_str(),
Brian Carlstromdf629502013-07-17 22:39:56 -0700360 PrettyDescriptor(src_descriptor.c_str()).c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700361 return false;
362 }
363
Ian Rogers62d6c772013-02-27 08:32:07 -0800364 return ConvertPrimitiveValue(throw_location, unbox_for_result,
365 src_class->GetPrimitiveType(), dst_class->GetPrimitiveType(),
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700366 boxed_value, unboxed_value);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700367}
368
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800369bool UnboxPrimitiveForArgument(mirror::Object* o, mirror::Class* dst_class, JValue& unboxed_value,
370 mirror::AbstractMethod* m, size_t index) {
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700371 CHECK(m != NULL);
Ian Rogers62d6c772013-02-27 08:32:07 -0800372 return UnboxPrimitive(NULL, o, dst_class, unboxed_value, m, index, NULL);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700373}
374
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800375bool UnboxPrimitiveForField(mirror::Object* o, mirror::Class* dst_class, JValue& unboxed_value,
376 mirror::Field* f) {
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700377 CHECK(f != NULL);
Ian Rogers62d6c772013-02-27 08:32:07 -0800378 return UnboxPrimitive(NULL, o, dst_class, unboxed_value, NULL, -1, f);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700379}
380
Ian Rogers62d6c772013-02-27 08:32:07 -0800381bool UnboxPrimitiveForResult(const ThrowLocation& throw_location, mirror::Object* o,
382 mirror::Class* dst_class, JValue& unboxed_value) {
383 return UnboxPrimitive(&throw_location, o, dst_class, unboxed_value, NULL, -1, NULL);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700384}
385
Elliott Hughes418d20f2011-09-22 14:00:39 -0700386} // namespace art