BAEL-7051 - Catch Common Mistakes with Error-Prone Library in Java (#15278)

This commit is contained in:
Alexandru Borza 2023-11-24 18:39:21 +02:00 committed by GitHub
parent 789ecf4581
commit c7ef62ee99
13 changed files with 240 additions and 39 deletions

View File

@ -889,7 +889,7 @@
<module>spring-vault</module>
<module>spring-web-modules</module>
<module>spring-websockets</module>
<module>static-analysis</module>
<!-- <module>static-analysis</module> - requires additional configuration to be compiled due to the JVM strong encapsulation -->
<module>tablesaw</module>
<module>tensorflow-java</module>
<module>testing-modules</module>
@ -1134,7 +1134,7 @@
<module>spring-vault</module>
<module>spring-web-modules</module>
<module>spring-websockets</module>
<module>static-analysis</module>
<!-- <module>static-analysis</module> - requires additional configuration to be compiled due to the JVM strong encapsulation -->
<module>tablesaw</module>
<module>tensorflow-java</module>
<module>testing-modules</module>

View File

@ -0,0 +1,10 @@
--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED
--add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED
--add-exports jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED
--add-exports jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED
--add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED
--add-exports jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED
--add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED
--add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
--add-opens jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED
--add-opens jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED

View File

@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>static-analysis</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>error-prone-project</artifactId>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<release>17</release>
<encoding>UTF-8</encoding>
<fork>true</fork>
<compilerArgs>
<arg>-XDcompilePolicy=simple</arg>
<arg>-Xplugin:ErrorProne</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED</arg>
<arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED</arg>
<arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED</arg>
</compilerArgs>
<annotationProcessorPaths>
<path>
<groupId>com.google.errorprone</groupId>
<artifactId>error_prone_core</artifactId>
<version>${error-prone.version}</version>
</path>
<path>
<groupId>com.baeldung</groupId>
<artifactId>my-bugchecker-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

View File

@ -0,0 +1,12 @@
package com.baeldung;
public class BuggyClass {
public static void main(String[] args) {
if (args.length == 0 || args[0] != null) {
new IllegalArgumentException();
}
}
public void emptyMethod() {
}
}

View File

@ -0,0 +1,6 @@
package com.baeldung;
public class ClassWithEmptyMethod {
public void theEmptyMethod() {
}
}

View File

@ -0,0 +1,56 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>static-analysis</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<packaging>jar</packaging>
<artifactId>my-bugchecker-plugin</artifactId>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>com.google.auto.service</groupId>
<artifactId>auto-service</artifactId>
<version>${google-auto-service.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.google.errorprone</groupId>
<artifactId>error_prone_annotation</artifactId>
<version>${error-prone.version}</version>
</dependency>
<dependency>
<groupId>com.google.errorprone</groupId>
<artifactId>error_prone_check_api</artifactId>
<version>${error-prone.version}</version>
</dependency>
<dependency>
<groupId>com.google.auto.service</groupId>
<artifactId>auto-service-annotations</artifactId>
<version>${google-auto-service.version}</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

View File

@ -0,0 +1,24 @@
package com.baeldung;
import com.google.auto.service.AutoService;
import com.google.errorprone.BugPattern;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker;
import com.google.errorprone.fixes.SuggestedFix;
import com.google.errorprone.matchers.Description;
import com.sun.source.tree.MethodTree;
@AutoService(BugChecker.class)
@BugPattern(name = "EmptyMethodCheck", summary = "Empty methods should be deleted", severity = BugPattern.SeverityLevel.ERROR)
public class EmptyMethodChecker extends BugChecker implements BugChecker.MethodTreeMatcher {
@Override
public Description matchMethod(MethodTree methodTree, VisitorState visitorState) {
if (methodTree.getBody()
.getStatements()
.isEmpty()) {
return describeMatch(methodTree, SuggestedFix.delete(methodTree));
}
return Description.NO_MATCH;
}
}

View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>static-analysis</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>pmd</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

View File

@ -1,37 +1,37 @@
<?xml version="1.0"?>
<ruleset name="Custom 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 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
<description>
This ruleset checks my code for bad stuff
</description>
<!-- We'll use the entire 'strings' ruleset -->
<rule ref="rulesets/java/strings.xml" />
<!-- Here's some rules we'll specify one at a time -->
<rule ref="rulesets/java/unusedcode.xml/UnusedLocalVariable" />
<rule ref="rulesets/java/unusedcode.xml/UnusedPrivateField" />
<rule ref="rulesets/java/imports.xml/DuplicateImports" />
<rule ref="rulesets/java/basic.xml/UnnecessaryConversionTemporary" />
<!-- We want to customize this rule a bit, change the message and raise
the priority -->
<rule ref="rulesets/java/basic.xml/EmptyCatchBlock" message="Must handle exceptions">
<priority>2</priority>
</rule>
<!-- Now we'll customize a rule's property value -->
<rule ref="rulesets/java/codesize.xml/CyclomaticComplexity">
<properties>
<property name="reportLevel" value="5" />
</properties>
</rule>
<!-- We want everything from braces.xml except WhileLoopsMustUseBraces -->
<rule ref="rulesets/java/braces.xml">
<exclude name="WhileLoopsMustUseBraces" />
</rule>
<?xml version="1.0"?>
<ruleset name="Custom 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 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
<description>
This ruleset checks my code for bad stuff
</description>
<!-- We'll use the entire 'strings' ruleset -->
<rule ref="rulesets/java/strings.xml" />
<!-- Here's some rules we'll specify one at a time -->
<rule ref="rulesets/java/unusedcode.xml/UnusedLocalVariable" />
<rule ref="rulesets/java/unusedcode.xml/UnusedPrivateField" />
<rule ref="rulesets/java/imports.xml/DuplicateImports" />
<rule ref="rulesets/java/basic.xml/UnnecessaryConversionTemporary" />
<!-- We want to customize this rule a bit, change the message and raise
the priority -->
<rule ref="rulesets/java/basic.xml/EmptyCatchBlock" message="Must handle exceptions">
<priority>2</priority>
</rule>
<!-- Now we'll customize a rule's property value -->
<rule ref="rulesets/java/codesize.xml/CyclomaticComplexity">
<properties>
<property name="reportLevel" value="5" />
</properties>
</rule>
<!-- We want everything from braces.xml except WhileLoopsMustUseBraces -->
<rule ref="rulesets/java/braces.xml">
<exclude name="WhileLoopsMustUseBraces" />
</rule>
</ruleset>

View File

@ -6,6 +6,7 @@
<artifactId>static-analysis</artifactId>
<version>1.0-SNAPSHOT</version>
<name>static-analysis</name>
<packaging>pom</packaging>
<parent>
<groupId>com.baeldung</groupId>
@ -29,4 +30,15 @@
</plugins>
</reporting>
</project>
<modules>
<module>pmd</module>
<module>my-bugchecker-plugin</module>
<module>error-prone-project</module>
</modules>
<properties>
<error-prone.version>2.23.0</error-prone.version>
<google-auto-service.version>1.0.1</google-auto-service.version>
</properties>
</project>