This is an automated email from the ASF dual-hosted git repository.

adangel pushed a commit to branch pmd7
in repository https://gitbox.apache.org/repos/asf/maven-pmd-plugin.git

commit 1ad328df01643f52e2e1c5df261855c2c81304dd
Author: Andreas Dangel <adan...@apache.org>
AuthorDate: Thu Jun 9 18:50:10 2022 +0200

    Upgrade to PMD 7.0.0-rc2
---
 pom.xml                                            |  4 +-
 .../org/apache/maven/plugins/pmd/CpdReport.java    | 13 -------
 .../apache/maven/plugins/pmd/exec/CpdExecutor.java | 40 +++++++------------
 .../maven/plugins/pmd/exec/CpdReportFilter.java    | 45 ++++++++++++++++++++++
 .../apache/maven/plugins/pmd/exec/PmdExecutor.java |  2 +-
 .../apache/maven/plugins/pmd/PmdReportTest.java    | 20 ++++++----
 6 files changed, 74 insertions(+), 50 deletions(-)

diff --git a/pom.xml b/pom.xml
index be8acfb..b945e06 100644
--- a/pom.xml
+++ b/pom.xml
@@ -28,7 +28,7 @@ under the License.
   </parent>
 
   <artifactId>maven-pmd-plugin</artifactId>
-  <version>3.21.1-SNAPSHOT</version>
+  <version>3.21.1-pmd-7-SNAPSHOT</version>
   <packaging>maven-plugin</packaging>
 
   <name>Apache Maven PMD Plugin</name>
@@ -83,7 +83,7 @@ under the License.
   <properties>
     <mavenVersion>3.2.5</mavenVersion>
     <javaVersion>8</javaVersion>
-    <pmdVersion>6.55.0</pmdVersion>
+    <pmdVersion>7.0.0-rc2</pmdVersion>
     <slf4jVersion>1.7.36</slf4jVersion>
     <aetherVersion>1.0.0.v20140518</aetherVersion>
     <doxiaVersion>1.12.0</doxiaVersion>
diff --git a/src/main/java/org/apache/maven/plugins/pmd/CpdReport.java 
b/src/main/java/org/apache/maven/plugins/pmd/CpdReport.java
index c01fdbc..45660e0 100644
--- a/src/main/java/org/apache/maven/plugins/pmd/CpdReport.java
+++ b/src/main/java/org/apache/maven/plugins/pmd/CpdReport.java
@@ -24,7 +24,6 @@ import java.util.Locale;
 import java.util.Properties;
 
 import net.sourceforge.pmd.cpd.JavaTokenizer;
-import net.sourceforge.pmd.cpd.renderer.CPDRenderer;
 import org.apache.maven.plugins.annotations.Component;
 import org.apache.maven.plugins.annotations.Mojo;
 import org.apache.maven.plugins.annotations.Parameter;
@@ -229,16 +228,4 @@ public class CpdReport extends AbstractPmdReport {
     public String getOutputName() {
         return "cpd";
     }
-
-    /**
-     * Create and return the correct renderer for the output type.
-     *
-     * @return the renderer based on the configured output
-     * @throws org.apache.maven.reporting.MavenReportException if no renderer 
found for the output type
-     * @deprecated Use {@link CpdExecutor#createRenderer(String, String)} 
instead.
-     */
-    @Deprecated
-    public CPDRenderer createRenderer() throws MavenReportException {
-        return CpdExecutor.createRenderer(format, getOutputEncoding());
-    }
 }
diff --git a/src/main/java/org/apache/maven/plugins/pmd/exec/CpdExecutor.java 
b/src/main/java/org/apache/maven/plugins/pmd/exec/CpdExecutor.java
index 43ba3c0..4f0a25f 100644
--- a/src/main/java/org/apache/maven/plugins/pmd/exec/CpdExecutor.java
+++ b/src/main/java/org/apache/maven/plugins/pmd/exec/CpdExecutor.java
@@ -26,23 +26,20 @@ import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.io.OutputStreamWriter;
 import java.io.Writer;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
 import java.util.Objects;
 
 import net.sourceforge.pmd.cpd.CPD;
 import net.sourceforge.pmd.cpd.CPDConfiguration;
+import net.sourceforge.pmd.cpd.CPDReport;
 import net.sourceforge.pmd.cpd.CSVRenderer;
 import net.sourceforge.pmd.cpd.EcmascriptLanguage;
 import net.sourceforge.pmd.cpd.JSPLanguage;
 import net.sourceforge.pmd.cpd.JavaLanguage;
 import net.sourceforge.pmd.cpd.Language;
 import net.sourceforge.pmd.cpd.LanguageFactory;
-import net.sourceforge.pmd.cpd.Match;
 import net.sourceforge.pmd.cpd.SimpleRenderer;
 import net.sourceforge.pmd.cpd.XMLRenderer;
-import net.sourceforge.pmd.cpd.renderer.CPDRenderer;
+import net.sourceforge.pmd.cpd.renderer.CPDReportRenderer;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugins.pmd.ExcludeDuplicationsFromFile;
 import org.apache.maven.reporting.MavenReportException;
@@ -207,7 +204,7 @@ public class CpdExecutor extends Executor {
         }
     }
 
-    private File writeReport(CPD cpd, CPDRenderer r, String extension) throws 
MavenReportException {
+    private File writeReport(CPD cpd, CPDReportRenderer r, String extension) 
throws MavenReportException {
         if (r == null) {
             return null;
         }
@@ -216,7 +213,7 @@ public class CpdExecutor extends Executor {
         targetDir.mkdirs();
         File targetFile = new File(targetDir, "cpd." + extension);
         try (Writer writer = new OutputStreamWriter(new 
FileOutputStream(targetFile), request.getOutputEncoding())) {
-            r.render(filterMatches(cpd.getMatches()), writer);
+            r.render(filterMatches(cpd.toReport()), writer);
             writer.flush();
         } catch (IOException ioe) {
             throw new MavenReportException(ioe.getMessage(), ioe);
@@ -225,7 +222,7 @@ public class CpdExecutor extends Executor {
     }
 
     private void writeFormattedReport(CPD cpd) throws MavenReportException {
-        CPDRenderer r = createRenderer(request.getFormat(), 
request.getOutputEncoding());
+        CPDReportRenderer r = createRenderer(request.getFormat(), 
request.getOutputEncoding());
         writeReport(cpd, r, request.getFormat());
     }
 
@@ -235,8 +232,8 @@ public class CpdExecutor extends Executor {
      * @return the renderer based on the configured output
      * @throws org.apache.maven.reporting.MavenReportException if no renderer 
found for the output type
      */
-    public static CPDRenderer createRenderer(String format, String 
outputEncoding) throws MavenReportException {
-        CPDRenderer renderer = null;
+    public static CPDReportRenderer createRenderer(String format, String 
outputEncoding) throws MavenReportException {
+        CPDReportRenderer renderer = null;
         if ("xml".equals(format)) {
             renderer = new XMLRenderer(outputEncoding);
         } else if ("csv".equals(format)) {
@@ -245,7 +242,8 @@ public class CpdExecutor extends Executor {
             renderer = new SimpleRenderer();
         } else if (!"".equals(format) && !"none".equals(format)) {
             try {
-                renderer = (CPDRenderer) 
Class.forName(format).getConstructor().newInstance();
+                renderer = (CPDReportRenderer)
+                        Class.forName(format).getConstructor().newInstance();
             } catch (Exception e) {
                 throw new MavenReportException(
                         "Can't find CPD custom format " + format + ": "
@@ -257,22 +255,12 @@ public class CpdExecutor extends Executor {
         return renderer;
     }
 
-    private Iterator<Match> filterMatches(Iterator<Match> matches) {
+    private CPDReport filterMatches(CPDReport report) {
         LOG.debug("Filtering duplications. Using " + 
excludeDuplicationsFromFile.countExclusions()
                 + " configured exclusions.");
-
-        List<Match> filteredMatches = new ArrayList<>();
-        int excludedDuplications = 0;
-        while (matches.hasNext()) {
-            Match match = matches.next();
-            if (excludeDuplicationsFromFile.isExcludedFromFailure(match)) {
-                excludedDuplications++;
-            } else {
-                filteredMatches.add(match);
-            }
-        }
-
-        LOG.debug("Excluded " + excludedDuplications + " duplications.");
-        return filteredMatches.iterator();
+        CpdReportFilter filter = new 
CpdReportFilter(excludeDuplicationsFromFile);
+        CPDReport filteredReport = report.filterMatches(filter);
+        LOG.debug("Excluded " + filter.getExcludedDuplications() + " 
duplications.");
+        return filteredReport;
     }
 }
diff --git 
a/src/main/java/org/apache/maven/plugins/pmd/exec/CpdReportFilter.java 
b/src/main/java/org/apache/maven/plugins/pmd/exec/CpdReportFilter.java
new file mode 100644
index 0000000..1a7c961
--- /dev/null
+++ b/src/main/java/org/apache/maven/plugins/pmd/exec/CpdReportFilter.java
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.plugins.pmd.exec;
+
+import net.sourceforge.pmd.cpd.Match;
+import net.sourceforge.pmd.util.Predicate;
+import org.apache.maven.plugins.pmd.ExcludeDuplicationsFromFile;
+
+class CpdReportFilter implements Predicate<Match> {
+    private final ExcludeDuplicationsFromFile excludeDuplicationsFromFile;
+    private int excludedDuplications = 0;
+
+    CpdReportFilter(ExcludeDuplicationsFromFile excludeDuplicationsFromFile) {
+        this.excludeDuplicationsFromFile = excludeDuplicationsFromFile;
+    }
+
+    @Override
+    public boolean test(Match match) {
+        if (excludeDuplicationsFromFile.isExcludedFromFailure(match)) {
+            excludedDuplications++;
+            return false;
+        }
+        return true;
+    }
+
+    int getExcludedDuplications() {
+        return excludedDuplications;
+    }
+}
diff --git a/src/main/java/org/apache/maven/plugins/pmd/exec/PmdExecutor.java 
b/src/main/java/org/apache/maven/plugins/pmd/exec/PmdExecutor.java
index 8e96bf4..ad0a5d3 100644
--- a/src/main/java/org/apache/maven/plugins/pmd/exec/PmdExecutor.java
+++ b/src/main/java/org/apache/maven/plugins/pmd/exec/PmdExecutor.java
@@ -30,6 +30,7 @@ import java.io.Writer;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Objects;
+import java.util.function.Predicate;
 
 import net.sourceforge.pmd.PMDConfiguration;
 import net.sourceforge.pmd.PmdAnalysis;
@@ -50,7 +51,6 @@ import net.sourceforge.pmd.renderers.HTMLRenderer;
 import net.sourceforge.pmd.renderers.Renderer;
 import net.sourceforge.pmd.renderers.TextRenderer;
 import net.sourceforge.pmd.renderers.XMLRenderer;
-import net.sourceforge.pmd.util.Predicate;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugins.pmd.ExcludeViolationsFromFile;
 import org.apache.maven.reporting.MavenReportException;
diff --git a/src/test/java/org/apache/maven/plugins/pmd/PmdReportTest.java 
b/src/test/java/org/apache/maven/plugins/pmd/PmdReportTest.java
index e3f4b3e..aa897f6 100644
--- a/src/test/java/org/apache/maven/plugins/pmd/PmdReportTest.java
+++ b/src/test/java/org/apache/maven/plugins/pmd/PmdReportTest.java
@@ -446,7 +446,8 @@ public class PmdReportTest extends 
AbstractPmdReportTestCase {
         // check that there is no violation reported for "unusedVar2" - as it 
is suppressed
         assertFalse(str.contains("Avoid unused private fields such as 
'unusedVar2'.\n </violation>"));
         // but it appears as suppressed
-        assertTrue(str.contains("suppressiontype=\"nopmd\" msg=\"Avoid unused 
private fields such as 'unusedVar2'.\""));
+        assertTrue(
+                str.contains("suppressiontype=\"//nopmd\" msg=\"Avoid unused 
private fields such as 'unusedVar2'.\""));
 
         // check if there's a link to the JXR files
         str = readFile(generatedReport);
@@ -470,7 +471,8 @@ public class PmdReportTest extends 
AbstractPmdReportTestCase {
         // check that there is no violation reported for "unusedVar2" - as it 
is suppressed
         assertFalse(str.contains("Avoid unused private fields such as 
'unusedVar2'.\n </violation>"));
         // but it appears as suppressed
-        assertTrue(str.contains("suppressiontype=\"nopmd\" msg=\"Avoid unused 
private fields such as 'unusedVar2'.\""));
+        assertTrue(
+                str.contains("suppressiontype=\"//nopmd\" msg=\"Avoid unused 
private fields such as 'unusedVar2'.\""));
 
         // check if there's a link to the JXR files
         str = readFile(generatedReport);
@@ -534,13 +536,14 @@ public class PmdReportTest extends 
AbstractPmdReportTestCase {
         File generatedFile = new File(getBasedir(), 
"target/test/unit/parse-error/target/pmd.xml");
         assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));
         String str = readFile(generatedFile);
-        assertTrue(str.contains("Error while parsing"));
         // The parse exception must be in the XML report
-        assertTrue(str.contains("ParseException: Encountered \"\" at line 23, 
column 5."));
+        assertTrue(str.contains("ParseException:"));
+        assertTrue(str.contains("Encountered  at line 23, column 5."));
 
         str = readFile(generatedReport);
         // The parse exception must also be in the HTML report
-        assertTrue(str.contains("ParseException: Encountered \"\" at line 23, 
column 5."));
+        assertTrue(str.contains("ParseException:"));
+        assertTrue(str.contains("Encountered  at line 23, column 5."));
     }
 
     public void testPMDProcessingErrorWithDetailsNoReport() throws Exception {
@@ -554,13 +557,14 @@ public class PmdReportTest extends 
AbstractPmdReportTestCase {
         File generatedFile = new File(getBasedir(), 
"target/test/unit/parse-error/target/pmd.xml");
         assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));
         String str = readFile(generatedFile);
-        assertTrue(str.contains("Error while parsing"));
         // The parse exception must be in the XML report
-        assertTrue(str.contains("ParseException: Encountered \"\" at line 23, 
column 5."));
+        assertTrue(str.contains("ParseException:"));
+        assertTrue(str.contains("Encountered  at line 23, column 5."));
 
         str = readFile(generatedReport);
         // The parse exception must NOT be in the HTML report, since 
reportProcessingErrors is false
-        assertFalse(str.contains("ParseException: Encountered \"\" at line 23, 
column 5."));
+        assertFalse(str.contains("ParseException:"));
+        assertFalse(str.contains("Encountered  at line 23, column 5."));
     }
 
     public void testPMDExcludeRootsShouldExcludeSubdirectories() throws 
Exception {

Reply via email to