From de740030d64b95c91fb19ef72ca2905fdbf59846 Mon Sep 17 00:00:00 2001 From: Markus KARG Date: Sat, 4 Jan 2025 17:42:15 +0000 Subject: [PATCH 1/3] Optional element ignores violations since a specific Java version --- .../modernizer_maven_plugin/Modernizer.java | 28 ++++++-- .../ModernizerClassVisitor.java | 1 + .../modernizer_maven_plugin/Violation.java | 14 ++++ .../src/main/resources/modernizer.xml | 69 ++++++++++++++++++- .../ModernizerTest.java | 2 +- 5 files changed, 104 insertions(+), 10 deletions(-) diff --git a/modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/Modernizer.java b/modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/Modernizer.java index 54189aa2..ca287673 100644 --- a/modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/Modernizer.java +++ b/modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/Modernizer.java @@ -21,7 +21,10 @@ import java.util.Collection; import java.util.HashMap; import java.util.Map; +import java.util.Optional; +import java.util.OptionalInt; import java.util.Set; +import java.util.function.ToIntFunction; import java.util.regex.Pattern; import javax.xml.parsers.DocumentBuilder; @@ -99,16 +102,17 @@ public static Map parseFromXml(InputStream is) Element element = (Element) nNode; String version = element.getElementsByTagName("version").item(0) .getTextContent(); - int versionNum; - if (version.startsWith("1.")) { - versionNum = Integer.parseInt(version.substring(2)); - } else { - versionNum = Integer.parseInt(version); - } + int versionNum = parseVersion(version); + Optional versionLimit = Optional.ofNullable( + element.getElementsByTagName("until").item(0)) + .map(Node::getTextContent); + OptionalInt versionLimitNum = mapToInt(versionLimit, + Modernizer::parseVersion); Violation violation = new Violation( element.getElementsByTagName("name").item(0) .getTextContent(), versionNum, + versionLimitNum, element.getElementsByTagName("comment").item(0) .getTextContent()); map.put(violation.getName(), violation); @@ -116,4 +120,16 @@ public static Map parseFromXml(InputStream is) return map; } + + private static int parseVersion(final String version) { + return Integer.parseInt( + version.startsWith("1.") ? version.substring(2) : version); + } + + private static OptionalInt mapToInt(final Optional optional, + final ToIntFunction mapper) { + return optional.isPresent() ? + OptionalInt.of(mapper.applyAsInt(optional.get())) : + OptionalInt.empty(); + } } diff --git a/modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/ModernizerClassVisitor.java b/modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/ModernizerClassVisitor.java index cd18aee4..abef6fe7 100644 --- a/modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/ModernizerClassVisitor.java +++ b/modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/ModernizerClassVisitor.java @@ -157,6 +157,7 @@ private void checkToken(String token, Violation violation, String name, int lineNumber) { if (violation != null && !exclusions.contains(token) && javaVersion >= violation.getVersion() && + (!violation.getUntil().isPresent() || javaVersion < violation.getUntil().getAsInt()) && !ignorePackages.contains(packageName)) { if (ignoreClass()) { return; diff --git a/modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/Violation.java b/modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/Violation.java index 530a6932..82b456b8 100644 --- a/modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/Violation.java +++ b/modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/Violation.java @@ -16,15 +16,25 @@ package org.gaul.modernizer_maven_plugin; +import java.util.Optional; +import java.util.OptionalInt; + public final class Violation { private final String name; private final int version; + private final OptionalInt until; // ignoring violation since this version private final String comment; + @Deprecated Violation(String name, int version, String comment) { + this(name, version, OptionalInt.empty(), comment); + } + + Violation(String name, int version, OptionalInt until, String comment) { this.name = Utils.checkNotNull(name); Utils.checkArgument(version >= 0); this.version = version; + this.until = Utils.checkNotNull(until); this.comment = Utils.checkNotNull(comment); } @@ -36,6 +46,10 @@ public int getVersion() { return version; } + public OptionalInt getUntil() { + return until; + } + public String getComment() { return comment; } diff --git a/modernizer-maven-plugin/src/main/resources/modernizer.xml b/modernizer-maven-plugin/src/main/resources/modernizer.xml index a7a93349..5320bc09 100644 --- a/modernizer-maven-plugin/src/main/resources/modernizer.xml +++ b/modernizer-maven-plugin/src/main/resources/modernizer.xml @@ -1214,27 +1214,55 @@ violation names use the same format that javap emits. java/io/File."<init>":(Ljava/lang/String;)V 7 + 11 Prefer java.nio.file.Paths.get(String) + + java/io/File."<init>":(Ljava/lang/String;)V + 11 + Prefer java.nio.file.Path.of(String) + + java/io/File."<init>":(Ljava/lang/String;Ljava/lang/String;)V 7 + 11 Prefer java.nio.file.Paths.get(String, String...) + + java/io/File."<init>":(Ljava/lang/String;Ljava/lang/String;)V + 11 + Prefer java.nio.file.Path.of(String, String...) + + java/io/File."<init>":(Ljava/net/URI;)V 7 + 11 Prefer java.nio.file.Paths.get(URI) + + java/io/File."<init>":(Ljava/net/URI;)V + 11 + Prefer java.nio.file.Path.of(URI) + + java/io/FileInputStream."<init>":(Ljava/lang/String;)V 7 + 11 Prefer java.nio.file.Files.newInputStream(java.nio.file.Paths.get(String)) + + java/io/FileInputStream."<init>":(Ljava/lang/String;)V + 11 + Prefer java.nio.file.Files.newInputStream(java.nio.file.Path.of(String)) + + java/io/FileInputStream."<init>":(Ljava/io/File;)V 7 @@ -1244,15 +1272,29 @@ violation names use the same format that javap emits. java/io/FileOutputStream."<init>":(Ljava/lang/String;)V 7 + 11 Prefer java.nio.file.Files.newOutputStream(java.nio.file.Paths.get(String)) + + java/io/FileOutputStream."<init>":(Ljava/lang/String;)V + 11 + Prefer java.nio.file.Files.newOutputStream(java.nio.file.Path.of(String)) + + java/io/FileOutputStream."<init>":(Ljava/lang/String;Z)V 7 + 11 Prefer java.nio.file.Files.newOutputStream(java.nio.file.Paths.get(String), CREATE, APPEND, WRITE) + + java/io/FileOutputStream."<init>":(Ljava/lang/String;Z)V + 11 + Prefer java.nio.file.Files.newOutputStream(java.nio.file.Path.of(String), CREATE, APPEND, WRITE) + + java/io/FileOutputStream."<init>":(Ljava/io/File;)V 7 @@ -1274,9 +1316,16 @@ violation names use the same format that javap emits. java/io/FileReader."<init>":(Ljava/lang/String;)V 7 + 11 Prefer java.nio.file.Files.newBufferedReader.<init>(java.nio.file.Paths.get(String)) + + java/io/FileReader."<init>":(Ljava/lang/String;)V + 11 + Prefer java.nio.file.Files.newBufferedReader.<init>(java.nio.file.Path.of(String)) + + java/io/FileReader."<init>":(Ljava/io/File;Ljava/nio/charset/Charset;)V 11 @@ -1286,7 +1335,7 @@ violation names use the same format that javap emits. java/io/FileReader."<init>":(Ljava/lang/String;Ljava/nio/charset/Charset;)V 11 - Prefer java.nio.file.Files.newBufferedReader.<init>(Paths.get(String), Charset) + Prefer java.nio.file.Files.newBufferedReader.<init>(Path.of(String), Charset) @@ -1304,15 +1353,29 @@ violation names use the same format that javap emits. java/io/FileWriter."<init>":(Ljava/lang/String;)V 7 + 11 Prefer java.nio.file.Files.newBufferedWriter.<init>(Paths.get(String)) + + java/io/FileWriter."<init>":(Ljava/lang/String;)V + 11 + Prefer java.nio.file.Files.newBufferedWriter.<init>(Path.of(String)) + + java/io/FileWriter."<init>":(Ljava/lang/String;Z)V 7 + 11 Prefer java.nio.file.Files.newBufferedWriter.<init>(Paths.get(String), java.nio.file.StandardOpenOption.CREATE, java.nio.file.StandardOpenOption.WRITE) + + java/io/FileWriter."<init>":(Ljava/lang/String;Z)V + 11 + Prefer java.nio.file.Files.newBufferedWriter.<init>(Path.of(String), java.nio.file.StandardOpenOption.CREATE, java.nio.file.StandardOpenOption.WRITE) + + java/io/FileWriter."<init>":(Ljava/io/File;Ljava/nio/charset/Charset;)V 11 @@ -1328,13 +1391,13 @@ violation names use the same format that javap emits. java/io/FileWriter."<init>":(Ljava/lang/String;Ljava/nio/charset/Charset;)V 11 - Prefer java.nio.file.Files.newBufferedWriter.<init>(Paths.get(String), Charset) + Prefer java.nio.file.Files.newBufferedWriter.<init>(Path.of(String), Charset) java/io/FileWriter."<init>":(Ljava/lang/String;Ljava/nio/charset/Charset;Z)V 11 - Prefer java.nio.file.Files.newBufferedWriter.<init>(Paths.get(String), Charset, java.nio.file.StandardOpenOption.CREATE, java.nio.file.StandardOpenOption.WRITE) + Prefer java.nio.file.Files.newBufferedWriter.<init>(Path.of(String), Charset, java.nio.file.StandardOpenOption.CREATE, java.nio.file.StandardOpenOption.WRITE) diff --git a/modernizer-maven-plugin/src/test/java/org/gaul/modernizer_maven_plugin/ModernizerTest.java b/modernizer-maven-plugin/src/test/java/org/gaul/modernizer_maven_plugin/ModernizerTest.java index 6a9b1632..6096ffca 100644 --- a/modernizer-maven-plugin/src/test/java/org/gaul/modernizer_maven_plugin/ModernizerTest.java +++ b/modernizer-maven-plugin/src/test/java/org/gaul/modernizer_maven_plugin/ModernizerTest.java @@ -390,7 +390,7 @@ public void testAnnotationViolation() throws Exception { String name = TestAnnotation.class.getName().replace('.', '/'); Map testViolations = Maps.newHashMap(); testViolations.put(name, - new Violation(name, 5, "")); + new Violation(name, 5, OptionalInt.empty(), "")); Modernizer modernizer = new Modernizer("1.5", testViolations, NO_EXCLUSIONS, NO_EXCLUSION_PATTERNS, NO_IGNORED_PACKAGES, NO_IGNORED_CLASS_NAMES, NO_EXCLUSION_PATTERNS); From 70447f8ab0f18189631da7f6172301e14df9afd3 Mon Sep 17 00:00:00 2001 From: Markus KARG Date: Sun, 12 Jan 2025 11:51:04 +0000 Subject: [PATCH 2/3] Omitting package names for parameters --- modernizer-maven-plugin/src/main/resources/modernizer.xml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/modernizer-maven-plugin/src/main/resources/modernizer.xml b/modernizer-maven-plugin/src/main/resources/modernizer.xml index 5320bc09..eb5410d5 100644 --- a/modernizer-maven-plugin/src/main/resources/modernizer.xml +++ b/modernizer-maven-plugin/src/main/resources/modernizer.xml @@ -1279,7 +1279,7 @@ violation names use the same format that javap emits. java/io/FileOutputStream."<init>":(Ljava/lang/String;)V 11 - Prefer java.nio.file.Files.newOutputStream(java.nio.file.Path.of(String)) + Prefer java.nio.file.Files.newOutputStream(Path.of(String)) @@ -1292,7 +1292,7 @@ violation names use the same format that javap emits. java/io/FileOutputStream."<init>":(Ljava/lang/String;Z)V 11 - Prefer java.nio.file.Files.newOutputStream(java.nio.file.Path.of(String), CREATE, APPEND, WRITE) + Prefer java.nio.file.Files.newOutputStream(Path.of(String), CREATE, APPEND, WRITE) @@ -1323,7 +1323,7 @@ violation names use the same format that javap emits. java/io/FileReader."<init>":(Ljava/lang/String;)V 11 - Prefer java.nio.file.Files.newBufferedReader.<init>(java.nio.file.Path.of(String)) + Prefer java.nio.file.Files.newBufferedReader.<init>(Path.of(String)) @@ -1373,7 +1373,7 @@ violation names use the same format that javap emits. java/io/FileWriter."<init>":(Ljava/lang/String;Z)V 11 - Prefer java.nio.file.Files.newBufferedWriter.<init>(Path.of(String), java.nio.file.StandardOpenOption.CREATE, java.nio.file.StandardOpenOption.WRITE) + Prefer java.nio.file.Files.newBufferedWriter.<init>(Path.of(String), CREATE, WRITE) From d1ee6ea74abcbce2d7625aa9a30f5cefbc55d630 Mon Sep 17 00:00:00 2001 From: Markus KARG Date: Sun, 12 Jan 2025 12:01:57 +0000 Subject: [PATCH 3/3] Fixed checkstyle violation --- .../gaul/modernizer_maven_plugin/ModernizerClassVisitor.java | 3 ++- .../main/java/org/gaul/modernizer_maven_plugin/Violation.java | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/ModernizerClassVisitor.java b/modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/ModernizerClassVisitor.java index abef6fe7..2dd3d80b 100644 --- a/modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/ModernizerClassVisitor.java +++ b/modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/ModernizerClassVisitor.java @@ -157,7 +157,8 @@ private void checkToken(String token, Violation violation, String name, int lineNumber) { if (violation != null && !exclusions.contains(token) && javaVersion >= violation.getVersion() && - (!violation.getUntil().isPresent() || javaVersion < violation.getUntil().getAsInt()) && + (!violation.getUntil().isPresent() || + javaVersion < violation.getUntil().getAsInt()) && !ignorePackages.contains(packageName)) { if (ignoreClass()) { return; diff --git a/modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/Violation.java b/modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/Violation.java index 82b456b8..fae20833 100644 --- a/modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/Violation.java +++ b/modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/Violation.java @@ -16,7 +16,6 @@ package org.gaul.modernizer_maven_plugin; -import java.util.Optional; import java.util.OptionalInt; public final class Violation {