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) {
|
public ClassTypeResolver(ClassLoader classLoader) {
|
||||||
pmdClassLoader = new PMDASMClassLoader(classLoader);
|
pmdClassLoader = PMDASMClassLoader.getInstance(classLoader);
|
||||||
}
|
}
|
||||||
|
|
||||||
// FUTURE ASTCompilationUnit should not be a TypeNode. Clean this up accordingly.
|
// FUTURE ASTCompilationUnit should not be a TypeNode. Clean this up accordingly.
|
||||||
|
@ -30,16 +30,32 @@ import org.objectweb.asm.ClassReader;
|
|||||||
* then the resource foo/Bar.class will not exist, too.
|
* then the resource foo/Bar.class will not exist, too.
|
||||||
*/
|
*/
|
||||||
public class PMDASMClassLoader extends ClassLoader {
|
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);
|
super(parent);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Caches the names of the classes that we can't load or that don't exist. */
|
/** 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
|
@Override
|
||||||
public Class<?> loadClass(String name) throws ClassNotFoundException {
|
public synchronized Class<?> loadClass(String name) throws ClassNotFoundException {
|
||||||
if (dontBother.contains(name)) {
|
if (dontBother.contains(name)) {
|
||||||
throw new ClassNotFoundException(name);
|
throw new ClassNotFoundException(name);
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@ public class PMDASMClassLoaderTest {
|
|||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
cl = new PMDASMClassLoader(getClass().getClassLoader());
|
cl = PMDASMClassLoader.getInstance(getClass().getClassLoader());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -78,7 +78,7 @@ public class PMDASMClassLoaderTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testCachingOfNotFoundClasses() throws Exception {
|
public void testCachingOfNotFoundClasses() throws Exception {
|
||||||
MockedClassLoader mockedClassloader = new MockedClassLoader();
|
MockedClassLoader mockedClassloader = new MockedClassLoader();
|
||||||
PMDASMClassLoader cl = new PMDASMClassLoader(mockedClassloader);
|
PMDASMClassLoader cl = PMDASMClassLoader.getInstance(mockedClassloader);
|
||||||
String notExistingClassname = "that.clazz.doesnot.Exist";
|
String notExistingClassname = "that.clazz.doesnot.Exist";
|
||||||
try {
|
try {
|
||||||
cl.loadClass(notExistingClassname);
|
cl.loadClass(notExistingClassname);
|
||||||
@ -116,7 +116,7 @@ public class PMDASMClassLoaderTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testCachingMemoryConsumption() throws Exception {
|
public void testCachingMemoryConsumption() throws Exception {
|
||||||
MockedClassLoader mockedClassLoader = new MockedClassLoader();
|
MockedClassLoader mockedClassLoader = new MockedClassLoader();
|
||||||
PMDASMClassLoader cl = new PMDASMClassLoader(mockedClassLoader);
|
PMDASMClassLoader cl = PMDASMClassLoader.getInstance(mockedClassLoader);
|
||||||
|
|
||||||
Runtime runtime = Runtime.getRuntime();
|
Runtime runtime = Runtime.getRuntime();
|
||||||
System.gc();
|
System.gc();
|
||||||
|
Reference in New Issue
Block a user