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

Add static method to class not working

Open beikov opened this issue 2 years ago • 3 comments

Hi there, I'm trying to add a static method to a newly created class but there seem to be method filters in place that prevent non-virtual methods to be compiled. This is how I create the method:

builder = builder.defineMethod(
	"get_" + getter.getName(),
	TypeDescription.Generic.OfNonGenericType.ForLoadedType.of(
		getterType
	),
	Opcodes.ACC_PROTECTED | Opcodes.ACC_STATIC
)
.withParameter( clazz )
.intercept(new Implementation.Simple(new GetFieldOnArgument(getter)));

net.bytebuddy.dynamic.scaffold.subclass.SubclassDynamicTypeBuilder.InstrumentableMatcher#resolve and net.bytebuddy.dynamic.scaffold.MethodGraph.Compiler.Default#compile both create matchers that only handle methods which are virtual.

How am I supposed to add a static method to a class that I want to create?

beikov avatar Jun 21 '23 09:06 beikov

I cannot reproduce this problem. Can you create an executable sample?

raphw avatar Jun 21 '23 20:06 raphw

Turns out, one of the parameters of the method was not visible to the class in which I wanted to define the method: bytebuddy-1465.zip

You are apparently filtering out methods with instrumentedType.getDeclaredMethods().filter(not(isVirtual()).and(relevanceMatcher)) in net.bytebuddy.dynamic.scaffold.MethodRegistry.Default#prepare, and the relevanceMatcher seems to consider visibility. Wouldn't it be better to throw an error instead?

beikov avatar Jun 22 '23 09:06 beikov

Those methods do sometimes exist for bridging calls with respect to generics. But it should indeed be more explicit if you define such methods explicitly. I will have a look if this can be improved.

raphw avatar Jun 22 '23 10:06 raphw