blob: 542f1a2e440722638148ffca667127d08ca57cc3 [file] [log] [blame]
Elliott Hughes418d20f2011-09-22 14:00:39 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "reflection.h"
18
19#include "class_linker.h"
20#include "jni_internal.h"
21#include "object.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080022#include "object_utils.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070023#include "scoped_thread_state_change.h"
24#include "well_known_classes.h"
Elliott Hughes418d20f2011-09-22 14:00:39 -070025
Elliott Hughes418d20f2011-09-22 14:00:39 -070026namespace art {
27
Ian Rogers00f7d0e2012-07-19 15:28:27 -070028jobject InvokeMethod(const ScopedObjectAccess& soa, jobject javaMethod, jobject javaReceiver,
Ian Rogers365c1022012-06-22 15:05:28 -070029 jobject javaArgs) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070030 jmethodID mid = soa.Env()->FromReflectedMethod(javaMethod);
31 Method* m = soa.DecodeMethod(mid);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070032
33 Class* declaring_class = m->GetDeclaringClass();
Ian Rogers0045a292012-03-31 21:08:41 -070034 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(declaring_class, true, true)) {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070035 return NULL;
36 }
37
38 Object* receiver = NULL;
39 if (!m->IsStatic()) {
40 // Check that the receiver is non-null and an instance of the field's declaring class.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070041 receiver = soa.Decode<Object*>(javaReceiver);
Elliott Hugheseac76672012-05-24 21:56:51 -070042 if (!VerifyObjectInClass(receiver, declaring_class)) {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070043 return NULL;
44 }
45
46 // Find the actual implementation of the virtual method.
47 m = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(m);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070048 mid = soa.EncodeMethod(m);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070049 }
50
51 // Get our arrays of arguments and their types, and check they're the same size.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070052 ObjectArray<Object>* objects = soa.Decode<ObjectArray<Object>*>(javaArgs);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080053 MethodHelper mh(m);
54 const DexFile::TypeList* classes = mh.GetParameterTypeList();
55 uint32_t classes_size = classes == NULL ? 0 : classes->Size();
56 uint32_t arg_count = (objects != NULL) ? objects->GetLength() : 0;
57 if (arg_count != classes_size) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070058 soa.Self()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070059 "wrong number of arguments; expected %d, got %d",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080060 classes_size, arg_count);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070061 return NULL;
62 }
63
64 // Translate javaArgs to a jvalue[].
65 UniquePtr<jvalue[]> args(new jvalue[arg_count]);
66 JValue* decoded_args = reinterpret_cast<JValue*>(args.get());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080067 for (uint32_t i = 0; i < arg_count; ++i) {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070068 Object* arg = objects->Get(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080069 Class* dst_class = mh.GetClassFromTypeIdx(classes->GetTypeItem(i).type_idx_);
Elliott Hughes37f77752012-05-21 15:12:47 -070070 if (!UnboxPrimitiveForArgument(arg, dst_class, decoded_args[i], m, i)) {
71 return NULL;
72 }
73 if (!dst_class->IsPrimitive()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070074 args[i].l = soa.AddLocalReference<jobject>(arg);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070075 }
76 }
77
78 // Invoke the method.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070079 JValue value(InvokeWithJValues(soa, javaReceiver, mid, args.get()));
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070080
81 // Wrap any exception with "Ljava/lang/reflect/InvocationTargetException;" and return early.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070082 if (soa.Self()->IsExceptionPending()) {
83 jthrowable th = soa.Env()->ExceptionOccurred();
84 soa.Env()->ExceptionClear();
85 jclass exception_class = soa.Env()->FindClass("java/lang/reflect/InvocationTargetException");
86 jmethodID mid = soa.Env()->GetMethodID(exception_class, "<init>", "(Ljava/lang/Throwable;)V");
87 jobject exception_instance = soa.Env()->NewObject(exception_class, mid, th);
88 soa.Env()->Throw(reinterpret_cast<jthrowable>(exception_instance));
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070089 return NULL;
90 }
91
92 // Box if necessary and return.
Elliott Hughesdbac3092012-03-16 18:00:30 -070093 BoxPrimitive(mh.GetReturnType()->GetPrimitiveType(), value);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070094 return soa.AddLocalReference<jobject>(value.GetL());
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070095}
96
Elliott Hugheseac76672012-05-24 21:56:51 -070097bool VerifyObjectInClass(Object* o, Class* c) {
Elliott Hughesb600b3f2012-03-14 13:57:24 -070098 const char* exception = NULL;
Elliott Hughes418d20f2011-09-22 14:00:39 -070099 if (o == NULL) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700100 exception = "Ljava/lang/NullPointerException;";
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700101 } else if (!o->InstanceOf(c)) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700102 exception = "Ljava/lang/IllegalArgumentException;";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700103 }
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700104 if (exception != NULL) {
105 std::string expected_class_name(PrettyDescriptor(c));
106 std::string actual_class_name(PrettyTypeOf(o));
Elliott Hugheseac76672012-05-24 21:56:51 -0700107 Thread::Current()->ThrowNewExceptionF(exception, "expected receiver of type %s, but got %s",
108 expected_class_name.c_str(), actual_class_name.c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700109 return false;
110 }
111 return true;
112}
113
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700114bool ConvertPrimitiveValue(Primitive::Type srcType, Primitive::Type dstType,
115 const JValue& src, JValue& dst) {
Jesse Wilsonc129a6b2011-11-24 14:47:46 -0500116 CHECK(srcType != Primitive::kPrimNot && dstType != Primitive::kPrimNot);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700117 switch (dstType) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700118 case Primitive::kPrimBoolean:
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700119 if (srcType == Primitive::kPrimBoolean) {
120 dst.SetZ(src.GetZ());
121 return true;
122 }
123 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700124 case Primitive::kPrimChar:
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700125 if (srcType == Primitive::kPrimChar) {
126 dst.SetC(src.GetC());
127 return true;
128 }
129 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700130 case Primitive::kPrimByte:
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700131 if (srcType == Primitive::kPrimByte) {
132 dst.SetB(src.GetB());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700133 return true;
134 }
135 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700136 case Primitive::kPrimShort:
137 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimShort) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700138 dst.SetS(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700139 return true;
140 }
141 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700142 case Primitive::kPrimInt:
143 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
144 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700145 dst.SetI(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::kPrimLong:
150 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
151 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700152 dst.SetJ(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700153 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700154 } else if (srcType == Primitive::kPrimLong) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700155 dst.SetJ(src.GetJ());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700156 return true;
157 }
158 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700159 case Primitive::kPrimFloat:
160 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
161 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700162 dst.SetF(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700163 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700164 } else if (srcType == Primitive::kPrimLong) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700165 dst.SetF(src.GetJ());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700166 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700167 } else if (srcType == Primitive::kPrimFloat) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700168 dst.SetF(src.GetF());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700169 return true;
170 }
171 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700172 case Primitive::kPrimDouble:
173 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
174 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700175 dst.SetD(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700176 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700177 } else if (srcType == Primitive::kPrimLong) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700178 dst.SetD(src.GetJ());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700179 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700180 } else if (srcType == Primitive::kPrimFloat) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700181 dst.SetD(src.GetF());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700182 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700183 } else if (srcType == Primitive::kPrimDouble) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700184 dst.SetJ(src.GetJ());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700185 return true;
186 }
187 break;
188 default:
189 break;
190 }
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700191 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700192 "invalid primitive conversion from %s to %s",
193 PrettyDescriptor(srcType).c_str(),
194 PrettyDescriptor(dstType).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700195 return false;
196}
197
Elliott Hughesdbac3092012-03-16 18:00:30 -0700198void BoxPrimitive(Primitive::Type src_class, JValue& value) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700199 if (src_class == Primitive::kPrimNot) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700200 return;
201 }
202
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700203 jmethodID m = NULL;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700204 switch (src_class) {
205 case Primitive::kPrimBoolean:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700206 m = WellKnownClasses::java_lang_Boolean_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700207 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700208 case Primitive::kPrimByte:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700209 m = WellKnownClasses::java_lang_Byte_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700210 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700211 case Primitive::kPrimChar:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700212 m = WellKnownClasses::java_lang_Character_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700213 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700214 case Primitive::kPrimDouble:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700215 m = WellKnownClasses::java_lang_Double_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700216 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700217 case Primitive::kPrimFloat:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700218 m = WellKnownClasses::java_lang_Float_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700219 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700220 case Primitive::kPrimInt:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700221 m = WellKnownClasses::java_lang_Integer_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700222 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700223 case Primitive::kPrimLong:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700224 m = WellKnownClasses::java_lang_Long_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700225 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700226 case Primitive::kPrimShort:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700227 m = WellKnownClasses::java_lang_Short_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700228 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700229 case Primitive::kPrimVoid:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700230 // There's no such thing as a void field, and void methods invoked via reflection return null.
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700231 value.SetL(NULL);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700232 return;
233 default:
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700234 LOG(FATAL) << static_cast<int>(src_class);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700235 }
236
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700237 if (kIsDebugBuild) {
238 MutexLock mu(*GlobalSynchronization::thread_suspend_count_lock_);
239 CHECK_EQ(Thread::Current()->GetState(), kRunnable);
240 }
241 ScopedObjectAccessUnchecked soa(Thread::Current());
Elliott Hughesdbac3092012-03-16 18:00:30 -0700242 JValue args[1] = { value };
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700243 soa.DecodeMethod(m)->Invoke(soa.Self(), NULL, args, &value);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700244}
245
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700246static std::string UnboxingFailureKind(Method* m, int index, Field* f)
247 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700248 if (m != NULL && index != -1) {
249 ++index; // Humans count from 1.
250 return StringPrintf("method %s argument %d", PrettyMethod(m, false).c_str(), index);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700251 }
252 if (f != NULL) {
253 return "field " + PrettyField(f, false);
254 }
255 return "result";
256}
257
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700258static bool UnboxPrimitive(Object* o, Class* dst_class, JValue& unboxed_value, Method* m,
259 int index, Field* f)
260 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700261 if (!dst_class->IsPrimitive()) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700262 if (o != NULL && !o->InstanceOf(dst_class)) {
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700263 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700264 "%s has type %s, got %s",
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700265 UnboxingFailureKind(m, index, f).c_str(),
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700266 PrettyDescriptor(dst_class).c_str(),
267 PrettyTypeOf(o).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700268 return false;
269 }
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700270 unboxed_value.SetL(o);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700271 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700272 } else if (dst_class->GetPrimitiveType() == Primitive::kPrimVoid) {
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700273 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
274 "can't unbox %s to void",
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700275 UnboxingFailureKind(m, index, f).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700276 return false;
277 }
278
279 if (o == NULL) {
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700280 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700281 "%s has type %s, got null",
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700282 UnboxingFailureKind(m, index, f).c_str(),
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700283 PrettyDescriptor(dst_class).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700284 return false;
285 }
286
Elliott Hughes1d878f32012-04-11 15:17:54 -0700287 JValue boxed_value;
Elliott Hughes95572412011-12-13 18:14:20 -0800288 std::string src_descriptor(ClassHelper(o->GetClass()).GetDescriptor());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700289 Class* src_class = NULL;
290 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
291 Field* primitive_field = o->GetClass()->GetIFields()->Get(0);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800292 if (src_descriptor == "Ljava/lang/Boolean;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700293 src_class = class_linker->FindPrimitiveClass('Z');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700294 boxed_value.SetZ(primitive_field->GetBoolean(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800295 } else if (src_descriptor == "Ljava/lang/Byte;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700296 src_class = class_linker->FindPrimitiveClass('B');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700297 boxed_value.SetB(primitive_field->GetByte(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800298 } else if (src_descriptor == "Ljava/lang/Character;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700299 src_class = class_linker->FindPrimitiveClass('C');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700300 boxed_value.SetC(primitive_field->GetChar(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800301 } else if (src_descriptor == "Ljava/lang/Float;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700302 src_class = class_linker->FindPrimitiveClass('F');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700303 boxed_value.SetF(primitive_field->GetFloat(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800304 } else if (src_descriptor == "Ljava/lang/Double;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700305 src_class = class_linker->FindPrimitiveClass('D');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700306 boxed_value.SetD(primitive_field->GetDouble(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800307 } else if (src_descriptor == "Ljava/lang/Integer;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700308 src_class = class_linker->FindPrimitiveClass('I');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700309 boxed_value.SetI(primitive_field->GetInt(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800310 } else if (src_descriptor == "Ljava/lang/Long;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700311 src_class = class_linker->FindPrimitiveClass('J');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700312 boxed_value.SetJ(primitive_field->GetLong(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800313 } else if (src_descriptor == "Ljava/lang/Short;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700314 src_class = class_linker->FindPrimitiveClass('S');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700315 boxed_value.SetS(primitive_field->GetShort(o));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700316 } else {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700317 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700318 "%s has type %s, got %s",
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700319 UnboxingFailureKind(m, index, f).c_str(),
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700320 PrettyDescriptor(dst_class).c_str(),
321 PrettyDescriptor(src_descriptor.c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700322 return false;
323 }
324
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700325 return ConvertPrimitiveValue(src_class->GetPrimitiveType(), dst_class->GetPrimitiveType(),
326 boxed_value, unboxed_value);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700327}
328
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700329bool UnboxPrimitiveForArgument(Object* o, Class* dst_class, JValue& unboxed_value, Method* m, size_t index) {
330 CHECK(m != NULL);
331 return UnboxPrimitive(o, dst_class, unboxed_value, m, index, NULL);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700332}
333
334bool UnboxPrimitiveForField(Object* o, Class* dst_class, JValue& unboxed_value, Field* f) {
335 CHECK(f != NULL);
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700336 return UnboxPrimitive(o, dst_class, unboxed_value, NULL, -1, f);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700337}
338
339bool UnboxPrimitiveForResult(Object* o, Class* dst_class, JValue& unboxed_value) {
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700340 return UnboxPrimitive(o, dst_class, unboxed_value, NULL, -1, NULL);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700341}
342
Elliott Hughes418d20f2011-09-22 14:00:39 -0700343} // namespace art