blob: b22212fdc5037a301ec7c045aca6fb7f8a44ae9b [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);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080029 switch (FieldHelper(f).GetTypeAsPrimitiveType()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070030 case Primitive::kPrimBoolean:
Elliott Hughes33203b52011-09-20 19:42:01 -070031 value.z = f->GetBoolean(o);
32 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070033 case Primitive::kPrimByte:
Elliott Hughes33203b52011-09-20 19:42:01 -070034 value.b = f->GetByte(o);
35 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070036 case Primitive::kPrimChar:
Elliott Hughes33203b52011-09-20 19:42:01 -070037 value.c = f->GetChar(o);
38 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070039 case Primitive::kPrimDouble:
Elliott Hughes33203b52011-09-20 19:42:01 -070040 value.d = f->GetDouble(o);
41 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070042 case Primitive::kPrimFloat:
Elliott Hughes33203b52011-09-20 19:42:01 -070043 value.f = f->GetFloat(o);
44 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070045 case Primitive::kPrimInt:
Elliott Hughes33203b52011-09-20 19:42:01 -070046 value.i = f->GetInt(o);
47 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070048 case Primitive::kPrimLong:
Elliott Hughes33203b52011-09-20 19:42:01 -070049 value.j = f->GetLong(o);
50 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070051 case Primitive::kPrimShort:
Elliott Hughes33203b52011-09-20 19:42:01 -070052 value.s = f->GetShort(o);
53 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070054 case Primitive::kPrimNot:
Elliott Hughes33203b52011-09-20 19:42:01 -070055 if (allow_references) {
56 value.l = f->GetObject(o);
57 return true;
58 }
59 // Else break to report an error.
60 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070061 case Primitive::kPrimVoid:
Elliott Hughes33203b52011-09-20 19:42:01 -070062 // Never okay.
63 break;
64 }
Elliott Hughes5cb5ad22011-10-02 12:13:39 -070065 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Elliott Hughes33203b52011-09-20 19:42:01 -070066 "Not a primitive field: %s", PrettyField(f).c_str());
67 return false;
68}
69
Elliott Hughes0512f022012-03-15 22:10:52 -070070static bool CheckReceiver(JNIEnv* env, jobject javaObj, Field* f, Object*& o) {
Elliott Hughesed1c1e32011-10-02 14:31:05 -070071 if (f->IsStatic()) {
72 o = NULL;
73 return true;
74 }
75
76 o = Decode<Object*>(env, javaObj);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080077 Class* declaringClass = f->GetDeclaringClass();
Elliott Hughesed1c1e32011-10-02 14:31:05 -070078 if (!VerifyObjectInClass(env, o, declaringClass)) {
79 return false;
80 }
81 return true;
82}
83
Elliott Hughes0512f022012-03-15 22:10:52 -070084static jobject Field_get(JNIEnv* env, jobject javaField, jobject javaObj) {
Elliott Hughes418d20f2011-09-22 14:00:39 -070085 Field* f = DecodeField(env->FromReflectedField(javaField));
Elliott Hughesed1c1e32011-10-02 14:31:05 -070086 Object* o = NULL;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080087 if (!CheckReceiver(env, javaObj, f, o)) {
88 return NULL;
89 }
90
91 // Get the field's value, boxing if necessary.
Elliott Hughesdbac3092012-03-16 18:00:30 -070092 JValue value = { 0 };
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080093 if (!GetFieldValue(o, f, value, true)) {
94 return NULL;
95 }
Elliott Hughesdbac3092012-03-16 18:00:30 -070096 BoxPrimitive(FieldHelper(f).GetTypeAsPrimitiveType(), value);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080097
98 return AddLocalReference<jobject>(env, value.l);
99}
100
Elliott Hughes0512f022012-03-15 22:10:52 -0700101static JValue GetPrimitiveField(JNIEnv* env, jobject javaField, jobject javaObj, char dst_descriptor) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800102 Field* f = DecodeField(env->FromReflectedField(javaField));
103 Object* o = NULL;
104 if (!CheckReceiver(env, javaObj, f, o)) {
Elliott Hughesed1c1e32011-10-02 14:31:05 -0700105 return JValue();
Elliott Hughes33203b52011-09-20 19:42:01 -0700106 }
107
108 // Read the value.
Elliott Hughesdbac3092012-03-16 18:00:30 -0700109 JValue field_value = { 0 };
Elliott Hughes33203b52011-09-20 19:42:01 -0700110 if (!GetFieldValue(o, f, field_value, false)) {
111 return JValue();
112 }
113
114 // Widen it if necessary (and possible).
115 JValue wide_value;
Elliott Hughes582a7d12011-10-10 18:38:42 -0700116 Class* dst_type = Runtime::Current()->GetClassLinker()->FindPrimitiveClass(dst_descriptor);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800117 if (!ConvertPrimitiveValue(FieldHelper(f).GetTypeAsPrimitiveType(), dst_type->GetPrimitiveType(),
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700118 field_value, wide_value)) {
Elliott Hughes33203b52011-09-20 19:42:01 -0700119 return JValue();
120 }
121 return wide_value;
122}
123
Elliott Hughes0512f022012-03-15 22:10:52 -0700124static jboolean Field_getBoolean(JNIEnv* env, jobject javaField, jobject javaObj) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800125 return GetPrimitiveField(env, javaField, javaObj, 'Z').z;
Elliott Hughes33203b52011-09-20 19:42:01 -0700126}
127
Elliott Hughes0512f022012-03-15 22:10:52 -0700128static jbyte Field_getByte(JNIEnv* env, jobject javaField, jobject javaObj) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800129 return GetPrimitiveField(env, javaField, javaObj, 'B').b;
Elliott Hughes33203b52011-09-20 19:42:01 -0700130}
131
Elliott Hughes0512f022012-03-15 22:10:52 -0700132static jchar Field_getChar(JNIEnv* env, jobject javaField, jobject javaObj) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800133 return GetPrimitiveField(env, javaField, javaObj, 'C').c;
Elliott Hughes33203b52011-09-20 19:42:01 -0700134}
135
Elliott Hughes0512f022012-03-15 22:10:52 -0700136static jdouble Field_getDouble(JNIEnv* env, jobject javaField, jobject javaObj) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800137 return GetPrimitiveField(env, javaField, javaObj, 'D').d;
Elliott Hughes33203b52011-09-20 19:42:01 -0700138}
139
Elliott Hughes0512f022012-03-15 22:10:52 -0700140static jfloat Field_getFloat(JNIEnv* env, jobject javaField, jobject javaObj) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800141 return GetPrimitiveField(env, javaField, javaObj, 'F').f;
Elliott Hughes33203b52011-09-20 19:42:01 -0700142}
143
Elliott Hughes0512f022012-03-15 22:10:52 -0700144static jint Field_getInt(JNIEnv* env, jobject javaField, jobject javaObj) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800145 return GetPrimitiveField(env, javaField, javaObj, 'I').i;
Elliott Hughes33203b52011-09-20 19:42:01 -0700146}
147
Elliott Hughes0512f022012-03-15 22:10:52 -0700148static jlong Field_getLong(JNIEnv* env, jobject javaField, jobject javaObj) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800149 return GetPrimitiveField(env, javaField, javaObj, 'J').j;
Elliott Hughes33203b52011-09-20 19:42:01 -0700150}
151
Elliott Hughes0512f022012-03-15 22:10:52 -0700152static jshort Field_getShort(JNIEnv* env, jobject javaField, jobject javaObj) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800153 return GetPrimitiveField(env, javaField, javaObj, 'S').s;
Elliott Hughes33203b52011-09-20 19:42:01 -0700154}
155
Elliott Hughes0512f022012-03-15 22:10:52 -0700156static void SetFieldValue(Object* o, Field* f, const JValue& new_value, bool allow_references) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800157 switch (FieldHelper(f).GetTypeAsPrimitiveType()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700158 case Primitive::kPrimBoolean:
Elliott Hughes33203b52011-09-20 19:42:01 -0700159 f->SetBoolean(o, new_value.z);
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700160 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700161 case Primitive::kPrimByte:
Elliott Hughes33203b52011-09-20 19:42:01 -0700162 f->SetByte(o, new_value.b);
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700163 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700164 case Primitive::kPrimChar:
Elliott Hughes33203b52011-09-20 19:42:01 -0700165 f->SetChar(o, new_value.c);
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700166 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700167 case Primitive::kPrimDouble:
Elliott Hughes33203b52011-09-20 19:42:01 -0700168 f->SetDouble(o, new_value.d);
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700169 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700170 case Primitive::kPrimFloat:
Elliott Hughes33203b52011-09-20 19:42:01 -0700171 f->SetFloat(o, new_value.f);
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700172 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700173 case Primitive::kPrimInt:
Elliott Hughes33203b52011-09-20 19:42:01 -0700174 f->SetInt(o, new_value.i);
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700175 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700176 case Primitive::kPrimLong:
Elliott Hughes33203b52011-09-20 19:42:01 -0700177 f->SetLong(o, new_value.j);
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700178 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700179 case Primitive::kPrimShort:
Elliott Hughes33203b52011-09-20 19:42:01 -0700180 f->SetShort(o, new_value.s);
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700181 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700182 case Primitive::kPrimNot:
Elliott Hughes33203b52011-09-20 19:42:01 -0700183 if (allow_references) {
184 f->SetObject(o, new_value.l);
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700185 break;
Elliott Hughes33203b52011-09-20 19:42:01 -0700186 }
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700187 // Else fall through to report an error.
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700188 case Primitive::kPrimVoid:
Elliott Hughes33203b52011-09-20 19:42:01 -0700189 // Never okay.
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700190 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700191 "Not a primitive field: %s", PrettyField(f).c_str());
192 return;
Elliott Hughes33203b52011-09-20 19:42:01 -0700193 }
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700194
195 // Special handling for final fields on SMP systems.
196 // We need a store/store barrier here (JMM requirement).
197 if (f->IsFinal()) {
198 ANDROID_MEMBAR_STORE();
199 }
Elliott Hughes33203b52011-09-20 19:42:01 -0700200}
201
Elliott Hughes0512f022012-03-15 22:10:52 -0700202static void Field_set(JNIEnv* env, jobject javaField, jobject javaObj, jobject javaValue) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800203 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
204 Field* f = DecodeField(env->FromReflectedField(javaField));
205
206 // Unbox the value, if necessary.
207 Object* boxed_value = Decode<Object*>(env, javaValue);
208 JValue unboxed_value;
Elliott Hughesdbac3092012-03-16 18:00:30 -0700209 if (!UnboxPrimitive(boxed_value, FieldHelper(f).GetType(), unboxed_value, "field")) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800210 return;
211 }
212
213 // Check that the receiver is non-null and an instance of the field's declaring class.
214 Object* o = NULL;
215 if (!CheckReceiver(env, javaObj, f, o)) {
216 return;
217 }
218
219 SetFieldValue(o, f, unboxed_value, true);
220}
221
Elliott Hughes0512f022012-03-15 22:10:52 -0700222static void SetPrimitiveField(JNIEnv* env, jobject javaField, jobject javaObj, char src_descriptor,
223 const JValue& new_value) {
Brian Carlstromb82b6872011-10-26 17:18:07 -0700224 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700225 Field* f = DecodeField(env->FromReflectedField(javaField));
Elliott Hughesed1c1e32011-10-02 14:31:05 -0700226 Object* o = NULL;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800227 if (!CheckReceiver(env, javaObj, f, o)) {
Elliott Hughesed1c1e32011-10-02 14:31:05 -0700228 return;
Elliott Hughes33203b52011-09-20 19:42:01 -0700229 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800230 FieldHelper fh(f);
231 if (!fh.IsPrimitiveType()) {
Jesse Wilsonc129a6b2011-11-24 14:47:46 -0500232 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
233 "Not a primitive field: %s", PrettyField(f).c_str());
234 return;
235 }
Elliott Hughes33203b52011-09-20 19:42:01 -0700236
237 // Widen the value if necessary (and possible).
238 JValue wide_value;
Elliott Hughes582a7d12011-10-10 18:38:42 -0700239 Class* src_type = Runtime::Current()->GetClassLinker()->FindPrimitiveClass(src_descriptor);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800240 if (!ConvertPrimitiveValue(src_type->GetPrimitiveType(), fh.GetTypeAsPrimitiveType(),
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700241 new_value, wide_value)) {
Elliott Hughes33203b52011-09-20 19:42:01 -0700242 return;
243 }
244
245 // Write the value.
246 SetFieldValue(o, f, wide_value, false);
247}
248
Elliott Hughes0512f022012-03-15 22:10:52 -0700249static void Field_setBoolean(JNIEnv* env, jobject javaField, jobject javaObj, jboolean value) {
Elliott Hughes33203b52011-09-20 19:42:01 -0700250 JValue v = { 0 };
251 v.z = value;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800252 SetPrimitiveField(env, javaField, javaObj, 'Z', v);
Elliott Hughes33203b52011-09-20 19:42:01 -0700253}
254
Elliott Hughes0512f022012-03-15 22:10:52 -0700255static void Field_setByte(JNIEnv* env, jobject javaField, jobject javaObj, jbyte value) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800256 JValue v = { 0 };
257 v.b = value;
258 SetPrimitiveField(env, javaField, javaObj, 'B', v);
Elliott Hughes33203b52011-09-20 19:42:01 -0700259}
260
Elliott Hughes0512f022012-03-15 22:10:52 -0700261static void Field_setChar(JNIEnv* env, jobject javaField, jobject javaObj, jchar value) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800262 JValue v = { 0 };
263 v.c = value;
264 SetPrimitiveField(env, javaField, javaObj, 'C', v);
265}
Elliott Hughes33203b52011-09-20 19:42:01 -0700266
Elliott Hughes0512f022012-03-15 22:10:52 -0700267static void Field_setDouble(JNIEnv* env, jobject javaField, jobject javaObj, jdouble value) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800268 JValue v = { 0 };
269 v.d = value;
270 SetPrimitiveField(env, javaField, javaObj, 'D', v);
271}
Elliott Hughes33203b52011-09-20 19:42:01 -0700272
Elliott Hughes0512f022012-03-15 22:10:52 -0700273static void Field_setFloat(JNIEnv* env, jobject javaField, jobject javaObj, jfloat value) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800274 JValue v = { 0 };
275 v.f = value;
276 SetPrimitiveField(env, javaField, javaObj, 'F', v);
277}
278
Elliott Hughes0512f022012-03-15 22:10:52 -0700279static void Field_setInt(JNIEnv* env, jobject javaField, jobject javaObj, jint value) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800280 JValue v = { 0 };
281 v.i = value;
282 SetPrimitiveField(env, javaField, javaObj, 'I', v);
283}
284
Elliott Hughes0512f022012-03-15 22:10:52 -0700285static void Field_setLong(JNIEnv* env, jobject javaField, jobject javaObj, jlong value) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800286 JValue v = { 0 };
287 v.j = value;
288 SetPrimitiveField(env, javaField, javaObj, 'J', v);
289}
290
Elliott Hughes0512f022012-03-15 22:10:52 -0700291static void Field_setShort(JNIEnv* env, jobject javaField, jobject javaObj, jshort value) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800292 JValue v = { 0 };
293 v.s = value;
294 SetPrimitiveField(env, javaField, javaObj, 'S', v);
Elliott Hughes33203b52011-09-20 19:42:01 -0700295}
296
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700297static JNINativeMethod gMethods[] = {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800298 NATIVE_METHOD(Field, get, "(Ljava/lang/Object;)Ljava/lang/Object;"),
299 NATIVE_METHOD(Field, getBoolean, "(Ljava/lang/Object;)Z"),
300 NATIVE_METHOD(Field, getByte, "(Ljava/lang/Object;)B"),
301 NATIVE_METHOD(Field, getChar, "(Ljava/lang/Object;)C"),
302 NATIVE_METHOD(Field, getDouble, "(Ljava/lang/Object;)D"),
303 NATIVE_METHOD(Field, getFloat, "(Ljava/lang/Object;)F"),
304 NATIVE_METHOD(Field, getInt, "(Ljava/lang/Object;)I"),
305 NATIVE_METHOD(Field, getLong, "(Ljava/lang/Object;)J"),
306 NATIVE_METHOD(Field, getShort, "(Ljava/lang/Object;)S"),
307 NATIVE_METHOD(Field, set, "(Ljava/lang/Object;Ljava/lang/Object;)V"),
308 NATIVE_METHOD(Field, setBoolean, "(Ljava/lang/Object;Z)V"),
309 NATIVE_METHOD(Field, setByte, "(Ljava/lang/Object;B)V"),
310 NATIVE_METHOD(Field, setChar, "(Ljava/lang/Object;C)V"),
311 NATIVE_METHOD(Field, setDouble, "(Ljava/lang/Object;D)V"),
312 NATIVE_METHOD(Field, setFloat, "(Ljava/lang/Object;F)V"),
313 NATIVE_METHOD(Field, setInt, "(Ljava/lang/Object;I)V"),
314 NATIVE_METHOD(Field, setLong, "(Ljava/lang/Object;J)V"),
315 NATIVE_METHOD(Field, setShort, "(Ljava/lang/Object;S)V"),
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700316};
317
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700318void register_java_lang_reflect_Field(JNIEnv* env) {
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700319 jniRegisterNativeMethods(env, "java/lang/reflect/Field", gMethods, NELEM(gMethods));
320}
321
322} // namespace art