blob: 7726998d51ecd4f9d309144298f0c8aea97e6b1f [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 Rogers365c1022012-06-22 15:05:28 -070023#include "scoped_jni_thread_state.h"
Elliott Hughes418d20f2011-09-22 14:00:39 -070024
Elliott Hughes418d20f2011-09-22 14:00:39 -070025namespace art {
26
27Method* gBoolean_valueOf;
28Method* gByte_valueOf;
29Method* gCharacter_valueOf;
30Method* gDouble_valueOf;
31Method* gFloat_valueOf;
32Method* gInteger_valueOf;
33Method* gLong_valueOf;
34Method* gShort_valueOf;
35
Jesse Wilson9a6bae82011-11-14 14:57:30 -050036void InitBoxingMethods() {
37 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
38 gBoolean_valueOf = class_linker->FindSystemClass("Ljava/lang/Boolean;")->FindDeclaredDirectMethod("valueOf", "(Z)Ljava/lang/Boolean;");
39 gByte_valueOf = class_linker->FindSystemClass("Ljava/lang/Byte;")->FindDeclaredDirectMethod("valueOf", "(B)Ljava/lang/Byte;");
40 gCharacter_valueOf = class_linker->FindSystemClass("Ljava/lang/Character;")->FindDeclaredDirectMethod("valueOf", "(C)Ljava/lang/Character;");
41 gDouble_valueOf = class_linker->FindSystemClass("Ljava/lang/Double;")->FindDeclaredDirectMethod("valueOf", "(D)Ljava/lang/Double;");
42 gFloat_valueOf = class_linker->FindSystemClass("Ljava/lang/Float;")->FindDeclaredDirectMethod("valueOf", "(F)Ljava/lang/Float;");
43 gInteger_valueOf = class_linker->FindSystemClass("Ljava/lang/Integer;")->FindDeclaredDirectMethod("valueOf", "(I)Ljava/lang/Integer;");
44 gLong_valueOf = class_linker->FindSystemClass("Ljava/lang/Long;")->FindDeclaredDirectMethod("valueOf", "(J)Ljava/lang/Long;");
45 gShort_valueOf = class_linker->FindSystemClass("Ljava/lang/Short;")->FindDeclaredDirectMethod("valueOf", "(S)Ljava/lang/Short;");
Elliott Hughes418d20f2011-09-22 14:00:39 -070046}
47
Ian Rogers365c1022012-06-22 15:05:28 -070048jobject InvokeMethod(const ScopedJniThreadState& ts, jobject javaMethod, jobject javaReceiver,
49 jobject javaArgs) {
50 jmethodID mid = ts.Env()->FromReflectedMethod(javaMethod);
51 Method* m = ts.DecodeMethod(mid);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070052
53 Class* declaring_class = m->GetDeclaringClass();
Ian Rogers0045a292012-03-31 21:08:41 -070054 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(declaring_class, true, true)) {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070055 return NULL;
56 }
57
58 Object* receiver = NULL;
59 if (!m->IsStatic()) {
60 // Check that the receiver is non-null and an instance of the field's declaring class.
Ian Rogers365c1022012-06-22 15:05:28 -070061 receiver = ts.Decode<Object*>(javaReceiver);
Elliott Hugheseac76672012-05-24 21:56:51 -070062 if (!VerifyObjectInClass(receiver, declaring_class)) {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070063 return NULL;
64 }
65
66 // Find the actual implementation of the virtual method.
67 m = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(m);
Ian Rogers365c1022012-06-22 15:05:28 -070068 mid = ts.EncodeMethod(m);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070069 }
70
71 // Get our arrays of arguments and their types, and check they're the same size.
Ian Rogers365c1022012-06-22 15:05:28 -070072 ObjectArray<Object>* objects = ts.Decode<ObjectArray<Object>*>(javaArgs);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080073 MethodHelper mh(m);
74 const DexFile::TypeList* classes = mh.GetParameterTypeList();
75 uint32_t classes_size = classes == NULL ? 0 : classes->Size();
76 uint32_t arg_count = (objects != NULL) ? objects->GetLength() : 0;
77 if (arg_count != classes_size) {
Ian Rogers365c1022012-06-22 15:05:28 -070078 ts.Self()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070079 "wrong number of arguments; expected %d, got %d",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080080 classes_size, arg_count);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070081 return NULL;
82 }
83
84 // Translate javaArgs to a jvalue[].
85 UniquePtr<jvalue[]> args(new jvalue[arg_count]);
86 JValue* decoded_args = reinterpret_cast<JValue*>(args.get());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080087 for (uint32_t i = 0; i < arg_count; ++i) {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070088 Object* arg = objects->Get(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080089 Class* dst_class = mh.GetClassFromTypeIdx(classes->GetTypeItem(i).type_idx_);
Elliott Hughes37f77752012-05-21 15:12:47 -070090 if (!UnboxPrimitiveForArgument(arg, dst_class, decoded_args[i], m, i)) {
91 return NULL;
92 }
93 if (!dst_class->IsPrimitive()) {
Ian Rogers365c1022012-06-22 15:05:28 -070094 args[i].l = ts.AddLocalReference<jobject>(arg);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070095 }
96 }
97
98 // Invoke the method.
Ian Rogers365c1022012-06-22 15:05:28 -070099 JValue value(InvokeWithJValues(ts, javaReceiver, mid, args.get()));
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700100
101 // Wrap any exception with "Ljava/lang/reflect/InvocationTargetException;" and return early.
Ian Rogers365c1022012-06-22 15:05:28 -0700102 if (ts.Self()->IsExceptionPending()) {
103 jthrowable th = ts.Env()->ExceptionOccurred();
104 ts.Env()->ExceptionClear();
105 jclass exception_class = ts.Env()->FindClass("java/lang/reflect/InvocationTargetException");
106 jmethodID mid = ts.Env()->GetMethodID(exception_class, "<init>", "(Ljava/lang/Throwable;)V");
107 jobject exception_instance = ts.Env()->NewObject(exception_class, mid, th);
108 ts.Env()->Throw(reinterpret_cast<jthrowable>(exception_instance));
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700109 return NULL;
110 }
111
112 // Box if necessary and return.
Elliott Hughesdbac3092012-03-16 18:00:30 -0700113 BoxPrimitive(mh.GetReturnType()->GetPrimitiveType(), value);
Ian Rogers365c1022012-06-22 15:05:28 -0700114 return ts.AddLocalReference<jobject>(value.GetL());
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700115}
116
Elliott Hugheseac76672012-05-24 21:56:51 -0700117bool VerifyObjectInClass(Object* o, Class* c) {
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700118 const char* exception = NULL;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700119 if (o == NULL) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700120 exception = "Ljava/lang/NullPointerException;";
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700121 } else if (!o->InstanceOf(c)) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700122 exception = "Ljava/lang/IllegalArgumentException;";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700123 }
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700124 if (exception != NULL) {
125 std::string expected_class_name(PrettyDescriptor(c));
126 std::string actual_class_name(PrettyTypeOf(o));
Elliott Hugheseac76672012-05-24 21:56:51 -0700127 Thread::Current()->ThrowNewExceptionF(exception, "expected receiver of type %s, but got %s",
128 expected_class_name.c_str(), actual_class_name.c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700129 return false;
130 }
131 return true;
132}
133
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700134bool ConvertPrimitiveValue(Primitive::Type srcType, Primitive::Type dstType,
135 const JValue& src, JValue& dst) {
Jesse Wilsonc129a6b2011-11-24 14:47:46 -0500136 CHECK(srcType != Primitive::kPrimNot && dstType != Primitive::kPrimNot);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700137 switch (dstType) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700138 case Primitive::kPrimBoolean:
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700139 if (srcType == Primitive::kPrimBoolean) {
140 dst.SetZ(src.GetZ());
141 return true;
142 }
143 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700144 case Primitive::kPrimChar:
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700145 if (srcType == Primitive::kPrimChar) {
146 dst.SetC(src.GetC());
147 return true;
148 }
149 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700150 case Primitive::kPrimByte:
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700151 if (srcType == Primitive::kPrimByte) {
152 dst.SetB(src.GetB());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700153 return true;
154 }
155 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700156 case Primitive::kPrimShort:
157 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimShort) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700158 dst.SetS(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700159 return true;
160 }
161 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700162 case Primitive::kPrimInt:
163 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
164 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700165 dst.SetI(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700166 return true;
167 }
168 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700169 case Primitive::kPrimLong:
170 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
171 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700172 dst.SetJ(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700173 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700174 } else if (srcType == Primitive::kPrimLong) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700175 dst.SetJ(src.GetJ());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700176 return true;
177 }
178 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700179 case Primitive::kPrimFloat:
180 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
181 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700182 dst.SetF(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.SetF(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.SetF(src.GetF());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700189 return true;
190 }
191 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700192 case Primitive::kPrimDouble:
193 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
194 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700195 dst.SetD(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700196 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700197 } else if (srcType == Primitive::kPrimLong) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700198 dst.SetD(src.GetJ());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700199 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700200 } else if (srcType == Primitive::kPrimFloat) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700201 dst.SetD(src.GetF());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700202 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700203 } else if (srcType == Primitive::kPrimDouble) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700204 dst.SetJ(src.GetJ());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700205 return true;
206 }
207 break;
208 default:
209 break;
210 }
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700211 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700212 "invalid primitive conversion from %s to %s",
213 PrettyDescriptor(srcType).c_str(),
214 PrettyDescriptor(dstType).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700215 return false;
216}
217
Elliott Hughesdbac3092012-03-16 18:00:30 -0700218void BoxPrimitive(Primitive::Type src_class, JValue& value) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700219 if (src_class == Primitive::kPrimNot) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700220 return;
221 }
222
223 Method* m = NULL;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700224 switch (src_class) {
225 case Primitive::kPrimBoolean:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700226 m = gBoolean_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700227 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700228 case Primitive::kPrimByte:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700229 m = gByte_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700230 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700231 case Primitive::kPrimChar:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700232 m = gCharacter_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700233 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700234 case Primitive::kPrimDouble:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700235 m = gDouble_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700236 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700237 case Primitive::kPrimFloat:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700238 m = gFloat_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700239 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700240 case Primitive::kPrimInt:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700241 m = gInteger_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700242 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700243 case Primitive::kPrimLong:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700244 m = gLong_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700245 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700246 case Primitive::kPrimShort:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700247 m = gShort_valueOf;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700248 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700249 case Primitive::kPrimVoid:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700250 // There's no such thing as a void field, and void methods invoked via reflection return null.
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700251 value.SetL(NULL);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700252 return;
253 default:
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700254 LOG(FATAL) << static_cast<int>(src_class);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700255 }
256
257 Thread* self = Thread::Current();
Elliott Hughes34e06962012-04-09 13:55:55 -0700258 ScopedThreadStateChange tsc(self, kRunnable);
Elliott Hughesdbac3092012-03-16 18:00:30 -0700259 JValue args[1] = { value };
Elliott Hughes77405792012-03-15 15:22:12 -0700260 m->Invoke(self, NULL, args, &value);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700261}
262
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700263static std::string UnboxingFailureKind(Method* m, int index, Field* f) {
264 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
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700274static bool UnboxPrimitive(Object* o, Class* dst_class, JValue& unboxed_value, Method* m, int index, Field* f) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700275 if (!dst_class->IsPrimitive()) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700276 if (o != NULL && !o->InstanceOf(dst_class)) {
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700277 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700278 "%s has type %s, got %s",
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700279 UnboxingFailureKind(m, index, f).c_str(),
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700280 PrettyDescriptor(dst_class).c_str(),
281 PrettyTypeOf(o).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700282 return false;
283 }
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700284 unboxed_value.SetL(o);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700285 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700286 } else if (dst_class->GetPrimitiveType() == Primitive::kPrimVoid) {
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700287 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
288 "can't unbox %s to void",
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700289 UnboxingFailureKind(m, index, f).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700290 return false;
291 }
292
293 if (o == NULL) {
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700294 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700295 "%s has type %s, got null",
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700296 UnboxingFailureKind(m, index, f).c_str(),
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700297 PrettyDescriptor(dst_class).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700298 return false;
299 }
300
Elliott Hughes1d878f32012-04-11 15:17:54 -0700301 JValue boxed_value;
Elliott Hughes95572412011-12-13 18:14:20 -0800302 std::string src_descriptor(ClassHelper(o->GetClass()).GetDescriptor());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700303 Class* src_class = NULL;
304 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
305 Field* primitive_field = o->GetClass()->GetIFields()->Get(0);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800306 if (src_descriptor == "Ljava/lang/Boolean;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700307 src_class = class_linker->FindPrimitiveClass('Z');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700308 boxed_value.SetZ(primitive_field->GetBoolean(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800309 } else if (src_descriptor == "Ljava/lang/Byte;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700310 src_class = class_linker->FindPrimitiveClass('B');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700311 boxed_value.SetB(primitive_field->GetByte(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800312 } else if (src_descriptor == "Ljava/lang/Character;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700313 src_class = class_linker->FindPrimitiveClass('C');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700314 boxed_value.SetC(primitive_field->GetChar(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800315 } else if (src_descriptor == "Ljava/lang/Float;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700316 src_class = class_linker->FindPrimitiveClass('F');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700317 boxed_value.SetF(primitive_field->GetFloat(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800318 } else if (src_descriptor == "Ljava/lang/Double;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700319 src_class = class_linker->FindPrimitiveClass('D');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700320 boxed_value.SetD(primitive_field->GetDouble(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800321 } else if (src_descriptor == "Ljava/lang/Integer;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700322 src_class = class_linker->FindPrimitiveClass('I');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700323 boxed_value.SetI(primitive_field->GetInt(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800324 } else if (src_descriptor == "Ljava/lang/Long;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700325 src_class = class_linker->FindPrimitiveClass('J');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700326 boxed_value.SetJ(primitive_field->GetLong(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800327 } else if (src_descriptor == "Ljava/lang/Short;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700328 src_class = class_linker->FindPrimitiveClass('S');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700329 boxed_value.SetS(primitive_field->GetShort(o));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700330 } else {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700331 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700332 "%s has type %s, got %s",
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700333 UnboxingFailureKind(m, index, f).c_str(),
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700334 PrettyDescriptor(dst_class).c_str(),
335 PrettyDescriptor(src_descriptor.c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700336 return false;
337 }
338
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700339 return ConvertPrimitiveValue(src_class->GetPrimitiveType(), dst_class->GetPrimitiveType(),
340 boxed_value, unboxed_value);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700341}
342
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700343bool UnboxPrimitiveForArgument(Object* o, Class* dst_class, JValue& unboxed_value, Method* m, size_t index) {
344 CHECK(m != NULL);
345 return UnboxPrimitive(o, dst_class, unboxed_value, m, index, NULL);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700346}
347
348bool UnboxPrimitiveForField(Object* o, Class* dst_class, JValue& unboxed_value, Field* f) {
349 CHECK(f != NULL);
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700350 return UnboxPrimitive(o, dst_class, unboxed_value, NULL, -1, f);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700351}
352
353bool UnboxPrimitiveForResult(Object* o, Class* dst_class, JValue& unboxed_value) {
Elliott Hughes84a5bb42012-05-16 17:52:15 -0700354 return UnboxPrimitive(o, dst_class, unboxed_value, NULL, -1, NULL);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700355}
356
Elliott Hughes418d20f2011-09-22 14:00:39 -0700357} // namespace art