raphw / byte-buddy

Runtime code generation for the Java virtual machine.
https://bytebuddy.net
Apache License 2.0
6.23k stars 804 forks source link

IllegalStateException: Cannot define abstract method '...' for non-abstract class when defining a native method #1569

Closed supersurviveur closed 6 days ago

supersurviveur commented 9 months ago

I'm trying to create a subclass where I override some methods with a native implementation. Here is my attempt :

Class<?> dynamicType = new ByteBuddy()
                .subclass(Block.class)
                .name("fr.supersurviveur.rustcraftmod.DynamicBlock")
                .defineMethod("onUse", ActionResult.class, Modifier.NATIVE | Modifier.PUBLIC)
                .withoutCode()
                .make()
                .load(getClass().getClassLoader())
                .getLoaded();

But it gives me this exception : java.lang.IllegalStateException: Cannot define abstract method 'onUse' for non-abstract class

It seems that withoutCode defines the method as abstract. Is it possible to prevent this?

dogourd commented 9 months ago

This looks like a BUG, ​​you may need to bypass TypeValidation now new ByteBuddy().with(TypeValidation.DISABLED)

supersurviveur commented 9 months ago

With new ByteBuddy().with(TypeValidation.DISABLED), I have an exception when i the method is used :

java.lang.AbstractMethodError: Receiver class fr.supersurviveur.rustcraftmod.DynamicBlock does not define or inherit an implementation of the resolved method 'net.minecraft.util.ActionResult onUse(net.minecraft.block.BlockState, net.minecraft.world.World, net.minecraft.util.math.BlockPos, net.minecraft.entity.player.PlayerEntity, net.minecraft.util.Hand, net.minecraft.util.hit.BlockHitResult)' of abstract class net.minecraft.block.AbstractBlock. Selected method is 'abstract net.minecraft.util.ActionResult fr.supersurviveur.rustcraftmod.DynamicBlock.onUse(net.minecraft.block.BlockState, net.minecraft.world.World, net.minecraft.util.math.BlockPos, net.minecraft.entity.player.PlayerEntity, net.minecraft.util.Hand, net.minecraft.util.hit.BlockHitResult)'.
raphw commented 9 months ago

This is a bug in the modifier resolution for new native methods indeed. Very little used feature, it seems. I just fixed this on master. Could you build Byte Buddy and test with the snapshot?

supersurviveur commented 9 months ago

This is a bug in the modifier resolution for new native methods indeed. Very little used feature, it seems. I just fixed this on master. Could you build Byte Buddy and test with the snapshot?

It works well with the snapshot, thank you !