Skip to content

Commit

Permalink
Merge pull request #1156 from uhafner/java17
Browse files Browse the repository at this point in the history
Switch the java version to 17
  • Loading branch information
uhafner authored Oct 26, 2024
2 parents 5ea4635 + 3e9037e commit 16026b2
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 78 deletions.
32 changes: 14 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,27 @@
[![Warnings](https://raw.githubusercontent.com/uhafner/codingstyle/main/badges/style.svg)](https://github.com/uhafner/codingstyle/actions/workflows/quality-monitor.yml)
[![Bugs](https://raw.githubusercontent.com/uhafner/codingstyle/main/badges/bugs.svg)](https://github.com/uhafner/codingstyle/actions/workflows/quality-monitor.yml)

Each Java project should follow a given coding style. I.e., all contributions to the source code should use the same
formatting rules, design principles, code patterns, idioms, etc. This coding style provides the set of rules that I
am using in my lectures about software development at Munich University of Applied Sciences.
Each Java project should follow a given coding style.
I.e., all contributions to the source code should use the same formatting rules, design principles, code patterns, idioms, etc.
This coding style provides the set of rules that I am using in my lectures about software development at Munich University of Applied Sciences.

This project describes the coding style in detail (currently only available in German) and serves as a template project.
It provides all necessary resources for a Java project to enforce this coding style using the following
static analysis tools via Maven (and partly in IntelliJ):
It provides all necessary resources for a Java project to enforce this coding style using the following static analysis tools via Maven (and partly in IntelliJ):
- [Checkstyle](https://checkstyle.org)
- [PMD](https://pmd.github.io/)
- [SpotBugs](https://spotbugs.github.io)
- [Error Prone](https://errorprone.info)

❗This project requires a JDK version of 11 or higher.❗
❗This project requires a JDK version of 17 or higher.❗

Moreover, this project provides some sample classes that already use this style guide. These classes can be used
as such but are not required in this project. These classes also use some additional libraries that are included
using the Maven dependency mechanism. If the sample classes are deleted, then the dependencies can be safely
deleted, too.
Moreover, this project provides some sample classes that already use this style guide.
These classes can be used as such but are not required in this project.
These classes also use some additional libraries that are included using the Maven dependency mechanism.
If the sample classes are deleted, then the dependencies can be safely deleted, too.

This project and the associated static analysis tools are already running in continuous integration: an example
CI pipeline is active for GitHub Actions. For [Jenkins](https://jenkins.io/) a full CI pipeline has been
configured that includes stages to compile, test, run static code analysis, run code coverage analysis,
and run mutation coverage analysis, see section [Continuous Integration](doc/Continuous-Integration.md) for details.
Additionally, some development tools are configured in this GitHub project to evaluate the quality of pull requests,
see section [integration of external tools](doc/Externe-Tool-Integration.md).
This project and the associated static analysis tools are already running in continuous integration: an example CI pipeline is active for GitHub Actions.
For [Jenkins](https://jenkins.io/) a full CI pipeline has been configured that includes stages to compile, test, run static code analysis, run code coverage analysis, and run mutation coverage analysis, see section [Continuous Integration](doc/Continuous-Integration.md) for details.
Additionally, some development tools are configured in this GitHub project to evaluate the quality of pull requests, see section [integration of external tools](doc/Externe-Tool-Integration.md).

Content of the style guide (only in German):
- [Formatierung](doc/Formatierung.md)
Expand All @@ -53,9 +49,9 @@ A lot of ideas in this style are based on the following path-breaking books abou
- [6] "Refactoring: Improving the Design of Existing Code", Martin Fowler, Addison Wesley, 1999
- [7] "Java by Comparison", Simon Harrer, Jörg Lenhard, Linus Dietz, Pragmatic Programmers, 2018

All documents in this project use the
[Creative Commons Attribution 4.0 International License](https://creativecommons.org/licenses/by/4.0/).
All documents in this project use the [Creative Commons Attribution 4.0 International License](https://creativecommons.org/licenses/by/4.0/).
Source code (snippets, examples, and classes) are using the [MIT license](https://en.wikipedia.org/wiki/MIT_License).

[![License: MIT](https://img.shields.io/badge/license-MIT-yellow.svg)](https://en.wikipedia.org/wiki/MIT_License)
[![License: CC BY 4.0](https://img.shields.io/badge/License-CC%20BY%204.0-lightgrey.svg)](https://creativecommons.org/licenses/by/4.0/)
![JDK17](https://img.shields.io/badge/jdk-17-blue.svg)
30 changes: 24 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>edu.hm.hafner</groupId>
<artifactId>codingstyle</artifactId>
<version>4.15.0-SNAPSHOT</version>
<version>5.0.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Java coding style</name>
Expand Down Expand Up @@ -57,7 +57,7 @@
<properties>
<source.encoding>UTF-8</source.encoding>
<project.build.sourceEncoding>${source.encoding}</project.build.sourceEncoding>
<java.version>11</java.version>
<java.version>17</java.version>

<module.name>${project.groupId}.codingstyle</module.name>

Expand Down Expand Up @@ -91,7 +91,7 @@
<maven-jar-plugin.version>3.4.2</maven-jar-plugin.version>
<maven-pmd-plugin.version>3.25.0</maven-pmd-plugin.version>
<pmd.version>7.6.0</pmd.version>
<maven-checkstyle-plugin.version>3.5.0</maven-checkstyle-plugin.version>
<maven-checkstyle-plugin.version>3.6.0</maven-checkstyle-plugin.version>
<checkstyle.version>10.18.2</checkstyle.version>
<spotbugs-maven-plugin.version>4.8.6.4</spotbugs-maven-plugin.version>
<findsecbugs-plugin.version>1.13.0</findsecbugs-plugin.version>
Expand Down Expand Up @@ -377,18 +377,24 @@
<exclusions>
<exclusion>**/docker/**</exclusion>
<exclusion>**/AbstractComparableTest.java</exclusion>
<exclusion>**/AbstractEqualsTest.java</exclusion>
<exclusion>**/EnsureTest.java</exclusion>
<exclusion>**/ResourceExtractorTest.java</exclusion>
</exclusions>
<activeRecipes>
<recipe>org.openrewrite.maven.BestPractices</recipe>
<recipe>org.openrewrite.maven.RemoveRedundantDependencyVersions</recipe>
<recipe>org.openrewrite.staticanalysis.AddSerialAnnotationToSerialVersionUID</recipe>
<recipe>org.openrewrite.staticanalysis.MissingOverrideAnnotation</recipe>
<recipe>org.openrewrite.staticanalysis.CodeCleanup</recipe>
<recipe>org.openrewrite.staticanalysis.CommonStaticAnalysis</recipe>
<recipe>org.openrewrite.staticanalysis.RemoveExtraSemicolons</recipe>
<recipe>org.openrewrite.java.migrate.Java8toJava11</recipe>
<recipe>org.openrewrite.java.RemoveUnusedImports</recipe>
<recipe>org.openrewrite.java.migrate.UpgradeToJava17</recipe>
<recipe>org.openrewrite.java.migrate.util.SequencedCollection</recipe>
<recipe>org.openrewrite.java.migrate.lang.var.UseVarForObject</recipe>
<recipe>org.openrewrite.java.migrate.net.JavaNetAPIs</recipe>
<recipe>org.openrewrite.java.migrate.util.JavaUtilAPIs</recipe>
<recipe>org.openrewrite.java.migrate.lang.StringRulesRecipes</recipe>
<recipe>org.openrewrite.java.format.RemoveTrailingWhitespace</recipe>
<recipe>org.openrewrite.java.format.BlankLines</recipe>
<recipe>org.openrewrite.java.format.EmptyNewlineAtEndOfFile</recipe>
Expand Down Expand Up @@ -678,7 +684,7 @@
<configuration>
<rules>
<requireJavaVersion>
<version>11</version>
<version>${java.version}</version>
</requireJavaVersion>
</rules>
</configuration>
Expand Down Expand Up @@ -888,6 +894,17 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>properties</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
Expand All @@ -898,6 +915,7 @@
<exclude>**/*ITest.*</exclude>
<exclude>**/*ArchitectureRulesTest$*</exclude>
</excludes>
<argLine>@{argLine} -javaagent:${org.mockito:mockito-core:jar}</argLine>
</configuration>
<dependencies>
<dependency>
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/edu/hm/hafner/util/Ensure.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public static void thatStatementIsNeverReached(final String explanation, final O
*/
@FormatMethod
private static void throwException(final String message, @CheckForNull final Object... args) {
throw new AssertionError(String.format(message, args));
throw new AssertionError(message.formatted(args));
}

/**
Expand All @@ -185,7 +185,7 @@ private static void throwException(final String message, @CheckForNull final Obj
*/
@FormatMethod
private static void throwNullPointerException(final String message, final Object... args) {
throw new NullPointerException(String.format(message, args)); // NOPMD
throw new NullPointerException(message.formatted(args)); // NOPMD
}

private Ensure() {
Expand Down Expand Up @@ -764,7 +764,7 @@ public ExceptionCondition(@CheckForNull final Throwable value) {
*/
@FormatMethod
public void isNeverThrown(final String explanation, final Object... args) {
throw new AssertionError(String.format(explanation, args), value);
throw new AssertionError(explanation.formatted(args), value);
}
}
}
6 changes: 4 additions & 2 deletions src/main/java/edu/hm/hafner/util/FilteredLog.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package edu.hm.hafner.util;

import java.io.Serial;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -21,6 +22,7 @@
* @author Ullrich Hafner
*/
public class FilteredLog implements Serializable {
@Serial
private static final long serialVersionUID = -8552323621953159904L;

private static final int DEFAULT_MAX_LINES = 20;
Expand Down Expand Up @@ -105,7 +107,7 @@ public void logInfo(final String message) {
*/
@FormatMethod
public void logInfo(final String format, final Object... args) {
logInfo(String.format(format, args));
logInfo(format.formatted(args));
}

/**
Expand Down Expand Up @@ -142,7 +144,7 @@ public void logError(final String message) {
*/
@FormatMethod
public void logError(final String format, final Object... args) {
logError(String.format(format, args));
logError(format.formatted(args));
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/edu/hm/hafner/util/LineRange.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package edu.hm.hafner.util;

import java.io.Serial;
import java.io.Serializable;
import java.util.Locale;
import java.util.NavigableSet;
Expand All @@ -12,6 +13,7 @@
* @author Ullrich Hafner
*/
public final class LineRange implements Serializable {
@Serial
private static final long serialVersionUID = -4124143085672930110L;

private final int start;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/edu/hm/hafner/util/LineRangeList.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package edu.hm.hafner.util;

import java.io.Serial;
import java.io.Serializable;
import java.util.AbstractList;
import java.util.Arrays;
Expand Down Expand Up @@ -35,6 +36,7 @@
* @author Kohsuke Kawaguchi
*/
public class LineRangeList extends AbstractList<LineRange> implements Serializable {
@Serial
private static final long serialVersionUID = -1123973098942984623L;
private static final int DEFAULT_CAPACITY = 16;
private static final boolean SEQUENTIAL = false;
Expand Down Expand Up @@ -133,9 +135,7 @@ private void ensure(final int n) {

@Override
public boolean contains(final Object o) {
if (o instanceof LineRange) {
var other = (LineRange) o;

if (o instanceof final LineRange other) {

Check warning on line 138 in src/main/java/edu/hm/hafner/util/LineRangeList.java

View workflow job for this annotation

GitHub Actions / Quality Monitor

Partially covered line

Line 138 is only partially covered, one branch is missing
for (var cursor = new Cursor(); cursor.hasNext();) {

Check warning on line 139 in src/main/java/edu/hm/hafner/util/LineRangeList.java

View workflow job for this annotation

GitHub Actions / Quality Monitor

Partially covered line

Line 139 is only partially covered, one branch is missing
if (cursor.compare(other)) {

Check warning on line 140 in src/main/java/edu/hm/hafner/util/LineRangeList.java

View workflow job for this annotation

GitHub Actions / Quality Monitor

Mutation survived

One mutation survived in line 140 (NegateConditionalsMutator)
Raw output
Survived mutations:
- negated conditional (org.pitest.mutationtest.engine.gregor.mutators.NegateConditionalsMutator)
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/edu/hm/hafner/util/LookaheadStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,6 @@ public int getLine() {

@Override @Generated
public String toString() {
return String.format("[%d] -> '%s'", line, lookaheadLine);
return "[%d] -> '%s'".formatted(line, lookaheadLine);
}
}
4 changes: 2 additions & 2 deletions src/main/java/edu/hm/hafner/util/PrefixLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public PrefixLogger(final PrintStream logger, final String prefix) {
this.toolName = prefix + " ";
}
else {
this.toolName = String.format("[%s] ", prefix);
this.toolName = "[%s] ".formatted(prefix);
}
delegate = logger;
}
Expand All @@ -44,7 +44,7 @@ public PrefixLogger(final PrintStream logger, final String prefix) {
*/
@FormatMethod
public void log(final String format, final Object... args) {
print(String.format(format, args));
print(format.formatted(args));
}

/**
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/edu/hm/hafner/util/SecureXmlParserFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import java.io.Reader;
import java.io.Serial;
import java.nio.charset.Charset;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
Expand Down Expand Up @@ -341,6 +342,7 @@ TransformerFactory createTransformerFactory() {
* Indicates that during parsing a non-recoverable error has been occurred.
*/
public static class ParsingException extends RuntimeException {
@Serial
private static final long serialVersionUID = -9016364685084958944L;

/**
Expand Down Expand Up @@ -368,7 +370,7 @@ public ParsingException(final Throwable cause) {
*/
@FormatMethod
public ParsingException(final String messageFormat, final Object... args) {
super(String.format(messageFormat, args));
super(messageFormat.formatted(args));
}

/**
Expand All @@ -388,11 +390,11 @@ public ParsingException(final String messageFormat, final Object... args) {
*/
@FormatMethod
public ParsingException(final Throwable cause, final String messageFormat, final Object... args) {
super(createMessage(cause, String.format(messageFormat, args)), cause);
super(createMessage(cause, messageFormat.formatted(args)), cause);
}

private static String createMessage(final Throwable cause, final String message) {
return String.format("%s%n%s%n%s", message,
return "%s%n%s%n%s".formatted(message,
ExceptionUtils.getMessage(cause), ExceptionUtils.getStackTrace(cause));
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/edu/hm/hafner/util/TreeString.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package edu.hm.hafner.util;

import java.io.Serial;
import java.io.Serializable;
import java.util.Map;

Expand All @@ -18,6 +19,7 @@
* @author Kohsuke Kawaguchi
*/
public final class TreeString implements Serializable {
@Serial
private static final long serialVersionUID = 3621959682117480904L;

/** Parent node that represents the prefix. */
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/edu/hm/hafner/util/ArchitectureRules.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public void check(final JavaMethod method, final ConditionEvents events) {
return;
}
events.add(SimpleConditionEvent.violated(method,
String.format("%s is not protected but the class might be extended in %s",
"%s is not protected but the class might be extended in %s".formatted(
method.getDescription(), method.getSourceCodeLocation())));
}
}
Expand Down
Loading

0 comments on commit 16026b2

Please sign in to comment.