From d2bd8c69f490c8d63401a0b7cad9f775e7a43f88 Mon Sep 17 00:00:00 2001 From: Graham Date: Mon, 23 Dec 2019 18:25:05 +0000 Subject: [PATCH] Convert ClassPath to Kotlin --- .../dev/openrs2/asm/classpath/ClassPath.java | 207 ------------------ .../dev/openrs2/asm/classpath/ClassPath.kt | 194 ++++++++++++++++ 2 files changed, 194 insertions(+), 207 deletions(-) delete mode 100644 asm/src/main/java/dev/openrs2/asm/classpath/ClassPath.java create mode 100644 asm/src/main/java/dev/openrs2/asm/classpath/ClassPath.kt diff --git a/asm/src/main/java/dev/openrs2/asm/classpath/ClassPath.java b/asm/src/main/java/dev/openrs2/asm/classpath/ClassPath.java deleted file mode 100644 index ffe2180f..00000000 --- a/asm/src/main/java/dev/openrs2/asm/classpath/ClassPath.java +++ /dev/null @@ -1,207 +0,0 @@ -package dev.openrs2.asm.classpath; - -import java.util.HashMap; -import java.util.Map; - -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableSet; -import dev.openrs2.asm.MemberDesc; -import dev.openrs2.asm.MemberRef; -import dev.openrs2.util.collect.DisjointSet; -import dev.openrs2.util.collect.ForestDisjointSet; -import org.objectweb.asm.commons.Remapper; -import org.objectweb.asm.tree.ClassNode; - -public final class ClassPath { - private final ClassLoader runtime; - private final ImmutableList dependencies, libraries; - private final Map cache = new HashMap<>(); - - public ClassPath(ClassLoader runtime, ImmutableList dependencies, ImmutableList libraries) { - this.runtime = runtime; - this.dependencies = dependencies; - this.libraries = libraries; - } - - public ImmutableList getLibraries() { - return libraries; - } - - public ImmutableList getLibraryClasses() { - var classes = ImmutableList.builder(); - - for (var library : libraries) { - for (var clazz : library) { - classes.add(get(clazz.name)); - } - } - - return classes.build(); - } - - public ClassMetadata get(String name) { - var metadata = cache.get(name); - if (metadata != null) { - return metadata; - } - - for (var library : libraries) { - var clazz = library.get(name); - if (clazz != null) { - metadata = new AsmClassMetadata(this, clazz, false); - cache.put(name, metadata); - return metadata; - } - } - - for (var library : dependencies) { - var clazz = library.get(name); - if (clazz != null) { - metadata = new AsmClassMetadata(this, clazz, true); - cache.put(name, metadata); - return metadata; - } - } - - var reflectionName = name.replace('/', '.'); - - Class clazz; - try { - clazz = runtime.loadClass(reflectionName); - } catch (ClassNotFoundException ex) { - throw new IllegalArgumentException("Unknown class " + name); - } - - metadata = new ReflectionClassMetadata(this, clazz); - cache.put(name, metadata); - return metadata; - } - - public ClassNode getNode(String name) { - for (var library : libraries) { - var clazz = library.get(name); - if (clazz != null) { - return clazz; - } - } - return null; - } - - public void remap(Remapper remapper) { - for (var library : libraries) { - library.remap(remapper); - } - - cache.clear(); - } - - public DisjointSet createInheritedFieldSets() { - var disjointSet = new ForestDisjointSet(); - var ancestorCache = new HashMap>(); - - for (var library : libraries) { - for (var clazz : library) { - populateInheritedFieldSets(ancestorCache, disjointSet, get(clazz.name)); - } - } - - return disjointSet; - } - - private ImmutableSet populateInheritedFieldSets(Map> ancestorCache, DisjointSet disjointSet, ClassMetadata clazz) { - var ancestors = ancestorCache.get(clazz); - if (ancestors != null) { - return ancestors; - } - var ancestorsBuilder = ImmutableSet.builder(); - - var superClass = clazz.getSuperClass(); - if (superClass != null) { - var fields = populateInheritedFieldSets(ancestorCache, disjointSet, superClass); - - for (var field : fields) { - var partition1 = disjointSet.add(new MemberRef(clazz.getName(), field)); - var partition2 = disjointSet.add(new MemberRef(superClass.getName(), field)); - disjointSet.union(partition1, partition2); - } - - ancestorsBuilder.addAll(fields); - } - - for (var superInterface : clazz.getSuperInterfaces()) { - var fields = populateInheritedFieldSets(ancestorCache, disjointSet, superInterface); - - for (var field : fields) { - var partition1 = disjointSet.add(new MemberRef(clazz.getName(), field)); - var partition2 = disjointSet.add(new MemberRef(superInterface.getName(), field)); - disjointSet.union(partition1, partition2); - } - - ancestorsBuilder.addAll(fields); - } - - for (var field : clazz.getFields()) { - disjointSet.add(new MemberRef(clazz.getName(), field)); - ancestorsBuilder.add(field); - } - - ancestors = ancestorsBuilder.build(); - ancestorCache.put(clazz, ancestors); - return ancestors; - } - - public DisjointSet createInheritedMethodSets() { - var disjointSet = new ForestDisjointSet(); - var ancestorCache = new HashMap>(); - - for (var library : libraries) { - for (var clazz : library) { - populateInheritedMethodSets(ancestorCache, disjointSet, get(clazz.name)); - } - } - - return disjointSet; - } - - private ImmutableSet populateInheritedMethodSets(Map> ancestorCache, DisjointSet disjointSet, ClassMetadata clazz) { - var ancestors = ancestorCache.get(clazz); - if (ancestors != null) { - return ancestors; - } - var ancestorsBuilder = ImmutableSet.builder(); - - var superClass = clazz.getSuperClass(); - if (superClass != null) { - var methods = populateInheritedMethodSets(ancestorCache, disjointSet, superClass); - - for (var method : methods) { - var partition1 = disjointSet.add(new MemberRef(clazz.getName(), method)); - var partition2 = disjointSet.add(new MemberRef(superClass.getName(), method)); - disjointSet.union(partition1, partition2); - } - - ancestorsBuilder.addAll(methods); - } - - for (var superInterface : clazz.getSuperInterfaces()) { - var methods = populateInheritedMethodSets(ancestorCache, disjointSet, superInterface); - - for (var method : methods) { - var partition1 = disjointSet.add(new MemberRef(clazz.getName(), method)); - var partition2 = disjointSet.add(new MemberRef(superInterface.getName(), method)); - disjointSet.union(partition1, partition2); - } - - ancestorsBuilder.addAll(methods); - } - - for (var method : clazz.getMethods()) { - disjointSet.add(new MemberRef(clazz.getName(), method)); - ancestorsBuilder.add(method); - } - - ancestors = ancestorsBuilder.build(); - ancestorCache.put(clazz, ancestors); - return ancestors; - } -} diff --git a/asm/src/main/java/dev/openrs2/asm/classpath/ClassPath.kt b/asm/src/main/java/dev/openrs2/asm/classpath/ClassPath.kt new file mode 100644 index 00000000..74b2446e --- /dev/null +++ b/asm/src/main/java/dev/openrs2/asm/classpath/ClassPath.kt @@ -0,0 +1,194 @@ +package dev.openrs2.asm.classpath + +import com.google.common.collect.ImmutableList +import dev.openrs2.asm.MemberDesc +import dev.openrs2.asm.MemberRef +import dev.openrs2.util.collect.DisjointSet +import dev.openrs2.util.collect.ForestDisjointSet +import org.objectweb.asm.commons.Remapper +import org.objectweb.asm.tree.ClassNode + +class ClassPath( + private val runtime: ClassLoader, + private val dependencies: ImmutableList, + val libraries: List +) { + private val cache = mutableMapOf() + + val libraryClasses: List + get() { + val classes = mutableListOf() + for (library in libraries) { + for (clazz in library) { + classes.add(get(clazz.name)) + } + } + return classes + } + + operator fun get(name: String): ClassMetadata { + var metadata = cache[name] + if (metadata != null) { + return metadata + } + + for (library in libraries) { + val clazz = library[name] + if (clazz != null) { + metadata = AsmClassMetadata(this, clazz, false) + cache[name] = metadata + return metadata + } + } + + for (library in dependencies) { + val clazz = library[name] + if (clazz != null) { + metadata = AsmClassMetadata(this, clazz, true) + cache[name] = metadata + return metadata + } + } + + val reflectionName = name.replace('/', '.') + + val clazz = try { + runtime.loadClass(reflectionName) + } catch (ex: ClassNotFoundException) { + throw IllegalArgumentException("Unknown class $name") + } + + metadata = ReflectionClassMetadata(this, clazz) + cache[name] = metadata + return metadata + } + + fun getNode(name: String): ClassNode? { + for (library in libraries) { + val clazz = library[name] + if (clazz != null) { + return clazz + } + } + + return null + } + + fun remap(remapper: Remapper) { + for (library in libraries) { + library.remap(remapper) + } + + cache.clear() + } + + fun createInheritedFieldSets(): DisjointSet { + val disjointSet = ForestDisjointSet() + val ancestorCache = mutableMapOf>() + + for (library in libraries) { + for (clazz in library) { + populateInheritedFieldSets(ancestorCache, disjointSet, get(clazz.name)) + } + } + + return disjointSet + } + + private fun populateInheritedFieldSets( + ancestorCache: MutableMap>, + disjointSet: DisjointSet, + clazz: ClassMetadata + ): Set { + val ancestors = ancestorCache[clazz] + if (ancestors != null) { + return ancestors + } + + val ancestorsBuilder = mutableSetOf() + + val superClass = clazz.superClass + if (superClass != null) { + val fields = populateInheritedFieldSets(ancestorCache, disjointSet, superClass) + for (field in fields) { + val partition1 = disjointSet.add(MemberRef(clazz.name, field)) + val partition2 = disjointSet.add(MemberRef(superClass.name, field)) + disjointSet.union(partition1, partition2) + } + ancestorsBuilder.addAll(fields) + } + + for (superInterface in clazz.superInterfaces) { + val fields = populateInheritedFieldSets(ancestorCache, disjointSet, superInterface) + for (field in fields) { + val partition1 = disjointSet.add(MemberRef(clazz.name, field)) + val partition2 = disjointSet.add(MemberRef(superInterface.name, field)) + disjointSet.union(partition1, partition2) + } + ancestorsBuilder.addAll(fields) + } + + for (field in clazz.fields) { + disjointSet.add(MemberRef(clazz.name, field)) + ancestorsBuilder.add(field) + } + + ancestorCache[clazz] = ancestorsBuilder + return ancestorsBuilder + } + + fun createInheritedMethodSets(): DisjointSet { + val disjointSet = ForestDisjointSet() + val ancestorCache = mutableMapOf>() + + for (library in libraries) { + for (clazz in library) { + populateInheritedMethodSets(ancestorCache, disjointSet, get(clazz.name)) + } + } + + return disjointSet + } + + private fun populateInheritedMethodSets( + ancestorCache: MutableMap>, + disjointSet: DisjointSet, + clazz: ClassMetadata + ): Set { + val ancestors = ancestorCache[clazz] + if (ancestors != null) { + return ancestors + } + + val ancestorsBuilder = mutableSetOf() + + val superClass = clazz.superClass + if (superClass != null) { + val methods = populateInheritedMethodSets(ancestorCache, disjointSet, superClass) + for (method in methods) { + val partition1 = disjointSet.add(MemberRef(clazz.name, method)) + val partition2 = disjointSet.add(MemberRef(superClass.name, method)) + disjointSet.union(partition1, partition2) + } + ancestorsBuilder.addAll(methods) + } + + for (superInterface in clazz.superInterfaces) { + val methods = populateInheritedMethodSets(ancestorCache, disjointSet, superInterface) + for (method in methods) { + val partition1 = disjointSet.add(MemberRef(clazz.name, method)) + val partition2 = disjointSet.add(MemberRef(superInterface.name, method)) + disjointSet.union(partition1, partition2) + } + ancestorsBuilder.addAll(methods) + } + + for (method in clazz.methods) { + disjointSet.add(MemberRef(clazz.name, method)) + ancestorsBuilder.add(method) + } + + ancestorCache[clazz] = ancestorsBuilder + return ancestorsBuilder + } +}