In short: 1. I have some final class that I want to create dynamic proxy for it. How can I do it? 2. Can I convert MethodHandle to Method?
Detai
Sorry, what you want isn't possible:
You can use CGLIB or Javassist to create proxies for concrete classes because these libraries dynamically generate a subclass of the class you're trying to proxy. A final
class can't be subclassed, so you can't create a proxy this way.
PowerMock does let you proxy final
classes and methods, but this is because it runs your tests under its special ClassLoader
which uses Javassist to modify the bytecode of the classes you wish to proxy as they're being loaded. (You wouldn't want to use this sort of thing in production, as generally the modified "zombie" version of the class that results won't be good for much else than running a specific mock unit test.)
The PowerMock approach wouldn't work here, however - you want to proxy java.lang.reflect.Method
, which is on the bootstrap classpath, and so would load before any PowerMock/Javassist type tool and therefore not be proxiable.