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