native_bridge_art_interface.cc revision 93de4273d72a2558a7b3423547b5074cd76c5796
1/*
2 * Copyright (C) 2014 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 "native_bridge_art_interface.h"
18
19#include "mirror/art_method-inl.h"
20#include "mirror/class-inl.h"
21#include "scoped_thread_state_change.h"
22
23namespace art {
24
25const char* GetMethodShorty(JNIEnv* env, jmethodID mid) {
26  ScopedObjectAccess soa(env);
27  StackHandleScope<1> scope(soa.Self());
28  mirror::ArtMethod* m = soa.DecodeMethod(mid);
29  MethodHelper mh(scope.NewHandle(m));
30  return mh.GetShorty();
31}
32
33uint32_t GetNativeMethodCount(JNIEnv* env, jclass clazz) {
34  if (clazz == nullptr)
35    return 0;
36
37  ScopedObjectAccess soa(env);
38  mirror::Class* c = soa.Decode<mirror::Class*>(clazz);
39
40  uint32_t native_method_count = 0;
41  for (uint32_t i = 0; i < c->NumDirectMethods(); ++i) {
42    mirror::ArtMethod* m = c->GetDirectMethod(i);
43    if (m->IsNative()) {
44      native_method_count++;
45    }
46  }
47  for (uint32_t i = 0; i < c->NumVirtualMethods(); ++i) {
48    mirror::ArtMethod* m = c->GetVirtualMethod(i);
49    if (m->IsNative()) {
50      native_method_count++;
51    }
52  }
53  return native_method_count;
54}
55
56uint32_t GetNativeMethods(JNIEnv* env, jclass clazz, JNINativeMethod* methods,
57                          uint32_t method_count) {
58  if ((clazz == nullptr) || (methods == nullptr)) {
59    return 0;
60  }
61  ScopedObjectAccess soa(env);
62  mirror::Class* c = soa.Decode<mirror::Class*>(clazz);
63
64  uint32_t count = 0;
65  for (uint32_t i = 0; i < c->NumDirectMethods(); ++i) {
66    mirror::ArtMethod* m = c->GetDirectMethod(i);
67    if (m->IsNative()) {
68      if (count < method_count) {
69        methods[count].name = m->GetName();
70        methods[count].signature = m->GetShorty();
71        methods[count].fnPtr = const_cast<void*>(m->GetNativeMethod());
72        count++;
73      } else {
74        LOG(WARNING) << "Output native method array too small. Skipping " << PrettyMethod(m);
75      }
76    }
77  }
78  for (uint32_t i = 0; i < c->NumVirtualMethods(); ++i) {
79    mirror::ArtMethod* m = c->GetVirtualMethod(i);
80    if (m->IsNative()) {
81      if (count < method_count) {
82        methods[count].name = m->GetName();
83        methods[count].signature = m->GetShorty();
84        methods[count].fnPtr = const_cast<void*>(m->GetNativeMethod());
85        count++;
86      } else {
87        LOG(WARNING) << "Output native method array too small. Skipping " << PrettyMethod(m);
88      }
89    }
90  }
91  return count;
92}
93
94};  // namespace art
95