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" |
| 20 | |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 21 | #include "art_method-inl.h" |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 22 | #include "dex_file_annotations.h" |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 23 | #include "handle.h" |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 24 | #include "jni_internal.h" |
| 25 | #include "mirror/class-inl.h" |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 26 | #include "mirror/method.h" |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 27 | #include "mirror/object-inl.h" |
| 28 | #include "mirror/object_array-inl.h" |
| 29 | #include "reflection.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 30 | #include "scoped_fast_native_object_access-inl.h" |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 31 | #include "well_known_classes.h" |
| 32 | |
| 33 | namespace art { |
| 34 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 35 | using android::base::StringPrintf; |
| 36 | |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame] | 37 | static jobjectArray Executable_getDeclaredAnnotationsNative(JNIEnv* env, jobject javaMethod) { |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 38 | 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 Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 42 | 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 Chartier | 1a5337f | 2016-10-13 13:48:23 -0700 | [diff] [blame] | 45 | mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), annotation_array_class, 0); |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 46 | return soa.AddLocalReference<jobjectArray>(empty_array); |
| 47 | } |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 48 | return soa.AddLocalReference<jobjectArray>(annotations::GetAnnotationsForMethod(method)); |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame] | 51 | static jobject Executable_getAnnotationNative(JNIEnv* env, |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 52 | jobject javaMethod, |
| 53 | jclass annotationType) { |
Neil Fuller | 16b21cd | 2016-08-12 09:37:02 +0100 | [diff] [blame] | 54 | 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 Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 60 | Handle<mirror::Class> klass(hs.NewHandle(soa.Decode<mirror::Class>(annotationType))); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 61 | return soa.AddLocalReference<jobject>(annotations::GetAnnotationForMethod(method, klass)); |
Neil Fuller | 16b21cd | 2016-08-12 09:37:02 +0100 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame] | 65 | static jobjectArray Executable_getSignatureAnnotation(JNIEnv* env, jobject javaMethod) { |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 66 | 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 Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 72 | return soa.AddLocalReference<jobjectArray>(annotations::GetSignatureAnnotationForMethod(method)); |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame] | 76 | static jobjectArray Executable_getParameterAnnotationsNative(JNIEnv* env, jobject javaMethod) { |
Neil Fuller | 16b21cd | 2016-08-12 09:37:02 +0100 | [diff] [blame] | 77 | ScopedFastNativeObjectAccess soa(env); |
| 78 | ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod); |
| 79 | if (method->IsProxyMethod()) { |
| 80 | return nullptr; |
| 81 | } else { |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 82 | return soa.AddLocalReference<jobjectArray>(annotations::GetParameterAnnotations(method)); |
Neil Fuller | 16b21cd | 2016-08-12 09:37:02 +0100 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 86 | static jobjectArray Executable_getParameters0(JNIEnv* env, jobject javaMethod) { |
| 87 | ScopedFastNativeObjectAccess soa(env); |
| 88 | Thread* self = soa.Self(); |
| 89 | StackHandleScope<8> hs(self); |
| 90 | |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 91 | Handle<mirror::Method> executable = hs.NewHandle(soa.Decode<mirror::Method>(javaMethod)); |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 92 | 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 Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 106 | if (UNLIKELY(names == nullptr || access_flags == nullptr)) { |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 107 | ThrowIllegalArgumentException( |
| 108 | StringPrintf("Missing parameter metadata for names or access flags for %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 109 | art_method->PrettyMethod().c_str()).c_str()); |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 110 | 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 Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 120 | art_method->PrettyMethod().c_str(), |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 121 | 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 Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 129 | soa.Decode<mirror::Class>(WellKnownClasses::java_lang_reflect_Parameter__array)); |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 130 | 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 Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 135 | if (UNLIKELY(parameter_array == nullptr)) { |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 136 | self->AssertPendingException(); |
| 137 | return nullptr; |
| 138 | } |
| 139 | |
| 140 | Handle<mirror::Class> parameter_class = |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 141 | hs.NewHandle(soa.Decode<mirror::Class>(WellKnownClasses::java_lang_reflect_Parameter)); |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 142 | ArtMethod* parameter_init = |
Andreas Gampe | 13b2784 | 2016-11-07 16:48:23 -0800 | [diff] [blame] | 143 | jni::DecodeArtMethod(WellKnownClasses::java_lang_reflect_Parameter_init); |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 144 | |
| 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 Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 157 | if (UNLIKELY(parameter == nullptr)) { |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 158 | 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 Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame] | 184 | static jboolean Executable_isAnnotationPresentNative(JNIEnv* env, |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 185 | jobject javaMethod, |
| 186 | jclass annotationType) { |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 187 | 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 Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 193 | Handle<mirror::Class> klass(hs.NewHandle(soa.Decode<mirror::Class>(annotationType))); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 194 | return annotations::IsMethodAnnotationPresent(method, klass); |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | static JNINativeMethod gMethods[] = { |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame] | 198 | NATIVE_METHOD(Executable, getAnnotationNative, |
Neil Fuller | 16b21cd | 2016-08-12 09:37:02 +0100 | [diff] [blame] | 199 | "!(Ljava/lang/Class;)Ljava/lang/annotation/Annotation;"), |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame] | 200 | NATIVE_METHOD(Executable, getDeclaredAnnotationsNative, "!()[Ljava/lang/annotation/Annotation;"), |
| 201 | NATIVE_METHOD(Executable, getParameterAnnotationsNative, |
Neil Fuller | 16b21cd | 2016-08-12 09:37:02 +0100 | [diff] [blame] | 202 | "!()[[Ljava/lang/annotation/Annotation;"), |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 203 | NATIVE_METHOD(Executable, getParameters0, "!()[Ljava/lang/reflect/Parameter;"), |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame] | 204 | NATIVE_METHOD(Executable, getSignatureAnnotation, "!()[Ljava/lang/String;"), |
| 205 | NATIVE_METHOD(Executable, isAnnotationPresentNative, "!(Ljava/lang/Class;)Z"), |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 206 | }; |
| 207 | |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame] | 208 | void register_java_lang_reflect_Executable(JNIEnv* env) { |
| 209 | REGISTER_NATIVE_METHODS("java/lang/reflect/Executable"); |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | } // namespace art |