From 1faf0ddca231572eeff4ab115d7f82c00f67c694 Mon Sep 17 00:00:00 2001 From: Algent Date: Wed, 2 Sep 2026 17:37:54 +0200 Subject: [PATCH] refactor: port ASM implicits to Java --- JAVA_MIGRATION.md | 15 ++ JAVA_MIGRATION_DIVERGENCES.md | 4 + JAVA_MIGRATION_HANDOFF.md | 23 ++-- .../multipart/asm/ASMImplicits$.java | 22 +++ .../multipart/asm/ASMImplicits.java | 128 ++++++++++++++++++ .../multipart/asm/ASMImplicits.scala | 22 --- .../multipart/asm/ASMMixinCompiler.scala | 6 +- .../multipart/asm/ASMMixinFactory.scala | 10 +- 8 files changed, 193 insertions(+), 37 deletions(-) create mode 100644 src/main/scala/codechicken/multipart/asm/ASMImplicits$.java create mode 100644 src/main/scala/codechicken/multipart/asm/ASMImplicits.java delete mode 100644 src/main/scala/codechicken/multipart/asm/ASMImplicits.scala diff --git a/JAVA_MIGRATION.md b/JAVA_MIGRATION.md index d70085b..f146e7f 100644 --- a/JAVA_MIGRATION.md +++ b/JAVA_MIGRATION.md @@ -1248,3 +1248,18 @@ generated-trait compatibility work. Scala-runtime removal remains a later projec six methods plus randomized lengths through 256, null and invalid decode lengths: all 450,790 cases matched on return values, exception types and full mutated arrays. No new difference needs a separate ledger entry. `multipart/asm/ASMImplicits.scala` is next; compiler behavior changes remain separate work. +- Characterized `ASMImplicits` against untouched Scala in five plain-JVM cases: all six runtime surfaces, identity + conversions, node names, null handling, BitSet self-replacement and clear-before-failure, independent plain-BitSet + copies, and boxed equality/hash behavior. Neither consumer audit identifies a direct external user. +- A follow-up characterization, also passed against the saved Scala reference jar, pins virtual `BitSet.equals` + dispatch even for identical references. The Java implementation uses an explicit null check here because + `Objects.equals` would skip that call. +- Ported the facade, companion and four nested value-class/extension types to two Java sources, retaining every + callable public name, descriptor and modifier. Seven Scala compiler/factory call sites now explicitly invoke the + helpers; compiler algorithms and trait registration are unchanged. The ledger records the lost implicit syntax and + the Java publication rule: explicitly constructing either public extension companion no longer replaces `MODULE$`, + verified against the reference in isolated class loaders. +- Clean formatting/checkstyle/build passes all 276 plain-JVM tests, and Java 8 Forge passes all 116 tests. All six + helpers have Java source markers and Java 8 class versions; compiler/factory public descriptors and the full + 464-class packaged inventory match the reference. `MultipartGenerator$` disassembly is unchanged. The next bounded + target is extracting `DebugPrinter` from `ASMMixinCompiler.scala`, leaving the compiler algorithms for later. diff --git a/JAVA_MIGRATION_DIVERGENCES.md b/JAVA_MIGRATION_DIVERGENCES.md index 551e773..3d578f9 100644 --- a/JAVA_MIGRATION_DIVERGENCES.md +++ b/JAVA_MIGRATION_DIVERGENCES.md @@ -18,6 +18,7 @@ and `JAVA_MIGRATION_CONSUMER_AUDIT.md` before extending this list. | `NormalOcclusionTest.apply(Traversable, Traversable)` | Materializes both inputs into Java lists before testing instead of nested Scala `forall`. Finite, side-effect-free collections give the same result; eager traversal can change side effects, failure timing and termination for unusual inputs. | | `MultipartRenderer` dynamic-part guard | Uses short-circuit `\|\|` instead of `\|`; the second getter is no longer evaluated when the first operand succeeds. Ordinary getters give the same render decision. | | `ControlKeyModifer.map()` | The exposed Java map has no Scala default-value wrapper: an absent key returns null from `get`, not false from Scala `apply`. Use `isControlDown` for the unchanged absent-means-false behavior. | +| Public companion constructors, including `ASMImplicits.ExtBitSet$` / `ExtClass$` | Explicit construction creates an independent instance instead of replacing `MODULE$`. Java initializes the retained final module field in its static initializer; use `MODULE$` for singleton identity. | Concrete collection implementations also change without changing the declared `java.lang.Iterable`/`List` contracts: @@ -125,6 +126,9 @@ Previously compiled calls to **retained** `$class` bridges still link. This does recipes, packet handlers, materials and generators). Static calls alone do not supply a singleton value. - Object-application sugar becomes explicit `.apply(...)`, e.g. `NormalOcclusionTest.apply(...)` and `MicroblockPlacement$.MODULE$.apply(...)`. +- `ASMImplicits` no longer provides Scala implicit/value-class syntax. Use `nodeName(clazz.getName)` and + `ExtBitSet$.MODULE$.copy$extension(bits)` explicitly, or construct its retained Java wrappers. Existing boxed and + `$extension` binary entry points remain; implicit conversion requires Scala metadata Java cannot emit. - Scala property assignment needs the explicit Java-authored `_$eq(...)` method, e.g. `renderID`, `loadingWorld`, `angelicaCompat` and microblock `shape`. - `tile.partList(i)` becomes `tile.partList.apply(i)`. Static bounds getters on Face, Corner, Edge and Post factories diff --git a/JAVA_MIGRATION_HANDOFF.md b/JAVA_MIGRATION_HANDOFF.md index 16c7720..492be12 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`. 155 commits including the API-cleanup plan, divergence-log cleanup and separate -characterization and port commits through the byte codec. +Branch: `algent/java`. Base: `master`. 158 commits including the API-cleanup plan, divergence-log cleanup and separate +characterization and port commits through the ASM implicit helpers. ## The one rule that matters @@ -68,7 +68,7 @@ awk -F'"' '/ ExtClass(Class clazz) { + return clazz; + } + + public String nodeName(String name) { + return name == null ? null : name.replace('.', '/'); + } +} diff --git a/src/main/scala/codechicken/multipart/asm/ASMImplicits.java b/src/main/scala/codechicken/multipart/asm/ASMImplicits.java new file mode 100644 index 0000000..0a273d3 --- /dev/null +++ b/src/main/scala/codechicken/multipart/asm/ASMImplicits.java @@ -0,0 +1,128 @@ +package codechicken.multipart.asm; + +import java.util.BitSet; +import java.util.Objects; + +/** Name and BitSet helpers, retaining the Scala value-class entry points for compiled callers. */ +public final class ASMImplicits { + + private ASMImplicits() {} + + public static BitSet ExtBitSet(BitSet bitset) { + return ASMImplicits$.MODULE$.ExtBitSet(bitset); + } + + public static Class ExtClass(Class clazz) { + return ASMImplicits$.MODULE$.ExtClass(clazz); + } + + public static String nodeName(String name) { + return ASMImplicits$.MODULE$.nodeName(name); + } + + public static final class ExtBitSet { + + private final BitSet bitset; + + public ExtBitSet(BitSet bitset) { + this.bitset = bitset; + } + + public BitSet bitset() { + return bitset; + } + + public BitSet set(BitSet other) { + return ExtBitSet$.MODULE$.set$extension(bitset, other); + } + + public BitSet copy() { + return ExtBitSet$.MODULE$.copy$extension(bitset); + } + + @Override + public int hashCode() { + return ExtBitSet$.MODULE$.hashCode$extension(bitset); + } + + @Override + public boolean equals(Object other) { + return ExtBitSet$.MODULE$.equals$extension(bitset, other); + } + } + + public static class ExtBitSet$ { + + public static final ExtBitSet$ MODULE$ = new ExtBitSet$(); + + public ExtBitSet$() {} + + public final BitSet set$extension(BitSet bitset, BitSet other) { + // Preserve clear-before-read: self-replacement empties the set, and a null source fails after clearing. + bitset.clear(); + bitset.or(other); + return bitset; + } + + public final BitSet copy$extension(BitSet bitset) { + return set$extension(new BitSet(), bitset); + } + + public final int hashCode$extension(BitSet bitset) { + return bitset.hashCode(); + } + + public final boolean equals$extension(BitSet bitset, Object other) { + if (!(other instanceof ExtBitSet)) return false; + BitSet otherBits = ((ExtBitSet) other).bitset(); + // Scala still dispatches equals for identical non-null references; Objects.equals would skip it. + return bitset == null ? otherBits == null : bitset.equals(otherBits); + } + } + + public static final class ExtClass { + + private final Class clazz; + + public ExtClass(Class clazz) { + this.clazz = clazz; + } + + public Class clazz() { + return clazz; + } + + public String nodeName() { + return ExtClass$.MODULE$.nodeName$extension(clazz); + } + + @Override + public int hashCode() { + return ExtClass$.MODULE$.hashCode$extension(clazz); + } + + @Override + public boolean equals(Object other) { + return ExtClass$.MODULE$.equals$extension(clazz, other); + } + } + + public static class ExtClass$ { + + public static final ExtClass$ MODULE$ = new ExtClass$(); + + public ExtClass$() {} + + public final String nodeName$extension(Class clazz) { + return ASMImplicits$.MODULE$.nodeName(clazz.getName()); + } + + public final int hashCode$extension(Class clazz) { + return clazz.hashCode(); + } + + public final boolean equals$extension(Class clazz, Object other) { + return other instanceof ExtClass && Objects.equals(clazz, ((ExtClass) other).clazz()); + } + } +} diff --git a/src/main/scala/codechicken/multipart/asm/ASMImplicits.scala b/src/main/scala/codechicken/multipart/asm/ASMImplicits.scala deleted file mode 100644 index 870e150..0000000 --- a/src/main/scala/codechicken/multipart/asm/ASMImplicits.scala +++ /dev/null @@ -1,22 +0,0 @@ -package codechicken.multipart.asm - -import java.util.BitSet - -object ASMImplicits { - implicit class ExtBitSet(val bitset: BitSet) extends AnyVal { - def set(b: BitSet) = { - bitset.clear() - bitset.or(b) - bitset - } - - def copy = new BitSet().set(bitset) - } - - def nodeName(name: String) = - if (name == null) null else name.replace('.', '/') - - implicit class ExtClass(val clazz: Class[_]) extends AnyVal { - def nodeName = ASMImplicits.nodeName(clazz.getName) - } -} diff --git a/src/main/scala/codechicken/multipart/asm/ASMMixinCompiler.scala b/src/main/scala/codechicken/multipart/asm/ASMMixinCompiler.scala index ff1c0c4..039b28f 100644 --- a/src/main/scala/codechicken/multipart/asm/ASMMixinCompiler.scala +++ b/src/main/scala/codechicken/multipart/asm/ASMMixinCompiler.scala @@ -204,7 +204,7 @@ object ASMMixinCompiler { cnode.name ) implicit def getClassInfo(clazz: Class[_]): ClassInfo = - if (clazz == null) null else getClassInfo(clazz.nodeName) + if (clazz == null) null else getClassInfo(nodeName(clazz.getName)) object ClassInfo { class ReflectionClassInfo(clazz: Class[_]) extends ClassInfo { @@ -212,12 +212,12 @@ object ASMMixinCompiler { def owner = ReflectionClassInfo.this def name = method.getName def desc = getType(method).getDescriptor - def exceptions = method.getExceptionTypes.map(_.nodeName) + def exceptions = method.getExceptionTypes.map(c => nodeName(c.getName)) def isPrivate = Modifier.isPrivate(method.getModifiers) def isAbstract = Modifier.isAbstract(method.getModifiers) } - def name = clazz.nodeName + def name = nodeName(clazz.getName) def superClass = Option(clazz.getSuperclass) def interfaces = clazz.getInterfaces.map(getClassInfo) def methods = clazz.getMethods.map(ReflectionMethodInfo(_)) diff --git a/src/main/scala/codechicken/multipart/asm/ASMMixinFactory.scala b/src/main/scala/codechicken/multipart/asm/ASMMixinFactory.scala index 598c83c..eb7a055 100644 --- a/src/main/scala/codechicken/multipart/asm/ASMMixinFactory.scala +++ b/src/main/scala/codechicken/multipart/asm/ASMMixinFactory.scala @@ -31,7 +31,7 @@ class ASMMixinFactory[T]( seq += traits(i) val c = ASMMixinCompiler - .mixinClasses(nextName(), baseType.nodeName, seq.result()) + .mixinClasses(nextName(), nodeName(baseType.getName), seq.result()) .asInstanceOf[Class[_ <: T]] onCompiled(c, traitSet) c.getDeclaredConstructor(paramTypes: _*) @@ -45,7 +45,7 @@ class ASMMixinFactory[T]( case Some(c) => c case None => val c = compile(traitSet) - classMap.put(traitSet.copy, c) + classMap.put(ExtBitSet$.MODULE$.copy$extension(traitSet), c) c }).newInstance(args: _*) } @@ -53,7 +53,7 @@ class ASMMixinFactory[T]( def getId(s_trait: String) = traitMap(s_trait) def registerTrait(traitClass: Class[_]): Int = registerTrait( - traitClass.nodeName + nodeName(traitClass.getName) ) def registerTrait(s_trait: String): Int = { @@ -84,7 +84,9 @@ class ASMMixinFactory[T]( info.name == parentName || info.superClass.exists(checkParent) if (!checkParent(getClassInfo(baseType))) throw new IllegalArgumentException( - baseType.nodeName + " does not extend parent " + parentName + " of mixin trait " + s_trait + nodeName( + baseType.getName + ) + " does not extend parent " + parentName + " of mixin trait " + s_trait ) if (info.isTrait) {