blob: 4eea533ab86e70ec2f6d9e766569d9e6f22bd095 [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 Hughes15216932012-03-21 21:53:06 -070029 DCHECK(f->GetDeclaringClass()->IsInitialized());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080030 switch (FieldHelper(f).GetTypeAsPrimitiveType()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070031 case Primitive::kPrimBoolean:
Elliott Hughes33203b52011-09-20 19:42:01 -070032 value.z = f->GetBoolean(o);
33 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070034 case Primitive::kPrimByte:
Elliott Hughes33203b52011-09-20 19:42:01 -070035 value.b = f->GetByte(o);
36 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070037 case Primitive::kPrimChar:
Elliott Hughes33203b52011-09-20 19:42:01 -070038 value.c = f->GetChar(o);
39 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070040 case Primitive::kPrimDouble:
Elliott Hughes33203b52011-09-20 19:42:01 -070041 value.d = f->GetDouble(o);
42 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070043 case Primitive::kPrimFloat:
Elliott Hughes33203b52011-09-20 19:42:01 -070044 value.f = f->GetFloat(o);
45 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070046 case Primitive::kPrimInt:
Elliott Hughes33203b52011-09-20 19:42:01 -070047 value.i = f->GetInt(o);
48 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070049 case Primitive::kPrimLong:
Elliott Hughes33203b52011-09-20 19:42:01 -070050 value.j = f->GetLong(o);
51 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070052 case Primitive::kPrimShort:
Elliott Hughes33203b52011-09-20 19:42:01 -070053 value.s = f->GetShort(o);
54 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070055 case Primitive::kPrimNot:
Elliott Hughes33203b52011-09-20 19:42:01 -070056 if (allow_references) {
57 value.l = f->GetObject(o);
58 return true;
59 }
60 // Else break to report an error.
61 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070062 case Primitive::kPrimVoid:
Elliott Hughes33203b52011-09-20 19:42:01 -070063 // Never okay.
64 break;
65 }
Elliott Hughes5cb5ad22011-10-02 12:13:39 -070066 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Elliott Hughes33203b52011-09-20 19:42:01 -070067 "Not a primitive field: %s", PrettyField(f).c_str());
68 return false;
69}
70
Elliott Hughes0512f022012-03-15 22:10:52 -070071static bool CheckReceiver(JNIEnv* env, jobject javaObj, Field* f, Object*& o) {
Elliott Hughesed1c1e32011-10-02 14:31:05 -070072 if (f->IsStatic()) {
73 o = NULL;
74 return true;
75 }
76
77 o = Decode<Object*>(env, javaObj);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080078 Class* declaringClass = f->GetDeclaringClass();
Elliott Hughesed1c1e32011-10-02 14:31:05 -070079 if (!VerifyObjectInClass(env, o, declaringClass)) {
80 return false;
81 }
82 return true;
83}
84
Elliott Hughes0512f022012-03-15 22:10:52 -070085static jobject Field_get(JNIEnv* env, jobject javaField, jobject javaObj) {
Elliott Hughes418d20f2011-09-22 14:00:39 -070086 Field* f = DecodeField(env->FromReflectedField(javaField));
Elliott Hughesed1c1e32011-10-02 14:31:05 -070087 Object* o = NULL;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080088 if (!CheckReceiver(env, javaObj, f, o)) {
89 return NULL;
90 }
91
92 // Get the field's value, boxing if necessary.
Elliott Hughesdbac3092012-03-16 18:00:30 -070093 JValue value = { 0 };
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080094 if (!GetFieldValue(o, f, value, true)) {
95 return NULL;
96 }
Elliott Hughesdbac3092012-03-16 18:00:30 -070097 BoxPrimitive(FieldHelper(f).GetTypeAsPrimitiveType(), value);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080098
99 return AddLocalReference<jobject>(env, value.l);
100}
101
Elliott Hughes0512f022012-03-15 22:10:52 -0700102static JValue GetPrimitiveField(JNIEnv* env, jobject javaField, jobject javaObj, char dst_descriptor) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800103 Field* f = DecodeField(env->FromReflectedField(javaField));
104 Object* o = NULL;
105 if (!CheckReceiver(env, javaObj, f, o)) {
Elliott Hughesed1c1e32011-10-02 14:31:05 -0700106 return JValue();
Elliott Hughes33203b52011-09-20 19:42:01 -0700107 }
108
109 // Read the value.
Elliott Hughesdbac3092012-03-16 18:00:30 -0700110 JValue field_value = { 0 };
Elliott Hughes33203b52011-09-20 19:42:01 -0700111 if (!GetFieldValue(o, f, field_value, false)) {
112 return JValue();
113 }
114
115 // Widen it if necessary (and possible).
116 JValue wide_value;
Elliott Hughes582a7d12011-10-10 18:38:42 -0700117 Class* dst_type = Runtime::Current()->GetClassLinker()->FindPrimitiveClass(dst_descriptor);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800118 if (!ConvertPrimitiveValue(FieldHelper(f).GetTypeAsPrimitiveType(), dst_type->GetPrimitiveType(),
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700119 field_value, wide_value)) {
Elliott Hughes33203b52011-09-20 19:42:01 -0700120 return JValue();
121 }
122 return wide_value;
123}
124
Elliott Hughes0512f022012-03-15 22:10:52 -0700125static jboolean Field_getBoolean(JNIEnv* env, jobject javaField, jobject javaObj) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800126 return GetPrimitiveField(env, javaField, javaObj, 'Z').z;
Elliott Hughes33203b52011-09-20 19:42:01 -0700127}
128
Elliott Hughes0512f022012-03-15 22:10:52 -0700129static jbyte Field_getByte(JNIEnv* env, jobject javaField, jobject javaObj) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800130 return GetPrimitiveField(env, javaField, javaObj, 'B').b;
Elliott Hughes33203b52011-09-20 19:42:01 -0700131}
132
Elliott Hughes0512f022012-03-15 22:10:52 -0700133static jchar Field_getChar(JNIEnv* env, jobject javaField, jobject javaObj) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800134 return GetPrimitiveField(env, javaField, javaObj, 'C').c;
Elliott Hughes33203b52011-09-20 19:42:01 -0700135}
136
Elliott Hughes0512f022012-03-15 22:10:52 -0700137static jdouble Field_getDouble(JNIEnv* env, jobject javaField, jobject javaObj) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800138 return GetPrimitiveField(env, javaField, javaObj, 'D').d;
Elliott Hughes33203b52011-09-20 19:42:01 -0700139}
140
Elliott Hughes0512f022012-03-15 22:10:52 -0700141static jfloat Field_getFloat(JNIEnv* env, jobject javaField, jobject javaObj) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800142 return GetPrimitiveField(env, javaField, javaObj, 'F').f;
Elliott Hughes33203b52011-09-20 19:42:01 -0700143}
144
Elliott Hughes0512f022012-03-15 22:10:52 -0700145static jint Field_getInt(JNIEnv* env, jobject javaField, jobject javaObj) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800146 return GetPrimitiveField(env, javaField, javaObj, 'I').i;
Elliott Hughes33203b52011-09-20 19:42:01 -0700147}
148
Elliott Hughes0512f022012-03-15 22:10:52 -0700149static jlong Field_getLong(JNIEnv* env, jobject javaField, jobject javaObj) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800150 return GetPrimitiveField(env, javaField, javaObj, 'J').j;
Elliott Hughes33203b52011-09-20 19:42:01 -0700151}
152
Elliott Hughes0512f022012-03-15 22:10:52 -0700153static jshort Field_getShort(JNIEnv* env, jobject javaField, jobject javaObj) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800154 return GetPrimitiveField(env, javaField, javaObj, 'S').s;
Elliott Hughes33203b52011-09-20 19:42:01 -0700155}
156
Elliott Hughes0512f022012-03-15 22:10:52 -0700157static void SetFieldValue(Object* o, Field* f, const JValue& new_value, bool allow_references) {
Elliott Hughes15216932012-03-21 21:53:06 -0700158 DCHECK(f->GetDeclaringClass()->IsInitialized());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800159 switch (FieldHelper(f).GetTypeAsPrimitiveType()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700160 case Primitive::kPrimBoolean:
Elliott Hughes33203b52011-09-20 19:42:01 -0700161 f->SetBoolean(o, new_value.z);
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700162 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700163 case Primitive::kPrimByte:
Elliott Hughes33203b52011-09-20 19:42:01 -0700164 f->SetByte(o, new_value.b);
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700165 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700166 case Primitive::kPrimChar:
Elliott Hughes33203b52011-09-20 19:42:01 -0700167 f->SetChar(o, new_value.c);
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700168 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700169 case Primitive::kPrimDouble:
Elliott Hughes33203b52011-09-20 19:42:01 -0700170 f->SetDouble(o, new_value.d);
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700171 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700172 case Primitive::kPrimFloat:
Elliott Hughes33203b52011-09-20 19:42:01 -0700173 f->SetFloat(o, new_value.f);
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700174 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700175 case Primitive::kPrimInt:
Elliott Hughes33203b52011-09-20 19:42:01 -0700176 f->SetInt(o, new_value.i);
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700177 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700178 case Primitive::kPrimLong:
Elliott Hughes33203b52011-09-20 19:42:01 -0700179 f->SetLong(o, new_value.j);
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700180 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700181 case Primitive::kPrimShort:
Elliott Hughes33203b52011-09-20 19:42:01 -0700182 f->SetShort(o, new_value.s);
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700183 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700184 case Primitive::kPrimNot:
Elliott Hughes33203b52011-09-20 19:42:01 -0700185 if (allow_references) {
186 f->SetObject(o, new_value.l);
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700187 break;
Elliott Hughes33203b52011-09-20 19:42:01 -0700188 }
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700189 // Else fall through to report an error.
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700190 case Primitive::kPrimVoid:
Elliott Hughes33203b52011-09-20 19:42:01 -0700191 // Never okay.
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700192 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700193 "Not a primitive field: %s", PrettyField(f).c_str());
194 return;
Elliott Hughes33203b52011-09-20 19:42:01 -0700195 }
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700196
197 // Special handling for final fields on SMP systems.
198 // We need a store/store barrier here (JMM requirement).
199 if (f->IsFinal()) {
200 ANDROID_MEMBAR_STORE();
201 }
Elliott Hughes33203b52011-09-20 19:42:01 -0700202}
203
Elliott Hughes0512f022012-03-15 22:10:52 -0700204static void Field_set(JNIEnv* env, jobject javaField, jobject javaObj, jobject javaValue) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800205 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
206 Field* f = DecodeField(env->FromReflectedField(javaField));
207
208 // Unbox the value, if necessary.
209 Object* boxed_value = Decode<Object*>(env, javaValue);
210 JValue unboxed_value;
Elliott Hughesdbac3092012-03-16 18:00:30 -0700211 if (!UnboxPrimitive(boxed_value, FieldHelper(f).GetType(), unboxed_value, "field")) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800212 return;
213 }
214
215 // Check that the receiver is non-null and an instance of the field's declaring class.
216 Object* o = NULL;
217 if (!CheckReceiver(env, javaObj, f, o)) {
218 return;
219 }
220
221 SetFieldValue(o, f, unboxed_value, true);
222}
223
Elliott Hughes0512f022012-03-15 22:10:52 -0700224static void SetPrimitiveField(JNIEnv* env, jobject javaField, jobject javaObj, char src_descriptor,
225 const JValue& new_value) {
Brian Carlstromb82b6872011-10-26 17:18:07 -0700226 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700227 Field* f = DecodeField(env->FromReflectedField(javaField));
Elliott Hughesed1c1e32011-10-02 14:31:05 -0700228 Object* o = NULL;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800229 if (!CheckReceiver(env, javaObj, f, o)) {
Elliott Hughesed1c1e32011-10-02 14:31:05 -0700230 return;
Elliott Hughes33203b52011-09-20 19:42:01 -0700231 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800232 FieldHelper fh(f);
233 if (!fh.IsPrimitiveType()) {
Jesse Wilsonc129a6b2011-11-24 14:47:46 -0500234 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
235 "Not a primitive field: %s", PrettyField(f).c_str());
236 return;
237 }
Elliott Hughes33203b52011-09-20 19:42:01 -0700238
239 // Widen the value if necessary (and possible).
240 JValue wide_value;
Elliott Hughes582a7d12011-10-10 18:38:42 -0700241 Class* src_type = Runtime::Current()->GetClassLinker()->FindPrimitiveClass(src_descriptor);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800242 if (!ConvertPrimitiveValue(src_type->GetPrimitiveType(), fh.GetTypeAsPrimitiveType(),
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700243 new_value, wide_value)) {
Elliott Hughes33203b52011-09-20 19:42:01 -0700244 return;
245 }
246
247 // Write the value.
248 SetFieldValue(o, f, wide_value, false);
249}
250
Elliott Hughes0512f022012-03-15 22:10:52 -0700251static void Field_setBoolean(JNIEnv* env, jobject javaField, jobject javaObj, jboolean value) {
Elliott Hughes33203b52011-09-20 19:42:01 -0700252 JValue v = { 0 };
253 v.z = value;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800254 SetPrimitiveField(env, javaField, javaObj, 'Z', v);
Elliott Hughes33203b52011-09-20 19:42:01 -0700255}
256
Elliott Hughes0512f022012-03-15 22:10:52 -0700257static void Field_setByte(JNIEnv* env, jobject javaField, jobject javaObj, jbyte value) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800258 JValue v = { 0 };
259 v.b = value;
260 SetPrimitiveField(env, javaField, javaObj, 'B', v);
Elliott Hughes33203b52011-09-20 19:42:01 -0700261}
262
Elliott Hughes0512f022012-03-15 22:10:52 -0700263static void Field_setChar(JNIEnv* env, jobject javaField, jobject javaObj, jchar value) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800264 JValue v = { 0 };
265 v.c = value;
266 SetPrimitiveField(env, javaField, javaObj, 'C', v);
267}
Elliott Hughes33203b52011-09-20 19:42:01 -0700268
Elliott Hughes0512f022012-03-15 22:10:52 -0700269static void Field_setDouble(JNIEnv* env, jobject javaField, jobject javaObj, jdouble value) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800270 JValue v = { 0 };
271 v.d = value;
272 SetPrimitiveField(env, javaField, javaObj, 'D', v);
273}
Elliott Hughes33203b52011-09-20 19:42:01 -0700274
Elliott Hughes0512f022012-03-15 22:10:52 -0700275static void Field_setFloat(JNIEnv* env, jobject javaField, jobject javaObj, jfloat value) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800276 JValue v = { 0 };
277 v.f = value;
278 SetPrimitiveField(env, javaField, javaObj, 'F', v);
279}
280
Elliott Hughes0512f022012-03-15 22:10:52 -0700281static void Field_setInt(JNIEnv* env, jobject javaField, jobject javaObj, jint value) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800282 JValue v = { 0 };
283 v.i = value;
284 SetPrimitiveField(env, javaField, javaObj, 'I', v);
285}
286
Elliott Hughes0512f022012-03-15 22:10:52 -0700287static void Field_setLong(JNIEnv* env, jobject javaField, jobject javaObj, jlong value) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800288 JValue v = { 0 };
289 v.j = value;
290 SetPrimitiveField(env, javaField, javaObj, 'J', v);
291}
292
Elliott Hughes0512f022012-03-15 22:10:52 -0700293static void Field_setShort(JNIEnv* env, jobject javaField, jobject javaObj, jshort value) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800294 JValue v = { 0 };
295 v.s = value;
296 SetPrimitiveField(env, javaField, javaObj, 'S', v);
Elliott Hughes33203b52011-09-20 19:42:01 -0700297}
298
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700299static JNINativeMethod gMethods[] = {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800300 NATIVE_METHOD(Field, get, "(Ljava/lang/Object;)Ljava/lang/Object;"),
301 NATIVE_METHOD(Field, getBoolean, "(Ljava/lang/Object;)Z"),
302 NATIVE_METHOD(Field, getByte, "(Ljava/lang/Object;)B"),
303 NATIVE_METHOD(Field, getChar, "(Ljava/lang/Object;)C"),
304 NATIVE_METHOD(Field, getDouble, "(Ljava/lang/Object;)D"),
305 NATIVE_METHOD(Field, getFloat, "(Ljava/lang/Object;)F"),
306 NATIVE_METHOD(Field, getInt, "(Ljava/lang/Object;)I"),
307 NATIVE_METHOD(Field, getLong, "(Ljava/lang/Object;)J"),
308 NATIVE_METHOD(Field, getShort, "(Ljava/lang/Object;)S"),
309 NATIVE_METHOD(Field, set, "(Ljava/lang/Object;Ljava/lang/Object;)V"),
310 NATIVE_METHOD(Field, setBoolean, "(Ljava/lang/Object;Z)V"),
311 NATIVE_METHOD(Field, setByte, "(Ljava/lang/Object;B)V"),
312 NATIVE_METHOD(Field, setChar, "(Ljava/lang/Object;C)V"),
313 NATIVE_METHOD(Field, setDouble, "(Ljava/lang/Object;D)V"),
314 NATIVE_METHOD(Field, setFloat, "(Ljava/lang/Object;F)V"),
315 NATIVE_METHOD(Field, setInt, "(Ljava/lang/Object;I)V"),
316 NATIVE_METHOD(Field, setLong, "(Ljava/lang/Object;J)V"),
317 NATIVE_METHOD(Field, setShort, "(Ljava/lang/Object;S)V"),
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700318};
319
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700320void register_java_lang_reflect_Field(JNIEnv* env) {
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700321 jniRegisterNativeMethods(env, "java/lang/reflect/Field", gMethods, NELEM(gMethods));
322}
323
324} // namespace art