From 862d4a35047486b92e62c9d248644b767f713383 Mon Sep 17 00:00:00 2001 From: Graham Date: Mon, 23 Dec 2019 17:35:04 +0000 Subject: [PATCH] Convert ReflectionClassMetadata to Kotlin --- .../classpath/ReflectionClassMetadata.java | 73 ------------------- .../asm/classpath/ReflectionClassMetadata.kt | 37 ++++++++++ 2 files changed, 37 insertions(+), 73 deletions(-) delete mode 100644 asm/src/main/java/dev/openrs2/asm/classpath/ReflectionClassMetadata.java create mode 100644 asm/src/main/java/dev/openrs2/asm/classpath/ReflectionClassMetadata.kt diff --git a/asm/src/main/java/dev/openrs2/asm/classpath/ReflectionClassMetadata.java b/asm/src/main/java/dev/openrs2/asm/classpath/ReflectionClassMetadata.java deleted file mode 100644 index ab753b03..00000000 --- a/asm/src/main/java/dev/openrs2/asm/classpath/ReflectionClassMetadata.java +++ /dev/null @@ -1,73 +0,0 @@ -package dev.openrs2.asm.classpath; - -import java.lang.reflect.Modifier; -import java.util.Arrays; - -import com.google.common.collect.ImmutableList; -import dev.openrs2.asm.MemberDesc; -import org.objectweb.asm.Type; - -public final class ReflectionClassMetadata extends ClassMetadata { - private final ClassPath classPath; - private final Class clazz; - - public ReflectionClassMetadata(ClassPath classPath, Class clazz) { - this.classPath = classPath; - this.clazz = clazz; - } - - @Override - public String getName() { - return clazz.getName().replace('.', '/'); - } - - @Override - public boolean getDependency() { - return true; - } - - @Override - public boolean getInterface() { - return clazz.isInterface(); - } - - @Override - public ClassMetadata getSuperClass() { - var superClass = clazz.getSuperclass(); - if (superClass != null) { - return classPath.get(superClass.getName().replace('.', '/')); - } - return null; - } - - @Override - public ImmutableList getSuperInterfaces() { - return Arrays.stream(clazz.getInterfaces()) - .map(i -> classPath.get(i.getName().replace('.', '/'))) - .collect(ImmutableList.toImmutableList()); - } - - @Override - public ImmutableList getFields() { - return Arrays.stream(clazz.getDeclaredFields()) - .map(f -> new MemberDesc(f.getName(), Type.getDescriptor(f.getType()))) - .collect(ImmutableList.toImmutableList()); - } - - @Override - public ImmutableList getMethods() { - return Arrays.stream(clazz.getDeclaredMethods()) - .map(m -> new MemberDesc(m.getName(), Type.getMethodDescriptor(m))) - .collect(ImmutableList.toImmutableList()); - } - - @Override - public boolean isNative(MemberDesc method) { - for (var m : clazz.getDeclaredMethods()) { - if (m.getName().equals(method.getName()) && Type.getMethodDescriptor(m).equals(method.getDesc())) { - return Modifier.isNative(m.getModifiers()); - } - } - return false; - } -} diff --git a/asm/src/main/java/dev/openrs2/asm/classpath/ReflectionClassMetadata.kt b/asm/src/main/java/dev/openrs2/asm/classpath/ReflectionClassMetadata.kt new file mode 100644 index 00000000..a8884ea1 --- /dev/null +++ b/asm/src/main/java/dev/openrs2/asm/classpath/ReflectionClassMetadata.kt @@ -0,0 +1,37 @@ +package dev.openrs2.asm.classpath + +import dev.openrs2.asm.MemberDesc +import org.objectweb.asm.Type +import java.lang.reflect.Modifier + +private val Class<*>.asmName: String + get() = name.replace('.', '/') + +class ReflectionClassMetadata(private val classPath: ClassPath, private val clazz: Class<*>) : ClassMetadata() { + override val name: String + get() = clazz.asmName + + override val dependency: Boolean + get() = true + + override val `interface`: Boolean + get() = clazz.isInterface + + override val superClass: ClassMetadata? + get() = if (clazz.superclass != null) classPath[clazz.superclass.asmName] else null + + override val superInterfaces + get() = clazz.interfaces.map { classPath[it.asmName] }.toList() + + override val fields + get() = clazz.declaredFields.map { MemberDesc(it.name, Type.getDescriptor(it.type)) }.toList() + + override val methods + get() = clazz.declaredMethods.map { MemberDesc(it.name, Type.getMethodDescriptor(it)) }.toList() + + override fun isNative(method: MemberDesc): Boolean { + val m = clazz.declaredMethods.find { it.name == method.name && Type.getMethodDescriptor(it) == method.desc } + ?: return false + return Modifier.isNative(m.modifiers) + } +}