quarks
quarks

Reputation: 35276

Runtime class override with ByteBuddy

I created a custom File based on java.io.File as such I have:

class CloudFile extends java.io.File {}

Is it possible with ByteBuddy to (at runtime) be able to replace all calls to the class java.io.File and masquerade it with the new CloudFile

So, for example I created an app that uses a library that extensively uses java.io.File what I want is any call of that library to the File class in my application would be a call to CloudFile

Or otherwise, a much more plausible solution, to use ByteBuddy to create a Classloader that will override the java.io.File -- and the question is what is the right take on this?

Upvotes: 0

Views: 461

Answers (1)

Rafael Winterhalter
Rafael Winterhalter

Reputation: 44007

You can redefine classes using a Java agent which Byte Buddy allows building with an AgentBuilder. You can then instruct it to redefine all your library code where you replace constructor calls to File with your CloudFile which I assume has virtual overrides for the methods in question.

In order to replace a constructor, create a transformer that registers a MemberSubstitution where the constructor is switched out.

Upvotes: 1

Related Questions