blob: c3f2a274cc09f2d7ee3a6d96b7c0640ad8804efc [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"
Elliott Hugheseac76672012-05-24 21:56:51 -070023#include "jni_internal.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"
26#include "mirror/object_array-inl.h"
Elliott Hughes418d20f2011-09-22 14:00:39 -070027#include "reflection.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070028#include "scoped_fast_native_object_access.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070029#include "well_known_classes.h"
Brian Carlstromf867b6f2011-09-16 12:17:25 -070030
Brian Carlstromf867b6f2011-09-16 12:17:25 -070031namespace art {
32
Jeff Hao13e748b2015-08-25 20:44:19 +000033static jobject Method_getAnnotationNative(JNIEnv* env, jobject javaMethod, jclass annotationType) {
34 ScopedFastNativeObjectAccess soa(env);
35 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
Nicolas Geoffray3a090922015-11-24 09:17:30 +000036 if (method->GetDeclaringClass()->IsProxyClass()) {
Jeff Hao13e748b2015-08-25 20:44:19 +000037 return nullptr;
38 }
39 StackHandleScope<1> hs(soa.Self());
40 Handle<mirror::Class> klass(hs.NewHandle(soa.Decode<mirror::Class*>(annotationType)));
41 return soa.AddLocalReference<jobject>(
42 method->GetDexFile()->GetAnnotationForMethod(method, klass));
43}
44
Jeff Hao13e748b2015-08-25 20:44:19 +000045static jobject Method_getDefaultValue(JNIEnv* env, jobject javaMethod) {
46 ScopedFastNativeObjectAccess soa(env);
47 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
48 if (!method->GetDeclaringClass()->IsAnnotation()) {
49 return nullptr;
50 }
51 return soa.AddLocalReference<jobject>(method->GetDexFile()->GetAnnotationDefaultValue(method));
52}
53
54static jobjectArray Method_getExceptionTypes(JNIEnv* env, jobject javaMethod) {
55 ScopedFastNativeObjectAccess soa(env);
56 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
Nicolas Geoffray3a090922015-11-24 09:17:30 +000057 if (method->GetDeclaringClass()->IsProxyClass()) {
Jeff Hao13e748b2015-08-25 20:44:19 +000058 mirror::Class* klass = method->GetDeclaringClass();
59 int throws_index = -1;
60 size_t i = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -070061 for (const auto& m : klass->GetDeclaredVirtualMethods(kRuntimePointerSize)) {
Jeff Hao13e748b2015-08-25 20:44:19 +000062 if (&m == method) {
63 throws_index = i;
64 break;
65 }
66 ++i;
67 }
68 CHECK_NE(throws_index, -1);
Nicolas Geoffray3a090922015-11-24 09:17:30 +000069 mirror::ObjectArray<mirror::Class>* declared_exceptions = klass->GetThrows()->Get(throws_index);
Jeff Hao13e748b2015-08-25 20:44:19 +000070 return soa.AddLocalReference<jobjectArray>(declared_exceptions->Clone(soa.Self()));
71 } else {
Jeff Hao2a5892f2015-08-31 15:00:40 -070072 mirror::ObjectArray<mirror::Class>* result_array =
Jeff Hao13e748b2015-08-25 20:44:19 +000073 method->GetDexFile()->GetExceptionTypesForMethod(method);
74 if (result_array == nullptr) {
75 // Return an empty array instead of a null pointer
76 mirror::Class* class_class = mirror::Class::GetJavaLangClass();
77 mirror::Class* class_array_class =
78 Runtime::Current()->GetClassLinker()->FindArrayClass(soa.Self(), &class_class);
Jeff Hao2a5892f2015-08-31 15:00:40 -070079 if (class_array_class == nullptr) {
80 return nullptr;
81 }
82 mirror::ObjectArray<mirror::Class>* empty_array =
83 mirror::ObjectArray<mirror::Class>::Alloc(soa.Self(), class_array_class, 0);
Jeff Hao13e748b2015-08-25 20:44:19 +000084 return soa.AddLocalReference<jobjectArray>(empty_array);
85 } else {
86 return soa.AddLocalReference<jobjectArray>(result_array);
87 }
88 }
89}
90
91static jobjectArray Method_getParameterAnnotationsNative(JNIEnv* env, jobject javaMethod) {
92 ScopedFastNativeObjectAccess soa(env);
93 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
Nicolas Geoffray3a090922015-11-24 09:17:30 +000094 if (method->GetDeclaringClass()->IsProxyClass()) {
Jeff Hao13e748b2015-08-25 20:44:19 +000095 return nullptr;
96 }
97 return soa.AddLocalReference<jobjectArray>(method->GetDexFile()->GetParameterAnnotations(method));
98}
99
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700100static jobject Method_invoke(JNIEnv* env, jobject javaMethod, jobject javaReceiver,
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700101 jobject javaArgs) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700102 ScopedFastNativeObjectAccess soa(env);
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700103 return InvokeMethod(soa, javaMethod, javaReceiver, javaArgs);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700104}
105
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700106static JNINativeMethod gMethods[] = {
Jeff Hao13e748b2015-08-25 20:44:19 +0000107 NATIVE_METHOD(Method, getAnnotationNative,
108 "!(Ljava/lang/Class;)Ljava/lang/annotation/Annotation;"),
Jeff Hao13e748b2015-08-25 20:44:19 +0000109 NATIVE_METHOD(Method, getDefaultValue, "!()Ljava/lang/Object;"),
110 NATIVE_METHOD(Method, getExceptionTypes, "!()[Ljava/lang/Class;"),
111 NATIVE_METHOD(Method, getParameterAnnotationsNative, "!()[[Ljava/lang/annotation/Annotation;"),
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700112 NATIVE_METHOD(Method, invoke, "!(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;"),
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700113};
114
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700115void register_java_lang_reflect_Method(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700116 REGISTER_NATIVE_METHODS("java/lang/reflect/Method");
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700117}
118
119} // namespace art