blob: 71379a51a550adc52b562e50774b9870d150c84d [file] [log] [blame]
Ian Rogers8b2c0b92013-09-19 02:56:49 -07001/*
2 * Copyright (C) 2008 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
Andreas Gampe277ccbd2014-11-03 21:36:10 -080017#include "java_lang_DexCache.h"
18
Ian Rogers8b2c0b92013-09-19 02:56:49 -070019#include "dex_file.h"
Ian Rogers68d8b422014-07-17 11:09:10 -070020#include "jni_internal.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070021#include "mirror/class-inl.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010022#include "mirror/dex_cache-inl.h"
Ian Rogers8b2c0b92013-09-19 02:56:49 -070023#include "mirror/object-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070024#include "scoped_fast_native_object_access-inl.h"
Ian Rogers8b2c0b92013-09-19 02:56:49 -070025#include "well_known_classes.h"
26
27namespace art {
28
29static jobject DexCache_getDexNative(JNIEnv* env, jobject javaDexCache) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070030 ScopedFastNativeObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -070031 ObjPtr<mirror::DexCache> dex_cache = soa.Decode<mirror::DexCache>(javaDexCache);
Ian Rogers8b2c0b92013-09-19 02:56:49 -070032 // Should only be called while holding the lock on the dex cache.
Ian Rogersd9c4fc92013-10-01 19:45:43 -070033 DCHECK_EQ(dex_cache->GetLockOwnerThreadId(), soa.Self()->GetThreadId());
Ian Rogers8b2c0b92013-09-19 02:56:49 -070034 const DexFile* dex_file = dex_cache->GetDexFile();
Mathieu Chartier2cebb242015-04-21 16:50:40 -070035 if (dex_file == nullptr) {
36 return nullptr;
Ian Rogers8b2c0b92013-09-19 02:56:49 -070037 }
38 void* address = const_cast<void*>(reinterpret_cast<const void*>(dex_file->Begin()));
39 jobject byte_buffer = env->NewDirectByteBuffer(address, dex_file->Size());
Mathieu Chartier2cebb242015-04-21 16:50:40 -070040 if (byte_buffer == nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -070041 DCHECK(soa.Self()->IsExceptionPending());
Mathieu Chartier2cebb242015-04-21 16:50:40 -070042 return nullptr;
Ian Rogers8b2c0b92013-09-19 02:56:49 -070043 }
44
45 jvalue args[1];
46 args[0].l = byte_buffer;
47 return env->CallStaticObjectMethodA(WellKnownClasses::com_android_dex_Dex,
48 WellKnownClasses::com_android_dex_Dex_create,
49 args);
50}
51
Vladimir Markoc1363122015-04-09 14:13:13 +010052static jobject DexCache_getResolvedType(JNIEnv* env, jobject javaDexCache, jint type_index) {
53 ScopedFastNativeObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -070054 ObjPtr<mirror::DexCache> dex_cache = soa.Decode<mirror::DexCache>(javaDexCache);
Vladimir Marko05792b92015-08-03 11:56:49 +010055 CHECK_LT(static_cast<size_t>(type_index), dex_cache->NumResolvedTypes());
Vladimir Markoc1363122015-04-09 14:13:13 +010056 return soa.AddLocalReference<jobject>(dex_cache->GetResolvedType(type_index));
57}
58
59static jobject DexCache_getResolvedString(JNIEnv* env, jobject javaDexCache, jint string_index) {
60 ScopedFastNativeObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -070061 ObjPtr<mirror::DexCache> dex_cache = soa.Decode<mirror::DexCache>(javaDexCache);
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070062 CHECK_LT(static_cast<size_t>(string_index), dex_cache->GetDexFile()->NumStringIds());
Vladimir Markoc1363122015-04-09 14:13:13 +010063 return soa.AddLocalReference<jobject>(dex_cache->GetResolvedString(string_index));
64}
65
66static void DexCache_setResolvedType(JNIEnv* env, jobject javaDexCache, jint type_index,
67 jobject type) {
68 ScopedFastNativeObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -070069 ObjPtr<mirror::DexCache> dex_cache = soa.Decode<mirror::DexCache>(javaDexCache);
Vladimir Marko05792b92015-08-03 11:56:49 +010070 CHECK_LT(static_cast<size_t>(type_index), dex_cache->NumResolvedTypes());
Mathieu Chartierbc5a7952016-10-17 15:46:31 -070071 dex_cache->SetResolvedType(type_index, soa.Decode<mirror::Class>(type));
Vladimir Markoc1363122015-04-09 14:13:13 +010072}
73
74static void DexCache_setResolvedString(JNIEnv* env, jobject javaDexCache, jint string_index,
75 jobject string) {
76 ScopedFastNativeObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -070077 ObjPtr<mirror::DexCache> dex_cache = soa.Decode<mirror::DexCache>(javaDexCache);
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070078 CHECK_LT(static_cast<size_t>(string_index), dex_cache->GetDexFile()->NumStringIds());
Mathieu Chartierbc5a7952016-10-17 15:46:31 -070079 dex_cache->SetResolvedString(string_index, soa.Decode<mirror::String>(string));
Vladimir Markoc1363122015-04-09 14:13:13 +010080}
81
Ian Rogers8b2c0b92013-09-19 02:56:49 -070082static JNINativeMethod gMethods[] = {
Ian Rogers1eb512d2013-10-18 15:42:20 -070083 NATIVE_METHOD(DexCache, getDexNative, "!()Lcom/android/dex/Dex;"),
Vladimir Markoc1363122015-04-09 14:13:13 +010084 NATIVE_METHOD(DexCache, getResolvedType, "!(I)Ljava/lang/Class;"),
85 NATIVE_METHOD(DexCache, getResolvedString, "!(I)Ljava/lang/String;"),
86 NATIVE_METHOD(DexCache, setResolvedType, "!(ILjava/lang/Class;)V"),
87 NATIVE_METHOD(DexCache, setResolvedString, "!(ILjava/lang/String;)V"),
Ian Rogers8b2c0b92013-09-19 02:56:49 -070088};
89
90void register_java_lang_DexCache(JNIEnv* env) {
91 REGISTER_NATIVE_METHODS("java/lang/DexCache");
92}
93
94} // namespace art