blob: b367ad3049ab54b8b3af09fc90fbd1170eed70c5 [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
36void InitBoxingMethod(JNIEnv* env, Method*& m, jclass c, const char* method_signature) {
37 m = DecodeMethod(env->GetStaticMethodID(c, "valueOf", method_signature));
38}
39
40void InitBoxingMethods(JNIEnv* env) {
41 InitBoxingMethod(env, gBoolean_valueOf, JniConstants::booleanClass, "(Z)Ljava/lang/Boolean;");
42 InitBoxingMethod(env, gByte_valueOf, JniConstants::byteClass, "(B)Ljava/lang/Byte;");
43 InitBoxingMethod(env, gCharacter_valueOf, JniConstants::characterClass, "(C)Ljava/lang/Character;");
44 InitBoxingMethod(env, gDouble_valueOf, JniConstants::doubleClass, "(D)Ljava/lang/Double;");
45 InitBoxingMethod(env, gFloat_valueOf, JniConstants::floatClass, "(F)Ljava/lang/Float;");
46 InitBoxingMethod(env, gInteger_valueOf, JniConstants::integerClass, "(I)Ljava/lang/Integer;");
47 InitBoxingMethod(env, gLong_valueOf, JniConstants::longClass, "(J)Ljava/lang/Long;");
48 InitBoxingMethod(env, gShort_valueOf, JniConstants::shortClass, "(S)Ljava/lang/Short;");
49}
50
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070051jobject InvokeMethod(JNIEnv* env, jobject javaMethod, jobject javaReceiver, jobject javaArgs, jobject javaParams) {
52 Thread* self = Thread::Current();
53 ScopedThreadStateChange tsc(self, Thread::kRunnable);
54
55 jmethodID mid = env->FromReflectedMethod(javaMethod);
56 Method* m = reinterpret_cast<Method*>(mid);
57
58 Class* declaring_class = m->GetDeclaringClass();
59 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(declaring_class, true)) {
60 return NULL;
61 }
62
63 Object* receiver = NULL;
64 if (!m->IsStatic()) {
65 // Check that the receiver is non-null and an instance of the field's declaring class.
66 receiver = Decode<Object*>(env, javaReceiver);
67 if (!VerifyObjectInClass(env, receiver, declaring_class)) {
68 return NULL;
69 }
70
71 // Find the actual implementation of the virtual method.
72 m = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(m);
Ian Rogers466bb252011-10-14 03:29:56 -070073 mid = reinterpret_cast<jmethodID>(m);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070074 }
75
76 // Get our arrays of arguments and their types, and check they're the same size.
77 ObjectArray<Object>* objects = Decode<ObjectArray<Object>*>(env, javaArgs);
78 ObjectArray<Class>* classes = Decode<ObjectArray<Class>*>(env, javaParams);
79 int32_t arg_count = (objects != NULL) ? objects->GetLength() : 0;
80 if (arg_count != classes->GetLength()) {
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",
83 classes->GetLength(), arg_count);
84 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());
90 for (int32_t i = 0; i < arg_count; ++i) {
91 Object* arg = objects->Get(i);
92 Class* dst_class = classes->Get(i);
93 if (dst_class->IsPrimitive()) {
94 if (!UnboxPrimitive(env, arg, dst_class, decoded_args[i])) {
95 return NULL;
96 }
97 } else {
98 args[i].l = AddLocalReference<jobject>(env, arg);
99 }
100 }
101
102 // Invoke the method.
103 JValue value = InvokeWithJValues(env, javaReceiver, mid, args.get());
104
105 // Wrap any exception with "Ljava/lang/reflect/InvocationTargetException;" and return early.
106 if (self->IsExceptionPending()) {
107 jthrowable th = env->ExceptionOccurred();
108 env->ExceptionClear();
109 jclass exception_class = env->FindClass("java/lang/reflect/InvocationTargetException");
110 jmethodID mid = env->GetMethodID(exception_class, "<init>", "(Ljava/lang/Throwable;)V");
111 jobject exception_instance = env->NewObject(exception_class, mid, th);
112 env->Throw(reinterpret_cast<jthrowable>(exception_instance));
113 return NULL;
114 }
115
116 // Box if necessary and return.
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700117 BoxPrimitive(env, m->GetReturnType()->GetPrimitiveType(), value);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700118 return AddLocalReference<jobject>(env, value.l);
119}
120
Elliott Hughes418d20f2011-09-22 14:00:39 -0700121bool VerifyObjectInClass(JNIEnv* env, Object* o, Class* c) {
122 if (o == NULL) {
123 jniThrowNullPointerException(env, "receiver for non-static field access was null");
124 return false;
125 }
126 if (!o->InstanceOf(c)) {
127 std::string expectedClassName(PrettyDescriptor(c->GetDescriptor()));
128 std::string actualClassName(PrettyTypeOf(o));
129 jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException",
130 "expected receiver of type %s, but got %s",
131 expectedClassName.c_str(), actualClassName.c_str());
132 return false;
133 }
134 return true;
135}
136
137/*
138 * Convert primitive, boxed data from "srcPtr" to "dstPtr".
139 *
140 * Section v2 2.6 lists the various conversions and promotions. We
141 * allow the "widening" and "identity" conversions, but don't allow the
142 * "narrowing" conversions.
143 *
144 * Allowed:
145 * byte to short, int, long, float, double
146 * short to int, long, float double
147 * char to int, long, float, double
148 * int to long, float, double
149 * long to float, double
150 * float to double
151 * Values of types byte, char, and short are "internally" widened to int.
152 *
153 * Returns the width in 32-bit words of the destination primitive, or
154 * -1 if the conversion is not allowed.
155 */
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700156bool ConvertPrimitiveValue(Primitive::Type srcType, Primitive::Type dstType,
157 const JValue& src, JValue& dst) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700158 switch (dstType) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700159 case Primitive::kPrimBoolean:
160 case Primitive::kPrimChar:
161 case Primitive::kPrimByte:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700162 if (srcType == dstType) {
163 dst.i = src.i;
164 return true;
165 }
166 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700167 case Primitive::kPrimShort:
168 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimShort) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700169 dst.i = src.i;
170 return true;
171 }
172 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700173 case Primitive::kPrimInt:
174 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
175 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700176 dst.i = src.i;
177 return true;
178 }
179 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700180 case Primitive::kPrimLong:
181 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
182 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700183 dst.j = src.i;
184 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700185 } else if (srcType == Primitive::kPrimLong) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700186 dst.j = src.j;
187 return true;
188 }
189 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700190 case Primitive::kPrimFloat:
191 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
192 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700193 dst.f = src.i;
194 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700195 } else if (srcType == Primitive::kPrimLong) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700196 dst.f = src.j;
197 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700198 } else if (srcType == Primitive::kPrimFloat) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700199 dst.i = src.i;
200 return true;
201 }
202 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700203 case Primitive::kPrimDouble:
204 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
205 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700206 dst.d = src.i;
207 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700208 } else if (srcType == Primitive::kPrimLong) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700209 dst.d = src.j;
210 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700211 } else if (srcType == Primitive::kPrimFloat) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700212 dst.d = src.f;
213 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700214 } else if (srcType == Primitive::kPrimDouble) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700215 dst.j = src.j;
216 return true;
217 }
218 break;
219 default:
220 break;
221 }
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700222 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700223 "invalid primitive conversion from %s to %s",
224 PrettyDescriptor(srcType).c_str(),
225 PrettyDescriptor(dstType).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700226 return false;
227}
228
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700229void BoxPrimitive(JNIEnv* env, Primitive::Type src_class, JValue& value) {
230 if (src_class == Primitive::kPrimNot) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700231 return;
232 }
233
234 Method* m = NULL;
235 UniquePtr<byte[]> args(new byte[8]);
236 memset(&args[0], 0, 8);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700237 switch (src_class) {
238 case Primitive::kPrimBoolean:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700239 m = gBoolean_valueOf;
240 *reinterpret_cast<uint32_t*>(&args[0]) = value.z;
241 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700242 case Primitive::kPrimByte:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700243 m = gByte_valueOf;
244 *reinterpret_cast<uint32_t*>(&args[0]) = value.b;
245 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700246 case Primitive::kPrimChar:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700247 m = gCharacter_valueOf;
248 *reinterpret_cast<uint32_t*>(&args[0]) = value.c;
249 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700250 case Primitive::kPrimDouble:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700251 m = gDouble_valueOf;
252 *reinterpret_cast<double*>(&args[0]) = value.d;
253 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700254 case Primitive::kPrimFloat:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700255 m = gFloat_valueOf;
256 *reinterpret_cast<float*>(&args[0]) = value.f;
257 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700258 case Primitive::kPrimInt:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700259 m = gInteger_valueOf;
260 *reinterpret_cast<uint32_t*>(&args[0]) = value.i;
261 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700262 case Primitive::kPrimLong:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700263 m = gLong_valueOf;
264 *reinterpret_cast<uint64_t*>(&args[0]) = value.j;
265 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700266 case Primitive::kPrimShort:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700267 m = gShort_valueOf;
268 *reinterpret_cast<uint32_t*>(&args[0]) = value.s;
269 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700270 case Primitive::kPrimVoid:
Elliott Hughes418d20f2011-09-22 14:00:39 -0700271 // There's no such thing as a void field, and void methods invoked via reflection return null.
272 value.l = NULL;
273 return;
274 default:
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700275 LOG(FATAL) << static_cast<int>(src_class);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700276 }
277
278 Thread* self = Thread::Current();
279 ScopedThreadStateChange tsc(self, Thread::kRunnable);
280 m->Invoke(self, NULL, args.get(), &value);
281}
282
283bool UnboxPrimitive(JNIEnv* env, Object* o, Class* dst_class, JValue& unboxed_value) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700284 if (!dst_class->IsPrimitive()) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700285 if (o != NULL && !o->InstanceOf(dst_class)) {
286 jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException",
287 "expected object of type %s, but got %s",
288 PrettyDescriptor(dst_class->GetDescriptor()).c_str(),
289 PrettyTypeOf(o).c_str());
290 return false;
291 }
292 unboxed_value.l = o;
293 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700294 } else if (dst_class->GetPrimitiveType() == Primitive::kPrimVoid) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700295 Thread::Current()->ThrowNewException("Ljava/lang/IllegalArgumentException;",
296 "can't unbox to void");
297 return false;
298 }
299
300 if (o == NULL) {
301 Thread::Current()->ThrowNewException("Ljava/lang/IllegalArgumentException;",
302 "null passed for boxed primitive type");
303 return false;
304 }
305
306 JValue boxed_value = { 0 };
307 const String* src_descriptor = o->GetClass()->GetDescriptor();
308 Class* src_class = NULL;
309 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
310 Field* primitive_field = o->GetClass()->GetIFields()->Get(0);
311 if (src_descriptor->Equals("Ljava/lang/Boolean;")) {
312 src_class = class_linker->FindPrimitiveClass('Z');
Ian Rogers466bb252011-10-14 03:29:56 -0700313 boxed_value.i = primitive_field->GetBoolean(o); // and extend read value to 32bits
Elliott Hughes418d20f2011-09-22 14:00:39 -0700314 } else if (src_descriptor->Equals("Ljava/lang/Byte;")) {
315 src_class = class_linker->FindPrimitiveClass('B');
Ian Rogers466bb252011-10-14 03:29:56 -0700316 boxed_value.i = primitive_field->GetByte(o); // and extend read value to 32bits
Elliott Hughes418d20f2011-09-22 14:00:39 -0700317 } else if (src_descriptor->Equals("Ljava/lang/Character;")) {
318 src_class = class_linker->FindPrimitiveClass('C');
Ian Rogers466bb252011-10-14 03:29:56 -0700319 boxed_value.i = primitive_field->GetChar(o); // and extend read value to 32bits
Elliott Hughes418d20f2011-09-22 14:00:39 -0700320 } else if (src_descriptor->Equals("Ljava/lang/Float;")) {
321 src_class = class_linker->FindPrimitiveClass('F');
322 boxed_value.f = primitive_field->GetFloat(o);
323 } else if (src_descriptor->Equals("Ljava/lang/Double;")) {
324 src_class = class_linker->FindPrimitiveClass('D');
325 boxed_value.d = primitive_field->GetDouble(o);
326 } else if (src_descriptor->Equals("Ljava/lang/Integer;")) {
327 src_class = class_linker->FindPrimitiveClass('I');
328 boxed_value.i = primitive_field->GetInt(o);
329 } else if (src_descriptor->Equals("Ljava/lang/Long;")) {
330 src_class = class_linker->FindPrimitiveClass('J');
331 boxed_value.j = primitive_field->GetLong(o);
332 } else if (src_descriptor->Equals("Ljava/lang/Short;")) {
333 src_class = class_linker->FindPrimitiveClass('S');
Ian Rogers466bb252011-10-14 03:29:56 -0700334 boxed_value.i = primitive_field->GetShort(o); // and extend read value to 32bits
Elliott Hughes418d20f2011-09-22 14:00:39 -0700335 } else {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700336 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Elliott Hughes418d20f2011-09-22 14:00:39 -0700337 "%s is not a boxed primitive type", PrettyDescriptor(src_descriptor).c_str());
338 return false;
339 }
340
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700341 return ConvertPrimitiveValue(src_class->GetPrimitiveType(), dst_class->GetPrimitiveType(),
342 boxed_value, unboxed_value);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700343}
344
345} // namespace art