Skip to content

Commit dc95481

Browse files
committed
Optional <until> element ignores violations since a specific Java version
1 parent f120fab commit dc95481

File tree

5 files changed

+106
-12
lines changed

5 files changed

+106
-12
lines changed

modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/Modernizer.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121
import java.util.Collection;
2222
import java.util.HashMap;
2323
import java.util.Map;
24+
import java.util.Optional;
25+
import java.util.OptionalInt;
2426
import java.util.Set;
27+
import java.util.function.ToIntFunction;
2528
import java.util.regex.Pattern;
2629

2730
import javax.xml.parsers.DocumentBuilder;
@@ -99,21 +102,34 @@ public static Map<String, Violation> parseFromXml(InputStream is)
99102
Element element = (Element) nNode;
100103
String version = element.getElementsByTagName("version").item(0)
101104
.getTextContent();
102-
int versionNum;
103-
if (version.startsWith("1.")) {
104-
versionNum = Integer.parseInt(version.substring(2));
105-
} else {
106-
versionNum = Integer.parseInt(version);
107-
}
105+
int versionNum = parseVersion(version);
106+
Optional<String> versionLimit = Optional.ofNullable(
107+
element.getElementsByTagName("until").item(0))
108+
.map(Node::getTextContent);
109+
OptionalInt versionLimitNum = mapToInt(versionLimit,
110+
Modernizer::parseVersion);
108111
Violation violation = new Violation(
109112
element.getElementsByTagName("name").item(0)
110113
.getTextContent(),
111114
versionNum,
115+
versionLimitNum,
112116
element.getElementsByTagName("comment").item(0)
113117
.getTextContent());
114118
map.put(violation.getName(), violation);
115119
}
116120

117121
return map;
118122
}
123+
124+
private static int parseVersion(final String version) {
125+
return Integer.parseInt(
126+
version.startsWith("1.") ? version.substring(2) : version);
127+
}
128+
129+
private static <T> OptionalInt mapToInt(final Optional<T> optional,
130+
final ToIntFunction<T> mapper) {
131+
return optional.isPresent() ?
132+
OptionalInt.of(mapper.applyAsInt(optional.get())) :
133+
OptionalInt.empty();
134+
}
119135
}

modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/ModernizerClassVisitor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ private void checkToken(String token, Violation violation, String name,
157157
int lineNumber) {
158158
if (violation != null && !exclusions.contains(token) &&
159159
javaVersion >= violation.getVersion() &&
160+
(!violation.getUntil().isPresent() || javaVersion < violation.getUntil().getAsInt()) &&
160161
!ignorePackages.contains(packageName)) {
161162
if (ignoreClass()) {
162163
return;

modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/Violation.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,25 @@
1616

1717
package org.gaul.modernizer_maven_plugin;
1818

19+
import java.util.Optional;
20+
import java.util.OptionalInt;
21+
1922
public final class Violation {
2023
private final String name;
2124
private final int version;
25+
private final OptionalInt until; // ignoring violation since this version
2226
private final String comment;
2327

28+
@Deprecated
2429
Violation(String name, int version, String comment) {
30+
this(name, version, OptionalInt.empty(), comment);
31+
}
32+
33+
Violation(String name, int version, OptionalInt until, String comment) {
2534
this.name = Utils.checkNotNull(name);
2635
Utils.checkArgument(version >= 0);
2736
this.version = version;
37+
this.until = Utils.checkNotNull(until);
2838
this.comment = Utils.checkNotNull(comment);
2939
}
3040

@@ -36,6 +46,10 @@ public int getVersion() {
3646
return version;
3747
}
3848

49+
public OptionalInt getUntil() {
50+
return until;
51+
}
52+
3953
public String getComment() {
4054
return comment;
4155
}

modernizer-maven-plugin/src/main/resources/modernizer.xml

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,27 +1214,55 @@ violation names use the same format that javap emits.
12141214
<violation>
12151215
<name>java/io/File."&lt;init&gt;":(Ljava/lang/String;)V</name>
12161216
<version>7</version>
1217+
<until>11</until>
12171218
<comment>Prefer java.nio.file.Paths.get(String)</comment>
12181219
</violation>
12191220

1221+
<violation>
1222+
<name>java/io/File."&lt;init&gt;":(Ljava/lang/String;)V</name>
1223+
<version>11</version>
1224+
<comment>Prefer java.nio.file.Path.of(String)</comment>
1225+
</violation>
1226+
12201227
<violation>
12211228
<name>java/io/File."&lt;init&gt;":(Ljava/lang/String;Ljava/lang/String;)V</name>
12221229
<version>7</version>
1230+
<until>11</until>
12231231
<comment>Prefer java.nio.file.Paths.get(String, String...)</comment>
12241232
</violation>
12251233

1234+
<violation>
1235+
<name>java/io/File."&lt;init&gt;":(Ljava/lang/String;Ljava/lang/String;)V</name>
1236+
<version>11</version>
1237+
<comment>Prefer java.nio.file.Path.of(String, String...)</comment>
1238+
</violation>
1239+
12261240
<violation>
12271241
<name>java/io/File."&lt;init&gt;":(Ljava/net/URI;)V</name>
12281242
<version>7</version>
1243+
<until>11</until>
12291244
<comment>Prefer java.nio.file.Paths.get(URI)</comment>
12301245
</violation>
12311246

1247+
<violation>
1248+
<name>java/io/File."&lt;init&gt;":(Ljava/net/URI;)V</name>
1249+
<version>11</version>
1250+
<comment>Prefer java.nio.file.Path.of(URI)</comment>
1251+
</violation>
1252+
12321253
<violation>
12331254
<name>java/io/FileInputStream."&lt;init&gt;":(Ljava/lang/String;)V</name>
12341255
<version>7</version>
1256+
<until>11</until>
12351257
<comment>Prefer java.nio.file.Files.newInputStream(java.nio.file.Paths.get(String))</comment>
12361258
</violation>
12371259

1260+
<violation>
1261+
<name>java/io/FileInputStream."&lt;init&gt;":(Ljava/lang/String;)V</name>
1262+
<version>11</version>
1263+
<comment>Prefer java.nio.file.Files.newInputStream(java.nio.file.Path.of(String))</comment>
1264+
</violation>
1265+
12381266
<violation>
12391267
<name>java/io/FileInputStream."&lt;init&gt;":(Ljava/io/File;)V</name>
12401268
<version>7</version>
@@ -1244,13 +1272,27 @@ violation names use the same format that javap emits.
12441272
<violation>
12451273
<name>java/io/FileOutputStream."&lt;init&gt;":(Ljava/lang/String;)V</name>
12461274
<version>7</version>
1275+
<until>11</until>
12471276
<comment>Prefer java.nio.file.Files.newOutputStream(java.nio.file.Paths.get(String))</comment>
12481277
</violation>
12491278

1279+
<violation>
1280+
<name>java/io/FileOutputStream."&lt;init&gt;":(Ljava/lang/String;)V</name>
1281+
<version>11</version>
1282+
<comment>Prefer java.nio.file.Files.newOutputStream(java.nio.file.Path.of(String))</comment>
1283+
</violation>
1284+
12501285
<violation>
12511286
<name>java/io/FileOutputStream."&lt;init&gt;":(Ljava/lang/String;Z)V</name>
12521287
<version>7</version>
1253-
<comment>Prefer java.nio.file.Files.newOutputStream(java.nio.file.Paths.get(String), java.nio.file.StandardOpenOption.CREATE, java.nio.file.StandardOpenOption.CREATE)</comment>
1288+
<until>11</until>
1289+
<comment>Prefer java.nio.file.Files.newOutputStream(java.nio.file.Paths.get(String), java.nio.file.StandardOpenOption.CREATE, java.nio.file.StandardOpenOption.WRITE)</comment>
1290+
</violation>
1291+
1292+
<violation>
1293+
<name>java/io/FileOutputStream."&lt;init&gt;":(Ljava/lang/String;Z)V</name>
1294+
<version>11</version>
1295+
<comment>Prefer java.nio.file.Files.newOutputStream(java.nio.file.Path.of(String), java.nio.file.StandardOpenOption.CREATE, java.nio.file.StandardOpenOption.WRITE)</comment>
12541296
</violation>
12551297

12561298
<violation>
@@ -1262,7 +1304,7 @@ violation names use the same format that javap emits.
12621304
<violation>
12631305
<name>java/io/FileOutputStream."&lt;init&gt;":(Ljava/io/File;Z)V</name>
12641306
<version>7</version>
1265-
<comment>Prefer java.nio.file.Files.newOutputStream(java.nio.file.Path, java.nio.file.StandardOpenOption.CREATE, java.nio.file.StandardOpenOption.CREATE)</comment>
1307+
<comment>Prefer java.nio.file.Files.newOutputStream(java.nio.file.Path, java.nio.file.StandardOpenOption.CREATE, java.nio.file.StandardOpenOption.WRITE)</comment>
12661308
</violation>
12671309

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

1323+
<violation>
1324+
<name>java/io/FileReader."&lt;init&gt;":(Ljava/lang/String;)V</name>
1325+
<version>11</version>
1326+
<comment>Prefer java.nio.file.Files.newBufferedReader.&lt;init&gt;(java.nio.file.Path.of(String))</comment>
1327+
</violation>
1328+
12801329
<violation>
12811330
<name>java/io/FileReader."&lt;init&gt;":(Ljava/io/File;Ljava/nio/charset/Charset;)V</name>
12821331
<version>11</version>
@@ -1286,7 +1335,7 @@ violation names use the same format that javap emits.
12861335
<violation>
12871336
<name>java/io/FileReader."&lt;init&gt;":(Ljava/lang/String;Ljava/nio/charset/Charset;)V</name>
12881337
<version>11</version>
1289-
<comment>Prefer java.nio.file.Files.newBufferedReader.&lt;init&gt;(Paths.get(String), Charset)</comment>
1338+
<comment>Prefer java.nio.file.Files.newBufferedReader.&lt;init&gt;(Path.of(String), Charset)</comment>
12901339
</violation>
12911340

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

1360+
<violation>
1361+
<name>java/io/FileWriter."&lt;init&gt;":(Ljava/lang/String;)V</name>
1362+
<version>11</version>
1363+
<comment>Prefer java.nio.file.Files.newBufferedWriter.&lt;init&gt;(Path.of(String))</comment>
1364+
</violation>
1365+
13101366
<violation>
13111367
<name>java/io/FileWriter."&lt;init&gt;":(Ljava/lang/String;Z)V</name>
13121368
<version>7</version>
1369+
<until>11</until>
13131370
<comment>Prefer java.nio.file.Files.newBufferedWriter.&lt;init&gt;(Paths.get(String), java.nio.file.StandardOpenOption.CREATE, java.nio.file.StandardOpenOption.WRITE)</comment>
13141371
</violation>
13151372

1373+
<violation>
1374+
<name>java/io/FileWriter."&lt;init&gt;":(Ljava/lang/String;Z)V</name>
1375+
<version>11</version>
1376+
<comment>Prefer java.nio.file.Files.newBufferedWriter.&lt;init&gt;(Path.of(String), java.nio.file.StandardOpenOption.CREATE, java.nio.file.StandardOpenOption.WRITE)</comment>
1377+
</violation>
1378+
13161379
<violation>
13171380
<name>java/io/FileWriter."&lt;init&gt;":(Ljava/io/File;Ljava/nio/charset/Charset;)V</name>
13181381
<version>11</version>
@@ -1328,13 +1391,13 @@ violation names use the same format that javap emits.
13281391
<violation>
13291392
<name>java/io/FileWriter."&lt;init&gt;":(Ljava/lang/String;Ljava/nio/charset/Charset;)V</name>
13301393
<version>11</version>
1331-
<comment>Prefer java.nio.file.Files.newBufferedWriter.&lt;init&gt;(Paths.get(String), Charset)</comment>
1394+
<comment>Prefer java.nio.file.Files.newBufferedWriter.&lt;init&gt;(Path.of(String), Charset)</comment>
13321395
</violation>
13331396

13341397
<violation>
13351398
<name>java/io/FileWriter."&lt;init&gt;":(Ljava/lang/String;Ljava/nio/charset/Charset;Z)V</name>
13361399
<version>11</version>
1337-
<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>
1400+
<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>
13381401
</violation>
13391402

13401403
<violation>

modernizer-maven-plugin/src/test/java/org/gaul/modernizer_maven_plugin/ModernizerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ public void testAnnotationViolation() throws Exception {
390390
String name = TestAnnotation.class.getName().replace('.', '/');
391391
Map<String, Violation> testViolations = Maps.newHashMap();
392392
testViolations.put(name,
393-
new Violation(name, 5, ""));
393+
new Violation(name, 5, OptionalInt.empty(), ""));
394394
Modernizer modernizer = new Modernizer("1.5", testViolations,
395395
NO_EXCLUSIONS, NO_EXCLUSION_PATTERNS, NO_IGNORED_PACKAGES,
396396
NO_IGNORED_CLASS_NAMES, NO_EXCLUSION_PATTERNS);

0 commit comments

Comments
 (0)