Saturday, June 8, 2019

Java's Proxy can't proxy a class, but Javassist can

One Java project I'm working on needs to be able to create instances of one class (specified dynamically) that act exactly like instances of a class with a very similar set of members. Java's built-in Proxy class can create an object that implements specified interfaces and delegates all method calls to a single handler, but since the resulting objects inherit from Proxy this works for interfaces only. However, the Javassist library provides ProxyFactory, which does something very similar but also allows setting the superclass.

No comments:

Post a Comment