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