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 | |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 17 | #include "class_linker.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 18 | #include "class_linker-inl.h" |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 19 | #include "common_throws.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 20 | #include "dex_file-inl.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 21 | #include "jni_internal.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 22 | #include "mirror/art_field-inl.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 23 | #include "mirror/class-inl.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 24 | #include "object_utils.h" |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 25 | #include "reflection.h" |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 26 | #include "scoped_fast_native_object_access.h" |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 27 | |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 28 | namespace art { |
| 29 | |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 30 | static bool GetFieldValue(const ScopedFastNativeObjectAccess& soa, mirror::Object* o, |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame^] | 31 | mirror::ArtField* f, Primitive::Type field_type, bool allow_references, |
| 32 | JValue* value) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 33 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame^] | 34 | DCHECK_EQ(value->GetJ(), INT64_C(0)); |
| 35 | DCHECK(f->GetDeclaringClass()->IsInitialized()); |
| 36 | switch (field_type) { |
| 37 | case Primitive::kPrimBoolean: |
| 38 | value->SetZ(f->GetBoolean(o)); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 39 | return true; |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame^] | 40 | case Primitive::kPrimByte: |
| 41 | value->SetB(f->GetByte(o)); |
| 42 | return true; |
| 43 | case Primitive::kPrimChar: |
| 44 | value->SetC(f->GetChar(o)); |
| 45 | return true; |
| 46 | case Primitive::kPrimDouble: |
| 47 | value->SetD(f->GetDouble(o)); |
| 48 | return true; |
| 49 | case Primitive::kPrimFloat: |
| 50 | value->SetF(f->GetFloat(o)); |
| 51 | return true; |
| 52 | case Primitive::kPrimInt: |
| 53 | value->SetI(f->GetInt(o)); |
| 54 | return true; |
| 55 | case Primitive::kPrimLong: |
| 56 | value->SetJ(f->GetLong(o)); |
| 57 | return true; |
| 58 | case Primitive::kPrimShort: |
| 59 | value->SetS(f->GetShort(o)); |
| 60 | return true; |
| 61 | case Primitive::kPrimNot: |
| 62 | if (allow_references) { |
| 63 | value->SetL(f->GetObject(o)); |
| 64 | return true; |
| 65 | } |
| 66 | // Else break to report an error. |
| 67 | break; |
| 68 | case Primitive::kPrimVoid: |
| 69 | // Never okay. |
| 70 | break; |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 71 | } |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame^] | 72 | ThrowIllegalArgumentException(nullptr, StringPrintf("Not a primitive field: %s", |
| 73 | PrettyField(f).c_str()).c_str()); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 74 | return false; |
| 75 | } |
| 76 | |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 77 | static bool CheckReceiver(const ScopedFastNativeObjectAccess& soa, jobject j_rcvr, |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame^] | 78 | mirror::ArtField* f, mirror::Object** class_or_rcvr) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 79 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame^] | 80 | soa.Self()->AssertThreadSuspensionIsAllowable(); |
Elliott Hughes | ed1c1e3 | 2011-10-02 14:31:05 -0700 | [diff] [blame] | 81 | if (f->IsStatic()) { |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame^] | 82 | SirtRef<mirror::Class> sirt_klass(soa.Self(), f->GetDeclaringClass()); |
| 83 | if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(sirt_klass, true, true))) { |
| 84 | DCHECK(soa.Self()->IsExceptionPending()); |
| 85 | *class_or_rcvr = nullptr; |
| 86 | return false; |
| 87 | } |
| 88 | *class_or_rcvr = sirt_klass.get(); |
Elliott Hughes | ed1c1e3 | 2011-10-02 14:31:05 -0700 | [diff] [blame] | 89 | return true; |
| 90 | } |
| 91 | |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame^] | 92 | *class_or_rcvr = soa.Decode<mirror::Object*>(j_rcvr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 93 | mirror::Class* declaringClass = f->GetDeclaringClass(); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame^] | 94 | if (!VerifyObjectIsClass(*class_or_rcvr, declaringClass)) { |
| 95 | DCHECK(soa.Self()->IsExceptionPending()); |
| 96 | *class_or_rcvr = nullptr; |
Elliott Hughes | ed1c1e3 | 2011-10-02 14:31:05 -0700 | [diff] [blame] | 97 | return false; |
| 98 | } |
| 99 | return true; |
| 100 | } |
| 101 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 102 | static jobject Field_get(JNIEnv* env, jobject javaField, jobject javaObj) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 103 | ScopedFastNativeObjectAccess soa(env); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame^] | 104 | CHECK(!kMovingFields) << "CheckReceiver may trigger thread suspension for initialization"; |
| 105 | mirror::ArtField* f = mirror::ArtField::FromReflectedField(soa, javaField); |
| 106 | mirror::Object* o = nullptr; |
| 107 | if (!CheckReceiver(soa, javaObj, f, &o)) { |
| 108 | DCHECK(soa.Self()->IsExceptionPending()); |
| 109 | return nullptr; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 110 | } |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame^] | 111 | // We now don't expect suspension unless an exception is thrown. |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 112 | // Get the field's value, boxing if necessary. |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame^] | 113 | Primitive::Type field_type = FieldHelper(f).GetTypeAsPrimitiveType(); |
Elliott Hughes | 1d878f3 | 2012-04-11 15:17:54 -0700 | [diff] [blame] | 114 | JValue value; |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame^] | 115 | if (!GetFieldValue(soa, o, f, field_type, true, &value)) { |
| 116 | DCHECK(soa.Self()->IsExceptionPending()); |
| 117 | return nullptr; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 118 | } |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame^] | 119 | return soa.AddLocalReference<jobject>(BoxPrimitive(field_type, value)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 122 | static JValue GetPrimitiveField(JNIEnv* env, jobject javaField, jobject javaObj, |
| 123 | char dst_descriptor) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 124 | ScopedFastNativeObjectAccess soa(env); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame^] | 125 | CHECK(!kMovingFields) << "CheckReceiver may trigger thread suspension for initialization"; |
| 126 | mirror::ArtField* f = mirror::ArtField::FromReflectedField(soa, javaField); |
| 127 | mirror::Object* o = nullptr; |
| 128 | if (!CheckReceiver(soa, javaObj, f, &o)) { |
| 129 | DCHECK(soa.Self()->IsExceptionPending()); |
Elliott Hughes | ed1c1e3 | 2011-10-02 14:31:05 -0700 | [diff] [blame] | 130 | return JValue(); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 131 | } |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame^] | 132 | // We now don't expect suspension unless an exception is thrown. |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 133 | // Read the value. |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame^] | 134 | Primitive::Type field_type = FieldHelper(f).GetTypeAsPrimitiveType(); |
Elliott Hughes | 1d878f3 | 2012-04-11 15:17:54 -0700 | [diff] [blame] | 135 | JValue field_value; |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame^] | 136 | if (!GetFieldValue(soa, o, f, field_type, false, &field_value)) { |
| 137 | DCHECK(soa.Self()->IsExceptionPending()); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 138 | return JValue(); |
| 139 | } |
| 140 | |
| 141 | // Widen it if necessary (and possible). |
| 142 | JValue wide_value; |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame^] | 143 | if (!ConvertPrimitiveValue(NULL, false, field_type, Primitive::GetType(dst_descriptor), |
| 144 | field_value, wide_value)) { |
| 145 | DCHECK(soa.Self()->IsExceptionPending()); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 146 | return JValue(); |
| 147 | } |
| 148 | return wide_value; |
| 149 | } |
| 150 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 151 | static jboolean Field_getBoolean(JNIEnv* env, jobject javaField, jobject javaObj) { |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 152 | return GetPrimitiveField(env, javaField, javaObj, 'Z').GetZ(); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 155 | static jbyte Field_getByte(JNIEnv* env, jobject javaField, jobject javaObj) { |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 156 | return GetPrimitiveField(env, javaField, javaObj, 'B').GetB(); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 159 | static jchar Field_getChar(JNIEnv* env, jobject javaField, jobject javaObj) { |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 160 | return GetPrimitiveField(env, javaField, javaObj, 'C').GetC(); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 163 | static jdouble Field_getDouble(JNIEnv* env, jobject javaField, jobject javaObj) { |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 164 | return GetPrimitiveField(env, javaField, javaObj, 'D').GetD(); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 167 | static jfloat Field_getFloat(JNIEnv* env, jobject javaField, jobject javaObj) { |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 168 | return GetPrimitiveField(env, javaField, javaObj, 'F').GetF(); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 171 | static jint Field_getInt(JNIEnv* env, jobject javaField, jobject javaObj) { |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 172 | return GetPrimitiveField(env, javaField, javaObj, 'I').GetI(); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 175 | static jlong Field_getLong(JNIEnv* env, jobject javaField, jobject javaObj) { |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 176 | return GetPrimitiveField(env, javaField, javaObj, 'J').GetJ(); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 179 | static jshort Field_getShort(JNIEnv* env, jobject javaField, jobject javaObj) { |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 180 | return GetPrimitiveField(env, javaField, javaObj, 'S').GetS(); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 183 | static void SetFieldValue(ScopedFastNativeObjectAccess& soa, mirror::Object* o, |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame^] | 184 | mirror::ArtField* f, Primitive::Type field_type, bool allow_references, |
| 185 | const JValue& new_value) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 186 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame^] | 187 | DCHECK(f->GetDeclaringClass()->IsInitialized()); |
| 188 | switch (field_type) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 189 | case Primitive::kPrimBoolean: |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 190 | f->SetBoolean<false>(o, new_value.GetZ()); |
Elliott Hughes | fe6207f | 2011-09-26 17:24:06 -0700 | [diff] [blame] | 191 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 192 | case Primitive::kPrimByte: |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 193 | f->SetByte<false>(o, new_value.GetB()); |
Elliott Hughes | fe6207f | 2011-09-26 17:24:06 -0700 | [diff] [blame] | 194 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 195 | case Primitive::kPrimChar: |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 196 | f->SetChar<false>(o, new_value.GetC()); |
Elliott Hughes | fe6207f | 2011-09-26 17:24:06 -0700 | [diff] [blame] | 197 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 198 | case Primitive::kPrimDouble: |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 199 | f->SetDouble<false>(o, new_value.GetD()); |
Elliott Hughes | fe6207f | 2011-09-26 17:24:06 -0700 | [diff] [blame] | 200 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 201 | case Primitive::kPrimFloat: |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 202 | f->SetFloat<false>(o, new_value.GetF()); |
Elliott Hughes | fe6207f | 2011-09-26 17:24:06 -0700 | [diff] [blame] | 203 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 204 | case Primitive::kPrimInt: |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 205 | f->SetInt<false>(o, new_value.GetI()); |
Elliott Hughes | fe6207f | 2011-09-26 17:24:06 -0700 | [diff] [blame] | 206 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 207 | case Primitive::kPrimLong: |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 208 | f->SetLong<false>(o, new_value.GetJ()); |
Elliott Hughes | fe6207f | 2011-09-26 17:24:06 -0700 | [diff] [blame] | 209 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 210 | case Primitive::kPrimShort: |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 211 | f->SetShort<false>(o, new_value.GetS()); |
Elliott Hughes | fe6207f | 2011-09-26 17:24:06 -0700 | [diff] [blame] | 212 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 213 | case Primitive::kPrimNot: |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 214 | if (allow_references) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 215 | f->SetObject<false>(o, new_value.GetL()); |
Elliott Hughes | fe6207f | 2011-09-26 17:24:06 -0700 | [diff] [blame] | 216 | break; |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 217 | } |
Elliott Hughes | fe6207f | 2011-09-26 17:24:06 -0700 | [diff] [blame] | 218 | // Else fall through to report an error. |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 219 | case Primitive::kPrimVoid: |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 220 | // Never okay. |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame^] | 221 | ThrowIllegalArgumentException(nullptr, StringPrintf("Not a primitive field: %s", |
| 222 | PrettyField(f).c_str()).c_str()); |
Elliott Hughes | fe6207f | 2011-09-26 17:24:06 -0700 | [diff] [blame] | 223 | return; |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 224 | } |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 227 | static void Field_set(JNIEnv* env, jobject javaField, jobject javaObj, jobject javaValue) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 228 | ScopedFastNativeObjectAccess soa(env); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame^] | 229 | CHECK(!kMovingFields) << "CheckReceiver may trigger thread suspension for initialization"; |
| 230 | mirror::ArtField* f = mirror::ArtField::FromReflectedField(soa, javaField); |
| 231 | // Check that the receiver is non-null and an instance of the field's declaring class. |
| 232 | mirror::Object* o = nullptr; |
| 233 | if (!CheckReceiver(soa, javaObj, f, &o)) { |
| 234 | DCHECK(soa.Self()->IsExceptionPending()); |
| 235 | return; |
| 236 | } |
| 237 | Primitive::Type field_prim_type; |
| 238 | mirror::Class* field_type; |
| 239 | { |
| 240 | FieldHelper fh(f); |
| 241 | const char* field_type_desciptor = fh.GetTypeDescriptor(); |
| 242 | field_prim_type = Primitive::GetType(field_type_desciptor[0]); |
| 243 | if (field_prim_type == Primitive::kPrimNot) { |
| 244 | SirtRef<mirror::Object> sirt_obj(soa.Self(), o); |
| 245 | // May cause resolution. |
| 246 | CHECK(!kMovingFields) << "Resolution may trigger thread suspension"; |
| 247 | field_type = fh.GetType(true); |
| 248 | if (field_type == nullptr) { |
| 249 | DCHECK(soa.Self()->IsExceptionPending()); |
| 250 | return; |
| 251 | } |
| 252 | } else { |
| 253 | field_type = Runtime::Current()->GetClassLinker()->FindPrimitiveClass(field_type_desciptor[0]); |
| 254 | } |
| 255 | } |
| 256 | // We now don't expect suspension unless an exception is thrown. |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 257 | // Unbox the value, if necessary. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 258 | mirror::Object* boxed_value = soa.Decode<mirror::Object*>(javaValue); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 259 | JValue unboxed_value; |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame^] | 260 | if (!UnboxPrimitiveForField(boxed_value, field_type, unboxed_value, f)) { |
| 261 | DCHECK(soa.Self()->IsExceptionPending()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 262 | return; |
| 263 | } |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame^] | 264 | SetFieldValue(soa, o, f, field_prim_type, true, unboxed_value); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 265 | } |
| 266 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 267 | static void SetPrimitiveField(JNIEnv* env, jobject javaField, jobject javaObj, char src_descriptor, |
| 268 | const JValue& new_value) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 269 | ScopedFastNativeObjectAccess soa(env); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame^] | 270 | mirror::ArtField* f = mirror::ArtField::FromReflectedField(soa, javaField); |
| 271 | mirror::Object* o = nullptr; |
| 272 | if (!CheckReceiver(soa, javaObj, f, &o)) { |
Elliott Hughes | ed1c1e3 | 2011-10-02 14:31:05 -0700 | [diff] [blame] | 273 | return; |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 274 | } |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame^] | 275 | Primitive::Type field_type = FieldHelper(f).GetTypeAsPrimitiveType(); |
| 276 | if (UNLIKELY(field_type == Primitive::kPrimNot)) { |
| 277 | ThrowIllegalArgumentException(nullptr, StringPrintf("Not a primitive field: %s", |
| 278 | PrettyField(f).c_str()).c_str()); |
Jesse Wilson | c129a6b | 2011-11-24 14:47:46 -0500 | [diff] [blame] | 279 | return; |
| 280 | } |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 281 | |
| 282 | // Widen the value if necessary (and possible). |
| 283 | JValue wide_value; |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame^] | 284 | if (!ConvertPrimitiveValue(nullptr, false, Primitive::GetType(src_descriptor), |
| 285 | field_type, new_value, wide_value)) { |
| 286 | DCHECK(soa.Self()->IsExceptionPending()); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 287 | return; |
| 288 | } |
| 289 | |
| 290 | // Write the value. |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame^] | 291 | SetFieldValue(soa, o, f, field_type, false, wide_value); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 294 | static void Field_setBoolean(JNIEnv* env, jobject javaField, jobject javaObj, jboolean z) { |
| 295 | JValue value; |
| 296 | value.SetZ(z); |
| 297 | SetPrimitiveField(env, javaField, javaObj, 'Z', value); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 300 | static void Field_setByte(JNIEnv* env, jobject javaField, jobject javaObj, jbyte b) { |
| 301 | JValue value; |
| 302 | value.SetB(b); |
| 303 | SetPrimitiveField(env, javaField, javaObj, 'B', value); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 306 | static void Field_setChar(JNIEnv* env, jobject javaField, jobject javaObj, jchar c) { |
| 307 | JValue value; |
| 308 | value.SetC(c); |
| 309 | SetPrimitiveField(env, javaField, javaObj, 'C', value); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 310 | } |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 311 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 312 | static void Field_setDouble(JNIEnv* env, jobject javaField, jobject javaObj, jdouble d) { |
| 313 | JValue value; |
| 314 | value.SetD(d); |
| 315 | SetPrimitiveField(env, javaField, javaObj, 'D', value); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 316 | } |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 317 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 318 | static void Field_setFloat(JNIEnv* env, jobject javaField, jobject javaObj, jfloat f) { |
| 319 | JValue value; |
| 320 | value.SetF(f); |
| 321 | SetPrimitiveField(env, javaField, javaObj, 'F', value); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 322 | } |
| 323 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 324 | static void Field_setInt(JNIEnv* env, jobject javaField, jobject javaObj, jint i) { |
| 325 | JValue value; |
| 326 | value.SetI(i); |
| 327 | SetPrimitiveField(env, javaField, javaObj, 'I', value); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 328 | } |
| 329 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 330 | static void Field_setLong(JNIEnv* env, jobject javaField, jobject javaObj, jlong j) { |
| 331 | JValue value; |
| 332 | value.SetJ(j); |
| 333 | SetPrimitiveField(env, javaField, javaObj, 'J', value); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 334 | } |
| 335 | |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 336 | static void Field_setShort(JNIEnv* env, jobject javaField, jobject javaObj, jshort s) { |
| 337 | JValue value; |
| 338 | value.SetS(s); |
| 339 | SetPrimitiveField(env, javaField, javaObj, 'S', value); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 342 | static JNINativeMethod gMethods[] = { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 343 | NATIVE_METHOD(Field, get, "!(Ljava/lang/Object;)Ljava/lang/Object;"), |
| 344 | NATIVE_METHOD(Field, getBoolean, "!(Ljava/lang/Object;)Z"), |
| 345 | NATIVE_METHOD(Field, getByte, "!(Ljava/lang/Object;)B"), |
| 346 | NATIVE_METHOD(Field, getChar, "!(Ljava/lang/Object;)C"), |
| 347 | NATIVE_METHOD(Field, getDouble, "!(Ljava/lang/Object;)D"), |
| 348 | NATIVE_METHOD(Field, getFloat, "!(Ljava/lang/Object;)F"), |
| 349 | NATIVE_METHOD(Field, getInt, "!(Ljava/lang/Object;)I"), |
| 350 | NATIVE_METHOD(Field, getLong, "!(Ljava/lang/Object;)J"), |
| 351 | NATIVE_METHOD(Field, getShort, "!(Ljava/lang/Object;)S"), |
| 352 | NATIVE_METHOD(Field, set, "!(Ljava/lang/Object;Ljava/lang/Object;)V"), |
| 353 | NATIVE_METHOD(Field, setBoolean, "!(Ljava/lang/Object;Z)V"), |
| 354 | NATIVE_METHOD(Field, setByte, "!(Ljava/lang/Object;B)V"), |
| 355 | NATIVE_METHOD(Field, setChar, "!(Ljava/lang/Object;C)V"), |
| 356 | NATIVE_METHOD(Field, setDouble, "!(Ljava/lang/Object;D)V"), |
| 357 | NATIVE_METHOD(Field, setFloat, "!(Ljava/lang/Object;F)V"), |
| 358 | NATIVE_METHOD(Field, setInt, "!(Ljava/lang/Object;I)V"), |
| 359 | NATIVE_METHOD(Field, setLong, "!(Ljava/lang/Object;J)V"), |
| 360 | NATIVE_METHOD(Field, setShort, "!(Ljava/lang/Object;S)V"), |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 361 | }; |
| 362 | |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 363 | void register_java_lang_reflect_Field(JNIEnv* env) { |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 364 | REGISTER_NATIVE_METHODS("java/lang/reflect/Field"); |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | } // namespace art |