diff --git a/JAVA_MIGRATION.md b/JAVA_MIGRATION.md index 2b6afbc..9c77323 100644 --- a/JAVA_MIGRATION.md +++ b/JAVA_MIGRATION.md @@ -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. diff --git a/JAVA_MIGRATION_HANDOFF.md b/JAVA_MIGRATION_HANDOFF.md index ebd7547..6d337b9 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`. 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'"' '/ - 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 diff --git a/src/main/scala/codechicken/multipart/asm/ClassInfoLookup.java b/src/main/scala/codechicken/multipart/asm/ClassInfoLookup.java new file mode 100644 index 0000000..40ed42b --- /dev/null +++ b/src/main/scala/codechicken/multipart/asm/ClassInfoLookup.java @@ -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> parentMethods(IterableView parents) { + return (IterableView>) ((TraversableLike) parents) + .flatMap(new AbstractFunction1>() { + + @Override + public GenTraversableOnce apply(ClassInfo parent) { + return parent.allMethods(); + } + }, IterableView$.MODULE$.canBuildFrom()); + } + + static Iterable allMethods(ClassInfo info) { + return (Iterable) ((TraversableLike) info.methods()) + .$plus$plus(info.parentMethods(), Iterable$.MODULE$.canBuildFrom()); + } + + static Option findPublicImpl(ClassInfo info, final String name, final String desc) { + return info.allMethods().find(new AbstractFunction1() { + + @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 cache, final String name) { + return cache.getOrElseUpdate(name, new AbstractFunction0() { + + @Override + public ClassInfo apply() { + return ClassInfo$.MODULE$.obtainInfo(name); + } + }); + } + + static Iterable reflectionInterfaces(Class type) { + return mapArray(type.getInterfaces(), new AbstractFunction1, ClassInfo>() { + + @Override + public ClassInfo apply(Class parent) { + return ASMMixinCompiler$.MODULE$.getClassInfo(parent); + } + }); + } + + static Iterable reflectionMethods(Class type, Function1 factory) { + return mapArray(type.getMethods(), factory); + } + + private static Iterable mapArray(A[] values, Function1 function) { + return (Iterable) ((TraversableLike) Predef.refArrayOps(values)) + .map(function, Array$.MODULE$.fallbackCanBuildFrom(DummyImplicit$.MODULE$.dummyImplicit())); + } + + static Seq nodeInterfaces(ClassNode node) { + return (Seq) ((TraversableLike) JavaConversions.asScalaBuffer(node.interfaces)) + .map(resolveName(), Buffer$.MODULE$.canBuildFrom()); + } + + static Iterable nodeMethods(Buffer methods, Function1 factory) { + return (Iterable) ((TraversableLike) methods).map(factory, Buffer$.MODULE$.canBuildFrom()); + } + + static Seq scalaInterfaces(Object symbol) { + return (Seq) ((TraversableLike) ((ScalaSignature.ClassSymbolRef) symbol).jInterfaces()) + .map(resolveName(), List$.MODULE$.canBuildFrom()); + } + + private static Function1 resolveName() { + return new AbstractFunction1() { + + @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 exceptions) { + return exceptions.toArray(new String[0]); + } + + static ClassInfo obtainInfo(String name, Function3 scalaFactory, + Function1 nodeFactory, Function1, 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 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.throwUnchecked(exception); + } + return type == null ? null : reflectionFactory.apply(type); + } + Option info = scalaInfo(node, false, scalaFactory); + return info.isDefined() ? info.get() : nodeFactory.apply(node); + } + + private static Option scalaInfo(ClassNode node, boolean object, + Function3 factory) { + Option annotation = ScalaSigReader$.MODULE$.ann(node); + if (!annotation.isDefined()) return Option.empty(); + ScalaSignature signature = ScalaSigReader$.MODULE$.read(annotation.get()); + String name = node.name.replace('/', '.'); + Option 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 throwUnchecked(Throwable throwable) throws E { + throw (E) throwable; + } +}