Skip to content

Commit

Permalink
🐛 Implement lazy loading
Browse files Browse the repository at this point in the history
  • Loading branch information
yhs0602 committed May 15, 2024
1 parent 645f1d6 commit 8c23488
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/main/kotlin/vm/classloader/DexClassLoader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class DexClassLoader(
TypeId("Ljava/lang/Object;") to ObjectType
)

private val typeIdToClassDef = mutableMapOf<TypeId, ParsedClass>()
private val loadedMockedClasses = mutableSetOf<Class<*>>()

private fun loadClass(clazz: Class<*>): Type {
Expand Down Expand Up @@ -87,16 +88,22 @@ class DexClassLoader(
mockedClasses.forEach { (typeId, mockedClass) ->
loadClass(typeId, mockedClass)
}
// load dex classes
// lazy load dex classes
dexFiles.forEach { dexFile ->
dexFile.classDefs.forEach { classDef ->
val typeId = classDef.classDef.typeId
loadClass(typeId, classDef)
typeIdToClassDef[typeId] = classDef
// loadClass(typeId, classDef)
}
}
}

fun getClass(typeId: TypeId): Type {
return loadedTypes[typeId] ?: error("Class ${typeId.descriptor} not loaded")
val loadedClass = loadedTypes[typeId]
if (loadedClass != null) {
return loadedClass
}
val classDef = typeIdToClassDef[typeId] ?: error("Class ${typeId.descriptor} not found")
return loadClass(typeId, classDef)
}
}

0 comments on commit 8c23488

Please sign in to comment.