blob: a6589bc073201e4138c50cd726684d10f6b8fe30 [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
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070020#include "base/enums.h"
Brian Carlstromf867b6f2011-09-16 12:17:25 -070021#include "class_linker.h"
Jeff Hao13e748b2015-08-25 20:44:19 +000022#include "class_linker-inl.h"
David Sehr9323e6e2016-09-13 08:58:35 -070023#include "dex_file_annotations.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070024#include "jni_internal.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026#include "mirror/object-inl.h"
27#include "mirror/object_array-inl.h"
Elliott Hughes418d20f2011-09-22 14:00:39 -070028#include "reflection.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070029#include "scoped_fast_native_object_access-inl.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070030#include "well_known_classes.h"
Brian Carlstromf867b6f2011-09-16 12:17:25 -070031
Brian Carlstromf867b6f2011-09-16 12:17:25 -070032namespace art {
33
Jeff Hao13e748b2015-08-25 20:44:19 +000034static jobject Method_getDefaultValue(JNIEnv* env, jobject javaMethod) {
35 ScopedFastNativeObjectAccess soa(env);
36 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
37 if (!method->GetDeclaringClass()->IsAnnotation()) {
38 return nullptr;
39 }
David Sehr9323e6e2016-09-13 08:58:35 -070040 return soa.AddLocalReference<jobject>(annotations::GetAnnotationDefaultValue(method));
Jeff Hao13e748b2015-08-25 20:44:19 +000041}
42
43static jobjectArray Method_getExceptionTypes(JNIEnv* env, jobject javaMethod) {
44 ScopedFastNativeObjectAccess soa(env);
45 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
Nicolas Geoffray3a090922015-11-24 09:17:30 +000046 if (method->GetDeclaringClass()->IsProxyClass()) {
Mathieu Chartierbc5a7952016-10-17 15:46:31 -070047 ObjPtr<mirror::Class> klass = method->GetDeclaringClass();
Jeff Hao13e748b2015-08-25 20:44:19 +000048 int throws_index = -1;
49 size_t i = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -070050 for (const auto& m : klass->GetDeclaredVirtualMethods(kRuntimePointerSize)) {
Jeff Hao13e748b2015-08-25 20:44:19 +000051 if (&m == method) {
52 throws_index = i;
53 break;
54 }
55 ++i;
56 }
57 CHECK_NE(throws_index, -1);
Nicolas Geoffray3a090922015-11-24 09:17:30 +000058 mirror::ObjectArray<mirror::Class>* declared_exceptions = klass->GetThrows()->Get(throws_index);
Jeff Hao13e748b2015-08-25 20:44:19 +000059 return soa.AddLocalReference<jobjectArray>(declared_exceptions->Clone(soa.Self()));
60 } else {
Jeff Hao2a5892f2015-08-31 15:00:40 -070061 mirror::ObjectArray<mirror::Class>* result_array =
David Sehr9323e6e2016-09-13 08:58:35 -070062 annotations::GetExceptionTypesForMethod(method);
Jeff Hao13e748b2015-08-25 20:44:19 +000063 if (result_array == nullptr) {
64 // Return an empty array instead of a null pointer
Mathieu Chartierbc5a7952016-10-17 15:46:31 -070065 ObjPtr<mirror::Class> class_class = mirror::Class::GetJavaLangClass();
66 ObjPtr<mirror::Class> class_array_class =
Jeff Hao13e748b2015-08-25 20:44:19 +000067 Runtime::Current()->GetClassLinker()->FindArrayClass(soa.Self(), &class_class);
Jeff Hao2a5892f2015-08-31 15:00:40 -070068 if (class_array_class == nullptr) {
69 return nullptr;
70 }
71 mirror::ObjectArray<mirror::Class>* empty_array =
72 mirror::ObjectArray<mirror::Class>::Alloc(soa.Self(), class_array_class, 0);
Jeff Hao13e748b2015-08-25 20:44:19 +000073 return soa.AddLocalReference<jobjectArray>(empty_array);
74 } else {
75 return soa.AddLocalReference<jobjectArray>(result_array);
76 }
77 }
78}
79
Jeff Hao11d5d8f2014-03-26 15:08:20 -070080static jobject Method_invoke(JNIEnv* env, jobject javaMethod, jobject javaReceiver,
Mathieu Chartierfc58af42015-04-16 18:00:39 -070081 jobject javaArgs) {
Ian Rogers53b8b092014-03-13 23:45:53 -070082 ScopedFastNativeObjectAccess soa(env);
Mathieu Chartierfc58af42015-04-16 18:00:39 -070083 return InvokeMethod(soa, javaMethod, javaReceiver, javaArgs);
Brian Carlstromf867b6f2011-09-16 12:17:25 -070084}
85
Brian Carlstromf867b6f2011-09-16 12:17:25 -070086static JNINativeMethod gMethods[] = {
Jeff Hao13e748b2015-08-25 20:44:19 +000087 NATIVE_METHOD(Method, getDefaultValue, "!()Ljava/lang/Object;"),
88 NATIVE_METHOD(Method, getExceptionTypes, "!()[Ljava/lang/Class;"),
Mathieu Chartierfc58af42015-04-16 18:00:39 -070089 NATIVE_METHOD(Method, invoke, "!(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;"),
Brian Carlstromf867b6f2011-09-16 12:17:25 -070090};
91
Brian Carlstromf867b6f2011-09-16 12:17:25 -070092void register_java_lang_reflect_Method(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -070093 REGISTER_NATIVE_METHODS("java/lang/reflect/Method");
Brian Carlstromf867b6f2011-09-16 12:17:25 -070094}
95
96} // namespace art