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 | |
| 19 | #include "art_method-inl.h" |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 20 | #include "dex_file_annotations.h" |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 21 | #include "handle.h" |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 22 | #include "jni_internal.h" |
| 23 | #include "mirror/class-inl.h" |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 24 | #include "mirror/method.h" |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 25 | #include "mirror/object-inl.h" |
| 26 | #include "mirror/object_array-inl.h" |
| 27 | #include "reflection.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 28 | #include "scoped_fast_native_object_access-inl.h" |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 29 | #include "well_known_classes.h" |
| 30 | |
| 31 | namespace art { |
| 32 | |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame] | 33 | static jobjectArray Executable_getDeclaredAnnotationsNative(JNIEnv* env, jobject javaMethod) { |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 34 | ScopedFastNativeObjectAccess soa(env); |
| 35 | ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod); |
| 36 | if (method->GetDeclaringClass()->IsProxyClass()) { |
| 37 | // Return an empty array instead of a null pointer. |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 38 | ObjPtr<mirror::Class> annotation_array_class = |
| 39 | soa.Decode<mirror::Class>(WellKnownClasses::java_lang_annotation_Annotation__array); |
| 40 | ObjPtr<mirror::ObjectArray<mirror::Object>> empty_array = |
Mathieu Chartier | 1a5337f | 2016-10-13 13:48:23 -0700 | [diff] [blame^] | 41 | mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), annotation_array_class, 0); |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 42 | return soa.AddLocalReference<jobjectArray>(empty_array); |
| 43 | } |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 44 | return soa.AddLocalReference<jobjectArray>(annotations::GetAnnotationsForMethod(method)); |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame] | 47 | static jobject Executable_getAnnotationNative(JNIEnv* env, |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 48 | jobject javaMethod, |
| 49 | jclass annotationType) { |
Neil Fuller | 16b21cd | 2016-08-12 09:37:02 +0100 | [diff] [blame] | 50 | ScopedFastNativeObjectAccess soa(env); |
| 51 | StackHandleScope<1> hs(soa.Self()); |
| 52 | ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod); |
| 53 | if (method->IsProxyMethod()) { |
| 54 | return nullptr; |
| 55 | } else { |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 56 | Handle<mirror::Class> klass(hs.NewHandle(soa.Decode<mirror::Class>(annotationType))); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 57 | return soa.AddLocalReference<jobject>(annotations::GetAnnotationForMethod(method, klass)); |
Neil Fuller | 16b21cd | 2016-08-12 09:37:02 +0100 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame] | 61 | static jobjectArray Executable_getSignatureAnnotation(JNIEnv* env, jobject javaMethod) { |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 62 | ScopedFastNativeObjectAccess soa(env); |
| 63 | ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod); |
| 64 | if (method->GetDeclaringClass()->IsProxyClass()) { |
| 65 | return nullptr; |
| 66 | } |
| 67 | StackHandleScope<1> hs(soa.Self()); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 68 | return soa.AddLocalReference<jobjectArray>(annotations::GetSignatureAnnotationForMethod(method)); |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame] | 72 | static jobjectArray Executable_getParameterAnnotationsNative(JNIEnv* env, jobject javaMethod) { |
Neil Fuller | 16b21cd | 2016-08-12 09:37:02 +0100 | [diff] [blame] | 73 | ScopedFastNativeObjectAccess soa(env); |
| 74 | ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod); |
| 75 | if (method->IsProxyMethod()) { |
| 76 | return nullptr; |
| 77 | } else { |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 78 | return soa.AddLocalReference<jobjectArray>(annotations::GetParameterAnnotations(method)); |
Neil Fuller | 16b21cd | 2016-08-12 09:37:02 +0100 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 82 | static jobjectArray Executable_getParameters0(JNIEnv* env, jobject javaMethod) { |
| 83 | ScopedFastNativeObjectAccess soa(env); |
| 84 | Thread* self = soa.Self(); |
| 85 | StackHandleScope<8> hs(self); |
| 86 | |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 87 | Handle<mirror::Method> executable = hs.NewHandle(soa.Decode<mirror::Method>(javaMethod)); |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 88 | ArtMethod* art_method = executable.Get()->GetArtMethod(); |
| 89 | if (art_method->GetDeclaringClass()->IsProxyClass()) { |
| 90 | return nullptr; |
| 91 | } |
| 92 | |
| 93 | // Find the MethodParameters system annotation. |
| 94 | MutableHandle<mirror::ObjectArray<mirror::String>> names = |
| 95 | hs.NewHandle<mirror::ObjectArray<mirror::String>>(nullptr); |
| 96 | MutableHandle<mirror::IntArray> access_flags = hs.NewHandle<mirror::IntArray>(nullptr); |
| 97 | if (!annotations::GetParametersMetadataForMethod(art_method, &names, &access_flags)) { |
| 98 | return nullptr; |
| 99 | } |
| 100 | |
| 101 | // Validate the MethodParameters system annotation data. |
| 102 | if (UNLIKELY(names.Get() == nullptr || access_flags.Get() == nullptr)) { |
| 103 | ThrowIllegalArgumentException( |
| 104 | StringPrintf("Missing parameter metadata for names or access flags for %s", |
| 105 | PrettyMethod(art_method).c_str()).c_str()); |
| 106 | return nullptr; |
| 107 | } |
| 108 | |
| 109 | // Check array sizes match each other |
| 110 | int32_t names_count = names.Get()->GetLength(); |
| 111 | int32_t access_flags_count = access_flags.Get()->GetLength(); |
| 112 | if (names_count != access_flags_count) { |
| 113 | ThrowIllegalArgumentException( |
| 114 | StringPrintf( |
| 115 | "Inconsistent parameter metadata for %s. names length: %d, access flags length: %d", |
| 116 | PrettyMethod(art_method).c_str(), |
| 117 | names_count, |
| 118 | access_flags_count).c_str()); |
| 119 | return nullptr; |
| 120 | } |
| 121 | |
| 122 | // Instantiate a Parameter[] to hold the result. |
| 123 | Handle<mirror::Class> parameter_array_class = |
| 124 | hs.NewHandle( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 125 | soa.Decode<mirror::Class>(WellKnownClasses::java_lang_reflect_Parameter__array)); |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 126 | Handle<mirror::ObjectArray<mirror::Object>> parameter_array = |
| 127 | hs.NewHandle( |
| 128 | mirror::ObjectArray<mirror::Object>::Alloc(self, |
| 129 | parameter_array_class.Get(), |
| 130 | names_count)); |
| 131 | if (UNLIKELY(parameter_array.Get() == nullptr)) { |
| 132 | self->AssertPendingException(); |
| 133 | return nullptr; |
| 134 | } |
| 135 | |
| 136 | Handle<mirror::Class> parameter_class = |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 137 | hs.NewHandle(soa.Decode<mirror::Class>(WellKnownClasses::java_lang_reflect_Parameter)); |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 138 | ArtMethod* parameter_init = |
| 139 | soa.DecodeMethod(WellKnownClasses::java_lang_reflect_Parameter_init); |
| 140 | |
| 141 | // Mutable handles used in the loop below to ensure cleanup without scaling the number of |
| 142 | // handles by the number of parameters. |
| 143 | MutableHandle<mirror::String> name = hs.NewHandle<mirror::String>(nullptr); |
| 144 | MutableHandle<mirror::Object> parameter = hs.NewHandle<mirror::Object>(nullptr); |
| 145 | |
| 146 | // Populate the Parameter[] to return. |
| 147 | for (int32_t parameter_index = 0; parameter_index < names_count; parameter_index++) { |
| 148 | name.Assign(names.Get()->Get(parameter_index)); |
| 149 | int32_t modifiers = access_flags.Get()->Get(parameter_index); |
| 150 | |
| 151 | // Allocate / initialize the Parameter to add to parameter_array. |
| 152 | parameter.Assign(parameter_class->AllocObject(self)); |
| 153 | if (UNLIKELY(parameter.Get() == nullptr)) { |
| 154 | self->AssertPendingOOMException(); |
| 155 | return nullptr; |
| 156 | } |
| 157 | |
| 158 | uint32_t args[5] = { PointerToLowMemUInt32(parameter.Get()), |
| 159 | PointerToLowMemUInt32(name.Get()), |
| 160 | static_cast<uint32_t>(modifiers), |
| 161 | PointerToLowMemUInt32(executable.Get()), |
| 162 | static_cast<uint32_t>(parameter_index) |
| 163 | }; |
| 164 | JValue result; |
| 165 | static const char* method_signature = "VLILI"; // return + parameter types |
| 166 | parameter_init->Invoke(self, args, sizeof(args), &result, method_signature); |
| 167 | if (UNLIKELY(self->IsExceptionPending())) { |
| 168 | return nullptr; |
| 169 | } |
| 170 | |
| 171 | // Store the Parameter in the Parameter[]. |
| 172 | parameter_array.Get()->Set(parameter_index, parameter.Get()); |
| 173 | if (UNLIKELY(self->IsExceptionPending())) { |
| 174 | return nullptr; |
| 175 | } |
| 176 | } |
| 177 | return soa.AddLocalReference<jobjectArray>(parameter_array.Get()); |
| 178 | } |
| 179 | |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame] | 180 | static jboolean Executable_isAnnotationPresentNative(JNIEnv* env, |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 181 | jobject javaMethod, |
| 182 | jclass annotationType) { |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 183 | ScopedFastNativeObjectAccess soa(env); |
| 184 | ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod); |
| 185 | if (method->GetDeclaringClass()->IsProxyClass()) { |
| 186 | return false; |
| 187 | } |
| 188 | StackHandleScope<1> hs(soa.Self()); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 189 | Handle<mirror::Class> klass(hs.NewHandle(soa.Decode<mirror::Class>(annotationType))); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 190 | return annotations::IsMethodAnnotationPresent(method, klass); |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | static JNINativeMethod gMethods[] = { |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame] | 194 | NATIVE_METHOD(Executable, getAnnotationNative, |
Neil Fuller | 16b21cd | 2016-08-12 09:37:02 +0100 | [diff] [blame] | 195 | "!(Ljava/lang/Class;)Ljava/lang/annotation/Annotation;"), |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame] | 196 | NATIVE_METHOD(Executable, getDeclaredAnnotationsNative, "!()[Ljava/lang/annotation/Annotation;"), |
| 197 | NATIVE_METHOD(Executable, getParameterAnnotationsNative, |
Neil Fuller | 16b21cd | 2016-08-12 09:37:02 +0100 | [diff] [blame] | 198 | "!()[[Ljava/lang/annotation/Annotation;"), |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 199 | NATIVE_METHOD(Executable, getParameters0, "!()[Ljava/lang/reflect/Parameter;"), |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame] | 200 | NATIVE_METHOD(Executable, getSignatureAnnotation, "!()[Ljava/lang/String;"), |
| 201 | NATIVE_METHOD(Executable, isAnnotationPresentNative, "!(Ljava/lang/Class;)Z"), |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 202 | }; |
| 203 | |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame] | 204 | void register_java_lang_reflect_Executable(JNIEnv* env) { |
| 205 | REGISTER_NATIVE_METHODS("java/lang/reflect/Executable"); |
Jeff Hao | 1133db7 | 2016-04-04 19:50:14 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | } // namespace art |