Skip to content

Commit 156ec30

Browse files
committed
Cache and reuse project's aux classpath classloader
1 parent 34287f4 commit 156ec30

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/plugin/PMDPlugin.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ public Color colorFor(RGB rgb) {
138138
return color;
139139
}
140140

141+
@Deprecated // use IProjectProperties#getAuxclasspath() instead
141142
public static void setJavaClassLoader(PMDConfiguration config, IProject project) {
142143

143144
IPreferences preferences = getDefault().loadPreferences();

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/runtime/cmd/BaseVisitor.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,9 @@ protected final void reviewResource(IResource resource) {
288288
}
289289
LOG.debug("discovered language: " + languageVersion);
290290

291-
PMDPlugin.setJavaClassLoader(configuration(), resource.getProject());
291+
if (PMDPlugin.getDefault().loadPreferences().isProjectBuildPathEnabled()) {
292+
configuration().setClassLoader(projectProperties.getAuxClasspath());
293+
}
292294

293295
final File sourceCodeFile = file.getRawLocation().toFile();
294296
if (included && getRuleSet().applies(sourceCodeFile) && isFileInWorkingSet(file)

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/runtime/properties/IProjectProperties.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,13 @@ public interface IProjectProperties {
198198
* @return include patterns
199199
*/
200200
Set<String> getBuildPathIncludePatterns();
201+
202+
/**
203+
* Determines the auxiliary classpath needed for type resolution.
204+
* The classloader is cached and used for all PMD executions for the same project.
205+
* The classloader is not stored to the project properties file.
206+
*
207+
* @return the classpath or <code>null</code> if the project is not a java project
208+
*/
209+
ClassLoader getAuxClasspath();
201210
}

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/runtime/properties/impl/ProjectPropertiesImpl.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,13 @@
5151
import org.eclipse.core.runtime.IPath;
5252
import org.eclipse.core.runtime.Path;
5353
import org.eclipse.jdt.core.IClasspathEntry;
54+
import org.eclipse.jdt.core.JavaCore;
5455
import org.eclipse.ui.IWorkingSet;
5556

57+
import net.sourceforge.pmd.PMD;
5658
import net.sourceforge.pmd.RuleSet;
5759
import net.sourceforge.pmd.eclipse.plugin.PMDPlugin;
60+
import net.sourceforge.pmd.eclipse.runtime.cmd.JavaProjectClassLoader;
5861
import net.sourceforge.pmd.eclipse.runtime.properties.IProjectProperties;
5962
import net.sourceforge.pmd.eclipse.runtime.properties.IProjectPropertiesManager;
6063
import net.sourceforge.pmd.eclipse.runtime.properties.PropertiesException;
@@ -89,6 +92,7 @@ public class ProjectPropertiesImpl implements IProjectProperties {
8992
private boolean fullBuildEnabled = true; // default in case didn't come from properties
9093
private Set<String> buildPathExcludePatterns = new HashSet<String>();
9194
private Set<String> buildPathIncludePatterns = new HashSet<String>();
95+
private ClassLoader auxclasspath;
9296

9397
/**
9498
* The default constructor takes a project as an argument
@@ -452,4 +456,21 @@ public Set<String> getBuildPathExcludePatterns() {
452456
public Set<String> getBuildPathIncludePatterns() {
453457
return buildPathIncludePatterns;
454458
}
459+
460+
@Override
461+
public ClassLoader getAuxClasspath() {
462+
try {
463+
if (project != null && project.hasNature(JavaCore.NATURE_ID)) {
464+
if (auxclasspath == null) {
465+
LOG.debug("Creating new auxclaspath class loader for project " + project.getName());
466+
auxclasspath = new JavaProjectClassLoader(PMD.class.getClassLoader(), project);
467+
}
468+
return auxclasspath;
469+
}
470+
} catch (CoreException e) {
471+
LOG.error("Error determining aux classpath", e);
472+
PMDPlugin.getDefault().logError("Error determining aux classpath", e);
473+
}
474+
return null;
475+
}
455476
}

0 commit comments

Comments
 (0)