blob: 7894c9bcfcb87e9f2c60082cb7cb16d0024807a2 [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"
Brian Carlstromf867b6f2011-09-16 12:17:25 -070020#include "class_linker.h"
Jeff Hao13e748b2015-08-25 20:44:19 +000021#include "class_linker-inl.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070022#include "jni_internal.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080023#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080024#include "mirror/object-inl.h"
25#include "mirror/object_array-inl.h"
Elliott Hughes418d20f2011-09-22 14:00:39 -070026#include "reflection.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070027#include "scoped_fast_native_object_access.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070028#include "well_known_classes.h"
Brian Carlstromf867b6f2011-09-16 12:17:25 -070029
Brian Carlstromf867b6f2011-09-16 12:17:25 -070030namespace art {
31
Jeff Hao13e748b2015-08-25 20:44:19 +000032static jobject Method_getAnnotationNative(JNIEnv* env, jobject javaMethod, jclass annotationType) {
33 ScopedFastNativeObjectAccess soa(env);
34 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
Igor Murashkin457e8742015-10-22 17:37:50 -070035 if (method->GetDeclaringClass()->IsAnyProxyClass()) {
Jeff Hao13e748b2015-08-25 20:44:19 +000036 return nullptr;
37 }
38 StackHandleScope<1> hs(soa.Self());
39 Handle<mirror::Class> klass(hs.NewHandle(soa.Decode<mirror::Class*>(annotationType)));
40 return soa.AddLocalReference<jobject>(
41 method->GetDexFile()->GetAnnotationForMethod(method, klass));
42}
43
44static jobjectArray Method_getDeclaredAnnotations(JNIEnv* env, jobject javaMethod) {
45 ScopedFastNativeObjectAccess soa(env);
46 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
Igor Murashkin457e8742015-10-22 17:37:50 -070047 if (method->GetDeclaringClass()->IsAnyProxyClass()) {
Jeff Hao13e748b2015-08-25 20:44:19 +000048 // Return an empty array instead of a null pointer.
49 mirror::Class* annotation_array_class =
50 soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_annotation_Annotation__array);
51 mirror::ObjectArray<mirror::Object>* empty_array =
52 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), annotation_array_class, 0);
53 return soa.AddLocalReference<jobjectArray>(empty_array);
54 }
55 return soa.AddLocalReference<jobjectArray>(method->GetDexFile()->GetAnnotationsForMethod(method));
56}
57
58static jobject Method_getDefaultValue(JNIEnv* env, jobject javaMethod) {
59 ScopedFastNativeObjectAccess soa(env);
60 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
61 if (!method->GetDeclaringClass()->IsAnnotation()) {
62 return nullptr;
63 }
64 return soa.AddLocalReference<jobject>(method->GetDexFile()->GetAnnotationDefaultValue(method));
65}
66
67static jobjectArray Method_getExceptionTypes(JNIEnv* env, jobject javaMethod) {
68 ScopedFastNativeObjectAccess soa(env);
69 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
Igor Murashkin457e8742015-10-22 17:37:50 -070070 if (method->GetDeclaringClass()->IsAnyProxyClass()) {
Jeff Hao13e748b2015-08-25 20:44:19 +000071 mirror::Class* klass = method->GetDeclaringClass();
72 int throws_index = -1;
73 size_t i = 0;
74 for (const auto& m : klass->GetVirtualMethods(sizeof(void*))) {
75 if (&m == method) {
76 throws_index = i;
77 break;
78 }
79 ++i;
80 }
81 CHECK_NE(throws_index, -1);
Igor Murashkin457e8742015-10-22 17:37:50 -070082 mirror::ObjectArray<mirror::Class>* declared_exceptions =
83 klass->GetThrowsForAnyProxy()->Get(throws_index);
Jeff Hao13e748b2015-08-25 20:44:19 +000084 return soa.AddLocalReference<jobjectArray>(declared_exceptions->Clone(soa.Self()));
85 } else {
Jeff Hao2a5892f2015-08-31 15:00:40 -070086 mirror::ObjectArray<mirror::Class>* result_array =
Jeff Hao13e748b2015-08-25 20:44:19 +000087 method->GetDexFile()->GetExceptionTypesForMethod(method);
88 if (result_array == nullptr) {
89 // Return an empty array instead of a null pointer
90 mirror::Class* class_class = mirror::Class::GetJavaLangClass();
91 mirror::Class* class_array_class =
92 Runtime::Current()->GetClassLinker()->FindArrayClass(soa.Self(), &class_class);
Jeff Hao2a5892f2015-08-31 15:00:40 -070093 if (class_array_class == nullptr) {
94 return nullptr;
95 }
96 mirror::ObjectArray<mirror::Class>* empty_array =
97 mirror::ObjectArray<mirror::Class>::Alloc(soa.Self(), class_array_class, 0);
Jeff Hao13e748b2015-08-25 20:44:19 +000098 return soa.AddLocalReference<jobjectArray>(empty_array);
99 } else {
100 return soa.AddLocalReference<jobjectArray>(result_array);
101 }
102 }
103}
104
105static jobjectArray Method_getParameterAnnotationsNative(JNIEnv* env, jobject javaMethod) {
106 ScopedFastNativeObjectAccess soa(env);
107 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
Igor Murashkin457e8742015-10-22 17:37:50 -0700108 if (method->GetDeclaringClass()->IsAnyProxyClass()) {
Jeff Hao13e748b2015-08-25 20:44:19 +0000109 return nullptr;
110 }
111 return soa.AddLocalReference<jobjectArray>(method->GetDexFile()->GetParameterAnnotations(method));
112}
113
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700114static jobject Method_invoke(JNIEnv* env, jobject javaMethod, jobject javaReceiver,
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700115 jobject javaArgs) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700116 ScopedFastNativeObjectAccess soa(env);
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700117 return InvokeMethod(soa, javaMethod, javaReceiver, javaArgs);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700118}
119
Jeff Hao13e748b2015-08-25 20:44:19 +0000120static jboolean Method_isAnnotationPresentNative(JNIEnv* env, jobject javaMethod,
121 jclass annotationType) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700122 ScopedFastNativeObjectAccess soa(env);
Jeff Hao13e748b2015-08-25 20:44:19 +0000123 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
Igor Murashkin457e8742015-10-22 17:37:50 -0700124 if (method->GetDeclaringClass()->IsAnyProxyClass()) {
Jeff Hao13e748b2015-08-25 20:44:19 +0000125 return false;
Ian Rogersc2b44472011-12-14 21:17:17 -0800126 }
Jeff Hao13e748b2015-08-25 20:44:19 +0000127 StackHandleScope<1> hs(soa.Self());
128 Handle<mirror::Class> klass(hs.NewHandle(soa.Decode<mirror::Class*>(annotationType)));
129 return method->GetDexFile()->IsMethodAnnotationPresent(method, klass);
Ian Rogersc2b44472011-12-14 21:17:17 -0800130}
131
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700132static JNINativeMethod gMethods[] = {
Jeff Hao13e748b2015-08-25 20:44:19 +0000133 NATIVE_METHOD(Method, getAnnotationNative,
134 "!(Ljava/lang/Class;)Ljava/lang/annotation/Annotation;"),
135 NATIVE_METHOD(Method, getDeclaredAnnotations, "!()[Ljava/lang/annotation/Annotation;"),
136 NATIVE_METHOD(Method, getDefaultValue, "!()Ljava/lang/Object;"),
137 NATIVE_METHOD(Method, getExceptionTypes, "!()[Ljava/lang/Class;"),
138 NATIVE_METHOD(Method, getParameterAnnotationsNative, "!()[[Ljava/lang/annotation/Annotation;"),
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700139 NATIVE_METHOD(Method, invoke, "!(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;"),
Jeff Hao13e748b2015-08-25 20:44:19 +0000140 NATIVE_METHOD(Method, isAnnotationPresentNative, "!(Ljava/lang/Class;)Z"),
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700141};
142
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700143void register_java_lang_reflect_Method(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700144 REGISTER_NATIVE_METHODS("java/lang/reflect/Method");
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700145}
146
147} // namespace art