blob: f209f1d73ad1cb227f13f7ed1a998002569b01e4 [file] [log] [blame]
Jeff Hao1133db72016-04-04 19:50:14 -07001/*
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
Neil Fuller0e844392016-09-08 13:43:31 +010017#include "java_lang_reflect_Executable.h"
Jeff Hao1133db72016-04-04 19:50:14 -070018
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
Jeff Hao1133db72016-04-04 19:50:14 -070022#include "art_method-inl.h"
David Sehr9323e6e2016-09-13 08:58:35 -070023#include "dex_file_annotations.h"
Neil Fuller79a21e72016-09-09 14:24:51 +010024#include "handle.h"
Jeff Hao1133db72016-04-04 19:50:14 -070025#include "jni_internal.h"
26#include "mirror/class-inl.h"
Neil Fuller79a21e72016-09-09 14:24:51 +010027#include "mirror/method.h"
Jeff Hao1133db72016-04-04 19:50:14 -070028#include "mirror/object-inl.h"
29#include "mirror/object_array-inl.h"
Andreas Gampe87583b32017-05-25 11:22:18 -070030#include "native_util.h"
Jeff Hao1133db72016-04-04 19:50:14 -070031#include "reflection.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070032#include "scoped_fast_native_object_access-inl.h"
Jeff Hao1133db72016-04-04 19:50:14 -070033#include "well_known_classes.h"
34
35namespace art {
36
Andreas Gampe46ee31b2016-12-14 10:11:49 -080037using android::base::StringPrintf;
38
Neil Fuller0e844392016-09-08 13:43:31 +010039static jobjectArray Executable_getDeclaredAnnotationsNative(JNIEnv* env, jobject javaMethod) {
Jeff Hao1133db72016-04-04 19:50:14 -070040 ScopedFastNativeObjectAccess soa(env);
41 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
42 if (method->GetDeclaringClass()->IsProxyClass()) {
43 // Return an empty array instead of a null pointer.
Mathieu Chartier0795f232016-09-27 18:43:30 -070044 ObjPtr<mirror::Class> annotation_array_class =
45 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_annotation_Annotation__array);
46 ObjPtr<mirror::ObjectArray<mirror::Object>> empty_array =
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070047 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), annotation_array_class, 0);
Jeff Hao1133db72016-04-04 19:50:14 -070048 return soa.AddLocalReference<jobjectArray>(empty_array);
49 }
David Sehr9323e6e2016-09-13 08:58:35 -070050 return soa.AddLocalReference<jobjectArray>(annotations::GetAnnotationsForMethod(method));
Jeff Hao1133db72016-04-04 19:50:14 -070051}
52
Neil Fuller0e844392016-09-08 13:43:31 +010053static jobject Executable_getAnnotationNative(JNIEnv* env,
Neil Fuller79a21e72016-09-09 14:24:51 +010054 jobject javaMethod,
55 jclass annotationType) {
Neil Fuller16b21cd2016-08-12 09:37:02 +010056 ScopedFastNativeObjectAccess soa(env);
57 StackHandleScope<1> hs(soa.Self());
58 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
59 if (method->IsProxyMethod()) {
60 return nullptr;
61 } else {
Mathieu Chartier0795f232016-09-27 18:43:30 -070062 Handle<mirror::Class> klass(hs.NewHandle(soa.Decode<mirror::Class>(annotationType)));
David Sehr9323e6e2016-09-13 08:58:35 -070063 return soa.AddLocalReference<jobject>(annotations::GetAnnotationForMethod(method, klass));
Neil Fuller16b21cd2016-08-12 09:37:02 +010064 }
65}
66
Neil Fuller0e844392016-09-08 13:43:31 +010067static jobjectArray Executable_getSignatureAnnotation(JNIEnv* env, jobject javaMethod) {
Jeff Hao1133db72016-04-04 19:50:14 -070068 ScopedFastNativeObjectAccess soa(env);
69 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
70 if (method->GetDeclaringClass()->IsProxyClass()) {
71 return nullptr;
72 }
73 StackHandleScope<1> hs(soa.Self());
David Sehr9323e6e2016-09-13 08:58:35 -070074 return soa.AddLocalReference<jobjectArray>(annotations::GetSignatureAnnotationForMethod(method));
Jeff Hao1133db72016-04-04 19:50:14 -070075}
76
77
Neil Fuller0e844392016-09-08 13:43:31 +010078static jobjectArray Executable_getParameterAnnotationsNative(JNIEnv* env, jobject javaMethod) {
Neil Fuller16b21cd2016-08-12 09:37:02 +010079 ScopedFastNativeObjectAccess soa(env);
80 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
81 if (method->IsProxyMethod()) {
82 return nullptr;
83 } else {
David Sehr9323e6e2016-09-13 08:58:35 -070084 return soa.AddLocalReference<jobjectArray>(annotations::GetParameterAnnotations(method));
Neil Fuller16b21cd2016-08-12 09:37:02 +010085 }
86}
87
Neil Fuller79a21e72016-09-09 14:24:51 +010088static jobjectArray Executable_getParameters0(JNIEnv* env, jobject javaMethod) {
89 ScopedFastNativeObjectAccess soa(env);
90 Thread* self = soa.Self();
91 StackHandleScope<8> hs(self);
92
Mathieu Chartier0795f232016-09-27 18:43:30 -070093 Handle<mirror::Method> executable = hs.NewHandle(soa.Decode<mirror::Method>(javaMethod));
Neil Fuller79a21e72016-09-09 14:24:51 +010094 ArtMethod* art_method = executable.Get()->GetArtMethod();
95 if (art_method->GetDeclaringClass()->IsProxyClass()) {
96 return nullptr;
97 }
98
99 // Find the MethodParameters system annotation.
100 MutableHandle<mirror::ObjectArray<mirror::String>> names =
101 hs.NewHandle<mirror::ObjectArray<mirror::String>>(nullptr);
102 MutableHandle<mirror::IntArray> access_flags = hs.NewHandle<mirror::IntArray>(nullptr);
103 if (!annotations::GetParametersMetadataForMethod(art_method, &names, &access_flags)) {
104 return nullptr;
105 }
106
107 // Validate the MethodParameters system annotation data.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800108 if (UNLIKELY(names == nullptr || access_flags == nullptr)) {
Neil Fuller79a21e72016-09-09 14:24:51 +0100109 ThrowIllegalArgumentException(
110 StringPrintf("Missing parameter metadata for names or access flags for %s",
David Sehr709b0702016-10-13 09:12:37 -0700111 art_method->PrettyMethod().c_str()).c_str());
Neil Fuller79a21e72016-09-09 14:24:51 +0100112 return nullptr;
113 }
114
115 // Check array sizes match each other
116 int32_t names_count = names.Get()->GetLength();
117 int32_t access_flags_count = access_flags.Get()->GetLength();
118 if (names_count != access_flags_count) {
119 ThrowIllegalArgumentException(
120 StringPrintf(
121 "Inconsistent parameter metadata for %s. names length: %d, access flags length: %d",
David Sehr709b0702016-10-13 09:12:37 -0700122 art_method->PrettyMethod().c_str(),
Neil Fuller79a21e72016-09-09 14:24:51 +0100123 names_count,
124 access_flags_count).c_str());
125 return nullptr;
126 }
127
128 // Instantiate a Parameter[] to hold the result.
129 Handle<mirror::Class> parameter_array_class =
130 hs.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700131 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_reflect_Parameter__array));
Neil Fuller79a21e72016-09-09 14:24:51 +0100132 Handle<mirror::ObjectArray<mirror::Object>> parameter_array =
133 hs.NewHandle(
134 mirror::ObjectArray<mirror::Object>::Alloc(self,
135 parameter_array_class.Get(),
136 names_count));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800137 if (UNLIKELY(parameter_array == nullptr)) {
Neil Fuller79a21e72016-09-09 14:24:51 +0100138 self->AssertPendingException();
139 return nullptr;
140 }
141
142 Handle<mirror::Class> parameter_class =
Mathieu Chartier0795f232016-09-27 18:43:30 -0700143 hs.NewHandle(soa.Decode<mirror::Class>(WellKnownClasses::java_lang_reflect_Parameter));
Neil Fuller79a21e72016-09-09 14:24:51 +0100144 ArtMethod* parameter_init =
Andreas Gampe13b27842016-11-07 16:48:23 -0800145 jni::DecodeArtMethod(WellKnownClasses::java_lang_reflect_Parameter_init);
Neil Fuller79a21e72016-09-09 14:24:51 +0100146
147 // Mutable handles used in the loop below to ensure cleanup without scaling the number of
148 // handles by the number of parameters.
149 MutableHandle<mirror::String> name = hs.NewHandle<mirror::String>(nullptr);
150 MutableHandle<mirror::Object> parameter = hs.NewHandle<mirror::Object>(nullptr);
151
152 // Populate the Parameter[] to return.
153 for (int32_t parameter_index = 0; parameter_index < names_count; parameter_index++) {
154 name.Assign(names.Get()->Get(parameter_index));
155 int32_t modifiers = access_flags.Get()->Get(parameter_index);
156
157 // Allocate / initialize the Parameter to add to parameter_array.
158 parameter.Assign(parameter_class->AllocObject(self));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800159 if (UNLIKELY(parameter == nullptr)) {
Neil Fuller79a21e72016-09-09 14:24:51 +0100160 self->AssertPendingOOMException();
161 return nullptr;
162 }
163
164 uint32_t args[5] = { PointerToLowMemUInt32(parameter.Get()),
165 PointerToLowMemUInt32(name.Get()),
166 static_cast<uint32_t>(modifiers),
167 PointerToLowMemUInt32(executable.Get()),
168 static_cast<uint32_t>(parameter_index)
169 };
170 JValue result;
171 static const char* method_signature = "VLILI"; // return + parameter types
172 parameter_init->Invoke(self, args, sizeof(args), &result, method_signature);
173 if (UNLIKELY(self->IsExceptionPending())) {
174 return nullptr;
175 }
176
177 // Store the Parameter in the Parameter[].
178 parameter_array.Get()->Set(parameter_index, parameter.Get());
179 if (UNLIKELY(self->IsExceptionPending())) {
180 return nullptr;
181 }
182 }
183 return soa.AddLocalReference<jobjectArray>(parameter_array.Get());
184}
185
Neil Fuller0e844392016-09-08 13:43:31 +0100186static jboolean Executable_isAnnotationPresentNative(JNIEnv* env,
Neil Fuller79a21e72016-09-09 14:24:51 +0100187 jobject javaMethod,
188 jclass annotationType) {
Jeff Hao1133db72016-04-04 19:50:14 -0700189 ScopedFastNativeObjectAccess soa(env);
190 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
191 if (method->GetDeclaringClass()->IsProxyClass()) {
192 return false;
193 }
194 StackHandleScope<1> hs(soa.Self());
Mathieu Chartier0795f232016-09-27 18:43:30 -0700195 Handle<mirror::Class> klass(hs.NewHandle(soa.Decode<mirror::Class>(annotationType)));
David Sehr9323e6e2016-09-13 08:58:35 -0700196 return annotations::IsMethodAnnotationPresent(method, klass);
Jeff Hao1133db72016-04-04 19:50:14 -0700197}
198
Narayan Kamath6b2dc312017-03-14 13:26:12 +0000199static jint Executable_compareMethodParametersInternal(JNIEnv* env,
200 jobject thisMethod,
201 jobject otherMethod) {
202 ScopedFastNativeObjectAccess soa(env);
203 ArtMethod* this_method = ArtMethod::FromReflectedMethod(soa, thisMethod);
204 ArtMethod* other_method = ArtMethod::FromReflectedMethod(soa, otherMethod);
205
206 this_method = this_method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
207 other_method = other_method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
208
209 const DexFile::TypeList* this_list = this_method->GetParameterTypeList();
210 const DexFile::TypeList* other_list = other_method->GetParameterTypeList();
211
212 if (this_list == other_list) {
213 return 0;
214 }
215
216 if (this_list == nullptr && other_list != nullptr) {
217 return -1;
218 }
219
220 if (other_list == nullptr && this_list != nullptr) {
221 return 1;
222 }
223
224 const int32_t this_size = this_list->Size();
225 const int32_t other_size = other_list->Size();
226
227 if (this_size != other_size) {
228 return (this_size - other_size);
229 }
230
231 for (int32_t i = 0; i < this_size; ++i) {
232 const DexFile::TypeId& lhs = this_method->GetDexFile()->GetTypeId(
233 this_list->GetTypeItem(i).type_idx_);
234 const DexFile::TypeId& rhs = other_method->GetDexFile()->GetTypeId(
235 other_list->GetTypeItem(i).type_idx_);
236
237 uint32_t lhs_len, rhs_len;
238 const char* lhs_data = this_method->GetDexFile()->StringDataAndUtf16LengthByIdx(
239 lhs.descriptor_idx_, &lhs_len);
240 const char* rhs_data = other_method->GetDexFile()->StringDataAndUtf16LengthByIdx(
241 rhs.descriptor_idx_, &rhs_len);
242
243 int cmp = strcmp(lhs_data, rhs_data);
244 if (cmp != 0) {
245 return (cmp < 0) ? -1 : 1;
246 }
247 }
248
249 return 0;
250}
251
252static jobject Executable_getMethodNameInternal(JNIEnv* env, jobject javaMethod) {
253 ScopedFastNativeObjectAccess soa(env);
254 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
255 method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
256 return soa.AddLocalReference<jobject>(method->GetNameAsString(soa.Self()));
257}
258
259static jobject Executable_getMethodReturnTypeInternal(JNIEnv* env, jobject javaMethod) {
260 ScopedFastNativeObjectAccess soa(env);
261 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
262 method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Vladimir Markob45528c2017-07-27 14:14:28 +0100263 ObjPtr<mirror::Class> return_type(method->ResolveReturnType());
Narayan Kamath6b2dc312017-03-14 13:26:12 +0000264 if (return_type.IsNull()) {
265 CHECK(soa.Self()->IsExceptionPending());
266 return nullptr;
267 }
268
269 return soa.AddLocalReference<jobject>(return_type);
270}
271
272// TODO: Move this to mirror::Class ? Other mirror types that commonly appear
273// as arrays have a GetArrayClass() method. This is duplicated in
274// java_lang_Class.cc as well.
275static ObjPtr<mirror::Class> GetClassArrayClass(Thread* self)
276 REQUIRES_SHARED(Locks::mutator_lock_) {
277 ObjPtr<mirror::Class> class_class = mirror::Class::GetJavaLangClass();
278 return Runtime::Current()->GetClassLinker()->FindArrayClass(self, &class_class);
279}
280
281static jobjectArray Executable_getParameterTypesInternal(JNIEnv* env, jobject javaMethod) {
282 ScopedFastNativeObjectAccess soa(env);
283 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
284 method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
285
286 const DexFile::TypeList* params = method->GetParameterTypeList();
287 if (params == nullptr) {
288 return nullptr;
289 }
290
291 const uint32_t num_params = params->Size();
292
293 StackHandleScope<3> hs(soa.Self());
294 Handle<mirror::Class> class_array_class = hs.NewHandle(GetClassArrayClass(soa.Self()));
295 Handle<mirror::ObjectArray<mirror::Class>> ptypes = hs.NewHandle(
296 mirror::ObjectArray<mirror::Class>::Alloc(soa.Self(), class_array_class.Get(), num_params));
297 if (ptypes.IsNull()) {
298 DCHECK(soa.Self()->IsExceptionPending());
299 return nullptr;
300 }
301
302 MutableHandle<mirror::Class> param(hs.NewHandle<mirror::Class>(nullptr));
303 for (uint32_t i = 0; i < num_params; ++i) {
304 const dex::TypeIndex type_idx = params->GetTypeItem(i).type_idx_;
305 param.Assign(Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method));
306 if (param.Get() == nullptr) {
307 DCHECK(soa.Self()->IsExceptionPending());
308 return nullptr;
309 }
310 ptypes->SetWithoutChecks<false>(i, param.Get());
311 }
312
313 return soa.AddLocalReference<jobjectArray>(ptypes.Get());
314}
315
316static jint Executable_getParameterCountInternal(JNIEnv* env, jobject javaMethod) {
317 ScopedFastNativeObjectAccess soa(env);
318 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
319 method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
320
321 const DexFile::TypeList* params = method->GetParameterTypeList();
322 return (params == nullptr) ? 0 : params->Size();
323}
324
325
Jeff Hao1133db72016-04-04 19:50:14 -0700326static JNINativeMethod gMethods[] = {
Narayan Kamath6b2dc312017-03-14 13:26:12 +0000327 FAST_NATIVE_METHOD(Executable, compareMethodParametersInternal,
328 "(Ljava/lang/reflect/Method;)I"),
Igor Murashkin3b6f4402017-02-16 16:13:17 -0800329 FAST_NATIVE_METHOD(Executable, getAnnotationNative,
Narayan Kamath6b2dc312017-03-14 13:26:12 +0000330 "(Ljava/lang/Class;)Ljava/lang/annotation/Annotation;"),
331 FAST_NATIVE_METHOD(Executable, getDeclaredAnnotationsNative,
332 "()[Ljava/lang/annotation/Annotation;"),
Igor Murashkin3b6f4402017-02-16 16:13:17 -0800333 FAST_NATIVE_METHOD(Executable, getParameterAnnotationsNative,
Narayan Kamath6b2dc312017-03-14 13:26:12 +0000334 "()[[Ljava/lang/annotation/Annotation;"),
335 FAST_NATIVE_METHOD(Executable, getMethodNameInternal, "()Ljava/lang/String;"),
336 FAST_NATIVE_METHOD(Executable, getMethodReturnTypeInternal, "()Ljava/lang/Class;"),
337 FAST_NATIVE_METHOD(Executable, getParameterTypesInternal, "()[Ljava/lang/Class;"),
338 FAST_NATIVE_METHOD(Executable, getParameterCountInternal, "()I"),
Igor Murashkin3b6f4402017-02-16 16:13:17 -0800339 FAST_NATIVE_METHOD(Executable, getParameters0, "()[Ljava/lang/reflect/Parameter;"),
340 FAST_NATIVE_METHOD(Executable, getSignatureAnnotation, "()[Ljava/lang/String;"),
341 FAST_NATIVE_METHOD(Executable, isAnnotationPresentNative, "(Ljava/lang/Class;)Z"),
Jeff Hao1133db72016-04-04 19:50:14 -0700342};
343
Neil Fuller0e844392016-09-08 13:43:31 +0100344void register_java_lang_reflect_Executable(JNIEnv* env) {
345 REGISTER_NATIVE_METHODS("java/lang/reflect/Executable");
Jeff Hao1133db72016-04-04 19:50:14 -0700346}
347
348} // namespace art