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" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 20 | #include "base/enums.h" |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 21 | #include "class_linker.h" |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 22 | #include "class_linker-inl.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 23 | #include "jni_internal.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 24 | #include "mirror/class-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 25 | #include "mirror/object-inl.h" |
| 26 | #include "mirror/object_array-inl.h" |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 27 | #include "reflection.h" |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 28 | #include "scoped_fast_native_object_access.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 29 | #include "well_known_classes.h" |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 30 | |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 31 | namespace art { |
| 32 | |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 33 | static jobject Method_getAnnotationNative(JNIEnv* env, jobject javaMethod, jclass annotationType) { |
| 34 | ScopedFastNativeObjectAccess soa(env); |
| 35 | ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod); |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 36 | if (method->GetDeclaringClass()->IsProxyClass()) { |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 37 | 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 Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 45 | static 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 | |
| 54 | static jobjectArray Method_getExceptionTypes(JNIEnv* env, jobject javaMethod) { |
| 55 | ScopedFastNativeObjectAccess soa(env); |
| 56 | ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod); |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 57 | if (method->GetDeclaringClass()->IsProxyClass()) { |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 58 | mirror::Class* klass = method->GetDeclaringClass(); |
| 59 | int throws_index = -1; |
| 60 | size_t i = 0; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 61 | for (const auto& m : klass->GetDeclaredVirtualMethods(kRuntimePointerSize)) { |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 62 | if (&m == method) { |
| 63 | throws_index = i; |
| 64 | break; |
| 65 | } |
| 66 | ++i; |
| 67 | } |
| 68 | CHECK_NE(throws_index, -1); |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 69 | mirror::ObjectArray<mirror::Class>* declared_exceptions = klass->GetThrows()->Get(throws_index); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 70 | return soa.AddLocalReference<jobjectArray>(declared_exceptions->Clone(soa.Self())); |
| 71 | } else { |
Jeff Hao | 2a5892f | 2015-08-31 15:00:40 -0700 | [diff] [blame] | 72 | mirror::ObjectArray<mirror::Class>* result_array = |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 73 | 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 Hao | 2a5892f | 2015-08-31 15:00:40 -0700 | [diff] [blame] | 79 | 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 Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 84 | return soa.AddLocalReference<jobjectArray>(empty_array); |
| 85 | } else { |
| 86 | return soa.AddLocalReference<jobjectArray>(result_array); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | static jobjectArray Method_getParameterAnnotationsNative(JNIEnv* env, jobject javaMethod) { |
| 92 | ScopedFastNativeObjectAccess soa(env); |
| 93 | ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod); |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 94 | if (method->GetDeclaringClass()->IsProxyClass()) { |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 95 | return nullptr; |
| 96 | } |
| 97 | return soa.AddLocalReference<jobjectArray>(method->GetDexFile()->GetParameterAnnotations(method)); |
| 98 | } |
| 99 | |
Jeff Hao | 11d5d8f | 2014-03-26 15:08:20 -0700 | [diff] [blame] | 100 | static jobject Method_invoke(JNIEnv* env, jobject javaMethod, jobject javaReceiver, |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 101 | jobject javaArgs) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 102 | ScopedFastNativeObjectAccess soa(env); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 103 | return InvokeMethod(soa, javaMethod, javaReceiver, javaArgs); |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 106 | static JNINativeMethod gMethods[] = { |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 107 | NATIVE_METHOD(Method, getAnnotationNative, |
| 108 | "!(Ljava/lang/Class;)Ljava/lang/annotation/Annotation;"), |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 109 | 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 Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 112 | 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] | 113 | }; |
| 114 | |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 115 | void register_java_lang_reflect_Method(JNIEnv* env) { |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 116 | REGISTER_NATIVE_METHODS("java/lang/reflect/Method"); |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | } // namespace art |