blob: 256a3d04de5f810058e685a34e82a220312a0b5e [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"
30#include "reflection.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070031#include "scoped_fast_native_object_access-inl.h"
Jeff Hao1133db72016-04-04 19:50:14 -070032#include "well_known_classes.h"
33
34namespace art {
35
Andreas Gampe46ee31b2016-12-14 10:11:49 -080036using android::base::StringPrintf;
37
Neil Fuller0e844392016-09-08 13:43:31 +010038static jobjectArray Executable_getDeclaredAnnotationsNative(JNIEnv* env, jobject javaMethod) {
Jeff Hao1133db72016-04-04 19:50:14 -070039 ScopedFastNativeObjectAccess soa(env);
40 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
41 if (method->GetDeclaringClass()->IsProxyClass()) {
42 // Return an empty array instead of a null pointer.
Mathieu Chartier0795f232016-09-27 18:43:30 -070043 ObjPtr<mirror::Class> annotation_array_class =
44 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_annotation_Annotation__array);
45 ObjPtr<mirror::ObjectArray<mirror::Object>> empty_array =
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070046 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), annotation_array_class, 0);
Jeff Hao1133db72016-04-04 19:50:14 -070047 return soa.AddLocalReference<jobjectArray>(empty_array);
48 }
David Sehr9323e6e2016-09-13 08:58:35 -070049 return soa.AddLocalReference<jobjectArray>(annotations::GetAnnotationsForMethod(method));
Jeff Hao1133db72016-04-04 19:50:14 -070050}
51
Neil Fuller0e844392016-09-08 13:43:31 +010052static jobject Executable_getAnnotationNative(JNIEnv* env,
Neil Fuller79a21e72016-09-09 14:24:51 +010053 jobject javaMethod,
54 jclass annotationType) {
Neil Fuller16b21cd2016-08-12 09:37:02 +010055 ScopedFastNativeObjectAccess soa(env);
56 StackHandleScope<1> hs(soa.Self());
57 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
58 if (method->IsProxyMethod()) {
59 return nullptr;
60 } else {
Mathieu Chartier0795f232016-09-27 18:43:30 -070061 Handle<mirror::Class> klass(hs.NewHandle(soa.Decode<mirror::Class>(annotationType)));
David Sehr9323e6e2016-09-13 08:58:35 -070062 return soa.AddLocalReference<jobject>(annotations::GetAnnotationForMethod(method, klass));
Neil Fuller16b21cd2016-08-12 09:37:02 +010063 }
64}
65
Neil Fuller0e844392016-09-08 13:43:31 +010066static jobjectArray Executable_getSignatureAnnotation(JNIEnv* env, jobject javaMethod) {
Jeff Hao1133db72016-04-04 19:50:14 -070067 ScopedFastNativeObjectAccess soa(env);
68 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
69 if (method->GetDeclaringClass()->IsProxyClass()) {
70 return nullptr;
71 }
72 StackHandleScope<1> hs(soa.Self());
David Sehr9323e6e2016-09-13 08:58:35 -070073 return soa.AddLocalReference<jobjectArray>(annotations::GetSignatureAnnotationForMethod(method));
Jeff Hao1133db72016-04-04 19:50:14 -070074}
75
76
Neil Fuller0e844392016-09-08 13:43:31 +010077static jobjectArray Executable_getParameterAnnotationsNative(JNIEnv* env, jobject javaMethod) {
Neil Fuller16b21cd2016-08-12 09:37:02 +010078 ScopedFastNativeObjectAccess soa(env);
79 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
80 if (method->IsProxyMethod()) {
81 return nullptr;
82 } else {
David Sehr9323e6e2016-09-13 08:58:35 -070083 return soa.AddLocalReference<jobjectArray>(annotations::GetParameterAnnotations(method));
Neil Fuller16b21cd2016-08-12 09:37:02 +010084 }
85}
86
Neil Fuller79a21e72016-09-09 14:24:51 +010087static jobjectArray Executable_getParameters0(JNIEnv* env, jobject javaMethod) {
88 ScopedFastNativeObjectAccess soa(env);
89 Thread* self = soa.Self();
90 StackHandleScope<8> hs(self);
91
Mathieu Chartier0795f232016-09-27 18:43:30 -070092 Handle<mirror::Method> executable = hs.NewHandle(soa.Decode<mirror::Method>(javaMethod));
Neil Fuller79a21e72016-09-09 14:24:51 +010093 ArtMethod* art_method = executable.Get()->GetArtMethod();
94 if (art_method->GetDeclaringClass()->IsProxyClass()) {
95 return nullptr;
96 }
97
98 // Find the MethodParameters system annotation.
99 MutableHandle<mirror::ObjectArray<mirror::String>> names =
100 hs.NewHandle<mirror::ObjectArray<mirror::String>>(nullptr);
101 MutableHandle<mirror::IntArray> access_flags = hs.NewHandle<mirror::IntArray>(nullptr);
102 if (!annotations::GetParametersMetadataForMethod(art_method, &names, &access_flags)) {
103 return nullptr;
104 }
105
106 // Validate the MethodParameters system annotation data.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800107 if (UNLIKELY(names == nullptr || access_flags == nullptr)) {
Neil Fuller79a21e72016-09-09 14:24:51 +0100108 ThrowIllegalArgumentException(
109 StringPrintf("Missing parameter metadata for names or access flags for %s",
David Sehr709b0702016-10-13 09:12:37 -0700110 art_method->PrettyMethod().c_str()).c_str());
Neil Fuller79a21e72016-09-09 14:24:51 +0100111 return nullptr;
112 }
113
114 // Check array sizes match each other
115 int32_t names_count = names.Get()->GetLength();
116 int32_t access_flags_count = access_flags.Get()->GetLength();
117 if (names_count != access_flags_count) {
118 ThrowIllegalArgumentException(
119 StringPrintf(
120 "Inconsistent parameter metadata for %s. names length: %d, access flags length: %d",
David Sehr709b0702016-10-13 09:12:37 -0700121 art_method->PrettyMethod().c_str(),
Neil Fuller79a21e72016-09-09 14:24:51 +0100122 names_count,
123 access_flags_count).c_str());
124 return nullptr;
125 }
126
127 // Instantiate a Parameter[] to hold the result.
128 Handle<mirror::Class> parameter_array_class =
129 hs.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700130 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_reflect_Parameter__array));
Neil Fuller79a21e72016-09-09 14:24:51 +0100131 Handle<mirror::ObjectArray<mirror::Object>> parameter_array =
132 hs.NewHandle(
133 mirror::ObjectArray<mirror::Object>::Alloc(self,
134 parameter_array_class.Get(),
135 names_count));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800136 if (UNLIKELY(parameter_array == nullptr)) {
Neil Fuller79a21e72016-09-09 14:24:51 +0100137 self->AssertPendingException();
138 return nullptr;
139 }
140
141 Handle<mirror::Class> parameter_class =
Mathieu Chartier0795f232016-09-27 18:43:30 -0700142 hs.NewHandle(soa.Decode<mirror::Class>(WellKnownClasses::java_lang_reflect_Parameter));
Neil Fuller79a21e72016-09-09 14:24:51 +0100143 ArtMethod* parameter_init =
Andreas Gampe13b27842016-11-07 16:48:23 -0800144 jni::DecodeArtMethod(WellKnownClasses::java_lang_reflect_Parameter_init);
Neil Fuller79a21e72016-09-09 14:24:51 +0100145
146 // Mutable handles used in the loop below to ensure cleanup without scaling the number of
147 // handles by the number of parameters.
148 MutableHandle<mirror::String> name = hs.NewHandle<mirror::String>(nullptr);
149 MutableHandle<mirror::Object> parameter = hs.NewHandle<mirror::Object>(nullptr);
150
151 // Populate the Parameter[] to return.
152 for (int32_t parameter_index = 0; parameter_index < names_count; parameter_index++) {
153 name.Assign(names.Get()->Get(parameter_index));
154 int32_t modifiers = access_flags.Get()->Get(parameter_index);
155
156 // Allocate / initialize the Parameter to add to parameter_array.
157 parameter.Assign(parameter_class->AllocObject(self));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800158 if (UNLIKELY(parameter == nullptr)) {
Neil Fuller79a21e72016-09-09 14:24:51 +0100159 self->AssertPendingOOMException();
160 return nullptr;
161 }
162
163 uint32_t args[5] = { PointerToLowMemUInt32(parameter.Get()),
164 PointerToLowMemUInt32(name.Get()),
165 static_cast<uint32_t>(modifiers),
166 PointerToLowMemUInt32(executable.Get()),
167 static_cast<uint32_t>(parameter_index)
168 };
169 JValue result;
170 static const char* method_signature = "VLILI"; // return + parameter types
171 parameter_init->Invoke(self, args, sizeof(args), &result, method_signature);
172 if (UNLIKELY(self->IsExceptionPending())) {
173 return nullptr;
174 }
175
176 // Store the Parameter in the Parameter[].
177 parameter_array.Get()->Set(parameter_index, parameter.Get());
178 if (UNLIKELY(self->IsExceptionPending())) {
179 return nullptr;
180 }
181 }
182 return soa.AddLocalReference<jobjectArray>(parameter_array.Get());
183}
184
Neil Fuller0e844392016-09-08 13:43:31 +0100185static jboolean Executable_isAnnotationPresentNative(JNIEnv* env,
Neil Fuller79a21e72016-09-09 14:24:51 +0100186 jobject javaMethod,
187 jclass annotationType) {
Jeff Hao1133db72016-04-04 19:50:14 -0700188 ScopedFastNativeObjectAccess soa(env);
189 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
190 if (method->GetDeclaringClass()->IsProxyClass()) {
191 return false;
192 }
193 StackHandleScope<1> hs(soa.Self());
Mathieu Chartier0795f232016-09-27 18:43:30 -0700194 Handle<mirror::Class> klass(hs.NewHandle(soa.Decode<mirror::Class>(annotationType)));
David Sehr9323e6e2016-09-13 08:58:35 -0700195 return annotations::IsMethodAnnotationPresent(method, klass);
Jeff Hao1133db72016-04-04 19:50:14 -0700196}
197
Narayan Kamath6b2dc312017-03-14 13:26:12 +0000198static jint Executable_compareMethodParametersInternal(JNIEnv* env,
199 jobject thisMethod,
200 jobject otherMethod) {
201 ScopedFastNativeObjectAccess soa(env);
202 ArtMethod* this_method = ArtMethod::FromReflectedMethod(soa, thisMethod);
203 ArtMethod* other_method = ArtMethod::FromReflectedMethod(soa, otherMethod);
204
205 this_method = this_method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
206 other_method = other_method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
207
208 const DexFile::TypeList* this_list = this_method->GetParameterTypeList();
209 const DexFile::TypeList* other_list = other_method->GetParameterTypeList();
210
211 if (this_list == other_list) {
212 return 0;
213 }
214
215 if (this_list == nullptr && other_list != nullptr) {
216 return -1;
217 }
218
219 if (other_list == nullptr && this_list != nullptr) {
220 return 1;
221 }
222
223 const int32_t this_size = this_list->Size();
224 const int32_t other_size = other_list->Size();
225
226 if (this_size != other_size) {
227 return (this_size - other_size);
228 }
229
230 for (int32_t i = 0; i < this_size; ++i) {
231 const DexFile::TypeId& lhs = this_method->GetDexFile()->GetTypeId(
232 this_list->GetTypeItem(i).type_idx_);
233 const DexFile::TypeId& rhs = other_method->GetDexFile()->GetTypeId(
234 other_list->GetTypeItem(i).type_idx_);
235
236 uint32_t lhs_len, rhs_len;
237 const char* lhs_data = this_method->GetDexFile()->StringDataAndUtf16LengthByIdx(
238 lhs.descriptor_idx_, &lhs_len);
239 const char* rhs_data = other_method->GetDexFile()->StringDataAndUtf16LengthByIdx(
240 rhs.descriptor_idx_, &rhs_len);
241
242 int cmp = strcmp(lhs_data, rhs_data);
243 if (cmp != 0) {
244 return (cmp < 0) ? -1 : 1;
245 }
246 }
247
248 return 0;
249}
250
251static jobject Executable_getMethodNameInternal(JNIEnv* env, jobject javaMethod) {
252 ScopedFastNativeObjectAccess soa(env);
253 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
254 method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
255 return soa.AddLocalReference<jobject>(method->GetNameAsString(soa.Self()));
256}
257
258static jobject Executable_getMethodReturnTypeInternal(JNIEnv* env, jobject javaMethod) {
259 ScopedFastNativeObjectAccess soa(env);
260 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
261 method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
262 ObjPtr<mirror::Class> return_type(method->GetReturnType(true /* resolve */));
263 if (return_type.IsNull()) {
264 CHECK(soa.Self()->IsExceptionPending());
265 return nullptr;
266 }
267
268 return soa.AddLocalReference<jobject>(return_type);
269}
270
271// TODO: Move this to mirror::Class ? Other mirror types that commonly appear
272// as arrays have a GetArrayClass() method. This is duplicated in
273// java_lang_Class.cc as well.
274static ObjPtr<mirror::Class> GetClassArrayClass(Thread* self)
275 REQUIRES_SHARED(Locks::mutator_lock_) {
276 ObjPtr<mirror::Class> class_class = mirror::Class::GetJavaLangClass();
277 return Runtime::Current()->GetClassLinker()->FindArrayClass(self, &class_class);
278}
279
280static jobjectArray Executable_getParameterTypesInternal(JNIEnv* env, jobject javaMethod) {
281 ScopedFastNativeObjectAccess soa(env);
282 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
283 method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
284
285 const DexFile::TypeList* params = method->GetParameterTypeList();
286 if (params == nullptr) {
287 return nullptr;
288 }
289
290 const uint32_t num_params = params->Size();
291
292 StackHandleScope<3> hs(soa.Self());
293 Handle<mirror::Class> class_array_class = hs.NewHandle(GetClassArrayClass(soa.Self()));
294 Handle<mirror::ObjectArray<mirror::Class>> ptypes = hs.NewHandle(
295 mirror::ObjectArray<mirror::Class>::Alloc(soa.Self(), class_array_class.Get(), num_params));
296 if (ptypes.IsNull()) {
297 DCHECK(soa.Self()->IsExceptionPending());
298 return nullptr;
299 }
300
301 MutableHandle<mirror::Class> param(hs.NewHandle<mirror::Class>(nullptr));
302 for (uint32_t i = 0; i < num_params; ++i) {
303 const dex::TypeIndex type_idx = params->GetTypeItem(i).type_idx_;
304 param.Assign(Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method));
305 if (param.Get() == nullptr) {
306 DCHECK(soa.Self()->IsExceptionPending());
307 return nullptr;
308 }
309 ptypes->SetWithoutChecks<false>(i, param.Get());
310 }
311
312 return soa.AddLocalReference<jobjectArray>(ptypes.Get());
313}
314
315static jint Executable_getParameterCountInternal(JNIEnv* env, jobject javaMethod) {
316 ScopedFastNativeObjectAccess soa(env);
317 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
318 method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
319
320 const DexFile::TypeList* params = method->GetParameterTypeList();
321 return (params == nullptr) ? 0 : params->Size();
322}
323
324
Jeff Hao1133db72016-04-04 19:50:14 -0700325static JNINativeMethod gMethods[] = {
Narayan Kamath6b2dc312017-03-14 13:26:12 +0000326 FAST_NATIVE_METHOD(Executable, compareMethodParametersInternal,
327 "(Ljava/lang/reflect/Method;)I"),
Igor Murashkin3b6f4402017-02-16 16:13:17 -0800328 FAST_NATIVE_METHOD(Executable, getAnnotationNative,
Narayan Kamath6b2dc312017-03-14 13:26:12 +0000329 "(Ljava/lang/Class;)Ljava/lang/annotation/Annotation;"),
330 FAST_NATIVE_METHOD(Executable, getDeclaredAnnotationsNative,
331 "()[Ljava/lang/annotation/Annotation;"),
Igor Murashkin3b6f4402017-02-16 16:13:17 -0800332 FAST_NATIVE_METHOD(Executable, getParameterAnnotationsNative,
Narayan Kamath6b2dc312017-03-14 13:26:12 +0000333 "()[[Ljava/lang/annotation/Annotation;"),
334 FAST_NATIVE_METHOD(Executable, getMethodNameInternal, "()Ljava/lang/String;"),
335 FAST_NATIVE_METHOD(Executable, getMethodReturnTypeInternal, "()Ljava/lang/Class;"),
336 FAST_NATIVE_METHOD(Executable, getParameterTypesInternal, "()[Ljava/lang/Class;"),
337 FAST_NATIVE_METHOD(Executable, getParameterCountInternal, "()I"),
Igor Murashkin3b6f4402017-02-16 16:13:17 -0800338 FAST_NATIVE_METHOD(Executable, getParameters0, "()[Ljava/lang/reflect/Parameter;"),
339 FAST_NATIVE_METHOD(Executable, getSignatureAnnotation, "()[Ljava/lang/String;"),
340 FAST_NATIVE_METHOD(Executable, isAnnotationPresentNative, "(Ljava/lang/Class;)Z"),
Jeff Hao1133db72016-04-04 19:50:14 -0700341};
342
Neil Fuller0e844392016-09-08 13:43:31 +0100343void register_java_lang_reflect_Executable(JNIEnv* env) {
344 REGISTER_NATIVE_METHODS("java/lang/reflect/Executable");
Jeff Hao1133db72016-04-04 19:50:14 -0700345}
346
347} // namespace art