byte-buddy icon indicating copy to clipboard operation
byte-buddy copied to clipboard

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

Open supersurviveur opened this issue 1 year ago • 4 comments

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?

supersurviveur avatar Dec 12 '23 11:12 supersurviveur

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

dogourd avatar Dec 13 '23 02:12 dogourd

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)'.

supersurviveur avatar Dec 13 '23 06:12 supersurviveur

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?

raphw avatar Dec 13 '23 11:12 raphw

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 !

supersurviveur avatar Dec 14 '23 06:12 supersurviveur