blob: 0bdfe820ff3ec9b4b046b4eea4463c15b186636e [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) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700155 switch (dstType) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700156 case Primitive::kPrimBoolean:
157 case Primitive::kPrimChar:
158 case Primitive::kPrimByte:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700159 if (srcType == dstType) {
160 dst.i = src.i;
161 return true;
162 }
163 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700164 case Primitive::kPrimShort:
165 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimShort) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700166 dst.i = src.i;
167 return true;
168 }
169 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700170 case Primitive::kPrimInt:
171 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
172 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700173 dst.i = src.i;
174 return true;
175 }
176 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700177 case Primitive::kPrimLong:
178 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
179 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700180 dst.j = src.i;
181 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700182 } else if (srcType == Primitive::kPrimLong) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700183 dst.j = src.j;
184 return true;
185 }
186 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700187 case Primitive::kPrimFloat:
188 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
189 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700190 dst.f = src.i;
191 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700192 } else if (srcType == Primitive::kPrimLong) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700193 dst.f = src.j;
194 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700195 } else if (srcType == Primitive::kPrimFloat) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700196 dst.i = src.i;
197 return true;
198 }
199 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700200 case Primitive::kPrimDouble:
201 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
202 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700203 dst.d = src.i;
204 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700205 } else if (srcType == Primitive::kPrimLong) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700206 dst.d = src.j;
207 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700208 } else if (srcType == Primitive::kPrimFloat) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700209 dst.d = src.f;
210 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700211 } else if (srcType == Primitive::kPrimDouble) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700212 dst.j = src.j;
213 return true;
214 }
215 break;
216 default:
217 break;
218 }
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700219 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700220 "invalid primitive conversion from %s to %s",
221 PrettyDescriptor(srcType).c_str(),
222 PrettyDescriptor(dstType).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700223 return false;
224}
225
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700226void BoxPrimitive(JNIEnv* env, Primitive::Type src_class, JValue& value) {
227 if (src_class == Primitive::kPrimNot) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700228 return;
229 }
230
231 Method* m = NULL;
232 UniquePtr<byte[]> args(new byte[8]);
233 memset(&args[0], 0, 8);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700234 switch (src_class) {
235 case Primitive::kPrimBoolean:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700236 m = gBoolean_valueOf;
237 *reinterpret_cast<uint32_t*>(&args[0]) = value.z;
238 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700239 case Primitive::kPrimByte:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700240 m = gByte_valueOf;
241 *reinterpret_cast<uint32_t*>(&args[0]) = value.b;
242 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700243 case Primitive::kPrimChar:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700244 m = gCharacter_valueOf;
245 *reinterpret_cast<uint32_t*>(&args[0]) = value.c;
246 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700247 case Primitive::kPrimDouble:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700248 m = gDouble_valueOf;
249 *reinterpret_cast<double*>(&args[0]) = value.d;
250 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700251 case Primitive::kPrimFloat:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700252 m = gFloat_valueOf;
253 *reinterpret_cast<float*>(&args[0]) = value.f;
254 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700255 case Primitive::kPrimInt:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700256 m = gInteger_valueOf;
257 *reinterpret_cast<uint32_t*>(&args[0]) = value.i;
258 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700259 case Primitive::kPrimLong:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700260 m = gLong_valueOf;
261 *reinterpret_cast<uint64_t*>(&args[0]) = value.j;
262 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700263 case Primitive::kPrimShort:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700264 m = gShort_valueOf;
265 *reinterpret_cast<uint32_t*>(&args[0]) = value.s;
266 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700267 case Primitive::kPrimVoid:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700268 // There's no such thing as a void field, and void methods invoked via reflection return null.
269 value.l = NULL;
270 return;
271 default:
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700272 LOG(FATAL) << static_cast<int>(src_class);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700273 }
274
275 Thread* self = Thread::Current();
276 ScopedThreadStateChange tsc(self, Thread::kRunnable);
277 m->Invoke(self, NULL, args.get(), &value);
278}
279
280bool UnboxPrimitive(JNIEnv* env, Object* o, Class* dst_class, JValue& unboxed_value) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700281 if (!dst_class->IsPrimitive()) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700282 if (o != NULL && !o->InstanceOf(dst_class)) {
283 jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException",
284 "expected object of type %s, but got %s",
285 PrettyDescriptor(dst_class->GetDescriptor()).c_str(),
286 PrettyTypeOf(o).c_str());
287 return false;
288 }
289 unboxed_value.l = o;
290 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700291 } else if (dst_class->GetPrimitiveType() == Primitive::kPrimVoid) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700292 Thread::Current()->ThrowNewException("Ljava/lang/IllegalArgumentException;",
293 "can't unbox to void");
294 return false;
295 }
296
297 if (o == NULL) {
298 Thread::Current()->ThrowNewException("Ljava/lang/IllegalArgumentException;",
299 "null passed for boxed primitive type");
300 return false;
301 }
302
303 JValue boxed_value = { 0 };
304 const String* src_descriptor = o->GetClass()->GetDescriptor();
305 Class* src_class = NULL;
306 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
307 Field* primitive_field = o->GetClass()->GetIFields()->Get(0);
308 if (src_descriptor->Equals("Ljava/lang/Boolean;")) {
309 src_class = class_linker->FindPrimitiveClass('Z');
Ian Rogers466bb252011-10-14 03:29:56 -0700310 boxed_value.i = primitive_field->GetBoolean(o); // and extend read value to 32bits
Elliott Hughes418d20f2011-09-22 14:00:39 -0700311 } else if (src_descriptor->Equals("Ljava/lang/Byte;")) {
312 src_class = class_linker->FindPrimitiveClass('B');
Ian Rogers466bb252011-10-14 03:29:56 -0700313 boxed_value.i = primitive_field->GetByte(o); // and extend read value to 32bits
Elliott Hughes418d20f2011-09-22 14:00:39 -0700314 } else if (src_descriptor->Equals("Ljava/lang/Character;")) {
315 src_class = class_linker->FindPrimitiveClass('C');
Ian Rogers466bb252011-10-14 03:29:56 -0700316 boxed_value.i = primitive_field->GetChar(o); // and extend read value to 32bits
Elliott Hughes418d20f2011-09-22 14:00:39 -0700317 } else if (src_descriptor->Equals("Ljava/lang/Float;")) {
318 src_class = class_linker->FindPrimitiveClass('F');
319 boxed_value.f = primitive_field->GetFloat(o);
320 } else if (src_descriptor->Equals("Ljava/lang/Double;")) {
321 src_class = class_linker->FindPrimitiveClass('D');
322 boxed_value.d = primitive_field->GetDouble(o);
323 } else if (src_descriptor->Equals("Ljava/lang/Integer;")) {
324 src_class = class_linker->FindPrimitiveClass('I');
325 boxed_value.i = primitive_field->GetInt(o);
326 } else if (src_descriptor->Equals("Ljava/lang/Long;")) {
327 src_class = class_linker->FindPrimitiveClass('J');
328 boxed_value.j = primitive_field->GetLong(o);
329 } else if (src_descriptor->Equals("Ljava/lang/Short;")) {
330 src_class = class_linker->FindPrimitiveClass('S');
Ian Rogers466bb252011-10-14 03:29:56 -0700331 boxed_value.i = primitive_field->GetShort(o); // and extend read value to 32bits
Elliott Hughes418d20f2011-09-22 14:00:39 -0700332 } else {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700333 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Elliott Hughes418d20f2011-09-22 14:00:39 -0700334 "%s is not a boxed primitive type", PrettyDescriptor(src_descriptor).c_str());
335 return false;
336 }
337
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700338 return ConvertPrimitiveValue(src_class->GetPrimitiveType(), dst_class->GetPrimitiveType(),
339 boxed_value, unboxed_value);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700340}
341
342} // namespace art