blob: 185fabc44eaf76b1e6ad3b711f7a9f6ce4fe7537 [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) {
29 return Decode<Object*>(env, jfield)->AsField()->GetAccessFlags() & kAccFieldFlagsMask;
30}
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 }
69 Thread::Current()->ThrowNewException("Ljava/lang/IllegalArgumentException;",
70 "Not a primitive field: %s", PrettyField(f).c_str());
71 return false;
72}
73
74JValue GetPrimitiveField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jchar targetDescriptor) {
Elliott Hughes418d20f2011-09-22 14:00:39 -070075 Field* f = DecodeField(env->FromReflectedField(javaField));
Elliott Hughes33203b52011-09-20 19:42:01 -070076
77 // Check that the receiver is non-null and an instance of the field's declaring class.
78 Object* o = Decode<Object*>(env, javaObj);
79 bool isStatic = (javaObj == NULL);
80 if (!isStatic) {
81 Class* declaringClass = Decode<Class*>(env, javaDeclaringClass);
82 if (!VerifyObjectInClass(env, o, declaringClass)) {
83 return JValue();
84 }
85 }
86
87 // Read the value.
88 JValue field_value;
89 if (!GetFieldValue(o, f, field_value, false)) {
90 return JValue();
91 }
92
93 // Widen it if necessary (and possible).
94 JValue wide_value;
95 Class* targetType = Runtime::Current()->GetClassLinker()->FindPrimitiveClass(targetDescriptor);
96 if (!ConvertPrimitiveValue(f->GetType(), targetType, field_value, wide_value)) {
97 return JValue();
98 }
99 return wide_value;
100}
101
102jbyte Field_getBField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar targetDescriptor) {
103 return GetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, targetDescriptor).b;
104}
105
106jchar Field_getCField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar targetDescriptor) {
107 return GetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, targetDescriptor).c;
108}
109
110jdouble Field_getDField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar targetDescriptor) {
111 return GetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, targetDescriptor).d;
112}
113
114jfloat Field_getFField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar targetDescriptor) {
115 return GetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, targetDescriptor).f;
116}
117
118jint Field_getIField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar targetDescriptor) {
119 return GetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, targetDescriptor).i;
120}
121
122jlong Field_getJField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar targetDescriptor) {
123 return GetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, targetDescriptor).j;
124}
125
126jshort Field_getSField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar targetDescriptor) {
127 return GetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, targetDescriptor).s;
128}
129
130jboolean Field_getZField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar targetDescriptor) {
131 return GetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, targetDescriptor).z;
132}
133
134void SetFieldValue(Object* o, Field* f, const JValue& new_value, bool allow_references) {
135 switch (f->GetType()->GetPrimitiveType()) {
136 case Class::kPrimBoolean:
137 f->SetBoolean(o, new_value.z);
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700138 break;
Elliott Hughes33203b52011-09-20 19:42:01 -0700139 case Class::kPrimByte:
140 f->SetByte(o, new_value.b);
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700141 break;
Elliott Hughes33203b52011-09-20 19:42:01 -0700142 case Class::kPrimChar:
143 f->SetChar(o, new_value.c);
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700144 break;
Elliott Hughes33203b52011-09-20 19:42:01 -0700145 case Class::kPrimDouble:
146 f->SetDouble(o, new_value.d);
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700147 break;
Elliott Hughes33203b52011-09-20 19:42:01 -0700148 case Class::kPrimFloat:
149 f->SetFloat(o, new_value.f);
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700150 break;
Elliott Hughes33203b52011-09-20 19:42:01 -0700151 case Class::kPrimInt:
152 f->SetInt(o, new_value.i);
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700153 break;
Elliott Hughes33203b52011-09-20 19:42:01 -0700154 case Class::kPrimLong:
155 f->SetLong(o, new_value.j);
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700156 break;
Elliott Hughes33203b52011-09-20 19:42:01 -0700157 case Class::kPrimShort:
158 f->SetShort(o, new_value.s);
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700159 break;
Elliott Hughes33203b52011-09-20 19:42:01 -0700160 case Class::kPrimNot:
161 if (allow_references) {
162 f->SetObject(o, new_value.l);
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700163 break;
Elliott Hughes33203b52011-09-20 19:42:01 -0700164 }
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700165 // Else fall through to report an error.
Elliott Hughes33203b52011-09-20 19:42:01 -0700166 case Class::kPrimVoid:
167 // Never okay.
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700168 Thread::Current()->ThrowNewException("Ljava/lang/IllegalArgumentException;",
169 "Not a primitive field: %s", PrettyField(f).c_str());
170 return;
Elliott Hughes33203b52011-09-20 19:42:01 -0700171 }
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700172
173 // Special handling for final fields on SMP systems.
174 // We need a store/store barrier here (JMM requirement).
175 if (f->IsFinal()) {
176 ANDROID_MEMBAR_STORE();
177 }
Elliott Hughes33203b52011-09-20 19:42:01 -0700178}
179
180void SetPrimitiveField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jchar targetDescriptor, const JValue& new_value) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700181 Field* f = DecodeField(env->FromReflectedField(javaField));
Elliott Hughes33203b52011-09-20 19:42:01 -0700182
183 // Check that the receiver is non-null and an instance of the field's declaring class.
184 Object* o = Decode<Object*>(env, javaObj);
185 bool isStatic = (javaObj == NULL);
186 if (!isStatic) {
187 Class* declaringClass = Decode<Class*>(env, javaDeclaringClass);
188 if (!VerifyObjectInClass(env, o, declaringClass)) {
189 return;
190 }
191 }
192
193 // Widen the value if necessary (and possible).
194 JValue wide_value;
195 Class* targetType = Runtime::Current()->GetClassLinker()->FindPrimitiveClass(targetDescriptor);
196 if (!ConvertPrimitiveValue(f->GetType(), targetType, new_value, wide_value)) {
197 return;
198 }
199
200 // Write the value.
201 SetFieldValue(o, f, wide_value, false);
202}
203
204void Field_setBField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar targetDescriptor, jbyte value) {
205 JValue v = { 0 };
206 v.b = value;
207 SetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, targetDescriptor, v);
208}
209
210void Field_setCField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar targetDescriptor, jchar value) {
211 JValue v = { 0 };
212 v.c = value;
213 SetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, targetDescriptor, v);
214}
215
216void Field_setDField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar targetDescriptor, jdouble value) {
217 JValue v = { 0 };
218 v.d = value;
219 SetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, targetDescriptor, v);
220}
221
222void Field_setFField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar targetDescriptor, jfloat value) {
223 JValue v = { 0 };
224 v.f = value;
225 SetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, targetDescriptor, v);
226}
227
228void Field_setIField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar targetDescriptor, jint value) {
229 JValue v = { 0 };
230 v.i = value;
231 SetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, targetDescriptor, v);
232}
233
234void Field_setJField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar targetDescriptor, jlong value) {
235 JValue v = { 0 };
236 v.j = value;
237 SetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, targetDescriptor, v);
238}
239
240void Field_setSField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar targetDescriptor, jshort value) {
241 JValue v = { 0 };
242 v.s = value;
243 SetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, targetDescriptor, v);
244}
245
246void Field_setZField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar targetDescriptor, jboolean value) {
247 JValue v = { 0 };
248 v.z = value;
249 SetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, targetDescriptor, v);
250}
251
252void Field_setField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jobject javaValue) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700253 Field* f = DecodeField(env->FromReflectedField(javaField));
Elliott Hughes33203b52011-09-20 19:42:01 -0700254
255 // Unbox the value, if necessary.
256 Object* boxed_value = Decode<Object*>(env, javaValue);
257 JValue unboxed_value;
258 if (!UnboxPrimitive(env, boxed_value, f->GetType(), unboxed_value)) {
259 return;
260 }
261
262 // Check that the receiver is non-null and an instance of the field's declaring class.
263 Object* o = Decode<Object*>(env, javaObj);
264 bool isStatic = (javaObj == NULL);
265 if (!isStatic) {
266 Class* declaringClass = Decode<Class*>(env, javaDeclaringClass);
267 if (!VerifyObjectInClass(env, o, declaringClass)) {
268 return;
269 }
270 }
271
272 SetFieldValue(o, f, unboxed_value, true);
273}
274
275jobject Field_getField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700276 Field* f = DecodeField(env->FromReflectedField(javaField));
Elliott Hughes33203b52011-09-20 19:42:01 -0700277
278 // Check that the receiver is non-null and an instance of the field's declaring class.
279 Object* o = Decode<Object*>(env, javaObj);
280 bool isStatic = (javaObj == NULL);
281 if (!isStatic) {
282 Class* declaringClass = Decode<Class*>(env, javaDeclaringClass);
283 if (!VerifyObjectInClass(env, o, declaringClass)) {
284 return NULL;
285 }
286 }
287
288 // Get the field's value, boxing if necessary.
289 JValue value;
290 if (!GetFieldValue(o, f, value, true)) {
291 return NULL;
292 }
293 BoxPrimitive(env, f->GetType(), value);
294
295 return AddLocalReference<jobject>(env, value.l);
296}
297
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700298static JNINativeMethod gMethods[] = {
Elliott Hughes33203b52011-09-20 19:42:01 -0700299 NATIVE_METHOD(Field, getFieldModifiers, "(Ljava/lang/Class;I)I"),
300
301 NATIVE_METHOD(Field, getBField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZC)B"),
302 NATIVE_METHOD(Field, getCField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZC)C"),
303 NATIVE_METHOD(Field, getDField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZC)D"),
304 NATIVE_METHOD(Field, getFField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZC)F"),
305 NATIVE_METHOD(Field, getField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZ)Ljava/lang/Object;"),
306 NATIVE_METHOD(Field, getIField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZC)I"),
307 NATIVE_METHOD(Field, getJField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZC)J"),
308 NATIVE_METHOD(Field, getSField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZC)S"),
309 NATIVE_METHOD(Field, getZField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZC)Z"),
310 NATIVE_METHOD(Field, setBField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZCB)V"),
311 NATIVE_METHOD(Field, setCField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZCC)V"),
312 NATIVE_METHOD(Field, setDField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZCD)V"),
313 NATIVE_METHOD(Field, setFField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZCF)V"),
314 NATIVE_METHOD(Field, setField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZLjava/lang/Object;)V"),
315 NATIVE_METHOD(Field, setIField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZCI)V"),
316 NATIVE_METHOD(Field, setJField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZCJ)V"),
317 NATIVE_METHOD(Field, setSField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZCS)V"),
318 NATIVE_METHOD(Field, setZField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZCZ)V"),
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700319};
320
321} // namespace
322
323void register_java_lang_reflect_Field(JNIEnv* env) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700324 InitBoxingMethods(env); // TODO: move to Runtime?
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700325 jniRegisterNativeMethods(env, "java/lang/reflect/Field", gMethods, NELEM(gMethods));
326}
327
328} // namespace art