Added examples for Nullable/NotNull Annotations,

more/better PMD rules
spotbugs
This commit is contained in:
Konrad Neitzel 2022-07-16 21:04:53 +02:00
parent d0a5ceacac
commit 8aab694bc0
5 changed files with 136 additions and 4 deletions

31
pmd-ruleset.xml Normal file
View File

@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="Custom Java 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>
Custom ruleset for Java
</description>
<rule ref="category/java/bestpractices.xml">
<!-- System.println is ok in simple Console App -->
<exclude name="SystemPrintln" />
</rule>
<rule ref="category/java/codestyle.xml" />
<rule ref="category/java/design.xml" />
<rule ref="category/java/documentation.xml" />
<rule ref="category/java/errorprone.xml" />
<rule ref="category/java/multithreading.xml" />
<rule ref="category/java/performance.xml" />
<rule ref="category/java/security.xml" />
</ruleset>

28
pom.xml
View File

@ -38,6 +38,7 @@
<codehaus.version.plugin>2.11.0</codehaus.version.plugin>
<spotbugs.maven.plugin>4.7.1.0</spotbugs.maven.plugin>
<spotbugs.version>4.7.1</spotbugs.version>
<jetbrains.annotations.version>23.0.0</jetbrains.annotations.version>
<!-- other properties -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@ -61,6 +62,14 @@
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- Dependency used for @NotNull / @Nullable -->
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>${jetbrains.annotations.version}</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
@ -227,6 +236,18 @@
<version>${spotbugs.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<!-- pmd does not stop build when violations are found -->
<goal>spotbugs</goal>
<!-- check stops the build when violations are found -->
<!-- <goal>check</goal> -->
</goals>
</execution>
</executions>
</plugin>
<plugin>
@ -265,8 +286,13 @@
</configuration>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>check</goal>
<!-- pmd does not stop build when violations are found -->
<goal>pmd</goal>
<!-- check stops the build when violations are found -->
<!-- <goal>check</goal> -->
</goals>
</execution>
</executions>

View File

@ -0,0 +1,57 @@
package de.kneitzel;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Simple Greeting class for some demonstration.
*/
public class Greeting {
/**
* Default name that should be greeted if no name is given.
*/
public static final String DEFAULT_NAME = "Welt";
/**
* Name that should be greeted
* <p>
* Cannot be null.
* </p>
*/
@NotNull
private final String name;
/**
* Gets the name that should be greeted.
* @return Name that should be greeted.
*/
public @NotNull String getName() {
return name;
}
/**
* Creates a new Instance of Greeting which greets the "World".
*/
public Greeting() {
this(null);
}
/**
* Creates a new instance of Greeting to greet the given name.
* @param name Name that should be greeted by Greeting.
*/
public Greeting(@Nullable final String name) {
this.name = name == null ? DEFAULT_NAME : name;
}
/**
* String representation of this instance.
* @return String representation of "Hello name!"
*/
@NotNull
@Override
public String toString() {
return "Hallo " + name + "!";
}
}

View File

@ -1,7 +1,23 @@
package de.kneitzel;
public class JavaApp {
public static void main(String[] args) {
System.out.println("Hello World!");
import org.jetbrains.annotations.NotNull;
/**
* Starting point of the JavaApp
*/
public final class JavaApp {
/**
* Provate Constructor - we never create an instance!
*/
private JavaApp() {}
/**
* Entry point of the application.
* @param args Commandline parameters.
*/
public static void main(@NotNull final String[] args) {
final Greeting greeting = new Greeting(null);
System.out.println(greeting);
}
}

View File

@ -1,2 +1,4 @@
module AppModule {
// Jetbrains Dependency: @NotNull / @Nullable
requires org.jetbrains.annotations;
}