blob: c29784151ca739e685800f450485d80e1c8b26e5 [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"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "mirror/abstract_method-inl.h"
23#include "mirror/class-inl.h"
24#include "mirror/field-inl.h"
25#include "mirror/object-inl.h"
26#include "mirror/object_array-inl.h"
27#include "mirror/proxy.h"
Ian Rogersaf6e67a2013-01-16 08:38:37 -080028#include "reflection.h"
29#include "scoped_thread_state_change.h"
TDYa1275bb86012012-04-11 05:57:28 -070030#include "ScopedLocalRef.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070031#include "well_known_classes.h"
TDYa1275bb86012012-04-11 05:57:28 -070032
jeffhao41005dd2012-05-09 17:58:52 -070033namespace art {
34
Ian Rogers57b86d42012-03-27 16:05:41 -070035// Helper function to allocate array for FILLED_NEW_ARRAY.
Ian Rogers62d6c772013-02-27 08:32:07 -080036mirror::Array* CheckAndAllocArrayFromCode(uint32_t type_idx, mirror::AbstractMethod* referrer,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037 int32_t component_count, Thread* self,
38 bool access_check) {
Ian Rogers57b86d42012-03-27 16:05:41 -070039 if (UNLIKELY(component_count < 0)) {
Ian Rogers62d6c772013-02-27 08:32:07 -080040 ThrowNegativeArraySizeException(component_count);
Ian Rogers57b86d42012-03-27 16:05:41 -070041 return NULL; // Failure
Elliott Hughes6c8867d2011-10-03 16:34:05 -070042 }
Ian Rogers62d6c772013-02-27 08:32:07 -080043 mirror::Class* klass = referrer->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers57b86d42012-03-27 16:05:41 -070044 if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve
Ian Rogers62d6c772013-02-27 08:32:07 -080045 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, referrer);
Ian Rogers57b86d42012-03-27 16:05:41 -070046 if (klass == NULL) { // Error
Ian Rogers50b35e22012-10-04 10:09:15 -070047 DCHECK(self->IsExceptionPending());
Ian Rogers57b86d42012-03-27 16:05:41 -070048 return NULL; // Failure
Ian Rogers19846512012-02-24 11:42:47 -080049 }
Ian Rogersea2a11d2011-10-11 16:48:51 -070050 }
Ian Rogers57b86d42012-03-27 16:05:41 -070051 if (UNLIKELY(klass->IsPrimitive() && !klass->IsPrimitiveInt())) {
52 if (klass->IsPrimitiveLong() || klass->IsPrimitiveDouble()) {
Ian Rogers62d6c772013-02-27 08:32:07 -080053 ThrowRuntimeException("Bad filled array request for type %s",
54 PrettyDescriptor(klass).c_str());
Ian Rogers573db4a2011-12-13 15:30:50 -080055 } else {
Ian Rogers62d6c772013-02-27 08:32:07 -080056 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
57 DCHECK(throw_location.GetMethod() == referrer);
58 self->ThrowNewExceptionF(throw_location, "Ljava/lang/InternalError;",
Ian Rogers50b35e22012-10-04 10:09:15 -070059 "Found type %s; filled-new-array not implemented for anything but \'int\'",
60 PrettyDescriptor(klass).c_str());
Ian Rogers573db4a2011-12-13 15:30:50 -080061 }
Ian Rogers57b86d42012-03-27 16:05:41 -070062 return NULL; // Failure
Ian Rogersad25ac52011-10-04 19:13:33 -070063 } else {
Ian Rogers57b86d42012-03-27 16:05:41 -070064 if (access_check) {
Ian Rogers62d6c772013-02-27 08:32:07 -080065 mirror::Class* referrer_klass = referrer->GetDeclaringClass();
66 if (UNLIKELY(!referrer_klass->CanAccess(klass))) {
67 ThrowIllegalAccessErrorClass(referrer_klass, klass);
Ian Rogers57b86d42012-03-27 16:05:41 -070068 return NULL; // Failure
69 }
Ian Rogers60db5ab2012-02-20 17:02:00 -080070 }
Ian Rogers57b86d42012-03-27 16:05:41 -070071 DCHECK(klass->IsArrayClass()) << PrettyClass(klass);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080072 return mirror::Array::Alloc(self, klass, component_count);
Ian Rogers57b86d42012-03-27 16:05:41 -070073 }
74}
75
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080076mirror::Field* FindFieldFromCode(uint32_t field_idx, const mirror::AbstractMethod* referrer,
Sebastien Hertz233ea8e2013-06-06 11:57:09 +020077 Thread* self, FindFieldType type, size_t expected_size,
78 bool access_check) {
Ian Rogers08f753d2012-08-24 14:35:25 -070079 bool is_primitive;
80 bool is_set;
81 bool is_static;
82 switch (type) {
83 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
84 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
85 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
86 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
87 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
88 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
89 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
90 case StaticPrimitiveWrite: // Keep GCC happy by having a default handler, fall-through.
91 default: is_primitive = true; is_set = true; is_static = true; break;
92 }
Ian Rogers57b86d42012-03-27 16:05:41 -070093 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080094 mirror::Field* resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
Ian Rogers57b86d42012-03-27 16:05:41 -070095 if (UNLIKELY(resolved_field == NULL)) {
Ian Rogers08f753d2012-08-24 14:35:25 -070096 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
97 return NULL; // Failure.
Sebastien Hertz233ea8e2013-06-06 11:57:09 +020098 }
99 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
100 if (access_check) {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200101 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700102 ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer);
Ian Rogers08f753d2012-08-24 14:35:25 -0700103 return NULL;
104 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800105 mirror::Class* referring_class = referrer->GetDeclaringClass();
Ian Rogerse2645d32012-04-11 14:42:42 -0700106 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
107 !referring_class->CanAccessMember(fields_class,
108 resolved_field->GetAccessFlags()))) {
109 // The referring class can't access the resolved field, this may occur as a result of a
110 // protected field being made public by a sub-class. Resort to the dex file to determine
111 // the correct class for the access check.
Ian Rogers4445a7e2012-10-05 17:19:13 -0700112 const DexFile& dex_file = *referring_class->GetDexCache()->GetDexFile();
Ian Rogerse2645d32012-04-11 14:42:42 -0700113 fields_class = class_linker->ResolveType(dex_file,
114 dex_file.GetFieldId(field_idx).class_idx_,
115 referring_class);
116 if (UNLIKELY(!referring_class->CanAccess(fields_class))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700117 ThrowIllegalAccessErrorClass(referring_class, fields_class);
Ian Rogerse2645d32012-04-11 14:42:42 -0700118 return NULL; // failure
119 } else if (UNLIKELY(!referring_class->CanAccessMember(fields_class,
120 resolved_field->GetAccessFlags()))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700121 ThrowIllegalAccessErrorField(referring_class, resolved_field);
Ian Rogerse2645d32012-04-11 14:42:42 -0700122 return NULL; // failure
123 }
124 }
125 if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700126 ThrowIllegalAccessErrorFinalField(referrer, resolved_field);
Ian Rogers57b86d42012-03-27 16:05:41 -0700127 return NULL; // failure
128 } else {
129 FieldHelper fh(resolved_field);
130 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
131 fh.FieldSize() != expected_size)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800132 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
133 DCHECK(throw_location.GetMethod() == referrer);
134 self->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
Ian Rogers57b86d42012-03-27 16:05:41 -0700135 "Attempted read of %zd-bit %s on field '%s'",
136 expected_size * (32 / sizeof(int32_t)),
137 is_primitive ? "primitive" : "non-primitive",
138 PrettyField(resolved_field, true).c_str());
139 return NULL; // failure
Ian Rogers60db5ab2012-02-20 17:02:00 -0800140 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700141 }
142 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200143 if (!is_static) {
144 // instance fields must be being accessed on an initialized class
145 return resolved_field;
146 } else {
147 // If the class is initialized we're done.
148 if (fields_class->IsInitialized()) {
149 return resolved_field;
150 } else if (Runtime::Current()->GetClassLinker()->EnsureInitialized(fields_class, true, true)) {
151 // Otherwise let's ensure the class is initialized before resolving the field.
152 return resolved_field;
153 } else {
154 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
155 return NULL; // failure
156 }
157 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700158}
159
160// Slow path method resolution
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800161mirror::AbstractMethod* FindMethodFromCode(uint32_t method_idx, mirror::Object* this_object,
162 mirror::AbstractMethod* referrer,
163 Thread* self, bool access_check, InvokeType type) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700164 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
165 bool is_direct = type == kStatic || type == kDirect;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800166 mirror::AbstractMethod* resolved_method = class_linker->ResolveMethod(method_idx, referrer, type);
Ian Rogers57b86d42012-03-27 16:05:41 -0700167 if (UNLIKELY(resolved_method == NULL)) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700168 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
169 return NULL; // Failure.
jeffhao262e2512012-12-11 09:46:43 -0800170 } else if (UNLIKELY(this_object == NULL && type != kStatic)) {
171 // Maintain interpreter-like semantics where NullPointerException is thrown
172 // after potential NoSuchMethodError from class linker.
Ian Rogers62d6c772013-02-27 08:32:07 -0800173 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
174 DCHECK(referrer == throw_location.GetMethod());
175 ThrowNullPointerExceptionForMethodAccess(throw_location, method_idx, type);
jeffhao262e2512012-12-11 09:46:43 -0800176 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700177 } else {
178 if (!access_check) {
179 if (is_direct) {
180 return resolved_method;
181 } else if (type == kInterface) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800182 mirror::AbstractMethod* interface_method =
Ian Rogers57b86d42012-03-27 16:05:41 -0700183 this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
184 if (UNLIKELY(interface_method == NULL)) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700185 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method, this_object,
186 referrer);
Ian Rogers08f753d2012-08-24 14:35:25 -0700187 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700188 } else {
189 return interface_method;
190 }
Ian Rogers60db5ab2012-02-20 17:02:00 -0800191 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800192 mirror::ObjectArray<mirror::AbstractMethod>* vtable;
Ian Rogers57b86d42012-03-27 16:05:41 -0700193 uint16_t vtable_index = resolved_method->GetMethodIndex();
194 if (type == kSuper) {
195 vtable = referrer->GetDeclaringClass()->GetSuperClass()->GetVTable();
196 } else {
197 vtable = this_object->GetClass()->GetVTable();
198 }
199 // TODO: eliminate bounds check?
200 return vtable->Get(vtable_index);
201 }
202 } else {
Ian Rogers08f753d2012-08-24 14:35:25 -0700203 // Incompatible class change should have been handled in resolve method.
204 if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) {
Ian Rogers2fc14272012-08-30 10:56:57 -0700205 ThrowIncompatibleClassChangeError(type, resolved_method->GetInvokeType(), resolved_method,
206 referrer);
Ian Rogers08f753d2012-08-24 14:35:25 -0700207 return NULL; // Failure.
208 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800209 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
210 mirror::Class* referring_class = referrer->GetDeclaringClass();
Ian Rogers57b86d42012-03-27 16:05:41 -0700211 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
212 !referring_class->CanAccessMember(methods_class,
213 resolved_method->GetAccessFlags()))) {
214 // The referring class can't access the resolved method, this may occur as a result of a
215 // protected method being made public by implementing an interface that re-declares the
216 // method public. Resort to the dex file to determine the correct class for the access check
Ian Rogers4445a7e2012-10-05 17:19:13 -0700217 const DexFile& dex_file = *referring_class->GetDexCache()->GetDexFile();
Ian Rogers57b86d42012-03-27 16:05:41 -0700218 methods_class = class_linker->ResolveType(dex_file,
219 dex_file.GetMethodId(method_idx).class_idx_,
220 referring_class);
221 if (UNLIKELY(!referring_class->CanAccess(methods_class))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700222 ThrowIllegalAccessErrorClassForMethodDispatch(referring_class, methods_class,
223 referrer, resolved_method, type);
Ian Rogers08f753d2012-08-24 14:35:25 -0700224 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700225 } else if (UNLIKELY(!referring_class->CanAccessMember(methods_class,
226 resolved_method->GetAccessFlags()))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700227 ThrowIllegalAccessErrorMethod(referring_class, resolved_method);
Ian Rogers08f753d2012-08-24 14:35:25 -0700228 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700229 }
230 }
231 if (is_direct) {
232 return resolved_method;
233 } else if (type == kInterface) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800234 mirror::AbstractMethod* interface_method =
Ian Rogers57b86d42012-03-27 16:05:41 -0700235 this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
236 if (UNLIKELY(interface_method == NULL)) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700237 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method, this_object,
238 referrer);
Ian Rogers08f753d2012-08-24 14:35:25 -0700239 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700240 } else {
241 return interface_method;
242 }
243 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800244 mirror::ObjectArray<mirror::AbstractMethod>* vtable;
Ian Rogers57b86d42012-03-27 16:05:41 -0700245 uint16_t vtable_index = resolved_method->GetMethodIndex();
246 if (type == kSuper) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800247 mirror::Class* super_class = referring_class->GetSuperClass();
Ian Rogers57b86d42012-03-27 16:05:41 -0700248 if (LIKELY(super_class != NULL)) {
249 vtable = referring_class->GetSuperClass()->GetVTable();
250 } else {
251 vtable = NULL;
252 }
253 } else {
254 vtable = this_object->GetClass()->GetVTable();
255 }
256 if (LIKELY(vtable != NULL &&
257 vtable_index < static_cast<uint32_t>(vtable->GetLength()))) {
258 return vtable->GetWithoutChecks(vtable_index);
259 } else {
Ian Rogers08f753d2012-08-24 14:35:25 -0700260 // Behavior to agree with that of the verifier.
261 MethodHelper mh(resolved_method);
262 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(), mh.GetName(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800263 mh.GetSignature());
Ian Rogers08f753d2012-08-24 14:35:25 -0700264 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700265 }
Ian Rogers60db5ab2012-02-20 17:02:00 -0800266 }
267 }
268 }
Ian Rogers60db5ab2012-02-20 17:02:00 -0800269}
270
jeffhaod7521322012-11-21 15:38:24 -0800271void ThrowStackOverflowError(Thread* self) {
272 CHECK(!self->IsHandlingStackOverflow()) << "Recursive stack overflow.";
Ian Rogers62d6c772013-02-27 08:32:07 -0800273
274 if (Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()) {
275 // Remove extra entry pushed onto second stack during method tracing.
276 Runtime::Current()->GetInstrumentation()->PopMethodForUnwind(self, false);
jeffhaod7521322012-11-21 15:38:24 -0800277 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800278
jeffhaod7521322012-11-21 15:38:24 -0800279 self->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute.
280 JNIEnvExt* env = self->GetJniEnv();
281 std::string msg("stack size ");
282 msg += PrettySize(self->GetStackSize());
283 // Use low-level JNI routine and pre-baked error class to avoid class linking operations that
284 // would consume more stack.
285 int rc = ::art::ThrowNewException(env, WellKnownClasses::java_lang_StackOverflowError,
286 msg.c_str(), NULL);
287 if (rc != JNI_OK) {
288 // TODO: ThrowNewException failed presumably because of an OOME, we continue to throw the OOME
289 // or die in the CHECK below. We may want to throw a pre-baked StackOverflowError
290 // instead.
291 LOG(ERROR) << "Couldn't throw new StackOverflowError because JNI ThrowNew failed.";
292 CHECK(self->IsExceptionPending());
293 }
294 self->ResetDefaultStackEnd(); // Return to default stack size.
295}
296
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800297JValue InvokeProxyInvocationHandler(ScopedObjectAccessUnchecked& soa, const char* shorty,
298 jobject rcvr_jobj, jobject interface_method_jobj,
299 std::vector<jvalue>& args) {
300 DCHECK(soa.Env()->IsInstanceOf(rcvr_jobj, WellKnownClasses::java_lang_reflect_Proxy));
301
302 // Build argument array possibly triggering GC.
303 soa.Self()->AssertThreadSuspensionIsAllowable();
304 jobjectArray args_jobj = NULL;
305 const JValue zero;
306 if (args.size() > 0) {
307 args_jobj = soa.Env()->NewObjectArray(args.size(), WellKnownClasses::java_lang_Object, NULL);
308 if (args_jobj == NULL) {
309 CHECK(soa.Self()->IsExceptionPending());
310 return zero;
311 }
312 for (size_t i = 0; i < args.size(); ++i) {
313 if (shorty[i + 1] == 'L') {
314 jobject val = args.at(i).l;
315 soa.Env()->SetObjectArrayElement(args_jobj, i, val);
316 } else {
317 JValue jv;
318 jv.SetJ(args.at(i).j);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800319 mirror::Object* val = BoxPrimitive(Primitive::GetType(shorty[i + 1]), jv);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800320 if (val == NULL) {
321 CHECK(soa.Self()->IsExceptionPending());
322 return zero;
323 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800324 soa.Decode<mirror::ObjectArray<mirror::Object>* >(args_jobj)->Set(i, val);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800325 }
326 }
327 }
328
329 // Call InvocationHandler.invoke(Object proxy, Method method, Object[] args).
330 jobject inv_hand = soa.Env()->GetObjectField(rcvr_jobj,
331 WellKnownClasses::java_lang_reflect_Proxy_h);
332 jvalue invocation_args[3];
333 invocation_args[0].l = rcvr_jobj;
334 invocation_args[1].l = interface_method_jobj;
335 invocation_args[2].l = args_jobj;
336 jobject result =
337 soa.Env()->CallObjectMethodA(inv_hand,
338 WellKnownClasses::java_lang_reflect_InvocationHandler_invoke,
339 invocation_args);
340
341 // Unbox result and handle error conditions.
Ian Rogers62d6c772013-02-27 08:32:07 -0800342 if (LIKELY(!soa.Self()->IsExceptionPending())) {
343 if (shorty[0] == 'V' || (shorty[0] == 'L' && result == NULL)) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800344 // Do nothing.
345 return zero;
346 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800347 mirror::Object* result_ref = soa.Decode<mirror::Object*>(result);
Ian Rogers62d6c772013-02-27 08:32:07 -0800348 mirror::Object* rcvr = soa.Decode<mirror::Object*>(rcvr_jobj);
349 mirror::AbstractMethod* interface_method =
350 soa.Decode<mirror::AbstractMethod*>(interface_method_jobj);
351 mirror::Class* result_type = MethodHelper(interface_method).GetReturnType();
352 mirror::AbstractMethod* proxy_method;
353 if (interface_method->GetDeclaringClass()->IsInterface()) {
354 proxy_method = rcvr->GetClass()->FindVirtualMethodForInterface(interface_method);
355 } else {
356 // Proxy dispatch to a method defined in Object.
357 DCHECK(interface_method->GetDeclaringClass()->IsObjectClass());
358 proxy_method = interface_method;
359 }
360 ThrowLocation throw_location(rcvr, proxy_method, -1);
361 JValue result_unboxed;
362 if (!UnboxPrimitiveForResult(throw_location, result_ref, result_type, result_unboxed)) {
Ian Rogers530f71c2013-02-22 23:29:00 -0800363 DCHECK(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800364 return zero;
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800365 }
366 return result_unboxed;
367 }
368 } else {
369 // In the case of checked exceptions that aren't declared, the exception must be wrapped by
370 // a UndeclaredThrowableException.
Ian Rogers62d6c772013-02-27 08:32:07 -0800371 mirror::Throwable* exception = soa.Self()->GetException(NULL);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800372 if (exception->IsCheckedException()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800373 mirror::Object* rcvr = soa.Decode<mirror::Object*>(rcvr_jobj);
374 mirror::SynthesizedProxyClass* proxy_class =
375 down_cast<mirror::SynthesizedProxyClass*>(rcvr->GetClass());
376 mirror::AbstractMethod* interface_method =
377 soa.Decode<mirror::AbstractMethod*>(interface_method_jobj);
378 mirror::AbstractMethod* proxy_method =
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800379 rcvr->GetClass()->FindVirtualMethodForInterface(interface_method);
380 int throws_index = -1;
381 size_t num_virt_methods = proxy_class->NumVirtualMethods();
382 for (size_t i = 0; i < num_virt_methods; i++) {
383 if (proxy_class->GetVirtualMethod(i) == proxy_method) {
384 throws_index = i;
385 break;
386 }
387 }
388 CHECK_NE(throws_index, -1);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800389 mirror::ObjectArray<mirror::Class>* declared_exceptions = proxy_class->GetThrows()->Get(throws_index);
390 mirror::Class* exception_class = exception->GetClass();
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800391 bool declares_exception = false;
392 for (int i = 0; i < declared_exceptions->GetLength() && !declares_exception; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800393 mirror::Class* declared_exception = declared_exceptions->Get(i);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800394 declares_exception = declared_exception->IsAssignableFrom(exception_class);
395 }
396 if (!declares_exception) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800397 ThrowLocation throw_location(rcvr, proxy_method, -1);
398 soa.Self()->ThrowNewWrappedException(throw_location,
399 "Ljava/lang/reflect/UndeclaredThrowableException;",
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800400 NULL);
401 }
402 }
403 return zero;
404 }
405}
406
Shih-wei Liao2d831012011-09-28 22:06:53 -0700407} // namespace art