blob: 52f8c81ab61c6bb511cad79315a939496bee6438 [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
Ian Rogers57b86d42012-03-27 16:05:41 -070036// Helper function to allocate array for FILLED_NEW_ARRAY.
Brian Carlstromea46f952013-07-30 01:26:50 -070037mirror::Array* CheckAndAllocArrayFromCode(uint32_t type_idx, mirror::ArtMethod* referrer,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038 int32_t component_count, Thread* self,
39 bool access_check) {
Ian Rogers57b86d42012-03-27 16:05:41 -070040 if (UNLIKELY(component_count < 0)) {
Ian Rogers62d6c772013-02-27 08:32:07 -080041 ThrowNegativeArraySizeException(component_count);
Ian Rogers57b86d42012-03-27 16:05:41 -070042 return NULL; // Failure
Elliott Hughes6c8867d2011-10-03 16:34:05 -070043 }
Ian Rogers62d6c772013-02-27 08:32:07 -080044 mirror::Class* klass = referrer->GetDexCacheResolvedTypes()->Get(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());
Ian Rogers57b86d42012-03-27 16:05:41 -070049 return NULL; // 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 }
Ian Rogers57b86d42012-03-27 16:05:41 -070063 return NULL; // Failure
Ian Rogersad25ac52011-10-04 19:13:33 -070064 } else {
Ian Rogers57b86d42012-03-27 16:05:41 -070065 if (access_check) {
Ian Rogers62d6c772013-02-27 08:32:07 -080066 mirror::Class* referrer_klass = referrer->GetDeclaringClass();
67 if (UNLIKELY(!referrer_klass->CanAccess(klass))) {
68 ThrowIllegalAccessErrorClass(referrer_klass, klass);
Ian Rogers57b86d42012-03-27 16:05:41 -070069 return NULL; // Failure
70 }
Ian Rogers60db5ab2012-02-20 17:02:00 -080071 }
Ian Rogers57b86d42012-03-27 16:05:41 -070072 DCHECK(klass->IsArrayClass()) << PrettyClass(klass);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080073 return mirror::Array::Alloc(self, klass, component_count);
Ian Rogers57b86d42012-03-27 16:05:41 -070074 }
75}
76
Brian Carlstromea46f952013-07-30 01:26:50 -070077mirror::ArtField* FindFieldFromCode(uint32_t field_idx, const mirror::ArtMethod* referrer,
Sebastien Hertz233ea8e2013-06-06 11:57:09 +020078 Thread* self, FindFieldType type, size_t expected_size,
79 bool access_check) {
Ian Rogers08f753d2012-08-24 14:35:25 -070080 bool is_primitive;
81 bool is_set;
82 bool is_static;
83 switch (type) {
84 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
85 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
86 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
87 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
88 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
89 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
90 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
91 case StaticPrimitiveWrite: // Keep GCC happy by having a default handler, fall-through.
92 default: is_primitive = true; is_set = true; is_static = true; break;
93 }
Ian Rogers57b86d42012-03-27 16:05:41 -070094 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromea46f952013-07-30 01:26:50 -070095 mirror::ArtField* resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
Ian Rogers57b86d42012-03-27 16:05:41 -070096 if (UNLIKELY(resolved_field == NULL)) {
Ian Rogers08f753d2012-08-24 14:35:25 -070097 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
98 return NULL; // Failure.
Sebastien Hertz233ea8e2013-06-06 11:57:09 +020099 }
100 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
101 if (access_check) {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200102 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700103 ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer);
Ian Rogers08f753d2012-08-24 14:35:25 -0700104 return NULL;
105 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800106 mirror::Class* referring_class = referrer->GetDeclaringClass();
Ian Rogerse2645d32012-04-11 14:42:42 -0700107 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
108 !referring_class->CanAccessMember(fields_class,
109 resolved_field->GetAccessFlags()))) {
110 // The referring class can't access the resolved field, this may occur as a result of a
111 // protected field being made public by a sub-class. Resort to the dex file to determine
112 // the correct class for the access check.
Ian Rogers4445a7e2012-10-05 17:19:13 -0700113 const DexFile& dex_file = *referring_class->GetDexCache()->GetDexFile();
Ian Rogerse2645d32012-04-11 14:42:42 -0700114 fields_class = class_linker->ResolveType(dex_file,
115 dex_file.GetFieldId(field_idx).class_idx_,
116 referring_class);
117 if (UNLIKELY(!referring_class->CanAccess(fields_class))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700118 ThrowIllegalAccessErrorClass(referring_class, fields_class);
Ian Rogerse2645d32012-04-11 14:42:42 -0700119 return NULL; // failure
120 } else if (UNLIKELY(!referring_class->CanAccessMember(fields_class,
121 resolved_field->GetAccessFlags()))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700122 ThrowIllegalAccessErrorField(referring_class, resolved_field);
Ian Rogerse2645d32012-04-11 14:42:42 -0700123 return NULL; // failure
124 }
125 }
126 if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700127 ThrowIllegalAccessErrorFinalField(referrer, resolved_field);
Ian Rogers57b86d42012-03-27 16:05:41 -0700128 return NULL; // failure
129 } else {
130 FieldHelper fh(resolved_field);
131 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
132 fh.FieldSize() != expected_size)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800133 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
134 DCHECK(throw_location.GetMethod() == referrer);
135 self->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
Ian Rogers57b86d42012-03-27 16:05:41 -0700136 "Attempted read of %zd-bit %s on field '%s'",
137 expected_size * (32 / sizeof(int32_t)),
138 is_primitive ? "primitive" : "non-primitive",
139 PrettyField(resolved_field, true).c_str());
140 return NULL; // failure
Ian Rogers60db5ab2012-02-20 17:02:00 -0800141 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700142 }
143 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200144 if (!is_static) {
145 // instance fields must be being accessed on an initialized class
146 return resolved_field;
147 } else {
148 // If the class is initialized we're done.
149 if (fields_class->IsInitialized()) {
150 return resolved_field;
151 } else if (Runtime::Current()->GetClassLinker()->EnsureInitialized(fields_class, true, true)) {
152 // Otherwise let's ensure the class is initialized before resolving the field.
153 return resolved_field;
154 } else {
155 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
156 return NULL; // failure
157 }
158 }
Ian Rogers57b86d42012-03-27 16:05:41 -0700159}
160
161// Slow path method resolution
Brian Carlstromea46f952013-07-30 01:26:50 -0700162mirror::ArtMethod* FindMethodFromCode(uint32_t method_idx, mirror::Object* this_object,
163 mirror::ArtMethod* referrer,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800164 Thread* self, bool access_check, InvokeType type) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700165 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
166 bool is_direct = type == kStatic || type == kDirect;
Brian Carlstromea46f952013-07-30 01:26:50 -0700167 mirror::ArtMethod* resolved_method = class_linker->ResolveMethod(method_idx, referrer, type);
Ian Rogers57b86d42012-03-27 16:05:41 -0700168 if (UNLIKELY(resolved_method == NULL)) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700169 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
170 return NULL; // Failure.
jeffhao262e2512012-12-11 09:46:43 -0800171 } else if (UNLIKELY(this_object == NULL && type != kStatic)) {
172 // Maintain interpreter-like semantics where NullPointerException is thrown
173 // after potential NoSuchMethodError from class linker.
Ian Rogers62d6c772013-02-27 08:32:07 -0800174 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
175 DCHECK(referrer == throw_location.GetMethod());
176 ThrowNullPointerExceptionForMethodAccess(throw_location, method_idx, type);
jeffhao262e2512012-12-11 09:46:43 -0800177 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700178 } else {
179 if (!access_check) {
180 if (is_direct) {
181 return resolved_method;
182 } else if (type == kInterface) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700183 mirror::ArtMethod* interface_method =
Ian Rogers57b86d42012-03-27 16:05:41 -0700184 this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
185 if (UNLIKELY(interface_method == NULL)) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700186 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method, this_object,
187 referrer);
Ian Rogers08f753d2012-08-24 14:35:25 -0700188 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700189 } else {
190 return interface_method;
191 }
Ian Rogers60db5ab2012-02-20 17:02:00 -0800192 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700193 mirror::ObjectArray<mirror::ArtMethod>* vtable;
Ian Rogers57b86d42012-03-27 16:05:41 -0700194 uint16_t vtable_index = resolved_method->GetMethodIndex();
195 if (type == kSuper) {
196 vtable = referrer->GetDeclaringClass()->GetSuperClass()->GetVTable();
197 } else {
198 vtable = this_object->GetClass()->GetVTable();
199 }
200 // TODO: eliminate bounds check?
201 return vtable->Get(vtable_index);
202 }
203 } else {
Ian Rogers08f753d2012-08-24 14:35:25 -0700204 // Incompatible class change should have been handled in resolve method.
205 if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) {
Ian Rogers2fc14272012-08-30 10:56:57 -0700206 ThrowIncompatibleClassChangeError(type, resolved_method->GetInvokeType(), resolved_method,
207 referrer);
Ian Rogers08f753d2012-08-24 14:35:25 -0700208 return NULL; // Failure.
209 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800210 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
211 mirror::Class* referring_class = referrer->GetDeclaringClass();
Ian Rogers57b86d42012-03-27 16:05:41 -0700212 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
213 !referring_class->CanAccessMember(methods_class,
214 resolved_method->GetAccessFlags()))) {
215 // The referring class can't access the resolved method, this may occur as a result of a
216 // protected method being made public by implementing an interface that re-declares the
217 // method public. Resort to the dex file to determine the correct class for the access check
Ian Rogers4445a7e2012-10-05 17:19:13 -0700218 const DexFile& dex_file = *referring_class->GetDexCache()->GetDexFile();
Ian Rogers57b86d42012-03-27 16:05:41 -0700219 methods_class = class_linker->ResolveType(dex_file,
220 dex_file.GetMethodId(method_idx).class_idx_,
221 referring_class);
222 if (UNLIKELY(!referring_class->CanAccess(methods_class))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700223 ThrowIllegalAccessErrorClassForMethodDispatch(referring_class, methods_class,
224 referrer, resolved_method, type);
Ian Rogers08f753d2012-08-24 14:35:25 -0700225 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700226 } else if (UNLIKELY(!referring_class->CanAccessMember(methods_class,
227 resolved_method->GetAccessFlags()))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700228 ThrowIllegalAccessErrorMethod(referring_class, resolved_method);
Ian Rogers08f753d2012-08-24 14:35:25 -0700229 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700230 }
231 }
232 if (is_direct) {
233 return resolved_method;
234 } else if (type == kInterface) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700235 mirror::ArtMethod* interface_method =
Ian Rogers57b86d42012-03-27 16:05:41 -0700236 this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
237 if (UNLIKELY(interface_method == NULL)) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700238 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method, this_object,
239 referrer);
Ian Rogers08f753d2012-08-24 14:35:25 -0700240 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700241 } else {
242 return interface_method;
243 }
244 } else {
Brian Carlstromea46f952013-07-30 01:26:50 -0700245 mirror::ObjectArray<mirror::ArtMethod>* vtable;
Ian Rogers57b86d42012-03-27 16:05:41 -0700246 uint16_t vtable_index = resolved_method->GetMethodIndex();
247 if (type == kSuper) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800248 mirror::Class* super_class = referring_class->GetSuperClass();
Ian Rogers57b86d42012-03-27 16:05:41 -0700249 if (LIKELY(super_class != NULL)) {
250 vtable = referring_class->GetSuperClass()->GetVTable();
251 } else {
252 vtable = NULL;
253 }
254 } else {
255 vtable = this_object->GetClass()->GetVTable();
256 }
257 if (LIKELY(vtable != NULL &&
258 vtable_index < static_cast<uint32_t>(vtable->GetLength()))) {
259 return vtable->GetWithoutChecks(vtable_index);
260 } else {
Ian Rogers08f753d2012-08-24 14:35:25 -0700261 // Behavior to agree with that of the verifier.
262 MethodHelper mh(resolved_method);
263 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(), mh.GetName(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800264 mh.GetSignature());
Ian Rogers08f753d2012-08-24 14:35:25 -0700265 return NULL; // Failure.
Ian Rogers57b86d42012-03-27 16:05:41 -0700266 }
Ian Rogers60db5ab2012-02-20 17:02:00 -0800267 }
268 }
269 }
Ian Rogers60db5ab2012-02-20 17:02:00 -0800270}
271
jeffhaod7521322012-11-21 15:38:24 -0800272void ThrowStackOverflowError(Thread* self) {
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700273 if (self->IsHandlingStackOverflow()) {
274 LOG(ERROR) << "Recursive stack overflow.";
275 // We don't fail here because SetStackEndForStackOverflow will print better diagnostics.
276 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800277
278 if (Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()) {
279 // Remove extra entry pushed onto second stack during method tracing.
280 Runtime::Current()->GetInstrumentation()->PopMethodForUnwind(self, false);
jeffhaod7521322012-11-21 15:38:24 -0800281 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800282
jeffhaod7521322012-11-21 15:38:24 -0800283 self->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute.
284 JNIEnvExt* env = self->GetJniEnv();
285 std::string msg("stack size ");
286 msg += PrettySize(self->GetStackSize());
287 // Use low-level JNI routine and pre-baked error class to avoid class linking operations that
288 // would consume more stack.
289 int rc = ::art::ThrowNewException(env, WellKnownClasses::java_lang_StackOverflowError,
290 msg.c_str(), NULL);
291 if (rc != JNI_OK) {
292 // TODO: ThrowNewException failed presumably because of an OOME, we continue to throw the OOME
293 // or die in the CHECK below. We may want to throw a pre-baked StackOverflowError
294 // instead.
295 LOG(ERROR) << "Couldn't throw new StackOverflowError because JNI ThrowNew failed.";
296 CHECK(self->IsExceptionPending());
297 }
298 self->ResetDefaultStackEnd(); // Return to default stack size.
299}
300
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800301JValue InvokeProxyInvocationHandler(ScopedObjectAccessUnchecked& soa, const char* shorty,
302 jobject rcvr_jobj, jobject interface_method_jobj,
303 std::vector<jvalue>& args) {
304 DCHECK(soa.Env()->IsInstanceOf(rcvr_jobj, WellKnownClasses::java_lang_reflect_Proxy));
305
306 // Build argument array possibly triggering GC.
307 soa.Self()->AssertThreadSuspensionIsAllowable();
308 jobjectArray args_jobj = NULL;
309 const JValue zero;
310 if (args.size() > 0) {
311 args_jobj = soa.Env()->NewObjectArray(args.size(), WellKnownClasses::java_lang_Object, NULL);
312 if (args_jobj == NULL) {
313 CHECK(soa.Self()->IsExceptionPending());
314 return zero;
315 }
316 for (size_t i = 0; i < args.size(); ++i) {
317 if (shorty[i + 1] == 'L') {
318 jobject val = args.at(i).l;
319 soa.Env()->SetObjectArrayElement(args_jobj, i, val);
320 } else {
321 JValue jv;
322 jv.SetJ(args.at(i).j);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800323 mirror::Object* val = BoxPrimitive(Primitive::GetType(shorty[i + 1]), jv);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800324 if (val == NULL) {
325 CHECK(soa.Self()->IsExceptionPending());
326 return zero;
327 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800328 soa.Decode<mirror::ObjectArray<mirror::Object>* >(args_jobj)->Set(i, val);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800329 }
330 }
331 }
332
Brian Carlstromea46f952013-07-30 01:26:50 -0700333 // Call Proxy.invoke(Proxy proxy, ArtMethod method, Object[] args).
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800334 jvalue invocation_args[3];
335 invocation_args[0].l = rcvr_jobj;
336 invocation_args[1].l = interface_method_jobj;
337 invocation_args[2].l = args_jobj;
338 jobject result =
Brian Carlstromea46f952013-07-30 01:26:50 -0700339 soa.Env()->CallStaticObjectMethodA(WellKnownClasses::java_lang_reflect_Proxy,
340 WellKnownClasses::java_lang_reflect_Proxy_invoke,
341 invocation_args);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800342
343 // Unbox result and handle error conditions.
Ian Rogers62d6c772013-02-27 08:32:07 -0800344 if (LIKELY(!soa.Self()->IsExceptionPending())) {
345 if (shorty[0] == 'V' || (shorty[0] == 'L' && result == NULL)) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800346 // Do nothing.
347 return zero;
348 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800349 mirror::Object* result_ref = soa.Decode<mirror::Object*>(result);
Ian Rogers62d6c772013-02-27 08:32:07 -0800350 mirror::Object* rcvr = soa.Decode<mirror::Object*>(rcvr_jobj);
Brian Carlstromea46f952013-07-30 01:26:50 -0700351 mirror::ArtMethod* interface_method =
352 soa.Decode<mirror::ArtMethod*>(interface_method_jobj);
Ian Rogers62d6c772013-02-27 08:32:07 -0800353 mirror::Class* result_type = MethodHelper(interface_method).GetReturnType();
Brian Carlstromea46f952013-07-30 01:26:50 -0700354 mirror::ArtMethod* proxy_method;
Ian Rogers62d6c772013-02-27 08:32:07 -0800355 if (interface_method->GetDeclaringClass()->IsInterface()) {
356 proxy_method = rcvr->GetClass()->FindVirtualMethodForInterface(interface_method);
357 } else {
358 // Proxy dispatch to a method defined in Object.
359 DCHECK(interface_method->GetDeclaringClass()->IsObjectClass());
360 proxy_method = interface_method;
361 }
362 ThrowLocation throw_location(rcvr, proxy_method, -1);
363 JValue result_unboxed;
364 if (!UnboxPrimitiveForResult(throw_location, result_ref, result_type, result_unboxed)) {
Ian Rogers530f71c2013-02-22 23:29:00 -0800365 DCHECK(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800366 return zero;
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800367 }
368 return result_unboxed;
369 }
370 } else {
371 // In the case of checked exceptions that aren't declared, the exception must be wrapped by
372 // a UndeclaredThrowableException.
Ian Rogers62d6c772013-02-27 08:32:07 -0800373 mirror::Throwable* exception = soa.Self()->GetException(NULL);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800374 if (exception->IsCheckedException()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800375 mirror::Object* rcvr = soa.Decode<mirror::Object*>(rcvr_jobj);
376 mirror::SynthesizedProxyClass* proxy_class =
377 down_cast<mirror::SynthesizedProxyClass*>(rcvr->GetClass());
Brian Carlstromea46f952013-07-30 01:26:50 -0700378 mirror::ArtMethod* interface_method =
379 soa.Decode<mirror::ArtMethod*>(interface_method_jobj);
380 mirror::ArtMethod* proxy_method =
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800381 rcvr->GetClass()->FindVirtualMethodForInterface(interface_method);
382 int throws_index = -1;
383 size_t num_virt_methods = proxy_class->NumVirtualMethods();
384 for (size_t i = 0; i < num_virt_methods; i++) {
385 if (proxy_class->GetVirtualMethod(i) == proxy_method) {
386 throws_index = i;
387 break;
388 }
389 }
390 CHECK_NE(throws_index, -1);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800391 mirror::ObjectArray<mirror::Class>* declared_exceptions = proxy_class->GetThrows()->Get(throws_index);
392 mirror::Class* exception_class = exception->GetClass();
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800393 bool declares_exception = false;
394 for (int i = 0; i < declared_exceptions->GetLength() && !declares_exception; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800395 mirror::Class* declared_exception = declared_exceptions->Get(i);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800396 declares_exception = declared_exception->IsAssignableFrom(exception_class);
397 }
398 if (!declares_exception) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800399 ThrowLocation throw_location(rcvr, proxy_method, -1);
400 soa.Self()->ThrowNewWrappedException(throw_location,
401 "Ljava/lang/reflect/UndeclaredThrowableException;",
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800402 NULL);
403 }
404 }
405 return zero;
406 }
407}
408
Shih-wei Liao2d831012011-09-28 22:06:53 -0700409} // namespace art