From 17bc1e287bdcf467c25d7f1866bdfe142fb086f0 Mon Sep 17 00:00:00 2001 From: Graham Date: Sat, 2 May 2020 23:09:13 +0100 Subject: [PATCH] Move common jar reading logic to a separate class Signed-off-by: Graham --- .../asm/io/AbstractJarLibraryWriter.kt | 49 +++++++++++++++++++ .../dev/openrs2/asm/io/JarLibraryWriter.kt | 45 +---------------- .../asm/io/ManifestJarLibraryWriter.kt | 2 +- 3 files changed, 52 insertions(+), 44 deletions(-) create mode 100644 asm/src/main/java/dev/openrs2/asm/io/AbstractJarLibraryWriter.kt diff --git a/asm/src/main/java/dev/openrs2/asm/io/AbstractJarLibraryWriter.kt b/asm/src/main/java/dev/openrs2/asm/io/AbstractJarLibraryWriter.kt new file mode 100644 index 00000000..a0eefc6e --- /dev/null +++ b/asm/src/main/java/dev/openrs2/asm/io/AbstractJarLibraryWriter.kt @@ -0,0 +1,49 @@ +package dev.openrs2.asm.io + +import dev.openrs2.asm.ClassVersionUtils +import dev.openrs2.asm.NopClassVisitor +import dev.openrs2.asm.classpath.ClassPath +import dev.openrs2.asm.classpath.Library +import dev.openrs2.asm.classpath.StackFrameClassWriter +import org.objectweb.asm.ClassWriter +import org.objectweb.asm.Opcodes +import org.objectweb.asm.util.CheckClassAdapter +import java.io.OutputStream +import java.util.jar.JarEntry +import java.util.jar.JarOutputStream + +abstract class AbstractJarLibraryWriter : LibraryWriter { + override fun write(output: OutputStream, classPath: ClassPath, library: Library) { + createJarOutputStream(output).use { jar -> + for (clazz in library) { + val writer = if (ClassVersionUtils.gte(clazz.version, Opcodes.V1_7)) { + StackFrameClassWriter(classPath) + } else { + ClassWriter(ClassWriter.COMPUTE_MAXS) + } + + clazz.accept(writer) + + jar.putNextEntry(JarEntry(clazz.name + CLASS_SUFFIX)) + jar.write(writer.toByteArray()) + + /* + * XXX(gpe): CheckClassAdapter breaks the Label offset + * calculation in the OriginalPcTable's write method, so we do + * a second pass without any attributes to check the class, + * feeding the callbacks into a no-op visitor. + */ + for (method in clazz.methods) { + method.attrs?.clear() + } + clazz.accept(CheckClassAdapter(NopClassVisitor, true)) + } + } + } + + protected abstract fun createJarOutputStream(output: OutputStream): JarOutputStream + + private companion object { + private const val CLASS_SUFFIX = ".class" + } +} diff --git a/asm/src/main/java/dev/openrs2/asm/io/JarLibraryWriter.kt b/asm/src/main/java/dev/openrs2/asm/io/JarLibraryWriter.kt index b1f91114..baac3745 100644 --- a/asm/src/main/java/dev/openrs2/asm/io/JarLibraryWriter.kt +++ b/asm/src/main/java/dev/openrs2/asm/io/JarLibraryWriter.kt @@ -1,52 +1,11 @@ package dev.openrs2.asm.io -import dev.openrs2.asm.ClassVersionUtils -import dev.openrs2.asm.NopClassVisitor -import dev.openrs2.asm.classpath.ClassPath -import dev.openrs2.asm.classpath.Library -import dev.openrs2.asm.classpath.StackFrameClassWriter import dev.openrs2.util.io.DeterministicJarOutputStream -import org.objectweb.asm.ClassWriter -import org.objectweb.asm.Opcodes -import org.objectweb.asm.util.CheckClassAdapter import java.io.OutputStream -import java.util.jar.JarEntry import java.util.jar.JarOutputStream -open class JarLibraryWriter : LibraryWriter { - override fun write(output: OutputStream, classPath: ClassPath, library: Library) { - createJarOutputStream(output).use { jar -> - for (clazz in library) { - val writer = if (ClassVersionUtils.gte(clazz.version, Opcodes.V1_7)) { - StackFrameClassWriter(classPath) - } else { - ClassWriter(ClassWriter.COMPUTE_MAXS) - } - - clazz.accept(writer) - - jar.putNextEntry(JarEntry(clazz.name + CLASS_SUFFIX)) - jar.write(writer.toByteArray()) - - /* - * XXX(gpe): CheckClassAdapter breaks the Label offset - * calculation in the OriginalPcTable's write method, so we do - * a second pass without any attributes to check the class, - * feeding the callbacks into a no-op visitor. - */ - for (method in clazz.methods) { - method.attrs?.clear() - } - clazz.accept(CheckClassAdapter(NopClassVisitor, true)) - } - } - } - - protected open fun createJarOutputStream(output: OutputStream): JarOutputStream { +class JarLibraryWriter : AbstractJarLibraryWriter() { + override fun createJarOutputStream(output: OutputStream): JarOutputStream { return DeterministicJarOutputStream(output) } - - private companion object { - private const val CLASS_SUFFIX = ".class" - } } diff --git a/asm/src/main/java/dev/openrs2/asm/io/ManifestJarLibraryWriter.kt b/asm/src/main/java/dev/openrs2/asm/io/ManifestJarLibraryWriter.kt index 98676d1d..1fb04ecf 100644 --- a/asm/src/main/java/dev/openrs2/asm/io/ManifestJarLibraryWriter.kt +++ b/asm/src/main/java/dev/openrs2/asm/io/ManifestJarLibraryWriter.kt @@ -5,7 +5,7 @@ import java.io.OutputStream import java.util.jar.JarOutputStream import java.util.jar.Manifest -class ManifestJarLibraryWriter(private val manifest: Manifest) : JarLibraryWriter() { +class ManifestJarLibraryWriter(private val manifest: Manifest) : AbstractJarLibraryWriter() { override fun createJarOutputStream(output: OutputStream): JarOutputStream { return DeterministicJarOutputStream(output, manifest) }