Author: adangel
Date: Sat Oct  1 09:30:00 2016
New Revision: 1762990

URL: http://svn.apache.org/viewvc?rev=1762990&view=rev
Log:
[MPMD-162] PMD/CPD report does not take into account pmd.excludeFromFailureFile
Further refactoring and logging

Modified:
    
maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/CpdReport.java
    
maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/ExcludeDuplicationsFromFile.java
    
maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/ExcludeViolationsFromFile.java
    
maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdReport.java

Modified: 
maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/CpdReport.java
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/CpdReport.java?rev=1762990&r1=1762989&r2=1762990&view=diff
==============================================================================
--- 
maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/CpdReport.java
 (original)
+++ 
maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/CpdReport.java
 Sat Oct  1 09:30:00 2016
@@ -110,6 +110,9 @@ public class CpdReport
     /** The CPD instance used to analyze the files. Will itself collect the 
duplicated code matches. */
     private CPD cpd;
 
+    /** Helper to exclude duplications from the result. */
+    private final ExcludeDuplicationsFromFile excludeDuplicationsFromFile = 
new ExcludeDuplicationsFromFile();
+
     /**
      * {@inheritDoc}
      */
@@ -244,16 +247,13 @@ public class CpdReport
                 filesToProcess = getFilesToProcess();
             }
 
-            if ( !StringUtils.isEmpty( excludeFromFailureFile ) )
+            try
             {
-                try
-                {
-                    loadExcludeFromFailuresData( excludeFromFailureFile );
-                }
-                catch ( MojoExecutionException e )
-                {
-                    throw new MavenReportException( "Error loading 
exclusions", e );
-                }
+                excludeDuplicationsFromFile.loadExcludeFromFailuresData( 
excludeFromFailureFile );
+            }
+            catch ( MojoExecutionException e )
+            {
+                throw new MavenReportException( "Error loading exclusions", e 
);
             }
 
             String encoding = determineEncoding( !filesToProcess.isEmpty() );
@@ -309,15 +309,25 @@ public class CpdReport
 
     private Iterator<Match> filterMatches( Iterator<Match> matches )
     {
+        getLog().debug( "Filtering duplications. Using " + 
excludeDuplicationsFromFile.countExclusions()
+            + " configured exclusions." );
+
         List<Match> filteredMatches = new ArrayList<>();
+        int excludedDuplications = 0;
         while ( matches.hasNext() )
         {
             Match match = matches.next();
-            if ( !isExcludedFromFailure( match ) )
+            if ( excludeDuplicationsFromFile.isExcludedFromFailure( match ) )
+            {
+                excludedDuplications++;
+            }
+            else
             {
                 filteredMatches.add( match );
             }
         }
+
+        getLog().debug( "Excluded " + excludedDuplications + " duplications." 
);
         return filteredMatches.iterator();
     }
 
@@ -425,19 +435,4 @@ public class CpdReport
 
         return renderer;
     }
-
-
-
-
-    private final ExcludeDuplicationsFromFile excludeDuplicationsFromFile = 
new ExcludeDuplicationsFromFile();
-    protected boolean isExcludedFromFailure( final Match errorDetail )
-    {
-        return excludeDuplicationsFromFile.isExcludedFromFailure( errorDetail 
);
-    }
-
-    protected void loadExcludeFromFailuresData( final String 
excludeFromFailureFile )
-        throws MojoExecutionException
-    {
-        excludeDuplicationsFromFile.loadExcludeFromFailuresData( 
excludeFromFailureFile );
-    }
 }

Modified: 
maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/ExcludeDuplicationsFromFile.java
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/ExcludeDuplicationsFromFile.java?rev=1762990&r1=1762989&r2=1762990&view=diff
==============================================================================
--- 
maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/ExcludeDuplicationsFromFile.java
 (original)
+++ 
maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/ExcludeDuplicationsFromFile.java
 Sat Oct  1 09:30:00 2016
@@ -153,4 +153,13 @@ public class ExcludeDuplicationsFromFile
         }
         return result;
     }
+
+    /**
+     * Determines how many exclusions are considered.
+     * @return the number of active exclusions
+     */
+    public int countExclusions()
+    {
+        return exclusionList.size();
+    }
 }

Modified: 
maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/ExcludeViolationsFromFile.java
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/ExcludeViolationsFromFile.java?rev=1762990&r1=1762989&r2=1762990&view=diff
==============================================================================
--- 
maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/ExcludeViolationsFromFile.java
 (original)
+++ 
maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/ExcludeViolationsFromFile.java
 Sat Oct  1 09:30:00 2016
@@ -29,10 +29,10 @@ import java.util.Map.Entry;
 import java.util.Properties;
 import java.util.Set;
 
+import org.apache.commons.lang3.StringUtils;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.pmd.model.Violation;
 import org.codehaus.plexus.util.IOUtil;
-import org.codehaus.plexus.util.StringUtils;
 
 import net.sourceforge.pmd.RuleViolation;
 
@@ -56,6 +56,11 @@ public class ExcludeViolationsFromFile
     public void loadExcludeFromFailuresData( final String 
excludeFromFailureFile )
         throws MojoExecutionException
     {
+        if ( StringUtils.isEmpty( excludeFromFailureFile ) )
+        {
+            return;
+        }
+
         File file = new File( excludeFromFailureFile );
         if ( !file.exists() )
         {
@@ -119,6 +124,20 @@ public class ExcludeViolationsFromFile
         return isExcludedFromFailure( className, 
errorDetail.getRule().getName() );
     }
 
+    /**
+     * Determines how many exclusions are considered.
+     * @return the number of active exclusions
+     */
+    public int countExclusions()
+    {
+        int result = 0;
+        for ( Set<String> rules : excludeFromFailureClasses.values() )
+        {
+            result += rules.size();
+        }
+        return result;
+    }
+
     private boolean isExcludedFromFailure( String className, String ruleName )
     {
         final Set<String> excludedRuleSet = excludeFromFailureClasses.get( 
className );

Modified: 
maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdReport.java
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdReport.java?rev=1762990&r1=1762989&r2=1762990&view=diff
==============================================================================
--- 
maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdReport.java
 (original)
+++ 
maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdReport.java
 Sat Oct  1 09:30:00 2016
@@ -170,6 +170,9 @@ public class PmdReport
     /** The PMD renderer for collecting violations. */
     private PmdCollectingRenderer renderer;
 
+    /** Helper to exclude violations given as a properties file. */
+    private final ExcludeViolationsFromFile excludeFromFile = new 
ExcludeViolationsFromFile();
+
     /**
      * per default pmd executions error are ignored to not break the whole
      *
@@ -300,16 +303,13 @@ public class PmdReport
             return;
         }
 
-        if ( !StringUtils.isEmpty( excludeFromFailureFile ) )
+        try
         {
-            try
-            {
-                loadExcludeFromFailuresData( excludeFromFailureFile );
-            }
-            catch ( MojoExecutionException e )
-            {
-                throw new MavenReportException( "Unable to load exclusions", e 
);
-            }
+            excludeFromFile.loadExcludeFromFailuresData( 
excludeFromFailureFile );
+        }
+        catch ( MojoExecutionException e )
+        {
+            throw new MavenReportException( "Unable to load exclusions", e );
         }
 
         // configure ResourceManager
@@ -400,15 +400,7 @@ public class PmdReport
             getLog().warn( renderer.getErrorsAsString() );
         }
 
-        Iterator<RuleViolation> violationIt = 
renderer.getViolations().iterator();
-        while ( violationIt.hasNext() )
-        {
-            RuleViolation rv = violationIt.next();
-            if ( isExcludedFromFailure( rv ) )
-            {
-                violationIt.remove();
-            }
-        }
+        removeExcludedViolations( renderer.getViolations() );
 
         // if format is XML, we need to output it even if the file list is 
empty or we have no violations
         // so the "check" goals can check for violations
@@ -430,6 +422,26 @@ public class PmdReport
         }
     }
 
+    private void removeExcludedViolations( List<RuleViolation> violations )
+    {
+        getLog().debug( "Removing excluded violations. Using " + 
excludeFromFile.countExclusions()
+            + " configured exclusions." );
+        int violationsBefore = violations.size();
+
+        Iterator<RuleViolation> iterator = violations.iterator();
+        while ( iterator.hasNext() )
+        {
+            RuleViolation rv = iterator.next();
+            if ( excludeFromFile.isExcludedFromFailure( rv ) )
+            {
+                iterator.remove();
+            }
+        }
+
+        int numberOfExcludedViolations = violationsBefore - violations.size();
+        getLog().debug( "Excluded " + numberOfExcludedViolations + " 
violations." );
+    }
+
     private void processFilesWithPMD( PMDConfiguration pmdConfiguration, 
List<DataSource> dataSources )
             throws MavenReportException
     {
@@ -636,28 +648,28 @@ public class PmdReport
     public final Renderer createRenderer()
         throws MavenReportException
     {
-        Renderer renderer = null;
+        Renderer result = null;
         if ( "xml".equals( format ) )
         {
-            renderer = new XMLRenderer( getOutputEncoding() );
+            result = new XMLRenderer( getOutputEncoding() );
         }
         else if ( "txt".equals( format ) )
         {
-            renderer = new TextRenderer();
+            result = new TextRenderer();
         }
         else if ( "csv".equals( format ) )
         {
-            renderer = new CSVRenderer();
+            result = new CSVRenderer();
         }
         else if ( "html".equals( format ) )
         {
-            renderer = new HTMLRenderer();
+            result = new HTMLRenderer();
         }
         else if ( !"".equals( format ) && !"none".equals( format ) )
         {
             try
             {
-                renderer = (Renderer) Class.forName( format ).getConstructor( 
Properties.class ).
+                result = (Renderer) Class.forName( format ).getConstructor( 
Properties.class ).
                                 newInstance( new Properties() );
             }
             catch ( Exception e )
@@ -667,19 +679,6 @@ public class PmdReport
             }
         }
 
-        return renderer;
-    }
-
-    private final ExcludeViolationsFromFile excludeFromFile = new 
ExcludeViolationsFromFile();
-
-    protected void loadExcludeFromFailuresData( final String 
excludeFromFailureFile )
-        throws MojoExecutionException
-    {
-        excludeFromFile.loadExcludeFromFailuresData( excludeFromFailureFile );
-    }
-
-    protected boolean isExcludedFromFailure( final RuleViolation errorDetail )
-    {
-        return excludeFromFile.isExcludedFromFailure( errorDetail );
+        return result;
     }
 }


Reply via email to