blob: a0a6a12505efa0044cbfaa93106e55d3e1c52d71 [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
19#include "art_method-inl.h"
David Sehr9323e6e2016-09-13 08:58:35 -070020#include "dex_file_annotations.h"
Neil Fuller79a21e72016-09-09 14:24:51 +010021#include "handle.h"
Jeff Hao1133db72016-04-04 19:50:14 -070022#include "jni_internal.h"
23#include "mirror/class-inl.h"
Neil Fuller79a21e72016-09-09 14:24:51 +010024#include "mirror/method.h"
Jeff Hao1133db72016-04-04 19:50:14 -070025#include "mirror/object-inl.h"
26#include "mirror/object_array-inl.h"
27#include "reflection.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070028#include "scoped_fast_native_object_access-inl.h"
Jeff Hao1133db72016-04-04 19:50:14 -070029#include "well_known_classes.h"
30
31namespace art {
32
Neil Fuller0e844392016-09-08 13:43:31 +010033static jobjectArray Executable_getDeclaredAnnotationsNative(JNIEnv* env, jobject javaMethod) {
Jeff Hao1133db72016-04-04 19:50:14 -070034 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 Chartier0795f232016-09-27 18:43:30 -070038 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 Chartier1a5337f2016-10-13 13:48:23 -070041 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), annotation_array_class, 0);
Jeff Hao1133db72016-04-04 19:50:14 -070042 return soa.AddLocalReference<jobjectArray>(empty_array);
43 }
David Sehr9323e6e2016-09-13 08:58:35 -070044 return soa.AddLocalReference<jobjectArray>(annotations::GetAnnotationsForMethod(method));
Jeff Hao1133db72016-04-04 19:50:14 -070045}
46
Neil Fuller0e844392016-09-08 13:43:31 +010047static jobject Executable_getAnnotationNative(JNIEnv* env,
Neil Fuller79a21e72016-09-09 14:24:51 +010048 jobject javaMethod,
49 jclass annotationType) {
Neil Fuller16b21cd2016-08-12 09:37:02 +010050 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 Chartier0795f232016-09-27 18:43:30 -070056 Handle<mirror::Class> klass(hs.NewHandle(soa.Decode<mirror::Class>(annotationType)));
David Sehr9323e6e2016-09-13 08:58:35 -070057 return soa.AddLocalReference<jobject>(annotations::GetAnnotationForMethod(method, klass));
Neil Fuller16b21cd2016-08-12 09:37:02 +010058 }
59}
60
Neil Fuller0e844392016-09-08 13:43:31 +010061static jobjectArray Executable_getSignatureAnnotation(JNIEnv* env, jobject javaMethod) {
Jeff Hao1133db72016-04-04 19:50:14 -070062 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 Sehr9323e6e2016-09-13 08:58:35 -070068 return soa.AddLocalReference<jobjectArray>(annotations::GetSignatureAnnotationForMethod(method));
Jeff Hao1133db72016-04-04 19:50:14 -070069}
70
71
Neil Fuller0e844392016-09-08 13:43:31 +010072static jobjectArray Executable_getParameterAnnotationsNative(JNIEnv* env, jobject javaMethod) {
Neil Fuller16b21cd2016-08-12 09:37:02 +010073 ScopedFastNativeObjectAccess soa(env);
74 ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
75 if (method->IsProxyMethod()) {
76 return nullptr;
77 } else {
David Sehr9323e6e2016-09-13 08:58:35 -070078 return soa.AddLocalReference<jobjectArray>(annotations::GetParameterAnnotations(method));
Neil Fuller16b21cd2016-08-12 09:37:02 +010079 }
80}
81
Neil Fuller79a21e72016-09-09 14:24:51 +010082static jobjectArray Executable_getParameters0(JNIEnv* env, jobject javaMethod) {
83 ScopedFastNativeObjectAccess soa(env);
84 Thread* self = soa.Self();
85 StackHandleScope<8> hs(self);
86
Mathieu Chartier0795f232016-09-27 18:43:30 -070087 Handle<mirror::Method> executable = hs.NewHandle(soa.Decode<mirror::Method>(javaMethod));
Neil Fuller79a21e72016-09-09 14:24:51 +010088 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 Chartier0795f232016-09-27 18:43:30 -0700125 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_reflect_Parameter__array));
Neil Fuller79a21e72016-09-09 14:24:51 +0100126 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 Chartier0795f232016-09-27 18:43:30 -0700137 hs.NewHandle(soa.Decode<mirror::Class>(WellKnownClasses::java_lang_reflect_Parameter));
Neil Fuller79a21e72016-09-09 14:24:51 +0100138 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 Fuller0e844392016-09-08 13:43:31 +0100180static jboolean Executable_isAnnotationPresentNative(JNIEnv* env,
Neil Fuller79a21e72016-09-09 14:24:51 +0100181 jobject javaMethod,
182 jclass annotationType) {
Jeff Hao1133db72016-04-04 19:50:14 -0700183 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 Chartier0795f232016-09-27 18:43:30 -0700189 Handle<mirror::Class> klass(hs.NewHandle(soa.Decode<mirror::Class>(annotationType)));
David Sehr9323e6e2016-09-13 08:58:35 -0700190 return annotations::IsMethodAnnotationPresent(method, klass);
Jeff Hao1133db72016-04-04 19:50:14 -0700191}
192
193static JNINativeMethod gMethods[] = {
Neil Fuller0e844392016-09-08 13:43:31 +0100194 NATIVE_METHOD(Executable, getAnnotationNative,
Neil Fuller16b21cd2016-08-12 09:37:02 +0100195 "!(Ljava/lang/Class;)Ljava/lang/annotation/Annotation;"),
Neil Fuller0e844392016-09-08 13:43:31 +0100196 NATIVE_METHOD(Executable, getDeclaredAnnotationsNative, "!()[Ljava/lang/annotation/Annotation;"),
197 NATIVE_METHOD(Executable, getParameterAnnotationsNative,
Neil Fuller16b21cd2016-08-12 09:37:02 +0100198 "!()[[Ljava/lang/annotation/Annotation;"),
Neil Fuller79a21e72016-09-09 14:24:51 +0100199 NATIVE_METHOD(Executable, getParameters0, "!()[Ljava/lang/reflect/Parameter;"),
Neil Fuller0e844392016-09-08 13:43:31 +0100200 NATIVE_METHOD(Executable, getSignatureAnnotation, "!()[Ljava/lang/String;"),
201 NATIVE_METHOD(Executable, isAnnotationPresentNative, "!(Ljava/lang/Class;)Z"),
Jeff Hao1133db72016-04-04 19:50:14 -0700202};
203
Neil Fuller0e844392016-09-08 13:43:31 +0100204void register_java_lang_reflect_Executable(JNIEnv* env) {
205 REGISTER_NATIVE_METHODS("java/lang/reflect/Executable");
Jeff Hao1133db72016-04-04 19:50:14 -0700206}
207
208} // namespace art