blob: 1ffad3fbf95aab0b3d29c02f89e4d2c47ee45f50 [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);
Mathieu Chartier66f19252012-09-18 08:57:04 -070031 AbstractMethod* 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.
Ian Rogersaf6e67a2013-01-16 08:38:37 -080093 return soa.AddLocalReference<jobject>(BoxPrimitive(mh.GetReturnType()->GetPrimitiveType(), value));
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070094}
95
Elliott Hugheseac76672012-05-24 21:56:51 -070096bool VerifyObjectInClass(Object* o, Class* c) {
Elliott Hughesb600b3f2012-03-14 13:57:24 -070097 const char* exception = NULL;
Elliott Hughes418d20f2011-09-22 14:00:39 -070098 if (o == NULL) {
Elliott Hugheseac76672012-05-24 21:56:51 -070099 exception = "Ljava/lang/NullPointerException;";
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700100 } else if (!o->InstanceOf(c)) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700101 exception = "Ljava/lang/IllegalArgumentException;";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700102 }
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700103 if (exception != NULL) {
104 std::string expected_class_name(PrettyDescriptor(c));
105 std::string actual_class_name(PrettyTypeOf(o));
Elliott Hugheseac76672012-05-24 21:56:51 -0700106 Thread::Current()->ThrowNewExceptionF(exception, "expected receiver of type %s, but got %s",
107 expected_class_name.c_str(), actual_class_name.c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700108 return false;
109 }
110 return true;
111}
112
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700113bool ConvertPrimitiveValue(Primitive::Type srcType, Primitive::Type dstType,
114 const JValue& src, JValue& dst) {
Jesse Wilsonc129a6b2011-11-24 14:47:46 -0500115 CHECK(srcType != Primitive::kPrimNot && dstType != Primitive::kPrimNot);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700116 switch (dstType) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700117 case Primitive::kPrimBoolean:
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700118 if (srcType == Primitive::kPrimBoolean) {
119 dst.SetZ(src.GetZ());
120 return true;
121 }
122 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700123 case Primitive::kPrimChar:
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700124 if (srcType == Primitive::kPrimChar) {
125 dst.SetC(src.GetC());
126 return true;
127 }
128 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700129 case Primitive::kPrimByte:
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700130 if (srcType == Primitive::kPrimByte) {
131 dst.SetB(src.GetB());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700132 return true;
133 }
134 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700135 case Primitive::kPrimShort:
136 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimShort) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700137 dst.SetS(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700138 return true;
139 }
140 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700141 case Primitive::kPrimInt:
142 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
143 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700144 dst.SetI(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700145 return true;
146 }
147 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700148 case Primitive::kPrimLong:
149 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
150 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700151 dst.SetJ(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700152 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700153 } else if (srcType == Primitive::kPrimLong) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700154 dst.SetJ(src.GetJ());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700155 return true;
156 }
157 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700158 case Primitive::kPrimFloat:
159 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
160 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700161 dst.SetF(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.SetF(src.GetJ());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700165 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700166 } else if (srcType == Primitive::kPrimFloat) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700167 dst.SetF(src.GetF());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700168 return true;
169 }
170 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700171 case Primitive::kPrimDouble:
172 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
173 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700174 dst.SetD(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.SetD(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.SetD(src.GetF());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700181 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700182 } else if (srcType == Primitive::kPrimDouble) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700183 dst.SetJ(src.GetJ());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700184 return true;
185 }
186 break;
187 default:
188 break;
189 }
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700190 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700191 "invalid primitive conversion from %s to %s",
192 PrettyDescriptor(srcType).c_str(),
193 PrettyDescriptor(dstType).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700194 return false;
195}
196
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800197Object* BoxPrimitive(Primitive::Type src_class, const JValue& value) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700198 if (src_class == Primitive::kPrimNot) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800199 return value.GetL();
Elliott Hughes418d20f2011-09-22 14:00:39 -0700200 }
201
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700202 jmethodID m = NULL;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700203 switch (src_class) {
204 case Primitive::kPrimBoolean:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700205 m = WellKnownClasses::java_lang_Boolean_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700206 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700207 case Primitive::kPrimByte:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700208 m = WellKnownClasses::java_lang_Byte_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700209 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700210 case Primitive::kPrimChar:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700211 m = WellKnownClasses::java_lang_Character_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700212 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700213 case Primitive::kPrimDouble:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700214 m = WellKnownClasses::java_lang_Double_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700215 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700216 case Primitive::kPrimFloat:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700217 m = WellKnownClasses::java_lang_Float_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700218 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700219 case Primitive::kPrimInt:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700220 m = WellKnownClasses::java_lang_Integer_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700221 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700222 case Primitive::kPrimLong:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700223 m = WellKnownClasses::java_lang_Long_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700224 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700225 case Primitive::kPrimShort:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700226 m = WellKnownClasses::java_lang_Short_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700227 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700228 case Primitive::kPrimVoid:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700229 // There's no such thing as a void field, and void methods invoked via reflection return null.
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800230 return NULL;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700231 default:
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700232 LOG(FATAL) << static_cast<int>(src_class);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700233 }
234
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700235 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogers50b35e22012-10-04 10:09:15 -0700236 if (kIsDebugBuild) {
237 CHECK_EQ(soa.Self()->GetState(), kRunnable);
238 }
Elliott Hughesdbac3092012-03-16 18:00:30 -0700239 JValue args[1] = { value };
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800240 JValue result;
241 soa.DecodeMethod(m)->Invoke(soa.Self(), NULL, args, &result);
242 return result.GetL();
Elliott Hughes418d20f2011-09-22 14:00:39 -0700243}
244
Mathieu Chartier66f19252012-09-18 08:57:04 -0700245static std::string UnboxingFailureKind(AbstractMethod* m, int index, Field* f)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700246 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700247 if (m != NULL && index != -1) {
248 ++index; // Humans count from 1.
249 return StringPrintf("method %s argument %d", PrettyMethod(m, false).c_str(), index);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700250 }
251 if (f != NULL) {
252 return "field " + PrettyField(f, false);
253 }
254 return "result";
255}
256
Mathieu Chartier66f19252012-09-18 08:57:04 -0700257static bool UnboxPrimitive(Object* o, Class* dst_class, JValue& unboxed_value, AbstractMethod* m,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700258 int index, Field* f)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700259 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700260 if (!dst_class->IsPrimitive()) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700261 if (o != NULL && !o->InstanceOf(dst_class)) {
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700262 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700263 "%s has type %s, got %s",
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700264 UnboxingFailureKind(m, index, f).c_str(),
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700265 PrettyDescriptor(dst_class).c_str(),
266 PrettyTypeOf(o).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700267 return false;
268 }
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700269 unboxed_value.SetL(o);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700270 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700271 } else if (dst_class->GetPrimitiveType() == Primitive::kPrimVoid) {
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700272 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
273 "can't unbox %s to void",
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700274 UnboxingFailureKind(m, index, f).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700275 return false;
276 }
277
278 if (o == NULL) {
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 null",
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());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700283 return false;
284 }
285
Elliott Hughes1d878f32012-04-11 15:17:54 -0700286 JValue boxed_value;
Elliott Hughes95572412011-12-13 18:14:20 -0800287 std::string src_descriptor(ClassHelper(o->GetClass()).GetDescriptor());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700288 Class* src_class = NULL;
289 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
290 Field* primitive_field = o->GetClass()->GetIFields()->Get(0);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800291 if (src_descriptor == "Ljava/lang/Boolean;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700292 src_class = class_linker->FindPrimitiveClass('Z');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700293 boxed_value.SetZ(primitive_field->GetBoolean(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800294 } else if (src_descriptor == "Ljava/lang/Byte;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700295 src_class = class_linker->FindPrimitiveClass('B');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700296 boxed_value.SetB(primitive_field->GetByte(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800297 } else if (src_descriptor == "Ljava/lang/Character;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700298 src_class = class_linker->FindPrimitiveClass('C');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700299 boxed_value.SetC(primitive_field->GetChar(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800300 } else if (src_descriptor == "Ljava/lang/Float;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700301 src_class = class_linker->FindPrimitiveClass('F');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700302 boxed_value.SetF(primitive_field->GetFloat(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800303 } else if (src_descriptor == "Ljava/lang/Double;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700304 src_class = class_linker->FindPrimitiveClass('D');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700305 boxed_value.SetD(primitive_field->GetDouble(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800306 } else if (src_descriptor == "Ljava/lang/Integer;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700307 src_class = class_linker->FindPrimitiveClass('I');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700308 boxed_value.SetI(primitive_field->GetInt(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800309 } else if (src_descriptor == "Ljava/lang/Long;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700310 src_class = class_linker->FindPrimitiveClass('J');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700311 boxed_value.SetJ(primitive_field->GetLong(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800312 } else if (src_descriptor == "Ljava/lang/Short;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700313 src_class = class_linker->FindPrimitiveClass('S');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700314 boxed_value.SetS(primitive_field->GetShort(o));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700315 } else {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700316 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700317 "%s has type %s, got %s",
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700318 UnboxingFailureKind(m, index, f).c_str(),
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700319 PrettyDescriptor(dst_class).c_str(),
320 PrettyDescriptor(src_descriptor.c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700321 return false;
322 }
323
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700324 return ConvertPrimitiveValue(src_class->GetPrimitiveType(), dst_class->GetPrimitiveType(),
325 boxed_value, unboxed_value);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700326}
327
Mathieu Chartier66f19252012-09-18 08:57:04 -0700328bool UnboxPrimitiveForArgument(Object* o, Class* dst_class, JValue& unboxed_value, AbstractMethod* m, size_t index) {
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700329 CHECK(m != NULL);
330 return UnboxPrimitive(o, dst_class, unboxed_value, m, index, NULL);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700331}
332
333bool UnboxPrimitiveForField(Object* o, Class* dst_class, JValue& unboxed_value, Field* f) {
334 CHECK(f != NULL);
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700335 return UnboxPrimitive(o, dst_class, unboxed_value, NULL, -1, f);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700336}
337
338bool UnboxPrimitiveForResult(Object* o, Class* dst_class, JValue& unboxed_value) {
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700339 return UnboxPrimitive(o, dst_class, unboxed_value, NULL, -1, NULL);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700340}
341
Elliott Hughes418d20f2011-09-22 14:00:39 -0700342} // namespace art