• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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 "art_method.h"
18 #include "base/enums.h"
19 #include "jni.h"
20 #include "mirror/array-inl.h"
21 #include "mirror/class-inl.h"
22 #include "mirror/dex_cache-inl.h"
23 #include "scoped_thread_state_change-inl.h"
24 #include "stack.h"
25 #include "thread.h"
26 
27 namespace art {
28 
29 namespace {
30 
Java_Main_cloneResolvedMethods(JNIEnv * env,jclass,jclass cls)31 extern "C" JNIEXPORT jobject JNICALL Java_Main_cloneResolvedMethods(JNIEnv* env,
32                                                                     jclass,
33                                                                     jclass cls) {
34   ScopedObjectAccess soa(Thread::Current());
35   mirror::DexCache* dex_cache = soa.Decode<mirror::Class>(cls)->GetDexCache();
36   size_t num_methods = dex_cache->NumResolvedMethods();
37   ArtMethod** methods = dex_cache->GetResolvedMethods();
38   CHECK_EQ(num_methods != 0u, methods != nullptr);
39   if (num_methods == 0u) {
40     return nullptr;
41   }
42   jarray array;
43   if (sizeof(void*) == 4) {
44     array = env->NewIntArray(num_methods);
45   } else {
46     array = env->NewLongArray(num_methods);
47   }
48   CHECK(array != nullptr);
49   mirror::PointerArray* pointer_array = soa.Decode<mirror::PointerArray>(array).Ptr();
50   for (size_t i = 0; i != num_methods; ++i) {
51     ArtMethod* method = mirror::DexCache::GetElementPtrSize(methods, i, kRuntimePointerSize);
52     pointer_array->SetElementPtrSize(i, method, kRuntimePointerSize);
53   }
54   return array;
55 }
56 
Java_Main_restoreResolvedMethods(JNIEnv *,jclass,jclass cls,jobject old_cache)57 extern "C" JNIEXPORT void JNICALL Java_Main_restoreResolvedMethods(
58     JNIEnv*, jclass, jclass cls, jobject old_cache) {
59   ScopedObjectAccess soa(Thread::Current());
60   mirror::DexCache* dex_cache = soa.Decode<mirror::Class>(cls)->GetDexCache();
61   size_t num_methods = dex_cache->NumResolvedMethods();
62   ArtMethod** methods = soa.Decode<mirror::Class>(cls)->GetDexCache()->GetResolvedMethods();
63   CHECK_EQ(num_methods != 0u, methods != nullptr);
64   ObjPtr<mirror::PointerArray> old = soa.Decode<mirror::PointerArray>(old_cache);
65   CHECK_EQ(methods != nullptr, old != nullptr);
66   CHECK_EQ(num_methods, static_cast<size_t>(old->GetLength()));
67   for (size_t i = 0; i != num_methods; ++i) {
68     ArtMethod* method = old->GetElementPtrSize<ArtMethod*>(i, kRuntimePointerSize);
69     mirror::DexCache::SetElementPtrSize(methods, i, method, kRuntimePointerSize);
70   }
71 }
72 
73 }  // namespace
74 
75 }  // namespace art
76