blob: 9198964f87aa068c1f2bc6c57204c7e88d66ed34 [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"
20
Brian Carlstromf867b6f2011-09-16 12:17:25 -070021#include "class_linker.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "class_linker-inl.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080023#include "common_throws.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070024#include "dex_file-inl.h"
David Sehr9323e6e2016-09-13 08:58:35 -070025#include "dex_file_annotations.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070026#include "jni_internal.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070027#include "mirror/class-inl.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070028#include "mirror/field.h"
Mathieu Chartier76433272014-09-26 14:32:37 -070029#include "reflection-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070030#include "scoped_fast_native_object_access-inl.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010031#include "utils.h"
Brian Carlstromf867b6f2011-09-16 12:17:25 -070032
Brian Carlstromf867b6f2011-09-16 12:17:25 -070033namespace art {
34
Andreas Gampe46ee31b2016-12-14 10:11:49 -080035using android::base::StringPrintf;
36
Mathieu Chartier76433272014-09-26 14:32:37 -070037template<bool kIsSet>
Mathieu Chartier299958f2016-10-05 13:48:41 -070038ALWAYS_INLINE inline static bool VerifyFieldAccess(Thread* self,
39 ObjPtr<mirror::Field> field,
40 ObjPtr<mirror::Object> obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070041 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier76433272014-09-26 14:32:37 -070042 if (kIsSet && field->IsFinal()) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000043 ThrowIllegalAccessException(
Andreas Gampec0d82292014-09-23 10:38:30 -070044 StringPrintf("Cannot set %s field %s of class %s",
45 PrettyJavaAccessFlags(field->GetAccessFlags()).c_str(),
David Sehr709b0702016-10-13 09:12:37 -070046 ArtField::PrettyField(field->GetArtField()).c_str(),
Andreas Gampec0d82292014-09-23 10:38:30 -070047 field->GetDeclaringClass() == nullptr ? "null" :
David Sehr709b0702016-10-13 09:12:37 -070048 field->GetDeclaringClass()->PrettyClass().c_str()).c_str());
Jeff Hao11d5d8f2014-03-26 15:08:20 -070049 return false;
50 }
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070051 ObjPtr<mirror::Class> calling_class;
52 if (!VerifyAccess(self,
Mathieu Chartierf8ac97f2016-10-05 15:56:52 -070053 obj,
54 field->GetDeclaringClass(),
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070055 field->GetAccessFlags(),
56 &calling_class,
57 1)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000058 ThrowIllegalAccessException(
Andreas Gampec0d82292014-09-23 10:38:30 -070059 StringPrintf("Class %s cannot access %s field %s of class %s",
David Sehr709b0702016-10-13 09:12:37 -070060 calling_class == nullptr ? "null" : calling_class->PrettyClass().c_str(),
Andreas Gampec0d82292014-09-23 10:38:30 -070061 PrettyJavaAccessFlags(field->GetAccessFlags()).c_str(),
David Sehr709b0702016-10-13 09:12:37 -070062 ArtField::PrettyField(field->GetArtField()).c_str(),
Andreas Gampec0d82292014-09-23 10:38:30 -070063 field->GetDeclaringClass() == nullptr ? "null" :
David Sehr709b0702016-10-13 09:12:37 -070064 field->GetDeclaringClass()->PrettyClass().c_str()).c_str());
Jeff Hao11d5d8f2014-03-26 15:08:20 -070065 return false;
66 }
67 return true;
68}
69
Mathieu Chartier76433272014-09-26 14:32:37 -070070template<bool kAllowReferences>
Mathieu Chartier299958f2016-10-05 13:48:41 -070071ALWAYS_INLINE inline static bool GetFieldValue(ObjPtr<mirror::Object> o,
72 ObjPtr<mirror::Field> f,
73 Primitive::Type field_type,
74 JValue* value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070075 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers62f05122014-03-21 11:21:29 -070076 DCHECK_EQ(value->GetJ(), INT64_C(0));
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070077 MemberOffset offset(f->GetOffset());
78 const bool is_volatile = f->IsVolatile();
Ian Rogers62f05122014-03-21 11:21:29 -070079 switch (field_type) {
80 case Primitive::kPrimBoolean:
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070081 value->SetZ(is_volatile ? o->GetFieldBooleanVolatile(offset) : o->GetFieldBoolean(offset));
Elliott Hughes33203b52011-09-20 19:42:01 -070082 return true;
Ian Rogers62f05122014-03-21 11:21:29 -070083 case Primitive::kPrimByte:
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070084 value->SetB(is_volatile ? o->GetFieldByteVolatile(offset) : o->GetFieldByte(offset));
Ian Rogers62f05122014-03-21 11:21:29 -070085 return true;
86 case Primitive::kPrimChar:
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070087 value->SetC(is_volatile ? o->GetFieldCharVolatile(offset) : o->GetFieldChar(offset));
Ian Rogers62f05122014-03-21 11:21:29 -070088 return true;
89 case Primitive::kPrimInt:
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070090 case Primitive::kPrimFloat:
91 value->SetI(is_volatile ? o->GetField32Volatile(offset) : o->GetField32(offset));
Ian Rogers62f05122014-03-21 11:21:29 -070092 return true;
93 case Primitive::kPrimLong:
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070094 case Primitive::kPrimDouble:
95 value->SetJ(is_volatile ? o->GetField64Volatile(offset) : o->GetField64(offset));
Ian Rogers62f05122014-03-21 11:21:29 -070096 return true;
97 case Primitive::kPrimShort:
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070098 value->SetS(is_volatile ? o->GetFieldShortVolatile(offset) : o->GetFieldShort(offset));
Ian Rogers62f05122014-03-21 11:21:29 -070099 return true;
100 case Primitive::kPrimNot:
Mathieu Chartier76433272014-09-26 14:32:37 -0700101 if (kAllowReferences) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700102 value->SetL(is_volatile ? o->GetFieldObjectVolatile<mirror::Object>(offset) :
103 o->GetFieldObject<mirror::Object>(offset));
Ian Rogers62f05122014-03-21 11:21:29 -0700104 return true;
105 }
106 // Else break to report an error.
107 break;
108 case Primitive::kPrimVoid:
109 // Never okay.
110 break;
Elliott Hughes33203b52011-09-20 19:42:01 -0700111 }
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700112 ThrowIllegalArgumentException(
David Sehr709b0702016-10-13 09:12:37 -0700113 StringPrintf("Not a primitive field: %s",
114 ArtField::PrettyField(f->GetArtField()).c_str()).c_str());
Elliott Hughes33203b52011-09-20 19:42:01 -0700115 return false;
116}
117
Mathieu Chartier76433272014-09-26 14:32:37 -0700118ALWAYS_INLINE inline static bool CheckReceiver(const ScopedFastNativeObjectAccess& soa,
Mathieu Chartier299958f2016-10-05 13:48:41 -0700119 jobject j_rcvr,
120 ObjPtr<mirror::Field>* f,
121 ObjPtr<mirror::Object>* class_or_rcvr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700122 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers62f05122014-03-21 11:21:29 -0700123 soa.Self()->AssertThreadSuspensionIsAllowable();
Mathieu Chartier299958f2016-10-05 13:48:41 -0700124 ObjPtr<mirror::Class> declaring_class = (*f)->GetDeclaringClass();
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700125 if ((*f)->IsStatic()) {
Mathieu Chartier299958f2016-10-05 13:48:41 -0700126 if (UNLIKELY(!declaring_class->IsInitialized())) {
Mathieu Chartier76433272014-09-26 14:32:37 -0700127 StackHandleScope<2> hs(soa.Self());
Mathieu Chartier299958f2016-10-05 13:48:41 -0700128 HandleWrapperObjPtr<mirror::Field> h_f(hs.NewHandleWrapper(f));
129 HandleWrapperObjPtr<mirror::Class> h_klass(hs.NewHandleWrapper(&declaring_class));
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700130 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartier76433272014-09-26 14:32:37 -0700131 if (UNLIKELY(!class_linker->EnsureInitialized(soa.Self(), h_klass, true, true))) {
132 DCHECK(soa.Self()->IsExceptionPending());
133 return false;
134 }
Ian Rogers62f05122014-03-21 11:21:29 -0700135 }
Mathieu Chartier299958f2016-10-05 13:48:41 -0700136 *class_or_rcvr = declaring_class;
Elliott Hughesed1c1e32011-10-02 14:31:05 -0700137 return true;
138 }
Mathieu Chartier299958f2016-10-05 13:48:41 -0700139 *class_or_rcvr = soa.Decode<mirror::Object>(j_rcvr);
140 if (!VerifyObjectIsClass(*class_or_rcvr, declaring_class)) {
Ian Rogers62f05122014-03-21 11:21:29 -0700141 DCHECK(soa.Self()->IsExceptionPending());
Elliott Hughesed1c1e32011-10-02 14:31:05 -0700142 return false;
143 }
144 return true;
145}
146
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700147static jobject Field_get(JNIEnv* env, jobject javaField, jobject javaObj) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700148 ScopedFastNativeObjectAccess soa(env);
Mathieu Chartier299958f2016-10-05 13:48:41 -0700149 ObjPtr<mirror::Field> f = soa.Decode<mirror::Field>(javaField);
150 ObjPtr<mirror::Object> o;
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700151 if (!CheckReceiver(soa, javaObj, &f, &o)) {
Ian Rogers62f05122014-03-21 11:21:29 -0700152 DCHECK(soa.Self()->IsExceptionPending());
153 return nullptr;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800154 }
Jeff Haocb4581a2014-03-28 15:43:37 -0700155 // If field is not set to be accessible, verify it can be accessed by the caller.
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700156 if (!f->IsAccessible() && !VerifyFieldAccess<false>(soa.Self(), f, o)) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700157 DCHECK(soa.Self()->IsExceptionPending());
158 return nullptr;
159 }
Ian Rogers62f05122014-03-21 11:21:29 -0700160 // We now don't expect suspension unless an exception is thrown.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800161 // Get the field's value, boxing if necessary.
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700162 Primitive::Type field_type = f->GetTypeAsPrimitiveType();
Elliott Hughes1d878f32012-04-11 15:17:54 -0700163 JValue value;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700164 if (!GetFieldValue<true>(o, f, field_type, &value)) {
Ian Rogers62f05122014-03-21 11:21:29 -0700165 DCHECK(soa.Self()->IsExceptionPending());
166 return nullptr;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800167 }
Mathieu Chartier299958f2016-10-05 13:48:41 -0700168 return soa.AddLocalReference<jobject>(BoxPrimitive(field_type, value));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800169}
170
Mathieu Chartier76433272014-09-26 14:32:37 -0700171template<Primitive::Type kPrimitiveType>
Mathieu Chartier299958f2016-10-05 13:48:41 -0700172ALWAYS_INLINE inline static JValue GetPrimitiveField(JNIEnv* env,
173 jobject javaField,
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700174 jobject javaObj) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700175 ScopedFastNativeObjectAccess soa(env);
Mathieu Chartier299958f2016-10-05 13:48:41 -0700176 ObjPtr<mirror::Field> f = soa.Decode<mirror::Field>(javaField);
177 ObjPtr<mirror::Object> o;
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700178 if (!CheckReceiver(soa, javaObj, &f, &o)) {
Ian Rogers62f05122014-03-21 11:21:29 -0700179 DCHECK(soa.Self()->IsExceptionPending());
Elliott Hughesed1c1e32011-10-02 14:31:05 -0700180 return JValue();
Elliott Hughes33203b52011-09-20 19:42:01 -0700181 }
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700182
Jeff Haocb4581a2014-03-28 15:43:37 -0700183 // If field is not set to be accessible, verify it can be accessed by the caller.
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700184 if (!f->IsAccessible() && !VerifyFieldAccess<false>(soa.Self(), f, o)) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700185 DCHECK(soa.Self()->IsExceptionPending());
186 return JValue();
187 }
188
Ian Rogers62f05122014-03-21 11:21:29 -0700189 // We now don't expect suspension unless an exception is thrown.
Elliott Hughes33203b52011-09-20 19:42:01 -0700190 // Read the value.
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700191 Primitive::Type field_type = f->GetTypeAsPrimitiveType();
Elliott Hughes1d878f32012-04-11 15:17:54 -0700192 JValue field_value;
Mathieu Chartier76433272014-09-26 14:32:37 -0700193 if (field_type == kPrimitiveType) {
194 // This if statement should get optimized out since we only pass in valid primitive types.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700195 if (UNLIKELY(!GetFieldValue<false>(o, f, kPrimitiveType, &field_value))) {
Mathieu Chartier76433272014-09-26 14:32:37 -0700196 DCHECK(soa.Self()->IsExceptionPending());
197 return JValue();
198 }
199 return field_value;
200 }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700201 if (!GetFieldValue<false>(o, f, field_type, &field_value)) {
Ian Rogers62f05122014-03-21 11:21:29 -0700202 DCHECK(soa.Self()->IsExceptionPending());
Elliott Hughes33203b52011-09-20 19:42:01 -0700203 return JValue();
204 }
Elliott Hughes33203b52011-09-20 19:42:01 -0700205 // Widen it if necessary (and possible).
206 JValue wide_value;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000207 if (!ConvertPrimitiveValue(false, field_type, kPrimitiveType, field_value,
Mathieu Chartier76433272014-09-26 14:32:37 -0700208 &wide_value)) {
Ian Rogers62f05122014-03-21 11:21:29 -0700209 DCHECK(soa.Self()->IsExceptionPending());
Elliott Hughes33203b52011-09-20 19:42:01 -0700210 return JValue();
211 }
212 return wide_value;
213}
214
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700215static jboolean Field_getBoolean(JNIEnv* env, jobject javaField, jobject javaObj) {
216 return GetPrimitiveField<Primitive::kPrimBoolean>(env, javaField, javaObj).GetZ();
Elliott Hughes33203b52011-09-20 19:42:01 -0700217}
218
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700219static jbyte Field_getByte(JNIEnv* env, jobject javaField, jobject javaObj) {
220 return GetPrimitiveField<Primitive::kPrimByte>(env, javaField, javaObj).GetB();
Elliott Hughes33203b52011-09-20 19:42:01 -0700221}
222
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700223static jchar Field_getChar(JNIEnv* env, jobject javaField, jobject javaObj) {
224 return GetPrimitiveField<Primitive::kPrimChar>(env, javaField, javaObj).GetC();
Elliott Hughes33203b52011-09-20 19:42:01 -0700225}
226
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700227static jdouble Field_getDouble(JNIEnv* env, jobject javaField, jobject javaObj) {
228 return GetPrimitiveField<Primitive::kPrimDouble>(env, javaField, javaObj).GetD();
Elliott Hughes33203b52011-09-20 19:42:01 -0700229}
230
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700231static jfloat Field_getFloat(JNIEnv* env, jobject javaField, jobject javaObj) {
232 return GetPrimitiveField<Primitive::kPrimFloat>(env, javaField, javaObj).GetF();
Elliott Hughes33203b52011-09-20 19:42:01 -0700233}
234
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700235static jint Field_getInt(JNIEnv* env, jobject javaField, jobject javaObj) {
236 return GetPrimitiveField<Primitive::kPrimInt>(env, javaField, javaObj).GetI();
Elliott Hughes33203b52011-09-20 19:42:01 -0700237}
238
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700239static jlong Field_getLong(JNIEnv* env, jobject javaField, jobject javaObj) {
240 return GetPrimitiveField<Primitive::kPrimLong>(env, javaField, javaObj).GetJ();
Elliott Hughes33203b52011-09-20 19:42:01 -0700241}
242
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700243static jshort Field_getShort(JNIEnv* env, jobject javaField, jobject javaObj) {
244 return GetPrimitiveField<Primitive::kPrimShort>(env, javaField, javaObj).GetS();
Elliott Hughes33203b52011-09-20 19:42:01 -0700245}
246
Mathieu Chartier299958f2016-10-05 13:48:41 -0700247ALWAYS_INLINE inline static void SetFieldValue(ObjPtr<mirror::Object> o,
248 ObjPtr<mirror::Field> f,
249 Primitive::Type field_type,
250 bool allow_references,
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700251 const JValue& new_value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700252 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers62f05122014-03-21 11:21:29 -0700253 DCHECK(f->GetDeclaringClass()->IsInitialized());
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700254 MemberOffset offset(f->GetOffset());
255 const bool is_volatile = f->IsVolatile();
Ian Rogers62f05122014-03-21 11:21:29 -0700256 switch (field_type) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700257 case Primitive::kPrimBoolean:
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700258 if (is_volatile) {
259 o->SetFieldBooleanVolatile<false>(offset, new_value.GetZ());
260 } else {
261 o->SetFieldBoolean<false>(offset, new_value.GetZ());
262 }
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700263 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700264 case Primitive::kPrimByte:
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700265 if (is_volatile) {
266 o->SetFieldBooleanVolatile<false>(offset, new_value.GetB());
267 } else {
268 o->SetFieldBoolean<false>(offset, new_value.GetB());
269 }
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700270 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700271 case Primitive::kPrimChar:
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700272 if (is_volatile) {
Fredrik Roubert3152c822015-07-28 00:14:08 +0200273 o->SetFieldCharVolatile<false>(offset, new_value.GetC());
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700274 } else {
Fredrik Roubert3152c822015-07-28 00:14:08 +0200275 o->SetFieldChar<false>(offset, new_value.GetC());
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700276 }
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700277 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700278 case Primitive::kPrimInt:
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700279 case Primitive::kPrimFloat:
280 if (is_volatile) {
281 o->SetField32Volatile<false>(offset, new_value.GetI());
282 } else {
283 o->SetField32<false>(offset, new_value.GetI());
284 }
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700285 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700286 case Primitive::kPrimLong:
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700287 case Primitive::kPrimDouble:
288 if (is_volatile) {
289 o->SetField64Volatile<false>(offset, new_value.GetJ());
290 } else {
291 o->SetField64<false>(offset, new_value.GetJ());
292 }
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700293 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700294 case Primitive::kPrimShort:
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700295 if (is_volatile) {
Mathieu Chartierca239af2015-03-29 18:27:50 -0700296 o->SetFieldShortVolatile<false>(offset, new_value.GetS());
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700297 } else {
Mathieu Chartierca239af2015-03-29 18:27:50 -0700298 o->SetFieldShort<false>(offset, new_value.GetS());
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700299 }
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700300 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700301 case Primitive::kPrimNot:
Elliott Hughes33203b52011-09-20 19:42:01 -0700302 if (allow_references) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700303 if (is_volatile) {
304 o->SetFieldObjectVolatile<false>(offset, new_value.GetL());
305 } else {
306 o->SetFieldObject<false>(offset, new_value.GetL());
307 }
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700308 break;
Elliott Hughes33203b52011-09-20 19:42:01 -0700309 }
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700310 // Else fall through to report an error.
Ian Rogersfc787ec2014-10-09 21:56:44 -0700311 FALLTHROUGH_INTENDED;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700312 case Primitive::kPrimVoid:
Elliott Hughes33203b52011-09-20 19:42:01 -0700313 // Never okay.
David Sehr709b0702016-10-13 09:12:37 -0700314 ThrowIllegalArgumentException(
315 StringPrintf("Not a primitive field: %s",
316 ArtField::PrettyField(f->GetArtField()).c_str()).c_str());
Elliott Hughesfe6207f2011-09-26 17:24:06 -0700317 return;
Elliott Hughes33203b52011-09-20 19:42:01 -0700318 }
Elliott Hughes33203b52011-09-20 19:42:01 -0700319}
320
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700321static void Field_set(JNIEnv* env, jobject javaField, jobject javaObj, jobject javaValue) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700322 ScopedFastNativeObjectAccess soa(env);
Mathieu Chartier299958f2016-10-05 13:48:41 -0700323 ObjPtr<mirror::Field> f = soa.Decode<mirror::Field>(javaField);
Ian Rogers62f05122014-03-21 11:21:29 -0700324 // Check that the receiver is non-null and an instance of the field's declaring class.
Mathieu Chartier299958f2016-10-05 13:48:41 -0700325 ObjPtr<mirror::Object> o;
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700326 if (!CheckReceiver(soa, javaObj, &f, &o)) {
Ian Rogers62f05122014-03-21 11:21:29 -0700327 DCHECK(soa.Self()->IsExceptionPending());
328 return;
329 }
Mathieu Chartier299958f2016-10-05 13:48:41 -0700330 ObjPtr<mirror::Class> field_type;
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700331 const char* field_type_desciptor = f->GetArtField()->GetTypeDescriptor();
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700332 Primitive::Type field_prim_type = Primitive::GetType(field_type_desciptor[0]);
333 if (field_prim_type == Primitive::kPrimNot) {
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700334 field_type = f->GetType();
335 DCHECK(field_type != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700336 } else {
337 field_type = Runtime::Current()->GetClassLinker()->FindPrimitiveClass(field_type_desciptor[0]);
Ian Rogers62f05122014-03-21 11:21:29 -0700338 }
339 // We now don't expect suspension unless an exception is thrown.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800340 // Unbox the value, if necessary.
Mathieu Chartier0795f232016-09-27 18:43:30 -0700341 ObjPtr<mirror::Object> boxed_value = soa.Decode<mirror::Object>(javaValue);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800342 JValue unboxed_value;
Mathieu Chartier0795f232016-09-27 18:43:30 -0700343 if (!UnboxPrimitiveForField(boxed_value,
Mathieu Chartierf8ac97f2016-10-05 15:56:52 -0700344 field_type,
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700345 f->GetArtField(),
346 &unboxed_value)) {
Ian Rogers62f05122014-03-21 11:21:29 -0700347 DCHECK(soa.Self()->IsExceptionPending());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800348 return;
349 }
Jeff Haocb4581a2014-03-28 15:43:37 -0700350 // If field is not set to be accessible, verify it can be accessed by the caller.
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700351 if (!f->IsAccessible() && !VerifyFieldAccess<true>(soa.Self(), f, o)) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700352 DCHECK(soa.Self()->IsExceptionPending());
353 return;
354 }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700355 SetFieldValue(o, f, field_prim_type, true, unboxed_value);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800356}
357
Mathieu Chartier76433272014-09-26 14:32:37 -0700358template<Primitive::Type kPrimitiveType>
Mathieu Chartier299958f2016-10-05 13:48:41 -0700359static void SetPrimitiveField(JNIEnv* env,
360 jobject javaField,
361 jobject javaObj,
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700362 const JValue& new_value) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700363 ScopedFastNativeObjectAccess soa(env);
Mathieu Chartier299958f2016-10-05 13:48:41 -0700364 ObjPtr<mirror::Field> f = soa.Decode<mirror::Field>(javaField);
365 ObjPtr<mirror::Object> o;
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700366 if (!CheckReceiver(soa, javaObj, &f, &o)) {
Elliott Hughesed1c1e32011-10-02 14:31:05 -0700367 return;
Elliott Hughes33203b52011-09-20 19:42:01 -0700368 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700369 Primitive::Type field_type = f->GetTypeAsPrimitiveType();
Ian Rogers62f05122014-03-21 11:21:29 -0700370 if (UNLIKELY(field_type == Primitive::kPrimNot)) {
David Sehr709b0702016-10-13 09:12:37 -0700371 ThrowIllegalArgumentException(
372 StringPrintf("Not a primitive field: %s",
373 ArtField::PrettyField(f->GetArtField()).c_str()).c_str());
Jesse Wilsonc129a6b2011-11-24 14:47:46 -0500374 return;
375 }
Elliott Hughes33203b52011-09-20 19:42:01 -0700376
377 // Widen the value if necessary (and possible).
378 JValue wide_value;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000379 if (!ConvertPrimitiveValue(false, kPrimitiveType, field_type, new_value, &wide_value)) {
Ian Rogers62f05122014-03-21 11:21:29 -0700380 DCHECK(soa.Self()->IsExceptionPending());
Elliott Hughes33203b52011-09-20 19:42:01 -0700381 return;
382 }
383
Jeff Haocb4581a2014-03-28 15:43:37 -0700384 // If field is not set to be accessible, verify it can be accessed by the caller.
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700385 if (!f->IsAccessible() && !VerifyFieldAccess<true>(soa.Self(), f, o)) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700386 DCHECK(soa.Self()->IsExceptionPending());
387 return;
388 }
389
Elliott Hughes33203b52011-09-20 19:42:01 -0700390 // Write the value.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700391 SetFieldValue(o, f, field_type, false, wide_value);
Elliott Hughes33203b52011-09-20 19:42:01 -0700392}
393
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700394static void Field_setBoolean(JNIEnv* env, jobject javaField, jobject javaObj, jboolean z) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700395 JValue value;
396 value.SetZ(z);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700397 SetPrimitiveField<Primitive::kPrimBoolean>(env, javaField, javaObj, value);
Elliott Hughes33203b52011-09-20 19:42:01 -0700398}
399
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700400static void Field_setByte(JNIEnv* env, jobject javaField, jobject javaObj, jbyte b) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700401 JValue value;
402 value.SetB(b);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700403 SetPrimitiveField<Primitive::kPrimByte>(env, javaField, javaObj, value);
Elliott Hughes33203b52011-09-20 19:42:01 -0700404}
405
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700406static void Field_setChar(JNIEnv* env, jobject javaField, jobject javaObj, jchar c) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700407 JValue value;
408 value.SetC(c);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700409 SetPrimitiveField<Primitive::kPrimChar>(env, javaField, javaObj, value);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800410}
Elliott Hughes33203b52011-09-20 19:42:01 -0700411
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700412static void Field_setDouble(JNIEnv* env, jobject javaField, jobject javaObj, jdouble d) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700413 JValue value;
414 value.SetD(d);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700415 SetPrimitiveField<Primitive::kPrimDouble>(env, javaField, javaObj, value);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800416}
Elliott Hughes33203b52011-09-20 19:42:01 -0700417
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700418static void Field_setFloat(JNIEnv* env, jobject javaField, jobject javaObj, jfloat f) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700419 JValue value;
420 value.SetF(f);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700421 SetPrimitiveField<Primitive::kPrimFloat>(env, javaField, javaObj, value);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800422}
423
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700424static void Field_setInt(JNIEnv* env, jobject javaField, jobject javaObj, jint i) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700425 JValue value;
426 value.SetI(i);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700427 SetPrimitiveField<Primitive::kPrimInt>(env, javaField, javaObj, value);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800428}
429
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700430static void Field_setLong(JNIEnv* env, jobject javaField, jobject javaObj, jlong j) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700431 JValue value;
432 value.SetJ(j);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700433 SetPrimitiveField<Primitive::kPrimLong>(env, javaField, javaObj, value);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800434}
435
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700436static void Field_setShort(JNIEnv* env, jobject javaField, jobject javaObj, jshort s) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700437 JValue value;
438 value.SetS(s);
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700439 SetPrimitiveField<Primitive::kPrimShort>(env, javaField, javaObj, value);
Elliott Hughes33203b52011-09-20 19:42:01 -0700440}
441
Jeff Hao13e748b2015-08-25 20:44:19 +0000442static jobject Field_getAnnotationNative(JNIEnv* env, jobject javaField, jclass annotationType) {
443 ScopedFastNativeObjectAccess soa(env);
444 StackHandleScope<1> hs(soa.Self());
Mathieu Chartier0795f232016-09-27 18:43:30 -0700445 ArtField* field = soa.Decode<mirror::Field>(javaField)->GetArtField();
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000446 if (field->GetDeclaringClass()->IsProxyClass()) {
Jeff Hao13e748b2015-08-25 20:44:19 +0000447 return nullptr;
448 }
Mathieu Chartier0795f232016-09-27 18:43:30 -0700449 Handle<mirror::Class> klass(hs.NewHandle(soa.Decode<mirror::Class>(annotationType)));
David Sehr9323e6e2016-09-13 08:58:35 -0700450 return soa.AddLocalReference<jobject>(annotations::GetAnnotationForField(field, klass));
Jeff Hao13e748b2015-08-25 20:44:19 +0000451}
452
Orion Hodson3d617ac2016-10-19 14:00:46 +0100453static jlong Field_getArtField(JNIEnv* env, jobject javaField) {
454 ScopedFastNativeObjectAccess soa(env);
455 ArtField* field = soa.Decode<mirror::Field>(javaField)->GetArtField();
456 return reinterpret_cast<jlong>(field);
457}
458
Narayan Kamath6b2dc312017-03-14 13:26:12 +0000459static jobject Field_getNameInternal(JNIEnv* env, jobject javaField) {
460 ScopedFastNativeObjectAccess soa(env);
461 ArtField* field = soa.Decode<mirror::Field>(javaField)->GetArtField();
462 return soa.AddLocalReference<jobject>(
463 field->GetStringName(soa.Self(), true /* resolve */));
464}
465
Jeff Hao13e748b2015-08-25 20:44:19 +0000466static jobjectArray Field_getDeclaredAnnotations(JNIEnv* env, jobject javaField) {
467 ScopedFastNativeObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700468 ArtField* field = soa.Decode<mirror::Field>(javaField)->GetArtField();
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000469 if (field->GetDeclaringClass()->IsProxyClass()) {
Jeff Hao13e748b2015-08-25 20:44:19 +0000470 // Return an empty array instead of a null pointer.
Mathieu Chartier299958f2016-10-05 13:48:41 -0700471 ObjPtr<mirror::Class> annotation_array_class =
472 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_annotation_Annotation__array);
473 ObjPtr<mirror::ObjectArray<mirror::Object>> empty_array =
474 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), annotation_array_class.Ptr(), 0);
Jeff Hao13e748b2015-08-25 20:44:19 +0000475 return soa.AddLocalReference<jobjectArray>(empty_array);
476 }
David Sehr9323e6e2016-09-13 08:58:35 -0700477 return soa.AddLocalReference<jobjectArray>(annotations::GetAnnotationsForField(field));
Jeff Hao13e748b2015-08-25 20:44:19 +0000478}
479
480static jobjectArray Field_getSignatureAnnotation(JNIEnv* env, jobject javaField) {
481 ScopedFastNativeObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700482 ArtField* field = soa.Decode<mirror::Field>(javaField)->GetArtField();
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000483 if (field->GetDeclaringClass()->IsProxyClass()) {
Jeff Hao13e748b2015-08-25 20:44:19 +0000484 return nullptr;
485 }
David Sehr9323e6e2016-09-13 08:58:35 -0700486 return soa.AddLocalReference<jobjectArray>(annotations::GetSignatureAnnotationForField(field));
Jeff Hao13e748b2015-08-25 20:44:19 +0000487}
488
Mathieu Chartier299958f2016-10-05 13:48:41 -0700489static jboolean Field_isAnnotationPresentNative(JNIEnv* env,
490 jobject javaField,
Jeff Hao13e748b2015-08-25 20:44:19 +0000491 jclass annotationType) {
492 ScopedFastNativeObjectAccess soa(env);
493 StackHandleScope<1> hs(soa.Self());
Mathieu Chartier0795f232016-09-27 18:43:30 -0700494 ArtField* field = soa.Decode<mirror::Field>(javaField)->GetArtField();
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000495 if (field->GetDeclaringClass()->IsProxyClass()) {
Jeff Hao13e748b2015-08-25 20:44:19 +0000496 return false;
497 }
Mathieu Chartier0795f232016-09-27 18:43:30 -0700498 Handle<mirror::Class> klass(hs.NewHandle(soa.Decode<mirror::Class>(annotationType)));
David Sehr9323e6e2016-09-13 08:58:35 -0700499 return annotations::IsFieldAnnotationPresent(field, klass);
Jeff Hao13e748b2015-08-25 20:44:19 +0000500}
501
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700502static JNINativeMethod gMethods[] = {
Igor Murashkin3b6f4402017-02-16 16:13:17 -0800503 FAST_NATIVE_METHOD(Field, get, "(Ljava/lang/Object;)Ljava/lang/Object;"),
504 FAST_NATIVE_METHOD(Field, getBoolean, "(Ljava/lang/Object;)Z"),
505 FAST_NATIVE_METHOD(Field, getByte, "(Ljava/lang/Object;)B"),
506 FAST_NATIVE_METHOD(Field, getChar, "(Ljava/lang/Object;)C"),
507 FAST_NATIVE_METHOD(Field, getAnnotationNative,
508 "(Ljava/lang/Class;)Ljava/lang/annotation/Annotation;"),
509 FAST_NATIVE_METHOD(Field, getArtField, "()J"),
510 FAST_NATIVE_METHOD(Field, getDeclaredAnnotations, "()[Ljava/lang/annotation/Annotation;"),
511 FAST_NATIVE_METHOD(Field, getSignatureAnnotation, "()[Ljava/lang/String;"),
512 FAST_NATIVE_METHOD(Field, getDouble, "(Ljava/lang/Object;)D"),
513 FAST_NATIVE_METHOD(Field, getFloat, "(Ljava/lang/Object;)F"),
514 FAST_NATIVE_METHOD(Field, getInt, "(Ljava/lang/Object;)I"),
515 FAST_NATIVE_METHOD(Field, getLong, "(Ljava/lang/Object;)J"),
Narayan Kamath6b2dc312017-03-14 13:26:12 +0000516 FAST_NATIVE_METHOD(Field, getNameInternal, "()Ljava/lang/String;"),
Igor Murashkin3b6f4402017-02-16 16:13:17 -0800517 FAST_NATIVE_METHOD(Field, getShort, "(Ljava/lang/Object;)S"),
518 FAST_NATIVE_METHOD(Field, isAnnotationPresentNative, "(Ljava/lang/Class;)Z"),
519 FAST_NATIVE_METHOD(Field, set, "(Ljava/lang/Object;Ljava/lang/Object;)V"),
520 FAST_NATIVE_METHOD(Field, setBoolean, "(Ljava/lang/Object;Z)V"),
521 FAST_NATIVE_METHOD(Field, setByte, "(Ljava/lang/Object;B)V"),
522 FAST_NATIVE_METHOD(Field, setChar, "(Ljava/lang/Object;C)V"),
523 FAST_NATIVE_METHOD(Field, setDouble, "(Ljava/lang/Object;D)V"),
524 FAST_NATIVE_METHOD(Field, setFloat, "(Ljava/lang/Object;F)V"),
525 FAST_NATIVE_METHOD(Field, setInt, "(Ljava/lang/Object;I)V"),
526 FAST_NATIVE_METHOD(Field, setLong, "(Ljava/lang/Object;J)V"),
527 FAST_NATIVE_METHOD(Field, setShort, "(Ljava/lang/Object;S)V"),
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700528};
529
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700530void register_java_lang_reflect_Field(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700531 REGISTER_NATIVE_METHODS("java/lang/reflect/Field");
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700532}
533
534} // namespace art