blob: 92a7ac9836df39425b158ee984d3fd1d31342743 [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"
Andreas Gampea14100c2017-04-24 15:09:56 -070020#include "nativehelper/jni_macros.h"
Andreas Gampe46ee31b2016-12-14 10:11:49 -080021
Neil Fuller60458a02016-09-01 15:32:44 +010022#include "art_method-inl.h"
23#include "common_throws.h"
24#include "dex_file-inl.h"
David Sehr9323e6e2016-09-13 08:58:35 -070025#include "dex_file_annotations.h"
Neil Fuller60458a02016-09-01 15:32:44 +010026#include "jni_internal.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070027#include "scoped_fast_native_object_access-inl.h"
Neil Fuller60458a02016-09-01 15:32:44 +010028#include "utils.h"
29
30namespace art {
31
Andreas Gampe46ee31b2016-12-14 10:11:49 -080032using android::base::StringPrintf;
33
Neil Fuller60458a02016-09-01 15:32:44 +010034static jobject Parameter_getAnnotationNative(JNIEnv* env,
35 jclass,
36 jobject javaMethod,
37 jint parameterIndex,
38 jclass annotationType) {
39 ScopedFastNativeObjectAccess soa(env);
40 if (UNLIKELY(javaMethod == nullptr)) {
41 ThrowNullPointerException("javaMethod == null");
42 return nullptr;
43 }
44
45 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
46 if (method->IsProxyMethod()) {
47 return nullptr;
48 }
49
50 uint32_t parameter_count = method->GetParameterTypeList()->Size();
51 if (UNLIKELY(parameterIndex < 0 || static_cast<uint32_t>(parameterIndex) >= parameter_count)) {
52 ThrowIllegalArgumentException(
53 StringPrintf("Illegal parameterIndex %d for %s, parameter_count is %d",
54 parameterIndex,
David Sehr709b0702016-10-13 09:12:37 -070055 method->PrettyMethod().c_str(),
Neil Fuller60458a02016-09-01 15:32:44 +010056 parameter_count).c_str());
57 return nullptr;
58 }
59
60 StackHandleScope<1> hs(soa.Self());
Mathieu Chartier0795f232016-09-27 18:43:30 -070061 Handle<mirror::Class> klass(hs.NewHandle(soa.Decode<mirror::Class>(annotationType)));
Neil Fuller60458a02016-09-01 15:32:44 +010062 return soa.AddLocalReference<jobject>(
David Sehr9323e6e2016-09-13 08:58:35 -070063 annotations::GetAnnotationForMethodParameter(method, parameterIndex, klass));
Neil Fuller60458a02016-09-01 15:32:44 +010064}
65
66static JNINativeMethod gMethods[] = {
Igor Murashkin3b6f4402017-02-16 16:13:17 -080067 FAST_NATIVE_METHOD(Parameter,
Neil Fuller60458a02016-09-01 15:32:44 +010068 getAnnotationNative,
Igor Murashkin3b6f4402017-02-16 16:13:17 -080069 "(Ljava/lang/reflect/Executable;ILjava/lang/Class;)Ljava/lang/annotation/Annotation;"),
Neil Fuller60458a02016-09-01 15:32:44 +010070};
71
72void register_java_lang_reflect_Parameter(JNIEnv* env) {
73 REGISTER_NATIVE_METHODS("java/lang/reflect/Parameter");
74}
75
76} // namespace art