Sure you can. Javassist compiled classes are first class classes (even if only for a short time) so it's a good idea to cache the created classes once they've been created to save yourself the overhead of recompiling them over-and-over. Plus you don't have to invent improbable names for your classes since you will only recompile the same virtual code once.
Javassist classes can be shared with multiple threads?
题
Can I share Javassist classes in multiple threads to gets a better performance in a web app? I think that I can create a cache with ConcurrentMap
for proxyClass
to avoid creation in each page request.
My code is:
ProxyFactory factory = new ProxyFactory();
factory.setFilter(IGNORE_BRIDGE_AND_OBJECT_METHODS);
factory.setInterfaces(new Class[] { type });
Class<?> proxyClass = factory.createClass(); // can I cache here after class creation?
Object proxyInstance = ...; // objenesis creates new instance here
setHandler(proxyInstance, myCustomHandlerHere);
UPDATE: I see the Javassist code, and Javassist also provides a cache.
解决方案
不隶属于 StackOverflow