blob: c4ab5d69fcd82af28267e1378b93d321abf6d975 [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"
Andreas Gampe87583b32017-05-25 11:22:18 -070027#include "native_util.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070028#include "scoped_fast_native_object_access-inl.h"
Neil Fuller60458a02016-09-01 15:32:44 +010029#include "utils.h"
30
31namespace art {
32
Andreas Gampe46ee31b2016-12-14 10:11:49 -080033using android::base::StringPrintf;
34
Neil Fuller60458a02016-09-01 15:32:44 +010035static 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,
David Sehr709b0702016-10-13 09:12:37 -070056 method->PrettyMethod().c_str(),
Neil Fuller60458a02016-09-01 15:32:44 +010057 parameter_count).c_str());
58 return nullptr;
59 }
60
61 StackHandleScope<1> hs(soa.Self());
Mathieu Chartier0795f232016-09-27 18:43:30 -070062 Handle<mirror::Class> klass(hs.NewHandle(soa.Decode<mirror::Class>(annotationType)));
Neil Fuller60458a02016-09-01 15:32:44 +010063 return soa.AddLocalReference<jobject>(
David Sehr9323e6e2016-09-13 08:58:35 -070064 annotations::GetAnnotationForMethodParameter(method, parameterIndex, klass));
Neil Fuller60458a02016-09-01 15:32:44 +010065}
66
67static JNINativeMethod gMethods[] = {
Igor Murashkin3b6f4402017-02-16 16:13:17 -080068 FAST_NATIVE_METHOD(Parameter,
Neil Fuller60458a02016-09-01 15:32:44 +010069 getAnnotationNative,
Igor Murashkin3b6f4402017-02-16 16:13:17 -080070 "(Ljava/lang/reflect/Executable;ILjava/lang/Class;)Ljava/lang/annotation/Annotation;"),
Neil Fuller60458a02016-09-01 15:32:44 +010071};
72
73void register_java_lang_reflect_Parameter(JNIEnv* env) {
74 REGISTER_NATIVE_METHODS("java/lang/reflect/Parameter");
75}
76
77} // namespace art