Skip to content

Commit

Permalink
Upgrade PMD to 7.0.0 (#462)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
holgerfriedrich and wborn authored Mar 25, 2024
1 parent 3f86ea6 commit 27fbb30
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -41,11 +39,16 @@ 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();
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;
Expand All @@ -55,19 +58,13 @@ public Object visit(ASTImportDeclaration node, Object data) {

@Override
public Object visit(ASTVariableDeclarator node, Object data) {
ASTType typeNode = node.getParent().getFirstChildOfType(ASTType.class);
if (typeNode != null) {
Node reftypeNode = typeNode.getChild(0);
if (reftypeNode instanceof ASTReferenceType) {
ASTClassOrInterfaceType classOrInterfaceType = reftypeNode
.getFirstChildOfType(ASTClassOrInterfaceType.class);
if (classOrInterfaceType != null) {
String className = classOrInterfaceType.getImage();

if (isClassNameForbidden(className)) {
addViolation(data, typeNode);
}
}
ASTType typeNode = node.getParent().firstChild(ASTType.class);
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);
Expand All @@ -77,8 +74,8 @@ private boolean isClassNameForbidden(String className) {
if (forbiddenLoggers.contains(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
// If the className is Logger but org.slf4j is not in the imports,
// that means the current Logger literal is not an org.slf4j.Logger
return LOGGER_LITERAL.equals(className) && !isSlf4jPackageImported;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<!-- This is an example how to define a Rule with XPath expression -->
<rule name="AvoidMavenPomderivedInClasspath" message="The classpath file contains maven.pomderived attribute "
class="net.sourceforge.pmd.lang.rule.XPathRule" language="xml">
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule" language="xml">
<description>
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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<ruleset name="Classpath Ruleset" xmlns="http://pmd.sourceforge.net/ruleset/2.0.0" 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">
<description>Rule set that contains custom defined rules regarding the .classpath files</description>
<rule class="org.openhab.tools.analysis.pmd.UseSLF4JLoggerRule" name="UseSLF4JLoggerRule"/>
<rule class="org.openhab.tools.analysis.pmd.UseSLF4JLoggerRule" name="UseSLF4JLoggerRule" language="java"/>
</ruleset>
2 changes: 1 addition & 1 deletion custom-checks/pmd/src/test/resources/pmd/ruleset/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<description>Rule set that contains custom defined rules regarding the pom.xml files</description>

<rule name="AvoidOverridingParentPomConfiguration" message="Avoid overriding configuration inherited by parent pom"
class="net.sourceforge.pmd.lang.rule.XPathRule" language="pom">
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule" language="pom">
<description>
Avoid overriding configuration inherited by parent pom
</description>
Expand Down
2 changes: 1 addition & 1 deletion docs/maven-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Dependency> | A list with artifacts that contain additional checks for PMD |

### sat-plugin:checkstyle
Expand Down
8 changes: 6 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>org.openhab.tools.sat</groupId>
<artifactId>pom</artifactId>
<version>0.16.0-SNAPSHOT</version>
Expand Down Expand Up @@ -67,7 +66,7 @@
<commons.lang3.version>3.12.0</commons.lang3.version>
<mockito.version>4.10.0</mockito.version>
<maven.resources.version>3.3.0</maven.resources.version>
<pmd.version>6.53.0</pmd.version>
<pmd.version>7.0.0</pmd.version>
<checkstyle.version>10.14.0</checkstyle.version>
<spotbugs.version>4.8.3</spotbugs.version>
<maven.core.version>3.6.0</maven.core.version>
Expand All @@ -94,6 +93,11 @@
</properties>

<dependencies>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-compat6</artifactId>
<version>7.0.0</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -69,7 +69,7 @@ public class PmdChecker extends AbstractChecker {
@Parameter
private List<Dependency> 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
*/
Expand Down
3 changes: 1 addition & 2 deletions sat-plugin/src/main/resources/rulesets/pmd/customrules.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
<description>Rule set that contains custom rules created for checking all bundles</description>
<rule class="org.openhab.tools.analysis.pmd.UseSLF4JLoggerRule" name="UseSLF4J" language="java"
message="The org.slf4j Logger should be used">
<rule class="org.openhab.tools.analysis.pmd.UseSLF4JLoggerRule" name="UseSLF4J" language="java">
<priority>3</priority>
</rule>
</ruleset>

0 comments on commit 27fbb30

Please sign in to comment.