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 | |
| 17 | #include "jni_internal.h" |
| 18 | #include "class_linker.h" |
| 19 | #include "object.h" |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 20 | #include "reflection.h" |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 21 | |
| 22 | #include "JniConstants.h" // Last to avoid problems with LOG redefinition. |
| 23 | |
| 24 | namespace art { |
| 25 | |
| 26 | namespace { |
| 27 | |
| 28 | jint Field_getFieldModifiers(JNIEnv* env, jobject jfield, jclass javaDeclaringClass, jint slot) { |
Elliott Hughes | 582a7d1 | 2011-10-10 18:38:42 -0700 | [diff] [blame^] | 29 | return Decode<Object*>(env, jfield)->AsField()->GetAccessFlags() & kAccJavaFlagsMask; |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 30 | } |
| 31 | |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 32 | bool GetFieldValue(Object* o, Field* f, JValue& value, bool allow_references) { |
| 33 | switch (f->GetType()->GetPrimitiveType()) { |
| 34 | case Class::kPrimBoolean: |
| 35 | value.z = f->GetBoolean(o); |
| 36 | return true; |
| 37 | case Class::kPrimByte: |
| 38 | value.b = f->GetByte(o); |
| 39 | return true; |
| 40 | case Class::kPrimChar: |
| 41 | value.c = f->GetChar(o); |
| 42 | return true; |
| 43 | case Class::kPrimDouble: |
| 44 | value.d = f->GetDouble(o); |
| 45 | return true; |
| 46 | case Class::kPrimFloat: |
| 47 | value.f = f->GetFloat(o); |
| 48 | return true; |
| 49 | case Class::kPrimInt: |
| 50 | value.i = f->GetInt(o); |
| 51 | return true; |
| 52 | case Class::kPrimLong: |
| 53 | value.j = f->GetLong(o); |
| 54 | return true; |
| 55 | case Class::kPrimShort: |
| 56 | value.s = f->GetShort(o); |
| 57 | return true; |
| 58 | case Class::kPrimNot: |
| 59 | if (allow_references) { |
| 60 | value.l = f->GetObject(o); |
| 61 | return true; |
| 62 | } |
| 63 | // Else break to report an error. |
| 64 | break; |
| 65 | case Class::kPrimVoid: |
| 66 | // Never okay. |
| 67 | break; |
| 68 | } |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 69 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;", |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 70 | "Not a primitive field: %s", PrettyField(f).c_str()); |
| 71 | return false; |
| 72 | } |
| 73 | |
Elliott Hughes | ed1c1e3 | 2011-10-02 14:31:05 -0700 | [diff] [blame] | 74 | bool CheckReceiver(JNIEnv* env, jobject javaObj, jclass javaDeclaringClass, Field* f, Object*& o) { |
| 75 | if (f->IsStatic()) { |
| 76 | o = NULL; |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | o = Decode<Object*>(env, javaObj); |
| 81 | Class* declaringClass = Decode<Class*>(env, javaDeclaringClass); |
| 82 | if (!VerifyObjectInClass(env, o, declaringClass)) { |
| 83 | return false; |
| 84 | } |
| 85 | return true; |
| 86 | } |
| 87 | |
Elliott Hughes | 582a7d1 | 2011-10-10 18:38:42 -0700 | [diff] [blame^] | 88 | JValue GetPrimitiveField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jchar dst_descriptor) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 89 | Field* f = DecodeField(env->FromReflectedField(javaField)); |
Elliott Hughes | ed1c1e3 | 2011-10-02 14:31:05 -0700 | [diff] [blame] | 90 | Object* o = NULL; |
| 91 | if (!CheckReceiver(env, javaObj, javaDeclaringClass, f, o)) { |
| 92 | return JValue(); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | // Read the value. |
| 96 | JValue field_value; |
| 97 | if (!GetFieldValue(o, f, field_value, false)) { |
| 98 | return JValue(); |
| 99 | } |
| 100 | |
| 101 | // Widen it if necessary (and possible). |
| 102 | JValue wide_value; |
Elliott Hughes | 582a7d1 | 2011-10-10 18:38:42 -0700 | [diff] [blame^] | 103 | Class* dst_type = Runtime::Current()->GetClassLinker()->FindPrimitiveClass(dst_descriptor); |
| 104 | if (!ConvertPrimitiveValue(f->GetType(), dst_type, field_value, wide_value)) { |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 105 | return JValue(); |
| 106 | } |
| 107 | return wide_value; |
| 108 | } |
| 109 | |
Elliott Hughes | 582a7d1 | 2011-10-10 18:38:42 -0700 | [diff] [blame^] | 110 | jbyte Field_getBField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar dst_descriptor) { |
| 111 | return GetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, dst_descriptor).b; |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Elliott Hughes | 582a7d1 | 2011-10-10 18:38:42 -0700 | [diff] [blame^] | 114 | jchar Field_getCField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar dst_descriptor) { |
| 115 | return GetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, dst_descriptor).c; |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Elliott Hughes | 582a7d1 | 2011-10-10 18:38:42 -0700 | [diff] [blame^] | 118 | jdouble Field_getDField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar dst_descriptor) { |
| 119 | return GetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, dst_descriptor).d; |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Elliott Hughes | 582a7d1 | 2011-10-10 18:38:42 -0700 | [diff] [blame^] | 122 | jfloat Field_getFField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar dst_descriptor) { |
| 123 | return GetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, dst_descriptor).f; |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Elliott Hughes | 582a7d1 | 2011-10-10 18:38:42 -0700 | [diff] [blame^] | 126 | jint Field_getIField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar dst_descriptor) { |
| 127 | return GetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, dst_descriptor).i; |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Elliott Hughes | 582a7d1 | 2011-10-10 18:38:42 -0700 | [diff] [blame^] | 130 | jlong Field_getJField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar dst_descriptor) { |
| 131 | return GetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, dst_descriptor).j; |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Elliott Hughes | 582a7d1 | 2011-10-10 18:38:42 -0700 | [diff] [blame^] | 134 | jshort Field_getSField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar dst_descriptor) { |
| 135 | return GetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, dst_descriptor).s; |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Elliott Hughes | 582a7d1 | 2011-10-10 18:38:42 -0700 | [diff] [blame^] | 138 | jboolean Field_getZField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar dst_descriptor) { |
| 139 | return GetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, dst_descriptor).z; |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | void SetFieldValue(Object* o, Field* f, const JValue& new_value, bool allow_references) { |
| 143 | switch (f->GetType()->GetPrimitiveType()) { |
| 144 | case Class::kPrimBoolean: |
| 145 | f->SetBoolean(o, new_value.z); |
Elliott Hughes | fe6207f | 2011-09-26 17:24:06 -0700 | [diff] [blame] | 146 | break; |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 147 | case Class::kPrimByte: |
| 148 | f->SetByte(o, new_value.b); |
Elliott Hughes | fe6207f | 2011-09-26 17:24:06 -0700 | [diff] [blame] | 149 | break; |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 150 | case Class::kPrimChar: |
| 151 | f->SetChar(o, new_value.c); |
Elliott Hughes | fe6207f | 2011-09-26 17:24:06 -0700 | [diff] [blame] | 152 | break; |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 153 | case Class::kPrimDouble: |
| 154 | f->SetDouble(o, new_value.d); |
Elliott Hughes | fe6207f | 2011-09-26 17:24:06 -0700 | [diff] [blame] | 155 | break; |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 156 | case Class::kPrimFloat: |
| 157 | f->SetFloat(o, new_value.f); |
Elliott Hughes | fe6207f | 2011-09-26 17:24:06 -0700 | [diff] [blame] | 158 | break; |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 159 | case Class::kPrimInt: |
| 160 | f->SetInt(o, new_value.i); |
Elliott Hughes | fe6207f | 2011-09-26 17:24:06 -0700 | [diff] [blame] | 161 | break; |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 162 | case Class::kPrimLong: |
| 163 | f->SetLong(o, new_value.j); |
Elliott Hughes | fe6207f | 2011-09-26 17:24:06 -0700 | [diff] [blame] | 164 | break; |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 165 | case Class::kPrimShort: |
| 166 | f->SetShort(o, new_value.s); |
Elliott Hughes | fe6207f | 2011-09-26 17:24:06 -0700 | [diff] [blame] | 167 | break; |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 168 | case Class::kPrimNot: |
| 169 | if (allow_references) { |
| 170 | f->SetObject(o, new_value.l); |
Elliott Hughes | fe6207f | 2011-09-26 17:24:06 -0700 | [diff] [blame] | 171 | break; |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 172 | } |
Elliott Hughes | fe6207f | 2011-09-26 17:24:06 -0700 | [diff] [blame] | 173 | // Else fall through to report an error. |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 174 | case Class::kPrimVoid: |
| 175 | // Never okay. |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 176 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;", |
Elliott Hughes | fe6207f | 2011-09-26 17:24:06 -0700 | [diff] [blame] | 177 | "Not a primitive field: %s", PrettyField(f).c_str()); |
| 178 | return; |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 179 | } |
Elliott Hughes | fe6207f | 2011-09-26 17:24:06 -0700 | [diff] [blame] | 180 | |
| 181 | // Special handling for final fields on SMP systems. |
| 182 | // We need a store/store barrier here (JMM requirement). |
| 183 | if (f->IsFinal()) { |
| 184 | ANDROID_MEMBAR_STORE(); |
| 185 | } |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Elliott Hughes | 582a7d1 | 2011-10-10 18:38:42 -0700 | [diff] [blame^] | 188 | void SetPrimitiveField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jchar src_descriptor, const JValue& new_value) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 189 | Field* f = DecodeField(env->FromReflectedField(javaField)); |
Elliott Hughes | ed1c1e3 | 2011-10-02 14:31:05 -0700 | [diff] [blame] | 190 | Object* o = NULL; |
| 191 | if (!CheckReceiver(env, javaObj, javaDeclaringClass, f, o)) { |
| 192 | return; |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | // Widen the value if necessary (and possible). |
| 196 | JValue wide_value; |
Elliott Hughes | 582a7d1 | 2011-10-10 18:38:42 -0700 | [diff] [blame^] | 197 | Class* src_type = Runtime::Current()->GetClassLinker()->FindPrimitiveClass(src_descriptor); |
| 198 | if (!ConvertPrimitiveValue(src_type, f->GetType(), new_value, wide_value)) { |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 199 | return; |
| 200 | } |
| 201 | |
| 202 | // Write the value. |
| 203 | SetFieldValue(o, f, wide_value, false); |
| 204 | } |
| 205 | |
Elliott Hughes | 582a7d1 | 2011-10-10 18:38:42 -0700 | [diff] [blame^] | 206 | void Field_setBField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar src_descriptor, jbyte value) { |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 207 | JValue v = { 0 }; |
| 208 | v.b = value; |
Elliott Hughes | 582a7d1 | 2011-10-10 18:38:42 -0700 | [diff] [blame^] | 209 | SetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, src_descriptor, v); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Elliott Hughes | 582a7d1 | 2011-10-10 18:38:42 -0700 | [diff] [blame^] | 212 | void Field_setCField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar src_descriptor, jchar value) { |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 213 | JValue v = { 0 }; |
| 214 | v.c = value; |
Elliott Hughes | 582a7d1 | 2011-10-10 18:38:42 -0700 | [diff] [blame^] | 215 | SetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, src_descriptor, v); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Elliott Hughes | 582a7d1 | 2011-10-10 18:38:42 -0700 | [diff] [blame^] | 218 | void Field_setDField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar src_descriptor, jdouble value) { |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 219 | JValue v = { 0 }; |
| 220 | v.d = value; |
Elliott Hughes | 582a7d1 | 2011-10-10 18:38:42 -0700 | [diff] [blame^] | 221 | SetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, src_descriptor, v); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Elliott Hughes | 582a7d1 | 2011-10-10 18:38:42 -0700 | [diff] [blame^] | 224 | void Field_setFField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar src_descriptor, jfloat value) { |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 225 | JValue v = { 0 }; |
| 226 | v.f = value; |
Elliott Hughes | 582a7d1 | 2011-10-10 18:38:42 -0700 | [diff] [blame^] | 227 | SetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, src_descriptor, v); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Elliott Hughes | 582a7d1 | 2011-10-10 18:38:42 -0700 | [diff] [blame^] | 230 | void Field_setIField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar src_descriptor, jint value) { |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 231 | JValue v = { 0 }; |
| 232 | v.i = value; |
Elliott Hughes | 582a7d1 | 2011-10-10 18:38:42 -0700 | [diff] [blame^] | 233 | SetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, src_descriptor, v); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Elliott Hughes | 582a7d1 | 2011-10-10 18:38:42 -0700 | [diff] [blame^] | 236 | void Field_setJField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar src_descriptor, jlong value) { |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 237 | JValue v = { 0 }; |
| 238 | v.j = value; |
Elliott Hughes | 582a7d1 | 2011-10-10 18:38:42 -0700 | [diff] [blame^] | 239 | SetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, src_descriptor, v); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Elliott Hughes | 582a7d1 | 2011-10-10 18:38:42 -0700 | [diff] [blame^] | 242 | void Field_setSField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar src_descriptor, jshort value) { |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 243 | JValue v = { 0 }; |
| 244 | v.s = value; |
Elliott Hughes | 582a7d1 | 2011-10-10 18:38:42 -0700 | [diff] [blame^] | 245 | SetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, src_descriptor, v); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Elliott Hughes | 582a7d1 | 2011-10-10 18:38:42 -0700 | [diff] [blame^] | 248 | void Field_setZField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jchar src_descriptor, jboolean value) { |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 249 | JValue v = { 0 }; |
| 250 | v.z = value; |
Elliott Hughes | 582a7d1 | 2011-10-10 18:38:42 -0700 | [diff] [blame^] | 251 | SetPrimitiveField(env, javaField, javaObj, javaDeclaringClass, src_descriptor, v); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | void Field_setField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean, jobject javaValue) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 255 | Field* f = DecodeField(env->FromReflectedField(javaField)); |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 256 | |
| 257 | // Unbox the value, if necessary. |
| 258 | Object* boxed_value = Decode<Object*>(env, javaValue); |
| 259 | JValue unboxed_value; |
| 260 | if (!UnboxPrimitive(env, boxed_value, f->GetType(), unboxed_value)) { |
| 261 | return; |
| 262 | } |
| 263 | |
| 264 | // Check that the receiver is non-null and an instance of the field's declaring class. |
Elliott Hughes | ed1c1e3 | 2011-10-02 14:31:05 -0700 | [diff] [blame] | 265 | Object* o = NULL; |
| 266 | if (!CheckReceiver(env, javaObj, javaDeclaringClass, f, o)) { |
| 267 | return; |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | SetFieldValue(o, f, unboxed_value, true); |
| 271 | } |
| 272 | |
| 273 | jobject Field_getField(JNIEnv* env, jobject javaField, jobject javaObj, jclass javaDeclaringClass, jclass, jint, jboolean) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 274 | Field* f = DecodeField(env->FromReflectedField(javaField)); |
Elliott Hughes | ed1c1e3 | 2011-10-02 14:31:05 -0700 | [diff] [blame] | 275 | Object* o = NULL; |
| 276 | if (!CheckReceiver(env, javaObj, javaDeclaringClass, f, o)) { |
| 277 | return NULL; |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | // Get the field's value, boxing if necessary. |
| 281 | JValue value; |
| 282 | if (!GetFieldValue(o, f, value, true)) { |
| 283 | return NULL; |
| 284 | } |
| 285 | BoxPrimitive(env, f->GetType(), value); |
| 286 | |
| 287 | return AddLocalReference<jobject>(env, value.l); |
| 288 | } |
| 289 | |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 290 | static JNINativeMethod gMethods[] = { |
Elliott Hughes | 33203b5 | 2011-09-20 19:42:01 -0700 | [diff] [blame] | 291 | NATIVE_METHOD(Field, getFieldModifiers, "(Ljava/lang/Class;I)I"), |
| 292 | |
| 293 | NATIVE_METHOD(Field, getBField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZC)B"), |
| 294 | NATIVE_METHOD(Field, getCField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZC)C"), |
| 295 | NATIVE_METHOD(Field, getDField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZC)D"), |
| 296 | NATIVE_METHOD(Field, getFField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZC)F"), |
| 297 | NATIVE_METHOD(Field, getField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZ)Ljava/lang/Object;"), |
| 298 | NATIVE_METHOD(Field, getIField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZC)I"), |
| 299 | NATIVE_METHOD(Field, getJField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZC)J"), |
| 300 | NATIVE_METHOD(Field, getSField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZC)S"), |
| 301 | NATIVE_METHOD(Field, getZField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZC)Z"), |
| 302 | NATIVE_METHOD(Field, setBField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZCB)V"), |
| 303 | NATIVE_METHOD(Field, setCField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZCC)V"), |
| 304 | NATIVE_METHOD(Field, setDField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZCD)V"), |
| 305 | NATIVE_METHOD(Field, setFField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZCF)V"), |
| 306 | NATIVE_METHOD(Field, setField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZLjava/lang/Object;)V"), |
| 307 | NATIVE_METHOD(Field, setIField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZCI)V"), |
| 308 | NATIVE_METHOD(Field, setJField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZCJ)V"), |
| 309 | NATIVE_METHOD(Field, setSField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZCS)V"), |
| 310 | NATIVE_METHOD(Field, setZField, "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Class;IZCZ)V"), |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 311 | }; |
| 312 | |
| 313 | } // namespace |
| 314 | |
| 315 | void register_java_lang_reflect_Field(JNIEnv* env) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 316 | InitBoxingMethods(env); // TODO: move to Runtime? |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 317 | jniRegisterNativeMethods(env, "java/lang/reflect/Field", gMethods, NELEM(gMethods)); |
| 318 | } |
| 319 | |
| 320 | } // namespace art |