Adrian Cole
Adrian Cole

Reputation: 792

Strategy for intercepting signed classes

I'm trying to make a concurrent test for com.microsoft.azure.eventprocessorhost.PartitionContext which has a multi-parameter package-protected constructor in a signed jar.

I'd like to basically ignore that constructor (or pass it nulls) and override one method to increment a counter. The class isn't final, and neither is the constructor or the method in question.

Subclassing that directly results in a security exception due to the signing. I've looked at various examples, and played with a local version of ByteBuddyTutorialExamplesTest, but do no avail.

At the moment, I'm considering giving up and instead intercepting the call site to that class. That said, it would be handy to have a fake version of the target (PartitionContext).

Am I missing any tricks, or is what I'm trying to accomplish not possible?

Upvotes: 2

Views: 133

Answers (1)

Rafael Winterhalter
Rafael Winterhalter

Reputation: 44032

Byte Buddy offers a ClassLoadingStrategy that injects a class into a class loader and which can use a given ProtectionDomain. The domain does however need to be provided explicitly.

To apply this strategy, the load method is overloaded with a second argument:

builder.make().load(sealedClass.getClassLoader(), 
  ClassLoadingStrategy.Default.INJECTION.with(sealedClass.getProtectionDomain()))

Upvotes: 2

Related Questions