blob: 2a3942829fdfc4c9452c7e9308415cd7736e6b09 [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
197static JNINativeMethod gMethods[] = {
Neil Fuller0e844392016-09-08 13:43:31 +0100198 NATIVE_METHOD(Executable, getAnnotationNative,
Neil Fuller16b21cd2016-08-12 09:37:02 +0100199 "!(Ljava/lang/Class;)Ljava/lang/annotation/Annotation;"),
Neil Fuller0e844392016-09-08 13:43:31 +0100200 NATIVE_METHOD(Executable, getDeclaredAnnotationsNative, "!()[Ljava/lang/annotation/Annotation;"),
201 NATIVE_METHOD(Executable, getParameterAnnotationsNative,
Neil Fuller16b21cd2016-08-12 09:37:02 +0100202 "!()[[Ljava/lang/annotation/Annotation;"),
Neil Fuller79a21e72016-09-09 14:24:51 +0100203 NATIVE_METHOD(Executable, getParameters0, "!()[Ljava/lang/reflect/Parameter;"),
Neil Fuller0e844392016-09-08 13:43:31 +0100204 NATIVE_METHOD(Executable, getSignatureAnnotation, "!()[Ljava/lang/String;"),
205 NATIVE_METHOD(Executable, isAnnotationPresentNative, "!(Ljava/lang/Class;)Z"),
Jeff Hao1133db72016-04-04 19:50:14 -0700206};
207
Neil Fuller0e844392016-09-08 13:43:31 +0100208void register_java_lang_reflect_Executable(JNIEnv* env) {
209 REGISTER_NATIVE_METHODS("java/lang/reflect/Executable");
Jeff Hao1133db72016-04-04 19:50:14 -0700210}
211
212} // namespace art