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