[HN Gopher] Mixin is a trait/mixin and bytecode weaving framewor...
       ___________________________________________________________________
        
       Mixin is a trait/mixin and bytecode weaving framework for Java
       using ASM
        
       Author : LelouBil
       Score  : 50 points
       Date   : 2024-12-28 02:10 UTC (20 hours ago)
        
 (HTM) web link (github.com)
 (TXT) w3m dump (github.com)
        
       | fnord77 wrote:
       | Isn't composition is superior to mixins for a few reasons,
       | particularly the "Single-responsibility principle" ?
        
         | twic wrote:
         | The purpose of this library is to make it easier to modify
         | existing code, eg to make existing classes implement new
         | interfaces. In particular, it seems to be used for Minecraft
         | modding.
        
           | LelouBil wrote:
           | Yes, Minecraft gets more and more data-driven, but for adding
           | completely new features like, a solar system with planets, or
           | turning the player into a robot of something, you'll have to
           | edit some of the built in code.
           | 
           | This Mixin library is very useful, since it allows different
           | mods to edit different parts of the same code without
           | interfering for example.
           | 
           | It's also very easy to use compared to jvm bytecode
           | manipulation, you just write a normal java function and put
           | an annotation on it, and boom, now it runs right before line
           | X inside of class Y, at runtime !
        
         | mudkipdev wrote:
         | You're referring to mixins in programming
         | (https://en.wikipedia.org/wiki/Mixin) which isn't related to
         | this library
        
         | based2 wrote:
         | https://tapestry.apache.org/component-mixins.html
        
       | love2read wrote:
       | For an example of how this is used when setup:
       | https://wiki.fabricmc.net/tutorial:mixin_examples
        
       | ddimitrov wrote:
       | What is the advantage compared to established AOP libraries, such
       | as AspectJ or BytecodeBuddy?
        
         | sgolestane wrote:
         | Both of these use ASM under the hood
        
         | gabizou wrote:
         | A primary advantage of Mixin is using Java as a DSL to apply
         | various kinds of bytecode transformation with some examples
         | here:
         | 
         | - Changing a constant within a target method
         | https://github.com/SpongePowered/Sponge/blob/06ebd1d3479b9ed...
         | 
         | - Replacing a method call within a specific method
         | https://github.com/SpongePowered/Sponge/blob/06ebd1d3479b9ed...
         | 
         | - Adding an interface and its implementation to the target
         | class (in this case net.minecraft.world.entity.Entity will now
         | implement org.spongepowered.api.entity.Entity and its methods)
         | https://github.com/SpongePowered/Sponge/blob/06ebd1d3479b9ed...
         | 
         | There's a fair bit more that is possible that would otherwise
         | require a fair bit more code wrangling with AspectJ or
         | BytecodeBuddy to achieve the same effects.
        
       | Dylan16807 wrote:
       | > ASM is an all purpose Java bytecode manipulation and analysis
       | framework.
       | 
       | Oh. I was expecting something much more weird from the title, and
       | that's a pretty confusing/misleading name for that.
        
       | anonymoushn wrote:
       | ah yes ASM, the bytecode manipulator for java. Please look
       | forward to JAVA, my upcoming tool for manipulating assembly code.
        
       | saagarjha wrote:
       | I kind of wish Java just went full Objective-C and provided
       | native APIs to patch and redirect methods. Having everyone ship a
       | bytecode manipulation library just to mock a method or two is
       | quite annoying given how easy it would be for the runtime to
       | offer this directly (and probably more performantly).
        
         | oftenwrong wrote:
         | Not sure if this fits, but JDK 24 includes a new API,
         | java.lang.classfile, for classfile manipulation.
         | 
         | https://openjdk.org/jeps/484
        
           | _old_dude_ wrote:
           | Is their a design document, somewhere about this new library
           | ?
           | 
           | The jep does not explain how to prepare to new Java bytecodes
           | and future enhancements. Am i suppose to only run with the
           | latest version of Java if i want to use that library ?
        
         | gabizou wrote:
         | The applications for Mixin wasn't to mock a method or two, it's
         | to enable full-scale modification of an existing Java
         | application at runtime without recompiling/redistributing the
         | original application. In the case of its origins, modifying
         | Minecraft server to add a non-obfuscated API and implement said
         | API on top of it through Mixins. The way this is achieved is
         | practically at the class load point in time so the runtime
         | costs are reduced to maybe a couple of seconds (as of current
         | writing, Sponge has somewhere around 1.2k mixins overall).
         | Could there be a better native API? I'm hoping so, Java's Class
         | File API (JEP-484) is a pretty good start, but I don't think
         | it's quite enough yet to enable the full functionality of what
         | Mixin does.
         | 
         | [1]:https://openjdk.org/jeps/484
        
       | sigy wrote:
       | I was curious about this as perhaps a new approach to recipe-
       | oriented bytecode injection. But the I looked at the project's
       | (long) history and docs status. It seems that the main
       | contributor has tapered off this project over time, and more
       | specifically hasn't been active since July. (Mumfrey, if you're
       | out there, please tell us what's up)
       | 
       | So, OP, what made you mention this here now?
        
         | Starlevel004 wrote:
         | There's a maintained fork here:
         | https://github.com/FabricMC/Mixin
        
         | asiekierka wrote:
         | IIRC, at least a few years ago, Mumfrey had a tendency to
         | develop Mixin in private and only push the commit backlog for
         | releases, leading to periods of time where no activity was
         | publicly visible. (Also IIRC, this is part of the reason why
         | the FabricMC fork exists.)
        
         | gabizou wrote:
         | I would say that the feature set is pretty powerful as is, and
         | it shows in the earlier history (having worked with Mixins
         | since before it was called Mixins). The history reaches further
         | back than the first commit to having been developed for
         | Mumfrey's own modloader (LiteLoader).
         | 
         | As someone else already replied, Mumfrey still practices
         | private development and only pushes new code after his own
         | extensive testing. He has long periods of "public" inactivity
         | because code refactors and other changes can be delicate given
         | the widespread usage of the library within the Minecraft
         | modding ecosystem, and as such, he doesn't want the distraction
         | that in-development can attract from said community.
        
       | based2 wrote:
       | ? alt: Java 23 - Class-File API (Second Preview)
       | 
       | https://news.ycombinator.com/item?id=41567720
        
       ___________________________________________________________________
       (page generated 2024-12-28 23:02 UTC)