forked from phoedos/pmd
First Version
git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@401 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
2
pmd-eclipse/build.properties
Normal file
2
pmd-eclipse/build.properties
Normal file
@ -0,0 +1,2 @@
|
||||
source.pmd-eclipse.jar = src/
|
||||
source.pmd.jar =
|
66
pmd-eclipse/plugin.xml
Normal file
66
pmd-eclipse/plugin.xml
Normal file
@ -0,0 +1,66 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<plugin
|
||||
id="net.sourceforge.pmd.eclipse"
|
||||
name="pmd-eclipse Plug-in"
|
||||
version="0.1.0"
|
||||
provider-name="PMD"
|
||||
class="net.sourceforge.pmd.eclipse.PMDPlugin">
|
||||
|
||||
<runtime>
|
||||
<library name="pmd-eclipse.jar">
|
||||
<export name="*"/>
|
||||
</library>
|
||||
<library name="lib/pmd-0.5.jar">
|
||||
<export name="*"/>
|
||||
</library>
|
||||
</runtime>
|
||||
<requires>
|
||||
<import plugin="org.eclipse.core.resources"/>
|
||||
<import plugin="org.eclipse.ui"/>
|
||||
<import plugin="org.apache.xerces"/>
|
||||
</requires>
|
||||
|
||||
|
||||
<extension
|
||||
point="org.eclipse.ui.actionSets">
|
||||
<actionSet
|
||||
label="Run PMD"
|
||||
visible="true"
|
||||
id="net.sourceforge.pmd.eclipse.actionSet">
|
||||
<menu
|
||||
label="&PMD"
|
||||
id="sampleMenu">
|
||||
<separator
|
||||
name="sampleGroup">
|
||||
</separator>
|
||||
</menu>
|
||||
<action
|
||||
label="&PMD"
|
||||
icon="icons/sample.gif"
|
||||
tooltip="Run PMD"
|
||||
class="net.sourceforge.pmd.eclipse.actions.PMDAction"
|
||||
menubarPath="sampleMenu/sampleGroup"
|
||||
toolbarPath="sampleGroup"
|
||||
id="net.sourceforge.pmd.eclipse.actions.PMDAction">
|
||||
</action>
|
||||
</actionSet>
|
||||
</extension>
|
||||
<extension
|
||||
point="org.eclipse.ui.perspectiveExtensions">
|
||||
<perspectiveExtension
|
||||
targetID="org.eclipse.ui.resourcePerspective">
|
||||
<actionSet
|
||||
id="net.sourceforge.pmd.eclipse.actionSet">
|
||||
</actionSet>
|
||||
</perspectiveExtension>
|
||||
</extension>
|
||||
<extension
|
||||
point="org.eclipse.ui.preferencePages">
|
||||
<page
|
||||
name="PMD"
|
||||
class="net.sourceforge.pmd.eclipse.preferences.PMDPreferencePage"
|
||||
id="net.sourceforge.pmd.eclipse.preferences.PMDPreferencePage">
|
||||
</page>
|
||||
</extension>
|
||||
|
||||
</plugin>
|
63
pmd-eclipse/src/net/sourceforge/pmd/eclipse/PMDPlugin.java
Normal file
63
pmd-eclipse/src/net/sourceforge/pmd/eclipse/PMDPlugin.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
95
pmd-eclipse/src/net/sourceforge/pmd/eclipse/PMDVisitor.java
Normal file
95
pmd-eclipse/src/net/sourceforge/pmd/eclipse/PMDVisitor.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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) {
|
||||
}
|
||||
}
|
@ -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 <samp>FieldEditorPreferencePage</samp>, 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.
|
||||
* <p>
|
||||
* 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) {
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user