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
17#include "java_lang_reflect_AbstractMethod.h"
18
19#include "art_method-inl.h"
20#include "jni_internal.h"
21#include "mirror/class-inl.h"
22#include "mirror/object-inl.h"
23#include "mirror/object_array-inl.h"
24#include "reflection.h"
25#include "scoped_fast_native_object_access.h"
26#include "well_known_classes.h"
27
28namespace art {
29
30static jobjectArray AbstractMethod_getDeclaredAnnotations(JNIEnv* env, jobject javaMethod) {
31  ScopedFastNativeObjectAccess soa(env);
32  ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
33  if (method->GetDeclaringClass()->IsProxyClass()) {
34    // Return an empty array instead of a null pointer.
35    mirror::Class* annotation_array_class =
36        soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_annotation_Annotation__array);
37    mirror::ObjectArray<mirror::Object>* empty_array =
38        mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), annotation_array_class, 0);
39    return soa.AddLocalReference<jobjectArray>(empty_array);
40  }
41  return soa.AddLocalReference<jobjectArray>(method->GetDexFile()->GetAnnotationsForMethod(method));
42}
43
44static jobjectArray AbstractMethod_getSignatureAnnotation(JNIEnv* env, jobject javaMethod) {
45  ScopedFastNativeObjectAccess soa(env);
46  ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
47  if (method->GetDeclaringClass()->IsProxyClass()) {
48    return nullptr;
49  }
50  StackHandleScope<1> hs(soa.Self());
51  return soa.AddLocalReference<jobjectArray>(
52      method->GetDexFile()->GetSignatureAnnotationForMethod(method));
53}
54
55
56static jboolean AbstractMethod_isAnnotationPresentNative(JNIEnv* env, jobject javaMethod,
57                                                         jclass annotationType) {
58  ScopedFastNativeObjectAccess soa(env);
59  ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
60  if (method->GetDeclaringClass()->IsProxyClass()) {
61    return false;
62  }
63  StackHandleScope<1> hs(soa.Self());
64  Handle<mirror::Class> klass(hs.NewHandle(soa.Decode<mirror::Class*>(annotationType)));
65  return method->GetDexFile()->IsMethodAnnotationPresent(method, klass);
66}
67
68static JNINativeMethod gMethods[] = {
69  NATIVE_METHOD(AbstractMethod, getDeclaredAnnotations, "!()[Ljava/lang/annotation/Annotation;"),
70  NATIVE_METHOD(AbstractMethod, getSignatureAnnotation, "!()[Ljava/lang/String;"),
71  NATIVE_METHOD(AbstractMethod, isAnnotationPresentNative, "!(Ljava/lang/Class;)Z"),
72};
73
74void register_java_lang_reflect_AbstractMethod(JNIEnv* env) {
75  REGISTER_NATIVE_METHODS("java/lang/reflect/AbstractMethod");
76}
77
78}  // namespace art
79