Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 1 | /* |
| 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 Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 17 | #include "java_lang_reflect_Field.h" |
| 18 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 19 | #include "android-base/stringprintf.h" |
Andreas Gampe | a14100c | 2017-04-24 15:09:56 -0700 | [diff] [blame] | 20 | #include "nativehelper/jni_macros.h" |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 21 | |
Andreas Gampe | a1d2f95 | 2017-04-20 22:53:58 -0700 | [diff] [blame] | 22 | #include "art_field-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 23 | #include "class_linker-inl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 24 | #include "class_linker.h" |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 25 | #include "common_throws.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 26 | #include "dex_file-inl.h" |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 27 | #include "dex_file_annotations.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 28 | #include "jni_internal.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 29 | #include "mirror/class-inl.h" |
Andreas Gampe | 895f922 | 2017-07-05 09:53:32 -0700 | [diff] [blame] | 30 | #include "mirror/field-inl.h" |
Andreas Gampe | 87583b3 | 2017-05-25 11:22:18 -0700 | [diff] [blame] | 31 | #include "native_util.h" |
Mathieu Chartier | 7643327 | 2014-09-26 14:32:37 -0700 | [diff] [blame] | 32 | #include "reflection-inl.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 33 | #include "scoped_fast_native_object_access-inl.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 34 | #include "utils.h" |
Andreas Gampe | a1d2f95 | 2017-04-20 22:53:58 -0700 | [diff] [blame] | 35 | #include "well_known_classes.h" |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 36 | |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 37 | namespace art { |
| 38 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 39 | using android::base::StringPrintf; |
| 40 | |
Mathieu Chartier | 7643327 | 2014-09-26 14:32:37 -0700 | [diff] [blame] | 41 | template<bool kIsSet> |
Mathieu Chartier | 299958f | 2016-10-05 13:48:41 -0700 | [diff] [blame] | 42 | ALWAYS_INLINE inline static bool VerifyFieldAccess(Thread* self, |
| 43 | ObjPtr<mirror::Field> field, |
| 44 | ObjPtr<mirror::Object> obj) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 45 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 7643327 | 2014-09-26 14:32:37 -0700 | [diff] [blame] | 46 | if (kIsSet && field->IsFinal()) { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 47 | ThrowIllegalAccessException( |
Andreas Gampe | c0d8229 | 2014-09-23 10:38:30 -0700 | [diff] [blame] | 48 | StringPrintf("Cannot set %s field %s of class %s", |
| 49 | PrettyJavaAccessFlags(field->GetAccessFlags()).c_str(), |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 50 | ArtField::PrettyField(field->GetArtField()).c_str(), |
Andreas Gampe | c0d8229 | 2014-09-23 10:38:30 -0700 | [diff] [blame] | 51 | field->GetDeclaringClass() == nullptr ? "null" : |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 52 | field->GetDeclaringClass()->PrettyClass().c_str()).c_str()); |
Jeff Hao | 11d5d8f | 2014-03-26 15:08:20 -0700 | [diff] [blame] | 53 | return false; |
| 54 | } |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 55 | ObjPtr<mirror::Class> calling_class; |
| 56 | if (!VerifyAccess(self, |
Mathieu Chartier | f8ac97f | 2016-10-05 15:56:52 -0700 | [diff] [blame] | 57 | obj, |
| 58 | field->GetDeclaringClass(), |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 59 | field->GetAccessFlags(), |
| 60 | &calling_class, |
| 61 | 1)) { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 62 | ThrowIllegalAccessException( |
Andreas Gampe | c0d8229 | 2014-09-23 10:38:30 -0700 | [diff] [blame] | 63 | StringPrintf("Class %s cannot access %s field %s of class %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 64 | calling_class == nullptr ? "null" : calling_class->PrettyClass().c_str(), |
Andreas Gampe | c0d8229 | 2014-09-23 10:38:30 -0700 | [diff] [blame] | 65 | PrettyJavaAccessFlags(field->GetAccessFlags()).c_str(), |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 66 | ArtField::PrettyField(field->GetArtField()).c_str(), |
Andreas Gampe | c0d8229 | 2014-09-23 10:38:30 -0700 | [diff] [blame] | 67 | field->GetDeclaringClass() == nullptr ? "null" : |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 68 | field->GetDeclaringClass()->PrettyClass().c_str()).c_str()); |
Jeff Hao | 11d5d8f | 2014-03-26 15:08:20 -0700 | [diff] [blame] | 69 | return false; |
| 70 | } |
| 71 | return true; |
| 72 | } |
| 73 | |
Mathieu Chartier | 7643327 | 2014-09-26 14:32:37 -0700 | [diff] [blame] | 74 | template<bool kAllowReferences> |
Mathieu Chartier | 299958f | 2016-10-05 13:48:41 -0700 | [diff] [blame] | 75 | ALWAYS_INLINE inline static bool GetFieldValue(ObjPtr<mirror::Object> o, |
| 76 | ObjPtr<mirror::Field> f, |
| 77 | Primitive::Type field_type, |
| 78 | JValue* value) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 79 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 80 | DCHECK_EQ(value->GetJ(), INT64_C(0)); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 81 | MemberOffset offset(f->GetOffset()); |
| 82 | const bool is_volatile = f->IsVolatile(); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 83 | switch (field_type) { |
| 84 | case Primitive::kPrimBoolean: |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 85 | value->SetZ(is_volatile ? o->GetFieldBooleanVolatile(offset) : o->GetFieldBoolean(offset)); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 86 | return true; |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 87 | case Primitive::kPrimByte: |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 88 | value->SetB(is_volatile ? o->GetFieldByteVolatile(offset) : o->GetFieldByte(offset)); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 89 | return true; |
| 90 | case Primitive::kPrimChar: |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 91 | value->SetC(is_volatile ? o->GetFieldCharVolatile(offset) : o->GetFieldChar(offset)); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 92 | return true; |
| 93 | case Primitive::kPrimInt: |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 94 | case Primitive::kPrimFloat: |
| 95 | value->SetI(is_volatile ? o->GetField32Volatile(offset) : o->GetField32(offset)); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 96 | return true; |
| 97 | case Primitive::kPrimLong: |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 98 | case Primitive::kPrimDouble: |
| 99 | value->SetJ(is_volatile ? o->GetField64Volatile(offset) : o->GetField64(offset)); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 100 | return true; |
| 101 | case Primitive::kPrimShort: |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 102 | value->SetS(is_volatile ? o->GetFieldShortVolatile(offset) : o->GetFieldShort(offset)); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 103 | return true; |
| 104 | case Primitive::kPrimNot: |
Mathieu Chartier | 7643327 | 2014-09-26 14:32:37 -0700 | [diff] [blame] | 105 | if (kAllowReferences) { |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 106 | value->SetL(is_volatile ? o->GetFieldObjectVolatile<mirror::Object>(offset) : |
| 107 | o->GetFieldObject<mirror::Object>(offset)); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 108 | return true; |
| 109 | } |
| 110 | // Else break to report an error. |
| 111 | break; |
| 112 | case Primitive::kPrimVoid: |
| 113 | // Never okay. |
| 114 | break; |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 115 | } |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 116 | ThrowIllegalArgumentException( |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 117 | StringPrintf("Not a primitive field: %s", |
| 118 | ArtField::PrettyField(f->GetArtField()).c_str()).c_str()); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 119 | return false; |
| 120 | } |
| 121 | |
Mathieu Chartier | 7643327 | 2014-09-26 14:32:37 -0700 | [diff] [blame] | 122 | ALWAYS_INLINE inline static bool CheckReceiver(const ScopedFastNativeObjectAccess& soa, |
Mathieu Chartier | 299958f | 2016-10-05 13:48:41 -0700 | [diff] [blame] | 123 | jobject j_rcvr, |
| 124 | ObjPtr<mirror::Field>* f, |
| 125 | ObjPtr<mirror::Object>* class_or_rcvr) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 126 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 127 | soa.Self()->AssertThreadSuspensionIsAllowable(); |
Mathieu Chartier | 299958f | 2016-10-05 13:48:41 -0700 | [diff] [blame] | 128 | ObjPtr<mirror::Class> declaring_class = (*f)->GetDeclaringClass(); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 129 | if ((*f)->IsStatic()) { |
Mathieu Chartier | 299958f | 2016-10-05 13:48:41 -0700 | [diff] [blame] | 130 | if (UNLIKELY(!declaring_class->IsInitialized())) { |
Mathieu Chartier | 7643327 | 2014-09-26 14:32:37 -0700 | [diff] [blame] | 131 | StackHandleScope<2> hs(soa.Self()); |
Mathieu Chartier | 299958f | 2016-10-05 13:48:41 -0700 | [diff] [blame] | 132 | HandleWrapperObjPtr<mirror::Field> h_f(hs.NewHandleWrapper(f)); |
| 133 | HandleWrapperObjPtr<mirror::Class> h_klass(hs.NewHandleWrapper(&declaring_class)); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 134 | ClassLinker* const class_linker = Runtime::Current()->GetClassLinker(); |
Mathieu Chartier | 7643327 | 2014-09-26 14:32:37 -0700 | [diff] [blame] | 135 | if (UNLIKELY(!class_linker->EnsureInitialized(soa.Self(), h_klass, true, true))) { |
| 136 | DCHECK(soa.Self()->IsExceptionPending()); |
| 137 | return false; |
| 138 | } |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 139 | } |
Mathieu Chartier | 299958f | 2016-10-05 13:48:41 -0700 | [diff] [blame] | 140 | *class_or_rcvr = declaring_class; |
Elliott Hughes | ed1c1e3 | 2011-10-02 14:31:05 -0700 | [diff] [blame] | 141 | return true; |
| 142 | } |
Mathieu Chartier | 299958f | 2016-10-05 13:48:41 -0700 | [diff] [blame] | 143 | *class_or_rcvr = soa.Decode<mirror::Object>(j_rcvr); |
| 144 | if (!VerifyObjectIsClass(*class_or_rcvr, declaring_class)) { |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 145 | DCHECK(soa.Self()->IsExceptionPending()); |
Elliott Hughes | ed1c1e3 | 2011-10-02 14:31:05 -0700 | [diff] [blame] | 146 | return false; |
| 147 | } |
| 148 | return true; |
| 149 | } |
| 150 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 151 | static jobject Field_get(JNIEnv* env, jobject javaField, jobject javaObj) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 152 | ScopedFastNativeObjectAccess soa(env); |
Mathieu Chartier | 299958f | 2016-10-05 13:48:41 -0700 | [diff] [blame] | 153 | ObjPtr<mirror::Field> f = soa.Decode<mirror::Field>(javaField); |
| 154 | ObjPtr<mirror::Object> o; |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 155 | if (!CheckReceiver(soa, javaObj, &f, &o)) { |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 156 | DCHECK(soa.Self()->IsExceptionPending()); |
| 157 | return nullptr; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 158 | } |
Jeff Hao | cb4581a | 2014-03-28 15:43:37 -0700 | [diff] [blame] | 159 | // If field is not set to be accessible, verify it can be accessed by the caller. |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 160 | if (!f->IsAccessible() && !VerifyFieldAccess<false>(soa.Self(), f, o)) { |
Jeff Hao | 11d5d8f | 2014-03-26 15:08:20 -0700 | [diff] [blame] | 161 | DCHECK(soa.Self()->IsExceptionPending()); |
| 162 | return nullptr; |
| 163 | } |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 164 | // We now don't expect suspension unless an exception is thrown. |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 165 | // Get the field's value, boxing if necessary. |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 166 | Primitive::Type field_type = f->GetTypeAsPrimitiveType(); |
Elliott Hughes | 1d878f3 | 2012-04-11 15:17:54 -0700 | [diff] [blame] | 167 | JValue value; |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 168 | if (!GetFieldValue<true>(o, f, field_type, &value)) { |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 169 | DCHECK(soa.Self()->IsExceptionPending()); |
| 170 | return nullptr; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 171 | } |
Mathieu Chartier | 299958f | 2016-10-05 13:48:41 -0700 | [diff] [blame] | 172 | return soa.AddLocalReference<jobject>(BoxPrimitive(field_type, value)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 173 | } |
| 174 | |
Mathieu Chartier | 7643327 | 2014-09-26 14:32:37 -0700 | [diff] [blame] | 175 | template<Primitive::Type kPrimitiveType> |
Mathieu Chartier | 299958f | 2016-10-05 13:48:41 -0700 | [diff] [blame] | 176 | ALWAYS_INLINE inline static JValue GetPrimitiveField(JNIEnv* env, |
| 177 | jobject javaField, |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 178 | jobject javaObj) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 179 | ScopedFastNativeObjectAccess soa(env); |
Mathieu Chartier | 299958f | 2016-10-05 13:48:41 -0700 | [diff] [blame] | 180 | ObjPtr<mirror::Field> f = soa.Decode<mirror::Field>(javaField); |
| 181 | ObjPtr<mirror::Object> o; |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 182 | if (!CheckReceiver(soa, javaObj, &f, &o)) { |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 183 | DCHECK(soa.Self()->IsExceptionPending()); |
Elliott Hughes | ed1c1e3 | 2011-10-02 14:31:05 -0700 | [diff] [blame] | 184 | return JValue(); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 185 | } |
Jeff Hao | 11d5d8f | 2014-03-26 15:08:20 -0700 | [diff] [blame] | 186 | |
Jeff Hao | cb4581a | 2014-03-28 15:43:37 -0700 | [diff] [blame] | 187 | // If field is not set to be accessible, verify it can be accessed by the caller. |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 188 | if (!f->IsAccessible() && !VerifyFieldAccess<false>(soa.Self(), f, o)) { |
Jeff Hao | 11d5d8f | 2014-03-26 15:08:20 -0700 | [diff] [blame] | 189 | DCHECK(soa.Self()->IsExceptionPending()); |
| 190 | return JValue(); |
| 191 | } |
| 192 | |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 193 | // We now don't expect suspension unless an exception is thrown. |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 194 | // Read the value. |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 195 | Primitive::Type field_type = f->GetTypeAsPrimitiveType(); |
Elliott Hughes | 1d878f3 | 2012-04-11 15:17:54 -0700 | [diff] [blame] | 196 | JValue field_value; |
Mathieu Chartier | 7643327 | 2014-09-26 14:32:37 -0700 | [diff] [blame] | 197 | if (field_type == kPrimitiveType) { |
| 198 | // This if statement should get optimized out since we only pass in valid primitive types. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 199 | if (UNLIKELY(!GetFieldValue<false>(o, f, kPrimitiveType, &field_value))) { |
Mathieu Chartier | 7643327 | 2014-09-26 14:32:37 -0700 | [diff] [blame] | 200 | DCHECK(soa.Self()->IsExceptionPending()); |
| 201 | return JValue(); |
| 202 | } |
| 203 | return field_value; |
| 204 | } |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 205 | if (!GetFieldValue<false>(o, f, field_type, &field_value)) { |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 206 | DCHECK(soa.Self()->IsExceptionPending()); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 207 | return JValue(); |
| 208 | } |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 209 | // Widen it if necessary (and possible). |
| 210 | JValue wide_value; |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 211 | if (!ConvertPrimitiveValue(false, field_type, kPrimitiveType, field_value, |
Mathieu Chartier | 7643327 | 2014-09-26 14:32:37 -0700 | [diff] [blame] | 212 | &wide_value)) { |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 213 | DCHECK(soa.Self()->IsExceptionPending()); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 214 | return JValue(); |
| 215 | } |
| 216 | return wide_value; |
| 217 | } |
| 218 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 219 | static jboolean Field_getBoolean(JNIEnv* env, jobject javaField, jobject javaObj) { |
| 220 | return GetPrimitiveField<Primitive::kPrimBoolean>(env, javaField, javaObj).GetZ(); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 223 | static jbyte Field_getByte(JNIEnv* env, jobject javaField, jobject javaObj) { |
| 224 | return GetPrimitiveField<Primitive::kPrimByte>(env, javaField, javaObj).GetB(); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 227 | static jchar Field_getChar(JNIEnv* env, jobject javaField, jobject javaObj) { |
| 228 | return GetPrimitiveField<Primitive::kPrimChar>(env, javaField, javaObj).GetC(); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 231 | static jdouble Field_getDouble(JNIEnv* env, jobject javaField, jobject javaObj) { |
| 232 | return GetPrimitiveField<Primitive::kPrimDouble>(env, javaField, javaObj).GetD(); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 235 | static jfloat Field_getFloat(JNIEnv* env, jobject javaField, jobject javaObj) { |
| 236 | return GetPrimitiveField<Primitive::kPrimFloat>(env, javaField, javaObj).GetF(); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 239 | static jint Field_getInt(JNIEnv* env, jobject javaField, jobject javaObj) { |
| 240 | return GetPrimitiveField<Primitive::kPrimInt>(env, javaField, javaObj).GetI(); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 243 | static jlong Field_getLong(JNIEnv* env, jobject javaField, jobject javaObj) { |
| 244 | return GetPrimitiveField<Primitive::kPrimLong>(env, javaField, javaObj).GetJ(); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 247 | static jshort Field_getShort(JNIEnv* env, jobject javaField, jobject javaObj) { |
| 248 | return GetPrimitiveField<Primitive::kPrimShort>(env, javaField, javaObj).GetS(); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Mathieu Chartier | 299958f | 2016-10-05 13:48:41 -0700 | [diff] [blame] | 251 | ALWAYS_INLINE inline static void SetFieldValue(ObjPtr<mirror::Object> o, |
| 252 | ObjPtr<mirror::Field> f, |
| 253 | Primitive::Type field_type, |
| 254 | bool allow_references, |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 255 | const JValue& new_value) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 256 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 257 | DCHECK(f->GetDeclaringClass()->IsInitialized()); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 258 | MemberOffset offset(f->GetOffset()); |
| 259 | const bool is_volatile = f->IsVolatile(); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 260 | switch (field_type) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 261 | case Primitive::kPrimBoolean: |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 262 | if (is_volatile) { |
| 263 | o->SetFieldBooleanVolatile<false>(offset, new_value.GetZ()); |
| 264 | } else { |
| 265 | o->SetFieldBoolean<false>(offset, new_value.GetZ()); |
| 266 | } |
Elliott Hughes | fe6207f | 2011-09-26 17:24:06 -0700 | [diff] [blame] | 267 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 268 | case Primitive::kPrimByte: |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 269 | if (is_volatile) { |
| 270 | o->SetFieldBooleanVolatile<false>(offset, new_value.GetB()); |
| 271 | } else { |
| 272 | o->SetFieldBoolean<false>(offset, new_value.GetB()); |
| 273 | } |
Elliott Hughes | fe6207f | 2011-09-26 17:24:06 -0700 | [diff] [blame] | 274 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 275 | case Primitive::kPrimChar: |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 276 | if (is_volatile) { |
Fredrik Roubert | 3152c82 | 2015-07-28 00:14:08 +0200 | [diff] [blame] | 277 | o->SetFieldCharVolatile<false>(offset, new_value.GetC()); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 278 | } else { |
Fredrik Roubert | 3152c82 | 2015-07-28 00:14:08 +0200 | [diff] [blame] | 279 | o->SetFieldChar<false>(offset, new_value.GetC()); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 280 | } |
Elliott Hughes | fe6207f | 2011-09-26 17:24:06 -0700 | [diff] [blame] | 281 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 282 | case Primitive::kPrimInt: |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 283 | 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 Hughes | fe6207f | 2011-09-26 17:24:06 -0700 | [diff] [blame] | 289 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 290 | case Primitive::kPrimLong: |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 291 | 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 Hughes | fe6207f | 2011-09-26 17:24:06 -0700 | [diff] [blame] | 297 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 298 | case Primitive::kPrimShort: |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 299 | if (is_volatile) { |
Mathieu Chartier | ca239af | 2015-03-29 18:27:50 -0700 | [diff] [blame] | 300 | o->SetFieldShortVolatile<false>(offset, new_value.GetS()); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 301 | } else { |
Mathieu Chartier | ca239af | 2015-03-29 18:27:50 -0700 | [diff] [blame] | 302 | o->SetFieldShort<false>(offset, new_value.GetS()); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 303 | } |
Elliott Hughes | fe6207f | 2011-09-26 17:24:06 -0700 | [diff] [blame] | 304 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 305 | case Primitive::kPrimNot: |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 306 | if (allow_references) { |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 307 | if (is_volatile) { |
| 308 | o->SetFieldObjectVolatile<false>(offset, new_value.GetL()); |
| 309 | } else { |
| 310 | o->SetFieldObject<false>(offset, new_value.GetL()); |
| 311 | } |
Elliott Hughes | fe6207f | 2011-09-26 17:24:06 -0700 | [diff] [blame] | 312 | break; |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 313 | } |
Elliott Hughes | fe6207f | 2011-09-26 17:24:06 -0700 | [diff] [blame] | 314 | // Else fall through to report an error. |
Ian Rogers | fc787ec | 2014-10-09 21:56:44 -0700 | [diff] [blame] | 315 | FALLTHROUGH_INTENDED; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 316 | case Primitive::kPrimVoid: |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 317 | // Never okay. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 318 | ThrowIllegalArgumentException( |
| 319 | StringPrintf("Not a primitive field: %s", |
| 320 | ArtField::PrettyField(f->GetArtField()).c_str()).c_str()); |
Elliott Hughes | fe6207f | 2011-09-26 17:24:06 -0700 | [diff] [blame] | 321 | return; |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 322 | } |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 325 | static void Field_set(JNIEnv* env, jobject javaField, jobject javaObj, jobject javaValue) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 326 | ScopedFastNativeObjectAccess soa(env); |
Mathieu Chartier | 299958f | 2016-10-05 13:48:41 -0700 | [diff] [blame] | 327 | ObjPtr<mirror::Field> f = soa.Decode<mirror::Field>(javaField); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 328 | // Check that the receiver is non-null and an instance of the field's declaring class. |
Mathieu Chartier | 299958f | 2016-10-05 13:48:41 -0700 | [diff] [blame] | 329 | ObjPtr<mirror::Object> o; |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 330 | if (!CheckReceiver(soa, javaObj, &f, &o)) { |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 331 | DCHECK(soa.Self()->IsExceptionPending()); |
| 332 | return; |
| 333 | } |
Mathieu Chartier | 299958f | 2016-10-05 13:48:41 -0700 | [diff] [blame] | 334 | ObjPtr<mirror::Class> field_type; |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 335 | const char* field_type_desciptor = f->GetArtField()->GetTypeDescriptor(); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 336 | Primitive::Type field_prim_type = Primitive::GetType(field_type_desciptor[0]); |
| 337 | if (field_prim_type == Primitive::kPrimNot) { |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 338 | field_type = f->GetType(); |
| 339 | DCHECK(field_type != nullptr); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 340 | } else { |
| 341 | field_type = Runtime::Current()->GetClassLinker()->FindPrimitiveClass(field_type_desciptor[0]); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 342 | } |
| 343 | // We now don't expect suspension unless an exception is thrown. |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 344 | // Unbox the value, if necessary. |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 345 | ObjPtr<mirror::Object> boxed_value = soa.Decode<mirror::Object>(javaValue); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 346 | JValue unboxed_value; |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 347 | if (!UnboxPrimitiveForField(boxed_value, |
Mathieu Chartier | f8ac97f | 2016-10-05 15:56:52 -0700 | [diff] [blame] | 348 | field_type, |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 349 | f->GetArtField(), |
| 350 | &unboxed_value)) { |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 351 | DCHECK(soa.Self()->IsExceptionPending()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 352 | return; |
| 353 | } |
Jeff Hao | cb4581a | 2014-03-28 15:43:37 -0700 | [diff] [blame] | 354 | // If field is not set to be accessible, verify it can be accessed by the caller. |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 355 | if (!f->IsAccessible() && !VerifyFieldAccess<true>(soa.Self(), f, o)) { |
Jeff Hao | 11d5d8f | 2014-03-26 15:08:20 -0700 | [diff] [blame] | 356 | DCHECK(soa.Self()->IsExceptionPending()); |
| 357 | return; |
| 358 | } |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 359 | SetFieldValue(o, f, field_prim_type, true, unboxed_value); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 360 | } |
| 361 | |
Mathieu Chartier | 7643327 | 2014-09-26 14:32:37 -0700 | [diff] [blame] | 362 | template<Primitive::Type kPrimitiveType> |
Mathieu Chartier | 299958f | 2016-10-05 13:48:41 -0700 | [diff] [blame] | 363 | static void SetPrimitiveField(JNIEnv* env, |
| 364 | jobject javaField, |
| 365 | jobject javaObj, |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 366 | const JValue& new_value) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 367 | ScopedFastNativeObjectAccess soa(env); |
Mathieu Chartier | 299958f | 2016-10-05 13:48:41 -0700 | [diff] [blame] | 368 | ObjPtr<mirror::Field> f = soa.Decode<mirror::Field>(javaField); |
| 369 | ObjPtr<mirror::Object> o; |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 370 | if (!CheckReceiver(soa, javaObj, &f, &o)) { |
Elliott Hughes | ed1c1e3 | 2011-10-02 14:31:05 -0700 | [diff] [blame] | 371 | return; |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 372 | } |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 373 | Primitive::Type field_type = f->GetTypeAsPrimitiveType(); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 374 | if (UNLIKELY(field_type == Primitive::kPrimNot)) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 375 | ThrowIllegalArgumentException( |
| 376 | StringPrintf("Not a primitive field: %s", |
| 377 | ArtField::PrettyField(f->GetArtField()).c_str()).c_str()); |
Jesse Wilson | c129a6b | 2011-11-24 14:47:46 -0500 | [diff] [blame] | 378 | return; |
| 379 | } |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 380 | |
| 381 | // Widen the value if necessary (and possible). |
| 382 | JValue wide_value; |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 383 | if (!ConvertPrimitiveValue(false, kPrimitiveType, field_type, new_value, &wide_value)) { |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 384 | DCHECK(soa.Self()->IsExceptionPending()); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 385 | return; |
| 386 | } |
| 387 | |
Jeff Hao | cb4581a | 2014-03-28 15:43:37 -0700 | [diff] [blame] | 388 | // If field is not set to be accessible, verify it can be accessed by the caller. |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 389 | if (!f->IsAccessible() && !VerifyFieldAccess<true>(soa.Self(), f, o)) { |
Jeff Hao | 11d5d8f | 2014-03-26 15:08:20 -0700 | [diff] [blame] | 390 | DCHECK(soa.Self()->IsExceptionPending()); |
| 391 | return; |
| 392 | } |
| 393 | |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 394 | // Write the value. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 395 | SetFieldValue(o, f, field_type, false, wide_value); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 396 | } |
| 397 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 398 | static void Field_setBoolean(JNIEnv* env, jobject javaField, jobject javaObj, jboolean z) { |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 399 | JValue value; |
| 400 | value.SetZ(z); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 401 | SetPrimitiveField<Primitive::kPrimBoolean>(env, javaField, javaObj, value); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 404 | static void Field_setByte(JNIEnv* env, jobject javaField, jobject javaObj, jbyte b) { |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 405 | JValue value; |
| 406 | value.SetB(b); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 407 | SetPrimitiveField<Primitive::kPrimByte>(env, javaField, javaObj, value); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 410 | static void Field_setChar(JNIEnv* env, jobject javaField, jobject javaObj, jchar c) { |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 411 | JValue value; |
| 412 | value.SetC(c); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 413 | SetPrimitiveField<Primitive::kPrimChar>(env, javaField, javaObj, value); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 414 | } |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 415 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 416 | static void Field_setDouble(JNIEnv* env, jobject javaField, jobject javaObj, jdouble d) { |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 417 | JValue value; |
| 418 | value.SetD(d); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 419 | SetPrimitiveField<Primitive::kPrimDouble>(env, javaField, javaObj, value); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 420 | } |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 421 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 422 | static void Field_setFloat(JNIEnv* env, jobject javaField, jobject javaObj, jfloat f) { |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 423 | JValue value; |
| 424 | value.SetF(f); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 425 | SetPrimitiveField<Primitive::kPrimFloat>(env, javaField, javaObj, value); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 426 | } |
| 427 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 428 | static void Field_setInt(JNIEnv* env, jobject javaField, jobject javaObj, jint i) { |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 429 | JValue value; |
| 430 | value.SetI(i); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 431 | SetPrimitiveField<Primitive::kPrimInt>(env, javaField, javaObj, value); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 432 | } |
| 433 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 434 | static void Field_setLong(JNIEnv* env, jobject javaField, jobject javaObj, jlong j) { |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 435 | JValue value; |
| 436 | value.SetJ(j); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 437 | SetPrimitiveField<Primitive::kPrimLong>(env, javaField, javaObj, value); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 438 | } |
| 439 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 440 | static void Field_setShort(JNIEnv* env, jobject javaField, jobject javaObj, jshort s) { |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 441 | JValue value; |
| 442 | value.SetS(s); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 443 | SetPrimitiveField<Primitive::kPrimShort>(env, javaField, javaObj, value); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 444 | } |
| 445 | |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 446 | static jobject Field_getAnnotationNative(JNIEnv* env, jobject javaField, jclass annotationType) { |
| 447 | ScopedFastNativeObjectAccess soa(env); |
| 448 | StackHandleScope<1> hs(soa.Self()); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 449 | ArtField* field = soa.Decode<mirror::Field>(javaField)->GetArtField(); |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 450 | if (field->GetDeclaringClass()->IsProxyClass()) { |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 451 | return nullptr; |
| 452 | } |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 453 | Handle<mirror::Class> klass(hs.NewHandle(soa.Decode<mirror::Class>(annotationType))); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 454 | return soa.AddLocalReference<jobject>(annotations::GetAnnotationForField(field, klass)); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Orion Hodson | 3d617ac | 2016-10-19 14:00:46 +0100 | [diff] [blame] | 457 | static 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 Kamath | 6b2dc31 | 2017-03-14 13:26:12 +0000 | [diff] [blame] | 463 | static 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 Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 470 | static jobjectArray Field_getDeclaredAnnotations(JNIEnv* env, jobject javaField) { |
| 471 | ScopedFastNativeObjectAccess soa(env); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 472 | ArtField* field = soa.Decode<mirror::Field>(javaField)->GetArtField(); |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 473 | if (field->GetDeclaringClass()->IsProxyClass()) { |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 474 | // Return an empty array instead of a null pointer. |
Mathieu Chartier | 299958f | 2016-10-05 13:48:41 -0700 | [diff] [blame] | 475 | 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 Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 479 | return soa.AddLocalReference<jobjectArray>(empty_array); |
| 480 | } |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 481 | return soa.AddLocalReference<jobjectArray>(annotations::GetAnnotationsForField(field)); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | static jobjectArray Field_getSignatureAnnotation(JNIEnv* env, jobject javaField) { |
| 485 | ScopedFastNativeObjectAccess soa(env); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 486 | ArtField* field = soa.Decode<mirror::Field>(javaField)->GetArtField(); |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 487 | if (field->GetDeclaringClass()->IsProxyClass()) { |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 488 | return nullptr; |
| 489 | } |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 490 | return soa.AddLocalReference<jobjectArray>(annotations::GetSignatureAnnotationForField(field)); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 491 | } |
| 492 | |
Mathieu Chartier | 299958f | 2016-10-05 13:48:41 -0700 | [diff] [blame] | 493 | static jboolean Field_isAnnotationPresentNative(JNIEnv* env, |
| 494 | jobject javaField, |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 495 | jclass annotationType) { |
| 496 | ScopedFastNativeObjectAccess soa(env); |
| 497 | StackHandleScope<1> hs(soa.Self()); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 498 | ArtField* field = soa.Decode<mirror::Field>(javaField)->GetArtField(); |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 499 | if (field->GetDeclaringClass()->IsProxyClass()) { |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 500 | return false; |
| 501 | } |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 502 | Handle<mirror::Class> klass(hs.NewHandle(soa.Decode<mirror::Class>(annotationType))); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 503 | return annotations::IsFieldAnnotationPresent(field, klass); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 504 | } |
| 505 | |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 506 | static JNINativeMethod gMethods[] = { |
Igor Murashkin | 3b6f440 | 2017-02-16 16:13:17 -0800 | [diff] [blame] | 507 | 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 Kamath | 6b2dc31 | 2017-03-14 13:26:12 +0000 | [diff] [blame] | 520 | FAST_NATIVE_METHOD(Field, getNameInternal, "()Ljava/lang/String;"), |
Igor Murashkin | 3b6f440 | 2017-02-16 16:13:17 -0800 | [diff] [blame] | 521 | 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 Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 532 | }; |
| 533 | |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 534 | void register_java_lang_reflect_Field(JNIEnv* env) { |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 535 | REGISTER_NATIVE_METHODS("java/lang/reflect/Field"); |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | } // namespace art |