Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame^] | 17 | #include "java_lang_reflect_Executable.h" |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 18 | |
| 19 | #include "art_method-inl.h" |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 20 | #include "dex_file_annotations.h" |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 21 | #include "jni_internal.h" |
| 22 | #include "mirror/class-inl.h" |
| 23 | #include "mirror/object-inl.h" |
| 24 | #include "mirror/object_array-inl.h" |
| 25 | #include "reflection.h" |
| 26 | #include "scoped_fast_native_object_access.h" |
| 27 | #include "well_known_classes.h" |
| 28 | |
| 29 | namespace art { |
| 30 | |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame^] | 31 | static jobjectArray Executable_getDeclaredAnnotationsNative(JNIEnv* env, jobject javaMethod) { |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 32 | ScopedFastNativeObjectAccess soa(env); |
| 33 | ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod); |
| 34 | if (method->GetDeclaringClass()->IsProxyClass()) { |
| 35 | // Return an empty array instead of a null pointer. |
| 36 | mirror::Class* annotation_array_class = |
| 37 | soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_annotation_Annotation__array); |
| 38 | mirror::ObjectArray<mirror::Object>* empty_array = |
| 39 | mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), annotation_array_class, 0); |
| 40 | return soa.AddLocalReference<jobjectArray>(empty_array); |
| 41 | } |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 42 | return soa.AddLocalReference<jobjectArray>(annotations::GetAnnotationsForMethod(method)); |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame^] | 45 | static jobject Executable_getAnnotationNative(JNIEnv* env, |
Neil Fuller | 16b21cd | 2016-08-12 09:37:02 +0100 | [diff] [blame] | 46 | jobject javaMethod, |
| 47 | jclass annotationType) { |
| 48 | ScopedFastNativeObjectAccess soa(env); |
| 49 | StackHandleScope<1> hs(soa.Self()); |
| 50 | ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod); |
| 51 | if (method->IsProxyMethod()) { |
| 52 | return nullptr; |
| 53 | } else { |
| 54 | Handle<mirror::Class> klass(hs.NewHandle(soa.Decode<mirror::Class*>(annotationType))); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 55 | return soa.AddLocalReference<jobject>(annotations::GetAnnotationForMethod(method, klass)); |
Neil Fuller | 16b21cd | 2016-08-12 09:37:02 +0100 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame^] | 59 | static jobjectArray Executable_getSignatureAnnotation(JNIEnv* env, jobject javaMethod) { |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 60 | ScopedFastNativeObjectAccess soa(env); |
| 61 | ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod); |
| 62 | if (method->GetDeclaringClass()->IsProxyClass()) { |
| 63 | return nullptr; |
| 64 | } |
| 65 | StackHandleScope<1> hs(soa.Self()); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 66 | return soa.AddLocalReference<jobjectArray>(annotations::GetSignatureAnnotationForMethod(method)); |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame^] | 70 | static jobjectArray Executable_getParameterAnnotationsNative(JNIEnv* env, jobject javaMethod) { |
Neil Fuller | 16b21cd | 2016-08-12 09:37:02 +0100 | [diff] [blame] | 71 | ScopedFastNativeObjectAccess soa(env); |
| 72 | ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod); |
| 73 | if (method->IsProxyMethod()) { |
| 74 | return nullptr; |
| 75 | } else { |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 76 | return soa.AddLocalReference<jobjectArray>(annotations::GetParameterAnnotations(method)); |
Neil Fuller | 16b21cd | 2016-08-12 09:37:02 +0100 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame^] | 80 | static jboolean Executable_isAnnotationPresentNative(JNIEnv* env, |
Neil Fuller | 16b21cd | 2016-08-12 09:37:02 +0100 | [diff] [blame] | 81 | jobject javaMethod, |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 82 | jclass annotationType) { |
| 83 | ScopedFastNativeObjectAccess soa(env); |
| 84 | ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod); |
| 85 | if (method->GetDeclaringClass()->IsProxyClass()) { |
| 86 | return false; |
| 87 | } |
| 88 | StackHandleScope<1> hs(soa.Self()); |
| 89 | Handle<mirror::Class> klass(hs.NewHandle(soa.Decode<mirror::Class*>(annotationType))); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 90 | return annotations::IsMethodAnnotationPresent(method, klass); |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | static JNINativeMethod gMethods[] = { |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame^] | 94 | NATIVE_METHOD(Executable, getAnnotationNative, |
Neil Fuller | 16b21cd | 2016-08-12 09:37:02 +0100 | [diff] [blame] | 95 | "!(Ljava/lang/Class;)Ljava/lang/annotation/Annotation;"), |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame^] | 96 | NATIVE_METHOD(Executable, getDeclaredAnnotationsNative, "!()[Ljava/lang/annotation/Annotation;"), |
| 97 | NATIVE_METHOD(Executable, getParameterAnnotationsNative, |
Neil Fuller | 16b21cd | 2016-08-12 09:37:02 +0100 | [diff] [blame] | 98 | "!()[[Ljava/lang/annotation/Annotation;"), |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame^] | 99 | NATIVE_METHOD(Executable, getSignatureAnnotation, "!()[Ljava/lang/String;"), |
| 100 | NATIVE_METHOD(Executable, isAnnotationPresentNative, "!(Ljava/lang/Class;)Z"), |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 101 | }; |
| 102 | |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame^] | 103 | void register_java_lang_reflect_Executable(JNIEnv* env) { |
| 104 | REGISTER_NATIVE_METHODS("java/lang/reflect/Executable"); |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | } // namespace art |