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