Clarification on include a classloader

Viewed 37

I am trying to create an Advice for sun.nio.ch.NioSocketImpl, which is in Platform classloader, using ForAdvice#include to be able to use my own class in the Advice (I want to keep data in a map between two Advices - timestamps for #connect and #close socket). Unfortunately, the class was not found.

java.lang.NoClassDefFoundError: pbouda/agents/socket/advice/SocketLifespanKeeper




new AgentBuilder.Default()
            .disableClassFormatChanges()
            .with(RedefinitionStrategy.RETRANSFORMATION)
            .ignore(none())
            .type(named("sun.nio.ch.NioSocketImpl"))
            .transform(new AgentBuilder.Transformer.ForAdvice()
                    .include(SocketAgent.class.getClassLoader())
                    .advice(connectMatcher, "pbouda.agents.socket.advice.NioSocketConnectAdvice"))
            .installOn(inst);

Is it even possible for classes that are in boot and platform classloaders?

1 Answers

Your advice should only refer to types that are guaranteeded to be available on the same class loader. If you want to communicate back to your agent, the Instrumentation API offers ways of injecting classes into the boot loader which is available to all classes. You would then need to inject these classes into the bootloader to later communicate with them through your advice class.

Related