Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 1 | /* |
| 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 Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame] | 17 | #include "java_lang_reflect_Executable.h" |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 18 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 19 | #include "android-base/stringprintf.h" |
Andreas Gampe | a14100c | 2017-04-24 15:09:56 -0700 | [diff] [blame] | 20 | #include "nativehelper/jni_macros.h" |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 21 | |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 22 | #include "art_method-inl.h" |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 23 | #include "class_root.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 24 | #include "dex/dex_file_annotations.h" |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 25 | #include "handle.h" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 26 | #include "jni/jni_internal.h" |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 27 | #include "mirror/class-inl.h" |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 28 | #include "mirror/method.h" |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 29 | #include "mirror/object-inl.h" |
| 30 | #include "mirror/object_array-inl.h" |
Andreas Gampe | 87583b3 | 2017-05-25 11:22:18 -0700 | [diff] [blame] | 31 | #include "native_util.h" |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 32 | #include "reflection.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 33 | #include "scoped_fast_native_object_access-inl.h" |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 34 | #include "well_known_classes.h" |
| 35 | |
| 36 | namespace art { |
| 37 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 38 | using android::base::StringPrintf; |
| 39 | |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame] | 40 | static jobjectArray Executable_getDeclaredAnnotationsNative(JNIEnv* env, jobject javaMethod) { |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 41 | ScopedFastNativeObjectAccess soa(env); |
| 42 | ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod); |
| 43 | if (method->GetDeclaringClass()->IsProxyClass()) { |
| 44 | // Return an empty array instead of a null pointer. |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 45 | ObjPtr<mirror::Class> annotation_array_class = |
| 46 | soa.Decode<mirror::Class>(WellKnownClasses::java_lang_annotation_Annotation__array); |
| 47 | ObjPtr<mirror::ObjectArray<mirror::Object>> empty_array = |
Mathieu Chartier | 1a5337f | 2016-10-13 13:48:23 -0700 | [diff] [blame] | 48 | mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), annotation_array_class, 0); |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 49 | return soa.AddLocalReference<jobjectArray>(empty_array); |
| 50 | } |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 51 | return soa.AddLocalReference<jobjectArray>(annotations::GetAnnotationsForMethod(method)); |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame] | 54 | static jobject Executable_getAnnotationNative(JNIEnv* env, |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 55 | jobject javaMethod, |
| 56 | jclass annotationType) { |
Neil Fuller | 16b21cd | 2016-08-12 09:37:02 +0100 | [diff] [blame] | 57 | ScopedFastNativeObjectAccess soa(env); |
| 58 | StackHandleScope<1> hs(soa.Self()); |
| 59 | ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod); |
| 60 | if (method->IsProxyMethod()) { |
| 61 | return nullptr; |
| 62 | } else { |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 63 | Handle<mirror::Class> klass(hs.NewHandle(soa.Decode<mirror::Class>(annotationType))); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 64 | return soa.AddLocalReference<jobject>(annotations::GetAnnotationForMethod(method, klass)); |
Neil Fuller | 16b21cd | 2016-08-12 09:37:02 +0100 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame] | 68 | static jobjectArray Executable_getSignatureAnnotation(JNIEnv* env, jobject javaMethod) { |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 69 | ScopedFastNativeObjectAccess soa(env); |
| 70 | ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod); |
| 71 | if (method->GetDeclaringClass()->IsProxyClass()) { |
| 72 | return nullptr; |
| 73 | } |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 74 | return soa.AddLocalReference<jobjectArray>(annotations::GetSignatureAnnotationForMethod(method)); |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame] | 78 | static jobjectArray Executable_getParameterAnnotationsNative(JNIEnv* env, jobject javaMethod) { |
Neil Fuller | 16b21cd | 2016-08-12 09:37:02 +0100 | [diff] [blame] | 79 | ScopedFastNativeObjectAccess soa(env); |
| 80 | ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod); |
| 81 | if (method->IsProxyMethod()) { |
| 82 | return nullptr; |
Neil Fuller | 16b21cd | 2016-08-12 09:37:02 +0100 | [diff] [blame] | 83 | } |
Orion Hodson | 58143d2 | 2018-02-20 08:44:20 +0000 | [diff] [blame] | 84 | |
| 85 | StackHandleScope<4> hs(soa.Self()); |
| 86 | Handle<mirror::ObjectArray<mirror::Object>> annotations = |
| 87 | hs.NewHandle(annotations::GetParameterAnnotations(method)); |
| 88 | if (annotations.IsNull()) { |
| 89 | return nullptr; |
| 90 | } |
| 91 | |
| 92 | // If the method is not a constructor, or has parameter annotations |
| 93 | // for each parameter, then we can return those annotations |
| 94 | // unmodified. Otherwise, we need to look at whether the |
| 95 | // constructor has implicit parameters as these may need padding |
| 96 | // with empty parameter annotations. |
| 97 | if (!method->IsConstructor() || |
| 98 | annotations->GetLength() == static_cast<int>(method->GetNumberOfParameters())) { |
| 99 | return soa.AddLocalReference<jobjectArray>(annotations.Get()); |
| 100 | } |
| 101 | |
| 102 | // If declaring class is a local or an enum, do not pad parameter |
| 103 | // annotations, as the implicit constructor parameters are an implementation |
| 104 | // detail rather than required by JLS. |
| 105 | Handle<mirror::Class> declaring_class = hs.NewHandle(method->GetDeclaringClass()); |
| 106 | if (annotations::GetEnclosingMethod(declaring_class) != nullptr || |
| 107 | declaring_class->IsEnum()) { |
| 108 | return soa.AddLocalReference<jobjectArray>(annotations.Get()); |
| 109 | } |
| 110 | |
| 111 | // Prepare to resize the annotations so there is 1:1 correspondence |
| 112 | // with the constructor parameters. |
| 113 | Handle<mirror::ObjectArray<mirror::Object>> resized_annotations = hs.NewHandle( |
| 114 | mirror::ObjectArray<mirror::Object>::Alloc( |
| 115 | soa.Self(), |
| 116 | annotations->GetClass(), |
| 117 | static_cast<int>(method->GetNumberOfParameters()))); |
| 118 | if (resized_annotations.IsNull()) { |
| 119 | DCHECK(soa.Self()->IsExceptionPending()); |
| 120 | return nullptr; |
| 121 | } |
| 122 | |
| 123 | static constexpr bool kTransactionActive = false; |
| 124 | const int32_t offset = resized_annotations->GetLength() - annotations->GetLength(); |
| 125 | if (offset > 0) { |
| 126 | // Workaround for dexers (d8/dx) that do not insert annotations |
| 127 | // for implicit parameters (b/68033708). |
| 128 | ObjPtr<mirror::Class> annotation_array_class = |
| 129 | soa.Decode<mirror::Class>(WellKnownClasses::java_lang_annotation_Annotation__array); |
| 130 | Handle<mirror::ObjectArray<mirror::Object>> empty_annotations = hs.NewHandle( |
| 131 | mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), annotation_array_class, 0)); |
| 132 | if (empty_annotations.IsNull()) { |
| 133 | DCHECK(soa.Self()->IsExceptionPending()); |
| 134 | return nullptr; |
| 135 | } |
| 136 | for (int i = 0; i < offset; ++i) { |
| 137 | resized_annotations->SetWithoutChecks<kTransactionActive>(i, empty_annotations.Get()); |
| 138 | } |
| 139 | for (int i = 0; i < annotations->GetLength(); ++i) { |
| 140 | ObjPtr<mirror::Object> annotation = annotations->GetWithoutChecks(i); |
| 141 | resized_annotations->SetWithoutChecks<kTransactionActive>(i + offset, annotation); |
| 142 | } |
| 143 | } else { |
| 144 | // Workaround for Jack (defunct) erroneously inserting annotations |
| 145 | // for local classes (b/68033708). |
| 146 | DCHECK_LT(offset, 0); |
| 147 | for (int i = 0; i < resized_annotations->GetLength(); ++i) { |
| 148 | ObjPtr<mirror::Object> annotation = annotations->GetWithoutChecks(i - offset); |
| 149 | resized_annotations->SetWithoutChecks<kTransactionActive>(i, annotation); |
| 150 | } |
| 151 | } |
| 152 | return soa.AddLocalReference<jobjectArray>(resized_annotations.Get()); |
Neil Fuller | 16b21cd | 2016-08-12 09:37:02 +0100 | [diff] [blame] | 153 | } |
| 154 | |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 155 | static jobjectArray Executable_getParameters0(JNIEnv* env, jobject javaMethod) { |
| 156 | ScopedFastNativeObjectAccess soa(env); |
| 157 | Thread* self = soa.Self(); |
| 158 | StackHandleScope<8> hs(self); |
| 159 | |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 160 | Handle<mirror::Method> executable = hs.NewHandle(soa.Decode<mirror::Method>(javaMethod)); |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 161 | ArtMethod* art_method = executable.Get()->GetArtMethod(); |
| 162 | if (art_method->GetDeclaringClass()->IsProxyClass()) { |
| 163 | return nullptr; |
| 164 | } |
| 165 | |
| 166 | // Find the MethodParameters system annotation. |
| 167 | MutableHandle<mirror::ObjectArray<mirror::String>> names = |
| 168 | hs.NewHandle<mirror::ObjectArray<mirror::String>>(nullptr); |
| 169 | MutableHandle<mirror::IntArray> access_flags = hs.NewHandle<mirror::IntArray>(nullptr); |
| 170 | if (!annotations::GetParametersMetadataForMethod(art_method, &names, &access_flags)) { |
| 171 | return nullptr; |
| 172 | } |
| 173 | |
| 174 | // Validate the MethodParameters system annotation data. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 175 | if (UNLIKELY(names == nullptr || access_flags == nullptr)) { |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 176 | ThrowIllegalArgumentException( |
| 177 | StringPrintf("Missing parameter metadata for names or access flags for %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 178 | art_method->PrettyMethod().c_str()).c_str()); |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 179 | return nullptr; |
| 180 | } |
| 181 | |
| 182 | // Check array sizes match each other |
| 183 | int32_t names_count = names.Get()->GetLength(); |
| 184 | int32_t access_flags_count = access_flags.Get()->GetLength(); |
| 185 | if (names_count != access_flags_count) { |
| 186 | ThrowIllegalArgumentException( |
| 187 | StringPrintf( |
| 188 | "Inconsistent parameter metadata for %s. names length: %d, access flags length: %d", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 189 | art_method->PrettyMethod().c_str(), |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 190 | names_count, |
| 191 | access_flags_count).c_str()); |
| 192 | return nullptr; |
| 193 | } |
| 194 | |
| 195 | // Instantiate a Parameter[] to hold the result. |
| 196 | Handle<mirror::Class> parameter_array_class = |
| 197 | hs.NewHandle( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 198 | soa.Decode<mirror::Class>(WellKnownClasses::java_lang_reflect_Parameter__array)); |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 199 | Handle<mirror::ObjectArray<mirror::Object>> parameter_array = |
| 200 | hs.NewHandle( |
| 201 | mirror::ObjectArray<mirror::Object>::Alloc(self, |
| 202 | parameter_array_class.Get(), |
| 203 | names_count)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 204 | if (UNLIKELY(parameter_array == nullptr)) { |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 205 | self->AssertPendingException(); |
| 206 | return nullptr; |
| 207 | } |
| 208 | |
| 209 | Handle<mirror::Class> parameter_class = |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 210 | hs.NewHandle(soa.Decode<mirror::Class>(WellKnownClasses::java_lang_reflect_Parameter)); |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 211 | ArtMethod* parameter_init = |
Andreas Gampe | 13b2784 | 2016-11-07 16:48:23 -0800 | [diff] [blame] | 212 | jni::DecodeArtMethod(WellKnownClasses::java_lang_reflect_Parameter_init); |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 213 | |
| 214 | // Mutable handles used in the loop below to ensure cleanup without scaling the number of |
| 215 | // handles by the number of parameters. |
| 216 | MutableHandle<mirror::String> name = hs.NewHandle<mirror::String>(nullptr); |
| 217 | MutableHandle<mirror::Object> parameter = hs.NewHandle<mirror::Object>(nullptr); |
| 218 | |
| 219 | // Populate the Parameter[] to return. |
| 220 | for (int32_t parameter_index = 0; parameter_index < names_count; parameter_index++) { |
| 221 | name.Assign(names.Get()->Get(parameter_index)); |
| 222 | int32_t modifiers = access_flags.Get()->Get(parameter_index); |
| 223 | |
| 224 | // Allocate / initialize the Parameter to add to parameter_array. |
| 225 | parameter.Assign(parameter_class->AllocObject(self)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 226 | if (UNLIKELY(parameter == nullptr)) { |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 227 | self->AssertPendingOOMException(); |
| 228 | return nullptr; |
| 229 | } |
| 230 | |
| 231 | uint32_t args[5] = { PointerToLowMemUInt32(parameter.Get()), |
| 232 | PointerToLowMemUInt32(name.Get()), |
| 233 | static_cast<uint32_t>(modifiers), |
| 234 | PointerToLowMemUInt32(executable.Get()), |
| 235 | static_cast<uint32_t>(parameter_index) |
| 236 | }; |
| 237 | JValue result; |
| 238 | static const char* method_signature = "VLILI"; // return + parameter types |
| 239 | parameter_init->Invoke(self, args, sizeof(args), &result, method_signature); |
| 240 | if (UNLIKELY(self->IsExceptionPending())) { |
| 241 | return nullptr; |
| 242 | } |
| 243 | |
| 244 | // Store the Parameter in the Parameter[]. |
| 245 | parameter_array.Get()->Set(parameter_index, parameter.Get()); |
| 246 | if (UNLIKELY(self->IsExceptionPending())) { |
| 247 | return nullptr; |
| 248 | } |
| 249 | } |
| 250 | return soa.AddLocalReference<jobjectArray>(parameter_array.Get()); |
| 251 | } |
| 252 | |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame] | 253 | static jboolean Executable_isAnnotationPresentNative(JNIEnv* env, |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 254 | jobject javaMethod, |
| 255 | jclass annotationType) { |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 256 | ScopedFastNativeObjectAccess soa(env); |
| 257 | ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod); |
| 258 | if (method->GetDeclaringClass()->IsProxyClass()) { |
| 259 | return false; |
| 260 | } |
| 261 | StackHandleScope<1> hs(soa.Self()); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 262 | Handle<mirror::Class> klass(hs.NewHandle(soa.Decode<mirror::Class>(annotationType))); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 263 | return annotations::IsMethodAnnotationPresent(method, klass); |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Narayan Kamath | 6b2dc31 | 2017-03-14 13:26:12 +0000 | [diff] [blame] | 266 | static jint Executable_compareMethodParametersInternal(JNIEnv* env, |
| 267 | jobject thisMethod, |
| 268 | jobject otherMethod) { |
| 269 | ScopedFastNativeObjectAccess soa(env); |
| 270 | ArtMethod* this_method = ArtMethod::FromReflectedMethod(soa, thisMethod); |
| 271 | ArtMethod* other_method = ArtMethod::FromReflectedMethod(soa, otherMethod); |
| 272 | |
| 273 | this_method = this_method->GetInterfaceMethodIfProxy(kRuntimePointerSize); |
| 274 | other_method = other_method->GetInterfaceMethodIfProxy(kRuntimePointerSize); |
| 275 | |
| 276 | const DexFile::TypeList* this_list = this_method->GetParameterTypeList(); |
| 277 | const DexFile::TypeList* other_list = other_method->GetParameterTypeList(); |
| 278 | |
| 279 | if (this_list == other_list) { |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | if (this_list == nullptr && other_list != nullptr) { |
| 284 | return -1; |
| 285 | } |
| 286 | |
| 287 | if (other_list == nullptr && this_list != nullptr) { |
| 288 | return 1; |
| 289 | } |
| 290 | |
| 291 | const int32_t this_size = this_list->Size(); |
| 292 | const int32_t other_size = other_list->Size(); |
| 293 | |
| 294 | if (this_size != other_size) { |
| 295 | return (this_size - other_size); |
| 296 | } |
| 297 | |
| 298 | for (int32_t i = 0; i < this_size; ++i) { |
| 299 | const DexFile::TypeId& lhs = this_method->GetDexFile()->GetTypeId( |
| 300 | this_list->GetTypeItem(i).type_idx_); |
| 301 | const DexFile::TypeId& rhs = other_method->GetDexFile()->GetTypeId( |
| 302 | other_list->GetTypeItem(i).type_idx_); |
| 303 | |
| 304 | uint32_t lhs_len, rhs_len; |
| 305 | const char* lhs_data = this_method->GetDexFile()->StringDataAndUtf16LengthByIdx( |
| 306 | lhs.descriptor_idx_, &lhs_len); |
| 307 | const char* rhs_data = other_method->GetDexFile()->StringDataAndUtf16LengthByIdx( |
| 308 | rhs.descriptor_idx_, &rhs_len); |
| 309 | |
| 310 | int cmp = strcmp(lhs_data, rhs_data); |
| 311 | if (cmp != 0) { |
| 312 | return (cmp < 0) ? -1 : 1; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | return 0; |
| 317 | } |
| 318 | |
Igor Murashkin | 06537f7 | 2018-02-22 15:03:05 -0800 | [diff] [blame] | 319 | static jstring Executable_getMethodNameInternal(JNIEnv* env, jobject javaMethod) { |
Narayan Kamath | 6b2dc31 | 2017-03-14 13:26:12 +0000 | [diff] [blame] | 320 | ScopedFastNativeObjectAccess soa(env); |
| 321 | ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod); |
| 322 | method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize); |
Vladimir Marko | 18090d1 | 2018-06-01 16:53:12 +0100 | [diff] [blame^] | 323 | return soa.AddLocalReference<jstring>(method->ResolveNameString()); |
Narayan Kamath | 6b2dc31 | 2017-03-14 13:26:12 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Igor Murashkin | 06537f7 | 2018-02-22 15:03:05 -0800 | [diff] [blame] | 326 | static jclass Executable_getMethodReturnTypeInternal(JNIEnv* env, jobject javaMethod) { |
Narayan Kamath | 6b2dc31 | 2017-03-14 13:26:12 +0000 | [diff] [blame] | 327 | ScopedFastNativeObjectAccess soa(env); |
| 328 | ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod); |
| 329 | method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize); |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 330 | ObjPtr<mirror::Class> return_type(method->ResolveReturnType()); |
Narayan Kamath | 6b2dc31 | 2017-03-14 13:26:12 +0000 | [diff] [blame] | 331 | if (return_type.IsNull()) { |
| 332 | CHECK(soa.Self()->IsExceptionPending()); |
| 333 | return nullptr; |
| 334 | } |
| 335 | |
Igor Murashkin | 06537f7 | 2018-02-22 15:03:05 -0800 | [diff] [blame] | 336 | return soa.AddLocalReference<jclass>(return_type); |
Narayan Kamath | 6b2dc31 | 2017-03-14 13:26:12 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Narayan Kamath | 6b2dc31 | 2017-03-14 13:26:12 +0000 | [diff] [blame] | 339 | static jobjectArray Executable_getParameterTypesInternal(JNIEnv* env, jobject javaMethod) { |
| 340 | ScopedFastNativeObjectAccess soa(env); |
| 341 | ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod); |
| 342 | method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize); |
| 343 | |
| 344 | const DexFile::TypeList* params = method->GetParameterTypeList(); |
| 345 | if (params == nullptr) { |
| 346 | return nullptr; |
| 347 | } |
| 348 | |
| 349 | const uint32_t num_params = params->Size(); |
| 350 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 351 | StackHandleScope<2> hs(soa.Self()); |
| 352 | ObjPtr<mirror::Class> class_array_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>(); |
Narayan Kamath | 6b2dc31 | 2017-03-14 13:26:12 +0000 | [diff] [blame] | 353 | Handle<mirror::ObjectArray<mirror::Class>> ptypes = hs.NewHandle( |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 354 | mirror::ObjectArray<mirror::Class>::Alloc(soa.Self(), class_array_class, num_params)); |
Narayan Kamath | 6b2dc31 | 2017-03-14 13:26:12 +0000 | [diff] [blame] | 355 | if (ptypes.IsNull()) { |
| 356 | DCHECK(soa.Self()->IsExceptionPending()); |
| 357 | return nullptr; |
| 358 | } |
| 359 | |
| 360 | MutableHandle<mirror::Class> param(hs.NewHandle<mirror::Class>(nullptr)); |
| 361 | for (uint32_t i = 0; i < num_params; ++i) { |
| 362 | const dex::TypeIndex type_idx = params->GetTypeItem(i).type_idx_; |
| 363 | param.Assign(Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method)); |
| 364 | if (param.Get() == nullptr) { |
| 365 | DCHECK(soa.Self()->IsExceptionPending()); |
| 366 | return nullptr; |
| 367 | } |
| 368 | ptypes->SetWithoutChecks<false>(i, param.Get()); |
| 369 | } |
| 370 | |
| 371 | return soa.AddLocalReference<jobjectArray>(ptypes.Get()); |
| 372 | } |
| 373 | |
| 374 | static jint Executable_getParameterCountInternal(JNIEnv* env, jobject javaMethod) { |
| 375 | ScopedFastNativeObjectAccess soa(env); |
| 376 | ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod); |
| 377 | method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize); |
| 378 | |
| 379 | const DexFile::TypeList* params = method->GetParameterTypeList(); |
| 380 | return (params == nullptr) ? 0 : params->Size(); |
| 381 | } |
| 382 | |
| 383 | |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 384 | static JNINativeMethod gMethods[] = { |
Narayan Kamath | 6b2dc31 | 2017-03-14 13:26:12 +0000 | [diff] [blame] | 385 | FAST_NATIVE_METHOD(Executable, compareMethodParametersInternal, |
| 386 | "(Ljava/lang/reflect/Method;)I"), |
Igor Murashkin | 3b6f440 | 2017-02-16 16:13:17 -0800 | [diff] [blame] | 387 | FAST_NATIVE_METHOD(Executable, getAnnotationNative, |
Narayan Kamath | 6b2dc31 | 2017-03-14 13:26:12 +0000 | [diff] [blame] | 388 | "(Ljava/lang/Class;)Ljava/lang/annotation/Annotation;"), |
| 389 | FAST_NATIVE_METHOD(Executable, getDeclaredAnnotationsNative, |
| 390 | "()[Ljava/lang/annotation/Annotation;"), |
Igor Murashkin | 3b6f440 | 2017-02-16 16:13:17 -0800 | [diff] [blame] | 391 | FAST_NATIVE_METHOD(Executable, getParameterAnnotationsNative, |
Narayan Kamath | 6b2dc31 | 2017-03-14 13:26:12 +0000 | [diff] [blame] | 392 | "()[[Ljava/lang/annotation/Annotation;"), |
| 393 | FAST_NATIVE_METHOD(Executable, getMethodNameInternal, "()Ljava/lang/String;"), |
| 394 | FAST_NATIVE_METHOD(Executable, getMethodReturnTypeInternal, "()Ljava/lang/Class;"), |
| 395 | FAST_NATIVE_METHOD(Executable, getParameterTypesInternal, "()[Ljava/lang/Class;"), |
| 396 | FAST_NATIVE_METHOD(Executable, getParameterCountInternal, "()I"), |
Igor Murashkin | 3b6f440 | 2017-02-16 16:13:17 -0800 | [diff] [blame] | 397 | FAST_NATIVE_METHOD(Executable, getParameters0, "()[Ljava/lang/reflect/Parameter;"), |
| 398 | FAST_NATIVE_METHOD(Executable, getSignatureAnnotation, "()[Ljava/lang/String;"), |
| 399 | FAST_NATIVE_METHOD(Executable, isAnnotationPresentNative, "(Ljava/lang/Class;)Z"), |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 400 | }; |
| 401 | |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame] | 402 | void register_java_lang_reflect_Executable(JNIEnv* env) { |
| 403 | REGISTER_NATIVE_METHODS("java/lang/reflect/Executable"); |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | } // namespace art |