Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 1 | /* |
| 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 Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 17 | #include "java_lang_reflect_Method.h" |
| 18 | |
Andreas Gampe | a14100c | 2017-04-24 15:09:56 -0700 | [diff] [blame] | 19 | #include "nativehelper/jni_macros.h" |
| 20 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 21 | #include "art_method-inl.h" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 22 | #include "base/enums.h" |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 23 | #include "class_linker-inl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 24 | #include "class_linker.h" |
Vladimir Marko | 5868ada | 2020-05-12 11:50:34 +0100 | [diff] [blame] | 25 | #include "class_root-inl.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 26 | #include "dex/dex_file_annotations.h" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 27 | #include "jni/jni_internal.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 28 | #include "mirror/class-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 29 | #include "mirror/object-inl.h" |
Andreas Gampe | 52ecb65 | 2018-10-24 15:18:21 -0700 | [diff] [blame] | 30 | #include "mirror/object_array-alloc-inl.h" |
Andreas Gampe | 87583b3 | 2017-05-25 11:22:18 -0700 | [diff] [blame] | 31 | #include "native_util.h" |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 32 | #include "reflection.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 33 | #include "scoped_fast_native_object_access-inl.h" |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 34 | |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 35 | namespace art { |
| 36 | |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 37 | static 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 Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 43 | return soa.AddLocalReference<jobject>(annotations::GetAnnotationDefaultValue(method)); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | static jobjectArray Method_getExceptionTypes(JNIEnv* env, jobject javaMethod) { |
| 47 | ScopedFastNativeObjectAccess soa(env); |
| 48 | ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod); |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 49 | if (method->GetDeclaringClass()->IsProxyClass()) { |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 50 | ObjPtr<mirror::Class> klass = method->GetDeclaringClass(); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 51 | int throws_index = -1; |
| 52 | size_t i = 0; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 53 | for (const auto& m : klass->GetDeclaredVirtualMethods(kRuntimePointerSize)) { |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 54 | if (&m == method) { |
| 55 | throws_index = i; |
| 56 | break; |
| 57 | } |
| 58 | ++i; |
| 59 | } |
| 60 | CHECK_NE(throws_index, -1); |
Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 61 | 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 Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 66 | } else { |
Vladimir Marko | 2d3065e | 2018-05-22 13:56:09 +0100 | [diff] [blame] | 67 | ObjPtr<mirror::ObjectArray<mirror::Class>> result_array = |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 68 | annotations::GetExceptionTypesForMethod(method); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 69 | if (result_array == nullptr) { |
| 70 | // Return an empty array instead of a null pointer |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 71 | ObjPtr<mirror::Class> class_array_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>(); |
| 72 | DCHECK(class_array_class != nullptr); |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 73 | ObjPtr<mirror::ObjectArray<mirror::Class>> empty_array = |
Jeff Hao | 2a5892f | 2015-08-31 15:00:40 -0700 | [diff] [blame] | 74 | mirror::ObjectArray<mirror::Class>::Alloc(soa.Self(), class_array_class, 0); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 75 | return soa.AddLocalReference<jobjectArray>(empty_array); |
| 76 | } else { |
| 77 | return soa.AddLocalReference<jobjectArray>(result_array); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
Chris Wailes | 9f61e0a | 2021-05-12 17:16:50 -0700 | [diff] [blame] | 82 | NO_STACK_PROTECTOR |
Jeff Hao | 11d5d8f | 2014-03-26 15:08:20 -0700 | [diff] [blame] | 83 | static jobject Method_invoke(JNIEnv* env, jobject javaMethod, jobject javaReceiver, |
Igor Murashkin | 06537f7 | 2018-02-22 15:03:05 -0800 | [diff] [blame] | 84 | jobjectArray javaArgs) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 85 | ScopedFastNativeObjectAccess soa(env); |
liulvping | fff1d8f | 2020-12-21 09:43:37 +0800 | [diff] [blame] | 86 | return InvokeMethod<kRuntimePointerSize>(soa, javaMethod, javaReceiver, javaArgs); |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 89 | static JNINativeMethod gMethods[] = { |
Igor Murashkin | 3b6f440 | 2017-02-16 16:13:17 -0800 | [diff] [blame] | 90 | 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 Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 93 | }; |
| 94 | |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 95 | void register_java_lang_reflect_Method(JNIEnv* env) { |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 96 | REGISTER_NATIVE_METHODS("java/lang/reflect/Method"); |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | } // namespace art |