blob: 70636d6919472f659fad14a446ca905d9d548ca6 [file] [log] [blame]
Brian Carlstromf867b6f2011-09-16 12:17:25 -07001/*
2 * Copyright (C) 2008 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 "jni_internal.h"
18#include "class_linker.h"
19#include "object.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080020#include "object_utils.h"
Elliott Hughes418d20f2011-09-22 14:00:39 -070021#include "reflection.h"
Brian Carlstromf867b6f2011-09-16 12:17:25 -070022
23#include "JniConstants.h" // Last to avoid problems with LOG redefinition.
24
25namespace art {
26
Elliott Hughes0512f022012-03-15 22:10:52 -070027static bool GetFieldValue(Object* o, Field* f, JValue& value, bool allow_references) {
Brian Carlstromb82b6872011-10-26 17:18:07 -070028 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Elliott Hughes923e8b82012-03-23 11:44:07 -070029 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(f->GetDeclaringClass(), true)) {
30 return false;
31 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080032 switch (FieldHelper(f).GetTypeAsPrimitiveType()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070033 case Primitive::kPrimBoolean:
Elliott Hughes33203b52011-09-20 19:42:01 -070034 value.z = f->GetBoolean(o);
35 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070036 case Primitive::kPrimByte:
Elliott Hughes33203b52011-09-20 19:42:01 -070037 value.b = f->GetByte(o);
38 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070039 case Primitive::kPrimChar:
Elliott Hughes33203b52011-09-20 19:42:01 -070040 value.c = f->GetChar(o);
41 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070042 case Primitive::kPrimDouble:
Elliott Hughes33203b52011-09-20 19:42:01 -070043 value.d = f->GetDouble(o);
44 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070045 case Primitive::kPrimFloat:
Elliott Hughes33203b52011-09-20 19:42:01 -070046 value.f = f->GetFloat(o);
47 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070048 case Primitive::kPrimInt:
Elliott Hughes33203b52011-09-20 19:42:01 -070049 value.i = f->GetInt(o);
50 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070051 case Primitive::kPrimLong:
Elliott Hughes33203b52011-09-20 19:42:01 -070052 value.j = f->GetLong(o);
53 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070054 case Primitive::kPrimShort:
Elliott Hughes33203b52011-09-20 19:42:01 -070055 value.s = f->GetShort(o);
56 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070057 case Primitive::kPrimNot:
Elliott Hughes33203b52011-09-20 19:42:01 -070058 if (allow_references) {
59 value.l = f->GetObject(o);
60 return true;
61 }
62 // Else break to report an error.
63 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070064 case Primitive::kPrimVoid:
Elliott Hughes33203b52011-09-20 19:42:01 -070065 // Never okay.
66 break;
67 }
Elliott Hughes5cb5ad22011-10-02 12:13:39 -070068 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Elliott Hughes33203b52011-09-20 19:42:01 -070069 "Not a primitive field: %s", PrettyField(f).c_str());
70 return false;
71}
72
Elliott Hughes0512f022012-03-15 22:10:52 -070073static bool CheckReceiver(JNIEnv* env, jobject javaObj, Field* f, Object*& o) {
Elliott Hughesed1c1e32011-10-02 14:31:05 -070074 if (f->IsStatic()) {
75 o = NULL;
76 return true;
77 }
78
79 o = Decode<Object*>(env, javaObj);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080080 Class* declaringClass = f->GetDeclaringClass();
Elliott Hughesed1c1e32011-10-02 14:31:05 -070081 if (!VerifyObjectInClass(env, o, declaringClass)) {
82 return false;
83 }
84 return true;
85}
86
Elliott Hughes0512f022012-03-15 22:10:52 -070087static jobject Field_get(JNIEnv* env, jobject javaField, jobject javaObj) {
Elliott Hughes418d20f2011-09-22 14:00:39 -070088 Field* f = DecodeField(env->FromReflectedField(javaField));
Elliott Hughesed1c1e32011-10-02 14:31:05 -070089 Object* o = NULL;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080090 if (!CheckReceiver(env, javaObj, f, o)) {
91 return NULL;
92 }
93
94 // Get the field's value, boxing if necessary.
Elliott Hughesdbac3092012-03-16 18:00:30 -070095 JValue value = { 0 };
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080096 if (!GetFieldValue(o, f, value, true)) {
97 return NULL;
98 }
Elliott Hughesdbac3092012-03-16 18:00:30 -070099 BoxPrimitive(FieldHelper(f).GetTypeAsPrimitiveType(), value);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800100
101 return AddLocalReference<jobject>(env, value.l);
102}
103
Elliott Hughes0512f022012-03-15 22:10:52 -0700104static JValue GetPrimitiveField(JNIEnv* env, jobject javaField, jobject javaObj, char dst_descriptor) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800105 Field* f = DecodeField(env->FromReflectedField(javaField));
106 Object* o = NULL;
107 if (!CheckReceiver(env, javaObj, f, o)) {
Elliott Hughesed1c1e32011-10-02 14:31:05 -0700108 return JValue();
Elliott Hughes33203b52011-09-20 19:42:01 -0700109 }
110
111 // Read the value.
Elliott Hughesdbac3092012-03-16 18:00:30 -0700112 JValue field_value = { 0 };
Elliott Hughes33203b52011-09-20 19:42:01 -0700113 if (!GetFieldValue(o, f, field_value, false)) {
114 return JValue();
115 }
116
117 // Widen it if necessary (and possible).
118 JValue wide_value;
Elliott Hughes582a7d12011-10-10 18:38:42 -0700119 Class* dst_type = Runtime::Current()->GetClassLinker()->FindPrimitiveClass(dst_descriptor);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800120 if (!ConvertPrimitiveValue(FieldHelper(f).GetTypeAsPrimitiveType(), dst_type->GetPrimitiveType(),
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700121 field_value, wide_value)) {
Elliott Hughes33203b52011-09-20 19:42:01 -0700122 return JValue();
123 }
124 return wide_value;
125}
126
Elliott Hughes0512f022012-03-15 22:10:52 -0700127static jboolean Field_getBoolean(JNIEnv* env, jobject javaField, jobject javaObj) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800128 return GetPrimitiveField(env, javaField, javaObj, 'Z').z;
Elliott Hughes33203b52011-09-20 19:42:01 -0700129}
130
Elliott Hughes0512f022012-03-15 22:10:52 -0700131static jbyte Field_getByte(JNIEnv* env, jobject javaField, jobject javaObj) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800132 return GetPrimitiveField(env, javaField, javaObj, 'B').b;
Elliott Hughes33203b52011-09-20 19:42:01 -0700133}
134
Elliott Hughes0512f022012-03-15 22:10:52 -0700135static jchar Field_getChar(JNIEnv* env, jobject javaField, jobject javaObj) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800136 return GetPrimitiveField(env, javaField, javaObj, 'C').c;
Elliott Hughes33203b52011-09-20 19:42:01 -0700137}
138
Elliott Hughes0512f022012-03-15 22:10:52 -0700139static jdouble Field_getDouble(JNIEnv* env, jobject javaField, jobject javaObj) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800140 return GetPrimitiveField(env, javaField, javaObj, 'D').d;
Elliott Hughes33203b52011-09-20 19:42:01 -0700141}
142
Elliott Hughes0512f022012-03-15 22:10:52 -0700143static jfloat Field_getFloat(JNIEnv* env, jobject javaField, jobject javaObj) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800144 return GetPrimitiveField(env, javaField, javaObj, 'F').f;
Elliott Hughes33203b52011-09-20 19:42:01 -0700145}
146
Elliott Hughes0512f022012-03-15 22:10:52 -0700147static jint Field_getInt(JNIEnv* env, jobject javaField, jobject javaObj) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800148 return GetPrimitiveField(env, javaField, javaObj, 'I').i;
Elliott Hughes33203b52011-09-20 19:42:01 -0700149}
150
Elliott Hughes0512f022012-03-15 22:10:52 -0700151static jlong Field_getLong(JNIEnv* env, jobject javaField, jobject javaObj) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800152 return GetPrimitiveField(env, javaField, javaObj, 'J').j;
Elliott Hughes33203b52011-09-20 19:42:01 -0700153}
154
Elliott Hughes0512f022012-03-15 22:10:52 -0700155static jshort Field_getShort(JNIEnv* env, jobject javaField, jobject javaObj) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800156 return GetPrimitiveField(env, javaField, javaObj, 'S').s;
Elliott Hughes33203b52011-09-20 19:42:01 -0700157}
158
Elliott Hughes0512f022012-03-15 22:10:52 -0700159static void SetFieldValue(Object* o, Field* f, const JValue& new_value, bool allow_references) {
Elliott Hughes923e8b82012-03-23 11:44:07 -0700160 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(f->GetDeclaringClass(), true)) {
161 return;
162 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800163 switch (FieldHelper(f).GetTypeAsPrimitiveType()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700164 case Primitive::kPrimBoolean:
Elliott Hughes33203b52011-09-20 19:42:01 -0700165 f->SetBoolean(o, new_value.z);
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700166 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700167 case Primitive::kPrimByte:
Elliott Hughes33203b52011-09-20 19:42:01 -0700168 f->SetByte(o, new_value.b);
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700169 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700170 case Primitive::kPrimChar:
Elliott Hughes33203b52011-09-20 19:42:01 -0700171 f->SetChar(o, new_value.c);
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700172 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700173 case Primitive::kPrimDouble:
Elliott Hughes33203b52011-09-20 19:42:01 -0700174 f->SetDouble(o, new_value.d);
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700175 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700176 case Primitive::kPrimFloat:
Elliott Hughes33203b52011-09-20 19:42:01 -0700177 f->SetFloat(o, new_value.f);
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700178 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700179 case Primitive::kPrimInt:
Elliott Hughes33203b52011-09-20 19:42:01 -0700180 f->SetInt(o, new_value.i);
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700181 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700182 case Primitive::kPrimLong:
Elliott Hughes33203b52011-09-20 19:42:01 -0700183 f->SetLong(o, new_value.j);
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700184 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700185 case Primitive::kPrimShort:
Elliott Hughes33203b52011-09-20 19:42:01 -0700186 f->SetShort(o, new_value.s);
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700187 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700188 case Primitive::kPrimNot:
Elliott Hughes33203b52011-09-20 19:42:01 -0700189 if (allow_references) {
190 f->SetObject(o, new_value.l);
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700191 break;
Elliott Hughes33203b52011-09-20 19:42:01 -0700192 }
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700193 // Else fall through to report an error.
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700194 case Primitive::kPrimVoid:
Elliott Hughes33203b52011-09-20 19:42:01 -0700195 // Never okay.
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700196 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700197 "Not a primitive field: %s", PrettyField(f).c_str());
198 return;
Elliott Hughes33203b52011-09-20 19:42:01 -0700199 }
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700200
201 // Special handling for final fields on SMP systems.
202 // We need a store/store barrier here (JMM requirement).
203 if (f->IsFinal()) {
204 ANDROID_MEMBAR_STORE();
205 }
Elliott Hughes33203b52011-09-20 19:42:01 -0700206}
207
Elliott Hughes0512f022012-03-15 22:10:52 -0700208static void Field_set(JNIEnv* env, jobject javaField, jobject javaObj, jobject javaValue) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800209 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
210 Field* f = DecodeField(env->FromReflectedField(javaField));
211
212 // Unbox the value, if necessary.
213 Object* boxed_value = Decode<Object*>(env, javaValue);
214 JValue unboxed_value;
Elliott Hughesdbac3092012-03-16 18:00:30 -0700215 if (!UnboxPrimitive(boxed_value, FieldHelper(f).GetType(), unboxed_value, "field")) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800216 return;
217 }
218
219 // Check that the receiver is non-null and an instance of the field's declaring class.
220 Object* o = NULL;
221 if (!CheckReceiver(env, javaObj, f, o)) {
222 return;
223 }
224
225 SetFieldValue(o, f, unboxed_value, true);
226}
227
Elliott Hughes0512f022012-03-15 22:10:52 -0700228static void SetPrimitiveField(JNIEnv* env, jobject javaField, jobject javaObj, char src_descriptor,
229 const JValue& new_value) {
Brian Carlstromb82b6872011-10-26 17:18:07 -0700230 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700231 Field* f = DecodeField(env->FromReflectedField(javaField));
Elliott Hughesed1c1e32011-10-02 14:31:05 -0700232 Object* o = NULL;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800233 if (!CheckReceiver(env, javaObj, f, o)) {
Elliott Hughesed1c1e32011-10-02 14:31:05 -0700234 return;
Elliott Hughes33203b52011-09-20 19:42:01 -0700235 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800236 FieldHelper fh(f);
237 if (!fh.IsPrimitiveType()) {
Jesse Wilsonc129a6b2011-11-24 14:47:46 -0500238 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
239 "Not a primitive field: %s", PrettyField(f).c_str());
240 return;
241 }
Elliott Hughes33203b52011-09-20 19:42:01 -0700242
243 // Widen the value if necessary (and possible).
244 JValue wide_value;
Elliott Hughes582a7d12011-10-10 18:38:42 -0700245 Class* src_type = Runtime::Current()->GetClassLinker()->FindPrimitiveClass(src_descriptor);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800246 if (!ConvertPrimitiveValue(src_type->GetPrimitiveType(), fh.GetTypeAsPrimitiveType(),
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700247 new_value, wide_value)) {
Elliott Hughes33203b52011-09-20 19:42:01 -0700248 return;
249 }
250
251 // Write the value.
252 SetFieldValue(o, f, wide_value, false);
253}
254
Elliott Hughes0512f022012-03-15 22:10:52 -0700255static void Field_setBoolean(JNIEnv* env, jobject javaField, jobject javaObj, jboolean value) {
Elliott Hughes33203b52011-09-20 19:42:01 -0700256 JValue v = { 0 };
257 v.z = value;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800258 SetPrimitiveField(env, javaField, javaObj, 'Z', v);
Elliott Hughes33203b52011-09-20 19:42:01 -0700259}
260
Elliott Hughes0512f022012-03-15 22:10:52 -0700261static void Field_setByte(JNIEnv* env, jobject javaField, jobject javaObj, jbyte value) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800262 JValue v = { 0 };
263 v.b = value;
264 SetPrimitiveField(env, javaField, javaObj, 'B', v);
Elliott Hughes33203b52011-09-20 19:42:01 -0700265}
266
Elliott Hughes0512f022012-03-15 22:10:52 -0700267static void Field_setChar(JNIEnv* env, jobject javaField, jobject javaObj, jchar value) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800268 JValue v = { 0 };
269 v.c = value;
270 SetPrimitiveField(env, javaField, javaObj, 'C', v);
271}
Elliott Hughes33203b52011-09-20 19:42:01 -0700272
Elliott Hughes0512f022012-03-15 22:10:52 -0700273static void Field_setDouble(JNIEnv* env, jobject javaField, jobject javaObj, jdouble value) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800274 JValue v = { 0 };
275 v.d = value;
276 SetPrimitiveField(env, javaField, javaObj, 'D', v);
277}
Elliott Hughes33203b52011-09-20 19:42:01 -0700278
Elliott Hughes0512f022012-03-15 22:10:52 -0700279static void Field_setFloat(JNIEnv* env, jobject javaField, jobject javaObj, jfloat value) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800280 JValue v = { 0 };
281 v.f = value;
282 SetPrimitiveField(env, javaField, javaObj, 'F', v);
283}
284
Elliott Hughes0512f022012-03-15 22:10:52 -0700285static void Field_setInt(JNIEnv* env, jobject javaField, jobject javaObj, jint value) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800286 JValue v = { 0 };
287 v.i = value;
288 SetPrimitiveField(env, javaField, javaObj, 'I', v);
289}
290
Elliott Hughes0512f022012-03-15 22:10:52 -0700291static void Field_setLong(JNIEnv* env, jobject javaField, jobject javaObj, jlong value) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800292 JValue v = { 0 };
293 v.j = value;
294 SetPrimitiveField(env, javaField, javaObj, 'J', v);
295}
296
Elliott Hughes0512f022012-03-15 22:10:52 -0700297static void Field_setShort(JNIEnv* env, jobject javaField, jobject javaObj, jshort value) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800298 JValue v = { 0 };
299 v.s = value;
300 SetPrimitiveField(env, javaField, javaObj, 'S', v);
Elliott Hughes33203b52011-09-20 19:42:01 -0700301}
302
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700303static JNINativeMethod gMethods[] = {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800304 NATIVE_METHOD(Field, get, "(Ljava/lang/Object;)Ljava/lang/Object;"),
305 NATIVE_METHOD(Field, getBoolean, "(Ljava/lang/Object;)Z"),
306 NATIVE_METHOD(Field, getByte, "(Ljava/lang/Object;)B"),
307 NATIVE_METHOD(Field, getChar, "(Ljava/lang/Object;)C"),
308 NATIVE_METHOD(Field, getDouble, "(Ljava/lang/Object;)D"),
309 NATIVE_METHOD(Field, getFloat, "(Ljava/lang/Object;)F"),
310 NATIVE_METHOD(Field, getInt, "(Ljava/lang/Object;)I"),
311 NATIVE_METHOD(Field, getLong, "(Ljava/lang/Object;)J"),
312 NATIVE_METHOD(Field, getShort, "(Ljava/lang/Object;)S"),
313 NATIVE_METHOD(Field, set, "(Ljava/lang/Object;Ljava/lang/Object;)V"),
314 NATIVE_METHOD(Field, setBoolean, "(Ljava/lang/Object;Z)V"),
315 NATIVE_METHOD(Field, setByte, "(Ljava/lang/Object;B)V"),
316 NATIVE_METHOD(Field, setChar, "(Ljava/lang/Object;C)V"),
317 NATIVE_METHOD(Field, setDouble, "(Ljava/lang/Object;D)V"),
318 NATIVE_METHOD(Field, setFloat, "(Ljava/lang/Object;F)V"),
319 NATIVE_METHOD(Field, setInt, "(Ljava/lang/Object;I)V"),
320 NATIVE_METHOD(Field, setLong, "(Ljava/lang/Object;J)V"),
321 NATIVE_METHOD(Field, setShort, "(Ljava/lang/Object;S)V"),
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700322};
323
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700324void register_java_lang_reflect_Field(JNIEnv* env) {
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700325 jniRegisterNativeMethods(env, "java/lang/reflect/Field", gMethods, NELEM(gMethods));
326}
327
328} // namespace art