Browse Source

CachedIntrospectionResults.clearClassLoader(null) removes cached classes for the system class loader

Issue: SPR-9189
3.1.x
Juergen Hoeller 13 years ago
parent
commit
ebbcc4d9fb
  1. 11
      org.springframework.beans/src/main/java/org/springframework/beans/CachedIntrospectionResults.java
  2. 18
      org.springframework.beans/src/test/java/org/springframework/beans/CachedIntrospectionResultsTests.java

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2010 the original author or authors. * Copyright 2002-2013 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -100,9 +100,6 @@ public class CachedIntrospectionResults {
* @param classLoader the ClassLoader to clear the cache for * @param classLoader the ClassLoader to clear the cache for
*/ */
public static void clearClassLoader(ClassLoader classLoader) { public static void clearClassLoader(ClassLoader classLoader) {
if (classLoader == null) {
return;
}
synchronized (classCache) { synchronized (classCache) {
for (Iterator<Class> it = classCache.keySet().iterator(); it.hasNext();) { for (Iterator<Class> it = classCache.keySet().iterator(); it.hasNext();) {
Class beanClass = it.next(); Class beanClass = it.next();
@ -183,12 +180,12 @@ public class CachedIntrospectionResults {
* @param parent the parent ClassLoader to check for * @param parent the parent ClassLoader to check for
*/ */
private static boolean isUnderneathClassLoader(ClassLoader candidate, ClassLoader parent) { private static boolean isUnderneathClassLoader(ClassLoader candidate, ClassLoader parent) {
if (candidate == null) {
return false;
}
if (candidate == parent) { if (candidate == parent) {
return true; return true;
} }
if (candidate == null) {
return false;
}
ClassLoader classLoaderToCheck = candidate; ClassLoader classLoaderToCheck = candidate;
while (classLoaderToCheck != null) { while (classLoaderToCheck != null) {
classLoaderToCheck = classLoaderToCheck.getParent(); classLoaderToCheck = classLoaderToCheck.getParent();

18
org.springframework.beans/src/test/java/org/springframework/beans/CachedIntrospectionResultsTests.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2008 the original author or authors. * Copyright 2002-2013 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,12 +16,14 @@
package org.springframework.beans; package org.springframework.beans;
import static org.junit.Assert.*; import java.util.ArrayList;
import org.junit.Test; import org.junit.Test;
import test.beans.TestBean;
import org.springframework.core.OverridingClassLoader; import org.springframework.core.OverridingClassLoader;
import test.beans.TestBean; import static org.junit.Assert.*;
/** /**
* @author Juergen Hoeller * @author Juergen Hoeller
@ -30,7 +32,7 @@ import test.beans.TestBean;
public final class CachedIntrospectionResultsTests { public final class CachedIntrospectionResultsTests {
@Test @Test
public void testAcceptClassLoader() throws Exception { public void acceptAndClearClassLoader() throws Exception {
BeanWrapper bw = new BeanWrapperImpl(TestBean.class); BeanWrapper bw = new BeanWrapperImpl(TestBean.class);
assertTrue(bw.isWritableProperty("name")); assertTrue(bw.isWritableProperty("name"));
assertTrue(bw.isWritableProperty("age")); assertTrue(bw.isWritableProperty("age"));
@ -50,4 +52,12 @@ public final class CachedIntrospectionResultsTests {
assertTrue(CachedIntrospectionResults.classCache.containsKey(TestBean.class)); assertTrue(CachedIntrospectionResults.classCache.containsKey(TestBean.class));
} }
@Test
public void clearClassLoaderForSystemClassLoader() throws Exception {
BeanUtils.getPropertyDescriptors(ArrayList.class);
assertTrue(CachedIntrospectionResults.classCache.containsKey(ArrayList.class));
CachedIntrospectionResults.clearClassLoader(ArrayList.class.getClassLoader());
assertFalse(CachedIntrospectionResults.classCache.containsKey(ArrayList.class));
}
} }

Loading…
Cancel
Save