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