blob: d9c9e3141abfcedbe9aec2d64fa1d77d1cf4805d [file] [log] [blame]
Shih-wei Liao2d831012011-09-28 22:06:53 -07001/*
Elliott Hughes0f3c5532012-03-30 14:51:51 -07002 * Copyright (C) 2012 The Android Open Source Project
Shih-wei Liao2d831012011-09-28 22:06:53 -07003 *
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
Ian Rogers7655f292013-07-29 11:07:13 -070017#include "entrypoints/entrypoint_utils.h"
Shih-wei Liao2d831012011-09-28 22:06:53 -070018
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080019#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070020#include "dex_file-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070021#include "gc/accounting/card_table-inl.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070022#include "mirror/art_field-inl.h"
23#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080024#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "mirror/object-inl.h"
Dragos Sbirleabd136a22013-08-13 18:07:04 -070026#include "object_utils.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "mirror/object_array-inl.h"
28#include "mirror/proxy.h"
Ian Rogersaf6e67a2013-01-16 08:38:37 -080029#include "reflection.h"
30#include "scoped_thread_state_change.h"
TDYa1275bb86012012-04-11 05:57:28 -070031#include "ScopedLocalRef.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070032#include "well_known_classes.h"
TDYa1275bb86012012-04-11 05:57:28 -070033
jeffhao41005dd2012-05-09 17:58:52 -070034namespace art {
35
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070036static inline bool CheckFilledNewArrayAlloc(uint32_t type_idx, mirror::ArtMethod* referrer,
37 int32_t component_count, Thread* self,
38 bool access_check, mirror::Class** klass_ptr)
39 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -070040 if (UNLIKELY(component_count < 0)) {
Ian Rogers62d6c772013-02-27 08:32:07 -080041 ThrowNegativeArraySizeException(component_count);
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070042 return false; // Failure
Elliott Hughes6c8867d2011-10-03 16:34:05 -070043 }
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070044 mirror::Class* klass = referrer->GetDexCacheResolvedTypes()->GetWithoutChecks(type_idx);
Ian Rogers57b86d42012-03-27 16:05:41 -070045 if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve
Ian Rogers62d6c772013-02-27 08:32:07 -080046 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, referrer);
Ian Rogers57b86d42012-03-27 16:05:41 -070047 if (klass == NULL) { // Error
Ian Rogers50b35e22012-10-04 10:09:15 -070048 DCHECK(self->IsExceptionPending());
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070049 return false; // Failure
Ian Rogers19846512012-02-24 11:42:47 -080050 }
Ian Rogersea2a11d2011-10-11 16:48:51 -070051 }
Ian Rogers57b86d42012-03-27 16:05:41 -070052 if (UNLIKELY(klass->IsPrimitive() && !klass->IsPrimitiveInt())) {
53 if (klass->IsPrimitiveLong() || klass->IsPrimitiveDouble()) {
Ian Rogers62d6c772013-02-27 08:32:07 -080054 ThrowRuntimeException("Bad filled array request for type %s",
55 PrettyDescriptor(klass).c_str());
Ian Rogers573db4a2011-12-13 15:30:50 -080056 } else {
Ian Rogers62d6c772013-02-27 08:32:07 -080057 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
58 DCHECK(throw_location.GetMethod() == referrer);
59 self->ThrowNewExceptionF(throw_location, "Ljava/lang/InternalError;",
Ian Rogers50b35e22012-10-04 10:09:15 -070060 "Found type %s; filled-new-array not implemented for anything but \'int\'",
61 PrettyDescriptor(klass).c_str());
Ian Rogers573db4a2011-12-13 15:30:50 -080062 }
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070063 return false; // Failure
Ian Rogers57b86d42012-03-27 16:05:41 -070064 }
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070065 if (access_check) {
66 mirror::Class* referrer_klass = referrer->GetDeclaringClass();
67 if (UNLIKELY(!referrer_klass->CanAccess(klass))) {
68 ThrowIllegalAccessErrorClass(referrer_klass, klass);
69 return false; // Failure
70 }
71 }
72 DCHECK(klass->IsArrayClass()) << PrettyClass(klass);
73 *klass_ptr = klass;
74 return true;
75}
76
77// Helper function to allocate array for FILLED_NEW_ARRAY.
78mirror::Array* CheckAndAllocArrayFromCode(uint32_t type_idx, mirror::ArtMethod* referrer,
79 int32_t component_count, Thread* self,
80 bool access_check) {
81 mirror::Class* klass;
82 if (UNLIKELY(!CheckFilledNewArrayAlloc(type_idx, referrer, component_count, self, access_check, &klass))) {
83 return NULL;
84 }
85 return mirror::Array::AllocUninstrumented(self, klass, component_count);
86}
87
88// Helper function to allocate array for FILLED_NEW_ARRAY.
89mirror::Array* CheckAndAllocArrayFromCodeInstrumented(uint32_t type_idx, mirror::ArtMethod* referrer,
90 int32_t component_count, Thread* self,
91 bool access_check) {
92 mirror::Class* klass;
93 if (UNLIKELY(!CheckFilledNewArrayAlloc(type_idx, referrer, component_count, self, access_check, &klass))) {
94 return NULL;
95 }
96 return mirror::Array::AllocInstrumented(self, klass, component_count);
Ian Rogers57b86d42012-03-27 16:05:41 -070097}
98
Brian Carlstromea46f952013-07-30 01:26:50 -070099mirror::ArtField* FindFieldFromCode(uint32_t field_idx, const mirror::ArtMethod* referrer,
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200100 Thread* self, FindFieldType type, size_t expected_size,
101 bool access_check) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700102 bool is_primitive;
103 bool is_set;
104 bool is_static;
105 switch (type) {
106 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
107 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
108 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
109 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
110 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
111 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
112 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
113 case StaticPrimitiveWrite: // Keep GCC happy by having a default handler, fall-through.
114 default: is_primitive = true; is_set = true; is_static = true; break;
115 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700116 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromea46f952013-07-30 01:26:50 -0700117 mirror::ArtField* resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
Ian Rogers57b86d42012-03-27 16:05:41 -0700118 if (UNLIKELY(resolved_field == NULL)) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700119 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
120 return NULL; // Failure.
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200121 }
122 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
123 if (access_check) {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200124 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700125 ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer);
Ian Rogers08f753d2012-08-24 14:35:25 -0700126 return NULL;
127 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800128 mirror::Class* referring_class = referrer->GetDeclaringClass();
Ian Rogerse2645d32012-04-11 14:42:42 -0700129 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
130 !referring_class->CanAccessMember(fields_class,
131 resolved_field->GetAccessFlags()))) {
132 // The referring class can't access the resolved field, this may occur as a result of a
133 // protected field being made public by a sub-class. Resort to the dex file to determine
134 // the correct class for the access check.
Ian Rogers4445a7e2012-10-05 17:19:13 -0700135 const DexFile& dex_file = *referring_class->GetDexCache()->GetDexFile();
Ian Rogerse2645d32012-04-11 14:42:42 -0700136 fields_class = class_linker->ResolveType(dex_file,
137 dex_file.GetFieldId(field_idx).class_idx_,
138 referring_class);
139 if (UNLIKELY(!referring_class->CanAccess(fields_class))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700140 ThrowIllegalAccessErrorClass(referring_class, fields_class);
Ian Rogerse2645d32012-04-11 14:42:42 -0700141 return NULL; // failure
142 } else if (UNLIKELY(!referring_class->CanAccessMember(fields_class,
143 resolved_field->GetAccessFlags()))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700144 ThrowIllegalAccessErrorField(referring_class, resolved_field);
Ian Rogerse2645d32012-04-11 14:42:42 -0700145 return NULL; // failure
146 }
147 }
148 if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700149 ThrowIllegalAccessErrorFinalField(referrer, resolved_field);
Ian Rogers57b86d42012-03-27 16:05:41 -0700150 return NULL; // failure
151 } else {
152 FieldHelper fh(resolved_field);
153 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
154 fh.FieldSize() != expected_size)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800155 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
156 DCHECK(throw_location.GetMethod() == referrer);
157 self->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
Ian Rogers57b86d42012-03-27 16:05:41 -0700158 "Attempted read of %zd-bit %s on field '%s'",
159 expected_size * (32 / sizeof(int32_t)),
160 is_primitive ? "primitive" : "non-primitive",
161 PrettyField(resolved_field, true).c_str());
162 return NULL; // failure
Ian Rogers60db5ab2012-02-20 17:02:00 -0800163 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700164 }
165 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200166 if (!is_static) {
167 // instance fields must be being accessed on an initialized class
168 return resolved_field;
169 } else {
170 // If the class is initialized we're done.
171 if (fields_class->IsInitialized()) {
172 return resolved_field;
173 } else if (Runtime::Current()->GetClassLinker()->EnsureInitialized(fields_class, true, true)) {
174 // Otherwise let's ensure the class is initialized before resolving the field.
175 return resolved_field;
176 } else {
177 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
178 return NULL; // failure
179 }
180 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700181}
182
183// Slow path method resolution
Brian Carlstromea46f952013-07-30 01:26:50 -0700184mirror::ArtMethod* FindMethodFromCode(uint32_t method_idx, mirror::Object* this_object,
185 mirror::ArtMethod* referrer,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800186 Thread* self, bool access_check, InvokeType type) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700187 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
188 bool is_direct = type == kStatic || type == kDirect;
Brian Carlstromea46f952013-07-30 01:26:50 -0700189 mirror::ArtMethod* resolved_method = class_linker->ResolveMethod(method_idx, referrer, type);
Ian Rogers57b86d42012-03-27 16:05:41 -0700190 if (UNLIKELY(resolved_method == NULL)) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700191 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
192 return NULL; // Failure.
jeffhao262e2512012-12-11 09:46:43 -0800193 } else if (UNLIKELY(this_object == NULL && type != kStatic)) {
194 // Maintain interpreter-like semantics where NullPointerException is thrown
195 // after potential NoSuchMethodError from class linker.
Ian Rogers62d6c772013-02-27 08:32:07 -0800196 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
197 DCHECK(referrer == throw_location.GetMethod());
198 ThrowNullPointerExceptionForMethodAccess(throw_location, method_idx, type);
jeffhao262e2512012-12-11 09:46:43 -0800199 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700200 } else {
201 if (!access_check) {
202 if (is_direct) {
203 return resolved_method;
204 } else if (type == kInterface) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700205 mirror::ArtMethod* interface_method =
Ian Rogers57b86d42012-03-27 16:05:41 -0700206 this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
207 if (UNLIKELY(interface_method == NULL)) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700208 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method, this_object,
209 referrer);
Ian Rogers08f753d2012-08-24 14:35:25 -0700210 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700211 } else {
212 return interface_method;
213 }
Ian Rogers60db5ab2012-02-20 17:02:00 -0800214 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700215 mirror::ObjectArray<mirror::ArtMethod>* vtable;
Ian Rogers57b86d42012-03-27 16:05:41 -0700216 uint16_t vtable_index = resolved_method->GetMethodIndex();
217 if (type == kSuper) {
218 vtable = referrer->GetDeclaringClass()->GetSuperClass()->GetVTable();
219 } else {
220 vtable = this_object->GetClass()->GetVTable();
221 }
222 // TODO: eliminate bounds check?
223 return vtable->Get(vtable_index);
224 }
225 } else {
Ian Rogers08f753d2012-08-24 14:35:25 -0700226 // Incompatible class change should have been handled in resolve method.
227 if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) {
Ian Rogers2fc14272012-08-30 10:56:57 -0700228 ThrowIncompatibleClassChangeError(type, resolved_method->GetInvokeType(), resolved_method,
229 referrer);
Ian Rogers08f753d2012-08-24 14:35:25 -0700230 return NULL; // Failure.
231 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800232 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
233 mirror::Class* referring_class = referrer->GetDeclaringClass();
Ian Rogers57b86d42012-03-27 16:05:41 -0700234 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
235 !referring_class->CanAccessMember(methods_class,
236 resolved_method->GetAccessFlags()))) {
237 // The referring class can't access the resolved method, this may occur as a result of a
238 // protected method being made public by implementing an interface that re-declares the
239 // method public. Resort to the dex file to determine the correct class for the access check
Ian Rogers4445a7e2012-10-05 17:19:13 -0700240 const DexFile& dex_file = *referring_class->GetDexCache()->GetDexFile();
Ian Rogers57b86d42012-03-27 16:05:41 -0700241 methods_class = class_linker->ResolveType(dex_file,
242 dex_file.GetMethodId(method_idx).class_idx_,
243 referring_class);
244 if (UNLIKELY(!referring_class->CanAccess(methods_class))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700245 ThrowIllegalAccessErrorClassForMethodDispatch(referring_class, methods_class,
246 referrer, resolved_method, type);
Ian Rogers08f753d2012-08-24 14:35:25 -0700247 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700248 } else if (UNLIKELY(!referring_class->CanAccessMember(methods_class,
249 resolved_method->GetAccessFlags()))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700250 ThrowIllegalAccessErrorMethod(referring_class, resolved_method);
Ian Rogers08f753d2012-08-24 14:35:25 -0700251 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700252 }
253 }
254 if (is_direct) {
255 return resolved_method;
256 } else if (type == kInterface) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700257 mirror::ArtMethod* interface_method =
Ian Rogers57b86d42012-03-27 16:05:41 -0700258 this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
259 if (UNLIKELY(interface_method == NULL)) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700260 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method, this_object,
261 referrer);
Ian Rogers08f753d2012-08-24 14:35:25 -0700262 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700263 } else {
264 return interface_method;
265 }
266 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700267 mirror::ObjectArray<mirror::ArtMethod>* vtable;
Ian Rogers57b86d42012-03-27 16:05:41 -0700268 uint16_t vtable_index = resolved_method->GetMethodIndex();
269 if (type == kSuper) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800270 mirror::Class* super_class = referring_class->GetSuperClass();
Ian Rogers57b86d42012-03-27 16:05:41 -0700271 if (LIKELY(super_class != NULL)) {
272 vtable = referring_class->GetSuperClass()->GetVTable();
273 } else {
274 vtable = NULL;
275 }
276 } else {
277 vtable = this_object->GetClass()->GetVTable();
278 }
279 if (LIKELY(vtable != NULL &&
280 vtable_index < static_cast<uint32_t>(vtable->GetLength()))) {
281 return vtable->GetWithoutChecks(vtable_index);
282 } else {
Ian Rogers08f753d2012-08-24 14:35:25 -0700283 // Behavior to agree with that of the verifier.
284 MethodHelper mh(resolved_method);
285 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(), mh.GetName(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800286 mh.GetSignature());
Ian Rogers08f753d2012-08-24 14:35:25 -0700287 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700288 }
Ian Rogers60db5ab2012-02-20 17:02:00 -0800289 }
290 }
291 }
Ian Rogers60db5ab2012-02-20 17:02:00 -0800292}
293
jeffhaod7521322012-11-21 15:38:24 -0800294void ThrowStackOverflowError(Thread* self) {
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700295 if (self->IsHandlingStackOverflow()) {
296 LOG(ERROR) << "Recursive stack overflow.";
297 // We don't fail here because SetStackEndForStackOverflow will print better diagnostics.
298 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800299
300 if (Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()) {
301 // Remove extra entry pushed onto second stack during method tracing.
302 Runtime::Current()->GetInstrumentation()->PopMethodForUnwind(self, false);
jeffhaod7521322012-11-21 15:38:24 -0800303 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800304
jeffhaod7521322012-11-21 15:38:24 -0800305 self->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute.
306 JNIEnvExt* env = self->GetJniEnv();
307 std::string msg("stack size ");
308 msg += PrettySize(self->GetStackSize());
309 // Use low-level JNI routine and pre-baked error class to avoid class linking operations that
310 // would consume more stack.
311 int rc = ::art::ThrowNewException(env, WellKnownClasses::java_lang_StackOverflowError,
312 msg.c_str(), NULL);
313 if (rc != JNI_OK) {
314 // TODO: ThrowNewException failed presumably because of an OOME, we continue to throw the OOME
315 // or die in the CHECK below. We may want to throw a pre-baked StackOverflowError
316 // instead.
317 LOG(ERROR) << "Couldn't throw new StackOverflowError because JNI ThrowNew failed.";
318 CHECK(self->IsExceptionPending());
319 }
320 self->ResetDefaultStackEnd(); // Return to default stack size.
321}
322
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800323JValue InvokeProxyInvocationHandler(ScopedObjectAccessUnchecked& soa, const char* shorty,
324 jobject rcvr_jobj, jobject interface_method_jobj,
325 std::vector<jvalue>& args) {
326 DCHECK(soa.Env()->IsInstanceOf(rcvr_jobj, WellKnownClasses::java_lang_reflect_Proxy));
327
328 // Build argument array possibly triggering GC.
329 soa.Self()->AssertThreadSuspensionIsAllowable();
330 jobjectArray args_jobj = NULL;
331 const JValue zero;
332 if (args.size() > 0) {
333 args_jobj = soa.Env()->NewObjectArray(args.size(), WellKnownClasses::java_lang_Object, NULL);
334 if (args_jobj == NULL) {
335 CHECK(soa.Self()->IsExceptionPending());
336 return zero;
337 }
338 for (size_t i = 0; i < args.size(); ++i) {
339 if (shorty[i + 1] == 'L') {
340 jobject val = args.at(i).l;
341 soa.Env()->SetObjectArrayElement(args_jobj, i, val);
342 } else {
343 JValue jv;
344 jv.SetJ(args.at(i).j);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800345 mirror::Object* val = BoxPrimitive(Primitive::GetType(shorty[i + 1]), jv);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800346 if (val == NULL) {
347 CHECK(soa.Self()->IsExceptionPending());
348 return zero;
349 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800350 soa.Decode<mirror::ObjectArray<mirror::Object>* >(args_jobj)->Set(i, val);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800351 }
352 }
353 }
354
Brian Carlstromea46f952013-07-30 01:26:50 -0700355 // Call Proxy.invoke(Proxy proxy, ArtMethod method, Object[] args).
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800356 jvalue invocation_args[3];
357 invocation_args[0].l = rcvr_jobj;
358 invocation_args[1].l = interface_method_jobj;
359 invocation_args[2].l = args_jobj;
360 jobject result =
Brian Carlstromea46f952013-07-30 01:26:50 -0700361 soa.Env()->CallStaticObjectMethodA(WellKnownClasses::java_lang_reflect_Proxy,
362 WellKnownClasses::java_lang_reflect_Proxy_invoke,
363 invocation_args);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800364
365 // Unbox result and handle error conditions.
Ian Rogers62d6c772013-02-27 08:32:07 -0800366 if (LIKELY(!soa.Self()->IsExceptionPending())) {
367 if (shorty[0] == 'V' || (shorty[0] == 'L' && result == NULL)) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800368 // Do nothing.
369 return zero;
370 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800371 mirror::Object* result_ref = soa.Decode<mirror::Object*>(result);
Ian Rogers62d6c772013-02-27 08:32:07 -0800372 mirror::Object* rcvr = soa.Decode<mirror::Object*>(rcvr_jobj);
Brian Carlstromea46f952013-07-30 01:26:50 -0700373 mirror::ArtMethod* interface_method =
374 soa.Decode<mirror::ArtMethod*>(interface_method_jobj);
Ian Rogers62d6c772013-02-27 08:32:07 -0800375 mirror::Class* result_type = MethodHelper(interface_method).GetReturnType();
Brian Carlstromea46f952013-07-30 01:26:50 -0700376 mirror::ArtMethod* proxy_method;
Ian Rogers62d6c772013-02-27 08:32:07 -0800377 if (interface_method->GetDeclaringClass()->IsInterface()) {
378 proxy_method = rcvr->GetClass()->FindVirtualMethodForInterface(interface_method);
379 } else {
380 // Proxy dispatch to a method defined in Object.
381 DCHECK(interface_method->GetDeclaringClass()->IsObjectClass());
382 proxy_method = interface_method;
383 }
384 ThrowLocation throw_location(rcvr, proxy_method, -1);
385 JValue result_unboxed;
386 if (!UnboxPrimitiveForResult(throw_location, result_ref, result_type, result_unboxed)) {
Ian Rogers530f71c2013-02-22 23:29:00 -0800387 DCHECK(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800388 return zero;
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800389 }
390 return result_unboxed;
391 }
392 } else {
393 // In the case of checked exceptions that aren't declared, the exception must be wrapped by
394 // a UndeclaredThrowableException.
Ian Rogers62d6c772013-02-27 08:32:07 -0800395 mirror::Throwable* exception = soa.Self()->GetException(NULL);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800396 if (exception->IsCheckedException()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800397 mirror::Object* rcvr = soa.Decode<mirror::Object*>(rcvr_jobj);
398 mirror::SynthesizedProxyClass* proxy_class =
399 down_cast<mirror::SynthesizedProxyClass*>(rcvr->GetClass());
Brian Carlstromea46f952013-07-30 01:26:50 -0700400 mirror::ArtMethod* interface_method =
401 soa.Decode<mirror::ArtMethod*>(interface_method_jobj);
402 mirror::ArtMethod* proxy_method =
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800403 rcvr->GetClass()->FindVirtualMethodForInterface(interface_method);
404 int throws_index = -1;
405 size_t num_virt_methods = proxy_class->NumVirtualMethods();
406 for (size_t i = 0; i < num_virt_methods; i++) {
407 if (proxy_class->GetVirtualMethod(i) == proxy_method) {
408 throws_index = i;
409 break;
410 }
411 }
412 CHECK_NE(throws_index, -1);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800413 mirror::ObjectArray<mirror::Class>* declared_exceptions = proxy_class->GetThrows()->Get(throws_index);
414 mirror::Class* exception_class = exception->GetClass();
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800415 bool declares_exception = false;
416 for (int i = 0; i < declared_exceptions->GetLength() && !declares_exception; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800417 mirror::Class* declared_exception = declared_exceptions->Get(i);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800418 declares_exception = declared_exception->IsAssignableFrom(exception_class);
419 }
420 if (!declares_exception) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800421 ThrowLocation throw_location(rcvr, proxy_method, -1);
422 soa.Self()->ThrowNewWrappedException(throw_location,
423 "Ljava/lang/reflect/UndeclaredThrowableException;",
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800424 NULL);
425 }
426 }
427 return zero;
428 }
429}
Shih-wei Liao2d831012011-09-28 22:06:53 -0700430} // namespace art