diff --git a/pmd-eclipse/build.properties b/pmd-eclipse/build.properties new file mode 100644 index 0000000000..16e20bc74b --- /dev/null +++ b/pmd-eclipse/build.properties @@ -0,0 +1,2 @@ +source.pmd-eclipse.jar = src/ +source.pmd.jar = diff --git a/pmd-eclipse/plugin.xml b/pmd-eclipse/plugin.xml new file mode 100644 index 0000000000..7d8ca6c715 --- /dev/null +++ b/pmd-eclipse/plugin.xml @@ -0,0 +1,66 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/PMDPlugin.java b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/PMDPlugin.java new file mode 100644 index 0000000000..5afffa1d59 --- /dev/null +++ b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/PMDPlugin.java @@ -0,0 +1,63 @@ +package net.sourceforge.pmd.eclipse; + +import org.eclipse.ui.plugin.*; +import org.eclipse.core.runtime.*; +import org.eclipse.core.resources.*; +import java.util.*; + +/** + * The main plugin class to be used in the desktop. + */ +public class PMDPlugin extends AbstractUIPlugin { + //The shared instance. + private static PMDPlugin plugin; + //Resource bundle. + private ResourceBundle resourceBundle; + + /** + * The constructor. + */ + public PMDPlugin(IPluginDescriptor descriptor) { + super(descriptor); + plugin = this; + try { + resourceBundle= ResourceBundle.getBundle("net.sourceforge.pmd.eclipse.PMDPluginResources"); + } catch (MissingResourceException x) { + resourceBundle = null; + } + } + + /** + * Returns the shared instance. + */ + public static PMDPlugin getDefault() { + return plugin; + } + + /** + * Returns the workspace instance. + */ + public static IWorkspace getWorkspace() { + return ResourcesPlugin.getWorkspace(); + } + + /** + * Returns the string from the plugin's resource bundle, + * or 'key' if not found. + */ + public static String getResourceString(String key) { + ResourceBundle bundle= PMDPlugin.getDefault().getResourceBundle(); + try { + return bundle.getString(key); + } catch (MissingResourceException e) { + return key; + } + } + + /** + * Returns the plugin's resource bundle, + */ + public ResourceBundle getResourceBundle() { + return resourceBundle; + } +} diff --git a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/PMDVisitor.java b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/PMDVisitor.java new file mode 100644 index 0000000000..4875b55075 --- /dev/null +++ b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/PMDVisitor.java @@ -0,0 +1,95 @@ +package net.sourceforge.pmd.eclipse; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.Reader; +import java.util.Iterator; + +import net.sourceforge.pmd.PMD; +import net.sourceforge.pmd.Report; +import net.sourceforge.pmd.RuleContext; +import net.sourceforge.pmd.RuleSet; +import net.sourceforge.pmd.RuleSetFactory; +import net.sourceforge.pmd.RuleViolation; +import net.sourceforge.pmd.eclipse.preferences.PMDPreferencePage; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IMarker; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.IResourceVisitor; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.jface.preference.IPreferenceStore; + +/** + * @author David Dixon-Peugh + * + * This class visits all of the resources in the Eclipse + * Workspace, and runs PMD on them if they happen to be + * Java files. + * + * Any violations get tagged onto the file as notes. + */ +public class PMDVisitor implements IResourceVisitor { + private PMD pmd = null; + private RuleSet ruleSet = null; + + /** + * No Argument Constructor + */ + public PMDVisitor(String ruleSetFile) + throws IOException + { + try { + pmd = new PMD(); + RuleSetFactory factory = new RuleSetFactory(); + ruleSet = + factory.createRuleSet(new FileInputStream(ruleSetFile)); + } catch (Throwable e) { + e.printStackTrace(); + } + + } + + private void runPMD( IFile file ) throws CoreException + { + Reader input = + new InputStreamReader( file.getContents() ); + RuleContext context = new RuleContext(); + context.setSourceCodeFilename( file.getName() ); + context.setReport( new Report() ); + + try { + pmd.processFile( input, ruleSet, context); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + + Iterator iter = context.getReport().iterator(); + while (iter.hasNext()) { + RuleViolation violation = (RuleViolation) iter.next(); + + IMarker marker = file.createMarker(IMarker.TASK); + marker.setAttribute( IMarker.MESSAGE, + violation.getDescription() ); + marker.setAttribute( IMarker.LINE_NUMBER, + violation.getLine() ); + + } + } + + /** + * @see org.eclipse.core.resources.IResourceVisitor#visit(IResource) + */ + public boolean visit(IResource resource) throws CoreException { + if ((resource instanceof IFile) && + (((IFile) resource).getFileExtension() != null) && + ((IFile) resource).getFileExtension().equals("java")) { + runPMD( (IFile) resource ); + return false; + } else { + return true; + } + } +} diff --git a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/actions/PMDAction.java b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/actions/PMDAction.java new file mode 100644 index 0000000000..ba3737489e --- /dev/null +++ b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/actions/PMDAction.java @@ -0,0 +1,68 @@ +package net.sourceforge.pmd.eclipse.actions; + +import java.io.IOException; + +import net.sourceforge.pmd.eclipse.PMDPlugin; +import net.sourceforge.pmd.eclipse.PMDVisitor; +import net.sourceforge.pmd.eclipse.preferences.PMDPreferencePage; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.jface.action.IAction; +import org.eclipse.jface.preference.IPreferenceStore; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.ui.IWorkbenchWindow; +import org.eclipse.ui.IWorkbenchWindowActionDelegate; + +/** + * Insert the type's description here. + * @see IWorkbenchWindowActionDelegate + */ +public class PMDAction implements IWorkbenchWindowActionDelegate { + /** + * The constructor. + */ + public PMDAction() { + } + + /** + * Insert the method's description here. + * @see IWorkbenchWindowActionDelegate#run + */ + public void run(IAction action) { + IPreferenceStore pref = + PMDPlugin.getDefault().getPreferenceStore(); + + String rulesetFile = + pref.getString(PMDPreferencePage.P_RULESETS); + + PMDVisitor visitor = null; + try { + visitor = new PMDVisitor(rulesetFile); + PMDPlugin.getWorkspace().getRoot().accept( visitor ); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * Insert the method's description here. + * @see IWorkbenchWindowActionDelegate#selectionChanged + */ + public void selectionChanged(IAction action, ISelection selection) { + } + + /** + * Insert the method's description here. + * @see IWorkbenchWindowActionDelegate#dispose + */ + public void dispose() { + } + + /** + * Insert the method's description here. + * @see IWorkbenchWindowActionDelegate#init + */ + public void init(IWorkbenchWindow window) { + } +} diff --git a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/preferences/PMDPreferencePage.java b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/preferences/PMDPreferencePage.java new file mode 100644 index 0000000000..2bca996da8 --- /dev/null +++ b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/preferences/PMDPreferencePage.java @@ -0,0 +1,57 @@ +package net.sourceforge.pmd.eclipse.preferences; + +import org.eclipse.jface.preference.*; +import org.eclipse.ui.IWorkbenchPreferencePage; +import org.eclipse.ui.IWorkbench; +import net.sourceforge.pmd.eclipse.PMDPlugin; +import org.eclipse.jface.preference.IPreferenceStore; + +/** + * This class represents a preference page that + * is contributed to the Preferences dialog. By + * subclassing FieldEditorPreferencePage, we + * can use the field support built into JFace that allows + * us to create a page that is small and knows how to + * save, restore and apply itself. + *

+ * This page is used to modify preferences only. They + * are stored in the preference store that belongs to + * the main plug-in class. That way, preferences can + * be accessed directly via the preference store. + */ + +public class PMDPreferencePage + extends FieldEditorPreferencePage + implements IWorkbenchPreferencePage { + public static final String P_RULESETS = + "net.sourceforge.pmd.eclipse.rulesets"; + + public PMDPreferencePage() { + super(GRID); + setPreferenceStore(PMDPlugin.getDefault().getPreferenceStore()); + setDescription("PMD Configuration Options"); + initializeDefaults(); + } +/** + * Sets the default values of the preferences. + */ + private void initializeDefaults() { + IPreferenceStore store = getPreferenceStore(); + store.setDefault(P_RULESETS, "rulesets/basic.xml"); + } + +/** + * Creates the field editors. Field editors are abstractions of + * the common GUI blocks needed to manipulate various types + * of preferences. Each field editor knows how to save and + * restore itself. + */ + + public void createFieldEditors() { + addField( new StringFieldEditor( P_RULESETS, + "&Ruleset", getFieldEditorParent() )); + } + + public void init(IWorkbench workbench) { + } +} \ No newline at end of file