Refactoring to improve performance

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@1934 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Philippe Herlin
2003-05-19 22:27:33 +00:00
parent 2aee4db4fd
commit 91b2b629b3
7 changed files with 245 additions and 30 deletions

View File

@ -1,5 +1,7 @@
package net.sourceforge.pmd.eclipse;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.core.resources.IFile;
@ -15,6 +17,9 @@ import org.eclipse.core.runtime.IProgressMonitor;
* @author Philippe Herlin
* @version $Revision$
* $Log$
* Revision 1.4 2003/05/19 22:27:32 phherlin
* Refactoring to improve performance
*
* Revision 1.3 2003/03/30 20:44:27 phherlin
* Adding logging
*
@ -23,6 +28,7 @@ public class PMDDeltaVisitor implements IResourceDeltaVisitor {
private static final Log log = LogFactory.getLog("net.sourceforge.pmd.eclipse.PMDDeltaVisitor");
private IProgressMonitor monitor;
private boolean useTaskMarker = false;
private Map accumulator;
/**
* Default construtor
@ -84,9 +90,16 @@ public class PMDDeltaVisitor implements IResourceDeltaVisitor {
if ((resource instanceof IFile)
&& (((IFile) resource).getFileExtension() != null)
&& ((IFile) resource).getFileExtension().equals("java")) {
if (monitor != null) monitor.subTask(((IFile) resource).getName());
PMDProcessor.getInstance().run((IFile) resource, useTaskMarker);
if (monitor != null) monitor.worked(1);
if (monitor != null) {
monitor.subTask(((IFile) resource).getName());
}
PMDProcessor.getInstance().run((IFile) resource, useTaskMarker, getAccumulator());
if (monitor != null) {
monitor.worked(1);
}
}
}
@ -122,4 +135,20 @@ public class PMDDeltaVisitor implements IResourceDeltaVisitor {
this.useTaskMarker = useTaskMarker;
}
/**
* Returns the accumulator.
* @return Map
*/
public Map getAccumulator() {
return accumulator;
}
/**
* Sets the accumulator.
* @param accumulator The accumulator to set
*/
public void setAccumulator(Map accumulator) {
this.accumulator = accumulator;
}
}

View File

@ -1,5 +1,7 @@
package net.sourceforge.pmd.eclipse;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.core.resources.IFile;
@ -20,6 +22,9 @@ import org.eclipse.core.runtime.IProgressMonitor;
* @version $Revision$
*
* $Log$
* Revision 1.12 2003/05/19 22:27:32 phherlin
* Refactoring to improve performance
*
* Revision 1.11 2003/03/30 20:47:03 phherlin
* Adding logging
*
@ -31,12 +36,14 @@ public class PMDVisitor implements IResourceVisitor {
private static final Log log = LogFactory.getLog("net.sourceforge.pmd.eclipse.PMDVisitor");
private IProgressMonitor monitor;
private boolean useTaskMarker = false;
private Map accumulator;
/**
* Construct with a progress monitor
* @param monitor a progress indicator
*/
public PMDVisitor(IProgressMonitor monitor) {
log.debug("Instanciating a new visitor");
this.monitor = monitor;
}
@ -51,9 +58,17 @@ public class PMDVisitor implements IResourceVisitor {
if ((resource instanceof IFile)
&& (((IFile) resource).getFileExtension() != null)
&& ((IFile) resource).getFileExtension().equals("java")) {
if (monitor != null) monitor.subTask(((IFile) resource).getName());
PMDProcessor.getInstance().run((IFile) resource, useTaskMarker);
if (monitor != null) monitor.worked(1);
if (monitor != null) {
monitor.subTask(((IFile) resource).getName());
}
PMDProcessor.getInstance().run((IFile) resource, useTaskMarker, getAccumulator());
if (monitor != null) {
monitor.worked(1);
}
fVisitChildren = false;
}
} else {
@ -78,4 +93,22 @@ public class PMDVisitor implements IResourceVisitor {
this.useTaskMarker = useTaskMarker;
}
/**
* Returns the accumulator.
* @return Map
*/
public Map getAccumulator() {
log.debug("Returning the accumulator " + accumulator);
return accumulator;
}
/**
* Sets the accumulator.
* @param accumulator The accumulator to set
*/
public void setAccumulator(Map accumulator) {
this.accumulator = accumulator;
log.debug("Setting accumulator " + accumulator);
}
}

View File

@ -0,0 +1,122 @@
package net.sourceforge.pmd.eclipse;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.core.internal.resources.MarkerInfo;
import org.eclipse.core.internal.resources.MarkerManager;
import org.eclipse.core.internal.resources.Workspace;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.runtime.CoreException;
/**
* This class is intended to run visitor objects. It's purpose is to
* factor the code arround a call to a visitor.
*
* @author phherlin
* @version $Revision$
*
* $Log$
* Revision 1.1 2003/05/19 22:27:33 phherlin
* Refactoring to improve performance
*
*
*/
public class PMDVisitorRunner {
private static final Log log = LogFactory.getLog("net.sourceforge.pmd.eclipse.PMDVisitorRunner");
/**
* Visit a resource
* @param resource the resource to visit
* @param visitor the visitor
*/
public void run(IResource resource, PMDVisitor visitor) throws CoreException {
Map markerDirectives = new HashMap();
visitor.setAccumulator(markerDirectives);
resource.accept(visitor);
applyDirectives(markerDirectives);
markerDirectives.clear();
}
/**
* Visit a project
* Note: A project is not resource, ie. doesn't extends IResource but
* accept resource visitors such as PMDVisitor
* @param project the project to visit
* @param visitor the visitor
*/
public void run(IProject project, PMDVisitor visitor) throws CoreException {
Map markerDirectives = new HashMap();
visitor.setAccumulator(markerDirectives);
project.accept(visitor);
applyDirectives(markerDirectives);
markerDirectives.clear();
}
/**
* Visit a resource delta
* @param resourceDelta a set of resources to visit
* @param visitor the visitor
*/
public void run(IResourceDelta resourceDelta, PMDDeltaVisitor visitor) throws CoreException {
Map markerDirectives = new HashMap();
visitor.setAccumulator(markerDirectives);
resourceDelta.accept(visitor);
applyDirectives(markerDirectives);
markerDirectives.clear();
}
/**
* Apply new markers to selected files
* @param markerDirectives map of new marker informations
*/
private void applyDirectives(Map markerDirectives) throws CoreException {
try {
log.info("Processing marker directives");
Set filesSet = markerDirectives.keySet();
Iterator i = filesSet.iterator();
if (i.hasNext()) {
IFile file = (IFile) i.next();
Workspace workspace = (Workspace) file.getWorkspace();
MarkerManager markerManager = workspace.getMarkerManager();
try {
workspace.prepareOperation();
workspace.beginOperation(true);
boolean fLoop = false;
do {
Set markerInfoSet = (Set) markerDirectives.get(file);
markerManager.removeMarkers(file, PMDPlugin.PMD_MARKER, true, IResource.DEPTH_INFINITE);
markerManager.add(file, (MarkerInfo[]) markerInfoSet.toArray(new MarkerInfo[markerInfoSet.size()]));
if (i.hasNext()) {
file = (IFile) i.next();
fLoop = true;
} else {
fLoop = false;
}
} while (fLoop);
} catch (CoreException e) {
log.warn("CoreException when setting marker info for file " + file.getName() + " : " + e.getMessage());
} finally {
workspace.endOperation(false, null);
}
}
} finally {
log.info("End of marker info directives processing");
}
}
}

View File

@ -6,6 +6,8 @@ import java.util.Iterator;
import net.sourceforge.pmd.eclipse.PMDConstants;
import net.sourceforge.pmd.eclipse.PMDPlugin;
import net.sourceforge.pmd.eclipse.PMDVisitor;
import net.sourceforge.pmd.eclipse.PMDVisitorRunner;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.core.resources.IResource;
@ -14,6 +16,7 @@ import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.internal.core.PackageFragmentRoot;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
@ -30,6 +33,9 @@ import org.eclipse.ui.IWorkbenchPart;
* @version $Revision$
*
* $Log$
* Revision 1.5 2003/05/19 22:27:33 phherlin
* Refactoring to improve performance
*
* Revision 1.4 2003/03/30 20:48:19 phherlin
* Adding logging
* Displaying error dialog in a thread safe way
@ -98,17 +104,21 @@ public class PMDCheckAction implements IObjectActionDelegate {
try {
if (element instanceof IResource) {
((IResource) element).accept(visitor);
new PMDVisitorRunner().run((IResource) element, visitor);
} else if (element instanceof ICompilationUnit) {
IResource resource = ((ICompilationUnit) element).getResource();
resource.accept(visitor);
new PMDVisitorRunner().run(resource, visitor);
} else if (element instanceof IJavaProject) {
IResource resource = ((IJavaProject) element).getResource();
resource.accept(visitor);
new PMDVisitorRunner().run(resource, visitor);
} else if (element instanceof IPackageFragment) {
IResource resource = ((IPackageFragment) element).getResource();
resource.accept(visitor);
new PMDVisitorRunner().run(resource, visitor);
} else if (element instanceof PackageFragmentRoot) {
IResource resource = ((PackageFragmentRoot) element).getResource();
new PMDVisitorRunner().run(resource, visitor);
} else { // else no processing for other types
log.info(element.getClass().getName() + " : PMD check on this resource's type is not supported");
monitor.worked(1);
}
} catch (CoreException e) {

View File

@ -33,15 +33,25 @@ import org.eclipse.ui.IWorkbenchPart;
* @version $Revision$
*
* $Log$
* Revision 1.4 2003/05/19 22:27:33 phherlin
* Refactoring to improve performance
*
* Revision 1.3 2003/03/30 20:48:59 phherlin
* Adding logging
* Displaying error dialog in a thread safe way
*
*/
public class PMDGenerateASTAction implements IObjectActionDelegate {
private static final Log log = LogFactory.getLog("net.sourceforge.pmd.eclipse.actions.PMDGenerateASTAction");
private IWorkbenchPart targetPart;
private static final String QUOTE = "\"";
private static final String INDENT = "\t";
private static final String TAG_BEGIN = "<";
private static final String TAG_END = ">";
private static final String TAG_ENDEND = "/>";
private static final String ENDTAG_BEGIN = TAG_BEGIN + "/";
private static final String ENDTAG_END = TAG_END;
private static Log log = LogFactory.getLog("net.sourceforge.pmd.eclipse.actions.PMDGenerateASTAction");
private IWorkbenchPart targetPart;
/**
* @see org.eclipse.ui.IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
@ -126,29 +136,29 @@ public class PMDGenerateASTAction implements IObjectActionDelegate {
*/
private void dump(SimpleNode node, PrintWriter out, String prefix) {
StringBuffer sb = new StringBuffer(prefix);
sb.append("<").append(node.toString());
sb.append(TAG_BEGIN).append(node.toString());
if (node.jjtGetNumChildren() == 0) {
if (node.getImage() != null) {
sb.append(">").append(node.getImage()).append("</").append(node.toString()).append(">");
sb.append(TAG_END).append(node.getImage()).append(ENDTAG_BEGIN).append(node.toString()).append(ENDTAG_END);
} else {
sb.append("/>");
sb.append(TAG_ENDEND);
}
out.println(sb.toString());
} else {
sb
.append(" beginLine=\"")
.append(" beginLine=" + QUOTE)
.append(node.getBeginLine())
.append("\"")
.append(" beginColumn=\"")
.append(QUOTE)
.append(" beginColumn=" + QUOTE)
.append(node.getBeginColumn())
.append("\"")
.append(" endLine=\"")
.append(QUOTE)
.append(" endLine=" + QUOTE)
.append(node.getEndLine())
.append("\"")
.append(" endColumn=\"")
.append(QUOTE)
.append(" endColumn=" + QUOTE)
.append(node.getEndColumn())
.append("\"")
.append(QUOTE)
.append(">");
if (node.getImage() != null) {
@ -160,7 +170,7 @@ public class PMDGenerateASTAction implements IObjectActionDelegate {
dump((SimpleNode) node.jjtGetChild(i), out, prefix + INDENT);
}
out.println(prefix + "</" + node.toString() + ">");
out.println(prefix + ENDTAG_BEGIN + node.toString() + ENDTAG_END);
}
}

View File

@ -12,6 +12,7 @@ import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
@ -27,6 +28,9 @@ import org.eclipse.ui.IWorkbenchPart;
* @version $Revision$
*
* $Log$
* Revision 1.4 2003/05/19 22:27:33 phherlin
* Refactoring to improve performance
*
* Revision 1.3 2003/03/30 20:49:37 phherlin
* Adding logging
* Displaying error dialog in a thread safe way
@ -99,7 +103,12 @@ public class PMDRemoveMarkersAction implements IViewActionDelegate, IObjectActio
} else if (element instanceof IPackageFragment) {
IResource resource = ((IPackageFragment) element).getResource();
resource.deleteMarkers(PMDPlugin.PMD_MARKER, true, IResource.DEPTH_INFINITE);
} // else no processing for other types
} else if (element instanceof IPackageFragmentRoot) {
IResource resource = ((IPackageFragmentRoot) element).getResource();
resource.deleteMarkers(PMDPlugin.PMD_MARKER, true, IResource.DEPTH_INFINITE);
} else {// else no processing for other types
log.info(element.getClass().getName() + " : Removing markers on this resource's type is not supported");
}
} catch (CoreException e) {
PMDPlugin.getDefault().showError(
PMDPlugin.getDefault().getMessage(PMDConstants.MSGKEY_ERROR_CORE_EXCEPTION),

View File

@ -6,12 +6,11 @@ import net.sourceforge.pmd.eclipse.PMDConstants;
import net.sourceforge.pmd.eclipse.PMDDeltaVisitor;
import net.sourceforge.pmd.eclipse.PMDPlugin;
import net.sourceforge.pmd.eclipse.PMDVisitor;
import net.sourceforge.pmd.eclipse.PMDVisitorRunner;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IResourceVisitor;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
@ -24,6 +23,9 @@ import org.eclipse.core.runtime.IProgressMonitor;
* @version $Revision$
*
* $Log$
* Revision 1.5 2003/05/19 22:27:33 phherlin
* Refactoring to improve performance
*
* Revision 1.4 2003/03/30 20:51:08 phherlin
* Adding logging
*
@ -99,7 +101,7 @@ public class PMDBuilder extends IncrementalProjectBuilder {
if ((resourceDelta != null) && (resourceDelta.getAffectedChildren().length != 0)) {
monitor.beginTask(PMDPlugin.getDefault().getMessage(PMDConstants.MSGKEY_PMD_PROCESSING), IProgressMonitor.UNKNOWN);
PMDDeltaVisitor visitor = new PMDDeltaVisitor(monitor);
resourceDelta.accept(visitor);
new PMDVisitorRunner().run(resourceDelta, visitor);
monitor.done();
} else {
log.info("No change reported. Performing a full build");
@ -117,8 +119,8 @@ public class PMDBuilder extends IncrementalProjectBuilder {
*/
private void processProjectFiles(IProject project, IProgressMonitor monitor) throws CoreException {
monitor.beginTask(PMDPlugin.getDefault().getMessage(PMDConstants.MSGKEY_PMD_PROCESSING), IProgressMonitor.UNKNOWN);
IResourceVisitor visitor = new PMDVisitor(monitor);
project.accept(visitor);
PMDVisitor visitor = new PMDVisitor(monitor);
new PMDVisitorRunner().run(project, visitor);
monitor.done();
}