• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "nativehelper/jni_macros.h"
20 
21 #include "art_method-inl.h"
22 #include "base/enums.h"
23 #include "class_linker-inl.h"
24 #include "class_linker.h"
25 #include "dex/dex_file_annotations.h"
26 #include "jni_internal.h"
27 #include "mirror/class-inl.h"
28 #include "mirror/object-inl.h"
29 #include "mirror/object_array-inl.h"
30 #include "native_util.h"
31 #include "reflection.h"
32 #include "scoped_fast_native_object_access-inl.h"
33 #include "well_known_classes.h"
34 
35 namespace art {
36 
Method_getDefaultValue(JNIEnv * env,jobject javaMethod)37 static jobject Method_getDefaultValue(JNIEnv* env, jobject javaMethod) {
38   ScopedFastNativeObjectAccess soa(env);
39   ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
40   if (!method->GetDeclaringClass()->IsAnnotation()) {
41     return nullptr;
42   }
43   return soa.AddLocalReference<jobject>(annotations::GetAnnotationDefaultValue(method));
44 }
45 
Method_getExceptionTypes(JNIEnv * env,jobject javaMethod)46 static jobjectArray Method_getExceptionTypes(JNIEnv* env, jobject javaMethod) {
47   ScopedFastNativeObjectAccess soa(env);
48   ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
49   if (method->GetDeclaringClass()->IsProxyClass()) {
50     ObjPtr<mirror::Class> klass = method->GetDeclaringClass();
51     int throws_index = -1;
52     size_t i = 0;
53     for (const auto& m : klass->GetDeclaredVirtualMethods(kRuntimePointerSize)) {
54       if (&m == method) {
55         throws_index = i;
56         break;
57       }
58       ++i;
59     }
60     CHECK_NE(throws_index, -1);
61     mirror::ObjectArray<mirror::Class>* declared_exceptions =
62         klass->GetProxyThrows()->Get(throws_index);
63     return soa.AddLocalReference<jobjectArray>(declared_exceptions->Clone(soa.Self()));
64   } else {
65     mirror::ObjectArray<mirror::Class>* result_array =
66         annotations::GetExceptionTypesForMethod(method);
67     if (result_array == nullptr) {
68       // Return an empty array instead of a null pointer
69       ObjPtr<mirror::Class> class_class = mirror::Class::GetJavaLangClass();
70       ObjPtr<mirror::Class> class_array_class =
71           Runtime::Current()->GetClassLinker()->FindArrayClass(soa.Self(), &class_class);
72       if (class_array_class == nullptr) {
73         return nullptr;
74       }
75       mirror::ObjectArray<mirror::Class>* empty_array =
76           mirror::ObjectArray<mirror::Class>::Alloc(soa.Self(), class_array_class, 0);
77       return soa.AddLocalReference<jobjectArray>(empty_array);
78     } else {
79       return soa.AddLocalReference<jobjectArray>(result_array);
80     }
81   }
82 }
83 
Method_invoke(JNIEnv * env,jobject javaMethod,jobject javaReceiver,jobjectArray javaArgs)84 static jobject Method_invoke(JNIEnv* env, jobject javaMethod, jobject javaReceiver,
85                              jobjectArray javaArgs) {
86   ScopedFastNativeObjectAccess soa(env);
87   return InvokeMethod(soa, javaMethod, javaReceiver, javaArgs);
88 }
89 
90 static JNINativeMethod gMethods[] = {
91   FAST_NATIVE_METHOD(Method, getDefaultValue, "()Ljava/lang/Object;"),
92   FAST_NATIVE_METHOD(Method, getExceptionTypes, "()[Ljava/lang/Class;"),
93   FAST_NATIVE_METHOD(Method, invoke, "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;"),
94 };
95 
register_java_lang_reflect_Method(JNIEnv * env)96 void register_java_lang_reflect_Method(JNIEnv* env) {
97   REGISTER_NATIVE_METHODS("java/lang/reflect/Method");
98 }
99 
100 }  // namespace art
101