blob: 89442703b6b1b9fa9e27395a40d07ee447a58651 [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"
Ian Rogers8b2c0b92013-09-19 02:56:49 -070021#include "mirror/dex_cache.h"
22#include "mirror/object-inl.h"
Ian Rogers1eb512d2013-10-18 15:42:20 -070023#include "scoped_fast_native_object_access.h"
Ian Rogers8b2c0b92013-09-19 02:56:49 -070024#include "well_known_classes.h"
25
26namespace art {
27
28static jobject DexCache_getDexNative(JNIEnv* env, jobject javaDexCache) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070029 ScopedFastNativeObjectAccess soa(env);
Ian Rogers8b2c0b92013-09-19 02:56:49 -070030 mirror::DexCache* dex_cache = soa.Decode<mirror::DexCache*>(javaDexCache);
31 // Should only be called while holding the lock on the dex cache.
Ian Rogersd9c4fc92013-10-01 19:45:43 -070032 DCHECK_EQ(dex_cache->GetLockOwnerThreadId(), soa.Self()->GetThreadId());
Ian Rogers8b2c0b92013-09-19 02:56:49 -070033 const DexFile* dex_file = dex_cache->GetDexFile();
34 if (dex_file == NULL) {
35 return NULL;
36 }
37 void* address = const_cast<void*>(reinterpret_cast<const void*>(dex_file->Begin()));
38 jobject byte_buffer = env->NewDirectByteBuffer(address, dex_file->Size());
39 if (byte_buffer == NULL) {
40 DCHECK(soa.Self()->IsExceptionPending());
41 return NULL;
42 }
43
44 jvalue args[1];
45 args[0].l = byte_buffer;
46 return env->CallStaticObjectMethodA(WellKnownClasses::com_android_dex_Dex,
47 WellKnownClasses::com_android_dex_Dex_create,
48 args);
49}
50
Vladimir Markoc1363122015-04-09 14:13:13 +010051static jobject DexCache_getResolvedType(JNIEnv* env, jobject javaDexCache, jint type_index) {
52 ScopedFastNativeObjectAccess soa(env);
53 mirror::DexCache* dex_cache = soa.Decode<mirror::DexCache*>(javaDexCache);
54 return soa.AddLocalReference<jobject>(dex_cache->GetResolvedType(type_index));
55}
56
57static jobject DexCache_getResolvedString(JNIEnv* env, jobject javaDexCache, jint string_index) {
58 ScopedFastNativeObjectAccess soa(env);
59 mirror::DexCache* dex_cache = soa.Decode<mirror::DexCache*>(javaDexCache);
60 return soa.AddLocalReference<jobject>(dex_cache->GetResolvedString(string_index));
61}
62
63static void DexCache_setResolvedType(JNIEnv* env, jobject javaDexCache, jint type_index,
64 jobject type) {
65 ScopedFastNativeObjectAccess soa(env);
66 mirror::DexCache* dex_cache = soa.Decode<mirror::DexCache*>(javaDexCache);
67 dex_cache->SetResolvedType(type_index, soa.Decode<mirror::Class*>(type));
68}
69
70static void DexCache_setResolvedString(JNIEnv* env, jobject javaDexCache, jint string_index,
71 jobject string) {
72 ScopedFastNativeObjectAccess soa(env);
73 mirror::DexCache* dex_cache = soa.Decode<mirror::DexCache*>(javaDexCache);
74 dex_cache->SetResolvedString(string_index, soa.Decode<mirror::String*>(string));
75}
76
Ian Rogers8b2c0b92013-09-19 02:56:49 -070077static JNINativeMethod gMethods[] = {
Ian Rogers1eb512d2013-10-18 15:42:20 -070078 NATIVE_METHOD(DexCache, getDexNative, "!()Lcom/android/dex/Dex;"),
Vladimir Markoc1363122015-04-09 14:13:13 +010079 NATIVE_METHOD(DexCache, getResolvedType, "!(I)Ljava/lang/Class;"),
80 NATIVE_METHOD(DexCache, getResolvedString, "!(I)Ljava/lang/String;"),
81 NATIVE_METHOD(DexCache, setResolvedType, "!(ILjava/lang/Class;)V"),
82 NATIVE_METHOD(DexCache, setResolvedString, "!(ILjava/lang/String;)V"),
Ian Rogers8b2c0b92013-09-19 02:56:49 -070083};
84
85void register_java_lang_DexCache(JNIEnv* env) {
86 REGISTER_NATIVE_METHODS("java/lang/DexCache");
87}
88
89} // namespace art