blob: addb5a3f1a7e785f713a333cec0fd622a44df080 [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"
Jeff Hao5d917302013-02-27 17:57:33 -080020#include "invoke_arg_array_builder.h"
Elliott Hughes418d20f2011-09-22 14:00:39 -070021#include "jni_internal.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "mirror/abstract_method.h"
23#include "mirror/abstract_method-inl.h"
24#include "mirror/class.h"
25#include "mirror/class-inl.h"
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080026#include "mirror/field-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "mirror/object_array.h"
28#include "mirror/object_array-inl.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080029#include "object_utils.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070030#include "scoped_thread_state_change.h"
31#include "well_known_classes.h"
Elliott Hughes418d20f2011-09-22 14:00:39 -070032
Elliott Hughes418d20f2011-09-22 14:00:39 -070033namespace art {
34
Ian Rogers00f7d0e2012-07-19 15:28:27 -070035jobject InvokeMethod(const ScopedObjectAccess& soa, jobject javaMethod, jobject javaReceiver,
Ian Rogers365c1022012-06-22 15:05:28 -070036 jobject javaArgs) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070037 jmethodID mid = soa.Env()->FromReflectedMethod(javaMethod);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038 mirror::AbstractMethod* m = soa.DecodeMethod(mid);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070039
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040 mirror::Class* declaring_class = m->GetDeclaringClass();
Ian Rogers0045a292012-03-31 21:08:41 -070041 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(declaring_class, true, true)) {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070042 return NULL;
43 }
44
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080045 mirror::Object* receiver = NULL;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070046 if (!m->IsStatic()) {
47 // Check that the receiver is non-null and an instance of the field's declaring class.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048 receiver = soa.Decode<mirror::Object*>(javaReceiver);
Elliott Hugheseac76672012-05-24 21:56:51 -070049 if (!VerifyObjectInClass(receiver, declaring_class)) {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070050 return NULL;
51 }
52
53 // Find the actual implementation of the virtual method.
54 m = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(m);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070055 mid = soa.EncodeMethod(m);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070056 }
57
58 // Get our arrays of arguments and their types, and check they're the same size.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080059 mirror::ObjectArray<mirror::Object>* objects =
60 soa.Decode<mirror::ObjectArray<mirror::Object>*>(javaArgs);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080061 MethodHelper mh(m);
62 const DexFile::TypeList* classes = mh.GetParameterTypeList();
63 uint32_t classes_size = classes == NULL ? 0 : classes->Size();
64 uint32_t arg_count = (objects != NULL) ? objects->GetLength() : 0;
65 if (arg_count != classes_size) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070066 soa.Self()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070067 "wrong number of arguments; expected %d, got %d",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080068 classes_size, arg_count);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070069 return NULL;
70 }
71
72 // Translate javaArgs to a jvalue[].
73 UniquePtr<jvalue[]> args(new jvalue[arg_count]);
74 JValue* decoded_args = reinterpret_cast<JValue*>(args.get());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080075 for (uint32_t i = 0; i < arg_count; ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080076 mirror::Object* arg = objects->Get(i);
77 mirror::Class* dst_class = mh.GetClassFromTypeIdx(classes->GetTypeItem(i).type_idx_);
Elliott Hughes37f77752012-05-21 15:12:47 -070078 if (!UnboxPrimitiveForArgument(arg, dst_class, decoded_args[i], m, i)) {
79 return NULL;
80 }
81 if (!dst_class->IsPrimitive()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070082 args[i].l = soa.AddLocalReference<jobject>(arg);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070083 }
84 }
85
86 // Invoke the method.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070087 JValue value(InvokeWithJValues(soa, javaReceiver, mid, args.get()));
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070088
89 // Wrap any exception with "Ljava/lang/reflect/InvocationTargetException;" and return early.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070090 if (soa.Self()->IsExceptionPending()) {
91 jthrowable th = soa.Env()->ExceptionOccurred();
92 soa.Env()->ExceptionClear();
93 jclass exception_class = soa.Env()->FindClass("java/lang/reflect/InvocationTargetException");
94 jmethodID mid = soa.Env()->GetMethodID(exception_class, "<init>", "(Ljava/lang/Throwable;)V");
95 jobject exception_instance = soa.Env()->NewObject(exception_class, mid, th);
96 soa.Env()->Throw(reinterpret_cast<jthrowable>(exception_instance));
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070097 return NULL;
98 }
99
100 // Box if necessary and return.
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800101 return soa.AddLocalReference<jobject>(BoxPrimitive(mh.GetReturnType()->GetPrimitiveType(), value));
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700102}
103
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800104bool VerifyObjectInClass(mirror::Object* o, mirror::Class* c) {
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700105 const char* exception = NULL;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700106 if (o == NULL) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700107 exception = "Ljava/lang/NullPointerException;";
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700108 } else if (!o->InstanceOf(c)) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700109 exception = "Ljava/lang/IllegalArgumentException;";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700110 }
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700111 if (exception != NULL) {
112 std::string expected_class_name(PrettyDescriptor(c));
113 std::string actual_class_name(PrettyTypeOf(o));
Elliott Hugheseac76672012-05-24 21:56:51 -0700114 Thread::Current()->ThrowNewExceptionF(exception, "expected receiver of type %s, but got %s",
115 expected_class_name.c_str(), actual_class_name.c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700116 return false;
117 }
118 return true;
119}
120
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700121bool ConvertPrimitiveValue(Primitive::Type srcType, Primitive::Type dstType,
122 const JValue& src, JValue& dst) {
Jesse Wilsonc129a6b2011-11-24 14:47:46 -0500123 CHECK(srcType != Primitive::kPrimNot && dstType != Primitive::kPrimNot);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700124 switch (dstType) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700125 case Primitive::kPrimBoolean:
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700126 if (srcType == Primitive::kPrimBoolean) {
127 dst.SetZ(src.GetZ());
128 return true;
129 }
130 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700131 case Primitive::kPrimChar:
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700132 if (srcType == Primitive::kPrimChar) {
133 dst.SetC(src.GetC());
134 return true;
135 }
136 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700137 case Primitive::kPrimByte:
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700138 if (srcType == Primitive::kPrimByte) {
139 dst.SetB(src.GetB());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700140 return true;
141 }
142 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700143 case Primitive::kPrimShort:
144 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimShort) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700145 dst.SetS(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700146 return true;
147 }
148 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700149 case Primitive::kPrimInt:
150 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
151 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700152 dst.SetI(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700153 return true;
154 }
155 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700156 case Primitive::kPrimLong:
157 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
158 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700159 dst.SetJ(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700160 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700161 } else if (srcType == Primitive::kPrimLong) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700162 dst.SetJ(src.GetJ());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700163 return true;
164 }
165 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700166 case Primitive::kPrimFloat:
167 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
168 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700169 dst.SetF(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700170 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700171 } else if (srcType == Primitive::kPrimLong) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700172 dst.SetF(src.GetJ());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700173 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700174 } else if (srcType == Primitive::kPrimFloat) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700175 dst.SetF(src.GetF());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700176 return true;
177 }
178 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700179 case Primitive::kPrimDouble:
180 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
181 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700182 dst.SetD(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700183 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700184 } else if (srcType == Primitive::kPrimLong) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700185 dst.SetD(src.GetJ());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700186 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700187 } else if (srcType == Primitive::kPrimFloat) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700188 dst.SetD(src.GetF());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700189 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700190 } else if (srcType == Primitive::kPrimDouble) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700191 dst.SetJ(src.GetJ());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700192 return true;
193 }
194 break;
195 default:
196 break;
197 }
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700198 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700199 "invalid primitive conversion from %s to %s",
200 PrettyDescriptor(srcType).c_str(),
201 PrettyDescriptor(dstType).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700202 return false;
203}
204
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800205mirror::Object* BoxPrimitive(Primitive::Type src_class, const JValue& value) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700206 if (src_class == Primitive::kPrimNot) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800207 return value.GetL();
Elliott Hughes418d20f2011-09-22 14:00:39 -0700208 }
209
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700210 jmethodID m = NULL;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700211 switch (src_class) {
212 case Primitive::kPrimBoolean:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700213 m = WellKnownClasses::java_lang_Boolean_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700214 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700215 case Primitive::kPrimByte:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700216 m = WellKnownClasses::java_lang_Byte_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700217 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700218 case Primitive::kPrimChar:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700219 m = WellKnownClasses::java_lang_Character_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700220 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700221 case Primitive::kPrimDouble:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700222 m = WellKnownClasses::java_lang_Double_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700223 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700224 case Primitive::kPrimFloat:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700225 m = WellKnownClasses::java_lang_Float_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700226 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700227 case Primitive::kPrimInt:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700228 m = WellKnownClasses::java_lang_Integer_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700229 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700230 case Primitive::kPrimLong:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700231 m = WellKnownClasses::java_lang_Long_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700232 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700233 case Primitive::kPrimShort:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700234 m = WellKnownClasses::java_lang_Short_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700235 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700236 case Primitive::kPrimVoid:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700237 // There's no such thing as a void field, and void methods invoked via reflection return null.
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800238 return NULL;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700239 default:
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700240 LOG(FATAL) << static_cast<int>(src_class);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700241 }
242
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700243 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogers50b35e22012-10-04 10:09:15 -0700244 if (kIsDebugBuild) {
245 CHECK_EQ(soa.Self()->GetState(), kRunnable);
246 }
Jeff Hao5d917302013-02-27 17:57:33 -0800247
248 ArgArray arg_array(NULL, 0);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800249 JValue result;
Jeff Hao5d917302013-02-27 17:57:33 -0800250 JValue float_result;
251 if (src_class == Primitive::kPrimDouble || src_class == Primitive::kPrimLong) {
252 arg_array.AppendWide(value.GetJ());
253 } else {
254 arg_array.Append(value.GetI());
255 }
256
257 soa.DecodeMethod(m)->Invoke(soa.Self(), arg_array.GetArray(), arg_array.GetNumBytes(),
258 &result, &float_result);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800259 return result.GetL();
Elliott Hughes418d20f2011-09-22 14:00:39 -0700260}
261
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800262static std::string UnboxingFailureKind(mirror::AbstractMethod* m, int index, mirror::Field* f)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700263 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700264 if (m != NULL && index != -1) {
265 ++index; // Humans count from 1.
266 return StringPrintf("method %s argument %d", PrettyMethod(m, false).c_str(), index);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700267 }
268 if (f != NULL) {
269 return "field " + PrettyField(f, false);
270 }
271 return "result";
272}
273
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800274static bool UnboxPrimitive(mirror::Object* o, mirror::Class* dst_class, JValue& unboxed_value,
275 mirror::AbstractMethod* m, int index, mirror::Field* f)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700276 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700277 if (!dst_class->IsPrimitive()) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700278 if (o != NULL && !o->InstanceOf(dst_class)) {
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700279 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700280 "%s has type %s, got %s",
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700281 UnboxingFailureKind(m, index, f).c_str(),
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700282 PrettyDescriptor(dst_class).c_str(),
283 PrettyTypeOf(o).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700284 return false;
285 }
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700286 unboxed_value.SetL(o);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700287 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700288 } else if (dst_class->GetPrimitiveType() == Primitive::kPrimVoid) {
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700289 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
290 "can't unbox %s to void",
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700291 UnboxingFailureKind(m, index, f).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700292 return false;
293 }
294
295 if (o == NULL) {
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700296 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700297 "%s has type %s, got null",
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700298 UnboxingFailureKind(m, index, f).c_str(),
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700299 PrettyDescriptor(dst_class).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700300 return false;
301 }
302
Elliott Hughes1d878f32012-04-11 15:17:54 -0700303 JValue boxed_value;
Elliott Hughes95572412011-12-13 18:14:20 -0800304 std::string src_descriptor(ClassHelper(o->GetClass()).GetDescriptor());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800305 mirror::Class* src_class = NULL;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700306 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800307 mirror::Field* primitive_field = o->GetClass()->GetIFields()->Get(0);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800308 if (src_descriptor == "Ljava/lang/Boolean;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700309 src_class = class_linker->FindPrimitiveClass('Z');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700310 boxed_value.SetZ(primitive_field->GetBoolean(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800311 } else if (src_descriptor == "Ljava/lang/Byte;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700312 src_class = class_linker->FindPrimitiveClass('B');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700313 boxed_value.SetB(primitive_field->GetByte(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800314 } else if (src_descriptor == "Ljava/lang/Character;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700315 src_class = class_linker->FindPrimitiveClass('C');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700316 boxed_value.SetC(primitive_field->GetChar(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800317 } else if (src_descriptor == "Ljava/lang/Float;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700318 src_class = class_linker->FindPrimitiveClass('F');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700319 boxed_value.SetF(primitive_field->GetFloat(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800320 } else if (src_descriptor == "Ljava/lang/Double;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700321 src_class = class_linker->FindPrimitiveClass('D');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700322 boxed_value.SetD(primitive_field->GetDouble(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800323 } else if (src_descriptor == "Ljava/lang/Integer;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700324 src_class = class_linker->FindPrimitiveClass('I');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700325 boxed_value.SetI(primitive_field->GetInt(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800326 } else if (src_descriptor == "Ljava/lang/Long;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700327 src_class = class_linker->FindPrimitiveClass('J');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700328 boxed_value.SetJ(primitive_field->GetLong(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800329 } else if (src_descriptor == "Ljava/lang/Short;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700330 src_class = class_linker->FindPrimitiveClass('S');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700331 boxed_value.SetS(primitive_field->GetShort(o));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700332 } else {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700333 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700334 "%s has type %s, got %s",
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700335 UnboxingFailureKind(m, index, f).c_str(),
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700336 PrettyDescriptor(dst_class).c_str(),
337 PrettyDescriptor(src_descriptor.c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700338 return false;
339 }
340
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700341 return ConvertPrimitiveValue(src_class->GetPrimitiveType(), dst_class->GetPrimitiveType(),
342 boxed_value, unboxed_value);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700343}
344
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800345bool UnboxPrimitiveForArgument(mirror::Object* o, mirror::Class* dst_class, JValue& unboxed_value,
346 mirror::AbstractMethod* m, size_t index) {
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700347 CHECK(m != NULL);
348 return UnboxPrimitive(o, dst_class, unboxed_value, m, index, NULL);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700349}
350
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800351bool UnboxPrimitiveForField(mirror::Object* o, mirror::Class* dst_class, JValue& unboxed_value,
352 mirror::Field* f) {
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700353 CHECK(f != NULL);
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700354 return UnboxPrimitive(o, dst_class, unboxed_value, NULL, -1, f);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700355}
356
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800357bool UnboxPrimitiveForResult(mirror::Object* o, mirror::Class* dst_class, JValue& unboxed_value) {
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700358 return UnboxPrimitive(o, dst_class, unboxed_value, NULL, -1, NULL);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700359}
360
Elliott Hughes418d20f2011-09-22 14:00:39 -0700361} // namespace art