forked from phoedos/pmd
Add JavaProjectClassLoader which serves as a ClassLoader for an IJavaProject. Pass this ClassLoader to the PMD instance.
git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@5822 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
@@ -37,8 +37,14 @@ package net.sourceforge.pmd.runtime.cmd;
|
||||
|
||||
import name.herlin.command.AbstractProcessableCommand;
|
||||
import name.herlin.command.CommandException;
|
||||
import net.sourceforge.pmd.PMD;
|
||||
import net.sourceforge.pmd.SourceType;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.eclipse.core.resources.IProject;
|
||||
import org.eclipse.core.runtime.IProgressMonitor;
|
||||
import org.eclipse.jdt.core.IJavaProject;
|
||||
import org.eclipse.jdt.core.JavaCore;
|
||||
|
||||
/**
|
||||
* This is a base implementation for a command inside the PMD plugin.
|
||||
@@ -74,6 +80,8 @@ import org.eclipse.core.runtime.IProgressMonitor;
|
||||
*
|
||||
*/
|
||||
public abstract class AbstractDefaultCommand extends AbstractProcessableCommand {
|
||||
private static final Logger log = Logger.getLogger(AbstractDefaultCommand.class);
|
||||
|
||||
private boolean readOnly;
|
||||
private boolean outputProperties;
|
||||
private boolean readyToExecute;
|
||||
@@ -264,4 +272,31 @@ public abstract class AbstractDefaultCommand extends AbstractProcessableCommand
|
||||
this.monitor.worked(work);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a PMD Engine for that project. The engine is parameterized
|
||||
* according to the target JDK of that project.
|
||||
*
|
||||
* @param project
|
||||
* @return
|
||||
*/
|
||||
protected PMD getPmdEngineForProject(final IProject project) throws CommandException {
|
||||
final IJavaProject javaProject = JavaCore.create(project);
|
||||
final PMD pmdEngine = new PMD();
|
||||
|
||||
if (javaProject.exists()) {
|
||||
final String compilerCompliance = javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true);
|
||||
log.debug("compilerCompliance = " + compilerCompliance);
|
||||
final SourceType s = SourceType.getSourceTypeForId("java " + compilerCompliance);
|
||||
if (s != null) {
|
||||
pmdEngine.setJavaVersion(s);
|
||||
} else {
|
||||
throw new CommandException("The target JDK, " + compilerCompliance + " is not yet supported"); // TODO NLS
|
||||
}
|
||||
pmdEngine.setClassLoader(new JavaProjectClassLoader(pmdEngine.getClassLoader(), javaProject));
|
||||
} else {
|
||||
throw new CommandException("The project " + project.getName() + " is not a Java project"); // TODO NLS
|
||||
}
|
||||
return pmdEngine;
|
||||
}
|
||||
}
|
@@ -0,0 +1,105 @@
|
||||
package net.sourceforge.pmd.runtime.cmd;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.eclipse.core.resources.IProject;
|
||||
import org.eclipse.core.resources.IWorkspaceRoot;
|
||||
import org.eclipse.core.runtime.IPath;
|
||||
import org.eclipse.jdt.core.IClasspathEntry;
|
||||
import org.eclipse.jdt.core.IJavaProject;
|
||||
import org.eclipse.jdt.core.JavaCore;
|
||||
import org.eclipse.jdt.core.JavaModelException;
|
||||
|
||||
/**
|
||||
* This is a ClassLoader for the Build Path of an IJavaProject.
|
||||
*/
|
||||
public class JavaProjectClassLoader extends URLClassLoader {
|
||||
private static final Logger log = Logger.getLogger(ReviewCodeCmd.class);
|
||||
|
||||
private Set javaProjects = new HashSet();
|
||||
private IWorkspaceRoot workspaceRoot;
|
||||
|
||||
public JavaProjectClassLoader(ClassLoader parent, IJavaProject javaProject) {
|
||||
super(new URL[0], parent);
|
||||
workspaceRoot = javaProject.getProject().getWorkspace().getRoot();
|
||||
addURLs(javaProject, false);
|
||||
|
||||
// No longer need these things, drop references
|
||||
javaProjects = null;
|
||||
workspaceRoot = null;
|
||||
}
|
||||
|
||||
private void addURLs(IJavaProject javaProject, boolean exportsOnly) {
|
||||
if (!javaProjects.contains(javaProject)) {
|
||||
javaProjects.add(javaProject);
|
||||
|
||||
try {
|
||||
// Add default output location
|
||||
addURL(javaProject.getOutputLocation());
|
||||
|
||||
// Add each classpath entry
|
||||
IClasspathEntry[] classpathEntries = javaProject.getResolvedClasspath(true);
|
||||
for (int i = 0; i < classpathEntries.length; i++) {
|
||||
IClasspathEntry classpathEntry = classpathEntries[i];
|
||||
if (classpathEntry.isExported() || !exportsOnly) {
|
||||
switch (classpathEntry.getEntryKind()) {
|
||||
|
||||
// Recurse on projects
|
||||
case IClasspathEntry.CPE_PROJECT:
|
||||
IProject project = javaProject.getProject().getWorkspace().getRoot().getProject(
|
||||
classpathEntry.getPath().toString());
|
||||
IJavaProject javaProj = JavaCore.create(project);
|
||||
if (javaProj != null) {
|
||||
addURLs(javaProj, true);
|
||||
}
|
||||
break;
|
||||
|
||||
// Library
|
||||
case IClasspathEntry.CPE_LIBRARY:
|
||||
addURL(classpathEntry);
|
||||
break;
|
||||
|
||||
// Only Source entries with custom output location need to be added
|
||||
case IClasspathEntry.CPE_SOURCE:
|
||||
IPath outputLocation = classpathEntry.getOutputLocation();
|
||||
if (outputLocation != null) {
|
||||
addURL(outputLocation);
|
||||
}
|
||||
break;
|
||||
|
||||
// Variable and Container entries should not be happening, because we've asked for resolved entries.
|
||||
case IClasspathEntry.CPE_VARIABLE:
|
||||
case IClasspathEntry.CPE_CONTAINER:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (JavaModelException e) {
|
||||
log.debug("MalformedURLException occurred: " + e.getLocalizedMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void addURL(IClasspathEntry classpathEntry) {
|
||||
addURL(classpathEntry.getPath());
|
||||
}
|
||||
|
||||
private void addURL(IPath path) {
|
||||
try {
|
||||
if (workspaceRoot.exists(path)) {
|
||||
// path = workspaceRoot.getFileForLocation(path).getFullPath();
|
||||
//path = workspaceRoot.getFile(path).getFullPath();
|
||||
path = workspaceRoot.getLocation().append(path);
|
||||
}
|
||||
URL url = path.toFile().getAbsoluteFile().toURI().toURL();
|
||||
addURL(url);
|
||||
} catch (MalformedURLException e) {
|
||||
log.debug("MalformedURLException occurred: " + e.getLocalizedMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
@@ -46,7 +46,6 @@ import name.herlin.command.CommandException;
|
||||
import name.herlin.command.Timer;
|
||||
import net.sourceforge.pmd.PMD;
|
||||
import net.sourceforge.pmd.RuleSet;
|
||||
import net.sourceforge.pmd.SourceType;
|
||||
import net.sourceforge.pmd.runtime.PMDRuntimeConstants;
|
||||
import net.sourceforge.pmd.runtime.PMDRuntimePlugin;
|
||||
import net.sourceforge.pmd.runtime.properties.IProjectProperties;
|
||||
@@ -312,34 +311,6 @@ public class ReviewCodeCmd extends AbstractDefaultCommand {
|
||||
return rule;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a PMD Engine for that project. The engine is parameterized
|
||||
* according to the target JDK of that project.
|
||||
*
|
||||
* @param project
|
||||
* @return
|
||||
*/
|
||||
private PMD getPmdEngineForProject(final IProject project) throws CommandException {
|
||||
final IJavaProject javaProject = JavaCore.create(project);
|
||||
final PMD pmdEngine = new PMD();
|
||||
|
||||
if (javaProject.exists()) {
|
||||
final String compilerCompliance = javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true);
|
||||
log.debug("compilerCompliance = " + compilerCompliance);
|
||||
final SourceType s = SourceType.getSourceTypeForId("java " + compilerCompliance);
|
||||
if (s != null) {
|
||||
pmdEngine.setJavaVersion(s);
|
||||
} else {
|
||||
throw new CommandException("The target JDK, " + compilerCompliance + " is not yet supported"); // TODO:
|
||||
// NLS
|
||||
}
|
||||
} else {
|
||||
throw new CommandException("The project " + project.getName() + " is not a Java project"); // TODO:
|
||||
// NLS
|
||||
}
|
||||
return pmdEngine;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the list of workbench resources
|
||||
*
|
||||
|
@@ -43,16 +43,6 @@ import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.eclipse.core.resources.IFile;
|
||||
import org.eclipse.core.resources.IProject;
|
||||
import org.eclipse.core.resources.IResource;
|
||||
import org.eclipse.core.runtime.CoreException;
|
||||
import org.eclipse.jdt.core.IJavaProject;
|
||||
import org.eclipse.jdt.core.JavaCore;
|
||||
import org.eclipse.swt.widgets.Display;
|
||||
import org.eclipse.ui.IPropertyListener;
|
||||
|
||||
import name.herlin.command.CommandException;
|
||||
import net.sourceforge.pmd.PMD;
|
||||
import net.sourceforge.pmd.PMDException;
|
||||
@@ -60,9 +50,16 @@ import net.sourceforge.pmd.Report;
|
||||
import net.sourceforge.pmd.Rule;
|
||||
import net.sourceforge.pmd.RuleContext;
|
||||
import net.sourceforge.pmd.RuleSet;
|
||||
import net.sourceforge.pmd.SourceType;
|
||||
import net.sourceforge.pmd.runtime.PMDRuntimeConstants;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.eclipse.core.resources.IFile;
|
||||
import org.eclipse.core.resources.IProject;
|
||||
import org.eclipse.core.resources.IResource;
|
||||
import org.eclipse.core.runtime.CoreException;
|
||||
import org.eclipse.swt.widgets.Display;
|
||||
import org.eclipse.ui.IPropertyListener;
|
||||
|
||||
/**
|
||||
* This command reviews a resource - a file - for a specific rule.
|
||||
*
|
||||
@@ -123,34 +120,6 @@ public class ReviewResourceForRuleCommand extends AbstractDefaultCommand {
|
||||
this.listenerList = new ArrayList();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a PMD Engine for that project. The engine is parameterized
|
||||
* according to the target JDK of that project.
|
||||
*
|
||||
* @param project
|
||||
* @return
|
||||
*/
|
||||
private PMD getPmdEngineForProject(final IProject project) throws CommandException {
|
||||
final IJavaProject javaProject = JavaCore.create(project);
|
||||
final PMD pmdEngine = new PMD();
|
||||
|
||||
if (javaProject.exists()) {
|
||||
final String compilerCompliance = javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true);
|
||||
log.debug("compilerCompliance = " + compilerCompliance);
|
||||
final SourceType s = SourceType.getSourceTypeForId("java " + compilerCompliance);
|
||||
if (s != null) {
|
||||
pmdEngine.setJavaVersion(s);
|
||||
} else {
|
||||
throw new CommandException("The target JDK, " + compilerCompliance + " is not yet supported"); // TODO:
|
||||
// NLS
|
||||
}
|
||||
} else {
|
||||
throw new CommandException("The project " + project.getName() + " is not a Java project"); // TODO:
|
||||
// NLS
|
||||
}
|
||||
return pmdEngine;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see net.sourceforge.pmd.runtime.cmd.AbstractDefaultCommand#execute()
|
||||
*/
|
||||
|
Reference in New Issue
Block a user