blob: 5f02ad0fd9ff2f791d687d01ba75c674886839a6 [file] [log] [blame]
Brian Carlstromf867b6f2011-09-16 12:17:25 -07001/*
2 * Copyright (C) 2008 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
Andreas Gampe277ccbd2014-11-03 21:36:10 -080017#include "java_lang_reflect_Method.h"
18
Andreas Gampea14100c2017-04-24 15:09:56 -070019#include "nativehelper/jni_macros.h"
20
Mathieu Chartiere401d142015-04-22 13:56:20 -070021#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070022#include "base/enums.h"
Jeff Hao13e748b2015-08-25 20:44:19 +000023#include "class_linker-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070024#include "class_linker.h"
Vladimir Marko5868ada2020-05-12 11:50:34 +010025#include "class_root-inl.h"
David Sehr9e734c72018-01-04 17:56:19 -080026#include "dex/dex_file_annotations.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010027#include "jni/jni_internal.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029#include "mirror/object-inl.h"
Andreas Gampe52ecb652018-10-24 15:18:21 -070030#include "mirror/object_array-alloc-inl.h"
Andreas Gampe87583b32017-05-25 11:22:18 -070031#include "native_util.h"
Elliott Hughes418d20f2011-09-22 14:00:39 -070032#include "reflection.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070033#include "scoped_fast_native_object_access-inl.h"
Brian Carlstromf867b6f2011-09-16 12:17:25 -070034
Brian Carlstromf867b6f2011-09-16 12:17:25 -070035namespace art {
36
Jeff Hao13e748b2015-08-25 20:44:19 +000037static jobject Method_getDefaultValue(JNIEnv* env, jobject javaMethod) {
38 ScopedFastNativeObjectAccess soa(env);
39 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
40 if (!method->GetDeclaringClass()->IsAnnotation()) {
41 return nullptr;
42 }
David Sehr9323e6e2016-09-13 08:58:35 -070043 return soa.AddLocalReference<jobject>(annotations::GetAnnotationDefaultValue(method));
Jeff Hao13e748b2015-08-25 20:44:19 +000044}
45
46static jobjectArray Method_getExceptionTypes(JNIEnv* env, jobject javaMethod) {
47 ScopedFastNativeObjectAccess soa(env);
48 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
Nicolas Geoffray3a090922015-11-24 09:17:30 +000049 if (method->GetDeclaringClass()->IsProxyClass()) {
Mathieu Chartierbc5a7952016-10-17 15:46:31 -070050 ObjPtr<mirror::Class> klass = method->GetDeclaringClass();
Jeff Hao13e748b2015-08-25 20:44:19 +000051 int throws_index = -1;
52 size_t i = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -070053 for (const auto& m : klass->GetDeclaredVirtualMethods(kRuntimePointerSize)) {
Jeff Hao13e748b2015-08-25 20:44:19 +000054 if (&m == method) {
55 throws_index = i;
56 break;
57 }
58 ++i;
59 }
60 CHECK_NE(throws_index, -1);
Vladimir Marko3068d582019-05-28 16:39:29 +010061 StackHandleScope<1u> hs(soa.Self());
62 Handle<mirror::ObjectArray<mirror::Class>> declared_exceptions =
63 hs.NewHandle(klass->GetProxyThrows()->Get(throws_index));
64 return soa.AddLocalReference<jobjectArray>(
65 mirror::ObjectArray<mirror::Class>::Clone(declared_exceptions, soa.Self()));
Jeff Hao13e748b2015-08-25 20:44:19 +000066 } else {
Vladimir Marko2d3065e2018-05-22 13:56:09 +010067 ObjPtr<mirror::ObjectArray<mirror::Class>> result_array =
David Sehr9323e6e2016-09-13 08:58:35 -070068 annotations::GetExceptionTypesForMethod(method);
Jeff Hao13e748b2015-08-25 20:44:19 +000069 if (result_array == nullptr) {
70 // Return an empty array instead of a null pointer
Vladimir Markoa8bba7d2018-05-30 15:18:48 +010071 ObjPtr<mirror::Class> class_array_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>();
72 DCHECK(class_array_class != nullptr);
Vladimir Markobcf17522018-06-01 13:14:32 +010073 ObjPtr<mirror::ObjectArray<mirror::Class>> empty_array =
Jeff Hao2a5892f2015-08-31 15:00:40 -070074 mirror::ObjectArray<mirror::Class>::Alloc(soa.Self(), class_array_class, 0);
Jeff Hao13e748b2015-08-25 20:44:19 +000075 return soa.AddLocalReference<jobjectArray>(empty_array);
76 } else {
77 return soa.AddLocalReference<jobjectArray>(result_array);
78 }
79 }
80}
81
Chris Wailes9f61e0a2021-05-12 17:16:50 -070082NO_STACK_PROTECTOR
Jeff Hao11d5d8f2014-03-26 15:08:20 -070083static jobject Method_invoke(JNIEnv* env, jobject javaMethod, jobject javaReceiver,
Igor Murashkin06537f72018-02-22 15:03:05 -080084 jobjectArray javaArgs) {
Ian Rogers53b8b092014-03-13 23:45:53 -070085 ScopedFastNativeObjectAccess soa(env);
liulvpingfff1d8f2020-12-21 09:43:37 +080086 return InvokeMethod<kRuntimePointerSize>(soa, javaMethod, javaReceiver, javaArgs);
Brian Carlstromf867b6f2011-09-16 12:17:25 -070087}
88
Brian Carlstromf867b6f2011-09-16 12:17:25 -070089static JNINativeMethod gMethods[] = {
Igor Murashkin3b6f4402017-02-16 16:13:17 -080090 FAST_NATIVE_METHOD(Method, getDefaultValue, "()Ljava/lang/Object;"),
91 FAST_NATIVE_METHOD(Method, getExceptionTypes, "()[Ljava/lang/Class;"),
92 FAST_NATIVE_METHOD(Method, invoke, "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;"),
Brian Carlstromf867b6f2011-09-16 12:17:25 -070093};
94
Brian Carlstromf867b6f2011-09-16 12:17:25 -070095void register_java_lang_reflect_Method(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -070096 REGISTER_NATIVE_METHODS("java/lang/reflect/Method");
Brian Carlstromf867b6f2011-09-16 12:17:25 -070097}
98
99} // namespace art