From 91b2b629b3c339e0e69657b3e4344a3ea7592db3 Mon Sep 17 00:00:00 2001 From: Philippe Herlin Date: Mon, 19 May 2003 22:27:33 +0000 Subject: [PATCH] Refactoring to improve performance git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@1934 51baf565-9d33-0410-a72c-fc3788e3496d --- .../pmd/eclipse/PMDDeltaVisitor.java | 35 ++++- .../sourceforge/pmd/eclipse/PMDVisitor.java | 39 +++++- .../pmd/eclipse/PMDVisitorRunner.java | 122 ++++++++++++++++++ .../pmd/eclipse/actions/PMDCheckAction.java | 18 ++- .../eclipse/actions/PMDGenerateASTAction.java | 38 ++++-- .../actions/PMDRemoveMarkersAction.java | 11 +- .../pmd/eclipse/builder/PMDBuilder.java | 12 +- 7 files changed, 245 insertions(+), 30 deletions(-) create mode 100644 pmd-eclipse/src/net/sourceforge/pmd/eclipse/PMDVisitorRunner.java diff --git a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/PMDDeltaVisitor.java b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/PMDDeltaVisitor.java index 214fa48ece..f401a960f0 100644 --- a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/PMDDeltaVisitor.java +++ b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/PMDDeltaVisitor.java @@ -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; + } + } \ No newline at end of file diff --git a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/PMDVisitor.java b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/PMDVisitor.java index 38253fc5c9..7f22b5b6a7 100644 --- a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/PMDVisitor.java +++ b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/PMDVisitor.java @@ -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); + } + } diff --git a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/PMDVisitorRunner.java b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/PMDVisitorRunner.java new file mode 100644 index 0000000000..4c93da49a6 --- /dev/null +++ b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/PMDVisitorRunner.java @@ -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"); + } + } + +} diff --git a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/actions/PMDCheckAction.java b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/actions/PMDCheckAction.java index 70cc42bb80..a2580d39d4 100644 --- a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/actions/PMDCheckAction.java +++ b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/actions/PMDCheckAction.java @@ -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) { diff --git a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/actions/PMDGenerateASTAction.java b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/actions/PMDGenerateASTAction.java index fa450c97d0..a644b4d9c4 100644 --- a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/actions/PMDGenerateASTAction.java +++ b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/actions/PMDGenerateASTAction.java @@ -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(""); + 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 + ""); + out.println(prefix + ENDTAG_BEGIN + node.toString() + ENDTAG_END); } } diff --git a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/actions/PMDRemoveMarkersAction.java b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/actions/PMDRemoveMarkersAction.java index 3f4dba66a8..f772052393 100644 --- a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/actions/PMDRemoveMarkersAction.java +++ b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/actions/PMDRemoveMarkersAction.java @@ -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), diff --git a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/builder/PMDBuilder.java b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/builder/PMDBuilder.java index a213c33a56..8696d15b9f 100644 --- a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/builder/PMDBuilder.java +++ b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/builder/PMDBuilder.java @@ -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(); }