Merge pull request #11 from rsalvador/PMDASTClassLoader

Reuse PMDASMClassLoader instance across all compilation units analyzed.
This commit is contained in:
Pelisse Romain
2012-11-28 11:17:09 -08:00
3 changed files with 23 additions and 7 deletions

View File

@ -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.

View File

@ -30,16 +30,32 @@ import org.objectweb.asm.ClassReader;
* then the resource foo/Bar.class will not exist, too.
*/
public class PMDASMClassLoader extends ClassLoader {
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;
}
//
public PMDASMClassLoader(ClassLoader parent) {
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);
}

View File

@ -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();