I'm trying to mock a class that gets loaded by some process to return another value.
Say I have
Class A
{
public static IClassType getTheClass()
{
new ClassB();
}
}
And I want that if some process calls ClassA.getTheClass()
to return ClassC
(which of course implements IClassType
)
Somehow I figured out this should be possible with Byte Buddy's hot swapping capabilities. But I fail to install an agent.
I tried with the -javaagent:C:\byte-buddy-agent-0.7.1.jar
switch wich gives me
java.lang.IllegalStateException: The Byte Buddy agent is not installed or not accessible
as well as with ByteBuddyAgent.install();
which produces
java.lang.IllegalStateException: Attachment provider cannot connect on the current JVM: ByteBuddyAgent.AttachmentProvider.Compound{attachmentProviders=[ByteBuddyAgent.AttachmentProvider.ForJigsawVm.INSTANCE, ByteBuddyAgent.AttachmentProvider.ForToolsJarVm.JVM_ROOT, ByteBuddyAgent.AttachmentProvider.ForToolsJarVm.JDK_ROOT, ByteBuddyAgent.AttachmentProvider.ForToolsJarVm.MACINTOSH]}
on my machine. Does this only work for OpenJDK? Am I missing something?
There was an error in the byte-buddy-agent.jar
until recently. Try using a newer version and it should function without a problem.
The ByteBuddyAgent
does indeed only work on JVM's that support some form of tools.jar
. (Currently, OpenJDK, OracleJDK and J9.)