forked from phoedos/pmd
Merge pull request #11 from rsalvador/PMDASTClassLoader
Reuse PMDASMClassLoader instance across all compilation units analyzed.
This commit is contained in:
@ -134,7 +134,7 @@ public class ClassTypeResolver extends JavaParserVisitorAdapter {
|
||||
}
|
||||
|
||||
public ClassTypeResolver(ClassLoader classLoader) {
|
||||
pmdClassLoader = new PMDASMClassLoader(classLoader);
|
||||
pmdClassLoader = PMDASMClassLoader.getInstance(classLoader);
|
||||
}
|
||||
|
||||
// FUTURE ASTCompilationUnit should not be a TypeNode. Clean this up accordingly.
|
||||
|
@ -31,15 +31,31 @@ import org.objectweb.asm.ClassReader;
|
||||
*/
|
||||
public class PMDASMClassLoader extends ClassLoader {
|
||||
|
||||
public PMDASMClassLoader(ClassLoader parent) {
|
||||
private static PMDASMClassLoader cachedPMDASMClassLoader;
|
||||
private static ClassLoader cachedClassLoader;
|
||||
|
||||
/**
|
||||
* A new PMDASMClassLoader is created for each compilation unit, this method allows to reuse the same
|
||||
* PMDASMClassLoader across all the compilation units.
|
||||
*/
|
||||
public static synchronized PMDASMClassLoader getInstance(ClassLoader parent) {
|
||||
if (parent == cachedClassLoader) return cachedPMDASMClassLoader;
|
||||
cachedClassLoader = parent;
|
||||
cachedPMDASMClassLoader = new PMDASMClassLoader(parent);
|
||||
return cachedPMDASMClassLoader;
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
private PMDASMClassLoader(ClassLoader parent) {
|
||||
super(parent);
|
||||
}
|
||||
|
||||
/** Caches the names of the classes that we can't load or that don't exist. */
|
||||
private Set<String> dontBother = Collections.synchronizedSet(new HashSet<String>());
|
||||
private final Set<String> dontBother = new HashSet<String>();
|
||||
|
||||
@Override
|
||||
public Class<?> loadClass(String name) throws ClassNotFoundException {
|
||||
public synchronized Class<?> loadClass(String name) throws ClassNotFoundException {
|
||||
if (dontBother.contains(name)) {
|
||||
throw new ClassNotFoundException(name);
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ public class PMDASMClassLoaderTest {
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
cl = new PMDASMClassLoader(getClass().getClassLoader());
|
||||
cl = PMDASMClassLoader.getInstance(getClass().getClassLoader());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -78,7 +78,7 @@ public class PMDASMClassLoaderTest {
|
||||
@Test
|
||||
public void testCachingOfNotFoundClasses() throws Exception {
|
||||
MockedClassLoader mockedClassloader = new MockedClassLoader();
|
||||
PMDASMClassLoader cl = new PMDASMClassLoader(mockedClassloader);
|
||||
PMDASMClassLoader cl = PMDASMClassLoader.getInstance(mockedClassloader);
|
||||
String notExistingClassname = "that.clazz.doesnot.Exist";
|
||||
try {
|
||||
cl.loadClass(notExistingClassname);
|
||||
@ -116,7 +116,7 @@ public class PMDASMClassLoaderTest {
|
||||
@Test
|
||||
public void testCachingMemoryConsumption() throws Exception {
|
||||
MockedClassLoader mockedClassLoader = new MockedClassLoader();
|
||||
PMDASMClassLoader cl = new PMDASMClassLoader(mockedClassLoader);
|
||||
PMDASMClassLoader cl = PMDASMClassLoader.getInstance(mockedClassLoader);
|
||||
|
||||
Runtime runtime = Runtime.getRuntime();
|
||||
System.gc();
|
||||
|
Reference in New Issue
Block a user