blob: 8f226ce621dabedbd288cbd65ec0fb6f5d1d476d [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"
20
Jeff Hao1133db72016-04-04 19:50:14 -070021#include "art_method-inl.h"
David Sehr9323e6e2016-09-13 08:58:35 -070022#include "dex_file_annotations.h"
Neil Fuller79a21e72016-09-09 14:24:51 +010023#include "handle.h"
Jeff Hao1133db72016-04-04 19:50:14 -070024#include "jni_internal.h"
25#include "mirror/class-inl.h"
Neil Fuller79a21e72016-09-09 14:24:51 +010026#include "mirror/method.h"
Jeff Hao1133db72016-04-04 19:50:14 -070027#include "mirror/object-inl.h"
28#include "mirror/object_array-inl.h"
29#include "reflection.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070030#include "scoped_fast_native_object_access-inl.h"
Jeff Hao1133db72016-04-04 19:50:14 -070031#include "well_known_classes.h"
32
33namespace art {
34
Andreas Gampe46ee31b2016-12-14 10:11:49 -080035using android::base::StringPrintf;
36
Neil Fuller0e844392016-09-08 13:43:31 +010037static jobjectArray Executable_getDeclaredAnnotationsNative(JNIEnv* env, jobject javaMethod) {
Jeff Hao1133db72016-04-04 19:50:14 -070038 ScopedFastNativeObjectAccess soa(env);
39 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
40 if (method->GetDeclaringClass()->IsProxyClass()) {
41 // Return an empty array instead of a null pointer.
Mathieu Chartier0795f232016-09-27 18:43:30 -070042 ObjPtr<mirror::Class> annotation_array_class =
43 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_annotation_Annotation__array);
44 ObjPtr<mirror::ObjectArray<mirror::Object>> empty_array =
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070045 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), annotation_array_class, 0);
Jeff Hao1133db72016-04-04 19:50:14 -070046 return soa.AddLocalReference<jobjectArray>(empty_array);
47 }
David Sehr9323e6e2016-09-13 08:58:35 -070048 return soa.AddLocalReference<jobjectArray>(annotations::GetAnnotationsForMethod(method));
Jeff Hao1133db72016-04-04 19:50:14 -070049}
50
Neil Fuller0e844392016-09-08 13:43:31 +010051static jobject Executable_getAnnotationNative(JNIEnv* env,
Neil Fuller79a21e72016-09-09 14:24:51 +010052 jobject javaMethod,
53 jclass annotationType) {
Neil Fuller16b21cd2016-08-12 09:37:02 +010054 ScopedFastNativeObjectAccess soa(env);
55 StackHandleScope<1> hs(soa.Self());
56 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
57 if (method->IsProxyMethod()) {
58 return nullptr;
59 } else {
Mathieu Chartier0795f232016-09-27 18:43:30 -070060 Handle<mirror::Class> klass(hs.NewHandle(soa.Decode<mirror::Class>(annotationType)));
David Sehr9323e6e2016-09-13 08:58:35 -070061 return soa.AddLocalReference<jobject>(annotations::GetAnnotationForMethod(method, klass));
Neil Fuller16b21cd2016-08-12 09:37:02 +010062 }
63}
64
Neil Fuller0e844392016-09-08 13:43:31 +010065static jobjectArray Executable_getSignatureAnnotation(JNIEnv* env, jobject javaMethod) {
Jeff Hao1133db72016-04-04 19:50:14 -070066 ScopedFastNativeObjectAccess soa(env);
67 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
68 if (method->GetDeclaringClass()->IsProxyClass()) {
69 return nullptr;
70 }
71 StackHandleScope<1> hs(soa.Self());
David Sehr9323e6e2016-09-13 08:58:35 -070072 return soa.AddLocalReference<jobjectArray>(annotations::GetSignatureAnnotationForMethod(method));
Jeff Hao1133db72016-04-04 19:50:14 -070073}
74
75
Neil Fuller0e844392016-09-08 13:43:31 +010076static jobjectArray Executable_getParameterAnnotationsNative(JNIEnv* env, jobject javaMethod) {
Neil Fuller16b21cd2016-08-12 09:37:02 +010077 ScopedFastNativeObjectAccess soa(env);
78 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
79 if (method->IsProxyMethod()) {
80 return nullptr;
81 } else {
David Sehr9323e6e2016-09-13 08:58:35 -070082 return soa.AddLocalReference<jobjectArray>(annotations::GetParameterAnnotations(method));
Neil Fuller16b21cd2016-08-12 09:37:02 +010083 }
84}
85
Neil Fuller79a21e72016-09-09 14:24:51 +010086static jobjectArray Executable_getParameters0(JNIEnv* env, jobject javaMethod) {
87 ScopedFastNativeObjectAccess soa(env);
88 Thread* self = soa.Self();
89 StackHandleScope<8> hs(self);
90
Mathieu Chartier0795f232016-09-27 18:43:30 -070091 Handle<mirror::Method> executable = hs.NewHandle(soa.Decode<mirror::Method>(javaMethod));
Neil Fuller79a21e72016-09-09 14:24:51 +010092 ArtMethod* art_method = executable.Get()->GetArtMethod();
93 if (art_method->GetDeclaringClass()->IsProxyClass()) {
94 return nullptr;
95 }
96
97 // Find the MethodParameters system annotation.
98 MutableHandle<mirror::ObjectArray<mirror::String>> names =
99 hs.NewHandle<mirror::ObjectArray<mirror::String>>(nullptr);
100 MutableHandle<mirror::IntArray> access_flags = hs.NewHandle<mirror::IntArray>(nullptr);
101 if (!annotations::GetParametersMetadataForMethod(art_method, &names, &access_flags)) {
102 return nullptr;
103 }
104
105 // Validate the MethodParameters system annotation data.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800106 if (UNLIKELY(names == nullptr || access_flags == nullptr)) {
Neil Fuller79a21e72016-09-09 14:24:51 +0100107 ThrowIllegalArgumentException(
108 StringPrintf("Missing parameter metadata for names or access flags for %s",
David Sehr709b0702016-10-13 09:12:37 -0700109 art_method->PrettyMethod().c_str()).c_str());
Neil Fuller79a21e72016-09-09 14:24:51 +0100110 return nullptr;
111 }
112
113 // Check array sizes match each other
114 int32_t names_count = names.Get()->GetLength();
115 int32_t access_flags_count = access_flags.Get()->GetLength();
116 if (names_count != access_flags_count) {
117 ThrowIllegalArgumentException(
118 StringPrintf(
119 "Inconsistent parameter metadata for %s. names length: %d, access flags length: %d",
David Sehr709b0702016-10-13 09:12:37 -0700120 art_method->PrettyMethod().c_str(),
Neil Fuller79a21e72016-09-09 14:24:51 +0100121 names_count,
122 access_flags_count).c_str());
123 return nullptr;
124 }
125
126 // Instantiate a Parameter[] to hold the result.
127 Handle<mirror::Class> parameter_array_class =
128 hs.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700129 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_reflect_Parameter__array));
Neil Fuller79a21e72016-09-09 14:24:51 +0100130 Handle<mirror::ObjectArray<mirror::Object>> parameter_array =
131 hs.NewHandle(
132 mirror::ObjectArray<mirror::Object>::Alloc(self,
133 parameter_array_class.Get(),
134 names_count));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800135 if (UNLIKELY(parameter_array == nullptr)) {
Neil Fuller79a21e72016-09-09 14:24:51 +0100136 self->AssertPendingException();
137 return nullptr;
138 }
139
140 Handle<mirror::Class> parameter_class =
Mathieu Chartier0795f232016-09-27 18:43:30 -0700141 hs.NewHandle(soa.Decode<mirror::Class>(WellKnownClasses::java_lang_reflect_Parameter));
Neil Fuller79a21e72016-09-09 14:24:51 +0100142 ArtMethod* parameter_init =
Andreas Gampe13b27842016-11-07 16:48:23 -0800143 jni::DecodeArtMethod(WellKnownClasses::java_lang_reflect_Parameter_init);
Neil Fuller79a21e72016-09-09 14:24:51 +0100144
145 // Mutable handles used in the loop below to ensure cleanup without scaling the number of
146 // handles by the number of parameters.
147 MutableHandle<mirror::String> name = hs.NewHandle<mirror::String>(nullptr);
148 MutableHandle<mirror::Object> parameter = hs.NewHandle<mirror::Object>(nullptr);
149
150 // Populate the Parameter[] to return.
151 for (int32_t parameter_index = 0; parameter_index < names_count; parameter_index++) {
152 name.Assign(names.Get()->Get(parameter_index));
153 int32_t modifiers = access_flags.Get()->Get(parameter_index);
154
155 // Allocate / initialize the Parameter to add to parameter_array.
156 parameter.Assign(parameter_class->AllocObject(self));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800157 if (UNLIKELY(parameter == nullptr)) {
Neil Fuller79a21e72016-09-09 14:24:51 +0100158 self->AssertPendingOOMException();
159 return nullptr;
160 }
161
162 uint32_t args[5] = { PointerToLowMemUInt32(parameter.Get()),
163 PointerToLowMemUInt32(name.Get()),
164 static_cast<uint32_t>(modifiers),
165 PointerToLowMemUInt32(executable.Get()),
166 static_cast<uint32_t>(parameter_index)
167 };
168 JValue result;
169 static const char* method_signature = "VLILI"; // return + parameter types
170 parameter_init->Invoke(self, args, sizeof(args), &result, method_signature);
171 if (UNLIKELY(self->IsExceptionPending())) {
172 return nullptr;
173 }
174
175 // Store the Parameter in the Parameter[].
176 parameter_array.Get()->Set(parameter_index, parameter.Get());
177 if (UNLIKELY(self->IsExceptionPending())) {
178 return nullptr;
179 }
180 }
181 return soa.AddLocalReference<jobjectArray>(parameter_array.Get());
182}
183
Neil Fuller0e844392016-09-08 13:43:31 +0100184static jboolean Executable_isAnnotationPresentNative(JNIEnv* env,
Neil Fuller79a21e72016-09-09 14:24:51 +0100185 jobject javaMethod,
186 jclass annotationType) {
Jeff Hao1133db72016-04-04 19:50:14 -0700187 ScopedFastNativeObjectAccess soa(env);
188 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
189 if (method->GetDeclaringClass()->IsProxyClass()) {
190 return false;
191 }
192 StackHandleScope<1> hs(soa.Self());
Mathieu Chartier0795f232016-09-27 18:43:30 -0700193 Handle<mirror::Class> klass(hs.NewHandle(soa.Decode<mirror::Class>(annotationType)));
David Sehr9323e6e2016-09-13 08:58:35 -0700194 return annotations::IsMethodAnnotationPresent(method, klass);
Jeff Hao1133db72016-04-04 19:50:14 -0700195}
196
Narayan Kamath6b2dc312017-03-14 13:26:12 +0000197static jint Executable_compareMethodParametersInternal(JNIEnv* env,
198 jobject thisMethod,
199 jobject otherMethod) {
200 ScopedFastNativeObjectAccess soa(env);
201 ArtMethod* this_method = ArtMethod::FromReflectedMethod(soa, thisMethod);
202 ArtMethod* other_method = ArtMethod::FromReflectedMethod(soa, otherMethod);
203
204 this_method = this_method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
205 other_method = other_method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
206
207 const DexFile::TypeList* this_list = this_method->GetParameterTypeList();
208 const DexFile::TypeList* other_list = other_method->GetParameterTypeList();
209
210 if (this_list == other_list) {
211 return 0;
212 }
213
214 if (this_list == nullptr && other_list != nullptr) {
215 return -1;
216 }
217
218 if (other_list == nullptr && this_list != nullptr) {
219 return 1;
220 }
221
222 const int32_t this_size = this_list->Size();
223 const int32_t other_size = other_list->Size();
224
225 if (this_size != other_size) {
226 return (this_size - other_size);
227 }
228
229 for (int32_t i = 0; i < this_size; ++i) {
230 const DexFile::TypeId& lhs = this_method->GetDexFile()->GetTypeId(
231 this_list->GetTypeItem(i).type_idx_);
232 const DexFile::TypeId& rhs = other_method->GetDexFile()->GetTypeId(
233 other_list->GetTypeItem(i).type_idx_);
234
235 uint32_t lhs_len, rhs_len;
236 const char* lhs_data = this_method->GetDexFile()->StringDataAndUtf16LengthByIdx(
237 lhs.descriptor_idx_, &lhs_len);
238 const char* rhs_data = other_method->GetDexFile()->StringDataAndUtf16LengthByIdx(
239 rhs.descriptor_idx_, &rhs_len);
240
241 int cmp = strcmp(lhs_data, rhs_data);
242 if (cmp != 0) {
243 return (cmp < 0) ? -1 : 1;
244 }
245 }
246
247 return 0;
248}
249
250static jobject Executable_getMethodNameInternal(JNIEnv* env, jobject javaMethod) {
251 ScopedFastNativeObjectAccess soa(env);
252 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
253 method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
254 return soa.AddLocalReference<jobject>(method->GetNameAsString(soa.Self()));
255}
256
257static jobject Executable_getMethodReturnTypeInternal(JNIEnv* env, jobject javaMethod) {
258 ScopedFastNativeObjectAccess soa(env);
259 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
260 method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
261 ObjPtr<mirror::Class> return_type(method->GetReturnType(true /* resolve */));
262 if (return_type.IsNull()) {
263 CHECK(soa.Self()->IsExceptionPending());
264 return nullptr;
265 }
266
267 return soa.AddLocalReference<jobject>(return_type);
268}
269
270// TODO: Move this to mirror::Class ? Other mirror types that commonly appear
271// as arrays have a GetArrayClass() method. This is duplicated in
272// java_lang_Class.cc as well.
273static ObjPtr<mirror::Class> GetClassArrayClass(Thread* self)
274 REQUIRES_SHARED(Locks::mutator_lock_) {
275 ObjPtr<mirror::Class> class_class = mirror::Class::GetJavaLangClass();
276 return Runtime::Current()->GetClassLinker()->FindArrayClass(self, &class_class);
277}
278
279static jobjectArray Executable_getParameterTypesInternal(JNIEnv* env, jobject javaMethod) {
280 ScopedFastNativeObjectAccess soa(env);
281 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
282 method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
283
284 const DexFile::TypeList* params = method->GetParameterTypeList();
285 if (params == nullptr) {
286 return nullptr;
287 }
288
289 const uint32_t num_params = params->Size();
290
291 StackHandleScope<3> hs(soa.Self());
292 Handle<mirror::Class> class_array_class = hs.NewHandle(GetClassArrayClass(soa.Self()));
293 Handle<mirror::ObjectArray<mirror::Class>> ptypes = hs.NewHandle(
294 mirror::ObjectArray<mirror::Class>::Alloc(soa.Self(), class_array_class.Get(), num_params));
295 if (ptypes.IsNull()) {
296 DCHECK(soa.Self()->IsExceptionPending());
297 return nullptr;
298 }
299
300 MutableHandle<mirror::Class> param(hs.NewHandle<mirror::Class>(nullptr));
301 for (uint32_t i = 0; i < num_params; ++i) {
302 const dex::TypeIndex type_idx = params->GetTypeItem(i).type_idx_;
303 param.Assign(Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method));
304 if (param.Get() == nullptr) {
305 DCHECK(soa.Self()->IsExceptionPending());
306 return nullptr;
307 }
308 ptypes->SetWithoutChecks<false>(i, param.Get());
309 }
310
311 return soa.AddLocalReference<jobjectArray>(ptypes.Get());
312}
313
314static jint Executable_getParameterCountInternal(JNIEnv* env, jobject javaMethod) {
315 ScopedFastNativeObjectAccess soa(env);
316 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
317 method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
318
319 const DexFile::TypeList* params = method->GetParameterTypeList();
320 return (params == nullptr) ? 0 : params->Size();
321}
322
323
Jeff Hao1133db72016-04-04 19:50:14 -0700324static JNINativeMethod gMethods[] = {
Narayan Kamath6b2dc312017-03-14 13:26:12 +0000325 FAST_NATIVE_METHOD(Executable, compareMethodParametersInternal,
326 "(Ljava/lang/reflect/Method;)I"),
Igor Murashkin3b6f4402017-02-16 16:13:17 -0800327 FAST_NATIVE_METHOD(Executable, getAnnotationNative,
Narayan Kamath6b2dc312017-03-14 13:26:12 +0000328 "(Ljava/lang/Class;)Ljava/lang/annotation/Annotation;"),
329 FAST_NATIVE_METHOD(Executable, getDeclaredAnnotationsNative,
330 "()[Ljava/lang/annotation/Annotation;"),
Igor Murashkin3b6f4402017-02-16 16:13:17 -0800331 FAST_NATIVE_METHOD(Executable, getParameterAnnotationsNative,
Narayan Kamath6b2dc312017-03-14 13:26:12 +0000332 "()[[Ljava/lang/annotation/Annotation;"),
333 FAST_NATIVE_METHOD(Executable, getMethodNameInternal, "()Ljava/lang/String;"),
334 FAST_NATIVE_METHOD(Executable, getMethodReturnTypeInternal, "()Ljava/lang/Class;"),
335 FAST_NATIVE_METHOD(Executable, getParameterTypesInternal, "()[Ljava/lang/Class;"),
336 FAST_NATIVE_METHOD(Executable, getParameterCountInternal, "()I"),
Igor Murashkin3b6f4402017-02-16 16:13:17 -0800337 FAST_NATIVE_METHOD(Executable, getParameters0, "()[Ljava/lang/reflect/Parameter;"),
338 FAST_NATIVE_METHOD(Executable, getSignatureAnnotation, "()[Ljava/lang/String;"),
339 FAST_NATIVE_METHOD(Executable, isAnnotationPresentNative, "(Ljava/lang/Class;)Z"),
Jeff Hao1133db72016-04-04 19:50:14 -0700340};
341
Neil Fuller0e844392016-09-08 13:43:31 +0100342void register_java_lang_reflect_Executable(JNIEnv* env) {
343 REGISTER_NATIVE_METHODS("java/lang/reflect/Executable");
Jeff Hao1133db72016-04-04 19:50:14 -0700344}
345
346} // namespace art