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 static void main(String[] args) {
new NoClassDefFoundError("should be intercepted!!!").toString();
new Foo("oh").toString();
}
不可解さは何ElementMatchers.nameContains("Foo")
とElementMatchers.isSubTypeOf(LinkageError.class)
を交換して期待される結果とFooのコンストラクタを与えることである:
テストコードが
public class MyInterceptor {
@RuntimeType
public static void intercept(@Origin Constructor<?> constructor) throws Exception {
System.out.println("Intercepted: " + constructor.getName());
}
}
傍受される。
bytebuddy version - 1.7.1 – dgt