Simplify ClassPath::get with computeIfAbsent

Unfortunately we can't do this in populateInherited{Field,Method} sets,
as they are recursive.
master
Graham 5 years ago
parent d2bd8c69f4
commit bfdfc2f211
  1. 19
      asm/src/main/java/dev/openrs2/asm/classpath/ClassPath.kt

@ -26,27 +26,18 @@ class ClassPath(
return classes return classes
} }
operator fun get(name: String): ClassMetadata { operator fun get(name: String): ClassMetadata = cache.computeIfAbsent(name) {
var metadata = cache[name]
if (metadata != null) {
return metadata
}
for (library in libraries) { for (library in libraries) {
val clazz = library[name] val clazz = library[name]
if (clazz != null) { if (clazz != null) {
metadata = AsmClassMetadata(this, clazz, false) return@computeIfAbsent AsmClassMetadata(this, clazz, false)
cache[name] = metadata
return metadata
} }
} }
for (library in dependencies) { for (library in dependencies) {
val clazz = library[name] val clazz = library[name]
if (clazz != null) { if (clazz != null) {
metadata = AsmClassMetadata(this, clazz, true) return@computeIfAbsent AsmClassMetadata(this, clazz, true)
cache[name] = metadata
return metadata
} }
} }
@ -58,9 +49,7 @@ class ClassPath(
throw IllegalArgumentException("Unknown class $name") throw IllegalArgumentException("Unknown class $name")
} }
metadata = ReflectionClassMetadata(this, clazz) return@computeIfAbsent ReflectionClassMetadata(this, clazz)
cache[name] = metadata
return metadata
} }
fun getNode(name: String): ClassNode? { fun getNode(name: String): ClassNode? {

Loading…
Cancel
Save