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_Parameter.h" 18 19 #include "android-base/stringprintf.h" 20 #include "nativehelper/jni_macros.h" 21 22 #include "art_method-inl.h" 23 #include "common_throws.h" 24 #include "dex_file-inl.h" 25 #include "dex_file_annotations.h" 26 #include "jni_internal.h" 27 #include "native_util.h" 28 #include "scoped_fast_native_object_access-inl.h" 29 #include "utils.h" 30 31 namespace art { 32 33 using android::base::StringPrintf; 34 35 static jobject Parameter_getAnnotationNative(JNIEnv* env, 36 jclass, 37 jobject javaMethod, 38 jint parameterIndex, 39 jclass annotationType) { 40 ScopedFastNativeObjectAccess soa(env); 41 if (UNLIKELY(javaMethod == nullptr)) { 42 ThrowNullPointerException("javaMethod == null"); 43 return nullptr; 44 } 45 46 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod); 47 if (method->IsProxyMethod()) { 48 return nullptr; 49 } 50 51 uint32_t parameter_count = method->GetParameterTypeList()->Size(); 52 if (UNLIKELY(parameterIndex < 0 || static_cast<uint32_t>(parameterIndex) >= parameter_count)) { 53 ThrowIllegalArgumentException( 54 StringPrintf("Illegal parameterIndex %d for %s, parameter_count is %d", 55 parameterIndex, 56 method->PrettyMethod().c_str(), 57 parameter_count).c_str()); 58 return nullptr; 59 } 60 61 StackHandleScope<1> hs(soa.Self()); 62 Handle<mirror::Class> klass(hs.NewHandle(soa.Decode<mirror::Class>(annotationType))); 63 return soa.AddLocalReference<jobject>( 64 annotations::GetAnnotationForMethodParameter(method, parameterIndex, klass)); 65 } 66 67 static JNINativeMethod gMethods[] = { 68 FAST_NATIVE_METHOD(Parameter, 69 getAnnotationNative, 70 "(Ljava/lang/reflect/Executable;ILjava/lang/Class;)Ljava/lang/annotation/Annotation;"), 71 }; 72 73 void register_java_lang_reflect_Parameter(JNIEnv* env) { 74 REGISTER_NATIVE_METHODS("java/lang/reflect/Parameter"); 75 } 76 77 } // namespace art 78