blob: c317be075761c5e10da8248b945510df5a80e8a5 [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"
Elliott Hughes418d20f2011-09-22 14:00:39 -070023
24#include "JniConstants.h" // Last to avoid problems with LOG redefinition.
25
26namespace art {
27
28Method* gBoolean_valueOf;
29Method* gByte_valueOf;
30Method* gCharacter_valueOf;
31Method* gDouble_valueOf;
32Method* gFloat_valueOf;
33Method* gInteger_valueOf;
34Method* gLong_valueOf;
35Method* gShort_valueOf;
36
Jesse Wilson9a6bae82011-11-14 14:57:30 -050037void InitBoxingMethods() {
38 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
39 gBoolean_valueOf = class_linker->FindSystemClass("Ljava/lang/Boolean;")->FindDeclaredDirectMethod("valueOf", "(Z)Ljava/lang/Boolean;");
40 gByte_valueOf = class_linker->FindSystemClass("Ljava/lang/Byte;")->FindDeclaredDirectMethod("valueOf", "(B)Ljava/lang/Byte;");
41 gCharacter_valueOf = class_linker->FindSystemClass("Ljava/lang/Character;")->FindDeclaredDirectMethod("valueOf", "(C)Ljava/lang/Character;");
42 gDouble_valueOf = class_linker->FindSystemClass("Ljava/lang/Double;")->FindDeclaredDirectMethod("valueOf", "(D)Ljava/lang/Double;");
43 gFloat_valueOf = class_linker->FindSystemClass("Ljava/lang/Float;")->FindDeclaredDirectMethod("valueOf", "(F)Ljava/lang/Float;");
44 gInteger_valueOf = class_linker->FindSystemClass("Ljava/lang/Integer;")->FindDeclaredDirectMethod("valueOf", "(I)Ljava/lang/Integer;");
45 gLong_valueOf = class_linker->FindSystemClass("Ljava/lang/Long;")->FindDeclaredDirectMethod("valueOf", "(J)Ljava/lang/Long;");
46 gShort_valueOf = class_linker->FindSystemClass("Ljava/lang/Short;")->FindDeclaredDirectMethod("valueOf", "(S)Ljava/lang/Short;");
Elliott Hughes418d20f2011-09-22 14:00:39 -070047}
48
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080049jobject InvokeMethod(JNIEnv* env, jobject javaMethod, jobject javaReceiver, jobject javaArgs) {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070050 Thread* self = Thread::Current();
51 ScopedThreadStateChange tsc(self, Thread::kRunnable);
52
53 jmethodID mid = env->FromReflectedMethod(javaMethod);
54 Method* m = reinterpret_cast<Method*>(mid);
55
56 Class* declaring_class = m->GetDeclaringClass();
57 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(declaring_class, true)) {
58 return NULL;
59 }
60
61 Object* receiver = NULL;
62 if (!m->IsStatic()) {
63 // Check that the receiver is non-null and an instance of the field's declaring class.
64 receiver = Decode<Object*>(env, javaReceiver);
65 if (!VerifyObjectInClass(env, receiver, declaring_class)) {
66 return NULL;
67 }
68
69 // Find the actual implementation of the virtual method.
70 m = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(m);
Ian Rogers466bb252011-10-14 03:29:56 -070071 mid = reinterpret_cast<jmethodID>(m);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070072 }
73
74 // Get our arrays of arguments and their types, and check they're the same size.
75 ObjectArray<Object>* objects = Decode<ObjectArray<Object>*>(env, javaArgs);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080076 MethodHelper mh(m);
77 const DexFile::TypeList* classes = mh.GetParameterTypeList();
78 uint32_t classes_size = classes == NULL ? 0 : classes->Size();
79 uint32_t arg_count = (objects != NULL) ? objects->GetLength() : 0;
80 if (arg_count != classes_size) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -070081 self->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070082 "wrong number of arguments; expected %d, got %d",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080083 classes_size, arg_count);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070084 return NULL;
85 }
86
87 // Translate javaArgs to a jvalue[].
88 UniquePtr<jvalue[]> args(new jvalue[arg_count]);
89 JValue* decoded_args = reinterpret_cast<JValue*>(args.get());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080090 for (uint32_t i = 0; i < arg_count; ++i) {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070091 Object* arg = objects->Get(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080092 Class* dst_class = mh.GetClassFromTypeIdx(classes->GetTypeItem(i).type_idx_);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070093 if (dst_class->IsPrimitive()) {
Elliott Hughesb600b3f2012-03-14 13:57:24 -070094 std::string what(StringPrintf("argument %d", i + 1)); // Humans count from 1.
95 if (!UnboxPrimitive(env, arg, dst_class, decoded_args[i], what.c_str())) {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070096 return NULL;
97 }
98 } else {
99 args[i].l = AddLocalReference<jobject>(env, arg);
100 }
101 }
102
103 // Invoke the method.
104 JValue value = InvokeWithJValues(env, javaReceiver, mid, args.get());
105
106 // Wrap any exception with "Ljava/lang/reflect/InvocationTargetException;" and return early.
107 if (self->IsExceptionPending()) {
108 jthrowable th = env->ExceptionOccurred();
109 env->ExceptionClear();
110 jclass exception_class = env->FindClass("java/lang/reflect/InvocationTargetException");
111 jmethodID mid = env->GetMethodID(exception_class, "<init>", "(Ljava/lang/Throwable;)V");
112 jobject exception_instance = env->NewObject(exception_class, mid, th);
113 env->Throw(reinterpret_cast<jthrowable>(exception_instance));
114 return NULL;
115 }
116
117 // Box if necessary and return.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800118 BoxPrimitive(env, mh.GetReturnType()->GetPrimitiveType(), value);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700119 return AddLocalReference<jobject>(env, value.l);
120}
121
Elliott Hughes418d20f2011-09-22 14:00:39 -0700122bool VerifyObjectInClass(JNIEnv* env, Object* o, Class* c) {
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700123 const char* exception = NULL;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700124 if (o == NULL) {
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700125 exception = "java/lang/NullPointerException";
126 } else if (!o->InstanceOf(c)) {
127 exception = "java/lang/IllegalArgumentException";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700128 }
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700129 if (exception != NULL) {
130 std::string expected_class_name(PrettyDescriptor(c));
131 std::string actual_class_name(PrettyTypeOf(o));
132 jniThrowExceptionFmt(env, exception, "expected receiver of type %s, but got %s",
133 expected_class_name.c_str(), actual_class_name.c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700134 return false;
135 }
136 return true;
137}
138
139/*
140 * Convert primitive, boxed data from "srcPtr" to "dstPtr".
141 *
142 * Section v2 2.6 lists the various conversions and promotions. We
143 * allow the "widening" and "identity" conversions, but don't allow the
144 * "narrowing" conversions.
145 *
146 * Allowed:
147 * byte to short, int, long, float, double
148 * short to int, long, float double
149 * char to int, long, float, double
150 * int to long, float, double
151 * long to float, double
152 * float to double
153 * Values of types byte, char, and short are "internally" widened to int.
154 *
155 * Returns the width in 32-bit words of the destination primitive, or
156 * -1 if the conversion is not allowed.
157 */
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700158bool ConvertPrimitiveValue(Primitive::Type srcType, Primitive::Type dstType,
159 const JValue& src, JValue& dst) {
Jesse Wilsonc129a6b2011-11-24 14:47:46 -0500160 CHECK(srcType != Primitive::kPrimNot && dstType != Primitive::kPrimNot);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700161 switch (dstType) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700162 case Primitive::kPrimBoolean:
163 case Primitive::kPrimChar:
164 case Primitive::kPrimByte:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700165 if (srcType == dstType) {
166 dst.i = src.i;
167 return true;
168 }
169 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700170 case Primitive::kPrimShort:
171 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimShort) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700172 dst.i = src.i;
173 return true;
174 }
175 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700176 case Primitive::kPrimInt:
177 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
178 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700179 dst.i = src.i;
180 return true;
181 }
182 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700183 case Primitive::kPrimLong:
184 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
185 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700186 dst.j = src.i;
187 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700188 } else if (srcType == Primitive::kPrimLong) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700189 dst.j = src.j;
190 return true;
191 }
192 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700193 case Primitive::kPrimFloat:
194 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
195 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700196 dst.f = src.i;
197 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700198 } else if (srcType == Primitive::kPrimLong) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700199 dst.f = src.j;
200 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700201 } else if (srcType == Primitive::kPrimFloat) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700202 dst.i = src.i;
203 return true;
204 }
205 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700206 case Primitive::kPrimDouble:
207 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
208 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700209 dst.d = src.i;
210 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700211 } else if (srcType == Primitive::kPrimLong) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700212 dst.d = src.j;
213 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700214 } else if (srcType == Primitive::kPrimFloat) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700215 dst.d = src.f;
216 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700217 } else if (srcType == Primitive::kPrimDouble) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700218 dst.j = src.j;
219 return true;
220 }
221 break;
222 default:
223 break;
224 }
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700225 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700226 "invalid primitive conversion from %s to %s",
227 PrettyDescriptor(srcType).c_str(),
228 PrettyDescriptor(dstType).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700229 return false;
230}
231
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700232void BoxPrimitive(JNIEnv* env, Primitive::Type src_class, JValue& value) {
233 if (src_class == Primitive::kPrimNot) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700234 return;
235 }
236
237 Method* m = NULL;
238 UniquePtr<byte[]> args(new byte[8]);
239 memset(&args[0], 0, 8);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700240 switch (src_class) {
241 case Primitive::kPrimBoolean:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700242 m = gBoolean_valueOf;
243 *reinterpret_cast<uint32_t*>(&args[0]) = value.z;
244 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700245 case Primitive::kPrimByte:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700246 m = gByte_valueOf;
247 *reinterpret_cast<uint32_t*>(&args[0]) = value.b;
248 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700249 case Primitive::kPrimChar:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700250 m = gCharacter_valueOf;
251 *reinterpret_cast<uint32_t*>(&args[0]) = value.c;
252 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700253 case Primitive::kPrimDouble:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700254 m = gDouble_valueOf;
255 *reinterpret_cast<double*>(&args[0]) = value.d;
256 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700257 case Primitive::kPrimFloat:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700258 m = gFloat_valueOf;
259 *reinterpret_cast<float*>(&args[0]) = value.f;
260 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700261 case Primitive::kPrimInt:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700262 m = gInteger_valueOf;
263 *reinterpret_cast<uint32_t*>(&args[0]) = value.i;
264 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700265 case Primitive::kPrimLong:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700266 m = gLong_valueOf;
267 *reinterpret_cast<uint64_t*>(&args[0]) = value.j;
268 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700269 case Primitive::kPrimShort:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700270 m = gShort_valueOf;
271 *reinterpret_cast<uint32_t*>(&args[0]) = value.s;
272 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700273 case Primitive::kPrimVoid:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700274 // There's no such thing as a void field, and void methods invoked via reflection return null.
275 value.l = NULL;
276 return;
277 default:
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700278 LOG(FATAL) << static_cast<int>(src_class);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700279 }
280
281 Thread* self = Thread::Current();
282 ScopedThreadStateChange tsc(self, Thread::kRunnable);
283 m->Invoke(self, NULL, args.get(), &value);
284}
285
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700286bool UnboxPrimitive(JNIEnv* env, Object* o, Class* dst_class, JValue& unboxed_value, const char* what) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700287 if (!dst_class->IsPrimitive()) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700288 if (o != NULL && !o->InstanceOf(dst_class)) {
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700289 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
290 "boxed object for %s should have type %s, but got %s",
291 what,
292 PrettyDescriptor(dst_class).c_str(),
293 PrettyTypeOf(o).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700294 return false;
295 }
296 unboxed_value.l = o;
297 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700298 } else if (dst_class->GetPrimitiveType() == Primitive::kPrimVoid) {
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700299 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
300 "can't unbox %s to void",
301 what);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700302 return false;
303 }
304
305 if (o == NULL) {
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700306 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
307 "%s should have type %s, got null",
308 what,
309 PrettyDescriptor(dst_class).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700310 return false;
311 }
312
313 JValue boxed_value = { 0 };
Elliott Hughes95572412011-12-13 18:14:20 -0800314 std::string src_descriptor(ClassHelper(o->GetClass()).GetDescriptor());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700315 Class* src_class = NULL;
316 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
317 Field* primitive_field = o->GetClass()->GetIFields()->Get(0);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800318 if (src_descriptor == "Ljava/lang/Boolean;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700319 src_class = class_linker->FindPrimitiveClass('Z');
Ian Rogers466bb252011-10-14 03:29:56 -0700320 boxed_value.i = primitive_field->GetBoolean(o); // and extend read value to 32bits
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800321 } else if (src_descriptor == "Ljava/lang/Byte;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700322 src_class = class_linker->FindPrimitiveClass('B');
Ian Rogers466bb252011-10-14 03:29:56 -0700323 boxed_value.i = primitive_field->GetByte(o); // and extend read value to 32bits
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800324 } else if (src_descriptor == "Ljava/lang/Character;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700325 src_class = class_linker->FindPrimitiveClass('C');
Ian Rogers466bb252011-10-14 03:29:56 -0700326 boxed_value.i = primitive_field->GetChar(o); // and extend read value to 32bits
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800327 } else if (src_descriptor == "Ljava/lang/Float;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700328 src_class = class_linker->FindPrimitiveClass('F');
329 boxed_value.f = primitive_field->GetFloat(o);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800330 } else if (src_descriptor == "Ljava/lang/Double;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700331 src_class = class_linker->FindPrimitiveClass('D');
332 boxed_value.d = primitive_field->GetDouble(o);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800333 } else if (src_descriptor == "Ljava/lang/Integer;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700334 src_class = class_linker->FindPrimitiveClass('I');
335 boxed_value.i = primitive_field->GetInt(o);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800336 } else if (src_descriptor == "Ljava/lang/Long;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700337 src_class = class_linker->FindPrimitiveClass('J');
338 boxed_value.j = primitive_field->GetLong(o);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800339 } else if (src_descriptor == "Ljava/lang/Short;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700340 src_class = class_linker->FindPrimitiveClass('S');
Ian Rogers466bb252011-10-14 03:29:56 -0700341 boxed_value.i = primitive_field->GetShort(o); // and extend read value to 32bits
Elliott Hughes418d20f2011-09-22 14:00:39 -0700342 } else {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700343 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700344 "%s should have type %s, got %s",
345 what,
346 PrettyDescriptor(dst_class).c_str(),
347 PrettyDescriptor(src_descriptor.c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700348 return false;
349 }
350
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700351 return ConvertPrimitiveValue(src_class->GetPrimitiveType(), dst_class->GetPrimitiveType(),
352 boxed_value, unboxed_value);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700353}
354
355} // namespace art