blob: f02c2d2561987273e285cc86e10b5df7bd6c8e32 [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"
22
23#include "JniConstants.h" // Last to avoid problems with LOG redefinition.
24
25namespace 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
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070048jobject InvokeMethod(JNIEnv* env, jobject javaMethod, jobject javaReceiver, jobject javaArgs, jobject javaParams) {
49 Thread* self = Thread::Current();
50 ScopedThreadStateChange tsc(self, Thread::kRunnable);
51
52 jmethodID mid = env->FromReflectedMethod(javaMethod);
53 Method* m = reinterpret_cast<Method*>(mid);
54
55 Class* declaring_class = m->GetDeclaringClass();
56 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(declaring_class, true)) {
57 return NULL;
58 }
59
60 Object* receiver = NULL;
61 if (!m->IsStatic()) {
62 // Check that the receiver is non-null and an instance of the field's declaring class.
63 receiver = Decode<Object*>(env, javaReceiver);
64 if (!VerifyObjectInClass(env, receiver, declaring_class)) {
65 return NULL;
66 }
67
68 // Find the actual implementation of the virtual method.
69 m = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(m);
Ian Rogers466bb252011-10-14 03:29:56 -070070 mid = reinterpret_cast<jmethodID>(m);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070071 }
72
73 // Get our arrays of arguments and their types, and check they're the same size.
74 ObjectArray<Object>* objects = Decode<ObjectArray<Object>*>(env, javaArgs);
75 ObjectArray<Class>* classes = Decode<ObjectArray<Class>*>(env, javaParams);
76 int32_t arg_count = (objects != NULL) ? objects->GetLength() : 0;
77 if (arg_count != classes->GetLength()) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -070078 self->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070079 "wrong number of arguments; expected %d, got %d",
80 classes->GetLength(), arg_count);
81 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());
87 for (int32_t i = 0; i < arg_count; ++i) {
88 Object* arg = objects->Get(i);
89 Class* dst_class = classes->Get(i);
90 if (dst_class->IsPrimitive()) {
91 if (!UnboxPrimitive(env, arg, dst_class, decoded_args[i])) {
92 return NULL;
93 }
94 } else {
95 args[i].l = AddLocalReference<jobject>(env, arg);
96 }
97 }
98
99 // Invoke the method.
100 JValue value = InvokeWithJValues(env, javaReceiver, mid, args.get());
101
102 // Wrap any exception with "Ljava/lang/reflect/InvocationTargetException;" and return early.
103 if (self->IsExceptionPending()) {
104 jthrowable th = env->ExceptionOccurred();
105 env->ExceptionClear();
106 jclass exception_class = env->FindClass("java/lang/reflect/InvocationTargetException");
107 jmethodID mid = env->GetMethodID(exception_class, "<init>", "(Ljava/lang/Throwable;)V");
108 jobject exception_instance = env->NewObject(exception_class, mid, th);
109 env->Throw(reinterpret_cast<jthrowable>(exception_instance));
110 return NULL;
111 }
112
113 // Box if necessary and return.
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700114 BoxPrimitive(env, m->GetReturnType()->GetPrimitiveType(), value);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700115 return AddLocalReference<jobject>(env, value.l);
116}
117
Elliott Hughes418d20f2011-09-22 14:00:39 -0700118bool VerifyObjectInClass(JNIEnv* env, Object* o, Class* c) {
119 if (o == NULL) {
120 jniThrowNullPointerException(env, "receiver for non-static field access was null");
121 return false;
122 }
123 if (!o->InstanceOf(c)) {
124 std::string expectedClassName(PrettyDescriptor(c->GetDescriptor()));
125 std::string actualClassName(PrettyTypeOf(o));
126 jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException",
127 "expected receiver of type %s, but got %s",
128 expectedClassName.c_str(), actualClassName.c_str());
129 return false;
130 }
131 return true;
132}
133
134/*
135 * Convert primitive, boxed data from "srcPtr" to "dstPtr".
136 *
137 * Section v2 2.6 lists the various conversions and promotions. We
138 * allow the "widening" and "identity" conversions, but don't allow the
139 * "narrowing" conversions.
140 *
141 * Allowed:
142 * byte to short, int, long, float, double
143 * short to int, long, float double
144 * char to int, long, float, double
145 * int to long, float, double
146 * long to float, double
147 * float to double
148 * Values of types byte, char, and short are "internally" widened to int.
149 *
150 * Returns the width in 32-bit words of the destination primitive, or
151 * -1 if the conversion is not allowed.
152 */
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700153bool ConvertPrimitiveValue(Primitive::Type srcType, Primitive::Type dstType,
154 const JValue& src, JValue& dst) {
Jesse Wilsonc129a6b2011-11-24 14:47:46 -0500155 CHECK(srcType != Primitive::kPrimNot && dstType != Primitive::kPrimNot);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700156 switch (dstType) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700157 case Primitive::kPrimBoolean:
158 case Primitive::kPrimChar:
159 case Primitive::kPrimByte:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700160 if (srcType == dstType) {
161 dst.i = src.i;
162 return true;
163 }
164 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700165 case Primitive::kPrimShort:
166 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimShort) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700167 dst.i = src.i;
168 return true;
169 }
170 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700171 case Primitive::kPrimInt:
172 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
173 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700174 dst.i = src.i;
175 return true;
176 }
177 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700178 case Primitive::kPrimLong:
179 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
180 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700181 dst.j = src.i;
182 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700183 } else if (srcType == Primitive::kPrimLong) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700184 dst.j = src.j;
185 return true;
186 }
187 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700188 case Primitive::kPrimFloat:
189 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
190 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700191 dst.f = src.i;
192 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700193 } else if (srcType == Primitive::kPrimLong) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700194 dst.f = src.j;
195 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700196 } else if (srcType == Primitive::kPrimFloat) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700197 dst.i = src.i;
198 return true;
199 }
200 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700201 case Primitive::kPrimDouble:
202 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
203 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700204 dst.d = src.i;
205 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700206 } else if (srcType == Primitive::kPrimLong) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700207 dst.d = src.j;
208 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700209 } else if (srcType == Primitive::kPrimFloat) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700210 dst.d = src.f;
211 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700212 } else if (srcType == Primitive::kPrimDouble) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700213 dst.j = src.j;
214 return true;
215 }
216 break;
217 default:
218 break;
219 }
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700220 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700221 "invalid primitive conversion from %s to %s",
222 PrettyDescriptor(srcType).c_str(),
223 PrettyDescriptor(dstType).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700224 return false;
225}
226
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700227void BoxPrimitive(JNIEnv* env, Primitive::Type src_class, JValue& value) {
228 if (src_class == Primitive::kPrimNot) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700229 return;
230 }
231
232 Method* m = NULL;
233 UniquePtr<byte[]> args(new byte[8]);
234 memset(&args[0], 0, 8);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700235 switch (src_class) {
236 case Primitive::kPrimBoolean:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700237 m = gBoolean_valueOf;
238 *reinterpret_cast<uint32_t*>(&args[0]) = value.z;
239 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700240 case Primitive::kPrimByte:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700241 m = gByte_valueOf;
242 *reinterpret_cast<uint32_t*>(&args[0]) = value.b;
243 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700244 case Primitive::kPrimChar:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700245 m = gCharacter_valueOf;
246 *reinterpret_cast<uint32_t*>(&args[0]) = value.c;
247 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700248 case Primitive::kPrimDouble:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700249 m = gDouble_valueOf;
250 *reinterpret_cast<double*>(&args[0]) = value.d;
251 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700252 case Primitive::kPrimFloat:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700253 m = gFloat_valueOf;
254 *reinterpret_cast<float*>(&args[0]) = value.f;
255 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700256 case Primitive::kPrimInt:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700257 m = gInteger_valueOf;
258 *reinterpret_cast<uint32_t*>(&args[0]) = value.i;
259 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700260 case Primitive::kPrimLong:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700261 m = gLong_valueOf;
262 *reinterpret_cast<uint64_t*>(&args[0]) = value.j;
263 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700264 case Primitive::kPrimShort:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700265 m = gShort_valueOf;
266 *reinterpret_cast<uint32_t*>(&args[0]) = value.s;
267 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700268 case Primitive::kPrimVoid:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700269 // There's no such thing as a void field, and void methods invoked via reflection return null.
270 value.l = NULL;
271 return;
272 default:
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700273 LOG(FATAL) << static_cast<int>(src_class);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700274 }
275
276 Thread* self = Thread::Current();
277 ScopedThreadStateChange tsc(self, Thread::kRunnable);
278 m->Invoke(self, NULL, args.get(), &value);
279}
280
281bool UnboxPrimitive(JNIEnv* env, Object* o, Class* dst_class, JValue& unboxed_value) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700282 if (!dst_class->IsPrimitive()) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700283 if (o != NULL && !o->InstanceOf(dst_class)) {
284 jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException",
285 "expected object of type %s, but got %s",
286 PrettyDescriptor(dst_class->GetDescriptor()).c_str(),
287 PrettyTypeOf(o).c_str());
288 return false;
289 }
290 unboxed_value.l = o;
291 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700292 } else if (dst_class->GetPrimitiveType() == Primitive::kPrimVoid) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700293 Thread::Current()->ThrowNewException("Ljava/lang/IllegalArgumentException;",
294 "can't unbox to void");
295 return false;
296 }
297
298 if (o == NULL) {
299 Thread::Current()->ThrowNewException("Ljava/lang/IllegalArgumentException;",
300 "null passed for boxed primitive type");
301 return false;
302 }
303
304 JValue boxed_value = { 0 };
305 const String* src_descriptor = o->GetClass()->GetDescriptor();
306 Class* src_class = NULL;
307 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
308 Field* primitive_field = o->GetClass()->GetIFields()->Get(0);
309 if (src_descriptor->Equals("Ljava/lang/Boolean;")) {
310 src_class = class_linker->FindPrimitiveClass('Z');
Ian Rogers466bb252011-10-14 03:29:56 -0700311 boxed_value.i = primitive_field->GetBoolean(o); // and extend read value to 32bits
Elliott Hughes418d20f2011-09-22 14:00:39 -0700312 } else if (src_descriptor->Equals("Ljava/lang/Byte;")) {
313 src_class = class_linker->FindPrimitiveClass('B');
Ian Rogers466bb252011-10-14 03:29:56 -0700314 boxed_value.i = primitive_field->GetByte(o); // and extend read value to 32bits
Elliott Hughes418d20f2011-09-22 14:00:39 -0700315 } else if (src_descriptor->Equals("Ljava/lang/Character;")) {
316 src_class = class_linker->FindPrimitiveClass('C');
Ian Rogers466bb252011-10-14 03:29:56 -0700317 boxed_value.i = primitive_field->GetChar(o); // and extend read value to 32bits
Elliott Hughes418d20f2011-09-22 14:00:39 -0700318 } else if (src_descriptor->Equals("Ljava/lang/Float;")) {
319 src_class = class_linker->FindPrimitiveClass('F');
320 boxed_value.f = primitive_field->GetFloat(o);
321 } else if (src_descriptor->Equals("Ljava/lang/Double;")) {
322 src_class = class_linker->FindPrimitiveClass('D');
323 boxed_value.d = primitive_field->GetDouble(o);
324 } else if (src_descriptor->Equals("Ljava/lang/Integer;")) {
325 src_class = class_linker->FindPrimitiveClass('I');
326 boxed_value.i = primitive_field->GetInt(o);
327 } else if (src_descriptor->Equals("Ljava/lang/Long;")) {
328 src_class = class_linker->FindPrimitiveClass('J');
329 boxed_value.j = primitive_field->GetLong(o);
330 } else if (src_descriptor->Equals("Ljava/lang/Short;")) {
331 src_class = class_linker->FindPrimitiveClass('S');
Ian Rogers466bb252011-10-14 03:29:56 -0700332 boxed_value.i = primitive_field->GetShort(o); // and extend read value to 32bits
Elliott Hughes418d20f2011-09-22 14:00:39 -0700333 } else {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700334 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Elliott Hughes418d20f2011-09-22 14:00:39 -0700335 "%s is not a boxed primitive type", PrettyDescriptor(src_descriptor).c_str());
336 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
343} // namespace art