Home | History | Annotate | Download | only in native
      1 /*
      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 
     17 #include "java_lang_DexCache.h"
     18 
     19 #include "dex_file.h"
     20 #include "jni_internal.h"
     21 #include "mirror/class-inl.h"
     22 #include "mirror/dex_cache-inl.h"
     23 #include "mirror/object-inl.h"
     24 #include "scoped_fast_native_object_access.h"
     25 #include "well_known_classes.h"
     26 
     27 namespace art {
     28 
     29 static jobject DexCache_getDexNative(JNIEnv* env, jobject javaDexCache) {
     30   ScopedFastNativeObjectAccess soa(env);
     31   mirror::DexCache* dex_cache = soa.Decode<mirror::DexCache*>(javaDexCache);
     32   // Should only be called while holding the lock on the dex cache.
     33   DCHECK_EQ(dex_cache->GetLockOwnerThreadId(), soa.Self()->GetThreadId());
     34   const DexFile* dex_file = dex_cache->GetDexFile();
     35   if (dex_file == nullptr) {
     36     return nullptr;
     37   }
     38   void* address = const_cast<void*>(reinterpret_cast<const void*>(dex_file->Begin()));
     39   jobject byte_buffer = env->NewDirectByteBuffer(address, dex_file->Size());
     40   if (byte_buffer == nullptr) {
     41     DCHECK(soa.Self()->IsExceptionPending());
     42     return nullptr;
     43   }
     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 
     52 static jobject DexCache_getResolvedType(JNIEnv* env, jobject javaDexCache, jint type_index) {
     53   ScopedFastNativeObjectAccess soa(env);
     54   mirror::DexCache* dex_cache = soa.Decode<mirror::DexCache*>(javaDexCache);
     55   CHECK_LT(static_cast<size_t>(type_index), dex_cache->NumResolvedTypes());
     56   return soa.AddLocalReference<jobject>(dex_cache->GetResolvedType(type_index));
     57 }
     58 
     59 static jobject DexCache_getResolvedString(JNIEnv* env, jobject javaDexCache, jint string_index) {
     60   ScopedFastNativeObjectAccess soa(env);
     61   mirror::DexCache* dex_cache = soa.Decode<mirror::DexCache*>(javaDexCache);
     62   CHECK_LT(static_cast<size_t>(string_index), dex_cache->NumStrings());
     63   return soa.AddLocalReference<jobject>(dex_cache->GetResolvedString(string_index));
     64 }
     65 
     66 static void DexCache_setResolvedType(JNIEnv* env, jobject javaDexCache, jint type_index,
     67                                      jobject type) {
     68   ScopedFastNativeObjectAccess soa(env);
     69   mirror::DexCache* dex_cache = soa.Decode<mirror::DexCache*>(javaDexCache);
     70   CHECK_LT(static_cast<size_t>(type_index), dex_cache->NumResolvedTypes());
     71   dex_cache->SetResolvedType(type_index, soa.Decode<mirror::Class*>(type));
     72 }
     73 
     74 static void DexCache_setResolvedString(JNIEnv* env, jobject javaDexCache, jint string_index,
     75                                        jobject string) {
     76   ScopedFastNativeObjectAccess soa(env);
     77   mirror::DexCache* dex_cache = soa.Decode<mirror::DexCache*>(javaDexCache);
     78   CHECK_LT(static_cast<size_t>(string_index), dex_cache->NumStrings());
     79   dex_cache->SetResolvedString(string_index, soa.Decode<mirror::String*>(string));
     80 }
     81 
     82 static JNINativeMethod gMethods[] = {
     83   NATIVE_METHOD(DexCache, getDexNative, "!()Lcom/android/dex/Dex;"),
     84   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"),
     88 };
     89 
     90 void register_java_lang_DexCache(JNIEnv* env) {
     91   REGISTER_NATIVE_METHODS("java/lang/DexCache");
     92 }
     93 
     94 }  // namespace art
     95