blob: 7d43b2ea94aa0b7080e86eda106e523a9f33ff5b [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"
Elliott Hughes418d20f2011-09-22 14:00:39 -070020#include "reflection.h"
Brian Carlstromf867b6f2011-09-16 12:17:25 -070021
22#include "JniConstants.h" // Last to avoid problems with LOG redefinition.
23
24namespace art {
25
26namespace {
27
28jint Field_getFieldModifiers(JNIEnv* env, jobject jfield, jclass javaDeclaringClass, jint slot) {
Elliott Hughes582a7d12011-10-10 18:38:42 -070029 return Decode<Object*>(env, jfield)->AsField()->GetAccessFlags() & kAccJavaFlagsMask;
Brian Carlstromf867b6f2011-09-16 12:17:25 -070030}
31
Elliott Hughes33203b52011-09-20 19:42:01 -070032bool GetFieldValue(Object* o, Field* f, JValue& value, bool allow_references) {
33 switch (f->GetType()->GetPrimitiveType()) {
34 case Class::kPrimBoolean:
35 value.z = f->GetBoolean(o);
36 return true;
37 case Class::kPrimByte:
38 value.b = f->GetByte(o);
39 return true;
40 case Class::kPrimChar:
41 value.c = f->GetChar(o);
42 return true;
43 case Class::kPrimDouble:
44 value.d = f->GetDouble(o);
45 return true;
46 case Class::kPrimFloat:
47 value.f = f->GetFloat(o);
48 return true;
49 case Class::kPrimInt:
50 value.i = f->GetInt(o);
51 return true;
52 case Class::kPrimLong:
53 value.j = f->GetLong(o);
54 return true;
55 case Class::kPrimShort:
56 value.s = f->GetShort(o);
57 return true;
58 case Class::kPrimNot:
59 if (allow_references) {
60 value.l = f->GetObject(o);
61 return true;
62 }
63 // Else break to report an error.
64 break;
65 case Class::kPrimVoid:
66 // Never okay.
67 break;
68 }
Elliott Hughes5cb5ad22011-10-02 12:13:39 -070069 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Elliott Hughes33203b52011-09-20 19:42:01 -070070 "Not a primitive field: %s", PrettyField(f).c_str());
71 return false;
72}
73
Elliott Hughesed1c1e32011-10-02 14:31:05 -070074bool CheckReceiver(JNIEnv* env, jobject javaObj, jclass javaDeclaringClass, Field* f, Object*& o) {
75 if (f->IsStatic()) {
76 o = NULL;
77 return true;
78 }
79
80 o = Decode<Object*>(env, javaObj);
81 Class* declaringClass = Decode<Class*>(env, javaDeclaringClass);
82 if (!VerifyObjectInClass(env, o, declaringClass)) {
83 return false;
84 }
85 return true;
86}
87
Elliott Hughes582a7d12011-10-10 18:38:42 -070088JValue GetPrimitiveField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jchar dst_descriptor) {
Elliott Hughes418d20f2011-09-22 14:00:39 -070089 Field* f = DecodeField(env->FromReflectedField(javaField));
Elliott Hughesed1c1e32011-10-02 14:31:05 -070090 Object* o = NULL;
91 if (!CheckReceiver(env, javaObj, javaDeclaringClass, f, o)) {
92 return JValue();
Elliott Hughes33203b52011-09-20 19:42:01 -070093 }
94
95 // Read the value.
96 JValue field_value;
97 if (!GetFieldValue(o, f, field_value, false)) {
98 return JValue();
99 }
100
101 // Widen it if necessary (and possible).
102 JValue wide_value;
Elliott Hughes582a7d12011-10-10 18:38:42 -0700103 Class* dst_type = Runtime::Current()->GetClassLinker()->FindPrimitiveClass(dst_descriptor);
104 if (!ConvertPrimitiveValue(f->GetType(), dst_type, field_value, wide_value)) {
Elliott Hughes33203b52011-09-20 19:42:01 -0700105 return JValue();
106 }
107 return wide_value;
108}
109
Elliott Hughes582a7d12011-10-10 18:38:42 -0700110jbyte Field_getBField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar dst_descriptor) {
111 return GetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, dst_descriptor).b;
Elliott Hughes33203b52011-09-20 19:42:01 -0700112}
113
Elliott Hughes582a7d12011-10-10 18:38:42 -0700114jchar Field_getCField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar dst_descriptor) {
115 return GetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, dst_descriptor).c;
Elliott Hughes33203b52011-09-20 19:42:01 -0700116}
117
Elliott Hughes582a7d12011-10-10 18:38:42 -0700118jdouble Field_getDField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar dst_descriptor) {
119 return GetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, dst_descriptor).d;
Elliott Hughes33203b52011-09-20 19:42:01 -0700120}
121
Elliott Hughes582a7d12011-10-10 18:38:42 -0700122jfloat Field_getFField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar dst_descriptor) {
123 return GetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, dst_descriptor).f;
Elliott Hughes33203b52011-09-20 19:42:01 -0700124}
125
Elliott Hughes582a7d12011-10-10 18:38:42 -0700126jint Field_getIField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar dst_descriptor) {
127 return GetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, dst_descriptor).i;
Elliott Hughes33203b52011-09-20 19:42:01 -0700128}
129
Elliott Hughes582a7d12011-10-10 18:38:42 -0700130jlong Field_getJField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar dst_descriptor) {
131 return GetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, dst_descriptor).j;
Elliott Hughes33203b52011-09-20 19:42:01 -0700132}
133
Elliott Hughes582a7d12011-10-10 18:38:42 -0700134jshort Field_getSField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar dst_descriptor) {
135 return GetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, dst_descriptor).s;
Elliott Hughes33203b52011-09-20 19:42:01 -0700136}
137
Elliott Hughes582a7d12011-10-10 18:38:42 -0700138jboolean Field_getZField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar dst_descriptor) {
139 return GetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, dst_descriptor).z;
Elliott Hughes33203b52011-09-20 19:42:01 -0700140}
141
142void SetFieldValue(Object* o, Field* f, const JValue& new_value, bool allow_references) {
143 switch (f->GetType()->GetPrimitiveType()) {
144 case Class::kPrimBoolean:
145 f->SetBoolean(o, new_value.z);
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700146 break;
Elliott Hughes33203b52011-09-20 19:42:01 -0700147 case Class::kPrimByte:
148 f->SetByte(o, new_value.b);
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700149 break;
Elliott Hughes33203b52011-09-20 19:42:01 -0700150 case Class::kPrimChar:
151 f->SetChar(o, new_value.c);
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700152 break;
Elliott Hughes33203b52011-09-20 19:42:01 -0700153 case Class::kPrimDouble:
154 f->SetDouble(o, new_value.d);
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700155 break;
Elliott Hughes33203b52011-09-20 19:42:01 -0700156 case Class::kPrimFloat:
157 f->SetFloat(o, new_value.f);
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700158 break;
Elliott Hughes33203b52011-09-20 19:42:01 -0700159 case Class::kPrimInt:
160 f->SetInt(o, new_value.i);
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700161 break;
Elliott Hughes33203b52011-09-20 19:42:01 -0700162 case Class::kPrimLong:
163 f->SetLong(o, new_value.j);
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700164 break;
Elliott Hughes33203b52011-09-20 19:42:01 -0700165 case Class::kPrimShort:
166 f->SetShort(o, new_value.s);
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700167 break;
Elliott Hughes33203b52011-09-20 19:42:01 -0700168 case Class::kPrimNot:
169 if (allow_references) {
170 f->SetObject(o, new_value.l);
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700171 break;
Elliott Hughes33203b52011-09-20 19:42:01 -0700172 }
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700173 // Else fall through to report an error.
Elliott Hughes33203b52011-09-20 19:42:01 -0700174 case Class::kPrimVoid:
175 // Never okay.
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700176 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700177 "Not a primitive field: %s", PrettyField(f).c_str());
178 return;
Elliott Hughes33203b52011-09-20 19:42:01 -0700179 }
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700180
181 // Special handling for final fields on SMP systems.
182 // We need a store/store barrier here (JMM requirement).
183 if (f->IsFinal()) {
184 ANDROID_MEMBAR_STORE();
185 }
Elliott Hughes33203b52011-09-20 19:42:01 -0700186}
187
Elliott Hughes582a7d12011-10-10 18:38:42 -0700188void SetPrimitiveField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jchar src_descriptor, const JValue& new_value) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700189 Field* f = DecodeField(env->FromReflectedField(javaField));
Elliott Hughesed1c1e32011-10-02 14:31:05 -0700190 Object* o = NULL;
191 if (!CheckReceiver(env, javaObj, javaDeclaringClass, f, o)) {
192 return;
Elliott Hughes33203b52011-09-20 19:42:01 -0700193 }
194
195 // Widen the value if necessary (and possible).
196 JValue wide_value;
Elliott Hughes582a7d12011-10-10 18:38:42 -0700197 Class* src_type = Runtime::Current()->GetClassLinker()->FindPrimitiveClass(src_descriptor);
198 if (!ConvertPrimitiveValue(src_type, f->GetType(), new_value, wide_value)) {
Elliott Hughes33203b52011-09-20 19:42:01 -0700199 return;
200 }
201
202 // Write the value.
203 SetFieldValue(o, f, wide_value, false);
204}
205
Elliott Hughes582a7d12011-10-10 18:38:42 -0700206void Field_setBField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar src_descriptor, jbyte value) {
Elliott Hughes33203b52011-09-20 19:42:01 -0700207 JValue v = { 0 };
208 v.b = value;
Elliott Hughes582a7d12011-10-10 18:38:42 -0700209 SetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, src_descriptor, v);
Elliott Hughes33203b52011-09-20 19:42:01 -0700210}
211
Elliott Hughes582a7d12011-10-10 18:38:42 -0700212void Field_setCField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar src_descriptor, jchar value) {
Elliott Hughes33203b52011-09-20 19:42:01 -0700213 JValue v = { 0 };
214 v.c = value;
Elliott Hughes582a7d12011-10-10 18:38:42 -0700215 SetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, src_descriptor, v);
Elliott Hughes33203b52011-09-20 19:42:01 -0700216}
217
Elliott Hughes582a7d12011-10-10 18:38:42 -0700218void Field_setDField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar src_descriptor, jdouble value) {
Elliott Hughes33203b52011-09-20 19:42:01 -0700219 JValue v = { 0 };
220 v.d = value;
Elliott Hughes582a7d12011-10-10 18:38:42 -0700221 SetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, src_descriptor, v);
Elliott Hughes33203b52011-09-20 19:42:01 -0700222}
223
Elliott Hughes582a7d12011-10-10 18:38:42 -0700224void Field_setFField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar src_descriptor, jfloat value) {
Elliott Hughes33203b52011-09-20 19:42:01 -0700225 JValue v = { 0 };
226 v.f = value;
Elliott Hughes582a7d12011-10-10 18:38:42 -0700227 SetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, src_descriptor, v);
Elliott Hughes33203b52011-09-20 19:42:01 -0700228}
229
Elliott Hughes582a7d12011-10-10 18:38:42 -0700230void Field_setIField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar src_descriptor, jint value) {
Elliott Hughes33203b52011-09-20 19:42:01 -0700231 JValue v = { 0 };
232 v.i = value;
Elliott Hughes582a7d12011-10-10 18:38:42 -0700233 SetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, src_descriptor, v);
Elliott Hughes33203b52011-09-20 19:42:01 -0700234}
235
Elliott Hughes582a7d12011-10-10 18:38:42 -0700236void Field_setJField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar src_descriptor, jlong value) {
Elliott Hughes33203b52011-09-20 19:42:01 -0700237 JValue v = { 0 };
238 v.j = value;
Elliott Hughes582a7d12011-10-10 18:38:42 -0700239 SetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, src_descriptor, v);
Elliott Hughes33203b52011-09-20 19:42:01 -0700240}
241
Elliott Hughes582a7d12011-10-10 18:38:42 -0700242void Field_setSField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar src_descriptor, jshort value) {
Elliott Hughes33203b52011-09-20 19:42:01 -0700243 JValue v = { 0 };
244 v.s = value;
Elliott Hughes582a7d12011-10-10 18:38:42 -0700245 SetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, src_descriptor, v);
Elliott Hughes33203b52011-09-20 19:42:01 -0700246}
247
Elliott Hughes582a7d12011-10-10 18:38:42 -0700248void Field_setZField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar src_descriptor, jboolean value) {
Elliott Hughes33203b52011-09-20 19:42:01 -0700249 JValue v = { 0 };
250 v.z = value;
Elliott Hughes582a7d12011-10-10 18:38:42 -0700251 SetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, src_descriptor, v);
Elliott Hughes33203b52011-09-20 19:42:01 -0700252}
253
254void Field_setField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jobject javaValue) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700255 Field* f = DecodeField(env->FromReflectedField(javaField));
Elliott Hughes33203b52011-09-20 19:42:01 -0700256
257 // Unbox the value, if necessary.
258 Object* boxed_value = Decode<Object*>(env, javaValue);
259 JValue unboxed_value;
260 if (!UnboxPrimitive(env, boxed_value, f->GetType(), unboxed_value)) {
261 return;
262 }
263
264 // Check that the receiver is non-null and an instance of the field's declaring class.
Elliott Hughesed1c1e32011-10-02 14:31:05 -0700265 Object* o = NULL;
266 if (!CheckReceiver(env, javaObj, javaDeclaringClass, f, o)) {
267 return;
Elliott Hughes33203b52011-09-20 19:42:01 -0700268 }
269
270 SetFieldValue(o, f, unboxed_value, true);
271}
272
273jobject Field_getField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700274 Field* f = DecodeField(env->FromReflectedField(javaField));
Elliott Hughesed1c1e32011-10-02 14:31:05 -0700275 Object* o = NULL;
276 if (!CheckReceiver(env, javaObj, javaDeclaringClass, f, o)) {
277 return NULL;
Elliott Hughes33203b52011-09-20 19:42:01 -0700278 }
279
280 // Get the field's value, boxing if necessary.
281 JValue value;
282 if (!GetFieldValue(o, f, value, true)) {
283 return NULL;
284 }
285 BoxPrimitive(env, f->GetType(), value);
286
287 return AddLocalReference<jobject>(env, value.l);
288}
289
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700290static JNINativeMethod gMethods[] = {
Elliott Hughes33203b52011-09-20 19:42:01 -0700291 NATIVE_METHOD(Field, getFieldModifiers, "(Ljava/lang/Class;I)I"),
292
293 NATIVE_METHOD(Field, getBField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZC)B"),
294 NATIVE_METHOD(Field, getCField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZC)C"),
295 NATIVE_METHOD(Field, getDField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZC)D"),
296 NATIVE_METHOD(Field, getFField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZC)F"),
297 NATIVE_METHOD(Field, getField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZ)Ljava/lang/Object;"),
298 NATIVE_METHOD(Field, getIField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZC)I"),
299 NATIVE_METHOD(Field, getJField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZC)J"),
300 NATIVE_METHOD(Field, getSField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZC)S"),
301 NATIVE_METHOD(Field, getZField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZC)Z"),
302 NATIVE_METHOD(Field, setBField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZCB)V"),
303 NATIVE_METHOD(Field, setCField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZCC)V"),
304 NATIVE_METHOD(Field, setDField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZCD)V"),
305 NATIVE_METHOD(Field, setFField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZCF)V"),
306 NATIVE_METHOD(Field, setField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZLjava/lang/Object;)V"),
307 NATIVE_METHOD(Field, setIField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZCI)V"),
308 NATIVE_METHOD(Field, setJField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZCJ)V"),
309 NATIVE_METHOD(Field, setSField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZCS)V"),
310 NATIVE_METHOD(Field, setZField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZCZ)V"),
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700311};
312
313} // namespace
314
315void register_java_lang_reflect_Field(JNIEnv* env) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700316 InitBoxingMethods(env); // TODO: move to Runtime?
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700317 jniRegisterNativeMethods(env, "java/lang/reflect/Field", gMethods, NELEM(gMethods));
318}
319
320} // namespace art