Skip to content

Commit c368068

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 df76f14 commit c368068

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -146,20 +146,16 @@ static CachedIntrospectionResults forClass(Class beanClass) throws BeansExceptio
146146
results = (CachedIntrospectionResults) value;
147147
}
148148
if (results == null) {
149-
// On JDK 1.5 and higher, it is almost always safe to cache the bean class...
150-
// The sole exception is a custom BeanInfo class being provided in a non-safe ClassLoader.
151-
boolean fullyCacheable =
152-
ClassUtils.isCacheSafe(beanClass, CachedIntrospectionResults.class.getClassLoader()) ||
153-
isClassLoaderAccepted(beanClass.getClassLoader());
154-
if (fullyCacheable || !ClassUtils.isPresent(beanClass.getName() + "BeanInfo", beanClass.getClassLoader())) {
155-
results = new CachedIntrospectionResults(beanClass, fullyCacheable);
149+
if (ClassUtils.isCacheSafe(beanClass, CachedIntrospectionResults.class.getClassLoader()) ||
150+
isClassLoaderAccepted(beanClass.getClassLoader())) {
151+
results = new CachedIntrospectionResults(beanClass);
156152
classCache.put(beanClass, results);
157153
}
158154
else {
159155
if (logger.isDebugEnabled()) {
160156
logger.debug("Not strongly caching class [" + beanClass.getName() + "] because it is not cache-safe");
161157
}
162-
results = new CachedIntrospectionResults(beanClass, true);
158+
results = new CachedIntrospectionResults(beanClass);
163159
classCache.put(beanClass, new WeakReference<CachedIntrospectionResults>(results));
164160
}
165161
}
@@ -222,7 +218,7 @@ private static boolean isUnderneathClassLoader(ClassLoader candidate, ClassLoade
222218
* @param beanClass the bean class to analyze
223219
* @throws BeansException in case of introspection failure
224220
*/
225-
private CachedIntrospectionResults(Class beanClass, boolean cacheFullMetadata) throws BeansException {
221+
private CachedIntrospectionResults(Class beanClass) throws BeansException {
226222
try {
227223
if (logger.isTraceEnabled()) {
228224
logger.trace("Getting BeanInfo for class [" + beanClass.getName() + "]");
@@ -270,9 +266,7 @@ private CachedIntrospectionResults(Class beanClass, boolean cacheFullMetadata) t
270266
(pd.getPropertyEditorClass() != null ?
271267
"; editor [" + pd.getPropertyEditorClass().getName() + "]" : ""));
272268
}
273-
if (cacheFullMetadata) {
274-
pd = buildGenericTypeAwarePropertyDescriptor(beanClass, pd);
275-
}
269+
pd = buildGenericTypeAwarePropertyDescriptor(beanClass, pd);
276270
this.propertyDescriptorCache.put(pd.getName(), pd);
277271
}
278272
}

0 commit comments

Comments
 (0)