Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional <until> element ignores violations since a specific Java version #313

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -99,21 +102,34 @@ public static Map<String, Violation> 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<String> 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);
}

return map;
}

private static int parseVersion(final String version) {
return Integer.parseInt(
version.startsWith("1.") ? version.substring(2) : version);
}

private static <T> OptionalInt mapToInt(final Optional<T> optional,
final ToIntFunction<T> mapper) {
return optional.isPresent() ?
OptionalInt.of(mapper.applyAsInt(optional.get())) :
OptionalInt.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +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()) &&
!ignorePackages.contains(packageName)) {
if (ignoreClass()) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,24 @@

package org.gaul.modernizer_maven_plugin;

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);
}

Expand All @@ -36,6 +45,10 @@ public int getVersion() {
return version;
}

public OptionalInt getUntil() {
return until;
}

public String getComment() {
return comment;
}
Expand Down
69 changes: 66 additions & 3 deletions modernizer-maven-plugin/src/main/resources/modernizer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1214,27 +1214,55 @@ violation names use the same format that javap emits.
<violation>
<name>java/io/File."&lt;init&gt;":(Ljava/lang/String;)V</name>
<version>7</version>
<until>11</until>
<comment>Prefer java.nio.file.Paths.get(String)</comment>
</violation>

<violation>
<name>java/io/File."&lt;init&gt;":(Ljava/lang/String;)V</name>
<version>11</version>
<comment>Prefer java.nio.file.Path.of(String)</comment>
</violation>

<violation>
<name>java/io/File."&lt;init&gt;":(Ljava/lang/String;Ljava/lang/String;)V</name>
<version>7</version>
<until>11</until>
<comment>Prefer java.nio.file.Paths.get(String, String...)</comment>
</violation>

<violation>
<name>java/io/File."&lt;init&gt;":(Ljava/lang/String;Ljava/lang/String;)V</name>
<version>11</version>
<comment>Prefer java.nio.file.Path.of(String, String...)</comment>
</violation>

<violation>
<name>java/io/File."&lt;init&gt;":(Ljava/net/URI;)V</name>
<version>7</version>
<until>11</until>
<comment>Prefer java.nio.file.Paths.get(URI)</comment>
</violation>

<violation>
<name>java/io/File."&lt;init&gt;":(Ljava/net/URI;)V</name>
<version>11</version>
<comment>Prefer java.nio.file.Path.of(URI)</comment>
</violation>

<violation>
<name>java/io/FileInputStream."&lt;init&gt;":(Ljava/lang/String;)V</name>
<version>7</version>
<until>11</until>
<comment>Prefer java.nio.file.Files.newInputStream(java.nio.file.Paths.get(String))</comment>
</violation>

<violation>
<name>java/io/FileInputStream."&lt;init&gt;":(Ljava/lang/String;)V</name>
<version>11</version>
<comment>Prefer java.nio.file.Files.newInputStream(java.nio.file.Path.of(String))</comment>
</violation>

<violation>
<name>java/io/FileInputStream."&lt;init&gt;":(Ljava/io/File;)V</name>
<version>7</version>
Expand All @@ -1244,15 +1272,29 @@ violation names use the same format that javap emits.
<violation>
<name>java/io/FileOutputStream."&lt;init&gt;":(Ljava/lang/String;)V</name>
<version>7</version>
<until>11</until>
<comment>Prefer java.nio.file.Files.newOutputStream(java.nio.file.Paths.get(String))</comment>
</violation>

<violation>
<name>java/io/FileOutputStream."&lt;init&gt;":(Ljava/lang/String;)V</name>
<version>11</version>
<comment>Prefer java.nio.file.Files.newOutputStream(Path.of(String))</comment>
</violation>

<violation>
<name>java/io/FileOutputStream."&lt;init&gt;":(Ljava/lang/String;Z)V</name>
<version>7</version>
<until>11</until>
<comment>Prefer java.nio.file.Files.newOutputStream(java.nio.file.Paths.get(String), CREATE, APPEND, WRITE)</comment>
</violation>

<violation>
<name>java/io/FileOutputStream."&lt;init&gt;":(Ljava/lang/String;Z)V</name>
<version>11</version>
<comment>Prefer java.nio.file.Files.newOutputStream(Path.of(String), CREATE, APPEND, WRITE)</comment>
</violation>

<violation>
<name>java/io/FileOutputStream."&lt;init&gt;":(Ljava/io/File;)V</name>
<version>7</version>
Expand All @@ -1274,9 +1316,16 @@ violation names use the same format that javap emits.
<violation>
<name>java/io/FileReader."&lt;init&gt;":(Ljava/lang/String;)V</name>
<version>7</version>
<until>11</until>
<comment>Prefer java.nio.file.Files.newBufferedReader.&lt;init&gt;(java.nio.file.Paths.get(String))</comment>
</violation>

<violation>
<name>java/io/FileReader."&lt;init&gt;":(Ljava/lang/String;)V</name>
<version>11</version>
<comment>Prefer java.nio.file.Files.newBufferedReader.&lt;init&gt;(Path.of(String))</comment>
</violation>

<violation>
<name>java/io/FileReader."&lt;init&gt;":(Ljava/io/File;Ljava/nio/charset/Charset;)V</name>
<version>11</version>
Expand All @@ -1286,7 +1335,7 @@ violation names use the same format that javap emits.
<violation>
<name>java/io/FileReader."&lt;init&gt;":(Ljava/lang/String;Ljava/nio/charset/Charset;)V</name>
<version>11</version>
<comment>Prefer java.nio.file.Files.newBufferedReader.&lt;init&gt;(Paths.get(String), Charset)</comment>
<comment>Prefer java.nio.file.Files.newBufferedReader.&lt;init&gt;(Path.of(String), Charset)</comment>
</violation>

<violation>
Expand All @@ -1304,15 +1353,29 @@ violation names use the same format that javap emits.
<violation>
<name>java/io/FileWriter."&lt;init&gt;":(Ljava/lang/String;)V</name>
<version>7</version>
<until>11</until>
<comment>Prefer java.nio.file.Files.newBufferedWriter.&lt;init&gt;(Paths.get(String))</comment>
</violation>

<violation>
<name>java/io/FileWriter."&lt;init&gt;":(Ljava/lang/String;)V</name>
<version>11</version>
<comment>Prefer java.nio.file.Files.newBufferedWriter.&lt;init&gt;(Path.of(String))</comment>
</violation>

<violation>
<name>java/io/FileWriter."&lt;init&gt;":(Ljava/lang/String;Z)V</name>
<version>7</version>
<until>11</until>
<comment>Prefer java.nio.file.Files.newBufferedWriter.&lt;init&gt;(Paths.get(String), java.nio.file.StandardOpenOption.CREATE, java.nio.file.StandardOpenOption.WRITE)</comment>
</violation>

<violation>
<name>java/io/FileWriter."&lt;init&gt;":(Ljava/lang/String;Z)V</name>
<version>11</version>
<comment>Prefer java.nio.file.Files.newBufferedWriter.&lt;init&gt;(Path.of(String), CREATE, WRITE)</comment>
</violation>

<violation>
<name>java/io/FileWriter."&lt;init&gt;":(Ljava/io/File;Ljava/nio/charset/Charset;)V</name>
<version>11</version>
Expand All @@ -1328,13 +1391,13 @@ violation names use the same format that javap emits.
<violation>
<name>java/io/FileWriter."&lt;init&gt;":(Ljava/lang/String;Ljava/nio/charset/Charset;)V</name>
<version>11</version>
<comment>Prefer java.nio.file.Files.newBufferedWriter.&lt;init&gt;(Paths.get(String), Charset)</comment>
<comment>Prefer java.nio.file.Files.newBufferedWriter.&lt;init&gt;(Path.of(String), Charset)</comment>
</violation>

<violation>
<name>java/io/FileWriter."&lt;init&gt;":(Ljava/lang/String;Ljava/nio/charset/Charset;Z)V</name>
<version>11</version>
<comment>Prefer java.nio.file.Files.newBufferedWriter.&lt;init&gt;(Paths.get(String), Charset, java.nio.file.StandardOpenOption.CREATE, java.nio.file.StandardOpenOption.WRITE)</comment>
<comment>Prefer java.nio.file.Files.newBufferedWriter.&lt;init&gt;(Path.of(String), Charset, java.nio.file.StandardOpenOption.CREATE, java.nio.file.StandardOpenOption.WRITE)</comment>
</violation>

<violation>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ public void testAnnotationViolation() throws Exception {
String name = TestAnnotation.class.getName().replace('.', '/');
Map<String, Violation> testViolations = Maps.newHashMap();
testViolations.put(name,
new Violation(name, 5, ""));
new Violation(name, 5, OptionalInt.empty(), ""));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a until test that makes sure the intended violation messages are triggered depending on the requested version?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This did not work in my testing.

Modernizer modernizer = new Modernizer("1.5", testViolations,
NO_EXCLUSIONS, NO_EXCLUSION_PATTERNS, NO_IGNORED_PACKAGES,
NO_IGNORED_CLASS_NAMES, NO_EXCLUSION_PATTERNS);
Expand Down
Loading