[MSHARED-1240] Update class version detection for newer JDKs 9 - 21
diff --git a/src/main/java/org/apache/maven/shared/jar/classes/JarClassesAnalysis.java b/src/main/java/org/apache/maven/shared/jar/classes/JarClassesAnalysis.java
index ed78dd5..68e297a 100644
--- a/src/main/java/org/apache/maven/shared/jar/classes/JarClassesAnalysis.java
+++ b/src/main/java/org/apache/maven/shared/jar/classes/JarClassesAnalysis.java
@@ -22,7 +22,11 @@
import javax.inject.Singleton;
import java.io.IOException;
+import java.util.Collections;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
+import java.util.Optional;
import java.util.jar.JarEntry;
import org.apache.bcel.classfile.ClassFormatException;
@@ -45,24 +49,37 @@
*/
@Singleton
@Named
+@SuppressWarnings("checkstyle:MagicNumber")
public class JarClassesAnalysis {
private final Logger logger = LoggerFactory.getLogger(getClass());
- private static final double JAVA_1_8_CLASS_VERSION = 52.0;
+ private static final Map<Double, String> JAVA_CLASS_VERSIONS;
- private static final double JAVA_1_7_CLASS_VERSION = 51.0;
-
- private static final double JAVA_1_6_CLASS_VERSION = 50.0;
-
- private static final double JAVA_1_5_CLASS_VERSION = 49.0;
-
- private static final double JAVA_1_4_CLASS_VERSION = 48.0;
-
- private static final double JAVA_1_3_CLASS_VERSION = 47.0;
-
- private static final double JAVA_1_2_CLASS_VERSION = 46.0;
-
- private static final double JAVA_1_1_CLASS_VERSION = 45.3;
+ static {
+ HashMap<Double, String> aMap = new HashMap<>();
+ aMap.put(65.0, "21");
+ aMap.put(64.0, "20");
+ aMap.put(63.0, "19");
+ aMap.put(62.0, "18");
+ aMap.put(61.0, "17");
+ aMap.put(60.0, "16");
+ aMap.put(59.0, "15");
+ aMap.put(58.0, "14");
+ aMap.put(57.0, "13");
+ aMap.put(56.0, "12");
+ aMap.put(55.0, "11");
+ aMap.put(54.0, "10");
+ aMap.put(53.0, "9");
+ aMap.put(52.0, "1.8");
+ aMap.put(51.0, "1.7");
+ aMap.put(50.0, "1.6");
+ aMap.put(49.0, "1.5");
+ aMap.put(48.0, "1.4");
+ aMap.put(47.0, "1.3");
+ aMap.put(46.0, "1.2");
+ aMap.put(45.3, "1.1");
+ JAVA_CLASS_VERSIONS = Collections.unmodifiableMap(aMap);
+ }
/**
* Analyze a JAR and find any classes and their details. Note that if the provided JAR analyzer has previously
@@ -131,23 +148,7 @@
}
}
- if (maxVersion == JAVA_1_8_CLASS_VERSION) {
- classes.setJdkRevision("1.8");
- } else if (maxVersion == JAVA_1_7_CLASS_VERSION) {
- classes.setJdkRevision("1.7");
- } else if (maxVersion == JAVA_1_6_CLASS_VERSION) {
- classes.setJdkRevision("1.6");
- } else if (maxVersion == JAVA_1_5_CLASS_VERSION) {
- classes.setJdkRevision("1.5");
- } else if (maxVersion == JAVA_1_4_CLASS_VERSION) {
- classes.setJdkRevision("1.4");
- } else if (maxVersion == JAVA_1_3_CLASS_VERSION) {
- classes.setJdkRevision("1.3");
- } else if (maxVersion == JAVA_1_2_CLASS_VERSION) {
- classes.setJdkRevision("1.2");
- } else if (maxVersion == JAVA_1_1_CLASS_VERSION) {
- classes.setJdkRevision("1.1");
- }
+ Optional.ofNullable(JAVA_CLASS_VERSIONS.get(maxVersion)).ifPresent(classes::setJdkRevision);
jarAnalyzer.getJarData().setJarClasses(classes);
}
diff --git a/src/test/java/org/apache/maven/shared/jar/classes/JarClassesAnalyzerTest.java b/src/test/java/org/apache/maven/shared/jar/classes/JarClassesAnalyzerTest.java
index e2b9fd2..8c7bb2d 100644
--- a/src/test/java/org/apache/maven/shared/jar/classes/JarClassesAnalyzerTest.java
+++ b/src/test/java/org/apache/maven/shared/jar/classes/JarClassesAnalyzerTest.java
@@ -113,7 +113,19 @@
{"helloworld-1.5.jar", "1.5"},
{"helloworld-1.6.jar", "1.6"},
{"helloworld-1.7.jar", "1.7"},
- {"helloworld-1.8.jar", "1.8"}
+ {"helloworld-1.8.jar", "1.8"},
+ {"helloworld-9.jar", "9"},
+ {"helloworld-10.jar", "10"},
+ {"helloworld-11.jar", "11"},
+ {"helloworld-12.jar", "12"},
+ {"helloworld-13.jar", "13"},
+ {"helloworld-14.jar", "14"},
+ {"helloworld-15.jar", "15"},
+ {"helloworld-16.jar", "16"},
+ {"helloworld-17.jar", "17"},
+ {"helloworld-18.jar", "18"},
+ {"helloworld-19.jar", "19"},
+ {"helloworld-20.jar", "20"}
};
}
diff --git a/src/test/resources/jars/helloworld-10.jar b/src/test/resources/jars/helloworld-10.jar
new file mode 100644
index 0000000..9e7fbc9
--- /dev/null
+++ b/src/test/resources/jars/helloworld-10.jar
Binary files differ
diff --git a/src/test/resources/jars/helloworld-11.jar b/src/test/resources/jars/helloworld-11.jar
new file mode 100644
index 0000000..725b98c
--- /dev/null
+++ b/src/test/resources/jars/helloworld-11.jar
Binary files differ
diff --git a/src/test/resources/jars/helloworld-12.jar b/src/test/resources/jars/helloworld-12.jar
new file mode 100644
index 0000000..4587737
--- /dev/null
+++ b/src/test/resources/jars/helloworld-12.jar
Binary files differ
diff --git a/src/test/resources/jars/helloworld-13.jar b/src/test/resources/jars/helloworld-13.jar
new file mode 100644
index 0000000..c5363ce
--- /dev/null
+++ b/src/test/resources/jars/helloworld-13.jar
Binary files differ
diff --git a/src/test/resources/jars/helloworld-14.jar b/src/test/resources/jars/helloworld-14.jar
new file mode 100644
index 0000000..1a736e7
--- /dev/null
+++ b/src/test/resources/jars/helloworld-14.jar
Binary files differ
diff --git a/src/test/resources/jars/helloworld-15.jar b/src/test/resources/jars/helloworld-15.jar
new file mode 100644
index 0000000..d58f62a
--- /dev/null
+++ b/src/test/resources/jars/helloworld-15.jar
Binary files differ
diff --git a/src/test/resources/jars/helloworld-16.jar b/src/test/resources/jars/helloworld-16.jar
new file mode 100644
index 0000000..48e69d4
--- /dev/null
+++ b/src/test/resources/jars/helloworld-16.jar
Binary files differ
diff --git a/src/test/resources/jars/helloworld-17.jar b/src/test/resources/jars/helloworld-17.jar
new file mode 100644
index 0000000..2126148
--- /dev/null
+++ b/src/test/resources/jars/helloworld-17.jar
Binary files differ
diff --git a/src/test/resources/jars/helloworld-18.jar b/src/test/resources/jars/helloworld-18.jar
new file mode 100644
index 0000000..c14c464
--- /dev/null
+++ b/src/test/resources/jars/helloworld-18.jar
Binary files differ
diff --git a/src/test/resources/jars/helloworld-19.jar b/src/test/resources/jars/helloworld-19.jar
new file mode 100644
index 0000000..832d55f
--- /dev/null
+++ b/src/test/resources/jars/helloworld-19.jar
Binary files differ
diff --git a/src/test/resources/jars/helloworld-20.jar b/src/test/resources/jars/helloworld-20.jar
new file mode 100644
index 0000000..2a14c37
--- /dev/null
+++ b/src/test/resources/jars/helloworld-20.jar
Binary files differ
diff --git a/src/test/resources/jars/helloworld-9.jar b/src/test/resources/jars/helloworld-9.jar
new file mode 100644
index 0000000..8c09626
--- /dev/null
+++ b/src/test/resources/jars/helloworld-9.jar
Binary files differ
diff --git a/src/test/resources/jars/src-helloWorld/net/test/HelloWorld.java b/src/test/resources/jars/src-helloWorld/net/test/HelloWorld.java
new file mode 100644
index 0000000..ca7bf8f
--- /dev/null
+++ b/src/test/resources/jars/src-helloWorld/net/test/HelloWorld.java
@@ -0,0 +1,26 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package net.test;
+
+public class HelloWorld {
+ public static void main(String[] var0) {
+ System.out.println("Hello, World!");
+ }
+}
diff --git a/src/test/resources/jars/src-helloWorld/prepare.sh b/src/test/resources/jars/src-helloWorld/prepare.sh
new file mode 100755
index 0000000..8b483d4
--- /dev/null
+++ b/src/test/resources/jars/src-helloWorld/prepare.sh
@@ -0,0 +1,25 @@
+#!/bin/sh
+
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+echo "Prepare test jar for release $1"
+
+javac --release $1 -d classes net/test/HelloWorld.java
+jar --create --file helloworld-$1.jar -C classes .
+mv helloworld-$1.jar ..
+rm -rf classes