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