From 397dbcc8aaaff5219bbe939b0d0d758fe0f53282 Mon Sep 17 00:00:00 2001 From: Holger Friedrich Date: Fri, 1 Mar 2024 08:59:43 +0100 Subject: [PATCH 1/2] Upgrade PMD to 7.0.0 Add support for Java 21. https://docs.pmd-code.org/pmd-doc-7.0.0/pmd_release_notes.html Signed-off-by: Holger Friedrich --- .../tools/analysis/pmd/UseSLF4JLoggerRule.java | 17 +++++++++-------- .../tools/analysis/pmd/test/ClasspathTest.java | 2 +- .../analysis/pmd/test/CustomRulesTest.java | 2 +- .../tools/analysis/pmd/test/PomTest.java | 2 +- .../test/resources/pmd/ruleset/classpath.xml | 2 +- .../test/resources/pmd/ruleset/customrules.xml | 2 +- .../pmd/src/test/resources/pmd/ruleset/pom.xml | 2 +- docs/maven-plugin.md | 2 +- pom.xml | 8 ++++++-- .../tools/analysis/tools/PmdChecker.java | 4 ++-- 10 files changed, 24 insertions(+), 19 deletions(-) diff --git a/custom-checks/pmd/src/main/java/org/openhab/tools/analysis/pmd/UseSLF4JLoggerRule.java b/custom-checks/pmd/src/main/java/org/openhab/tools/analysis/pmd/UseSLF4JLoggerRule.java index 6c0c431e..e1d9e246 100644 --- a/custom-checks/pmd/src/main/java/org/openhab/tools/analysis/pmd/UseSLF4JLoggerRule.java +++ b/custom-checks/pmd/src/main/java/org/openhab/tools/analysis/pmd/UseSLF4JLoggerRule.java @@ -45,7 +45,7 @@ public UseSLF4JLoggerRule() { public Object visit(ASTImportDeclaration node, Object data) { String fullImportName = node.getImportedName(); if (forbiddenLoggers.contains(fullImportName)) { - addViolation(data, node); + asCtx(data).addViolation(node); } else if ("org.slf4j.Logger".equals(fullImportName) || ("org.slf4j".equals(fullImportName) && node.isImportOnDemand())) { isSlf4jPackageImported = true; @@ -55,17 +55,18 @@ public Object visit(ASTImportDeclaration node, Object data) { @Override public Object visit(ASTVariableDeclarator node, Object data) { - ASTType typeNode = node.getParent().getFirstChildOfType(ASTType.class); + ASTType typeNode = node.getParent().firstChild(ASTType.class); if (typeNode != null) { - Node reftypeNode = typeNode.getChild(0); + // getChild(0) returns out of bounds if no child exists, getFirstChild returns null + Node reftypeNode = typeNode.getFirstChild(); if (reftypeNode instanceof ASTReferenceType) { - ASTClassOrInterfaceType classOrInterfaceType = reftypeNode - .getFirstChildOfType(ASTClassOrInterfaceType.class); + ASTClassOrInterfaceType classOrInterfaceType = reftypeNode.firstChild(ASTClassOrInterfaceType.class); if (classOrInterfaceType != null) { - String className = classOrInterfaceType.getImage(); + // getImage will now return null, not sure if getSimpleName is the correct replacement + String className = classOrInterfaceType.getSimpleName(); if (isClassNameForbidden(className)) { - addViolation(data, typeNode); + asCtx(data).addViolation(typeNode); } } } @@ -77,7 +78,7 @@ private boolean isClassNameForbidden(String className) { if (forbiddenLoggers.contains(className)) { return true; } - // If the classname is Logger but org.slf4j is not in the imports, + // If the className is Logger but org.slf4j is not in the imports, // that means the current Logger literal is not a sfl4j.Logger return LOGGER_LITERAL.equals(className) && !isSlf4jPackageImported; } diff --git a/custom-checks/pmd/src/test/java/org/openhab/tools/analysis/pmd/test/ClasspathTest.java b/custom-checks/pmd/src/test/java/org/openhab/tools/analysis/pmd/test/ClasspathTest.java index 26ce0b2c..188ba6cd 100644 --- a/custom-checks/pmd/src/test/java/org/openhab/tools/analysis/pmd/test/ClasspathTest.java +++ b/custom-checks/pmd/src/test/java/org/openhab/tools/analysis/pmd/test/ClasspathTest.java @@ -14,7 +14,7 @@ import org.junit.jupiter.api.BeforeEach; -import net.sourceforge.pmd.testframework.SimpleAggregatorTst; +import net.sourceforge.pmd.test.SimpleAggregatorTst; /** * Test class that includes all custom PMD tests for the .classpath files diff --git a/custom-checks/pmd/src/test/java/org/openhab/tools/analysis/pmd/test/CustomRulesTest.java b/custom-checks/pmd/src/test/java/org/openhab/tools/analysis/pmd/test/CustomRulesTest.java index ae63425b..d0852cca 100644 --- a/custom-checks/pmd/src/test/java/org/openhab/tools/analysis/pmd/test/CustomRulesTest.java +++ b/custom-checks/pmd/src/test/java/org/openhab/tools/analysis/pmd/test/CustomRulesTest.java @@ -14,7 +14,7 @@ import org.junit.jupiter.api.BeforeEach; -import net.sourceforge.pmd.testframework.SimpleAggregatorTst; +import net.sourceforge.pmd.test.SimpleAggregatorTst; /** * Test class that tests all custom PMD rules. diff --git a/custom-checks/pmd/src/test/java/org/openhab/tools/analysis/pmd/test/PomTest.java b/custom-checks/pmd/src/test/java/org/openhab/tools/analysis/pmd/test/PomTest.java index 5b8e8440..1693fb56 100644 --- a/custom-checks/pmd/src/test/java/org/openhab/tools/analysis/pmd/test/PomTest.java +++ b/custom-checks/pmd/src/test/java/org/openhab/tools/analysis/pmd/test/PomTest.java @@ -14,7 +14,7 @@ import org.junit.jupiter.api.BeforeEach; -import net.sourceforge.pmd.testframework.SimpleAggregatorTst; +import net.sourceforge.pmd.test.SimpleAggregatorTst; /** * Test class that includes all custom PMD tests for the pom.xml files diff --git a/custom-checks/pmd/src/test/resources/pmd/ruleset/classpath.xml b/custom-checks/pmd/src/test/resources/pmd/ruleset/classpath.xml index 98c4fa50..63bb2114 100644 --- a/custom-checks/pmd/src/test/resources/pmd/ruleset/classpath.xml +++ b/custom-checks/pmd/src/test/resources/pmd/ruleset/classpath.xml @@ -6,7 +6,7 @@ + class="net.sourceforge.pmd.lang.rule.xpath.XPathRule" language="xml"> Eclipse is adding the attribute "maven.pomderived" automatically to the classpath, when converting a project to Maven project, but most of the time it is not required diff --git a/custom-checks/pmd/src/test/resources/pmd/ruleset/customrules.xml b/custom-checks/pmd/src/test/resources/pmd/ruleset/customrules.xml index b8b9a6a2..8262b254 100644 --- a/custom-checks/pmd/src/test/resources/pmd/ruleset/customrules.xml +++ b/custom-checks/pmd/src/test/resources/pmd/ruleset/customrules.xml @@ -1,5 +1,5 @@ Rule set that contains custom defined rules regarding the .classpath files - + diff --git a/custom-checks/pmd/src/test/resources/pmd/ruleset/pom.xml b/custom-checks/pmd/src/test/resources/pmd/ruleset/pom.xml index a8b4db74..af3dd03a 100644 --- a/custom-checks/pmd/src/test/resources/pmd/ruleset/pom.xml +++ b/custom-checks/pmd/src/test/resources/pmd/ruleset/pom.xml @@ -5,7 +5,7 @@ Rule set that contains custom defined rules regarding the pom.xml files + class="net.sourceforge.pmd.lang.rule.xpath.XPathRule" language="pom"> Avoid overriding configuration inherited by parent pom diff --git a/docs/maven-plugin.md b/docs/maven-plugin.md index b40f493e..c9c0e25e 100644 --- a/docs/maven-plugin.md +++ b/docs/maven-plugin.md @@ -87,7 +87,7 @@ Parameters: | ------ | ------| -------- | | **pmdRuleset** | String | Relative path of the XML configuration to use. If not set the default ruleset file will be used | | **pmdFilter** | String | Relative path of a suppression.properties file that lists classes and rules to be excluded from failures. If not set no classes and no rules will be excluded | -| **maven.pmd.version** | String | The version of the maven-pmd-plugin that will be used (Default value is **3.15.0**)| +| **maven.pmd.version** | String | The version of the maven-pmd-plugin that will be used (Default value is **3.21.2**)| | **pmdPlugins** | List | A list with artifacts that contain additional checks for PMD | ### sat-plugin:checkstyle diff --git a/pom.xml b/pom.xml index 379a9fbe..77764f36 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,6 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - org.openhab.tools.sat pom 0.16.0-SNAPSHOT @@ -67,7 +66,7 @@ 3.12.0 4.10.0 3.3.0 - 6.53.0 + 7.0.0 10.14.0 4.8.3 3.6.0 @@ -94,6 +93,11 @@ + + net.sourceforge.pmd + pmd-compat6 + 7.0.0 + javax.xml.bind jaxb-api diff --git a/sat-plugin/src/main/java/org/openhab/tools/analysis/tools/PmdChecker.java b/sat-plugin/src/main/java/org/openhab/tools/analysis/tools/PmdChecker.java index 0a337e70..2b3cba70 100644 --- a/sat-plugin/src/main/java/org/openhab/tools/analysis/tools/PmdChecker.java +++ b/sat-plugin/src/main/java/org/openhab/tools/analysis/tools/PmdChecker.java @@ -60,7 +60,7 @@ public class PmdChecker extends AbstractChecker { /** * The version of the maven-pmd-plugin that will be used */ - @Parameter(property = "maven.pmd.version", defaultValue = "3.20.0") + @Parameter(property = "maven.pmd.version", defaultValue = "3.21.2") private String mavenPmdVersion; /** @@ -69,7 +69,7 @@ public class PmdChecker extends AbstractChecker { @Parameter private List pmdPlugins = new ArrayList<>(); - private static final String PMD_VERSION = "6.53.0"; + private static final String PMD_VERSION = "7.0.0"; /** * Location of the properties files that contains configuration options for the maven-pmd-plugin */ From 2f971edc8526a79ccfb49a03e6f6c7ae89409442 Mon Sep 17 00:00:00 2001 From: Wouter Born Date: Fri, 1 Mar 2024 20:04:36 +0100 Subject: [PATCH 2/2] Fix UseSLF4JLoggerRule Signed-off-by: Wouter Born --- .../analysis/pmd/UseSLF4JLoggerRule.java | 30 ++++++++----------- .../resources/rulesets/pmd/customrules.xml | 3 +- 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/custom-checks/pmd/src/main/java/org/openhab/tools/analysis/pmd/UseSLF4JLoggerRule.java b/custom-checks/pmd/src/main/java/org/openhab/tools/analysis/pmd/UseSLF4JLoggerRule.java index e1d9e246..2b504f59 100644 --- a/custom-checks/pmd/src/main/java/org/openhab/tools/analysis/pmd/UseSLF4JLoggerRule.java +++ b/custom-checks/pmd/src/main/java/org/openhab/tools/analysis/pmd/UseSLF4JLoggerRule.java @@ -15,13 +15,11 @@ import java.util.HashSet; import java.util.Set; -import net.sourceforge.pmd.lang.ast.Node; -import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType; import net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration; -import net.sourceforge.pmd.lang.java.ast.ASTReferenceType; import net.sourceforge.pmd.lang.java.ast.ASTType; import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclarator; import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule; +import net.sourceforge.pmd.lang.java.symbols.JTypeDeclSymbol; /** * Checks if a logger other than the one provided by slf4j is used @@ -41,6 +39,11 @@ public UseSLF4JLoggerRule() { forbiddenLoggers.add("org.apache.commons.logging.Log"); } + @Override + public String getMessage() { + return "The org.slf4j Logger should be used"; + } + @Override public Object visit(ASTImportDeclaration node, Object data) { String fullImportName = node.getImportedName(); @@ -56,19 +59,12 @@ public Object visit(ASTImportDeclaration node, Object data) { @Override public Object visit(ASTVariableDeclarator node, Object data) { ASTType typeNode = node.getParent().firstChild(ASTType.class); - if (typeNode != null) { - // getChild(0) returns out of bounds if no child exists, getFirstChild returns null - Node reftypeNode = typeNode.getFirstChild(); - if (reftypeNode instanceof ASTReferenceType) { - ASTClassOrInterfaceType classOrInterfaceType = reftypeNode.firstChild(ASTClassOrInterfaceType.class); - if (classOrInterfaceType != null) { - // getImage will now return null, not sure if getSimpleName is the correct replacement - String className = classOrInterfaceType.getSimpleName(); - - if (isClassNameForbidden(className)) { - asCtx(data).addViolation(typeNode); - } - } + if (typeNode != null && typeNode.getTypeMirror().isClassOrInterface()) { + JTypeDeclSymbol symbol = typeNode.getTypeMirror().getSymbol(); + String className = symbol.getPackageName().equals(symbol.getSimpleName()) ? symbol.getSimpleName() + : symbol.getPackageName() + "." + symbol.getSimpleName(); + if (isClassNameForbidden(className)) { + asCtx(data).addViolation(typeNode); } } return super.visit(node, data); @@ -79,7 +75,7 @@ private boolean isClassNameForbidden(String className) { return true; } // If the className is Logger but org.slf4j is not in the imports, - // that means the current Logger literal is not a sfl4j.Logger + // that means the current Logger literal is not an org.slf4j.Logger return LOGGER_LITERAL.equals(className) && !isSlf4jPackageImported; } } diff --git a/sat-plugin/src/main/resources/rulesets/pmd/customrules.xml b/sat-plugin/src/main/resources/rulesets/pmd/customrules.xml index 52e48d18..75c5103c 100644 --- a/sat-plugin/src/main/resources/rulesets/pmd/customrules.xml +++ b/sat-plugin/src/main/resources/rulesets/pmd/customrules.xml @@ -2,8 +2,7 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.net/ruleset_2_0_0.xsd"> Rule set that contains custom rules created for checking all bundles - + 3