Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 "reflection.h" |
| 18 | |
| 19 | #include "class_linker.h" |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 20 | #include "common_throws.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 21 | #include "dex_file-inl.h" |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 22 | #include "invoke_arg_array_builder.h" |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 23 | #include "jni_internal.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 24 | #include "mirror/art_field-inl.h" |
| 25 | #include "mirror/art_method-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 26 | #include "mirror/class.h" |
| 27 | #include "mirror/class-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 28 | #include "mirror/object_array.h" |
| 29 | #include "mirror/object_array-inl.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 30 | #include "object_utils.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 31 | #include "scoped_thread_state_change.h" |
| 32 | #include "well_known_classes.h" |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 33 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 34 | namespace art { |
| 35 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 36 | jobject InvokeMethod(const ScopedObjectAccess& soa, jobject javaMethod, jobject javaReceiver, |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 37 | jobject javaArgs) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 38 | jmethodID mid = soa.Env()->FromReflectedMethod(javaMethod); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 39 | mirror::ArtMethod* m = soa.DecodeMethod(mid); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 40 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 41 | mirror::Class* declaring_class = m->GetDeclaringClass(); |
Ian Rogers | 0045a29 | 2012-03-31 21:08:41 -0700 | [diff] [blame] | 42 | if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(declaring_class, true, true)) { |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 43 | return NULL; |
| 44 | } |
| 45 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 46 | mirror::Object* receiver = NULL; |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 47 | if (!m->IsStatic()) { |
| 48 | // 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] | 49 | receiver = soa.Decode<mirror::Object*>(javaReceiver); |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 50 | if (!VerifyObjectInClass(receiver, declaring_class)) { |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 51 | return NULL; |
| 52 | } |
| 53 | |
| 54 | // Find the actual implementation of the virtual method. |
| 55 | m = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(m); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 56 | mid = soa.EncodeMethod(m); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | // Get our arrays of arguments and their types, and check they're the same size. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 60 | mirror::ObjectArray<mirror::Object>* objects = |
| 61 | soa.Decode<mirror::ObjectArray<mirror::Object>*>(javaArgs); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 62 | MethodHelper mh(m); |
| 63 | const DexFile::TypeList* classes = mh.GetParameterTypeList(); |
| 64 | uint32_t classes_size = classes == NULL ? 0 : classes->Size(); |
| 65 | uint32_t arg_count = (objects != NULL) ? objects->GetLength() : 0; |
| 66 | if (arg_count != classes_size) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 67 | ThrowIllegalArgumentException(NULL, |
| 68 | StringPrintf("Wrong number of arguments; expected %d, got %d", |
| 69 | classes_size, arg_count).c_str()); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 70 | return NULL; |
| 71 | } |
| 72 | |
| 73 | // Translate javaArgs to a jvalue[]. |
| 74 | UniquePtr<jvalue[]> args(new jvalue[arg_count]); |
| 75 | JValue* decoded_args = reinterpret_cast<JValue*>(args.get()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 76 | for (uint32_t i = 0; i < arg_count; ++i) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 77 | mirror::Object* arg = objects->Get(i); |
| 78 | mirror::Class* dst_class = mh.GetClassFromTypeIdx(classes->GetTypeItem(i).type_idx_); |
Elliott Hughes | 37f7775 | 2012-05-21 15:12:47 -0700 | [diff] [blame] | 79 | if (!UnboxPrimitiveForArgument(arg, dst_class, decoded_args[i], m, i)) { |
| 80 | return NULL; |
| 81 | } |
| 82 | if (!dst_class->IsPrimitive()) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 83 | args[i].l = soa.AddLocalReference<jobject>(arg); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
| 87 | // Invoke the method. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 88 | JValue value(InvokeWithJValues(soa, javaReceiver, mid, args.get())); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 89 | |
| 90 | // Wrap any exception with "Ljava/lang/reflect/InvocationTargetException;" and return early. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 91 | if (soa.Self()->IsExceptionPending()) { |
| 92 | jthrowable th = soa.Env()->ExceptionOccurred(); |
| 93 | soa.Env()->ExceptionClear(); |
| 94 | jclass exception_class = soa.Env()->FindClass("java/lang/reflect/InvocationTargetException"); |
| 95 | jmethodID mid = soa.Env()->GetMethodID(exception_class, "<init>", "(Ljava/lang/Throwable;)V"); |
| 96 | jobject exception_instance = soa.Env()->NewObject(exception_class, mid, th); |
| 97 | soa.Env()->Throw(reinterpret_cast<jthrowable>(exception_instance)); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 98 | return NULL; |
| 99 | } |
| 100 | |
| 101 | // Box if necessary and return. |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 102 | return soa.AddLocalReference<jobject>(BoxPrimitive(mh.GetReturnType()->GetPrimitiveType(), value)); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 105 | bool VerifyObjectInClass(mirror::Object* o, mirror::Class* c) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 106 | if (o == NULL) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 107 | ThrowNullPointerException(NULL, "null receiver"); |
| 108 | return false; |
Elliott Hughes | b600b3f | 2012-03-14 13:57:24 -0700 | [diff] [blame] | 109 | } else if (!o->InstanceOf(c)) { |
Elliott Hughes | b600b3f | 2012-03-14 13:57:24 -0700 | [diff] [blame] | 110 | std::string expected_class_name(PrettyDescriptor(c)); |
| 111 | std::string actual_class_name(PrettyTypeOf(o)); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 112 | ThrowIllegalArgumentException(NULL, |
| 113 | StringPrintf("Expected receiver of type %s, but got %s", |
| 114 | expected_class_name.c_str(), |
| 115 | actual_class_name.c_str()).c_str()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 116 | return false; |
| 117 | } |
| 118 | return true; |
| 119 | } |
| 120 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 121 | bool ConvertPrimitiveValue(const ThrowLocation* throw_location, bool unbox_for_result, |
| 122 | Primitive::Type srcType, Primitive::Type dstType, |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 123 | const JValue& src, JValue& dst) { |
Jesse Wilson | c129a6b | 2011-11-24 14:47:46 -0500 | [diff] [blame] | 124 | CHECK(srcType != Primitive::kPrimNot && dstType != Primitive::kPrimNot); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 125 | switch (dstType) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 126 | case Primitive::kPrimBoolean: |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 127 | if (srcType == Primitive::kPrimBoolean) { |
| 128 | dst.SetZ(src.GetZ()); |
| 129 | return true; |
| 130 | } |
| 131 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 132 | case Primitive::kPrimChar: |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 133 | if (srcType == Primitive::kPrimChar) { |
| 134 | dst.SetC(src.GetC()); |
| 135 | return true; |
| 136 | } |
| 137 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 138 | case Primitive::kPrimByte: |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 139 | if (srcType == Primitive::kPrimByte) { |
| 140 | dst.SetB(src.GetB()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 141 | return true; |
| 142 | } |
| 143 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 144 | case Primitive::kPrimShort: |
| 145 | if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimShort) { |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 146 | dst.SetS(src.GetI()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 147 | return true; |
| 148 | } |
| 149 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 150 | case Primitive::kPrimInt: |
| 151 | if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar || |
| 152 | srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) { |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 153 | dst.SetI(src.GetI()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 154 | return true; |
| 155 | } |
| 156 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 157 | case Primitive::kPrimLong: |
| 158 | if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar || |
| 159 | srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) { |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 160 | dst.SetJ(src.GetI()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 161 | return true; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 162 | } else if (srcType == Primitive::kPrimLong) { |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 163 | dst.SetJ(src.GetJ()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 164 | return true; |
| 165 | } |
| 166 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 167 | case Primitive::kPrimFloat: |
| 168 | if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar || |
| 169 | srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) { |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 170 | dst.SetF(src.GetI()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 171 | return true; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 172 | } else if (srcType == Primitive::kPrimLong) { |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 173 | dst.SetF(src.GetJ()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 174 | return true; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 175 | } else if (srcType == Primitive::kPrimFloat) { |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 176 | dst.SetF(src.GetF()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 177 | return true; |
| 178 | } |
| 179 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 180 | case Primitive::kPrimDouble: |
| 181 | if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar || |
| 182 | srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) { |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 183 | dst.SetD(src.GetI()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 184 | return true; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 185 | } else if (srcType == Primitive::kPrimLong) { |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 186 | dst.SetD(src.GetJ()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 187 | return true; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 188 | } else if (srcType == Primitive::kPrimFloat) { |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 189 | dst.SetD(src.GetF()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 190 | return true; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 191 | } else if (srcType == Primitive::kPrimDouble) { |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 192 | dst.SetJ(src.GetJ()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 193 | return true; |
| 194 | } |
| 195 | break; |
| 196 | default: |
| 197 | break; |
| 198 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 199 | if (!unbox_for_result) { |
| 200 | ThrowIllegalArgumentException(throw_location, |
| 201 | StringPrintf("Invalid primitive conversion from %s to %s", |
| 202 | PrettyDescriptor(srcType).c_str(), |
| 203 | PrettyDescriptor(dstType).c_str()).c_str()); |
| 204 | } else { |
| 205 | ThrowClassCastException(throw_location, |
| 206 | StringPrintf("Couldn't convert result of type %s to %s", |
| 207 | PrettyDescriptor(srcType).c_str(), |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 208 | PrettyDescriptor(dstType).c_str()).c_str()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 209 | } |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 210 | return false; |
| 211 | } |
| 212 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 213 | mirror::Object* BoxPrimitive(Primitive::Type src_class, const JValue& value) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 214 | if (src_class == Primitive::kPrimNot) { |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 215 | return value.GetL(); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 218 | jmethodID m = NULL; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 219 | switch (src_class) { |
| 220 | case Primitive::kPrimBoolean: |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 221 | m = WellKnownClasses::java_lang_Boolean_valueOf; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 222 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 223 | case Primitive::kPrimByte: |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 224 | m = WellKnownClasses::java_lang_Byte_valueOf; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 225 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 226 | case Primitive::kPrimChar: |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 227 | m = WellKnownClasses::java_lang_Character_valueOf; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 228 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 229 | case Primitive::kPrimDouble: |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 230 | m = WellKnownClasses::java_lang_Double_valueOf; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 231 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 232 | case Primitive::kPrimFloat: |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 233 | m = WellKnownClasses::java_lang_Float_valueOf; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 234 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 235 | case Primitive::kPrimInt: |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 236 | m = WellKnownClasses::java_lang_Integer_valueOf; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 237 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 238 | case Primitive::kPrimLong: |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 239 | m = WellKnownClasses::java_lang_Long_valueOf; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 240 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 241 | case Primitive::kPrimShort: |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 242 | m = WellKnownClasses::java_lang_Short_valueOf; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 243 | break; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 244 | case Primitive::kPrimVoid: |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 245 | // There's no such thing as a void field, and void methods invoked via reflection return null. |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 246 | return NULL; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 247 | default: |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 248 | LOG(FATAL) << static_cast<int>(src_class); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 251 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 252 | if (kIsDebugBuild) { |
| 253 | CHECK_EQ(soa.Self()->GetState(), kRunnable); |
| 254 | } |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 255 | |
| 256 | ArgArray arg_array(NULL, 0); |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 257 | JValue result; |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 258 | if (src_class == Primitive::kPrimDouble || src_class == Primitive::kPrimLong) { |
| 259 | arg_array.AppendWide(value.GetJ()); |
| 260 | } else { |
| 261 | arg_array.Append(value.GetI()); |
| 262 | } |
| 263 | |
| 264 | soa.DecodeMethod(m)->Invoke(soa.Self(), arg_array.GetArray(), arg_array.GetNumBytes(), |
Jeff Hao | 6474d19 | 2013-03-26 14:08:09 -0700 | [diff] [blame] | 265 | &result, 'L'); |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 266 | return result.GetL(); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 269 | static std::string UnboxingFailureKind(mirror::ArtMethod* m, int index, mirror::ArtField* f) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 270 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 84a5bb4 | 2012-05-16 17:52:15 -0700 | [diff] [blame] | 271 | if (m != NULL && index != -1) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 272 | ++index; // Humans count from 1. |
Elliott Hughes | 84a5bb4 | 2012-05-16 17:52:15 -0700 | [diff] [blame] | 273 | return StringPrintf("method %s argument %d", PrettyMethod(m, false).c_str(), index); |
Elliott Hughes | aaa5edc | 2012-05-16 15:54:30 -0700 | [diff] [blame] | 274 | } |
| 275 | if (f != NULL) { |
| 276 | return "field " + PrettyField(f, false); |
| 277 | } |
| 278 | return "result"; |
| 279 | } |
| 280 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 281 | static bool UnboxPrimitive(const ThrowLocation* throw_location, mirror::Object* o, |
| 282 | mirror::Class* dst_class, JValue& unboxed_value, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 283 | mirror::ArtMethod* m, int index, mirror::ArtField* f) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 284 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 285 | bool unbox_for_result = (f == NULL) && (index == -1); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 286 | if (!dst_class->IsPrimitive()) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 287 | if (UNLIKELY(o != NULL && !o->InstanceOf(dst_class))) { |
| 288 | if (!unbox_for_result) { |
| 289 | ThrowIllegalArgumentException(throw_location, |
| 290 | StringPrintf("%s has type %s, got %s", |
| 291 | UnboxingFailureKind(m, index, f).c_str(), |
| 292 | PrettyDescriptor(dst_class).c_str(), |
| 293 | PrettyTypeOf(o).c_str()).c_str()); |
| 294 | } else { |
| 295 | ThrowClassCastException(throw_location, |
| 296 | StringPrintf("Couldn't convert result of type %s to %s", |
| 297 | PrettyTypeOf(o).c_str(), |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 298 | PrettyDescriptor(dst_class).c_str()).c_str()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 299 | } |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 300 | return false; |
| 301 | } |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 302 | unboxed_value.SetL(o); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 303 | return true; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 304 | } |
| 305 | if (UNLIKELY(dst_class->GetPrimitiveType() == Primitive::kPrimVoid)) { |
| 306 | ThrowIllegalArgumentException(throw_location, |
| 307 | StringPrintf("Can't unbox %s to void", |
| 308 | UnboxingFailureKind(m, index, f).c_str()).c_str()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 309 | return false; |
| 310 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 311 | if (UNLIKELY(o == NULL)) { |
| 312 | if (!unbox_for_result) { |
| 313 | ThrowIllegalArgumentException(throw_location, |
| 314 | StringPrintf("%s has type %s, got null", |
| 315 | UnboxingFailureKind(m, index, f).c_str(), |
| 316 | PrettyDescriptor(dst_class).c_str()).c_str()); |
| 317 | } else { |
| 318 | ThrowNullPointerException(throw_location, |
| 319 | StringPrintf("Expected to unbox a '%s' primitive type but was returned null", |
| 320 | PrettyDescriptor(dst_class).c_str()).c_str()); |
| 321 | } |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 322 | return false; |
| 323 | } |
| 324 | |
Elliott Hughes | 1d878f3 | 2012-04-11 15:17:54 -0700 | [diff] [blame] | 325 | JValue boxed_value; |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 326 | std::string src_descriptor(ClassHelper(o->GetClass()).GetDescriptor()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 327 | mirror::Class* src_class = NULL; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 328 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 329 | mirror::ArtField* primitive_field = o->GetClass()->GetIFields()->Get(0); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 330 | if (src_descriptor == "Ljava/lang/Boolean;") { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 331 | src_class = class_linker->FindPrimitiveClass('Z'); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 332 | boxed_value.SetZ(primitive_field->GetBoolean(o)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 333 | } else if (src_descriptor == "Ljava/lang/Byte;") { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 334 | src_class = class_linker->FindPrimitiveClass('B'); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 335 | boxed_value.SetB(primitive_field->GetByte(o)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 336 | } else if (src_descriptor == "Ljava/lang/Character;") { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 337 | src_class = class_linker->FindPrimitiveClass('C'); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 338 | boxed_value.SetC(primitive_field->GetChar(o)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 339 | } else if (src_descriptor == "Ljava/lang/Float;") { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 340 | src_class = class_linker->FindPrimitiveClass('F'); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 341 | boxed_value.SetF(primitive_field->GetFloat(o)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 342 | } else if (src_descriptor == "Ljava/lang/Double;") { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 343 | src_class = class_linker->FindPrimitiveClass('D'); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 344 | boxed_value.SetD(primitive_field->GetDouble(o)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 345 | } else if (src_descriptor == "Ljava/lang/Integer;") { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 346 | src_class = class_linker->FindPrimitiveClass('I'); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 347 | boxed_value.SetI(primitive_field->GetInt(o)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 348 | } else if (src_descriptor == "Ljava/lang/Long;") { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 349 | src_class = class_linker->FindPrimitiveClass('J'); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 350 | boxed_value.SetJ(primitive_field->GetLong(o)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 351 | } else if (src_descriptor == "Ljava/lang/Short;") { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 352 | src_class = class_linker->FindPrimitiveClass('S'); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 353 | boxed_value.SetS(primitive_field->GetShort(o)); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 354 | } else { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 355 | ThrowIllegalArgumentException(throw_location, |
| 356 | StringPrintf("%s has type %s, got %s", |
| 357 | UnboxingFailureKind(m, index, f).c_str(), |
| 358 | PrettyDescriptor(dst_class).c_str(), |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 359 | PrettyDescriptor(src_descriptor.c_str()).c_str()).c_str()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 360 | return false; |
| 361 | } |
| 362 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 363 | return ConvertPrimitiveValue(throw_location, unbox_for_result, |
| 364 | src_class->GetPrimitiveType(), dst_class->GetPrimitiveType(), |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 365 | boxed_value, unboxed_value); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 368 | bool UnboxPrimitiveForArgument(mirror::Object* o, mirror::Class* dst_class, JValue& unboxed_value, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 369 | mirror::ArtMethod* m, size_t index) { |
Elliott Hughes | 84a5bb4 | 2012-05-16 17:52:15 -0700 | [diff] [blame] | 370 | CHECK(m != NULL); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 371 | return UnboxPrimitive(NULL, o, dst_class, unboxed_value, m, index, NULL); |
Elliott Hughes | aaa5edc | 2012-05-16 15:54:30 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 374 | bool UnboxPrimitiveForField(mirror::Object* o, mirror::Class* dst_class, JValue& unboxed_value, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 375 | mirror::ArtField* f) { |
Elliott Hughes | aaa5edc | 2012-05-16 15:54:30 -0700 | [diff] [blame] | 376 | CHECK(f != NULL); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 377 | return UnboxPrimitive(NULL, o, dst_class, unboxed_value, NULL, -1, f); |
Elliott Hughes | aaa5edc | 2012-05-16 15:54:30 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 380 | bool UnboxPrimitiveForResult(const ThrowLocation& throw_location, mirror::Object* o, |
| 381 | mirror::Class* dst_class, JValue& unboxed_value) { |
| 382 | return UnboxPrimitive(&throw_location, o, dst_class, unboxed_value, NULL, -1, NULL); |
Elliott Hughes | aaa5edc | 2012-05-16 15:54:30 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 385 | } // namespace art |