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