refactor: port ClassInfo metadata lookup to Java

This commit is contained in:
Algent committed 2026-09-03 10:00:24 +02:00
1 parent abdf0b0559
commit 10d0f173ba
4 files changed
+274 -52

No files matched your search

+32
View File
@@ -1405,3 +1405,35 @@ generated-trait compatibility work. Scala-runtime removal remains a later projec
Java files and 9 Scala files / 1,726 nonblank Scala lines. No new compatibility divergence is introduced. Next is
`ASMMixinCompiler.scala`, bounded to `ClassInfo`/`MethodInfo` metadata lookup and traversal, with fresh characterization
before touching its Forge-initialized state and no trait-rewriting algorithm changes.
- Characterized the compiler metadata unit before modification in `abdf0b0`: six JVM cases cover hierarchy order and
diamond duplicates, parent-view capture/laziness, strict/view concatenation, virtual selection and short-circuiting,
mutable node metadata, reflection order/descriptors/exceptions and case-class outer owners. Five Forge cases cover
bytecode versus reflection roots, exact string-key caching and name-only node overloads, null/failure caching,
`internalDefine` invalidation and Scala trait/companion metadata. Untouched Scala passes 326 JVM / 127 Forge tests.
- Extracted metadata lookup, cache population, parent flattening, method concatenation/selection, interface/method
mapping and exception-name copying into package-private Java `ClassInfoLookup`. The same Scala collection operations
and builders preserve result representations, laziness, traversal order and virtual dispatch. The nested model,
scalar accessors, state, implicit overloads and construction callbacks remain in Scala; no compiler composition,
trait registration/rewriting, stack-analysis or signature-decoding algorithm changed.
- Found and retained two necessary source bridges. javac cannot name the concrete metadata classes nested under
`ClassInfo$`: it expects an extra `$` absent from their existing binary names. Scala's `IterableLike.view()` also
has an `Object` generic return signature but an `IterableView` descriptor; calling it from Java emits a nonexistent
`Object`-returning method. Construction and the initial `.view` call stay Scala. Path-dependent `ClassSymbolRef`
travels through an `Object` parameter and is cast back only at its retained Scala construction boundary.
- Preserved existing quirks exposed by characterization: dotted/slashed keys have independent metadata cache entries,
`internalDefine` invalidates only the normalized key, null names are cached as null while load failures are not,
bytecode roots can return `Some(null)`, and the `StackAnalyser$` companion's superclass/interface queries throw
`ClassCastException` in the current signature model. These are unchanged behavior and do not add divergence entries.
- Saved the pre-port source/jar, reports and 40 generated dumps in ignored `run/migration-class-info-reference/`.
All 16 named compiler APIs match by public/protected member names/descriptors/modifiers/generic declarations and
private fields. All four metadata case-class/companion serialization IDs match. All 205 other named compiler
methods and 36 algorithm closures match after normalizing private closure and captured-variable numbering; 137
ASM/generator classes outside the compiler have identical disassembly. All 40 dump names/hashes match, including
the new deterministic cache-invalidation fixture; the local dev configuration is unchanged.
- Formatting/checkstyle/build and Forge pass, including a clean build after stopping Gradle: 326 JVM / 127 Forge,
zero failures/errors/skips, with the characterization tests unchanged after the port. The forced Scala compilation
guard is retained and all five `@Mod` annotations match both clean packaged jar versions. Private implementation
closures become the helper/callbacks and the packaged inventory falls from 451 to 450. Sources now total 208 Java
files and 9 Scala files / 1,714 nonblank Scala lines. Next is the bounded descriptor/bridge-emission helper unit in
`ASMMixinCompiler.scala`: `seperateDesc`, `staticDesc`, `finishBridgeCall`, `writeBridge` and `writeStaticBridge`, with
fresh characterization and no composition/trait-rewriting algorithm changes.
+38 -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`. 174 commits including the API-cleanup plan, divergence-log cleanup, ports
through the `StackAnalyser` control-flow extraction, and the ASM characterization backfill and review fixes.
Branch: `algent/java`. Base: `master`. 176 commits including the API-cleanup plan, divergence-log cleanup, ports
through the `ClassInfo` metadata lookup extraction, and the ASM characterization backfill and review fixes.
## The one rule that matters
@@ -74,7 +74,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: **320 plain-JVM tests and 122 Java 8 Forge dedicated-server tests passing.** The ignored local server
Current baseline: **326 plain-JVM tests and 127 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.
@@ -280,7 +280,7 @@ across the port. It is also where the two shim constraints in the gotchas list w
`rayTraceAll`'s index production is now characterized, closing the gap the read-path cleanup left: the index written
into `ExtendedMOP.data` is what `reduceMOP` hands back to every click, activate, harvest and pick block.
207 Java files, 9 Scala files, 1,726 Scala lines left (non-blank; that is the metric this figure has always used).
208 Java files, 9 Scala files, 1,714 Scala lines left (non-blank; that is the metric this figure has always used).
## What is left, and in what order
@@ -654,11 +654,40 @@ replaced by the Java helper, taking the packaged inventory from 452 to 451. The
unchanged; both clean jars have all five `@Mod` annotations matching their packaged version. No new divergence entry
is needed.
**Next: `multipart/asm/ASMMixinCompiler.scala`, bounded to `ClassInfo`/`MethodInfo` metadata lookup and traversal.**
Characterize hierarchy order, lazy traversal, public implementation selection and lookup/cache behavior first, using
Forge where the compiler's `LaunchClassLoader` initialization requires it. Keep trait registration/rewriting algorithms,
external Scala-trait support, retained `StackAnalyser` model and `ScalaSignature` primitive/erased bridges unchanged.
Save a fresh reference and compare generated dumps again before expanding the compiler scope.
`ClassInfo` metadata lookup and traversal now use package-private Java `ClassInfoLookup`. Six JVM and five Forge
characterization cases passed against untouched Scala and were committed separately as `abdf0b0`. They freeze own/
superclass/interface order with diamond duplicates, lazy parent-method reads versus captured parent identities,
strict versus view-backed concatenation, overridable method selection and predicate short-circuiting, mutable node
wrappers, reflection order/descriptors/exceptions, case-class outer owners, exact cache keys, null/failure caching,
`internalDefine` invalidation, and external Scala-trait/companion metadata selection. They pass unchanged after the port.
The nested model, fields, scalar accessors, implicit overloads, constructor callbacks and initial `.view` expression
remain Scala. Two concrete Java-source limitations require those bridges: `ClassInfo$`'s nested classes have binary
names missing the additional `$` javac expects, and Scala's `IterableLike.view()` generic signature says `Object`
while its descriptor returns `IterableView`, causing Java source calls to emit a missing `Object`-returning descriptor.
The Java helper keeps path-dependent `ClassSymbolRef` out of its signatures, as the existing signature parser does.
Its traversal calls retain Scala's collection builders and virtual dispatch; it does not make lazy inputs eager.
The existing metadata quirks remain: dotted/slashed strings are separate cache keys, `internalDefine` invalidates
only the normalized key, a bytecode root can report `Some(null)`, and the `StackAnalyser$` companion's superclass/
interface queries throw `ClassCastException` in the current signature model. These are characterized existing
behaviors, not new divergences. No trait registration, rewriting or signature-decoder algorithm changed.
Local evidence is under ignored `run/migration-class-info-reference/`: pre-port source/jar, test reports, generated
dumps, clean logs and comparison scripts. All 16 named compiler APIs retain public/protected members, flags, generic
signatures and private fields; all four metadata case-class/companion serialization IDs match. The 205 other named
compiler methods and 36 algorithm closures have identical disassembly after normalizing private closure/capture
numbering, as do 137 ASM/generator classes outside the compiler. All 40 generated dump names/hashes match (the extra
dump is the new cache-invalidation fixture). Clean build and Forge pass at 326 JVM / 127 Forge, zero skips. The
packaged class count falls from 451 to 450; no new ledger entry is needed. The forced Scala compilation guard remains,
and all five `@Mod` annotations match both clean jar versions.
**Next: `multipart/asm/ASMMixinCompiler.scala`, bounded to descriptor and bridge-emission helpers:** `seperateDesc`
(keep its existing spelling), `staticDesc`, `finishBridgeCall`, `writeBridge`, and `writeStaticBridge`. Characterize
descriptors, argument/local widths, emitted instruction order, return opcodes and max-stack/local values first, with
Forge where singleton initialization requires it. Keep composition/trait-rewriting algorithms, external Scala-trait
support, retained metadata/stack models and `ScalaSignature` primitive/erased bridges unchanged. Save a fresh reference
and compare all generated dumps before expanding scope.
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.
@@ -140,11 +140,12 @@ object ASMMixinCompiler {
override def toString =
getClass.getName.replaceAll(".+[\\$\\.]", "") + "(" + name + ")"
def parentMethods = (superClass ++ interfaces).view.flatMap(_.allMethods)
def allMethods: Iterable[MethodInfo] = methods ++ parentMethods
def findPublicImpl(name: String, desc: String) = allMethods.find(m =>
m.name == name && m.desc == desc && !m.isAbstract && !m.isPrivate
)
// javac reads view()'s generic return as Object instead of its IterableView descriptor.
def parentMethods =
ClassInfoLookup.parentMethods((superClass ++ interfaces).view)
def allMethods: Iterable[MethodInfo] = ClassInfoLookup.allMethods(this)
def findPublicImpl(name: String, desc: String) =
ClassInfoLookup.findPublicImpl(this, name, desc)
def isScala = false
def isTrait = false
@@ -156,7 +157,7 @@ object ASMMixinCompiler {
def remClassInfo(name: String) = infoCache.remove(name)
implicit def getClassInfo(name: String) =
infoCache.getOrElseUpdate(name, ClassInfo.obtainInfo(name))
ClassInfoLookup.getClassInfo(infoCache, name)
implicit def getClassInfo(cnode: ClassNode): ClassInfo = getClassInfo(
cnode.name
)
@@ -169,15 +170,16 @@ object ASMMixinCompiler {
def owner = ReflectionClassInfo.this
def name = method.getName
def desc = getType(method).getDescriptor
def exceptions = method.getExceptionTypes.map(c => nodeName(c.getName))
def exceptions = ClassInfoLookup.exceptionNames(method)
def isPrivate = Modifier.isPrivate(method.getModifiers)
def isAbstract = Modifier.isAbstract(method.getModifiers)
}
def name = nodeName(clazz.getName)
def superClass = Option(clazz.getSuperclass)
def interfaces = clazz.getInterfaces.map(getClassInfo)
def methods = clazz.getMethods.map(ReflectionMethodInfo(_))
def interfaces = ClassInfoLookup.reflectionInterfaces(clazz)
def methods =
ClassInfoLookup.reflectionMethods(clazz, ReflectionMethodInfo(_))
}
class ClassNodeInfo(val cnode: ClassNode) extends ClassInfo {
@@ -185,15 +187,16 @@ object ASMMixinCompiler {
def owner = ClassNodeInfo.this
def name = mnode.name
def desc = mnode.desc
def exceptions = Array(mnode.exceptions: _*)
def exceptions = ClassInfoLookup.exceptionNames(mnode.exceptions)
def isPrivate = (mnode.access & ACC_PRIVATE) != 0
def isAbstract = (mnode.access & ACC_ABSTRACT) != 0
}
def name = cnode.name
def superClass = Some(cnode.superName)
def interfaces: Seq[ClassInfo] = cnode.interfaces.map(getClassInfo)
def methods = cnode.methods.map(MethodNodeInfoSource)
def interfaces: Seq[ClassInfo] = ClassInfoLookup.nodeInterfaces(cnode)
def methods =
ClassInfoLookup.nodeMethods(cnode.methods, MethodNodeInfoSource)
}
class ScalaClassInfo(
@@ -202,43 +205,25 @@ object ASMMixinCompiler {
val csym: ScalaSignature#ClassSymbolRef
) extends ClassNodeInfo(cnode$) {
override def superClass = Some(csym.jParent)
override def interfaces = csym.jInterfaces.map(getClassInfo)
override def interfaces = ClassInfoLookup.scalaInterfaces(csym)
override def isScala = true
override def isTrait = csym.isTrait
override def isObject = csym.isObject
}
private[ASMMixinCompiler] def obtainInfo(name: String): ClassInfo = {
if (name == null) return null
def scalaInfo(cnode: ClassNode, obj: Boolean) =
ScalaSigReader$.MODULE$.ann(cnode).flatMap { ann =>
val sig = ScalaSigReader$.MODULE$.read(ann)
val name = cnode.name.replace('/', '.')
(if (obj) sig.findObject(name) else sig.findClass(name))
.map(csym => new ScalaClassInfo(cnode, sig, csym))
}
if (name.endsWith("$")) { // find scala object
val baseName = name.substring(0, name.length - 1)
val baseNode = classNode(baseName)
if (baseNode != null) scalaInfo(baseNode, true) match {
case Some(info) => return info
case None =>
}
}
classNode(name) match {
case null =>
cl.findClass(name.replace('/', '.')) match {
case null => null
case c => new ReflectionClassInfo(c)
}
case cnode =>
scalaInfo(cnode, false).getOrElse(new ClassNodeInfo(cnode))
}
}
private[ASMMixinCompiler] def obtainInfo(name: String): ClassInfo =
ClassInfoLookup.obtainInfo(
name,
(node, sig, symbol) =>
new ScalaClassInfo(
node,
sig,
symbol.asInstanceOf[ScalaSignature#ClassSymbolRef]
),
node => new ClassNodeInfo(node),
clazz => new ReflectionClassInfo(clazz)
)
}
import StackAnalyser.width
@@ -0,0 +1,176 @@
package codechicken.multipart.asm;
import java.lang.reflect.Method;
import java.util.Objects;
import org.objectweb.asm.tree.AnnotationNode;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.MethodNode;
import codechicken.multipart.asm.ASMMixinCompiler.ClassInfo;
import codechicken.multipart.asm.ASMMixinCompiler.ClassInfo$;
import codechicken.multipart.asm.ASMMixinCompiler.MethodInfo;
import scala.Array$;
import scala.Function1;
import scala.Function3;
import scala.Option;
import scala.Predef;
import scala.Predef.DummyImplicit$;
import scala.collection.GenTraversableOnce;
import scala.collection.Iterable;
import scala.collection.Iterable$;
import scala.collection.IterableView;
import scala.collection.IterableView$;
import scala.collection.JavaConversions;
import scala.collection.Seq;
import scala.collection.TraversableLike;
import scala.collection.immutable.List$;
import scala.collection.mutable.Buffer;
import scala.collection.mutable.Buffer$;
import scala.collection.mutable.Map;
import scala.runtime.AbstractFunction0;
import scala.runtime.AbstractFunction1;
/**
* Metadata lookup behind the retained Scala nested models, construction callbacks and view bridge. Raw traversal
* interfaces disambiguate Scala's covariant generic methods while preserving virtual collection dispatch.
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
final class ClassInfoLookup {
private ClassInfoLookup() {}
static IterableView<MethodInfo, Iterable<?>> parentMethods(IterableView<ClassInfo, ?> parents) {
return (IterableView<MethodInfo, Iterable<?>>) ((TraversableLike) parents)
.flatMap(new AbstractFunction1<ClassInfo, GenTraversableOnce<MethodInfo>>() {
@Override
public GenTraversableOnce<MethodInfo> apply(ClassInfo parent) {
return parent.allMethods();
}
}, IterableView$.MODULE$.canBuildFrom());
}
static Iterable<MethodInfo> allMethods(ClassInfo info) {
return (Iterable<MethodInfo>) ((TraversableLike) info.methods())
.$plus$plus(info.parentMethods(), Iterable$.MODULE$.canBuildFrom());
}
static Option<MethodInfo> findPublicImpl(ClassInfo info, final String name, final String desc) {
return info.allMethods().find(new AbstractFunction1<MethodInfo, Object>() {
@Override
public Object apply(MethodInfo method) {
return Objects.equals(method.name(), name) && Objects.equals(method.desc(), desc)
&& !method.isAbstract()
&& !method.isPrivate();
}
});
}
static ClassInfo getClassInfo(Map<String, ClassInfo> cache, final String name) {
return cache.getOrElseUpdate(name, new AbstractFunction0<ClassInfo>() {
@Override
public ClassInfo apply() {
return ClassInfo$.MODULE$.obtainInfo(name);
}
});
}
static Iterable<ClassInfo> reflectionInterfaces(Class<?> type) {
return mapArray(type.getInterfaces(), new AbstractFunction1<Class<?>, ClassInfo>() {
@Override
public ClassInfo apply(Class<?> parent) {
return ASMMixinCompiler$.MODULE$.getClassInfo(parent);
}
});
}
static Iterable<MethodInfo> reflectionMethods(Class<?> type, Function1<Method, MethodInfo> factory) {
return mapArray(type.getMethods(), factory);
}
private static <A, B> Iterable<B> mapArray(A[] values, Function1<A, B> function) {
return (Iterable<B>) ((TraversableLike) Predef.refArrayOps(values))
.map(function, Array$.MODULE$.fallbackCanBuildFrom(DummyImplicit$.MODULE$.dummyImplicit()));
}
static Seq<ClassInfo> nodeInterfaces(ClassNode node) {
return (Seq<ClassInfo>) ((TraversableLike) JavaConversions.asScalaBuffer(node.interfaces))
.map(resolveName(), Buffer$.MODULE$.canBuildFrom());
}
static Iterable<MethodInfo> nodeMethods(Buffer<MethodNode> methods, Function1<MethodNode, MethodInfo> factory) {
return (Iterable<MethodInfo>) ((TraversableLike) methods).map(factory, Buffer$.MODULE$.canBuildFrom());
}
static Seq<ClassInfo> scalaInterfaces(Object symbol) {
return (Seq<ClassInfo>) ((TraversableLike) ((ScalaSignature.ClassSymbolRef) symbol).jInterfaces())
.map(resolveName(), List$.MODULE$.canBuildFrom());
}
private static Function1<String, ClassInfo> resolveName() {
return new AbstractFunction1<String, ClassInfo>() {
@Override
public ClassInfo apply(String name) {
return ASMMixinCompiler$.MODULE$.getClassInfo(name);
}
};
}
static String[] exceptionNames(Method method) {
Class<?>[] exceptions = method.getExceptionTypes();
String[] names = new String[exceptions.length];
for (int i = 0; i < names.length; i++) names[i] = ASMImplicits.nodeName(exceptions[i].getName());
return names;
}
static String[] exceptionNames(java.util.List<String> exceptions) {
return exceptions.toArray(new String[0]);
}
static ClassInfo obtainInfo(String name, Function3<ClassNode, ScalaSignature, Object, ClassInfo> scalaFactory,
Function1<ClassNode, ClassInfo> nodeFactory, Function1<Class<?>, ClassInfo> reflectionFactory) {
if (name == null) return null;
ASMMixinCompiler$ compiler = ASMMixinCompiler$.MODULE$;
if (name.endsWith("$")) {
ClassNode base = compiler.classNode(name.substring(0, name.length() - 1));
if (base != null) {
Option<ClassInfo> info = scalaInfo(base, true, scalaFactory);
if (info.isDefined()) return info.get();
}
}
ClassNode node = compiler.classNode(name);
if (node == null) {
Class<?> type;
try {
type = compiler.cl().findClass(name.replace('/', '.'));
} catch (ClassNotFoundException exception) {
return ClassInfoLookup.<ClassInfo, RuntimeException>throwUnchecked(exception);
}
return type == null ? null : reflectionFactory.apply(type);
}
Option<ClassInfo> info = scalaInfo(node, false, scalaFactory);
return info.isDefined() ? info.get() : nodeFactory.apply(node);
}
private static Option<ClassInfo> scalaInfo(ClassNode node, boolean object,
Function3<ClassNode, ScalaSignature, Object, ClassInfo> factory) {
Option<AnnotationNode> annotation = ScalaSigReader$.MODULE$.ann(node);
if (!annotation.isDefined()) return Option.empty();
ScalaSignature signature = ScalaSigReader$.MODULE$.read(annotation.get());
String name = node.name.replace('/', '.');
Option<? extends ScalaSignature.ClassSymbolRef> symbol = object ? signature.findObject(name)
: signature.findClass(name);
if (!symbol.isDefined()) return Option.empty();
return new scala.Some<>(factory.apply(node, signature, symbol.get()));
}
@SuppressWarnings("unchecked")
private static <R, E extends Throwable> R throwUnchecked(Throwable throwable) throws E {
throw (E) throwable;
}
}