blob: 2e4dd8a59901c361ff1f2657ac3db58e23f345af [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
Andreas Gampe277ccbd2014-11-03 21:36:10 -080017#include "java_lang_reflect_Field.h"
18
Andreas Gampe46ee31b2016-12-14 10:11:49 -080019#include "android-base/stringprintf.h"
Andreas Gampea14100c2017-04-24 15:09:56 -070020#include "nativehelper/jni_macros.h"
Andreas Gampe46ee31b2016-12-14 10:11:49 -080021
Andreas Gampea1d2f952017-04-20 22:53:58 -070022#include "art_field-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080023#include "class_linker-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070024#include "class_linker.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080025#include "common_throws.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070026#include "dex_file-inl.h"
David Sehr9323e6e2016-09-13 08:58:35 -070027#include "dex_file_annotations.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070028#include "jni_internal.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070029#include "mirror/class-inl.h"
Andreas Gampe895f9222017-07-05 09:53:32 -070030#include "mirror/field-inl.h"
Andreas Gampe87583b32017-05-25 11:22:18 -070031#include "native_util.h"
Mathieu Chartier76433272014-09-26 14:32:37 -070032#include "reflection-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070033#include "scoped_fast_native_object_access-inl.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010034#include "utils.h"
Andreas Gampea1d2f952017-04-20 22:53:58 -070035#include "well_known_classes.h"
Brian Carlstromf867b6f2011-09-16 12:17:25 -070036
Brian Carlstromf867b6f2011-09-16 12:17:25 -070037namespace art {
38
Andreas Gampe46ee31b2016-12-14 10:11:49 -080039using android::base::StringPrintf;
40
Mathieu Chartier76433272014-09-26 14:32:37 -070041template<bool kIsSet>
Mathieu Chartier299958f2016-10-05 13:48:41 -070042ALWAYS_INLINE inline static bool VerifyFieldAccess(Thread* self,
43 ObjPtr<mirror::Field> field,
44 ObjPtr<mirror::Object> obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070045 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier76433272014-09-26 14:32:37 -070046 if (kIsSet && field->IsFinal()) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000047 ThrowIllegalAccessException(
Andreas Gampec0d82292014-09-23 10:38:30 -070048 StringPrintf("Cannot set %s field %s of class %s",
49 PrettyJavaAccessFlags(field->GetAccessFlags()).c_str(),
David Sehr709b0702016-10-13 09:12:37 -070050 ArtField::PrettyField(field->GetArtField()).c_str(),
Andreas Gampec0d82292014-09-23 10:38:30 -070051 field->GetDeclaringClass() == nullptr ? "null" :
David Sehr709b0702016-10-13 09:12:37 -070052 field->GetDeclaringClass()->PrettyClass().c_str()).c_str());
Jeff Hao11d5d8f2014-03-26 15:08:20 -070053 return false;
54 }
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070055 ObjPtr<mirror::Class> calling_class;
56 if (!VerifyAccess(self,
Mathieu Chartierf8ac97f2016-10-05 15:56:52 -070057 obj,
58 field->GetDeclaringClass(),
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070059 field->GetAccessFlags(),
60 &calling_class,
61 1)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000062 ThrowIllegalAccessException(
Andreas Gampec0d82292014-09-23 10:38:30 -070063 StringPrintf("Class %s cannot access %s field %s of class %s",
David Sehr709b0702016-10-13 09:12:37 -070064 calling_class == nullptr ? "null" : calling_class->PrettyClass().c_str(),
Andreas Gampec0d82292014-09-23 10:38:30 -070065 PrettyJavaAccessFlags(field->GetAccessFlags()).c_str(),
David Sehr709b0702016-10-13 09:12:37 -070066 ArtField::PrettyField(field->GetArtField()).c_str(),
Andreas Gampec0d82292014-09-23 10:38:30 -070067 field->GetDeclaringClass() == nullptr ? "null" :
David Sehr709b0702016-10-13 09:12:37 -070068 field->GetDeclaringClass()->PrettyClass().c_str()).c_str());
Jeff Hao11d5d8f2014-03-26 15:08:20 -070069 return false;
70 }
71 return true;
72}
73
Mathieu Chartier76433272014-09-26 14:32:37 -070074template<bool kAllowReferences>
Mathieu Chartier299958f2016-10-05 13:48:41 -070075ALWAYS_INLINE inline static bool GetFieldValue(ObjPtr<mirror::Object> o,
76 ObjPtr<mirror::Field> f,
77 Primitive::Type field_type,
78 JValue* value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070079 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers62f05122014-03-21 11:21:29 -070080 DCHECK_EQ(value->GetJ(), INT64_C(0));
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070081 MemberOffset offset(f->GetOffset());
82 const bool is_volatile = f->IsVolatile();
Ian Rogers62f05122014-03-21 11:21:29 -070083 switch (field_type) {
84 case Primitive::kPrimBoolean:
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070085 value->SetZ(is_volatile ? o->GetFieldBooleanVolatile(offset) : o->GetFieldBoolean(offset));
Elliott Hughes33203b52011-09-20 19:42:01 -070086 return true;
Ian Rogers62f05122014-03-21 11:21:29 -070087 case Primitive::kPrimByte:
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070088 value->SetB(is_volatile ? o->GetFieldByteVolatile(offset) : o->GetFieldByte(offset));
Ian Rogers62f05122014-03-21 11:21:29 -070089 return true;
90 case Primitive::kPrimChar:
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070091 value->SetC(is_volatile ? o->GetFieldCharVolatile(offset) : o->GetFieldChar(offset));
Ian Rogers62f05122014-03-21 11:21:29 -070092 return true;
93 case Primitive::kPrimInt:
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070094 case Primitive::kPrimFloat:
95 value->SetI(is_volatile ? o->GetField32Volatile(offset) : o->GetField32(offset));
Ian Rogers62f05122014-03-21 11:21:29 -070096 return true;
97 case Primitive::kPrimLong:
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070098 case Primitive::kPrimDouble:
99 value->SetJ(is_volatile ? o->GetField64Volatile(offset) : o->GetField64(offset));
Ian Rogers62f05122014-03-21 11:21:29 -0700100 return true;
101 case Primitive::kPrimShort:
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700102 value->SetS(is_volatile ? o->GetFieldShortVolatile(offset) : o->GetFieldShort(offset));
Ian Rogers62f05122014-03-21 11:21:29 -0700103 return true;
104 case Primitive::kPrimNot:
Mathieu Chartier76433272014-09-26 14:32:37 -0700105 if (kAllowReferences) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700106 value->SetL(is_volatile ? o->GetFieldObjectVolatile<mirror::Object>(offset) :
107 o->GetFieldObject<mirror::Object>(offset));
Ian Rogers62f05122014-03-21 11:21:29 -0700108 return true;
109 }
110 // Else break to report an error.
111 break;
112 case Primitive::kPrimVoid:
113 // Never okay.
114 break;
Elliott Hughes33203b52011-09-20 19:42:01 -0700115 }
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700116 ThrowIllegalArgumentException(
David Sehr709b0702016-10-13 09:12:37 -0700117 StringPrintf("Not a primitive field: %s",
118 ArtField::PrettyField(f->GetArtField()).c_str()).c_str());
Elliott Hughes33203b52011-09-20 19:42:01 -0700119 return false;
120}
121
Mathieu Chartier76433272014-09-26 14:32:37 -0700122ALWAYS_INLINE inline static bool CheckReceiver(const ScopedFastNativeObjectAccess& soa,
Mathieu Chartier299958f2016-10-05 13:48:41 -0700123 jobject j_rcvr,
124 ObjPtr<mirror::Field>* f,
125 ObjPtr<mirror::Object>* class_or_rcvr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700126 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers62f05122014-03-21 11:21:29 -0700127 soa.Self()->AssertThreadSuspensionIsAllowable();
Mathieu Chartier299958f2016-10-05 13:48:41 -0700128 ObjPtr<mirror::Class> declaring_class = (*f)->GetDeclaringClass();
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700129 if ((*f)->IsStatic()) {
Mathieu Chartier299958f2016-10-05 13:48:41 -0700130 if (UNLIKELY(!declaring_class->IsInitialized())) {
Mathieu Chartier76433272014-09-26 14:32:37 -0700131 StackHandleScope<2> hs(soa.Self());
Mathieu Chartier299958f2016-10-05 13:48:41 -0700132 HandleWrapperObjPtr<mirror::Field> h_f(hs.NewHandleWrapper(f));
133 HandleWrapperObjPtr<mirror::Class> h_klass(hs.NewHandleWrapper(&declaring_class));
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700134 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartier76433272014-09-26 14:32:37 -0700135 if (UNLIKELY(!class_linker->EnsureInitialized(soa.Self(), h_klass, true, true))) {
136 DCHECK(soa.Self()->IsExceptionPending());
137 return false;
138 }
Ian Rogers62f05122014-03-21 11:21:29 -0700139 }
Mathieu Chartier299958f2016-10-05 13:48:41 -0700140 *class_or_rcvr = declaring_class;
Elliott Hughesed1c1e32011-10-02 14:31:05 -0700141 return true;
142 }
Mathieu Chartier299958f2016-10-05 13:48:41 -0700143 *class_or_rcvr = soa.Decode<mirror::Object>(j_rcvr);
144 if (!VerifyObjectIsClass(*class_or_rcvr, declaring_class)) {
Ian Rogers62f05122014-03-21 11:21:29 -0700145 DCHECK(soa.Self()->IsExceptionPending());
Elliott Hughesed1c1e32011-10-02 14:31:05 -0700146 return false;
147 }
148 return true;
149}
150
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700151static jobject Field_get(JNIEnv* env, jobject javaField, jobject javaObj) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700152 ScopedFastNativeObjectAccess soa(env);
Mathieu Chartier299958f2016-10-05 13:48:41 -0700153 ObjPtr<mirror::Field> f = soa.Decode<mirror::Field>(javaField);
154 ObjPtr<mirror::Object> o;
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700155 if (!CheckReceiver(soa, javaObj, &f, &o)) {
Ian Rogers62f05122014-03-21 11:21:29 -0700156 DCHECK(soa.Self()->IsExceptionPending());
157 return nullptr;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800158 }
Jeff Haocb4581a2014-03-28 15:43:37 -0700159 // If field is not set to be accessible, verify it can be accessed by the caller.
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700160 if (!f->IsAccessible() && !VerifyFieldAccess<false>(soa.Self(), f, o)) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700161 DCHECK(soa.Self()->IsExceptionPending());
162 return nullptr;
163 }
Ian Rogers62f05122014-03-21 11:21:29 -0700164 // We now don't expect suspension unless an exception is thrown.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800165 // Get the field's value, boxing if necessary.
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700166 Primitive::Type field_type = f->GetTypeAsPrimitiveType();
Elliott Hughes1d878f32012-04-11 15:17:54 -0700167 JValue value;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700168 if (!GetFieldValue<true>(o, f, field_type, &value)) {
Ian Rogers62f05122014-03-21 11:21:29 -0700169 DCHECK(soa.Self()->IsExceptionPending());
170 return nullptr;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800171 }
Mathieu Chartier299958f2016-10-05 13:48:41 -0700172 return soa.AddLocalReference<jobject>(BoxPrimitive(field_type, value));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800173}
174
Mathieu Chartier76433272014-09-26 14:32:37 -0700175template<Primitive::Type kPrimitiveType>
Mathieu Chartier299958f2016-10-05 13:48:41 -0700176ALWAYS_INLINE inline static JValue GetPrimitiveField(JNIEnv* env,
177 jobject javaField,
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700178 jobject javaObj) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700179 ScopedFastNativeObjectAccess soa(env);
Mathieu Chartier299958f2016-10-05 13:48:41 -0700180 ObjPtr<mirror::Field> f = soa.Decode<mirror::Field>(javaField);
181 ObjPtr<mirror::Object> o;
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700182 if (!CheckReceiver(soa, javaObj, &f, &o)) {
Ian Rogers62f05122014-03-21 11:21:29 -0700183 DCHECK(soa.Self()->IsExceptionPending());
Elliott Hughesed1c1e32011-10-02 14:31:05 -0700184 return JValue();
Elliott Hughes33203b52011-09-20 19:42:01 -0700185 }
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700186
Jeff Haocb4581a2014-03-28 15:43:37 -0700187 // If field is not set to be accessible, verify it can be accessed by the caller.
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700188 if (!f->IsAccessible() && !VerifyFieldAccess<false>(soa.Self(), f, o)) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700189 DCHECK(soa.Self()->IsExceptionPending());
190 return JValue();
191 }
192
Ian Rogers62f05122014-03-21 11:21:29 -0700193 // We now don't expect suspension unless an exception is thrown.
Elliott Hughes33203b52011-09-20 19:42:01 -0700194 // Read the value.
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700195 Primitive::Type field_type = f->GetTypeAsPrimitiveType();
Elliott Hughes1d878f32012-04-11 15:17:54 -0700196 JValue field_value;
Mathieu Chartier76433272014-09-26 14:32:37 -0700197 if (field_type == kPrimitiveType) {
198 // This if statement should get optimized out since we only pass in valid primitive types.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700199 if (UNLIKELY(!GetFieldValue<false>(o, f, kPrimitiveType, &field_value))) {
Mathieu Chartier76433272014-09-26 14:32:37 -0700200 DCHECK(soa.Self()->IsExceptionPending());
201 return JValue();
202 }
203 return field_value;
204 }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700205 if (!GetFieldValue<false>(o, f, field_type, &field_value)) {
Ian Rogers62f05122014-03-21 11:21:29 -0700206 DCHECK(soa.Self()->IsExceptionPending());
Elliott Hughes33203b52011-09-20 19:42:01 -0700207 return JValue();
208 }
Elliott Hughes33203b52011-09-20 19:42:01 -0700209 // Widen it if necessary (and possible).
210 JValue wide_value;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000211 if (!ConvertPrimitiveValue(false, field_type, kPrimitiveType, field_value,
Mathieu Chartier76433272014-09-26 14:32:37 -0700212 &wide_value)) {
Ian Rogers62f05122014-03-21 11:21:29 -0700213 DCHECK(soa.Self()->IsExceptionPending());
Elliott Hughes33203b52011-09-20 19:42:01 -0700214 return JValue();
215 }
216 return wide_value;
217}
218
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700219static jboolean Field_getBoolean(JNIEnv* env, jobject javaField, jobject javaObj) {
220 return GetPrimitiveField<Primitive::kPrimBoolean>(env, javaField, javaObj).GetZ();
Elliott Hughes33203b52011-09-20 19:42:01 -0700221}
222
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700223static jbyte Field_getByte(JNIEnv* env, jobject javaField, jobject javaObj) {
224 return GetPrimitiveField<Primitive::kPrimByte>(env, javaField, javaObj).GetB();
Elliott Hughes33203b52011-09-20 19:42:01 -0700225}
226
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700227static jchar Field_getChar(JNIEnv* env, jobject javaField, jobject javaObj) {
228 return GetPrimitiveField<Primitive::kPrimChar>(env, javaField, javaObj).GetC();
Elliott Hughes33203b52011-09-20 19:42:01 -0700229}
230
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700231static jdouble Field_getDouble(JNIEnv* env, jobject javaField, jobject javaObj) {
232 return GetPrimitiveField<Primitive::kPrimDouble>(env, javaField, javaObj).GetD();
Elliott Hughes33203b52011-09-20 19:42:01 -0700233}
234
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700235static jfloat Field_getFloat(JNIEnv* env, jobject javaField, jobject javaObj) {
236 return GetPrimitiveField<Primitive::kPrimFloat>(env, javaField, javaObj).GetF();
Elliott Hughes33203b52011-09-20 19:42:01 -0700237}
238
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700239static jint Field_getInt(JNIEnv* env, jobject javaField, jobject javaObj) {
240 return GetPrimitiveField<Primitive::kPrimInt>(env, javaField, javaObj).GetI();
Elliott Hughes33203b52011-09-20 19:42:01 -0700241}
242
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700243static jlong Field_getLong(JNIEnv* env, jobject javaField, jobject javaObj) {
244 return GetPrimitiveField<Primitive::kPrimLong>(env, javaField, javaObj).GetJ();
Elliott Hughes33203b52011-09-20 19:42:01 -0700245}
246
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700247static jshort Field_getShort(JNIEnv* env, jobject javaField, jobject javaObj) {
248 return GetPrimitiveField<Primitive::kPrimShort>(env, javaField, javaObj).GetS();
Elliott Hughes33203b52011-09-20 19:42:01 -0700249}
250
Mathieu Chartier299958f2016-10-05 13:48:41 -0700251ALWAYS_INLINE inline static void SetFieldValue(ObjPtr<mirror::Object> o,
252 ObjPtr<mirror::Field> f,
253 Primitive::Type field_type,
254 bool allow_references,
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700255 const JValue& new_value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700256 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers62f05122014-03-21 11:21:29 -0700257 DCHECK(f->GetDeclaringClass()->IsInitialized());
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700258 MemberOffset offset(f->GetOffset());
259 const bool is_volatile = f->IsVolatile();
Ian Rogers62f05122014-03-21 11:21:29 -0700260 switch (field_type) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700261 case Primitive::kPrimBoolean:
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700262 if (is_volatile) {
263 o->SetFieldBooleanVolatile<false>(offset, new_value.GetZ());
264 } else {
265 o->SetFieldBoolean<false>(offset, new_value.GetZ());
266 }
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700267 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700268 case Primitive::kPrimByte:
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700269 if (is_volatile) {
270 o->SetFieldBooleanVolatile<false>(offset, new_value.GetB());
271 } else {
272 o->SetFieldBoolean<false>(offset, new_value.GetB());
273 }
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700274 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700275 case Primitive::kPrimChar:
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700276 if (is_volatile) {
Fredrik Roubert3152c822015-07-28 00:14:08 +0200277 o->SetFieldCharVolatile<false>(offset, new_value.GetC());
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700278 } else {
Fredrik Roubert3152c822015-07-28 00:14:08 +0200279 o->SetFieldChar<false>(offset, new_value.GetC());
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700280 }
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700281 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700282 case Primitive::kPrimInt:
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700283 case Primitive::kPrimFloat:
284 if (is_volatile) {
285 o->SetField32Volatile<false>(offset, new_value.GetI());
286 } else {
287 o->SetField32<false>(offset, new_value.GetI());
288 }
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700289 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700290 case Primitive::kPrimLong:
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700291 case Primitive::kPrimDouble:
292 if (is_volatile) {
293 o->SetField64Volatile<false>(offset, new_value.GetJ());
294 } else {
295 o->SetField64<false>(offset, new_value.GetJ());
296 }
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700297 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700298 case Primitive::kPrimShort:
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700299 if (is_volatile) {
Mathieu Chartierca239af2015-03-29 18:27:50 -0700300 o->SetFieldShortVolatile<false>(offset, new_value.GetS());
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700301 } else {
Mathieu Chartierca239af2015-03-29 18:27:50 -0700302 o->SetFieldShort<false>(offset, new_value.GetS());
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700303 }
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700304 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700305 case Primitive::kPrimNot:
Elliott Hughes33203b52011-09-20 19:42:01 -0700306 if (allow_references) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700307 if (is_volatile) {
308 o->SetFieldObjectVolatile<false>(offset, new_value.GetL());
309 } else {
310 o->SetFieldObject<false>(offset, new_value.GetL());
311 }
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700312 break;
Elliott Hughes33203b52011-09-20 19:42:01 -0700313 }
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700314 // Else fall through to report an error.
Ian Rogersfc787ec2014-10-09 21:56:44 -0700315 FALLTHROUGH_INTENDED;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700316 case Primitive::kPrimVoid:
Elliott Hughes33203b52011-09-20 19:42:01 -0700317 // Never okay.
David Sehr709b0702016-10-13 09:12:37 -0700318 ThrowIllegalArgumentException(
319 StringPrintf("Not a primitive field: %s",
320 ArtField::PrettyField(f->GetArtField()).c_str()).c_str());
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700321 return;
Elliott Hughes33203b52011-09-20 19:42:01 -0700322 }
Elliott Hughes33203b52011-09-20 19:42:01 -0700323}
324
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700325static void Field_set(JNIEnv* env, jobject javaField, jobject javaObj, jobject javaValue) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700326 ScopedFastNativeObjectAccess soa(env);
Mathieu Chartier299958f2016-10-05 13:48:41 -0700327 ObjPtr<mirror::Field> f = soa.Decode<mirror::Field>(javaField);
Ian Rogers62f05122014-03-21 11:21:29 -0700328 // Check that the receiver is non-null and an instance of the field's declaring class.
Mathieu Chartier299958f2016-10-05 13:48:41 -0700329 ObjPtr<mirror::Object> o;
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700330 if (!CheckReceiver(soa, javaObj, &f, &o)) {
Ian Rogers62f05122014-03-21 11:21:29 -0700331 DCHECK(soa.Self()->IsExceptionPending());
332 return;
333 }
Mathieu Chartier299958f2016-10-05 13:48:41 -0700334 ObjPtr<mirror::Class> field_type;
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700335 const char* field_type_desciptor = f->GetArtField()->GetTypeDescriptor();
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700336 Primitive::Type field_prim_type = Primitive::GetType(field_type_desciptor[0]);
337 if (field_prim_type == Primitive::kPrimNot) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700338 field_type = f->GetType();
339 DCHECK(field_type != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700340 } else {
341 field_type = Runtime::Current()->GetClassLinker()->FindPrimitiveClass(field_type_desciptor[0]);
Ian Rogers62f05122014-03-21 11:21:29 -0700342 }
343 // We now don't expect suspension unless an exception is thrown.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800344 // Unbox the value, if necessary.
Mathieu Chartier0795f232016-09-27 18:43:30 -0700345 ObjPtr<mirror::Object> boxed_value = soa.Decode<mirror::Object>(javaValue);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800346 JValue unboxed_value;
Mathieu Chartier0795f232016-09-27 18:43:30 -0700347 if (!UnboxPrimitiveForField(boxed_value,
Mathieu Chartierf8ac97f2016-10-05 15:56:52 -0700348 field_type,
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700349 f->GetArtField(),
350 &unboxed_value)) {
Ian Rogers62f05122014-03-21 11:21:29 -0700351 DCHECK(soa.Self()->IsExceptionPending());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800352 return;
353 }
Jeff Haocb4581a2014-03-28 15:43:37 -0700354 // If field is not set to be accessible, verify it can be accessed by the caller.
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700355 if (!f->IsAccessible() && !VerifyFieldAccess<true>(soa.Self(), f, o)) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700356 DCHECK(soa.Self()->IsExceptionPending());
357 return;
358 }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700359 SetFieldValue(o, f, field_prim_type, true, unboxed_value);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800360}
361
Mathieu Chartier76433272014-09-26 14:32:37 -0700362template<Primitive::Type kPrimitiveType>
Mathieu Chartier299958f2016-10-05 13:48:41 -0700363static void SetPrimitiveField(JNIEnv* env,
364 jobject javaField,
365 jobject javaObj,
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700366 const JValue& new_value) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700367 ScopedFastNativeObjectAccess soa(env);
Mathieu Chartier299958f2016-10-05 13:48:41 -0700368 ObjPtr<mirror::Field> f = soa.Decode<mirror::Field>(javaField);
369 ObjPtr<mirror::Object> o;
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700370 if (!CheckReceiver(soa, javaObj, &f, &o)) {
Elliott Hughesed1c1e32011-10-02 14:31:05 -0700371 return;
Elliott Hughes33203b52011-09-20 19:42:01 -0700372 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700373 Primitive::Type field_type = f->GetTypeAsPrimitiveType();
Ian Rogers62f05122014-03-21 11:21:29 -0700374 if (UNLIKELY(field_type == Primitive::kPrimNot)) {
David Sehr709b0702016-10-13 09:12:37 -0700375 ThrowIllegalArgumentException(
376 StringPrintf("Not a primitive field: %s",
377 ArtField::PrettyField(f->GetArtField()).c_str()).c_str());
Jesse Wilsonc129a6b2011-11-24 14:47:46 -0500378 return;
379 }
Elliott Hughes33203b52011-09-20 19:42:01 -0700380
381 // Widen the value if necessary (and possible).
382 JValue wide_value;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000383 if (!ConvertPrimitiveValue(false, kPrimitiveType, field_type, new_value, &wide_value)) {
Ian Rogers62f05122014-03-21 11:21:29 -0700384 DCHECK(soa.Self()->IsExceptionPending());
Elliott Hughes33203b52011-09-20 19:42:01 -0700385 return;
386 }
387
Jeff Haocb4581a2014-03-28 15:43:37 -0700388 // If field is not set to be accessible, verify it can be accessed by the caller.
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700389 if (!f->IsAccessible() && !VerifyFieldAccess<true>(soa.Self(), f, o)) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700390 DCHECK(soa.Self()->IsExceptionPending());
391 return;
392 }
393
Elliott Hughes33203b52011-09-20 19:42:01 -0700394 // Write the value.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700395 SetFieldValue(o, f, field_type, false, wide_value);
Elliott Hughes33203b52011-09-20 19:42:01 -0700396}
397
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700398static void Field_setBoolean(JNIEnv* env, jobject javaField, jobject javaObj, jboolean z) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700399 JValue value;
400 value.SetZ(z);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700401 SetPrimitiveField<Primitive::kPrimBoolean>(env, javaField, javaObj, value);
Elliott Hughes33203b52011-09-20 19:42:01 -0700402}
403
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700404static void Field_setByte(JNIEnv* env, jobject javaField, jobject javaObj, jbyte b) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700405 JValue value;
406 value.SetB(b);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700407 SetPrimitiveField<Primitive::kPrimByte>(env, javaField, javaObj, value);
Elliott Hughes33203b52011-09-20 19:42:01 -0700408}
409
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700410static void Field_setChar(JNIEnv* env, jobject javaField, jobject javaObj, jchar c) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700411 JValue value;
412 value.SetC(c);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700413 SetPrimitiveField<Primitive::kPrimChar>(env, javaField, javaObj, value);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800414}
Elliott Hughes33203b52011-09-20 19:42:01 -0700415
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700416static void Field_setDouble(JNIEnv* env, jobject javaField, jobject javaObj, jdouble d) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700417 JValue value;
418 value.SetD(d);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700419 SetPrimitiveField<Primitive::kPrimDouble>(env, javaField, javaObj, value);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800420}
Elliott Hughes33203b52011-09-20 19:42:01 -0700421
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700422static void Field_setFloat(JNIEnv* env, jobject javaField, jobject javaObj, jfloat f) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700423 JValue value;
424 value.SetF(f);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700425 SetPrimitiveField<Primitive::kPrimFloat>(env, javaField, javaObj, value);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800426}
427
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700428static void Field_setInt(JNIEnv* env, jobject javaField, jobject javaObj, jint i) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700429 JValue value;
430 value.SetI(i);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700431 SetPrimitiveField<Primitive::kPrimInt>(env, javaField, javaObj, value);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800432}
433
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700434static void Field_setLong(JNIEnv* env, jobject javaField, jobject javaObj, jlong j) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700435 JValue value;
436 value.SetJ(j);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700437 SetPrimitiveField<Primitive::kPrimLong>(env, javaField, javaObj, value);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800438}
439
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700440static void Field_setShort(JNIEnv* env, jobject javaField, jobject javaObj, jshort s) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700441 JValue value;
442 value.SetS(s);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700443 SetPrimitiveField<Primitive::kPrimShort>(env, javaField, javaObj, value);
Elliott Hughes33203b52011-09-20 19:42:01 -0700444}
445
Jeff Hao13e748b2015-08-25 20:44:19 +0000446static jobject Field_getAnnotationNative(JNIEnv* env, jobject javaField, jclass annotationType) {
447 ScopedFastNativeObjectAccess soa(env);
448 StackHandleScope<1> hs(soa.Self());
Mathieu Chartier0795f232016-09-27 18:43:30 -0700449 ArtField* field = soa.Decode<mirror::Field>(javaField)->GetArtField();
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000450 if (field->GetDeclaringClass()->IsProxyClass()) {
Jeff Hao13e748b2015-08-25 20:44:19 +0000451 return nullptr;
452 }
Mathieu Chartier0795f232016-09-27 18:43:30 -0700453 Handle<mirror::Class> klass(hs.NewHandle(soa.Decode<mirror::Class>(annotationType)));
David Sehr9323e6e2016-09-13 08:58:35 -0700454 return soa.AddLocalReference<jobject>(annotations::GetAnnotationForField(field, klass));
Jeff Hao13e748b2015-08-25 20:44:19 +0000455}
456
Orion Hodson3d617ac2016-10-19 14:00:46 +0100457static jlong Field_getArtField(JNIEnv* env, jobject javaField) {
458 ScopedFastNativeObjectAccess soa(env);
459 ArtField* field = soa.Decode<mirror::Field>(javaField)->GetArtField();
460 return reinterpret_cast<jlong>(field);
461}
462
Narayan Kamath6b2dc312017-03-14 13:26:12 +0000463static jobject Field_getNameInternal(JNIEnv* env, jobject javaField) {
464 ScopedFastNativeObjectAccess soa(env);
465 ArtField* field = soa.Decode<mirror::Field>(javaField)->GetArtField();
466 return soa.AddLocalReference<jobject>(
467 field->GetStringName(soa.Self(), true /* resolve */));
468}
469
Jeff Hao13e748b2015-08-25 20:44:19 +0000470static jobjectArray Field_getDeclaredAnnotations(JNIEnv* env, jobject javaField) {
471 ScopedFastNativeObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700472 ArtField* field = soa.Decode<mirror::Field>(javaField)->GetArtField();
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000473 if (field->GetDeclaringClass()->IsProxyClass()) {
Jeff Hao13e748b2015-08-25 20:44:19 +0000474 // Return an empty array instead of a null pointer.
Mathieu Chartier299958f2016-10-05 13:48:41 -0700475 ObjPtr<mirror::Class> annotation_array_class =
476 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_annotation_Annotation__array);
477 ObjPtr<mirror::ObjectArray<mirror::Object>> empty_array =
478 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), annotation_array_class.Ptr(), 0);
Jeff Hao13e748b2015-08-25 20:44:19 +0000479 return soa.AddLocalReference<jobjectArray>(empty_array);
480 }
David Sehr9323e6e2016-09-13 08:58:35 -0700481 return soa.AddLocalReference<jobjectArray>(annotations::GetAnnotationsForField(field));
Jeff Hao13e748b2015-08-25 20:44:19 +0000482}
483
484static jobjectArray Field_getSignatureAnnotation(JNIEnv* env, jobject javaField) {
485 ScopedFastNativeObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700486 ArtField* field = soa.Decode<mirror::Field>(javaField)->GetArtField();
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000487 if (field->GetDeclaringClass()->IsProxyClass()) {
Jeff Hao13e748b2015-08-25 20:44:19 +0000488 return nullptr;
489 }
David Sehr9323e6e2016-09-13 08:58:35 -0700490 return soa.AddLocalReference<jobjectArray>(annotations::GetSignatureAnnotationForField(field));
Jeff Hao13e748b2015-08-25 20:44:19 +0000491}
492
Mathieu Chartier299958f2016-10-05 13:48:41 -0700493static jboolean Field_isAnnotationPresentNative(JNIEnv* env,
494 jobject javaField,
Jeff Hao13e748b2015-08-25 20:44:19 +0000495 jclass annotationType) {
496 ScopedFastNativeObjectAccess soa(env);
497 StackHandleScope<1> hs(soa.Self());
Mathieu Chartier0795f232016-09-27 18:43:30 -0700498 ArtField* field = soa.Decode<mirror::Field>(javaField)->GetArtField();
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000499 if (field->GetDeclaringClass()->IsProxyClass()) {
Jeff Hao13e748b2015-08-25 20:44:19 +0000500 return false;
501 }
Mathieu Chartier0795f232016-09-27 18:43:30 -0700502 Handle<mirror::Class> klass(hs.NewHandle(soa.Decode<mirror::Class>(annotationType)));
David Sehr9323e6e2016-09-13 08:58:35 -0700503 return annotations::IsFieldAnnotationPresent(field, klass);
Jeff Hao13e748b2015-08-25 20:44:19 +0000504}
505
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700506static JNINativeMethod gMethods[] = {
Igor Murashkin3b6f4402017-02-16 16:13:17 -0800507 FAST_NATIVE_METHOD(Field, get, "(Ljava/lang/Object;)Ljava/lang/Object;"),
508 FAST_NATIVE_METHOD(Field, getBoolean, "(Ljava/lang/Object;)Z"),
509 FAST_NATIVE_METHOD(Field, getByte, "(Ljava/lang/Object;)B"),
510 FAST_NATIVE_METHOD(Field, getChar, "(Ljava/lang/Object;)C"),
511 FAST_NATIVE_METHOD(Field, getAnnotationNative,
512 "(Ljava/lang/Class;)Ljava/lang/annotation/Annotation;"),
513 FAST_NATIVE_METHOD(Field, getArtField, "()J"),
514 FAST_NATIVE_METHOD(Field, getDeclaredAnnotations, "()[Ljava/lang/annotation/Annotation;"),
515 FAST_NATIVE_METHOD(Field, getSignatureAnnotation, "()[Ljava/lang/String;"),
516 FAST_NATIVE_METHOD(Field, getDouble, "(Ljava/lang/Object;)D"),
517 FAST_NATIVE_METHOD(Field, getFloat, "(Ljava/lang/Object;)F"),
518 FAST_NATIVE_METHOD(Field, getInt, "(Ljava/lang/Object;)I"),
519 FAST_NATIVE_METHOD(Field, getLong, "(Ljava/lang/Object;)J"),
Narayan Kamath6b2dc312017-03-14 13:26:12 +0000520 FAST_NATIVE_METHOD(Field, getNameInternal, "()Ljava/lang/String;"),
Igor Murashkin3b6f4402017-02-16 16:13:17 -0800521 FAST_NATIVE_METHOD(Field, getShort, "(Ljava/lang/Object;)S"),
522 FAST_NATIVE_METHOD(Field, isAnnotationPresentNative, "(Ljava/lang/Class;)Z"),
523 FAST_NATIVE_METHOD(Field, set, "(Ljava/lang/Object;Ljava/lang/Object;)V"),
524 FAST_NATIVE_METHOD(Field, setBoolean, "(Ljava/lang/Object;Z)V"),
525 FAST_NATIVE_METHOD(Field, setByte, "(Ljava/lang/Object;B)V"),
526 FAST_NATIVE_METHOD(Field, setChar, "(Ljava/lang/Object;C)V"),
527 FAST_NATIVE_METHOD(Field, setDouble, "(Ljava/lang/Object;D)V"),
528 FAST_NATIVE_METHOD(Field, setFloat, "(Ljava/lang/Object;F)V"),
529 FAST_NATIVE_METHOD(Field, setInt, "(Ljava/lang/Object;I)V"),
530 FAST_NATIVE_METHOD(Field, setLong, "(Ljava/lang/Object;J)V"),
531 FAST_NATIVE_METHOD(Field, setShort, "(Ljava/lang/Object;S)V"),
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700532};
533
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700534void register_java_lang_reflect_Field(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700535 REGISTER_NATIVE_METHODS("java/lang/reflect/Field");
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700536}
537
538} // namespace art