blob: 0bb9e382d3a8854484d40babb0f014debcc85147 [file] [log] [blame]
Neil Fuller60458a02016-09-01 15:32:44 +01001/*
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
Andreas Gampe46ee31b2016-12-14 10:11:49 -080019#include "android-base/stringprintf.h"
20
Neil Fuller60458a02016-09-01 15:32:44 +010021#include "art_method-inl.h"
22#include "common_throws.h"
23#include "dex_file-inl.h"
David Sehr9323e6e2016-09-13 08:58:35 -070024#include "dex_file_annotations.h"
Neil Fuller60458a02016-09-01 15:32:44 +010025#include "jni_internal.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070026#include "scoped_fast_native_object_access-inl.h"
Neil Fuller60458a02016-09-01 15:32:44 +010027#include "utils.h"
28
29namespace art {
30
Andreas Gampe46ee31b2016-12-14 10:11:49 -080031using android::base::StringPrintf;
32
Neil Fuller60458a02016-09-01 15:32:44 +010033static 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,
David Sehr709b0702016-10-13 09:12:37 -070054 method->PrettyMethod().c_str(),
Neil Fuller60458a02016-09-01 15:32:44 +010055 parameter_count).c_str());
56 return nullptr;
57 }
58
59 StackHandleScope<1> hs(soa.Self());
Mathieu Chartier0795f232016-09-27 18:43:30 -070060 Handle<mirror::Class> klass(hs.NewHandle(soa.Decode<mirror::Class>(annotationType)));
Neil Fuller60458a02016-09-01 15:32:44 +010061 return soa.AddLocalReference<jobject>(
David Sehr9323e6e2016-09-13 08:58:35 -070062 annotations::GetAnnotationForMethodParameter(method, parameterIndex, klass));
Neil Fuller60458a02016-09-01 15:32:44 +010063}
64
65static JNINativeMethod gMethods[] = {
66 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