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 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 19 | #include "art_method-inl.h" |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 20 | #include "class_linker.h" |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 21 | #include "class_linker-inl.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 22 | #include "jni_internal.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 23 | #include "mirror/class-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 24 | #include "mirror/object-inl.h" |
| 25 | #include "mirror/object_array-inl.h" |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 26 | #include "reflection.h" |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 27 | #include "scoped_fast_native_object_access.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 28 | #include "well_known_classes.h" |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 29 | |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 30 | namespace art { |
| 31 | |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 32 | static jobject Method_getAnnotationNative(JNIEnv* env, jobject javaMethod, jclass annotationType) { |
| 33 | ScopedFastNativeObjectAccess soa(env); |
| 34 | ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod); |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 35 | if (method->GetDeclaringClass()->IsProxyClass()) { |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 36 | 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 | |
| 44 | static jobjectArray Method_getDeclaredAnnotations(JNIEnv* env, jobject javaMethod) { |
| 45 | ScopedFastNativeObjectAccess soa(env); |
| 46 | ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod); |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 47 | if (method->GetDeclaringClass()->IsProxyClass()) { |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 48 | // 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 | |
| 58 | static 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 | |
| 67 | static jobjectArray Method_getExceptionTypes(JNIEnv* env, jobject javaMethod) { |
| 68 | ScopedFastNativeObjectAccess soa(env); |
| 69 | ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod); |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 70 | if (method->GetDeclaringClass()->IsProxyClass()) { |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 71 | mirror::Class* klass = method->GetDeclaringClass(); |
| 72 | int throws_index = -1; |
| 73 | size_t i = 0; |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 74 | for (const auto& m : klass->GetDeclaredVirtualMethods(sizeof(void*))) { |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 75 | if (&m == method) { |
| 76 | throws_index = i; |
| 77 | break; |
| 78 | } |
| 79 | ++i; |
| 80 | } |
| 81 | CHECK_NE(throws_index, -1); |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 82 | mirror::ObjectArray<mirror::Class>* declared_exceptions = klass->GetThrows()->Get(throws_index); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 83 | return soa.AddLocalReference<jobjectArray>(declared_exceptions->Clone(soa.Self())); |
| 84 | } else { |
Jeff Hao | 2a5892f | 2015-08-31 15:00:40 -0700 | [diff] [blame] | 85 | mirror::ObjectArray<mirror::Class>* result_array = |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 86 | method->GetDexFile()->GetExceptionTypesForMethod(method); |
| 87 | if (result_array == nullptr) { |
| 88 | // Return an empty array instead of a null pointer |
| 89 | mirror::Class* class_class = mirror::Class::GetJavaLangClass(); |
| 90 | mirror::Class* class_array_class = |
| 91 | Runtime::Current()->GetClassLinker()->FindArrayClass(soa.Self(), &class_class); |
Jeff Hao | 2a5892f | 2015-08-31 15:00:40 -0700 | [diff] [blame] | 92 | if (class_array_class == nullptr) { |
| 93 | return nullptr; |
| 94 | } |
| 95 | mirror::ObjectArray<mirror::Class>* empty_array = |
| 96 | mirror::ObjectArray<mirror::Class>::Alloc(soa.Self(), class_array_class, 0); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 97 | return soa.AddLocalReference<jobjectArray>(empty_array); |
| 98 | } else { |
| 99 | return soa.AddLocalReference<jobjectArray>(result_array); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | static jobjectArray Method_getParameterAnnotationsNative(JNIEnv* env, jobject javaMethod) { |
| 105 | ScopedFastNativeObjectAccess soa(env); |
| 106 | ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod); |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 107 | if (method->GetDeclaringClass()->IsProxyClass()) { |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 108 | return nullptr; |
| 109 | } |
| 110 | return soa.AddLocalReference<jobjectArray>(method->GetDexFile()->GetParameterAnnotations(method)); |
| 111 | } |
| 112 | |
Jeff Hao | 11d5d8f | 2014-03-26 15:08:20 -0700 | [diff] [blame] | 113 | static jobject Method_invoke(JNIEnv* env, jobject javaMethod, jobject javaReceiver, |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 114 | jobject javaArgs) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 115 | ScopedFastNativeObjectAccess soa(env); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 116 | return InvokeMethod(soa, javaMethod, javaReceiver, javaArgs); |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 119 | static jboolean Method_isAnnotationPresentNative(JNIEnv* env, jobject javaMethod, |
| 120 | jclass annotationType) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 121 | ScopedFastNativeObjectAccess soa(env); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 122 | ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod); |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 123 | if (method->GetDeclaringClass()->IsProxyClass()) { |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 124 | return false; |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 125 | } |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 126 | StackHandleScope<1> hs(soa.Self()); |
| 127 | Handle<mirror::Class> klass(hs.NewHandle(soa.Decode<mirror::Class*>(annotationType))); |
| 128 | return method->GetDexFile()->IsMethodAnnotationPresent(method, klass); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 131 | static JNINativeMethod gMethods[] = { |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 132 | NATIVE_METHOD(Method, getAnnotationNative, |
| 133 | "!(Ljava/lang/Class;)Ljava/lang/annotation/Annotation;"), |
| 134 | NATIVE_METHOD(Method, getDeclaredAnnotations, "!()[Ljava/lang/annotation/Annotation;"), |
| 135 | NATIVE_METHOD(Method, getDefaultValue, "!()Ljava/lang/Object;"), |
| 136 | NATIVE_METHOD(Method, getExceptionTypes, "!()[Ljava/lang/Class;"), |
| 137 | NATIVE_METHOD(Method, getParameterAnnotationsNative, "!()[[Ljava/lang/annotation/Annotation;"), |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 138 | NATIVE_METHOD(Method, invoke, "!(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;"), |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 139 | NATIVE_METHOD(Method, isAnnotationPresentNative, "!(Ljava/lang/Class;)Z"), |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 140 | }; |
| 141 | |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 142 | void register_java_lang_reflect_Method(JNIEnv* env) { |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 143 | REGISTER_NATIVE_METHODS("java/lang/reflect/Method"); |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | } // namespace art |