Skip to content

Commit 784e6e1

Browse files
committed
add version comparator
1 parent 11775ea commit 784e6e1

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

src/main/java/de/thetaphi/forbiddenapis/Constants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public interface Constants {
2727
final String BS_JDK_NONPORTABLE = "jdk-non-portable";
2828

2929
final Pattern JDK_SIG_PATTERN = Pattern.compile("(jdk\\-.*?\\-)(\\d+)(\\.\\d+)?(\\.\\d+)*");
30+
final Pattern ENDS_WITH_VERSION_PATTERN = Pattern.compile("(.*?)\\-(\\d+(\\.\\d+)*)");
3031

3132
final String DEPRECATED_WARN_FAIL_ON_UNRESOLVABLE_SIGNATURES =
3233
"The setting 'failOnUnresolvableSignatures' was deprecated and will be removed in next version. Use 'ignoreSignaturesOfMissingClasses' instead.";

src/main/java/de/thetaphi/forbiddenapis/Signatures.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public final class Signatures implements Constants {
6464
static {
6565
try (final InputStream in = Checker.class.getResourceAsStream("signatures/list");
6666
final BufferedReader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8))) {
67-
final SortedSet<String> names = new TreeSet<String>();
67+
final SortedSet<String> names = new TreeSet<String>(VersionCompare.BUNDLED_SIGNATURES_COMPARATOR);
6868
String name;
6969
while ((name = reader.readLine()) != null) {
7070
names.add(name);
@@ -302,7 +302,7 @@ private void addBundledSignatures(String name, String jdkTargetVersion, boolean
302302
if (readExternal) {
303303
name = fixTargetVersion(name);
304304
// automatically expand the compiler version in here (for jdk-* signatures without version):
305-
if (!BUNDLED_SIGNATURES_NAMES.contains(name) && jdkTargetVersion != null && name.startsWith("jdk-") && !name.matches(".*?\\-\\d+(\\.\\d+)*")) {
305+
if (!BUNDLED_SIGNATURES_NAMES.contains(name) && jdkTargetVersion != null && name.startsWith("jdk-") && !ENDS_WITH_VERSION_PATTERN.matcher(name).matches()) {
306306
name = name + "-" + jdkTargetVersion;
307307
name = fixTargetVersion(name);
308308
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* (C) Copyright Uwe Schindler (Generics Policeman) and others.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package de.thetaphi.forbiddenapis;
18+
19+
import java.util.Comparator;
20+
import java.util.regex.Matcher;
21+
import java.util.regex.Pattern;
22+
23+
/** Some static utilities for analyzing with ASM, also constants. */
24+
final class VersionCompare {
25+
26+
private static final Pattern DOT_SPLITTER_PATTERN = Pattern.compile("\\.");
27+
28+
private VersionCompare() {}
29+
30+
public static int compareVersions(String version1, String version2) {
31+
final String[] version1Splits = DOT_SPLITTER_PATTERN.split(version1),
32+
version2Splits = DOT_SPLITTER_PATTERN.split(version2);
33+
final int maxLengthOfVersionSplits = Math.max(version1Splits.length, version2Splits.length);
34+
35+
for (int i = 0; i < maxLengthOfVersionSplits; i++) {
36+
final int v1 = i < version1Splits.length ? Integer.parseInt(version1Splits[i]) : 0;
37+
final int v2 = i < version2Splits.length ? Integer.parseInt(version2Splits[i]) : 0;
38+
final int compare = Integer.compare(v1, v2);
39+
if (compare != 0) {
40+
return compare;
41+
}
42+
}
43+
return 0;
44+
}
45+
46+
public static Comparator<String> VERSION_COMPARATOR = new Comparator<String>() {
47+
@Override
48+
public int compare(String version1, String version2) {
49+
return compareVersions(version1, version2);
50+
}
51+
};
52+
53+
public static Comparator<String> BUNDLED_SIGNATURES_COMPARATOR = new Comparator<String>() {
54+
@Override
55+
public int compare(String bs1, String bs2) {
56+
final Matcher m1 = Constants.ENDS_WITH_VERSION_PATTERN.matcher(bs1),
57+
m2 = Constants.ENDS_WITH_VERSION_PATTERN.matcher(bs2);
58+
if (m1.matches() && m2.matches()) {
59+
final int prefixCmp = m1.group(1).compareTo(m2.group(1));
60+
if (prefixCmp != 0) {
61+
return prefixCmp;
62+
}
63+
return compareVersions(m1.group(2), m2.group(2));
64+
} else {
65+
return bs1.compareTo(bs2);
66+
}
67+
}
68+
};
69+
70+
}

0 commit comments

Comments
 (0)