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