In a long running high volume application, we have had an occurence of a NPE at
com.thoughtworks.xstream.converters.reflection.Sun14ReflectionProvider.newInstance(Sun14ReflectionProvider.java:74).
After looking at the source, at tiny window has been found in the 'getMungedConstructor'
method involving the use of WeakReference and bad luck with the timing of the
garbage collector.
A suggested fix for this method follows:
private Constructor getMungedConstructor(Class type) throws NoSuchMethodException {
WeakReference ref = (WeakReference)constructorCache.get(type);
Constructor mungedConstructor = ref != null? ref.get(): null;
if (mungedConstructor == null) {
Constructor javaLangObjectConstructor = Object.class.getDeclaredConstructor(new Class[0]);
mungedConstructor = reflectionFactory.newConstructorForSerialization(type, javaLangObjectConstructor);
ref = new WeakReference(mungedConstructor);
constructorCache.put(type, ref);
}
return mungedConstructor;
}
Thank you for your attention.