0
由於某些原因,我還無法解決,我的代理程序不攔截java LinkageError實例。用bytebuddy攔截錯誤構造函數
代理代碼:
import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.implementation.SuperMethodCall;
import net.bytebuddy.matcher.ElementMatchers;
import java.lang.instrument.Instrumentation;
public class MyAgent {
public static void premain(String arguments, Instrumentation instrumentation) {
new AgentBuilder.Default()
.type(ElementMatchers.isSubTypeOf(LinkageError.class))
.transform((builder, type, classLoader, module) ->
builder.constructor(ElementMatchers.isDefaultConstructor())
.intercept(SuperMethodCall.INSTANCE.andThen(MethodDelegation.to(MyInterceptor.class)))
).installOn(instrumentation);
}
}
攔截代碼:
public class MyInterceptor {
@RuntimeType
public static void intercept(@Origin Constructor<?> constructor) throws Exception {
System.out.println("Intercepted: " + constructor.getName());
}
}
測試代碼:
public static void main(String[] args) {
new NoClassDefFoundError("should be intercepted!!!").toString();
new Foo("oh").toString();
}
更令人不解的是,隨着ElementMatchers.nameContains("Foo")
更換ElementMatchers.isSubTypeOf(LinkageError.class)
給出了預期的結果和Foo構造被攔截。
bytebuddy版本 - 1.7.1 – dgt