java - Method intercepted twice even though it was called once -
in following code snippet i'm calling method dostuff
once on instance of subclass
. intercepted twice.
note dostuff
defined in parent class superclass
. if dostuff
defined in subclass
interception logic work expected: 1 interception.
am using byte buddy incorrectly?
package com.test; import static net.bytebuddy.matcher.elementmatchers.any; import static net.bytebuddy.matcher.elementmatchers.namestartswith; import java.util.concurrent.callable; import net.bytebuddy.agent.bytebuddyagent; import net.bytebuddy.agent.builder.agentbuilder; import net.bytebuddy.description.type.typedescription; import net.bytebuddy.dynamic.dynamictype.builder; import net.bytebuddy.implementation.methoddelegation; import net.bytebuddy.implementation.bind.annotation.runtimetype; import net.bytebuddy.implementation.bind.annotation.supercall; import org.junit.test; public class reprobugtest { @test public void reprobug() { new agentbuilder.default().type(namestartswith("com.test")) .transform(new agentbuilder.transformer() { @override public builder<?> transform( builder<?> builder, typedescription td) { return builder.method(any()) .intercept( methoddelegation.to(methodinterceptor.class)); } }) .installon( bytebuddyagent.installonopenjdk()); subclass subclass = new subclass(); subclass.dostuff(); } } class superclass { public void dostuff() { system.out.println("doing stuff..."); } } class subclass extends superclass { } class methodinterceptor { @runtimetype public static object intercept(@supercall callable<?> zuper) throws exception { // intercepted twice, bug? system.out.println("intercepted"); object returnvalue = zuper.call(); return returnvalue; } }
you intercepting method call every type, i.e. both subclass
, superclass
. need further specify interceptor methods intercept. in case, want intercept methods if declared given type.
this easy implement. instead of builder.method(any())
, should intercept builder.method(isdeclaredby(td))
. way, method intercepted if declared intercepted type.
finally, can see from, source code using older version of byte buddy. version 0.7-rc6 runs stable, has additional features , fixes several bugs. (however, apis still need changed.)
Comments
Post a Comment