From fda857963bf906b0eae284ae621777250f0cceed Mon Sep 17 00:00:00 2001 From: Graham Date: Sat, 29 Feb 2020 14:39:03 +0000 Subject: [PATCH] Make ClassPath::get() return null if a class is not found --- .../openrs2/asm/classpath/AsmClassMetadata.kt | 4 ++-- .../dev/openrs2/asm/classpath/ClassPath.kt | 22 ++++++++++++++----- .../asm/classpath/ReflectionClassMetadata.kt | 4 ++-- .../dev/openrs2/deob/remap/TypedRemapper.kt | 4 ++-- .../deob/transform/OverrideTransformer.kt | 2 +- 5 files changed, 23 insertions(+), 13 deletions(-) diff --git a/asm/src/main/java/dev/openrs2/asm/classpath/AsmClassMetadata.kt b/asm/src/main/java/dev/openrs2/asm/classpath/AsmClassMetadata.kt index e4e72fd0..9c1af044 100644 --- a/asm/src/main/java/dev/openrs2/asm/classpath/AsmClassMetadata.kt +++ b/asm/src/main/java/dev/openrs2/asm/classpath/AsmClassMetadata.kt @@ -16,10 +16,10 @@ class AsmClassMetadata( get() = clazz.access and Opcodes.ACC_INTERFACE != 0 override val superClass - get() = if (clazz.superName != null) classPath[clazz.superName] else null + get() = if (clazz.superName != null) classPath[clazz.superName]!! else null override val superInterfaces - get() = clazz.interfaces.map { classPath[it] }.toList() + get() = clazz.interfaces.map { classPath[it]!! }.toList() override val fields get() = clazz.fields.map { MemberDesc(it) }.toList() diff --git a/asm/src/main/java/dev/openrs2/asm/classpath/ClassPath.kt b/asm/src/main/java/dev/openrs2/asm/classpath/ClassPath.kt index 58028e5e..9a496535 100644 --- a/asm/src/main/java/dev/openrs2/asm/classpath/ClassPath.kt +++ b/asm/src/main/java/dev/openrs2/asm/classpath/ClassPath.kt @@ -13,20 +13,30 @@ class ClassPath( private val dependencies: List, val libraries: List ) { - private val cache = mutableMapOf() + private val cache = mutableMapOf() val libraryClasses: List get() { val classes = mutableListOf() for (library in libraries) { for (clazz in library) { - classes.add(get(clazz.name)) + classes.add(get(clazz.name)!!) } } return classes } - operator fun get(name: String): ClassMetadata = cache.computeIfAbsent(name) { + private inline fun computeIfAbsent(name: String, f: (String) -> ClassMetadata?): ClassMetadata? { + if (cache.containsKey(name)) { + return cache[name] + } + + val clazz = f(name) + cache[name] = clazz + return clazz + } + + operator fun get(name: String): ClassMetadata? = computeIfAbsent(name) { for (library in libraries) { val clazz = library[name] if (clazz != null) { @@ -44,7 +54,7 @@ class ClassPath( val clazz = try { runtime.loadClass(name.toBinaryClassName()) } catch (ex: ClassNotFoundException) { - throw IllegalArgumentException("Unknown class $name") + return@computeIfAbsent null } return@computeIfAbsent ReflectionClassMetadata(this, clazz) @@ -75,7 +85,7 @@ class ClassPath( for (library in libraries) { for (clazz in library) { - populateInheritedFieldSets(ancestorCache, disjointSet, get(clazz.name)) + populateInheritedFieldSets(ancestorCache, disjointSet, get(clazz.name)!!) } } @@ -130,7 +140,7 @@ class ClassPath( for (library in libraries) { for (clazz in library) { - populateInheritedMethodSets(ancestorCache, disjointSet, get(clazz.name)) + populateInheritedMethodSets(ancestorCache, disjointSet, get(clazz.name)!!) } } diff --git a/asm/src/main/java/dev/openrs2/asm/classpath/ReflectionClassMetadata.kt b/asm/src/main/java/dev/openrs2/asm/classpath/ReflectionClassMetadata.kt index d39ad244..74fa9337 100644 --- a/asm/src/main/java/dev/openrs2/asm/classpath/ReflectionClassMetadata.kt +++ b/asm/src/main/java/dev/openrs2/asm/classpath/ReflectionClassMetadata.kt @@ -18,10 +18,10 @@ class ReflectionClassMetadata(private val classPath: ClassPath, private val claz get() = clazz.isInterface override val superClass: ClassMetadata? - get() = if (clazz.superclass != null) classPath[clazz.superclass.asmName] else null + get() = if (clazz.superclass != null) classPath[clazz.superclass.asmName]!! else null override val superInterfaces - get() = clazz.interfaces.map { classPath[it.asmName] }.toList() + get() = clazz.interfaces.map { classPath[it.asmName]!! }.toList() override val fields get() = clazz.declaredFields.map { MemberDesc(it.name, Type.getDescriptor(it.type)) }.toList() diff --git a/deob/src/main/java/dev/openrs2/deob/remap/TypedRemapper.kt b/deob/src/main/java/dev/openrs2/deob/remap/TypedRemapper.kt index 19f6ec14..5e4e1025 100644 --- a/deob/src/main/java/dev/openrs2/deob/remap/TypedRemapper.kt +++ b/deob/src/main/java/dev/openrs2/deob/remap/TypedRemapper.kt @@ -137,7 +137,7 @@ class TypedRemapper private constructor( var skip = false for ((owner, name) in partition) { - val clazz = classPath[owner] + val clazz = classPath[owner]!! if (name in EXCLUDED_FIELDS) { skip = true @@ -187,7 +187,7 @@ class TypedRemapper private constructor( fun isMethodImmutable(classPath: ClassPath, partition: DisjointSet.Partition): Boolean { for (method in partition) { - val clazz = classPath[method.owner] + val clazz = classPath[method.owner]!! if (method.name in EXCLUDED_METHODS) { return true diff --git a/deob/src/main/java/dev/openrs2/deob/transform/OverrideTransformer.kt b/deob/src/main/java/dev/openrs2/deob/transform/OverrideTransformer.kt index 4717da43..8780341e 100644 --- a/deob/src/main/java/dev/openrs2/deob/transform/OverrideTransformer.kt +++ b/deob/src/main/java/dev/openrs2/deob/transform/OverrideTransformer.kt @@ -28,7 +28,7 @@ class OverrideTransformer : Transformer() { return false } - if (!classPath[clazz.name].isOverride(MemberDesc(method))) { + if (!classPath[clazz.name]!!.isOverride(MemberDesc(method))) { return false }