From 8ef8d4c898ee53f799c89cd66152606b53cb03cf Mon Sep 17 00:00:00 2001 From: George Gastaldi Date: Thu, 16 Jul 2020 10:44:07 -0300 Subject: [PATCH] Added JDK 9 version for JarEntries.getRealName --- io/pom.xml | 17 +++++++++ .../io/smallrye/common/io/jar/JarEntries.java | 36 +++++++++++++++++++ ...{JarEntriesTest.java => JarEntriesIT.java} | 5 +-- 3 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 io/src/main/java9/io/smallrye/common/io/jar/JarEntries.java rename io/src/test/java/io/smallrye/common/io/jar/{JarEntriesTest.java => JarEntriesIT.java} (74%) diff --git a/io/pom.xml b/io/pom.xml index 328fe83f..46da06f5 100644 --- a/io/pom.xml +++ b/io/pom.xml @@ -26,6 +26,23 @@ test + + + + org.apache.maven.plugins + maven-failsafe-plugin + 3.0.0-M5 + + + + integration-test + verify + + + + + + coverage diff --git a/io/src/main/java9/io/smallrye/common/io/jar/JarEntries.java b/io/src/main/java9/io/smallrye/common/io/jar/JarEntries.java new file mode 100644 index 00000000..3697d6ba --- /dev/null +++ b/io/src/main/java9/io/smallrye/common/io/jar/JarEntries.java @@ -0,0 +1,36 @@ +package io.smallrye.common.io.jar; + +import java.lang.reflect.Method; +import java.util.jar.JarEntry; + +public class JarEntries { + + private static final Method REAL_NAME_METHOD; + + static { + Method method; + try { + method = Class.forName("java.util.jar.JarFile$JarFileEntry").getDeclaredMethod("realName"); + method.setAccessible(true); + } catch (NoSuchMethodException | ClassNotFoundException e) { + method = null; + } + REAL_NAME_METHOD = method; + } + + /** + * Returns the real name of this {@link JarEntry}. On Java 8, it returns the {@link JarEntry#getName()} + * On Java 10+, a getRealName() method was added + */ + public static String getRealName(JarEntry jarEntry) { + if (REAL_NAME_METHOD != null) { + try { + return REAL_NAME_METHOD.invoke(jarEntry).toString(); + } catch (Exception e) { + // This should never happen + } + } + // As a safe net, fallback to the original value + return jarEntry.getName(); + } +} diff --git a/io/src/test/java/io/smallrye/common/io/jar/JarEntriesTest.java b/io/src/test/java/io/smallrye/common/io/jar/JarEntriesIT.java similarity index 74% rename from io/src/test/java/io/smallrye/common/io/jar/JarEntriesTest.java rename to io/src/test/java/io/smallrye/common/io/jar/JarEntriesIT.java index 1ff4430d..973b3ec2 100644 --- a/io/src/test/java/io/smallrye/common/io/jar/JarEntriesTest.java +++ b/io/src/test/java/io/smallrye/common/io/jar/JarEntriesIT.java @@ -9,14 +9,15 @@ import org.junit.Test; -public class JarEntriesTest { +// This needs to be run as an integration-test +public class JarEntriesIT { @Test public void shouldUseRealName() throws IOException { File tmpFile = MultiReleaseJarGenerator.generateMultiReleaseJar(); JarFile jarFile = JarFiles.create(tmpFile); JarEntry jarEntry = jarFile.getJarEntry("foo.txt"); - assertEquals(jarEntry.getRealName(), JarEntries.getRealName(jarEntry)); + assertEquals("META-INF/versions/9/foo.txt", JarEntries.getRealName(jarEntry)); } }