blob: 5584da32d520fb8c693872e3a2e3db227936744b [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);
138 return;
139 case Class::kPrimByte:
140 f->SetByte(o, new_value.b);
141 return;
142 case Class::kPrimChar:
143 f->SetChar(o, new_value.c);
144 return;
145 case Class::kPrimDouble:
146 f->SetDouble(o, new_value.d);
147 return;
148 case Class::kPrimFloat:
149 f->SetFloat(o, new_value.f);
150 return;
151 case Class::kPrimInt:
152 f->SetInt(o, new_value.i);
153 return;
154 case Class::kPrimLong:
155 f->SetLong(o, new_value.j);
156 return;
157 case Class::kPrimShort:
158 f->SetShort(o, new_value.s);
159 return;
160 case Class::kPrimNot:
161 if (allow_references) {
162 f->SetObject(o, new_value.l);
163 return;
164 }
165 // Else break to report an error.
166 break;
167 case Class::kPrimVoid:
168 // Never okay.
169 break;
170 }
171 Thread::Current()->ThrowNewException("Ljava/lang/IllegalArgumentException;",
172 "Not a primitive field: %s", PrettyField(f).c_str());
173}
174
175void SetPrimitiveField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jchar targetDescriptor, const JValue& new_value) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700176 Field* f = DecodeField(env->FromReflectedField(javaField));
Elliott Hughes33203b52011-09-20 19:42:01 -0700177
178 // Check that the receiver is non-null and an instance of the field's declaring class.
179 Object* o = Decode<Object*>(env, javaObj);
180 bool isStatic = (javaObj == NULL);
181 if (!isStatic) {
182 Class* declaringClass = Decode<Class*>(env, javaDeclaringClass);
183 if (!VerifyObjectInClass(env, o, declaringClass)) {
184 return;
185 }
186 }
187
188 // Widen the value if necessary (and possible).
189 JValue wide_value;
190 Class* targetType = Runtime::Current()->GetClassLinker()->FindPrimitiveClass(targetDescriptor);
191 if (!ConvertPrimitiveValue(f->GetType(), targetType, new_value, wide_value)) {
192 return;
193 }
194
195 // Write the value.
196 SetFieldValue(o, f, wide_value, false);
197}
198
199void Field_setBField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar targetDescriptor, jbyte value) {
200 JValue v = { 0 };
201 v.b = value;
202 SetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, targetDescriptor, v);
203}
204
205void Field_setCField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar targetDescriptor, jchar value) {
206 JValue v = { 0 };
207 v.c = value;
208 SetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, targetDescriptor, v);
209}
210
211void Field_setDField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar targetDescriptor, jdouble value) {
212 JValue v = { 0 };
213 v.d = value;
214 SetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, targetDescriptor, v);
215}
216
217void Field_setFField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar targetDescriptor, jfloat value) {
218 JValue v = { 0 };
219 v.f = value;
220 SetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, targetDescriptor, v);
221}
222
223void Field_setIField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar targetDescriptor, jint value) {
224 JValue v = { 0 };
225 v.i = value;
226 SetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, targetDescriptor, v);
227}
228
229void Field_setJField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar targetDescriptor, jlong value) {
230 JValue v = { 0 };
231 v.j = value;
232 SetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, targetDescriptor, v);
233}
234
235void Field_setSField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar targetDescriptor, jshort value) {
236 JValue v = { 0 };
237 v.s = value;
238 SetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, targetDescriptor, v);
239}
240
241void Field_setZField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar targetDescriptor, jboolean value) {
242 JValue v = { 0 };
243 v.z = value;
244 SetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, targetDescriptor, v);
245}
246
247void Field_setField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jobject javaValue) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700248 Field* f = DecodeField(env->FromReflectedField(javaField));
Elliott Hughes33203b52011-09-20 19:42:01 -0700249
250 // Unbox the value, if necessary.
251 Object* boxed_value = Decode<Object*>(env, javaValue);
252 JValue unboxed_value;
253 if (!UnboxPrimitive(env, boxed_value, f->GetType(), unboxed_value)) {
254 return;
255 }
256
257 // Check that the receiver is non-null and an instance of the field's declaring class.
258 Object* o = Decode<Object*>(env, javaObj);
259 bool isStatic = (javaObj == NULL);
260 if (!isStatic) {
261 Class* declaringClass = Decode<Class*>(env, javaDeclaringClass);
262 if (!VerifyObjectInClass(env, o, declaringClass)) {
263 return;
264 }
265 }
266
267 SetFieldValue(o, f, unboxed_value, true);
268}
269
270jobject Field_getField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700271 Field* f = DecodeField(env->FromReflectedField(javaField));
Elliott Hughes33203b52011-09-20 19:42:01 -0700272
273 // Check that the receiver is non-null and an instance of the field's declaring class.
274 Object* o = Decode<Object*>(env, javaObj);
275 bool isStatic = (javaObj == NULL);
276 if (!isStatic) {
277 Class* declaringClass = Decode<Class*>(env, javaDeclaringClass);
278 if (!VerifyObjectInClass(env, o, declaringClass)) {
279 return NULL;
280 }
281 }
282
283 // Get the field's value, boxing if necessary.
284 JValue value;
285 if (!GetFieldValue(o, f, value, true)) {
286 return NULL;
287 }
288 BoxPrimitive(env, f->GetType(), value);
289
290 return AddLocalReference<jobject>(env, value.l);
291}
292
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700293static JNINativeMethod gMethods[] = {
Elliott Hughes33203b52011-09-20 19:42:01 -0700294 NATIVE_METHOD(Field, getFieldModifiers, "(Ljava/lang/Class;I)I"),
295
296 NATIVE_METHOD(Field, getBField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZC)B"),
297 NATIVE_METHOD(Field, getCField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZC)C"),
298 NATIVE_METHOD(Field, getDField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZC)D"),
299 NATIVE_METHOD(Field, getFField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZC)F"),
300 NATIVE_METHOD(Field, getField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZ)Ljava/lang/Object;"),
301 NATIVE_METHOD(Field, getIField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZC)I"),
302 NATIVE_METHOD(Field, getJField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZC)J"),
303 NATIVE_METHOD(Field, getSField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZC)S"),
304 NATIVE_METHOD(Field, getZField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZC)Z"),
305 NATIVE_METHOD(Field, setBField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZCB)V"),
306 NATIVE_METHOD(Field, setCField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZCC)V"),
307 NATIVE_METHOD(Field, setDField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZCD)V"),
308 NATIVE_METHOD(Field, setFField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZCF)V"),
309 NATIVE_METHOD(Field, setField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZLjava/lang/Object;)V"),
310 NATIVE_METHOD(Field, setIField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZCI)V"),
311 NATIVE_METHOD(Field, setJField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZCJ)V"),
312 NATIVE_METHOD(Field, setSField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZCS)V"),
313 NATIVE_METHOD(Field, setZField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZCZ)V"),
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700314};
315
316} // namespace
317
318void register_java_lang_reflect_Field(JNIEnv* env) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700319 InitBoxingMethods(env); // TODO: move to Runtime?
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700320 jniRegisterNativeMethods(env, "java/lang/reflect/Field", gMethods, NELEM(gMethods));
321}
322
323} // namespace art