Skip to content

Commit 27fbb30

Browse files
Upgrade PMD to 7.0.0 (#462)
* 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 <mail@holger-friedrich.de> Co-authored-by: Wouter Born <github@maindrain.net>
1 parent 3f86ea6 commit 27fbb30

File tree

11 files changed

+32
-32
lines changed

11 files changed

+32
-32
lines changed

custom-checks/pmd/src/main/java/org/openhab/tools/analysis/pmd/UseSLF4JLoggerRule.java

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@
1515
import java.util.HashSet;
1616
import java.util.Set;
1717

18-
import net.sourceforge.pmd.lang.ast.Node;
19-
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType;
2018
import net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration;
21-
import net.sourceforge.pmd.lang.java.ast.ASTReferenceType;
2219
import net.sourceforge.pmd.lang.java.ast.ASTType;
2320
import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclarator;
2421
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
22+
import net.sourceforge.pmd.lang.java.symbols.JTypeDeclSymbol;
2523

2624
/**
2725
* Checks if a logger other than the one provided by slf4j is used
@@ -41,11 +39,16 @@ public UseSLF4JLoggerRule() {
4139
forbiddenLoggers.add("org.apache.commons.logging.Log");
4240
}
4341

42+
@Override
43+
public String getMessage() {
44+
return "The org.slf4j Logger should be used";
45+
}
46+
4447
@Override
4548
public Object visit(ASTImportDeclaration node, Object data) {
4649
String fullImportName = node.getImportedName();
4750
if (forbiddenLoggers.contains(fullImportName)) {
48-
addViolation(data, node);
51+
asCtx(data).addViolation(node);
4952
} else if ("org.slf4j.Logger".equals(fullImportName)
5053
|| ("org.slf4j".equals(fullImportName) && node.isImportOnDemand())) {
5154
isSlf4jPackageImported = true;
@@ -55,19 +58,13 @@ public Object visit(ASTImportDeclaration node, Object data) {
5558

5659
@Override
5760
public Object visit(ASTVariableDeclarator node, Object data) {
58-
ASTType typeNode = node.getParent().getFirstChildOfType(ASTType.class);
59-
if (typeNode != null) {
60-
Node reftypeNode = typeNode.getChild(0);
61-
if (reftypeNode instanceof ASTReferenceType) {
62-
ASTClassOrInterfaceType classOrInterfaceType = reftypeNode
63-
.getFirstChildOfType(ASTClassOrInterfaceType.class);
64-
if (classOrInterfaceType != null) {
65-
String className = classOrInterfaceType.getImage();
66-
67-
if (isClassNameForbidden(className)) {
68-
addViolation(data, typeNode);
69-
}
70-
}
61+
ASTType typeNode = node.getParent().firstChild(ASTType.class);
62+
if (typeNode != null && typeNode.getTypeMirror().isClassOrInterface()) {
63+
JTypeDeclSymbol symbol = typeNode.getTypeMirror().getSymbol();
64+
String className = symbol.getPackageName().equals(symbol.getSimpleName()) ? symbol.getSimpleName()
65+
: symbol.getPackageName() + "." + symbol.getSimpleName();
66+
if (isClassNameForbidden(className)) {
67+
asCtx(data).addViolation(typeNode);
7168
}
7269
}
7370
return super.visit(node, data);
@@ -77,8 +74,8 @@ private boolean isClassNameForbidden(String className) {
7774
if (forbiddenLoggers.contains(className)) {
7875
return true;
7976
}
80-
// If the classname is Logger but org.slf4j is not in the imports,
81-
// that means the current Logger literal is not a sfl4j.Logger
77+
// If the className is Logger but org.slf4j is not in the imports,
78+
// that means the current Logger literal is not an org.slf4j.Logger
8279
return LOGGER_LITERAL.equals(className) && !isSlf4jPackageImported;
8380
}
8481
}

custom-checks/pmd/src/test/java/org/openhab/tools/analysis/pmd/test/ClasspathTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import org.junit.jupiter.api.BeforeEach;
1616

17-
import net.sourceforge.pmd.testframework.SimpleAggregatorTst;
17+
import net.sourceforge.pmd.test.SimpleAggregatorTst;
1818

1919
/**
2020
* Test class that includes all custom PMD tests for the .classpath files

custom-checks/pmd/src/test/java/org/openhab/tools/analysis/pmd/test/CustomRulesTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import org.junit.jupiter.api.BeforeEach;
1616

17-
import net.sourceforge.pmd.testframework.SimpleAggregatorTst;
17+
import net.sourceforge.pmd.test.SimpleAggregatorTst;
1818

1919
/**
2020
* Test class that tests all custom PMD rules.

custom-checks/pmd/src/test/java/org/openhab/tools/analysis/pmd/test/PomTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import org.junit.jupiter.api.BeforeEach;
1616

17-
import net.sourceforge.pmd.testframework.SimpleAggregatorTst;
17+
import net.sourceforge.pmd.test.SimpleAggregatorTst;
1818

1919
/**
2020
* Test class that includes all custom PMD tests for the pom.xml files

custom-checks/pmd/src/test/resources/pmd/ruleset/classpath.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<!-- This is an example how to define a Rule with XPath expression -->
88
<rule name="AvoidMavenPomderivedInClasspath" message="The classpath file contains maven.pomderived attribute "
9-
class="net.sourceforge.pmd.lang.rule.XPathRule" language="xml">
9+
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule" language="xml">
1010
<description>
1111
Eclipse is adding the attribute "maven.pomderived" automatically to the classpath, when converting a
1212
project to Maven project, but most of the time it is not required
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<ruleset name="Classpath Ruleset" xmlns="http://pmd.sourceforge.net/ruleset/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
22
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.net/ruleset_2_0_0.xsd">
33
<description>Rule set that contains custom defined rules regarding the .classpath files</description>
4-
<rule class="org.openhab.tools.analysis.pmd.UseSLF4JLoggerRule" name="UseSLF4JLoggerRule"/>
4+
<rule class="org.openhab.tools.analysis.pmd.UseSLF4JLoggerRule" name="UseSLF4JLoggerRule" language="java"/>
55
</ruleset>

custom-checks/pmd/src/test/resources/pmd/ruleset/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<description>Rule set that contains custom defined rules regarding the pom.xml files</description>
66

77
<rule name="AvoidOverridingParentPomConfiguration" message="Avoid overriding configuration inherited by parent pom"
8-
class="net.sourceforge.pmd.lang.rule.XPathRule" language="pom">
8+
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule" language="pom">
99
<description>
1010
Avoid overriding configuration inherited by parent pom
1111
</description>

docs/maven-plugin.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ Parameters:
8787
| ------ | ------| -------- |
8888
| **pmdRuleset** | String | Relative path of the XML configuration to use. If not set the default ruleset file will be used |
8989
| **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 |
90-
| **maven.pmd.version** | String | The version of the maven-pmd-plugin that will be used (Default value is **3.15.0**)|
90+
| **maven.pmd.version** | String | The version of the maven-pmd-plugin that will be used (Default value is **3.21.2**)|
9191
| **pmdPlugins** | List<Dependency> | A list with artifacts that contain additional checks for PMD |
9292

9393
### sat-plugin:checkstyle

pom.xml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
44

55
<modelVersion>4.0.0</modelVersion>
6-
76
<groupId>org.openhab.tools.sat</groupId>
87
<artifactId>pom</artifactId>
98
<version>0.16.0-SNAPSHOT</version>
@@ -67,7 +66,7 @@
6766
<commons.lang3.version>3.12.0</commons.lang3.version>
6867
<mockito.version>4.10.0</mockito.version>
6968
<maven.resources.version>3.3.0</maven.resources.version>
70-
<pmd.version>6.53.0</pmd.version>
69+
<pmd.version>7.0.0</pmd.version>
7170
<checkstyle.version>10.14.0</checkstyle.version>
7271
<spotbugs.version>4.8.3</spotbugs.version>
7372
<maven.core.version>3.6.0</maven.core.version>
@@ -94,6 +93,11 @@
9493
</properties>
9594

9695
<dependencies>
96+
<dependency>
97+
<groupId>net.sourceforge.pmd</groupId>
98+
<artifactId>pmd-compat6</artifactId>
99+
<version>7.0.0</version>
100+
</dependency>
97101
<dependency>
98102
<groupId>javax.xml.bind</groupId>
99103
<artifactId>jaxb-api</artifactId>

sat-plugin/src/main/java/org/openhab/tools/analysis/tools/PmdChecker.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class PmdChecker extends AbstractChecker {
6060
/**
6161
* The version of the maven-pmd-plugin that will be used
6262
*/
63-
@Parameter(property = "maven.pmd.version", defaultValue = "3.20.0")
63+
@Parameter(property = "maven.pmd.version", defaultValue = "3.21.2")
6464
private String mavenPmdVersion;
6565

6666
/**
@@ -69,7 +69,7 @@ public class PmdChecker extends AbstractChecker {
6969
@Parameter
7070
private List<Dependency> pmdPlugins = new ArrayList<>();
7171

72-
private static final String PMD_VERSION = "6.53.0";
72+
private static final String PMD_VERSION = "7.0.0";
7373
/**
7474
* Location of the properties files that contains configuration options for the maven-pmd-plugin
7575
*/

0 commit comments

Comments
 (0)