refactor: port class-byte helpers to Java

This commit is contained in:
Algent committed 2026-09-03 16:52:13 +02:00
1 parent 4928cdfe5e
commit 9cf5ab38a8
5 files changed
+135 -52

No files matched your search

+28 -1
View File
@@ -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.
+32 -9
View File
@@ -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'"' '/<testsuite /{t+=$4;f+=$8;e+=$10} END{print "tests="t" failures="f" e
grep -o 'tests="[0-9]*" skipped="[0-9]*" failures="[0-9]*" errors="[0-9]*"' run/server/junit-out/TEST-*.xml
```
Current baseline: **333 plain-JVM tests and 163 Java 8 Forge dedicated-server tests passing.** The ignored local server
Current baseline: **333 plain-JVM tests and 173 Java 8 Forge dedicated-server tests passing.** The ignored local server
EULA is accepted in this checkout. GitHub Actions runs the same self-validating Forge suite in a dependent job after
the shared GTNH build; keep both jobs required.
@@ -826,13 +826,36 @@ All 455 class APIs match except the additive base hook; 3,713 other method bodie
no remaining unsafe external calls/field accesses to the eight transformed Java tile inputs. All 40 generated outputs
retain their instructions: 34 names/hashes match exactly, and six composite numbers move because the new tests create
the client composite earlier. The dev config and forced Scala-compilation guard are unchanged, and all five `@Mod`
versions match both packaged jar versions. The full client rendering check remains open until the fixed jar is tried
in the pack; see the dated note in the manual checklist.
versions match both packaged jar versions. The user has now retested placement with the supplied fix and confirmed
that it no longer crashes. The broader static/dynamic rendering and part-update checks remain open; see the dated
note in the manual checklist.
**Next: `ASMMixinCompiler` class-byte loading/cache helpers: `getBytes`, `classNode` and `internalDefine`.**
Characterize name normalization, cache identity/invalidation, transformer exclusions/remapping, nulls and failure
ordering before extraction. Preserve the existing loader and cache algorithm. Keep reflective class definition,
Java-trait rewriting and composition separate, and retain external Scala-trait support and the model bridges.
`ASMMixinCompiler.getBytes`, `classNode` and `internalDefine` now delegate to Java `ClassBytes`. Ten Forge
characterization tests passed against untouched Scala and were committed first as `4928cdf`; they are unchanged
after extraction. The fixtures temporarily substitute a recording LaunchClassLoader and restore both caches and
environment flags. They exercise the real FML remapper and reflective transformer chain, including original-name
remapping, argument order, prefix short-circuiting, null results and reflective exception wrapping. They also pin
normalized cache keys, raw-array identity, fresh nodes with expanded frames, null caching, load retries, cached parse
failures, metadata invalidation, and publication before a failed debug dump. `internalDefine` still does not define
a JVM class. The existing loader/cache algorithms and initialization order are unchanged.
Reference source/jar, reports, 41 generated dumps and verification scripts are in ignored
`run/migration-class-bytes-reference/`. All 423 non-closure class APIs match, including all 16 named compiler APIs,
private fields and generic signatures. All 3,615 other method bodies and 30 other compiler closures match after
normalizing private closure numbering. All 41 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. The Scala singleton, external Scala-trait support and retained model bridges remain intact.
Formatting/checkstyle/build and Forge pass, including clean verification after stopping Gradle: 333 JVM / 173 Forge,
zero failures/errors/skips. 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. Manual client checks and the
previously documented Java-source model/trait limitations remain outstanding.
**Next: `ASMMixinCompiler.define` reflective class definition.** Characterize publication, debug accounting and
definition/error ordering first, including duplicate definitions and reflection wrapping. Preserve its current
algorithm and leave startup initialization, Java-trait rewriting and composition separate. Retain external
Scala-trait support and the model bridges.
The remaining generated microblock traits still need the Phase 7 abstract-Java-mixin and side-only-member support
before their Java mixin inputs are safe; do not bypass those prerequisites by flattening their inheritance.
+3 -2
View File
@@ -38,8 +38,9 @@ below its table.
`MultipartRenderer$.renderTileEntityAt`: its class call to `TileMultipartClient.hasDynamicParts()` encountered the
transformed runtime interface. The renderer now calls the generated getter through a stable `TileMultipart` base
hook. Four headless Forge cases cover the actual renderer bytecode's guards and dispatch; both nonempty cases
reproduced the original crash. Re-test placement, static/dynamic drawing and part updates in the full client with the
fixed jar before marking the rendering entries complete.
reproduced the original crash. The user retested with the supplied fix and confirmed that placement no longer
crashes. Static/dynamic drawing and part updates still need their full client checks before marking the rendering
entries complete.
## Placement and interaction
@@ -1,20 +1,17 @@
package codechicken.multipart.asm
import scala.collection.mutable.{Map => 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)
@@ -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<String> exclusions = (Set<String>) compiler.f_transformerExceptions().get(compiler.cl());
return JavaConversions.asScalaSet(exclusions).find(new AbstractFunction1<String, Object>() {
@Override
public Object apply(String prefix) {
return name.startsWith(prefix);
}
}).isEmpty();
}
static void internalDefine(Map<String, byte[]> 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<String, byte[]> cache, String name) {
final String normalized = ASMImplicits.nodeName(name);
byte[] bytes = cache.getOrElseUpdate(normalized, new AbstractFunction0<byte[]>() {
@Override
public byte[] apply() {
return ASMMixinCompiler$.MODULE$.getBytes(normalized);
}
});
return bytes == null ? null : ASMHelper.createClassNode(bytes, ClassReader.EXPAND_FRAMES);
}
}