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