mardi 6 août 2019

Java - prevent field assignment using reflection on instantiation?

I have a java class in a 3rd party lib with a private member which is assigned at class instantiation.

public class CacheLookupUtil extends AbstractCacheLookupUtil<InvocationContext> {
  @Inject
  private BeanManagerUtil beanManagerUtil;

  private CacheKeyGenerator defaultCacheKeyGenerator = new DefaultCacheKeyGenerator();
  private CacheResolverFactory defaultCacheResolverFactory = new DefaultCacheResolverFactory();

  ...
  ...
}

My problem is that the assignment of defaultCacheResolverFactory is causing an exception due to the wrong constructor having been chosen.

If I try to subclass CacheLookupUtil, this assignment is still done in the parent class, so I'm no further ahead.

Is there any mechanism I can use in Java reflection that would allow me to construct/instantiate the object, but prevent the assignment of defaultCacheResolverFactory, and allowing me to set the value via reflection?

I know this is an ugly solution, but to be honest, I cannot visualize any other way to proceed.





Aucun commentaire:

Enregistrer un commentaire