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
17 #include "java_lang_reflect_Method.h"
18
19 #include "art_method-inl.h"
20 #include "class_linker.h"
21 #include "class_linker-inl.h"
22 #include "jni_internal.h"
23 #include "mirror/class-inl.h"
24 #include "mirror/object-inl.h"
25 #include "mirror/object_array-inl.h"
26 #include "reflection.h"
27 #include "scoped_fast_native_object_access.h"
28 #include "well_known_classes.h"
29
30 namespace art {
31
Method_getAnnotationNative(JNIEnv * env,jobject javaMethod,jclass annotationType)32 static jobject Method_getAnnotationNative(JNIEnv* env, jobject javaMethod, jclass annotationType) {
33 ScopedFastNativeObjectAccess soa(env);
34 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
35 if (method->GetDeclaringClass()->IsProxyClass()) {
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
Method_getDefaultValue(JNIEnv * env,jobject javaMethod)44 static jobject Method_getDefaultValue(JNIEnv* env, jobject javaMethod) {
45 ScopedFastNativeObjectAccess soa(env);
46 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
47 if (!method->GetDeclaringClass()->IsAnnotation()) {
48 return nullptr;
49 }
50 return soa.AddLocalReference<jobject>(method->GetDexFile()->GetAnnotationDefaultValue(method));
51 }
52
Method_getExceptionTypes(JNIEnv * env,jobject javaMethod)53 static jobjectArray Method_getExceptionTypes(JNIEnv* env, jobject javaMethod) {
54 ScopedFastNativeObjectAccess soa(env);
55 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
56 if (method->GetDeclaringClass()->IsProxyClass()) {
57 mirror::Class* klass = method->GetDeclaringClass();
58 int throws_index = -1;
59 size_t i = 0;
60 for (const auto& m : klass->GetDeclaredVirtualMethods(sizeof(void*))) {
61 if (&m == method) {
62 throws_index = i;
63 break;
64 }
65 ++i;
66 }
67 CHECK_NE(throws_index, -1);
68 mirror::ObjectArray<mirror::Class>* declared_exceptions = klass->GetThrows()->Get(throws_index);
69 return soa.AddLocalReference<jobjectArray>(declared_exceptions->Clone(soa.Self()));
70 } else {
71 mirror::ObjectArray<mirror::Class>* result_array =
72 method->GetDexFile()->GetExceptionTypesForMethod(method);
73 if (result_array == nullptr) {
74 // Return an empty array instead of a null pointer
75 mirror::Class* class_class = mirror::Class::GetJavaLangClass();
76 mirror::Class* class_array_class =
77 Runtime::Current()->GetClassLinker()->FindArrayClass(soa.Self(), &class_class);
78 if (class_array_class == nullptr) {
79 return nullptr;
80 }
81 mirror::ObjectArray<mirror::Class>* empty_array =
82 mirror::ObjectArray<mirror::Class>::Alloc(soa.Self(), class_array_class, 0);
83 return soa.AddLocalReference<jobjectArray>(empty_array);
84 } else {
85 return soa.AddLocalReference<jobjectArray>(result_array);
86 }
87 }
88 }
89
Method_getParameterAnnotationsNative(JNIEnv * env,jobject javaMethod)90 static jobjectArray Method_getParameterAnnotationsNative(JNIEnv* env, jobject javaMethod) {
91 ScopedFastNativeObjectAccess soa(env);
92 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
93 if (method->GetDeclaringClass()->IsProxyClass()) {
94 return nullptr;
95 }
96 return soa.AddLocalReference<jobjectArray>(method->GetDexFile()->GetParameterAnnotations(method));
97 }
98
Method_invoke(JNIEnv * env,jobject javaMethod,jobject javaReceiver,jobject javaArgs)99 static jobject Method_invoke(JNIEnv* env, jobject javaMethod, jobject javaReceiver,
100 jobject javaArgs) {
101 ScopedFastNativeObjectAccess soa(env);
102 return InvokeMethod(soa, javaMethod, javaReceiver, javaArgs);
103 }
104
105 static JNINativeMethod gMethods[] = {
106 NATIVE_METHOD(Method, getAnnotationNative,
107 "!(Ljava/lang/Class;)Ljava/lang/annotation/Annotation;"),
108 NATIVE_METHOD(Method, getDefaultValue, "!()Ljava/lang/Object;"),
109 NATIVE_METHOD(Method, getExceptionTypes, "!()[Ljava/lang/Class;"),
110 NATIVE_METHOD(Method, getParameterAnnotationsNative, "!()[[Ljava/lang/annotation/Annotation;"),
111 NATIVE_METHOD(Method, invoke, "!(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;"),
112 };
113
register_java_lang_reflect_Method(JNIEnv * env)114 void register_java_lang_reflect_Method(JNIEnv* env) {
115 REGISTER_NATIVE_METHODS("java/lang/reflect/Method");
116 }
117
118 } // namespace art
119