blob: 8fcf6aca08b51028d289aaa77f063aa291d6f485 [file] [log] [blame]
Jeff Hao1133db72016-04-04 19:50:14 -07001/*
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 Fuller0e844392016-09-08 13:43:31 +010017#include "java_lang_reflect_Executable.h"
Jeff Hao1133db72016-04-04 19:50:14 -070018
19#include "art_method-inl.h"
David Sehr9323e6e2016-09-13 08:58:35 -070020#include "dex_file_annotations.h"
Jeff Hao1133db72016-04-04 19:50:14 -070021#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
29namespace art {
30
Neil Fuller0e844392016-09-08 13:43:31 +010031static jobjectArray Executable_getDeclaredAnnotationsNative(JNIEnv* env, jobject javaMethod) {
Jeff Hao1133db72016-04-04 19:50:14 -070032 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 Sehr9323e6e2016-09-13 08:58:35 -070042 return soa.AddLocalReference<jobjectArray>(annotations::GetAnnotationsForMethod(method));
Jeff Hao1133db72016-04-04 19:50:14 -070043}
44
Neil Fuller0e844392016-09-08 13:43:31 +010045static jobject Executable_getAnnotationNative(JNIEnv* env,
Neil Fuller16b21cd2016-08-12 09:37:02 +010046 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 Sehr9323e6e2016-09-13 08:58:35 -070055 return soa.AddLocalReference<jobject>(annotations::GetAnnotationForMethod(method, klass));
Neil Fuller16b21cd2016-08-12 09:37:02 +010056 }
57}
58
Neil Fuller0e844392016-09-08 13:43:31 +010059static jobjectArray Executable_getSignatureAnnotation(JNIEnv* env, jobject javaMethod) {
Jeff Hao1133db72016-04-04 19:50:14 -070060 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 Sehr9323e6e2016-09-13 08:58:35 -070066 return soa.AddLocalReference<jobjectArray>(annotations::GetSignatureAnnotationForMethod(method));
Jeff Hao1133db72016-04-04 19:50:14 -070067}
68
69
Neil Fuller0e844392016-09-08 13:43:31 +010070static jobjectArray Executable_getParameterAnnotationsNative(JNIEnv* env, jobject javaMethod) {
Neil Fuller16b21cd2016-08-12 09:37:02 +010071 ScopedFastNativeObjectAccess soa(env);
72 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
73 if (method->IsProxyMethod()) {
74 return nullptr;
75 } else {
David Sehr9323e6e2016-09-13 08:58:35 -070076 return soa.AddLocalReference<jobjectArray>(annotations::GetParameterAnnotations(method));
Neil Fuller16b21cd2016-08-12 09:37:02 +010077 }
78}
79
Neil Fuller0e844392016-09-08 13:43:31 +010080static jboolean Executable_isAnnotationPresentNative(JNIEnv* env,
Neil Fuller16b21cd2016-08-12 09:37:02 +010081 jobject javaMethod,
Jeff Hao1133db72016-04-04 19:50:14 -070082 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 Sehr9323e6e2016-09-13 08:58:35 -070090 return annotations::IsMethodAnnotationPresent(method, klass);
Jeff Hao1133db72016-04-04 19:50:14 -070091}
92
93static JNINativeMethod gMethods[] = {
Neil Fuller0e844392016-09-08 13:43:31 +010094 NATIVE_METHOD(Executable, getAnnotationNative,
Neil Fuller16b21cd2016-08-12 09:37:02 +010095 "!(Ljava/lang/Class;)Ljava/lang/annotation/Annotation;"),
Neil Fuller0e844392016-09-08 13:43:31 +010096 NATIVE_METHOD(Executable, getDeclaredAnnotationsNative, "!()[Ljava/lang/annotation/Annotation;"),
97 NATIVE_METHOD(Executable, getParameterAnnotationsNative,
Neil Fuller16b21cd2016-08-12 09:37:02 +010098 "!()[[Ljava/lang/annotation/Annotation;"),
Neil Fuller0e844392016-09-08 13:43:31 +010099 NATIVE_METHOD(Executable, getSignatureAnnotation, "!()[Ljava/lang/String;"),
100 NATIVE_METHOD(Executable, isAnnotationPresentNative, "!(Ljava/lang/Class;)Z"),
Jeff Hao1133db72016-04-04 19:50:14 -0700101};
102
Neil Fuller0e844392016-09-08 13:43:31 +0100103void register_java_lang_reflect_Executable(JNIEnv* env) {
104 REGISTER_NATIVE_METHODS("java/lang/reflect/Executable");
Jeff Hao1133db72016-04-04 19:50:14 -0700105}
106
107} // namespace art