Skip to content

Commit bed2134

Browse files
committed
CachedIntrospectionResults uses full WeakReference for any non-safe ClassLoader arrangement
Previously, CachedIntrospectionResults had three modes of caching, with the intermediate mode relying on WeakReferences in the JDK PropertyDescriptor implementation. Since the JDK is actually using SoftReferences there these days, losing information in case of a GC run with tough memory constraints, we want to allow for hard references in PropertyDescriptor objects and therefore use a full WeakReference for the CachedIntrospectionResults object itself. Issue: SPR-10028
1 parent dd68fec commit bed2134

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

org.springframework.beans/src/main/java/org/springframework/beans/CachedIntrospectionResults.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -140,20 +140,16 @@ static CachedIntrospectionResults forClass(Class beanClass) throws BeansExceptio
140140
results = (CachedIntrospectionResults) value;
141141
}
142142
if (results == null) {
143-
// On JDK 1.5 and higher, it is almost always safe to cache the bean class...
144-
// The sole exception is a custom BeanInfo class being provided in a non-safe ClassLoader.
145-
boolean fullyCacheable =
146-
ClassUtils.isCacheSafe(beanClass, CachedIntrospectionResults.class.getClassLoader()) ||
147-
isClassLoaderAccepted(beanClass.getClassLoader());
148-
if (fullyCacheable || !ClassUtils.isPresent(beanClass.getName() + "BeanInfo", beanClass.getClassLoader())) {
149-
results = new CachedIntrospectionResults(beanClass, fullyCacheable);
143+
if (ClassUtils.isCacheSafe(beanClass, CachedIntrospectionResults.class.getClassLoader()) ||
144+
isClassLoaderAccepted(beanClass.getClassLoader())) {
145+
results = new CachedIntrospectionResults(beanClass);
150146
classCache.put(beanClass, results);
151147
}
152148
else {
153149
if (logger.isDebugEnabled()) {
154150
logger.debug("Not strongly caching class [" + beanClass.getName() + "] because it is not cache-safe");
155151
}
156-
results = new CachedIntrospectionResults(beanClass, true);
152+
results = new CachedIntrospectionResults(beanClass);
157153
classCache.put(beanClass, new WeakReference<CachedIntrospectionResults>(results));
158154
}
159155
}
@@ -216,7 +212,7 @@ private static boolean isUnderneathClassLoader(ClassLoader candidate, ClassLoade
216212
* @param beanClass the bean class to analyze
217213
* @throws BeansException in case of introspection failure
218214
*/
219-
private CachedIntrospectionResults(Class beanClass, boolean cacheFullMetadata) throws BeansException {
215+
private CachedIntrospectionResults(Class beanClass) throws BeansException {
220216
try {
221217
if (logger.isTraceEnabled()) {
222218
logger.trace("Getting BeanInfo for class [" + beanClass.getName() + "]");
@@ -252,9 +248,7 @@ private CachedIntrospectionResults(Class beanClass, boolean cacheFullMetadata) t
252248
(pd.getPropertyEditorClass() != null ?
253249
"; editor [" + pd.getPropertyEditorClass().getName() + "]" : ""));
254250
}
255-
if (cacheFullMetadata) {
256-
pd = buildGenericTypeAwarePropertyDescriptor(beanClass, pd);
257-
}
251+
pd = buildGenericTypeAwarePropertyDescriptor(beanClass, pd);
258252
this.propertyDescriptorCache.put(pd.getName(), pd);
259253
}
260254
}

0 commit comments

Comments
 (0)