From 9cf5ab38a8ba844f198da4002e201f6f96e80a30 Mon Sep 17 00:00:00 2001 From: Algent Date: Thu, 3 Sep 2026 16:52:13 +0200 Subject: [PATCH] refactor: port class-byte helpers to Java --- JAVA_MIGRATION.md | 29 +++++++- JAVA_MIGRATION_HANDOFF.md | 41 +++++++++--- JAVA_MIGRATION_MANUAL_CHECKS.md | 5 +- .../multipart/asm/ASMMixinCompiler.scala | 45 ++----------- .../codechicken/multipart/asm/ClassBytes.java | 67 +++++++++++++++++++ 5 files changed, 135 insertions(+), 52 deletions(-) create mode 100644 src/main/scala/codechicken/multipart/asm/ClassBytes.java diff --git a/JAVA_MIGRATION.md b/JAVA_MIGRATION.md index 4a1a30f..94c014b 100644 --- a/JAVA_MIGRATION.md +++ b/JAVA_MIGRATION.md @@ -1571,4 +1571,31 @@ generated-trait compatibility work. Scala-runtime removal remains a later projec No unsafe external calls/field accesses to the eight transformed Java tile inputs remain. All 40 generated outputs keep their instructions: 34 names/hashes are exact; six composites are renumbered by the new tests' earlier client tile construction. The dev config and forced Scala-compilation guard remain unchanged; all five `@Mod` versions - match both packaged jars. Full-client placement/rendering with the fixed jar still needs confirmation. + match both packaged jars. The user subsequently confirmed that placement with the supplied fix no longer crashes; + the broader static/dynamic drawing and part-update checklist remains open. +- Added ten Forge characterization tests for `ASMMixinCompiler.getBytes`, `classNode` and `internalDefine`, passing + against untouched Scala and committed separately as `4928cdf`. A recording LaunchClassLoader exercises the real + reflective transformer chain; the real FML remapper is tested with a temporary mapping/environment flag. Fixtures + restore loader, caches and flags. They pin dotted/slash names, remapper input and transformer argument order, + exclusion short-circuiting, raw-array identity, nulls, reflection wrapping, expanded frames, fresh node parsing, + cached parse failures versus retried load failures, normalized metadata invalidation and publication before dump + failure. `internalDefine` still stores bytes without defining a JVM class. Baseline: 333 JVM / 173 Forge. +- Extracted those three helpers into Java `ClassBytes`, retaining the Scala singleton, exact entry points and private + cache fields. Scala `find`/`getOrElseUpdate` dispatch remains, as do the load-before-exclusion and + publish/invalidate-before-dump ordering. The helper resolves the retained `MODULE$` internally: joint compilation + cannot expose the Scala-authored `ASMMixinCompiler$` as a Java parameter to a Scala caller. This follows the earlier + metadata helper pattern and changes no consumer API. Reflective class definition, startup initialization, trait + registration/rewriting, composition and compiler algorithms are unchanged. +- Saved pre-port source/jar, reports, 41 generated dumps and reproducible checks in ignored + `run/migration-class-bytes-reference/`. All 423 non-closure class APIs match by names/descriptors/modifiers/generic + signatures and private fields, including all 16 named compiler APIs. All 3,615 other method bodies and 30 other + compiler closures match after normalizing private closure numbering. All 41 generated dump names/hashes match + exactly. Two private Scala closures become two Java callbacks plus their helper, taking the packaged inventory + from 455 to 456 classes. The shared compiler ledger entry covers this, with no new effective divergence. Sources + total 211 Java files and 9 Scala files / 1,597 nonblank Scala lines. +- Formatting/checkstyle/build and Forge pass, including a clean build after stopping Gradle: 333 JVM / 173 Forge, + zero failures/errors/skips. All ten characterization tests remain unchanged. Clean APIs and dumps repeat the + matches above; the dev config and forced Scala-compilation guard are unchanged. All five `@Mod` versions match + both packaged jar versions. External Scala-trait tests remain green; existing manual client checks and Java-source + model/trait limitations remain. Next: `ASMMixinCompiler.define`, characterized first for publication/debug + accounting, reflective definition and failure ordering, without algorithm changes. diff --git a/JAVA_MIGRATION_HANDOFF.md b/JAVA_MIGRATION_HANDOFF.md index 3d85bd5..d23a8d7 100644 --- a/JAVA_MIGRATION_HANDOFF.md +++ b/JAVA_MIGRATION_HANDOFF.md @@ -12,8 +12,8 @@ what breaks, and what is left. The other documents hold the reasoning. | `JAVA_MIGRATION_MANUAL_CHECKS.md` | What no automated test can cover, and must be checked by hand in a client | | `JAVA_MIGRATION_PROFILE.md` | The focused baseline, first measured result, findings, and exact rerun command | -Branch: `algent/java`. Base: `master`. 187 commits including the API-cleanup plan, divergence-log cleanup, ports -through Scala-trait registration metadata, the ASM characterization backfill, and review/client-rendering fixes. +Branch: `algent/java`. Base: `master`. 189 commits including the API-cleanup plan, divergence-log cleanup, ports +through class-byte loading/cache helpers, the ASM characterization backfill, and review/client-rendering fixes. The multipart review corrections (`5af333c`) are on `algent/java`; subsequent migration work continues in that branch. @@ -76,7 +76,7 @@ awk -F'"' '/ MMap, ListBuffer => MList, Set => MSet} -import java.util.{Set => JSet} import scala.collection.JavaConversions._ import org.objectweb.asm.tree._ import org.objectweb.asm.Opcodes._ -import org.objectweb.asm.ClassReader import org.objectweb.asm.Type import org.objectweb.asm.MethodVisitor import Type._ import codechicken.lib.asm.ASMHelper._ -import codechicken.lib.asm.{InsnListSection, InsnComparator, ObfMapping} +import codechicken.lib.asm.{InsnListSection, InsnComparator} import java.lang.reflect.Method import java.lang.reflect.Modifier import net.minecraft.launchwrapper.LaunchClassLoader -import cpw.mods.fml.common.asm.transformers.deobf.FMLDeobfuscatingRemapper import ASMImplicits._ object ASMMixinCompiler { @@ -59,44 +56,12 @@ object ASMMixinCompiler { getBytes("cpw/mods/fml/common/asm/FMLSanityChecker") - def getBytes(name: String): Array[Byte] = { - val jName = name.replace('/', '.') - if (jName == "java.lang.Object") - return null + def getBytes(name: String): Array[Byte] = ClassBytes.getBytes(name) - def useTransformers = f_transformerExceptions - .get(cl) - .asInstanceOf[JSet[String]] - .find(jName.startsWith) - .isEmpty + def internalDefine(name$ : String, bytes: Array[Byte]) = + ClassBytes.internalDefine(traitByteMap, name$, bytes) - val obfName = - if (ObfMapping.obfuscated) - FMLDeobfuscatingRemapper.INSTANCE.unmap(name).replace('/', '.') - else jName - val bytes = cl.getClassBytes(obfName) - if (bytes != null && useTransformers) - return m_runTransformers - .invoke(cl, jName, obfName, bytes) - .asInstanceOf[Array[Byte]] - - return bytes - } - - def internalDefine(name$ : String, bytes: Array[Byte]) { - val name = nodeName(name$) - traitByteMap.put(name, bytes) - remClassInfo(name) - DebugPrinter$.MODULE$.dump(name, bytes) - } - - def classNode(name$ : String) = { - val name = nodeName(name$) - traitByteMap.getOrElseUpdate(name, getBytes(name)) match { - case null => null - case v => createClassNode(v, ClassReader.EXPAND_FRAMES) - } - } + def classNode(name$ : String) = ClassBytes.classNode(traitByteMap, name$) def getMixinInfo(name: String) = mixinMap.get(name) diff --git a/src/main/scala/codechicken/multipart/asm/ClassBytes.java b/src/main/scala/codechicken/multipart/asm/ClassBytes.java new file mode 100644 index 0000000..4795487 --- /dev/null +++ b/src/main/scala/codechicken/multipart/asm/ClassBytes.java @@ -0,0 +1,67 @@ +package codechicken.multipart.asm; + +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.util.Set; + +import org.objectweb.asm.ClassReader; +import org.objectweb.asm.tree.ClassNode; + +import codechicken.lib.asm.ASMHelper; +import codechicken.lib.asm.ObfMapping; +import cpw.mods.fml.common.asm.transformers.deobf.FMLDeobfuscatingRemapper; +import scala.collection.JavaConversions; +import scala.collection.mutable.Map; +import scala.runtime.AbstractFunction0; +import scala.runtime.AbstractFunction1; + +/** Class-byte loading and caching behind the retained compiler singleton and Scala entry points. */ +final class ClassBytes { + + private ClassBytes() {} + + static byte[] getBytes(String name) throws IOException, IllegalAccessException, InvocationTargetException { + ASMMixinCompiler$ compiler = ASMMixinCompiler$.MODULE$; + final String javaName = name.replace('/', '.'); + if (javaName.equals("java.lang.Object")) return null; + String obfuscatedName = ObfMapping.obfuscated ? FMLDeobfuscatingRemapper.INSTANCE.unmap(name).replace('/', '.') + : javaName; + byte[] bytes = compiler.cl().getClassBytes(obfuscatedName); + if (bytes != null && useTransformers(compiler, javaName)) { + return (byte[]) compiler.m_runTransformers().invoke(compiler.cl(), javaName, obfuscatedName, bytes); + } + return bytes; + } + + @SuppressWarnings("unchecked") + private static boolean useTransformers(ASMMixinCompiler$ compiler, final String name) + throws IllegalAccessException { + Set exclusions = (Set) compiler.f_transformerExceptions().get(compiler.cl()); + return JavaConversions.asScalaSet(exclusions).find(new AbstractFunction1() { + + @Override + public Object apply(String prefix) { + return name.startsWith(prefix); + } + }).isEmpty(); + } + + static void internalDefine(Map cache, String name, byte[] bytes) { + String normalized = ASMImplicits.nodeName(name); + cache.put(normalized, bytes); + ASMMixinCompiler$.MODULE$.remClassInfo(normalized); + DebugPrinter$.MODULE$.dump(normalized, bytes); + } + + static ClassNode classNode(Map cache, String name) { + final String normalized = ASMImplicits.nodeName(name); + byte[] bytes = cache.getOrElseUpdate(normalized, new AbstractFunction0() { + + @Override + public byte[] apply() { + return ASMMixinCompiler$.MODULE$.getBytes(normalized); + } + }); + return bytes == null ? null : ASMHelper.createClassNode(bytes, ClassReader.EXPAND_FRAMES); + } +}